PIVX Core  5.6.99
P2P Digital Currency
protocol.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin developers
3 // Copyright (c) 2014-2021 The Dash Core developers
4 // Copyright (c) 2016-2022 The PIVX Core developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef __cplusplus
9 #error This header can only be compiled as C++.
10 #endif
11 
12 #ifndef PIVX_PROTOCOL_H
13 #define PIVX_PROTOCOL_H
14 
15 #include "netaddress.h"
16 #include "serialize.h"
17 #include "streams.h"
18 #include "uint256.h"
19 #include "version.h"
20 
21 #include <stdint.h>
22 #include <string>
23 
31 {
32 public:
33  static constexpr size_t MESSAGE_START_SIZE = 4;
34  static constexpr size_t COMMAND_SIZE = 12;
35  static constexpr size_t MESSAGE_SIZE_SIZE = 4;
36  static constexpr size_t CHECKSUM_SIZE = 4;
40  typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
41 
42  explicit CMessageHeader(const MessageStartChars& pchMessageStartIn);
43 
47  CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
48 
49  std::string GetCommand() const;
50  bool IsValid(const MessageStartChars& messageStart) const;
51 
52  SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
53 
56  uint32_t nMessageSize;
58 };
59 
64 namespace NetMsgType
65 {
71 extern const char* VERSION;
77 extern const char* VERACK;
83 extern const char* ADDR;
89 extern const char *ADDRV2;
95 extern const char *SENDADDRV2;
101 extern const char* INV;
106 extern const char* GETDATA;
113 extern const char* MERKLEBLOCK;
119 extern const char* GETBLOCKS;
126 extern const char* GETHEADERS;
131 extern const char* TX;
138 extern const char* HEADERS;
143 extern const char* BLOCK;
149 extern const char* GETADDR;
156 extern const char* MEMPOOL;
162 extern const char* PING;
169 extern const char* PONG;
176 extern const char* ALERT;
183 extern const char* NOTFOUND;
192 extern const char* FILTERLOAD;
201 extern const char* FILTERADD;
210 extern const char* FILTERCLEAR;
217 extern const char* SENDHEADERS;
222 extern const char* SPORK;
226 extern const char* GETSPORKS;
230 extern const char* MNBROADCAST;
235 extern const char* MNBROADCAST2;
239 extern const char* MNPING;
244 extern const char* MNWINNER;
248 extern const char* GETMNWINNERS;
252 extern const char* GETMNLIST;
256 extern const char* BUDGETPROPOSAL;
260 extern const char* BUDGETVOTE;
264 extern const char* BUDGETVOTESYNC;
268 extern const char* FINALBUDGET;
272 extern const char* FINALBUDGETVOTE;
276 extern const char* SYNCSTATUSCOUNT;
280 extern const char* QFCOMMITMENT;
284 extern const char* QSENDRECSIGS;
288 extern const char* MNAUTH;
289 /*
290  * Messages for LLMQ-DKG inter-quorum communication
291  */
292 extern const char* QCONTRIB;
293 extern const char* QCOMPLAINT;
294 extern const char* QJUSTIFICATION;
295 extern const char* QPCOMMITMENT;
296 extern const char* QSIGSESANN;
297 extern const char* QSIGSHARESINV;
298 extern const char* QGETSIGSHARES;
299 extern const char* QBSIGSHARES;
300 extern const char* QSIGREC;
301 extern const char* QSIGSHARE;
302 extern const char* CLSIG;
303 }; // namespace NetMsgType
304 
305 /* Get a vector of all valid message types (see above) */
306 const std::vector<std::string>& getAllNetMessageTypes();
307 
308 /* Get a vector of all tier two valid message types (see above) */
309 const std::vector<std::string>& getTierTwoNetMessageTypes();
310 
312 enum ServiceFlags : uint64_t {
313  // Nothing
315  // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
316  // set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
317  // network services but don't provide them.
318  NODE_NETWORK = (1 << 0),
319 
320  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
321  NODE_BLOOM = (1 << 2),
322 
323  // NODE_BLOOM_WITHOUT_MN means the node has the same features as NODE_BLOOM with the only difference
324  // that the node doesn't want to receive master nodes messages. (the 1<<3 was not picked as constant because on bitcoin 0.14 is witness and we want that update here )
326 
327  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
328  // isn't getting used, or one not being used much, and notify the
329  // bitcoin-development mailing list. Remember that service bits are just
330  // unauthenticated advertisements, so your code must be robust against
331  // collisions and other cases where nodes may be advertising a service they
332  // do not actually support. Other service bits should be allocated via the
333  // BIP process.
334 };
335 
337 class CAddress : public CService
338 {
339  static constexpr uint32_t TIME_INIT{100000000};
340 
357  static constexpr uint32_t DISK_VERSION_INIT{220000};
358  static constexpr uint32_t DISK_VERSION_IGNORE_MASK{0b00000000'00000111'11111111'11111111};
362  static constexpr uint32_t DISK_VERSION_ADDRV2{1 << 29};
363  static_assert((DISK_VERSION_INIT & ~DISK_VERSION_IGNORE_MASK) == 0, "DISK_VERSION_INIT must be covered by DISK_VERSION_IGNORE_MASK");
364  static_assert((DISK_VERSION_ADDRV2 & DISK_VERSION_IGNORE_MASK) == 0, "DISK_VERSION_ADDRV2 must not be covered by DISK_VERSION_IGNORE_MASK");
365 
366 public:
367  CAddress() : CService{} {};
368  CAddress(CService ipIn, ServiceFlags nServicesIn) : CService{ipIn}, nServices{nServicesIn} {};
369  CAddress(CService ipIn, ServiceFlags nServicesIn, uint32_t nTimeIn) : CService{ipIn}, nTime{nTimeIn}, nServices{nServicesIn} {};
370 
372  {
373  // CAddress has a distinct network serialization and a disk serialization, but it should never
374  // be hashed (except through CHashWriter in addrdb.cpp, which sets SER_DISK), and it's
375  // ambiguous what that would mean. Make sure no code relying on that is introduced:
376  assert(!(s.GetType() & SER_GETHASH));
377  bool use_v2;
378  bool store_time;
379  if (s.GetType() & SER_DISK) {
380  // In the disk serialization format, the encoding (v1 or v2) is determined by a flag version
381  // that's part of the serialization itself. ADDRV2_FORMAT in the stream version only determines
382  // whether V2 is chosen/permitted at all.
383  uint32_t stored_format_version = DISK_VERSION_INIT;
384  if (s.GetVersion() & ADDRV2_FORMAT) stored_format_version |= DISK_VERSION_ADDRV2;
385  READWRITE(stored_format_version);
386  stored_format_version &= ~DISK_VERSION_IGNORE_MASK; // ignore low bits
387  if (stored_format_version == 0) {
388  use_v2 = false;
389  } else if (stored_format_version == DISK_VERSION_ADDRV2 && (s.GetVersion() & ADDRV2_FORMAT)) {
390  // Only support v2 deserialization if ADDRV2_FORMAT is set.
391  use_v2 = true;
392  } else {
393  throw std::ios_base::failure("Unsupported CAddress disk format version");
394  }
395  store_time = true;
396  } else {
397  // In the network serialization format, the encoding (v1 or v2) is determined directly by
398  // the value of ADDRV2_FORMAT in the stream version, as no explicitly encoded version
399  // exists in the stream.
400  assert(s.GetType() & SER_NETWORK);
401  use_v2 = s.GetVersion() & ADDRV2_FORMAT;
402  // The only time we serialize a CAddress object without nTime is in
403  // the initial VERSION messages which contain two CAddress records.
404  // At that point, the serialization version is INIT_PROTO_VERSION.
405  // After the version handshake, serialization version is >=
406  // MIN_PEER_PROTO_VERSION and all ADDR messages are serialized with
407  // nTime.
408  store_time = s.GetVersion() != INIT_PROTO_VERSION;
409  }
410 
411  SER_READ(obj, obj.nTime = TIME_INIT);
412  if (store_time) READWRITE(obj.nTime);
413  // nServices is serialized as CompactSize in V2; as uint64_t in V1.
414  if (use_v2) {
415  uint64_t services_tmp;
416  SER_WRITE(obj, services_tmp = obj.nServices);
417  READWRITE(Using<CompactSizeFormatter<false>>(services_tmp));
418  SER_READ(obj, obj.nServices = static_cast<ServiceFlags>(services_tmp));
419  } else {
420  READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));
421  }
422  // Invoke V1/V2 serializer for CService parent object.
423  OverrideStream<Stream> os(&s, s.GetType(), use_v2 ? ADDRV2_FORMAT : 0);
424  SerReadWriteMany(os, ser_action, ReadWriteAsHelper<CService>(obj));
425  }
426 
428  uint32_t nTime{TIME_INIT};
431 };
432 
438  // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
439  // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
441  MSG_TXLOCK_REQUEST, // Deprecated
442  MSG_TXLOCK_VOTE, // Deprecated
453  MSG_DSTX, // Deprecated
462 };
463 
465 class CInv
466 {
467 public:
468  CInv();
469  CInv(int typeIn, const uint256& hashIn);
470 
471  SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }
472 
473  friend bool operator<(const CInv& a, const CInv& b);
474 
475  bool IsMasterNodeType() const;
476  std::string ToString() const;
477 
478  // TODO: make private (improve encapsulation)
479  int type;
481 
482 private:
483  std::string GetCommand() const;
484 };
485 
486 #endif // PIVX_PROTOCOL_H
A CService with information about it as peer.
Definition: protocol.h:338
static constexpr uint32_t DISK_VERSION_IGNORE_MASK
Definition: protocol.h:358
SERIALIZE_METHODS(CAddress, obj)
Definition: protocol.h:371
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
Definition: protocol.h:430
static constexpr uint32_t DISK_VERSION_INIT
Historically, CAddress disk serialization stored the CLIENT_VERSION, optionally OR'ed with the ADDRV2...
Definition: protocol.h:357
CAddress()
Definition: protocol.h:367
uint32_t nTime
Always included in serialization, except in the network format on INIT_PROTO_VERSION.
Definition: protocol.h:428
CAddress(CService ipIn, ServiceFlags nServicesIn)
Definition: protocol.h:368
static constexpr uint32_t DISK_VERSION_ADDRV2
The version number written in disk serialized addresses to indicate V2 serializations.
Definition: protocol.h:362
CAddress(CService ipIn, ServiceFlags nServicesIn, uint32_t nTimeIn)
Definition: protocol.h:369
static constexpr uint32_t TIME_INIT
Definition: protocol.h:339
inv message data
Definition: protocol.h:466
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:203
int type
Definition: protocol.h:479
std::string ToString() const
Definition: protocol.cpp:245
std::string GetCommand() const
Definition: protocol.cpp:212
CInv()
Definition: protocol.cpp:191
bool IsMasterNodeType() const
Definition: protocol.cpp:208
uint256 hash
Definition: protocol.h:480
SERIALIZE_METHODS(CInv, obj)
Definition: protocol.h:471
Message header.
Definition: protocol.h:31
static constexpr size_t MESSAGE_SIZE_OFFSET
Definition: protocol.h:37
static constexpr size_t CHECKSUM_OFFSET
Definition: protocol.h:38
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:40
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:54
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:36
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:35
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:165
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:55
static constexpr size_t HEADER_SIZE
Definition: protocol.h:39
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:57
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:33
std::string GetCommand() const
Definition: protocol.cpp:160
SERIALIZE_METHODS(CMessageHeader, obj)
Definition: protocol.h:52
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:34
uint32_t nMessageSize
Definition: protocol.h:56
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:138
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:484
256-bit opaque blob.
Definition: uint256.h:138
Bitcoin protocol message types.
Definition: protocol.cpp:18
const char * QFCOMMITMENT
The qfcommit message is used to propagate LLMQ final commitments.
Definition: protocol.cpp:56
const char * MNBROADCAST2
The mnbroadcast2 message is used to broadcast masternode startup data to connected peers Supporting B...
Definition: protocol.cpp:45
const char * BUDGETVOTESYNC
The budgetvotesync message is used to request budget vote data from connected peers.
Definition: protocol.cpp:51
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:38
const char * MNWINNER
The mnwinner message is used to relay and distribute consensus for masternode payout ordering.
Definition: protocol.cpp:47
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:31
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.cpp:40
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:30
const char * GETMNLIST
The dseg message is used to request the Masternode list or an specific entry.
Definition: protocol.cpp:55
const char * QSIGSESANN
Definition: protocol.cpp:63
const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message,...
Definition: protocol.cpp:22
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:41
const char * PONG
The pong message replies to a ping message, proving to the pinging node that the ponging node is stil...
Definition: protocol.cpp:35
const char * GETMNWINNERS
The getmnwinners message is used to request winning masternode data from connected peers.
Definition: protocol.cpp:48
const char * QSENDRECSIGS
The qsendrecsigs message is used to propagate LLMQ intra-quorum partial recovered signatures.
Definition: protocol.cpp:57
const char * QGETSIGSHARES
Definition: protocol.cpp:65
const char * QBSIGSHARES
Definition: protocol.cpp:66
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:32
const char * FINALBUDGETVOTE
The finalbudgetvote message is used to broadcast or relay finalized budget votes to connected peers.
Definition: protocol.cpp:53
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:37
const char * QSIGREC
Definition: protocol.cpp:67
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:33
const char * QCONTRIB
Definition: protocol.cpp:59
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:29
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:39
const char * QPCOMMITMENT
Definition: protocol.cpp:62
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:21
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:19
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:27
const char * MNBROADCAST
The mnbroadcast message is used to broadcast masternode startup data to connected peers.
Definition: protocol.cpp:44
const char * QCOMPLAINT
Definition: protocol.cpp:60
const char * QSIGSHARE
Definition: protocol.cpp:68
const char * FINALBUDGET
The finalbudget message is used to broadcast or relay finalized budget metadata to connected peers.
Definition: protocol.cpp:52
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:28
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:25
const char * MNPING
The mnping message is used to ensure a masternode is still active.
Definition: protocol.cpp:46
const char * QJUSTIFICATION
Definition: protocol.cpp:61
const char * BUDGETVOTE
The budgetvote message is used to broadcast or relay budget proposal votes to connected peers.
Definition: protocol.cpp:50
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:20
const char * SYNCSTATUSCOUNT
The syncstatuscount message is used to track the layer 2 syncing process.
Definition: protocol.cpp:54
const char * BUDGETPROPOSAL
The budgetproposal message is used to broadcast or relay budget proposal metadata to connected peers.
Definition: protocol.cpp:49
const char * CLSIG
Definition: protocol.cpp:69
const char * ALERT
The alert message warns nodes of problems that may affect them or the rest of the network.
Definition: protocol.cpp:36
const char * SENDADDRV2
The sendaddrv2 message signals support for receiving ADDRV2 messages (BIP155).
Definition: protocol.cpp:23
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.cpp:34
const char * SPORK
The spork message is used to send spork values to connected peers.
Definition: protocol.cpp:42
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:26
const char * GETSPORKS
The getsporks message is used to request spork data from connected peers.
Definition: protocol.cpp:43
const char * QSIGSHARESINV
Definition: protocol.cpp:64
const char * MNAUTH
The mnauth message is used authenticate MN connections.
Definition: protocol.cpp:58
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:24
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:250
GetDataMsg
getdata message types
Definition: protocol.h:434
@ MSG_TX
Definition: protocol.h:436
@ MSG_BUDGET_VOTE
Definition: protocol.h:446
@ MSG_FILTERED_BLOCK
Definition: protocol.h:440
@ MSG_QUORUM_JUSTIFICATION
Definition: protocol.h:457
@ MSG_BUDGET_PROPOSAL
Definition: protocol.h:447
@ MSG_QUORUM_COMPLAINT
Definition: protocol.h:456
@ MSG_MASTERNODE_QUORUM
Definition: protocol.h:450
@ MSG_TYPE_MAX
Definition: protocol.h:461
@ MSG_MASTERNODE_ANNOUNCE
Definition: protocol.h:451
@ MSG_MASTERNODE_WINNER
Definition: protocol.h:444
@ MSG_BLOCK
Definition: protocol.h:437
@ MSG_MASTERNODE_PING
Definition: protocol.h:452
@ UNDEFINED
Definition: protocol.h:435
@ MSG_TXLOCK_REQUEST
Definition: protocol.h:441
@ MSG_QUORUM_CONTRIB
Definition: protocol.h:455
@ MSG_MASTERNODE_SCANNING_ERROR
Definition: protocol.h:445
@ MSG_BUDGET_FINALIZED_VOTE
Definition: protocol.h:449
@ MSG_TXLOCK_VOTE
Definition: protocol.h:442
@ MSG_QUORUM_PREMATURE_COMMITMENT
Definition: protocol.h:458
@ MSG_QUORUM_RECOVERED_SIG
Definition: protocol.h:459
@ MSG_DSTX
Definition: protocol.h:453
@ MSG_SPORK
Definition: protocol.h:443
@ MSG_CLSIG
Definition: protocol.h:460
@ MSG_BUDGET_FINALIZED
Definition: protocol.h:448
@ MSG_QUORUM_FINAL_COMMITMENT
Definition: protocol.h:454
const std::vector< std::string > & getTierTwoNetMessageTypes()
Definition: protocol.cpp:255
ServiceFlags
nServices flags
Definition: protocol.h:312
@ NODE_NONE
Definition: protocol.h:314
@ NODE_BLOOM
Definition: protocol.h:321
@ NODE_NETWORK
Definition: protocol.h:318
@ NODE_BLOOM_WITHOUT_MN
Definition: protocol.h:325
#define SER_WRITE(obj, code)
Definition: serialize.h:186
@ SER_DISK
Definition: serialize.h:175
@ SER_NETWORK
Definition: serialize.h:174
@ SER_GETHASH
Definition: serialize.h:176
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, const Args &... args)
Definition: serialize.h:1396
#define SER_READ(obj, code)
Definition: serialize.h:185
#define READWRITE(...)
Definition: serialize.h:183
Formatter for integers in CompactSize format.
Definition: serialize.h:706
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:670