forked from libbitcoin/libbitcoin-network
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget_data.cpp
More file actions
194 lines (157 loc) · 5.04 KB
/
get_data.cpp
File metadata and controls
194 lines (157 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/network/messages/peer/detail/get_data.hpp>
#include <algorithm>
#include <bitcoin/network/messages/peer/enums/identifier.hpp>
#include <bitcoin/network/messages/peer/enums/level.hpp>
#include <bitcoin/network/messages/peer/enums/magic_numbers.hpp>
#include <bitcoin/network/messages/peer/detail/inventory_item.hpp>
#include <bitcoin/network/messages/peer/message.hpp>
namespace libbitcoin {
namespace network {
namespace messages {
namespace peer {
using namespace system;
// filtered_block flag allowed by bip37.
const std::string get_data::command = "getdata";
const identifier get_data::id = identifier::get_data;
const uint32_t get_data::version_minimum = level::minimum_protocol;
const uint32_t get_data::version_maximum = level::maximum_protocol;
// static
typename get_data::cptr get_data::deserialize(uint32_t version,
const system::data_chunk& data) NOEXCEPT
{
system::istream source{ data };
system::byte_reader reader{ source };
const auto message = to_shared(deserialize(version, reader));
return reader ? message : nullptr;
}
// static
get_data get_data::deserialize(uint32_t version, reader& source) NOEXCEPT
{
if (version < version_minimum || version > version_maximum)
source.invalidate();
const auto size = source.read_size(max_inventory);
get_data get{};
get.items.reserve(size);
for (size_t item{}; item < size; ++item)
get.items.push_back(item::deserialize(version, source));
return get;
}
bool get_data::serialize(uint32_t version,
const system::data_slab& data) const NOEXCEPT
{
system::ostream sink{ data };
system::byte_writer writer{ sink };
serialize(version, writer);
return writer;
}
void get_data::serialize(uint32_t version, writer& sink) const NOEXCEPT
{
BC_DEBUG_ONLY(const auto bytes = size(version);)
BC_DEBUG_ONLY(const auto start = sink.get_write_position();)
sink.write_variable(items.size());
for (const auto& item: items)
item.serialize(version, sink);
BC_ASSERT(sink && sink.get_write_position() - start == bytes);
}
size_t get_data::size(uint32_t version) const NOEXCEPT
{
return variable_size(items.size()) +
(items.size() * item::size(version));
}
inventory_items get_data::select(selector types) const NOEXCEPT
{
inventory_items out{};
out.reserve(count(types));
for (const auto& item: items)
if (item.is_selected(types))
out.push_back(item);
return out;
}
inventory_items get_data::filter(type_id type) const NOEXCEPT
{
inventory_items out{};
out.reserve(count(type));
for (const auto& item: items)
if (item.type == type)
out.push_back(item);
return out;
}
hashes get_data::to_hashes(type_id type) const NOEXCEPT
{
hashes out{};
out.reserve(count(type));
for (const auto& item: items)
if (item.type == type)
out.push_back(item.hash);
return out;
}
size_t get_data::count(selector type) const NOEXCEPT
{
const auto is_selected = [type](const item& item)
{
return item.is_selected(type);
};
return std::count_if(items.begin(), items.end(), is_selected);
}
size_t get_data::count(type_id type) const NOEXCEPT
{
const auto is_type = [type](const item& item)
{
return item.is_type(type);
};
return std::count_if(items.begin(), items.end(), is_type);
}
bool get_data::any(type_id type) const NOEXCEPT
{
const auto is_type = [type](const item& item)
{
return item.is_type(type);
};
return std::any_of(items.begin(), items.end(), is_type);
}
bool get_data::any_transaction() const NOEXCEPT
{
const auto is_transaction = [](const item& item)
{
return item.is_transaction_type();
};
return std::any_of(items.begin(), items.end(), is_transaction);
}
bool get_data::any_block() const NOEXCEPT
{
const auto is_block = [](const item& item)
{
return item.is_block_type();
};
return std::any_of(items.begin(), items.end(), is_block);
}
bool get_data::any_witness() const NOEXCEPT
{
const auto is_witness = [](const item& item)
{
return item.is_witness_type();
};
return std::any_of(items.begin(), items.end(), is_witness);
}
} // namespace peer
} // namespace messages
} // namespace network
} // namespace libbitcoin