Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

protocol.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef __cplusplus
7 #error This header can only be compiled as C++.
8 #endif
9 
10 #ifndef BITCOIN_PROTOCOL_H
11 #define BITCOIN_PROTOCOL_H
12 
13 #include <netaddress.h>
14 #include <serialize.h>
15 #include <uint256.h>
16 #include <version.h>
17 
18 #include <atomic>
19 #include <stdint.h>
20 #include <string>
21 
29 {
30 public:
31  static constexpr size_t MESSAGE_START_SIZE = 4;
32  static constexpr size_t COMMAND_SIZE = 12;
33  static constexpr size_t MESSAGE_SIZE_SIZE = 4;
34  static constexpr size_t CHECKSUM_SIZE = 4;
38  typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
39 
40  explicit CMessageHeader(const MessageStartChars& pchMessageStartIn);
41  CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
42 
43  std::string GetCommand() const;
44  bool IsValid(const MessageStartChars& messageStart) const;
45 
47 
48  template <typename Stream, typename Operation>
49  inline void SerializationOp(Stream& s, Operation ser_action)
50  {
55  }
56 
59  uint32_t nMessageSize;
61 };
62 
67 namespace NetMsgType {
68 
74 extern const char *VERSION;
80 extern const char *VERACK;
86 extern const char *ADDR;
92 extern const char *INV;
97 extern const char *GETDATA;
104 extern const char *MERKLEBLOCK;
110 extern const char *GETBLOCKS;
117 extern const char *GETHEADERS;
122 extern const char *TX;
129 extern const char *HEADERS;
134 extern const char *BLOCK;
140 extern const char *GETADDR;
147 extern const char *MEMPOOL;
153 extern const char *PING;
160 extern const char *PONG;
167 extern const char *NOTFOUND;
176 extern const char *FILTERLOAD;
185 extern const char *FILTERADD;
194 extern const char *FILTERCLEAR;
201 extern const char *REJECT;
208 extern const char *SENDHEADERS;
209 
217 extern const char *SENDCMPCT;
223 extern const char *CMPCTBLOCK;
229 extern const char *GETBLOCKTXN;
235 extern const char *BLOCKTXN;
236 
237 // Dash message types
238 // NOTE: do NOT declare non-implmented here, we don't want them to be exposed to the outside
239 // TODO: add description
240 extern const char *LEGACYTXLOCKREQUEST; // only present for backwards compatibility
241 extern const char *SPORK;
242 extern const char *GETSPORKS;
243 extern const char *DSACCEPT;
244 extern const char *DSVIN;
245 extern const char *DSFINALTX;
246 extern const char *DSSIGNFINALTX;
247 extern const char *DSCOMPLETE;
248 extern const char *DSSTATUSUPDATE;
249 extern const char *DSTX;
250 extern const char *DSQUEUE;
251 extern const char *SENDDSQUEUE;
252 extern const char *SYNCSTATUSCOUNT;
253 extern const char *MNGOVERNANCESYNC;
254 extern const char *MNGOVERNANCEOBJECT;
255 extern const char *MNGOVERNANCEOBJECTVOTE;
256 extern const char *GETMNLISTDIFF;
257 extern const char *MNLISTDIFF;
258 extern const char *QSENDRECSIGS;
259 extern const char *QFCOMMITMENT;
260 extern const char *QCONTRIB;
261 extern const char *QCOMPLAINT;
262 extern const char *QJUSTIFICATION;
263 extern const char *QPCOMMITMENT;
264 extern const char *QWATCH;
265 extern const char *QSIGSESANN;
266 extern const char *QSIGSHARESINV;
267 extern const char *QGETSIGSHARES;
268 extern const char *QBSIGSHARES;
269 extern const char *QSIGREC;
270 extern const char *QSIGSHARE;
271 extern const char *CLSIG;
272 extern const char *ISLOCK;
273 extern const char *MNAUTH;
274 };
275 
276 /* Get a vector of all valid message types (see above) */
277 const std::vector<std::string> &getAllNetMessageTypes();
278 
280 enum ServiceFlags : uint64_t {
281  // Nothing
283  // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
284  // set by all Dash Core non pruned nodes, and is unset by SPV clients or other light clients.
285  NODE_NETWORK = (1 << 0),
286  // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
287  // Dash Core does not support this but a patch set called Bitcoin XT does.
288  // See BIP 64 for details on how this is implemented.
289  NODE_GETUTXO = (1 << 1),
290  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
291  // Dash Core nodes used to support this by default, without advertising this bit,
292  // but no longer do as of protocol version 70201 (= NO_BLOOM_VERSION)
293  NODE_BLOOM = (1 << 2),
294  // NODE_XTHIN means the node supports Xtreme Thinblocks
295  // If this is turned off then the node will not service nor make xthin requests
296  NODE_XTHIN = (1 << 4),
297  // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
298  // serving the last 288 blocks
299  // See BIP159 for details on how this is implemented.
300  NODE_NETWORK_LIMITED = (1 << 10),
301 
302  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
303  // isn't getting used, or one not being used much, and notify the
304  // bitcoin-development mailing list. Remember that service bits are just
305  // unauthenticated advertisements, so your code must be robust against
306  // collisions and other cases where nodes may be advertising a service they
307  // do not actually support. Other service bits should be allocated via the
308  // BIP process.
309 };
310 
336 
338 void SetServiceFlagsIBDCache(bool status);
339 
345 static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
346  return !(GetDesirableServiceFlags(services) & (~services));
347 }
348 
353 static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
354  return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
355 }
356 
358 class CAddress : public CService
359 {
360 public:
361  CAddress();
362  explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
363 
364  void Init();
365 
367 
368  template <typename Stream, typename Operation>
369  inline void SerializationOp(Stream& s, Operation ser_action)
370  {
371  if (ser_action.ForRead())
372  Init();
373  int nVersion = s.GetVersion();
374  if (s.GetType() & SER_DISK)
375  READWRITE(nVersion);
376  if ((s.GetType() & SER_DISK) ||
377  (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
378  READWRITE(nTime);
379  uint64_t nServicesInt = nServices;
380  READWRITE(nServicesInt);
381  nServices = (ServiceFlags)nServicesInt;
382  READWRITE(*(CService*)this);
383  }
384 
385  // TODO: make private (improves encapsulation)
386 public:
388 
389  // disk and network only
390  unsigned int nTime;
391 };
392 
399  MSG_TX = 1,
401  // The following can only occur in getdata. Invs always use TX or BLOCK.
403  // Dash message types
404  // NOTE: declare non-implmented here, we must keep this enum consistent and backwards compatible
406  /* MSG_TXLOCK_VOTE = 5, Legacy InstantSend and not used anymore */
408  /* 7 - 15 were used in old Dash versions and were mainly budget and MN broadcast/ping related*/
409  MSG_DSTX = 16,
412  /* 19 was used for MSG_MASTERNODE_VERIFY and is not supported anymore */
413  // Nodes may always request a MSG_CMPCT_BLOCK in a getdata, however,
414  // MSG_CMPCT_BLOCK should not appear in any invs except as a part of getdata.
417  /* MSG_QUORUM_DUMMY_COMMITMENT = 22, */ // was shortly used on testnet/devnet/regtest
422  /* MSG_QUORUM_DEBUG_STATUS = 27, */ // was shortly used on testnet/devnet/regtest
424  MSG_CLSIG = 29,
426 };
427 
429 class CInv
430 {
431 public:
432  CInv();
433  CInv(int typeIn, const uint256& hashIn);
434 
436 
437  template <typename Stream, typename Operation>
438  inline void SerializationOp(Stream& s, Operation ser_action)
439  {
440  READWRITE(type);
441  READWRITE(hash);
442  }
443 
444  friend bool operator<(const CInv& a, const CInv& b);
445 
446  bool IsKnownType() const;
447  std::string GetCommand() const;
448  std::string ToString() const;
449 
450 private:
451  const char* GetCommandInternal() const;
452 
453  // TODO: make private (improves encapsulation)
454 public:
455  int type;
457 };
458 
459 
460 #endif // BITCOIN_PROTOCOL_H
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:31
const char * QBSIGSHARES
Definition: protocol.cpp:72
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:34
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:23
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:60
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
ServiceFlags
nServices flags
Definition: protocol.h:280
const char * DSACCEPT
Definition: protocol.cpp:47
#define READWRITE(obj)
Definition: serialize.h:165
const char * QPCOMMITMENT
Definition: protocol.cpp:67
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:33
std::string GetCommand() const
Definition: protocol.cpp:274
std::string ToString() const
Definition: protocol.cpp:283
const char * QFCOMMITMENT
Definition: protocol.cpp:63
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:293
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:29
void Init()
Definition: protocol.cpp:224
Defined in BIP152.
Definition: protocol.h:415
inv message data
Definition: protocol.h:429
const char * QSIGSESANN
Definition: protocol.cpp:69
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:39
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:58
const char * DSVIN
Definition: protocol.cpp:48
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:369
const char * DSSTATUSUPDATE
Definition: protocol.cpp:52
ADD_SERIALIZE_METHODS
Definition: protocol.h:366
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:148
const char * GetCommandInternal() const
Definition: protocol.cpp:248
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:49
GetDataMsg
getdata / inv message types.
Definition: protocol.h:397
uint32_t nMessageSize
Definition: protocol.h:59
const char * QSIGSHARE
Definition: protocol.cpp:74
std::string GetCommand() const
Definition: protocol.cpp:165
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:32
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:170
#define FLATDATA(obj)
Definition: serialize.h:370
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:27
const char * QJUSTIFICATION
Definition: protocol.cpp:66
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:21
const char * DSFINALTX
Definition: protocol.cpp:49
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:201
const char * ISLOCK
Definition: protocol.cpp:76
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:25
const char * MNLISTDIFF
Definition: protocol.cpp:61
const char * DSCOMPLETE
Definition: protocol.cpp:51
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:32
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
Definition: protocol.h:345
Bitcoin protocol message types.
Definition: protocol.cpp:17
int type
Definition: protocol.h:455
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
const char * LEGACYTXLOCKREQUEST
Definition: protocol.cpp:44
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:38
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:30
const char * GETSPORKS
Definition: protocol.cpp:46
A CService with information about it as peer.
Definition: protocol.h:358
const char * QSIGREC
Definition: protocol.cpp:73
uint256 hash
Definition: protocol.h:456
const char * GETMNLISTDIFF
Definition: protocol.cpp:60
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:20
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:36
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:34
const char * MNGOVERNANCESYNC
Definition: protocol.cpp:57
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:33
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:28
const char * DSSIGNFINALTX
Definition: protocol.cpp:50
static bool MayHaveUsefulAddressDB(ServiceFlags services)
Checks if a peer with the given service flags may be capable of having a robust address-storage DB...
Definition: protocol.h:353
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:38
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected...
Definition: protocol.cpp:37
const char * QSIGSHARESINV
Definition: protocol.cpp:70
static constexpr size_t MESSAGE_SIZE_OFFSET
Definition: protocol.h:35
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:31
const char * SYNCSTATUSCOUNT
Definition: protocol.cpp:56
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:24
void SetServiceFlagsIBDCache(bool status)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:208
ADD_SERIALIZE_METHODS
Definition: protocol.h:435
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
const char * QSENDRECSIGS
Definition: protocol.cpp:62
256-bit opaque blob.
Definition: uint256.h:123
unsigned int nTime
Definition: protocol.h:390
ServiceFlags nServices
Definition: protocol.h:387
const char * QGETSIGSHARES
Definition: protocol.cpp:71
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:40
const char * QCOMPLAINT
Definition: protocol.cpp:65
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
const char * DSQUEUE
Definition: protocol.cpp:54
static const int CADDR_TIME_VERSION
nTime field added to CAddress, starting with this version; if possible, avoid requesting addresses no...
Definition: version.h:30
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:22
bool IsKnownType() const
Definition: protocol.cpp:243
const char * DSTX
Definition: protocol.cpp:53
const char * MNAUTH
Definition: protocol.cpp:77
const char * QCONTRIB
Definition: protocol.cpp:64
static constexpr size_t HEADER_SIZE
Definition: protocol.h:37
CInv()
Definition: protocol.cpp:230
const char * CLSIG
Definition: protocol.cpp:75
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:438
const char * MNGOVERNANCEOBJECT
Definition: protocol.cpp:58
const char * MNGOVERNANCEOBJECTVOTE
Definition: protocol.cpp:59
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
const char * QWATCH
Definition: protocol.cpp:68
const char * SPORK
Definition: protocol.cpp:45
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:238
const char * SENDDSQUEUE
Definition: protocol.cpp:55
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:57
Defined in BIP37.
Definition: protocol.h:402
static constexpr size_t CHECKSUM_OFFSET
Definition: protocol.h:36
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:35
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:41
Message header.
Definition: protocol.h:28
Released under the MIT license