Bitcoin
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 <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 
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;
214 extern const char *FEEFILTER;
222 extern const char *SENDCMPCT;
228 extern const char *CMPCTBLOCK;
234 extern const char *GETBLOCKTXN;
240 extern const char *BLOCKTXN;
241 };
242 
243 /* Get a vector of all valid message types (see above) */
244 const std::vector<std::string> &getAllNetMessageTypes();
245 
248  // Nothing
250  // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
251  // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.
252  NODE_NETWORK = (1 << 0),
253  // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
254  // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
255  // See BIP 64 for details on how this is implemented.
256  NODE_GETUTXO = (1 << 1),
257  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
258  // Bitcoin Core nodes used to support this by default, without advertising this bit,
259  // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
260  NODE_BLOOM = (1 << 2),
261  // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
262  // witness data.
263  NODE_WITNESS = (1 << 3),
264  // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
265  // serving the last 288 (2 day) blocks
266  // See BIP159 for details on how this is implemented.
267  NODE_NETWORK_LIMITED = (1 << 10),
268 
269  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
270  // isn't getting used, or one not being used much, and notify the
271  // bitcoin-development mailing list. Remember that service bits are just
272  // unauthenticated advertisements, so your code must be robust against
273  // collisions and other cases where nodes may be advertising a service they
274  // do not actually support. Other service bits should be allocated via the
275  // BIP process.
276 };
277 
303 
305 void SetServiceFlagsIBDCache(bool status);
306 
312 static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
313  return !(GetDesirableServiceFlags(services) & (~services));
314 }
315 
320 static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
321  return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
322 }
323 
325 class CAddress : public CService
326 {
327 public:
328  CAddress();
329  explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
330 
331  void Init();
332 
334 
335  template <typename Stream, typename Operation>
336  inline void SerializationOp(Stream& s, Operation ser_action)
337  {
338  if (ser_action.ForRead())
339  Init();
340  int nVersion = s.GetVersion();
341  if (s.GetType() & SER_DISK)
342  READWRITE(nVersion);
343  if ((s.GetType() & SER_DISK) ||
344  (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
345  READWRITE(nTime);
346  uint64_t nServicesInt = nServices;
347  READWRITE(nServicesInt);
348  nServices = static_cast<ServiceFlags>(nServicesInt);
349  READWRITEAS(CService, *this);
350  }
351 
352  // TODO: make private (improves encapsulation)
353 public:
355 
356  // disk and network only
357  unsigned int nTime;
358 };
359 
361 const uint32_t MSG_WITNESS_FLAG = 1 << 30;
362 const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
363 
369 {
371  MSG_TX = 1,
373  // The following can only occur in getdata. Invs always use TX or BLOCK.
379 };
380 
382 class CInv
383 {
384 public:
385  CInv();
386  CInv(int typeIn, const uint256& hashIn);
387 
389 
390  template <typename Stream, typename Operation>
391  inline void SerializationOp(Stream& s, Operation ser_action)
392  {
393  READWRITE(type);
394  READWRITE(hash);
395  }
396 
397  friend bool operator<(const CInv& a, const CInv& b);
398 
399  std::string GetCommand() const;
400  std::string ToString() const;
401 
402 public:
403  int type;
405 };
406 
407 #endif // BITCOIN_PROTOCOL_H
Definition: protocol.h:325
const char * ADDR
Definition: protocol.cpp:20
const char * VERACK
Definition: protocol.cpp:19
Definition: serialize.h:176
ServiceFlags
Definition: protocol.h:247
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:31
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:57
Defined in BIP144.
Definition: protocol.h:377
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:200
Defined in BIP152.
Definition: protocol.h:375
Definition: protocol.h:371
uint256 hash
Definition: protocol.h:404
const char * GETBLOCKTXN
Definition: protocol.cpp:42
const char * NOTFOUND
Definition: protocol.cpp:33
const char * BLOCK
Definition: protocol.cpp:28
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:336
Defined in BIP144.
Definition: protocol.h:376
const char * INV
Definition: protocol.cpp:21
Definition: protocol.h:252
const char * SENDCMPCT
Definition: protocol.cpp:40
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:49
ADD_SERIALIZE_METHODS
Definition: protocol.h:46
const char * MEMPOOL
Definition: protocol.cpp:30
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:34
const char * TX
Definition: protocol.cpp:26
Definition: protocol.h:260
const char * HEADERS
Definition: protocol.cpp:27
GetDataMsg
Definition: protocol.h:368
const uint32_t MSG_WITNESS_FLAG
Definition: protocol.h:361
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:169
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:101
static constexpr size_t CHECKSUM_OFFSET
Definition: protocol.h:36
const char * FILTERLOAD
Definition: protocol.cpp:34
Definition: protocol.h:372
unsigned char uint8_t
Definition: stdint.h:19
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:362
const char * FILTERADD
Definition: protocol.cpp:35
#define READWRITEAS(type, obj)
Definition: serialize.h:185
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:58
uint32_t nMessageSize
Definition: protocol.h:59
const char * GETBLOCKS
Definition: protocol.cpp:24
const char * REJECT
Definition: protocol.cpp:37
Definition: protocol.h:267
Definition: protocol.cpp:17
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:32
Definition: protocol.h:370
const char * SENDHEADERS
Definition: protocol.cpp:38
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:79
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Definition: protocol.cpp:132
std::string GetCommand() const
Definition: protocol.cpp:96
Definition: protocol.h:249
std::string GetCommand() const
Definition: protocol.cpp:174
Definition: serialize.h:177
Definition: protocol.h:378
static bool HasAllDesirableServiceFlags(ServiceFlags services)
Definition: protocol.h:312
unsigned int uint32_t
Definition: stdint.h:21
CInv()
Definition: protocol.cpp:161
unsigned long long uint64_t
Definition: stdint.h:22
Definition: protocol.h:28
const char * GETDATA
Definition: protocol.cpp:22
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:391
const char * FILTERCLEAR
Definition: protocol.cpp:36
const char * PONG
Definition: protocol.cpp:32
ServiceFlags nServices
Definition: protocol.h:354
const char * VERSION
Definition: protocol.cpp:18
static bool MayHaveUsefulAddressDB(ServiceFlags services)
Definition: protocol.h:320
void SetServiceFlagsIBDCache(bool status)
Definition: protocol.cpp:139
CAddress()
Definition: protocol.cpp:144
std::string ToString() const
Definition: protocol.cpp:191
const char * GETHEADERS
Definition: protocol.cpp:25
Definition: uint256.h:121
unsigned int nTime
Definition: protocol.h:357
const char * GETADDR
Definition: protocol.cpp:29
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:38
Definition: protocol.h:263
ADD_SERIALIZE_METHODS
Definition: protocol.h:333
const char * BLOCKTXN
Definition: protocol.cpp:43
const char * PING
Definition: protocol.cpp:31
static const int CADDR_TIME_VERSION
Definition: version.h:25
int type
Definition: protocol.h:403
Definition: protocol.h:382
Definition: netaddress.h:140
const char * CMPCTBLOCK
Definition: protocol.cpp:41
const char * MERKLEBLOCK
Definition: protocol.cpp:23
static constexpr size_t HEADER_SIZE
Definition: protocol.h:37
#define READWRITE(...)
Definition: serialize.h:184
const char * FEEFILTER
Definition: protocol.cpp:39
Definition: protocol.h:256
void Init()
Definition: protocol.cpp:155
Defined in BIP37.
Definition: protocol.h:374
ADD_SERIALIZE_METHODS
Definition: protocol.h:388
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:60
static constexpr size_t MESSAGE_SIZE_OFFSET
Definition: protocol.h:35
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:33