Bitcoin ABC  0.22.13
P2P Digital Currency
protocol.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <array>
19 #include <cstdint>
20 #include <string>
21 
22 class Config;
23 
28 static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 2 * 1024 * 1024;
29 
38 public:
39  static constexpr size_t MESSAGE_START_SIZE = 4;
40  static constexpr size_t COMMAND_SIZE = 12;
41  static constexpr size_t MESSAGE_SIZE_SIZE = 4;
42  static constexpr size_t CHECKSUM_SIZE = 4;
43  static constexpr size_t MESSAGE_SIZE_OFFSET =
44  MESSAGE_START_SIZE + COMMAND_SIZE;
45  static constexpr size_t CHECKSUM_OFFSET =
46  MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
47  static constexpr size_t HEADER_SIZE =
48  MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
49  typedef std::array<uint8_t, MESSAGE_START_SIZE> MessageMagic;
50 
51  explicit CMessageHeader(const MessageMagic &pchMessageStartIn);
52 
59  CMessageHeader(const MessageMagic &pchMessageStartIn,
60  const char *pszCommand, unsigned int nMessageSizeIn);
61 
62  std::string GetCommand() const;
63  bool IsValid(const Config &config) const;
64  bool IsValidWithoutConfig(const MessageMagic &magic) const;
65  bool IsOversized(const Config &config) const;
66 
68 
69  template <typename Stream, typename Operation>
70  inline void SerializationOp(Stream &s, Operation ser_action) {
75  }
76 
77  MessageMagic pchMessageStart;
78  std::array<char, COMMAND_SIZE> pchCommand;
79  uint32_t nMessageSize;
81 };
82 
87 namespace NetMsgType {
88 
94 extern const char *VERSION;
100 extern const char *VERACK;
106 extern const char *ADDR;
112 extern const char *INV;
117 extern const char *GETDATA;
124 extern const char *MERKLEBLOCK;
130 extern const char *GETBLOCKS;
137 extern const char *GETHEADERS;
142 extern const char *TX;
149 extern const char *HEADERS;
154 extern const char *BLOCK;
160 extern const char *GETADDR;
167 extern const char *MEMPOOL;
173 extern const char *PING;
180 extern const char *PONG;
187 extern const char *NOTFOUND;
196 extern const char *FILTERLOAD;
205 extern const char *FILTERADD;
214 extern const char *FILTERCLEAR;
221 extern const char *SENDHEADERS;
227 extern const char *FEEFILTER;
235 extern const char *SENDCMPCT;
241 extern const char *CMPCTBLOCK;
247 extern const char *GETBLOCKTXN;
253 extern const char *BLOCKTXN;
259 extern const char *GETCFILTERS;
264 extern const char *CFILTER;
272 extern const char *GETCFHEADERS;
278 extern const char *CFHEADERS;
285 extern const char *GETCFCHECKPT;
290 extern const char *CFCHECKPT;
294 extern const char *AVAHELLO;
299 extern const char *AVAPOLL;
304 extern const char *AVARESPONSE;
305 
311 bool IsBlockLike(const std::string &strCommand);
312 }; // namespace NetMsgType
313 
315 const std::vector<std::string> &getAllNetMessageTypes();
316 
320 enum ServiceFlags : uint64_t {
321  // NOTE: When adding here, be sure to update serviceFlagToStr too
322  // Nothing
324  // NODE_NETWORK means that the node is capable of serving the complete block
325  // chain. It is currently set by all Bitcoin ABC non pruned nodes, and is
326  // unset by SPV clients or other light clients.
327  NODE_NETWORK = (1 << 0),
328  // NODE_GETUTXO means the node is capable of responding to the getutxo
329  // protocol request. Bitcoin ABC does not support this but a patch set
330  // called Bitcoin XT does. See BIP 64 for details on how this is
331  // implemented.
332  NODE_GETUTXO = (1 << 1),
333  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered
334  // connections. Bitcoin ABC nodes used to support this by default, without
335  // advertising this bit, but no longer do as of protocol version 70011 (=
336  // NO_BLOOM_VERSION)
337  NODE_BLOOM = (1 << 2),
338  // Bit 4 was NODE_XTHIN, removed in v0.22.12
339 
340  // Bit 5 was NODE_BITCOIN_CASH, removed in v0.22.8
341 
342  // NODE_COMPACT_FILTERS means the node will service basic block filter
343  // requests.
344  // See BIP157 and BIP158 for details on how this is implemented.
346 
347  // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation
348  // of only serving the last 288 (2 day) blocks
349  // See BIP159 for details on how this is implemented.
350  NODE_NETWORK_LIMITED = (1 << 10),
351 
352  // The last non experimental service bit, helper for looping over the flags
354 
355  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
356  // isn't getting used, or one not being used much, and notify the
357  // bitcoin-development mailing list. Remember that service bits are just
358  // unauthenticated advertisements, so your code must be robust against
359  // collisions and other cases where nodes may be advertising a service they
360  // do not actually support. Other service bits should be allocated via the
361  // BIP process.
362 
363  // NODE_AVALANCHE means the node supports Bitcoin Cash's avalanche
364  // preconsensus mechanism.
365  NODE_AVALANCHE = (1 << 24),
366 };
367 
373 std::vector<std::string> serviceFlagsToStr(const uint64_t flags);
374 
400 
405 void SetServiceFlagsIBDCache(bool status);
406 
412 static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
413  return !(GetDesirableServiceFlags(services) & (~services));
414 }
415 
420 static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
421  return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
422 }
423 
427 class CAddress : public CService {
428 public:
429  CAddress();
430  explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
431 
432  void Init();
433 
435 
436  template <typename Stream, typename Operation>
437  inline void SerializationOp(Stream &s, Operation ser_action) {
438  if (ser_action.ForRead()) {
439  Init();
440  }
441  int nVersion = s.GetVersion();
442  if (s.GetType() & SER_DISK) {
443  READWRITE(nVersion);
444  }
445  if ((s.GetType() & SER_DISK) ||
446  (nVersion != INIT_PROTO_VERSION && !(s.GetType() & SER_GETHASH))) {
447  // The only time we serialize a CAddress object without nTime is in
448  // the initial VERSION messages which contain two CAddress records.
449  // At that point, the serialization version is INIT_PROTO_VERSION.
450  // After the version handshake, serialization version is >=
451  // MIN_PEER_PROTO_VERSION and all ADDR messages are serialized with
452  // nTime.
453  READWRITE(nTime);
454  }
455  uint64_t nServicesInt = nServices;
456  READWRITE(nServicesInt);
457  nServices = static_cast<ServiceFlags>(nServicesInt);
458  READWRITEAS(CService, *this);
459  }
460 
461  // TODO: make private (improves encapsulation)
462 public:
464 
465  // disk and network only
466  unsigned int nTime;
467 };
468 
470 const uint32_t MSG_TYPE_MASK = 0xffffffff >> 3;
471 
479  MSG_TX = 1,
481  // The following can only occur in getdata. Invs always use TX or BLOCK.
486 };
487 
493 class CInv {
494 public:
495  // TODO: make private (improves encapsulation)
496  uint32_t type;
498 
499 public:
500  CInv() : type(0), hash() {}
501  CInv(uint32_t typeIn, const uint256 &hashIn) : type(typeIn), hash(hashIn) {}
502 
504 
505  template <typename Stream, typename Operation>
506  inline void SerializationOp(Stream &s, Operation ser_action) {
507  READWRITE(type);
508  READWRITE(hash);
509  }
510 
511  friend bool operator<(const CInv &a, const CInv &b) {
512  return a.type < b.type || (a.type == b.type && a.hash < b.hash);
513  }
514 
515  std::string GetCommand() const;
516  std::string ToString() const;
517 
518  uint32_t GetKind() const { return type & MSG_TYPE_MASK; }
519 
520  bool IsTx() const {
521  auto k = GetKind();
522  return k == MSG_TX;
523  }
524 
525  bool IsSomeBlock() const {
526  auto k = GetKind();
527  return k == MSG_BLOCK || k == MSG_FILTERED_BLOCK ||
528  k == MSG_CMPCT_BLOCK;
529  }
530 };
531 
532 #endif // BITCOIN_PROTOCOL_H
const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
Definition: protocol.cpp:46
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 * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:37
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
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:80
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:45
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (Currently 2MB).
Definition: protocol.h:28
ServiceFlags
nServices flags.
Definition: protocol.h:320
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:41
CMessageHeader(const MessageMagic &pchMessageStartIn)
Definition: protocol.cpp:84
const std::vector< std::string > & getAllNetMessageTypes()
Get a vector of all valid message types (see above)
Definition: protocol.cpp:255
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:32
Defined in BIP152.
Definition: protocol.h:485
const char * AVAHELLO
Contains a delegation and a signature.
Definition: protocol.cpp:52
Inv(ventory) message data.
Definition: protocol.h:493
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:42
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:437
ADD_SERIALIZE_METHODS
Definition: protocol.h:434
const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
Definition: protocol.cpp:49
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:70
GetDataMsg
getdata / inv message types.
Definition: protocol.h:477
uint32_t nMessageSize
Definition: protocol.h:79
const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
Definition: protocol.cpp:47
MessageMagic pchMessageStart
Definition: protocol.h:77
const uint32_t MSG_TYPE_MASK
getdata message type flags
Definition: protocol.h:470
std::string GetCommand() const
Definition: protocol.cpp:112
#define READWRITEAS(type, obj)
Definition: serialize.h:192
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 * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:30
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.cpp:50
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:24
std::vector< std::string > serviceFlagsToStr(const uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:292
CInv(uint32_t typeIn, const uint256 &hashIn)
Definition: protocol.h:501
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:67
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:204
uint32_t GetKind() const
Definition: protocol.h:518
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:28
bool IsOversized(const Config &config) const
Definition: protocol.cpp:188
Definition: config.h:19
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:40
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
Definition: protocol.h:412
Bitcoin protocol message types.
Definition: protocol.cpp:20
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:14
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:179
Removed for block.
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:40
bool IsTx() const
Definition: protocol.h:520
A CService with information about it as peer.
Definition: protocol.h:427
uint256 hash
Definition: protocol.h:497
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:23
std::array< char, COMMAND_SIZE > pchCommand
Definition: protocol.h:78
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:39
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:42
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:36
const char * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
Definition: protocol.cpp:41
const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks...
Definition: protocol.cpp:48
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:420
static constexpr size_t MESSAGE_SIZE_OFFSET
Definition: protocol.h:43
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:39
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:27
void SetServiceFlagsIBDCache(bool status)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:212
ADD_SERIALIZE_METHODS
Definition: protocol.h:503
int flags
Definition: bitcoin-tx.cpp:529
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:22
256-bit opaque blob.
Definition: uint256.h:120
unsigned int nTime
Definition: protocol.h:466
ServiceFlags nServices
Definition: protocol.h:463
const char * AVARESPONSE
Contains an avalanche::Response.
Definition: protocol.cpp:54
std::array< uint8_t, MESSAGE_START_SIZE > MessageMagic
Definition: protocol.h:49
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:21
uint32_t type
Definition: protocol.h:496
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:25
const char * AVAPOLL
Contains an avalanche::Poll.
Definition: protocol.cpp:53
bool IsValid(const Config &config) const
Definition: protocol.cpp:147
bool IsBlockLike(const std::string &strCommand)
Indicate if the message is used to transmit the content of a block.
Definition: protocol.cpp:56
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.cpp:51
static constexpr size_t HEADER_SIZE
Definition: protocol.h:47
bool IsSomeBlock() const
Definition: protocol.h:525
CInv()
Definition: protocol.h:500
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:506
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:29
#define READWRITE(...)
Definition: serialize.h:191
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.h:511
bool IsValidWithoutConfig(const MessageMagic &magic) const
This is a transition method in order to stay compatible with older code that do not use the config...
Definition: protocol.cpp:171
Defined in BIP37.
Definition: protocol.h:483
static constexpr size_t CHECKSUM_OFFSET
Definition: protocol.h:45
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:38
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:44
Message header.
Definition: protocol.h:37