veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
net_entities.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BFI_BITCOIN_NET_NET_ENTITIES_HPP
7#define BFI_BITCOIN_NET_NET_ENTITIES_HPP
8
9#include <veriblock/bfi/bitcoin/block.hpp>
10#include <veriblock/bfi/bitcoin/serialize.hpp>
11
12namespace altintegration {
13
14namespace btc {
15
17enum ServiceFlags : uint64_t {
18 // NOTE: When adding here, be sure to update qt/guiutil.cpp's
19 // formatServicesStr too
20 // Nothing
21 NODE_NONE = 0,
22 // NODE_NETWORK means that the node is capable of serving the complete block
23 // chain. It is currently
24 // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or
25 // other light clients.
26 NODE_NETWORK = (1 << 0),
27 // NODE_GETUTXO means the node is capable of responding to the getutxo
28 // protocol request.
29 // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
30 // See BIP 64 for details on how this is implemented.
31 NODE_GETUTXO = (1 << 1),
32 // NODE_BLOOM means the node is capable and willing to handle bloom-filtered
33 // connections.
34 // Bitcoin Core nodes used to support this by default, without advertising
35 // this bit,
36 // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
37 NODE_BLOOM = (1 << 2),
38 // NODE_WITNESS indicates that a node can be asked for blocks and transactions
39 // including
40 // witness data.
41 NODE_WITNESS = (1 << 3),
42 // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of
43 // only
44 // serving the last 288 (2 day) blocks
45 // See BIP159 for details on how this is implemented.
46 NODE_NETWORK_LIMITED = (1 << 10),
47
48 // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
49 // isn't getting used, or one not being used much, and notify the
50 // bitcoin-development mailing list. Remember that service bits are just
51 // unauthenticated advertisements, so your code must be robust against
52 // collisions and other cases where nodes may be advertising a service they
53 // do not actually support. Other service bits should be allocated via the
54 // BIP process.
55};
56
58struct NetAddr {
59 // in network byte order
60 uint8_t ip[16];
61
62 ADD_SERIALIZE_METHODS;
63
64 template <typename Stream, typename Operation>
65 inline void SerializationOp(Stream& s, Operation ser_action) {
66 READWRITE(this->ip);
67 }
68
69 friend bool operator==(const NetAddr& a, const NetAddr& b) {
70 return (memcmp(a.ip, b.ip, 16) == 0);
71 }
72 friend bool operator!=(const NetAddr& a, const NetAddr& b) {
73 return !(a == b);
74 }
75};
76
77struct SubNet {
81 uint8_t netmask[16];
83 bool valid;
84
85 ADD_SERIALIZE_METHODS;
86
87 template <typename Stream, typename Operation>
88 inline void SerializationOp(Stream& s, Operation ser_action) {
89 READWRITE(this->network);
90 READWRITE(this->netmask);
91 READWRITE(this->valid);
92 }
93
94 friend bool operator==(const SubNet& a, const SubNet& b) {
95 return a.valid == b.valid && a.network == b.network &&
96 0 == memcmp(a.netmask, b.netmask, 16);
97 }
98 friend bool operator!=(const SubNet& a, const SubNet& b) { return !(a == b); }
99};
100
102struct Service : public NetAddr {
103 uint16_t port; // host order
104
105 ADD_SERIALIZE_METHODS;
106
107 template <typename Stream, typename Operation>
108 inline void SerializationOp(Stream& s, Operation ser_action) {
109 READWRITEAS(NetAddr, *this);
110 READWRITE(WrapBigEndian(this->port));
111 }
112
113 friend bool operator==(const Service& a, const Service& b) {
114 return a.port == b.port && ((NetAddr&)a == (NetAddr&)b);
115 }
116 friend bool operator!=(const Service& a, const Service& b) {
117 return !(a == b);
118 }
119};
120
121struct Address : public Service {
122 ServiceFlags nServices;
123 // disk and network only
124 uint32_t nTime;
125
126 ADD_SERIALIZE_METHODS;
127
128 template <typename Stream, typename Operation>
129 inline void SerializationOp(Stream& s, Operation ser_action) {
130 uint64_t nServicesInt = nServices;
131 READWRITE(nTime);
132 READWRITE(nServicesInt);
133 nServices = static_cast<ServiceFlags>(nServicesInt);
134 READWRITEAS(Service, *this);
135 }
136
137 friend bool operator==(const Address& a, const Address& b) {
138 return (a.nTime == b.nTime) && ((Service&)a == (Service&)b);
139 }
140 friend bool operator!=(const Address& a, const Address& b) {
141 return !(a == b);
142 }
143};
144
145struct BanEntry {
146 int32_t nVersion;
147 int64_t nCreateTime;
148 int64_t nBanUntil;
149 uint8_t banReason;
150
151 ADD_SERIALIZE_METHODS;
152
153 template <typename Stream, typename Operation>
154 inline void SerializationOp(Stream& s, Operation ser_action) {
155 READWRITE(this->nVersion);
156 READWRITE(this->nCreateTime);
157 READWRITE(this->nBanUntil);
158 READWRITE(this->banReason);
159 }
160
161 friend bool operator==(const BanEntry& a, const BanEntry& b) {
162 return a.nVersion == b.nVersion && a.nCreateTime == b.nCreateTime &&
163 a.nBanUntil == b.nBanUntil && a.banReason == b.banReason;
164 }
165 friend bool operator!=(const BanEntry& a, const BanEntry& b) {
166 return !(a == b);
167 }
168};
169
170struct Inv {
171 int32_t type;
172 uint256 hash;
173
174 ADD_SERIALIZE_METHODS;
175
176 template <typename Stream, typename Operation>
177 inline void SerializationOp(Stream& s, Operation ser_action) {
178 READWRITE(this->type);
179 READWRITE(this->hash);
180 }
181
182 friend bool operator==(const Inv& a, const Inv& b) {
183 return a.type == b.type && a.hash == b.hash;
184 }
185 friend bool operator!=(const Inv& a, const Inv& b) { return !(a == b); }
186};
187
189 uint256 blockhash;
190 std::vector<uint16_t> indexes;
191
192 ADD_SERIALIZE_METHODS;
193
194 template <typename Stream, typename Operation>
195 inline void SerializationOp(Stream& s, Operation ser_action) {
196 READWRITE(this->blockhash);
197 uint64_t indexes_size = (uint64_t)this->indexes.size();
198 READWRITE(COMPACTSIZE(indexes_size));
199 if (ser_action.ForRead()) {
200 size_t i = 0;
201 while (indexes.size() < indexes_size) {
202 this->indexes.resize(
203 std::min((uint64_t)(1000 + this->indexes.size()), indexes_size));
204 for (; i < this->indexes.size(); ++i) {
205 uint64_t index = 0;
206 READWRITE(COMPACTSIZE(index));
207 if (index > std::numeric_limits<uint16_t>::max()) {
208 throw std::ios_base::failure("index overflowed 16 bits");
209 }
210 this->indexes[i] = (uint16_t)index;
211 }
212 }
213
214 int32_t offset = 0;
215 for (size_t j = 0; j < this->indexes.size(); j++) {
216 if (int32_t(this->indexes[j]) + offset >
217 std::numeric_limits<uint16_t>::max())
218 throw std::ios_base::failure("indexes overflowed 16 bits");
219 this->indexes[j] = this->indexes[j] + (uint16_t)offset;
220 offset = int32_t(this->indexes[j]) + 1;
221 }
222 } else {
223 for (size_t i = 0; i < this->indexes.size(); i++) {
224 uint64_t index =
225 this->indexes[i] - (i == 0 ? 0 : (this->indexes[i - 1] + 1));
226 READWRITE(COMPACTSIZE(index));
227 }
228 }
229 }
230
231 friend bool operator==(const BlockTransactionsRequest& a,
232 const BlockTransactionsRequest& b) {
233 return a.blockhash == b.blockhash && a.indexes == b.indexes;
234 }
235 friend bool operator!=(const BlockTransactionsRequest& a,
236 const BlockTransactionsRequest& b) {
237 return !(a == b);
238 }
239};
240
241// Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and
242// PartiallyDownloadedBlock
244 // Used as an offset since last prefilled tx in BlockHeaderAndShortTxIDs,
245 // as a proper transaction-in-block-index in PartiallyDownloadedBlock
246 uint16_t index;
247 Transaction tx;
248
249 ADD_SERIALIZE_METHODS;
250
251 template <typename Stream, typename Operation>
252 inline void SerializationOp(Stream& s, Operation ser_action) {
253 uint64_t idx = this->index;
254 READWRITE(COMPACTSIZE(idx));
255 if (idx > std::numeric_limits<uint16_t>::max()) {
256 throw std::ios_base::failure("index overflowed 16-bits");
257 }
258 index = (uint16_t)idx;
259 READWRITE(this->tx);
260 }
261
262 friend bool operator==(const PrefilledTransaction& a,
263 const PrefilledTransaction& b) {
264 return a.index == b.index && a.tx == b.tx;
265 }
266 friend bool operator!=(const PrefilledTransaction& a,
267 const PrefilledTransaction& b) {
268 return !(a == b);
269 }
270};
271
273 static const uint32_t SHORTTXIDS_LENGTH = 6;
274
275 BlockHeader header;
276 uint64_t nonce;
277 std::vector<uint64_t> shorttxids;
278 std::vector<PrefilledTransaction> prefilledtxn;
279
280 ADD_SERIALIZE_METHODS;
281
282 template <typename Stream, typename Operation>
283 inline void SerializationOp(Stream& s, Operation ser_action) {
284 READWRITE(this->header);
285 READWRITE(this->nonce);
286
287 uint64_t shorttxids_size = (uint64_t)this->shorttxids.size();
288 READWRITE(COMPACTSIZE(shorttxids_size));
289 if (ser_action.ForRead()) {
290 size_t i = 0;
291 while (this->shorttxids.size() < shorttxids_size) {
292 this->shorttxids.resize(std::min(
293 (uint64_t)(1000 + this->shorttxids.size()), shorttxids_size));
294 for (; i < shorttxids.size(); i++) {
295 uint32_t lsb = 0;
296 uint16_t msb = 0;
297 READWRITE(lsb);
298 READWRITE(msb);
299 this->shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
300 }
301 }
302 } else {
303 for (size_t i = 0; i < this->shorttxids.size(); i++) {
304 uint32_t lsb = this->shorttxids[i] & 0xffffffff;
305 uint16_t msb = (this->shorttxids[i] >> 32) & 0xffff;
306 READWRITE(lsb);
307 READWRITE(msb);
308 }
309 }
310
311 READWRITE(this->prefilledtxn);
312
313 if (this->shorttxids.size() + this->prefilledtxn.size() >
314 std::numeric_limits<uint16_t>::max()) {
315 throw std::ios_base::failure("indexes overflowed 16 bits");
316 }
317 }
318
319 friend bool operator==(const BlockHeaderAndShortTxIDs& a,
320 const BlockHeaderAndShortTxIDs& b) {
321 return a.header == b.header && a.nonce == b.nonce &&
322 a.shorttxids == b.shorttxids && a.prefilledtxn == b.prefilledtxn;
323 }
324 friend bool operator!=(const BlockHeaderAndShortTxIDs& a,
325 const BlockHeaderAndShortTxIDs& b) {
326 return !(a == b);
327 }
328};
329
331 // A BlockTransactions message
332 uint256 blockhash;
333 std::vector<Transaction> txn;
334
335 ADD_SERIALIZE_METHODS;
336
337 template <typename Stream, typename Operation>
338 inline void SerializationOp(Stream& s, Operation ser_action) {
339 READWRITE(this->blockhash);
340 uint64_t txn_size = (uint64_t)this->txn.size();
341 READWRITE(COMPACTSIZE(txn_size));
342 if (ser_action.ForRead()) {
343 size_t i = 0;
344 while (this->txn.size() < txn_size) {
345 this->txn.resize(
346 std::min((uint64_t)(1000 + this->txn.size()), txn_size));
347 for (; i < this->txn.size(); i++) {
348 READWRITE(this->txn[i]);
349 }
350 }
351 } else {
352 for (size_t i = 0; i < txn.size(); i++) {
353 READWRITE(this->txn[i]);
354 }
355 }
356 }
357
358 friend bool operator==(const BlockTransactions& a,
359 const BlockTransactions& b) {
360 return a.blockhash == b.blockhash && a.txn == b.txn;
361 }
362 friend bool operator!=(const BlockTransactions& a,
363 const BlockTransactions& b) {
364 return !(a == b);
365 }
366};
367
369 std::vector<uint8_t> vData;
370 uint32_t nHashFuncs;
371 uint32_t nTweak;
372 uint8_t nFlags;
373
374 ADD_SERIALIZE_METHODS;
375
376 template <typename Stream, typename Operation>
377 inline void SerializationOp(Stream& s, Operation ser_action) {
378 READWRITE(this->vData);
379 READWRITE(this->nHashFuncs);
380 READWRITE(this->nTweak);
381 READWRITE(this->nFlags);
382 }
383
384 friend bool operator==(const BloomFilter& a, const BloomFilter& b) {
385 return a.vData == b.vData && a.nHashFuncs == b.nHashFuncs &&
386 a.nTweak == b.nTweak && a.nFlags == b.nFlags;
387 }
388 friend bool operator!=(const BloomFilter& a, const BloomFilter& b) {
389 return !(a == b);
390 }
391};
392
393} // namespace btc
394
395} // namespace altintegration
396
397#endif
Defines logging helpers.
Definition: block.hpp:14
IP address (IPv6, or IPv4 using mapped IPv6 range.
A combination of a network address (CNetAddr) and a (TCP) port.
bool valid
Is this value valid? (only used to signal parse errors)
NetAddr network
Network (base) address.
uint8_t netmask[16]
Netmask, in network byte order.
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.hpp:98