Bitcoin Core  21.99.0
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <protocol.h>
7 
8 #include <util/system.h>
9 
10 static std::atomic<bool> g_initial_block_download_completed(false);
11 
12 namespace NetMsgType {
13 const char *VERSION="version";
14 const char *VERACK="verack";
15 const char *ADDR="addr";
16 const char *ADDRV2="addrv2";
17 const char *SENDADDRV2="sendaddrv2";
18 const char *INV="inv";
19 const char *GETDATA="getdata";
20 const char *MERKLEBLOCK="merkleblock";
21 const char *GETBLOCKS="getblocks";
22 const char *GETHEADERS="getheaders";
23 const char *TX="tx";
24 const char *HEADERS="headers";
25 const char *BLOCK="block";
26 const char *GETADDR="getaddr";
27 const char *MEMPOOL="mempool";
28 const char *PING="ping";
29 const char *PONG="pong";
30 const char *NOTFOUND="notfound";
31 const char *FILTERLOAD="filterload";
32 const char *FILTERADD="filteradd";
33 const char *FILTERCLEAR="filterclear";
34 const char *SENDHEADERS="sendheaders";
35 const char *FEEFILTER="feefilter";
36 const char *SENDCMPCT="sendcmpct";
37 const char *CMPCTBLOCK="cmpctblock";
38 const char *GETBLOCKTXN="getblocktxn";
39 const char *BLOCKTXN="blocktxn";
40 const char *GETCFILTERS="getcfilters";
41 const char *CFILTER="cfilter";
42 const char *GETCFHEADERS="getcfheaders";
43 const char *CFHEADERS="cfheaders";
44 const char *GETCFCHECKPT="getcfcheckpt";
45 const char *CFCHECKPT="cfcheckpt";
46 const char *WTXIDRELAY="wtxidrelay";
47 } // namespace NetMsgType
48 
52 const static std::string allNetMessageTypes[] = {
87 };
88 const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));
89 
91 {
93  memset(pchCommand, 0, sizeof(pchCommand));
94  nMessageSize = -1;
95  memset(pchChecksum, 0, CHECKSUM_SIZE);
96 }
97 
98 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
99 {
100  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
101 
102  // Copy the command name, zero-padding to COMMAND_SIZE bytes
103  size_t i = 0;
104  for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
105  assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
106  for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;
107 
108  nMessageSize = nMessageSizeIn;
109  memset(pchChecksum, 0, CHECKSUM_SIZE);
110 }
111 
112 std::string CMessageHeader::GetCommand() const
113 {
114  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
115 }
116 
118 {
119  // Check the command string for errors
120  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; ++p1) {
121  if (*p1 == 0) {
122  // Must be all zeros after the first zero
123  for (; p1 < pchCommand + COMMAND_SIZE; ++p1) {
124  if (*p1 != 0) {
125  return false;
126  }
127  }
128  } else if (*p1 < ' ' || *p1 > 0x7E) {
129  return false;
130  }
131  }
132 
133  return true;
134 }
135 
136 
140  }
142 }
143 
144 void SetServiceFlagsIBDCache(bool state) {
146 }
147 
149 {
150  type = 0;
151  hash.SetNull();
152 }
153 
154 CInv::CInv(uint32_t typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
155 
156 bool operator<(const CInv& a, const CInv& b)
157 {
158  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
159 }
160 
161 std::string CInv::GetCommand() const
162 {
163  std::string cmd;
164  if (type & MSG_WITNESS_FLAG)
165  cmd.append("witness-");
166  int masked = type & MSG_TYPE_MASK;
167  switch (masked)
168  {
169  case MSG_TX: return cmd.append(NetMsgType::TX);
170  // WTX is not a message type, just an inv type
171  case MSG_WTX: return cmd.append("wtx");
172  case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
173  case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
174  case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
175  default:
176  throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
177  }
178 }
179 
180 std::string CInv::ToString() const
181 {
182  try {
183  return strprintf("%s %s", GetCommand(), hash.ToString());
184  } catch(const std::out_of_range &) {
185  return strprintf("0x%08x %s", type, hash.ToString());
186  }
187 }
188 
189 const std::vector<std::string> &getAllNetMessageTypes()
190 {
191  return allNetMessageTypesVec;
192 }
193 
199 static std::string serviceFlagToStr(size_t bit)
200 {
201  const uint64_t service_flag = 1ULL << bit;
202  switch ((ServiceFlags)service_flag) {
203  case NODE_NONE: abort(); // impossible
204  case NODE_NETWORK: return "NETWORK";
205  case NODE_BLOOM: return "BLOOM";
206  case NODE_WITNESS: return "WITNESS";
207  case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
208  case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
209  // Not using default, so we get warned when a case is missing
210  }
211 
212  std::ostringstream stream;
213  stream.imbue(std::locale::classic());
214  stream << "UNKNOWN[";
215  stream << "2^" << bit;
216  stream << "]";
217  return stream.str();
218 }
219 
220 std::vector<std::string> serviceFlagsToStr(uint64_t flags)
221 {
222  std::vector<std::string> str_flags;
223 
224  for (size_t i = 0; i < sizeof(flags) * 8; ++i) {
225  if (flags & (1ULL << i)) {
226  str_flags.emplace_back(serviceFlagToStr(i));
227  }
228  }
229 
230  return str_flags;
231 }
232 
233 GenTxid ToGenTxid(const CInv& inv)
234 {
235  assert(inv.IsGenTxMsg());
236  return {inv.IsMsgWtx(), inv.hash};
237 }
CMessageHeader::pchCommand
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:53
MSG_BLOCK
@ MSG_BLOCK
Definition: protocol.h:410
SetServiceFlagsIBDCache
void SetServiceFlagsIBDCache(bool state)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:144
NetMsgType
Bitcoin protocol message types.
Definition: protocol.cpp:12
GenTxid
A generic txid reference (txid or wtxid).
Definition: transaction.h:390
CMessageHeader::nMessageSize
uint32_t nMessageSize
Definition: protocol.h:54
flags
int flags
Definition: bitcoin-tx.cpp:512
MSG_FILTERED_BLOCK
@ MSG_FILTERED_BLOCK
Defined in BIP37.
Definition: protocol.h:413
MSG_CMPCT_BLOCK
@ MSG_CMPCT_BLOCK
Defined in BIP152.
Definition: protocol.h:414
serviceFlagsToStr
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:220
NetMsgType::PING
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.cpp:28
NetMsgType::FILTERLOAD
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:31
GetDesirableServiceFlags
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:137
operator<
bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:156
base_blob::SetNull
void SetNull()
Definition: uint256.h:39
NetMsgType::SENDADDRV2
const char * SENDADDRV2
The sendaddrv2 message signals support for receiving ADDRV2 messages (BIP155).
Definition: protocol.cpp:17
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:269
CMessageHeader::GetCommand
std::string GetCommand() const
Definition: protocol.cpp:112
NODE_NETWORK_LIMITED
@ NODE_NETWORK_LIMITED
Definition: protocol.h:289
allNetMessageTypesVec
const static std::vector< std::string > allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes))
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:275
NetMsgType::CFHEADERS
const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
Definition: protocol.cpp:43
CInv
inv message data
Definition: protocol.h:423
NetMsgType::PONG
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:29
NODE_COMPACT_FILTERS
@ NODE_COMPACT_FILTERS
Definition: protocol.h:285
serviceFlagToStr
static std::string serviceFlagToStr(size_t bit)
Convert a service flag (NODE_*) to a human readable string.
Definition: protocol.cpp:199
NetMsgType::SENDCMPCT
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:36
CInv::IsGenTxMsg
bool IsGenTxMsg() const
Definition: protocol.h:445
NetMsgType::ADDRV2
const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message,...
Definition: protocol.cpp:16
MSG_TX
@ MSG_TX
Definition: protocol.h:409
protocol.h
NetMsgType::INV
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:18
NetMsgType::GETHEADERS
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:22
NetMsgType::CFILTER
const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
Definition: protocol.cpp:41
NetMsgType::FEEFILTER
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:35
NetMsgType::HEADERS
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:24
NetMsgType::GETCFCHECKPT
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.cpp:44
NetMsgType::WTXIDRELAY
const char * WTXIDRELAY
Indicates that a node prefers to relay transactions via wtxid, rather than txid.
Definition: protocol.cpp:46
CMessageHeader::COMMAND_SIZE
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:32
CMessageHeader::IsCommandValid
bool IsCommandValid() const
Definition: protocol.cpp:117
NODE_BLOOM
@ NODE_BLOOM
Definition: protocol.h:279
strnlen
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
NetMsgType::GETCFHEADERS
const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks,...
Definition: protocol.cpp:42
NetMsgType::FILTERCLEAR
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.cpp:33
base_blob::ToString
std::string ToString() const
Definition: uint256.cpp:64
uint256
256-bit opaque blob.
Definition: uint256.h:124
NetMsgType::SENDHEADERS
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:34
CMessageHeader::MESSAGE_START_SIZE
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:31
NetMsgType::GETBLOCKS
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:21
CMessageHeader::CMessageHeader
CMessageHeader()
Definition: protocol.cpp:90
NetMsgType::GETDATA
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:19
CInv::GetCommand
std::string GetCommand() const
Definition: protocol.cpp:161
NetMsgType::NOTFOUND
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:30
NODE_NONE
@ NODE_NONE
Definition: protocol.h:272
CMessageHeader::CHECKSUM_SIZE
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:34
NetMsgType::ADDR
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:15
NetMsgType::BLOCK
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:25
NODE_WITNESS
@ NODE_WITNESS
Definition: protocol.h:282
system.h
ToGenTxid
GenTxid ToGenTxid(const CInv &inv)
Convert a TX/WITNESS_TX/WTX CInv to a GenTxid.
Definition: protocol.cpp:233
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
NetMsgType::MEMPOOL
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:27
CInv::CInv
CInv()
Definition: protocol.cpp:148
NetMsgType::TX
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:23
CInv::hash
uint256 hash
Definition: protocol.h:455
MSG_WTX
@ MSG_WTX
Defined in BIP 339.
Definition: protocol.h:411
NetMsgType::CMPCTBLOCK
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids".
Definition: protocol.cpp:37
MSG_TYPE_MASK
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:401
CMessageHeader::pchMessageStart
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:52
getAllNetMessageTypes
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:189
NetMsgType::VERACK
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:14
NetMsgType::FILTERADD
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:32
NetMsgType::GETBLOCKTXN
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:38
CInv::IsMsgWtx
bool IsMsgWtx() const
Definition: protocol.h:439
NetMsgType::CFCHECKPT
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.cpp:45
g_initial_block_download_completed
static std::atomic< bool > g_initial_block_download_completed(false)
NetMsgType::VERSION
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:13
CMessageHeader::pchChecksum
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:55
allNetMessageTypes
const static std::string allNetMessageTypes[]
All known message types.
Definition: protocol.cpp:52
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
NetMsgType::BLOCKTXN
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:39
CInv::type
uint32_t type
Definition: protocol.h:454
CInv::ToString
std::string ToString() const
Definition: protocol.cpp:180
MSG_WITNESS_FLAG
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:400
NetMsgType::GETCFILTERS
const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
Definition: protocol.cpp:40
NetMsgType::GETADDR
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:26
NetMsgType::MERKLEBLOCK
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:20