Bitcoin Core  0.20.99
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 <stdint.h>
19 #include <string>
20 
28 {
29 public:
30  static constexpr size_t MESSAGE_START_SIZE = 4;
31  static constexpr size_t COMMAND_SIZE = 12;
32  static constexpr size_t MESSAGE_SIZE_SIZE = 4;
33  static constexpr size_t CHECKSUM_SIZE = 4;
34  static constexpr size_t MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE;
35  static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
36  static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
37  typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
38 
39  explicit CMessageHeader(const MessageStartChars& pchMessageStartIn);
40 
44  CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
45 
46  std::string GetCommand() const;
47  bool IsValid(const MessageStartChars& messageStart) const;
48 
49  SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
50 
53  uint32_t nMessageSize;
55 };
56 
61 namespace NetMsgType {
62 
68 extern const char* VERSION;
74 extern const char* VERACK;
80 extern const char* ADDR;
86 extern const char* INV;
91 extern const char* GETDATA;
98 extern const char* MERKLEBLOCK;
104 extern const char* GETBLOCKS;
111 extern const char* GETHEADERS;
116 extern const char* TX;
123 extern const char* HEADERS;
128 extern const char* BLOCK;
134 extern const char* GETADDR;
141 extern const char* MEMPOOL;
147 extern const char* PING;
154 extern const char* PONG;
161 extern const char* NOTFOUND;
170 extern const char* FILTERLOAD;
179 extern const char* FILTERADD;
188 extern const char* FILTERCLEAR;
195 extern const char* SENDHEADERS;
201 extern const char* FEEFILTER;
209 extern const char* SENDCMPCT;
215 extern const char* CMPCTBLOCK;
221 extern const char* GETBLOCKTXN;
227 extern const char* BLOCKTXN;
233 extern const char* GETCFILTERS;
238 extern const char* CFILTER;
246 extern const char* GETCFHEADERS;
251 extern const char* CFHEADERS;
258 extern const char* GETCFCHECKPT;
263 extern const char* CFCHECKPT;
264 }; // namespace NetMsgType
265 
266 /* Get a vector of all valid message types (see above) */
267 const std::vector<std::string>& getAllNetMessageTypes();
268 
270 enum ServiceFlags : uint64_t {
271  // NOTE: When adding here, be sure to update serviceFlagToStr too
272  // Nothing
274  // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
275  // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.
276  NODE_NETWORK = (1 << 0),
277  // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
278  // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
279  // See BIP 64 for details on how this is implemented.
280  NODE_GETUTXO = (1 << 1),
281  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
282  // Bitcoin Core nodes used to support this by default, without advertising this bit,
283  // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
284  NODE_BLOOM = (1 << 2),
285  // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
286  // witness data.
287  NODE_WITNESS = (1 << 3),
288  // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
289  // serving the last 288 (2 day) blocks
290  // See BIP159 for details on how this is implemented.
291  NODE_NETWORK_LIMITED = (1 << 10),
292 
293  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
294  // isn't getting used, or one not being used much, and notify the
295  // bitcoin-development mailing list. Remember that service bits are just
296  // unauthenticated advertisements, so your code must be robust against
297  // collisions and other cases where nodes may be advertising a service they
298  // do not actually support. Other service bits should be allocated via the
299  // BIP process.
300 };
301 
307 std::vector<std::string> serviceFlagsToStr(uint64_t flags);
308 
334 
336 void SetServiceFlagsIBDCache(bool status);
337 
343 static inline bool HasAllDesirableServiceFlags(ServiceFlags services)
344 {
345  return !(GetDesirableServiceFlags(services) & (~services));
346 }
347 
352 static inline bool MayHaveUsefulAddressDB(ServiceFlags services)
353 {
354  return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
355 }
356 
358 class CAddress : public CService
359 {
360  static constexpr uint32_t TIME_INIT{100000000};
361 
362 public:
363  CAddress() : CService{} {};
364  explicit CAddress(CService ipIn, ServiceFlags nServicesIn) : CService{ipIn}, nServices{nServicesIn} {};
365 
367  {
368  SER_READ(obj, obj.nTime = TIME_INIT);
369  int nVersion = s.GetVersion();
370  if (s.GetType() & SER_DISK) {
371  READWRITE(nVersion);
372  }
373  if ((s.GetType() & SER_DISK) ||
374  (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH))) {
375  READWRITE(obj.nTime);
376  }
377  READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));
378  READWRITEAS(CService, obj);
379  }
380 
382  // disk and network only
383  uint32_t nTime{TIME_INIT};
384 };
385 
387 const uint32_t MSG_WITNESS_FLAG = 1 << 30;
388 const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
389 
396  MSG_TX = 1,
398  // The following can only occur in getdata. Invs always use TX or BLOCK.
404 };
405 
407 class CInv
408 {
409 public:
410  CInv();
411  CInv(int typeIn, const uint256& hashIn);
412 
413  SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }
414 
415  friend bool operator<(const CInv& a, const CInv& b);
416 
417  std::string GetCommand() const;
418  std::string ToString() const;
419 
420  int type;
422 };
423 
424 #endif // BITCOIN_PROTOCOL_H
const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
Definition: protocol.cpp:43
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 * 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:54
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
ServiceFlags
nServices flags
Definition: protocol.h:270
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:32
Defined in BIP144.
Definition: protocol.h:402
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:197
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:29
Defined in BIP152.
Definition: protocol.h:400
inv message data
Definition: protocol.h:407
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:52
Defined in BIP144.
Definition: protocol.h:401
const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
Definition: protocol.cpp:46
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:89
SERIALIZE_METHODS(CAddress, obj)
Definition: protocol.h:366
GetDataMsg
getdata / inv message types.
Definition: protocol.h:394
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:387
uint32_t nMessageSize
Definition: protocol.h:53
const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
Definition: protocol.cpp:44
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:388
std::string GetCommand() const
Definition: protocol.cpp:111
#define READWRITEAS(type, obj)
Definition: serialize.h:172
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:116
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:27
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
Definition: serialize.h:465
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.cpp:47
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:21
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:69
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:147
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:25
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:31
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
Definition: protocol.h:343
Bitcoin protocol message types.
Definition: protocol.cpp:17
int type
Definition: protocol.h:420
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:138
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:37
A CService with information about it as peer.
Definition: protocol.h:358
uint256 hash
Definition: protocol.h:421
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:33
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 * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
Definition: protocol.cpp:38
const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks...
Definition: protocol.cpp:45
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:352
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:37
static constexpr size_t MESSAGE_SIZE_OFFSET
Definition: protocol.h:34
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:30
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:154
int flags
Definition: bitcoin-tx.cpp:509
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:367
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
256-bit opaque blob.
Definition: uint256.h:120
SERIALIZE_METHODS(CMessageHeader, obj)
Definition: protocol.h:49
CAddress()
Definition: protocol.h:363
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:232
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:497
CAddress(CService ipIn, ServiceFlags nServicesIn)
Definition: protocol.h:364
static const int CADDR_TIME_VERSION
nTime field added to CAddress, starting with this version; if possible, avoid requesting addresses no...
Definition: version.h:25
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:22
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.cpp:48
static constexpr size_t HEADER_SIZE
Definition: protocol.h:36
#define SER_READ(obj, code)
Definition: serialize.h:173
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
#define VERSION
#define READWRITE(...)
Definition: serialize.h:171
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:51
Defined in BIP37.
Definition: protocol.h:399
static constexpr size_t CHECKSUM_OFFSET
Definition: protocol.h:35
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:27
SERIALIZE_METHODS(CInv, obj)
Definition: protocol.h:413