Bitcoin Core  0.19.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 
50 
51  template <typename Stream, typename Operation>
52  inline void SerializationOp(Stream& s, Operation ser_action)
53  {
58  }
59 
62  uint32_t nMessageSize;
64 };
65 
70 namespace NetMsgType {
71 
77 extern const char *VERSION;
83 extern const char *VERACK;
89 extern const char *ADDR;
95 extern const char *INV;
100 extern const char *GETDATA;
107 extern const char *MERKLEBLOCK;
113 extern const char *GETBLOCKS;
120 extern const char *GETHEADERS;
125 extern const char *TX;
132 extern const char *HEADERS;
137 extern const char *BLOCK;
143 extern const char *GETADDR;
150 extern const char *MEMPOOL;
156 extern const char *PING;
163 extern const char *PONG;
170 extern const char *NOTFOUND;
179 extern const char *FILTERLOAD;
188 extern const char *FILTERADD;
197 extern const char *FILTERCLEAR;
204 extern const char *SENDHEADERS;
210 extern const char *FEEFILTER;
218 extern const char *SENDCMPCT;
224 extern const char *CMPCTBLOCK;
230 extern const char *GETBLOCKTXN;
236 extern const char *BLOCKTXN;
237 };
238 
239 /* Get a vector of all valid message types (see above) */
240 const std::vector<std::string> &getAllNetMessageTypes();
241 
243 enum ServiceFlags : uint64_t {
244  // NOTE: When adding here, be sure to update qt/guiutil.cpp's formatServicesStr too
245  // Nothing
247  // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
248  // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.
249  NODE_NETWORK = (1 << 0),
250  // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
251  // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
252  // See BIP 64 for details on how this is implemented.
253  NODE_GETUTXO = (1 << 1),
254  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
255  // Bitcoin Core nodes used to support this by default, without advertising this bit,
256  // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
257  NODE_BLOOM = (1 << 2),
258  // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
259  // witness data.
260  NODE_WITNESS = (1 << 3),
261  // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
262  // serving the last 288 (2 day) blocks
263  // See BIP159 for details on how this is implemented.
264  NODE_NETWORK_LIMITED = (1 << 10),
265 
266  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
267  // isn't getting used, or one not being used much, and notify the
268  // bitcoin-development mailing list. Remember that service bits are just
269  // unauthenticated advertisements, so your code must be robust against
270  // collisions and other cases where nodes may be advertising a service they
271  // do not actually support. Other service bits should be allocated via the
272  // BIP process.
273 };
274 
300 
302 void SetServiceFlagsIBDCache(bool status);
303 
309 static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
310  return !(GetDesirableServiceFlags(services) & (~services));
311 }
312 
317 static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
318  return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
319 }
320 
322 class CAddress : public CService
323 {
324 public:
325  CAddress();
326  explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
327 
328  void Init();
329 
331 
332  template <typename Stream, typename Operation>
333  inline void SerializationOp(Stream& s, Operation ser_action)
334  {
335  if (ser_action.ForRead())
336  Init();
337  int nVersion = s.GetVersion();
338  if (s.GetType() & SER_DISK)
339  READWRITE(nVersion);
340  if ((s.GetType() & SER_DISK) ||
341  (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
342  READWRITE(nTime);
343  uint64_t nServicesInt = nServices;
344  READWRITE(nServicesInt);
345  nServices = static_cast<ServiceFlags>(nServicesInt);
346  READWRITEAS(CService, *this);
347  }
348 
349  // TODO: make private (improves encapsulation)
350 public:
352 
353  // disk and network only
354  unsigned int nTime;
355 };
356 
358 const uint32_t MSG_WITNESS_FLAG = 1 << 30;
359 const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
360 
366 {
368  MSG_TX = 1,
370  // The following can only occur in getdata. Invs always use TX or BLOCK.
376 };
377 
379 class CInv
380 {
381 public:
382  CInv();
383  CInv(int typeIn, const uint256& hashIn);
384 
386 
387  template <typename Stream, typename Operation>
388  inline void SerializationOp(Stream& s, Operation ser_action)
389  {
390  READWRITE(type);
391  READWRITE(hash);
392  }
393 
394  friend bool operator<(const CInv& a, const CInv& b);
395 
396  std::string GetCommand() const;
397  std::string ToString() const;
398 
399 public:
400  int type;
402 };
403 
404 #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 * 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:63
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
ServiceFlags
nServices flags
Definition: protocol.h:243
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:32
Defined in BIP144.
Definition: protocol.h:374
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:203
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:372
inv message data
Definition: protocol.h:379
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:61
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:333
Defined in BIP144.
Definition: protocol.h:373
ADD_SERIALIZE_METHODS
Definition: protocol.h:330
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:77
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:52
GetDataMsg
getdata / inv message types.
Definition: protocol.h:365
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:358
uint32_t nMessageSize
Definition: protocol.h:62
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:359
std::string GetCommand() const
Definition: protocol.cpp:99
#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:32
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:104
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 * 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:61
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:135
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:309
Bitcoin protocol message types.
Definition: protocol.cpp:17
int type
Definition: protocol.h:400
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:150
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:322
uint256 hash
Definition: protocol.h:401
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
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:317
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:142
ADD_SERIALIZE_METHODS
Definition: protocol.h:385
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
unsigned int nTime
Definition: protocol.h:354
ServiceFlags nServices
Definition: protocol.h:351
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
static constexpr size_t HEADER_SIZE
Definition: protocol.h:36
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:388
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
#define VERSION
#define READWRITE(...)
Definition: serialize.h:191
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:60
Defined in BIP37.
Definition: protocol.h:371
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