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