Bitcoin ABC 0.33.5
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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 <chainparams.h>
9#include <common/system.h>
10#include <config.h>
11#include <logging.h>
12
13#include <atomic>
14
15static std::atomic<bool> g_initial_block_download_completed(false);
16
17namespace NetMsgType {
18const char *VERSION = "version";
19const char *VERACK = "verack";
20const char *ADDR = "addr";
21const char *ADDRV2 = "addrv2";
22const char *SENDADDRV2 = "sendaddrv2";
23const char *INV = "inv";
24const char *GETDATA = "getdata";
25const char *MERKLEBLOCK = "merkleblock";
26const char *GETBLOCKS = "getblocks";
27const char *GETHEADERS = "getheaders";
28const char *TX = "tx";
29const char *HEADERS = "headers";
30const char *BLOCK = "block";
31const char *GETADDR = "getaddr";
32const char *MEMPOOL = "mempool";
33const char *PING = "ping";
34const char *PONG = "pong";
35const char *NOTFOUND = "notfound";
36const char *FILTERLOAD = "filterload";
37const char *FILTERADD = "filteradd";
38const char *FILTERCLEAR = "filterclear";
39const char *SENDHEADERS = "sendheaders";
40const char *FEEFILTER = "feefilter";
41const char *SENDCMPCT = "sendcmpct";
42const char *CMPCTBLOCK = "cmpctblock";
43const char *GETBLOCKTXN = "getblocktxn";
44const char *BLOCKTXN = "blocktxn";
45const char *GETCFILTERS = "getcfilters";
46const char *CFILTER = "cfilter";
47const char *GETCFHEADERS = "getcfheaders";
48const char *CFHEADERS = "cfheaders";
49const char *GETCFCHECKPT = "getcfcheckpt";
50const char *CFCHECKPT = "cfcheckpt";
51const char *AVAHELLO = "avahello";
52const char *AVAPOLL = "avapoll";
53const char *AVARESPONSE = "avaresponse";
54const char *AVAPROOF = "avaproof";
55const char *GETAVAADDR = "getavaaddr";
56const char *GETAVAPROOFS = "getavaproofs";
57const char *AVAPROOFS = "avaproofs";
58const char *AVAPROOFSREQ = "avaproofsreq";
59
60bool IsBlockLike(const std::string &strCommand) {
61 return strCommand == NetMsgType::BLOCK ||
62 strCommand == NetMsgType::CMPCTBLOCK ||
63 strCommand == NetMsgType::BLOCKTXN;
64}
65}; // namespace NetMsgType
66
71static const std::string allNetMessageTypes[] = {
86};
87static const std::vector<std::string>
89 std::end(allNetMessageTypes));
90
92 memset(std::begin(pchMessageStart), 0, MESSAGE_START_SIZE);
93 memset(m_msg_type.data(), 0, sizeof(m_msg_type));
94 nMessageSize = -1;
95 memset(pchChecksum, 0, CHECKSUM_SIZE);
96}
97
99 const char *msg_type,
100 unsigned int nMessageSizeIn) {
101 memcpy(std::begin(pchMessageStart), std::begin(pchMessageStartIn),
103 // Copy the message type name, zero-padding to MESSAGE_TYPE_SIZE bytes
104 size_t i = 0;
105 for (; i < m_msg_type.size() && msg_type[i] != 0; ++i) {
106 m_msg_type[i] = msg_type[i];
107 }
108 // Assert that the message type name passed in is not longer than
109 // MESSAGE_TYPE_SIZE
110 assert(msg_type[i] == 0);
111 for (; i < m_msg_type.size(); ++i) {
112 m_msg_type[i] = 0;
113 }
114
115 nMessageSize = nMessageSizeIn;
116 memset(pchChecksum, 0, CHECKSUM_SIZE);
117}
118
120 return std::string(m_msg_type.data(),
121 m_msg_type.data() +
123}
124
126 // Check the message type string for errors
127 for (const char *p1 = m_msg_type.data();
128 p1 < m_msg_type.data() + MESSAGE_TYPE_SIZE; p1++) {
129 if (*p1 == 0) {
130 // Must be all zeros after the first zero
131 for (; p1 < m_msg_type.data() + MESSAGE_TYPE_SIZE; ++p1) {
132 if (*p1 != 0) {
133 return false;
134 }
135 }
136 } else if (*p1 < ' ' || *p1 > 0x7E) {
137 return false;
138 }
139 }
140
141 return true;
142}
143
144bool CMessageHeader::IsOversized(const Config &config) const {
145 // Scale the maximum accepted size with the block size for messages with
146 // block content
148 return nMessageSize > 2 * config.GetMaxBlockSize();
149 }
150
152}
153
155 if ((services & NODE_NETWORK_LIMITED) &&
158 }
160}
161
162void SetServiceFlagsIBDCache(bool state) {
164}
165
166std::string CInv::GetMessageType() const {
167 std::string cmd;
168 switch (GetKind()) {
169 case MSG_TX:
170 return cmd.append(NetMsgType::TX);
171 case MSG_BLOCK:
172 return cmd.append(NetMsgType::BLOCK);
174 return cmd.append(NetMsgType::MERKLEBLOCK);
175 case MSG_CMPCT_BLOCK:
176 return cmd.append(NetMsgType::CMPCTBLOCK);
177 case MSG_AVA_PROOF:
178 return cmd.append(NetMsgType::AVAPROOF);
180 // Stake contender does not have a NetMsgType because there is no
181 // contender-specific message, so we hard code the name here.
182 return cmd.append("stakecontender");
183 default:
184 throw std::out_of_range(strprintf(
185 "CInv::GetMessageType(): type=%d unknown type", type));
186 }
187}
188
189std::string CInv::ToString() const {
190 try {
191 return strprintf("%s %s", GetMessageType(), hash.ToString());
192 } catch (const std::out_of_range &) {
193 return strprintf("0x%08x %s", type, hash.ToString());
194 }
195}
196
197const std::vector<std::string> &getAllNetMessageTypes() {
199}
200
206static std::string serviceFlagToStr(const size_t bit) {
207 const uint64_t service_flag = 1ULL << bit;
208 switch (ServiceFlags(service_flag)) {
209 case NODE_NONE:
210 // impossible
211 abort();
212 case NODE_NETWORK:
213 return "NETWORK";
214 case NODE_GETUTXO:
215 return "GETUTXO";
216 case NODE_BLOOM:
217 return "BLOOM";
219 return "NETWORK_LIMITED";
221 return "COMPACT_FILTERS";
222 case NODE_AVALANCHE:
223 return "AVALANCHE";
224 default:
225 std::ostringstream stream;
226 stream.imbue(std::locale::classic());
227 stream << "UNKNOWN[";
228 stream << "2^" << bit;
229 stream << "]";
230 return stream.str();
231 }
232}
233
234std::vector<std::string> serviceFlagsToStr(const uint64_t flags) {
235 std::vector<std::string> str_flags;
236
237 for (size_t i = 0; i < sizeof(flags) * 8; ++i) {
238 if (flags & (1ULL << i)) {
239 str_flags.emplace_back(serviceFlagToStr(i));
240 }
241 }
242
243 return str_flags;
244}
int flags
Definition: bitcoin-tx.cpp:546
std::string GetMessageType() const
Definition: protocol.cpp:166
uint32_t GetKind() const
Definition: protocol.h:606
std::string ToString() const
Definition: protocol.cpp:189
uint32_t type
Definition: protocol.h:591
uint256 hash
Definition: protocol.h:592
std::array< char, MESSAGE_TYPE_SIZE > m_msg_type
Definition: protocol.h:70
static constexpr size_t MESSAGE_TYPE_SIZE
Definition: protocol.h:37
bool IsMessageTypeValid() const
Definition: protocol.cpp:125
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:39
MessageMagic pchMessageStart
Definition: protocol.h:69
std::string GetMessageType() const
Definition: protocol.cpp:119
bool IsOversized(const Config &config) const
Definition: protocol.cpp:144
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:72
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:36
uint32_t nMessageSize
Definition: protocol.h:71
std::array< uint8_t, MESSAGE_START_SIZE > MessageMagic
Definition: protocol.h:47
Definition: config.h:19
virtual uint64_t GetMaxBlockSize() const =0
std::string ToString() const
Definition: uint256.h:80
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
Bitcoin protocol message types.
Definition: protocol.cpp:17
bool IsBlockLike(const std::string &strCommand)
Indicate if the message is used to transmit the content of a block.
Definition: protocol.cpp:60
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:36
const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
Definition: protocol.cpp:48
const char * AVAPROOFSREQ
Request for missing avalanche proofs after an avaproofs message has been processed.
Definition: protocol.cpp:58
const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
Definition: protocol.cpp:46
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:30
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.cpp:38
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:29
const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message,...
Definition: protocol.cpp:21
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:39
const char * AVAPROOFS
The avaproofs message the proof short ids of all the valid proofs that we know.
Definition: protocol.cpp:57
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:34
const char * GETAVAPROOFS
The getavaproofs message requests an avaproofs message that provides the proof short ids of all the v...
Definition: protocol.cpp:56
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:41
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:31
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.cpp:49
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:35
const char * GETAVAADDR
The getavaaddr message requests an addr message from the receiving node, containing IP addresses of t...
Definition: protocol.cpp:55
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids".
Definition: protocol.cpp:42
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:32
const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
Definition: protocol.cpp:45
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:28
const char * AVAHELLO
Contains a delegation and a signature.
Definition: protocol.cpp:51
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:37
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:20
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:26
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:40
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:27
const char * AVARESPONSE
Contains an avalanche::Response.
Definition: protocol.cpp:53
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:24
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:44
const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks,...
Definition: protocol.cpp:47
const char * SENDADDRV2
The sendaddrv2 message signals support for receiving ADDRV2 messages (BIP155).
Definition: protocol.cpp:22
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.cpp:33
const char * AVAPOLL
Contains an avalanche::Poll.
Definition: protocol.cpp:52
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:25
const char * AVAPROOF
Contains an avalanche::Proof.
Definition: protocol.cpp:54
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.cpp:50
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:43
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:23
const std::vector< std::string > & getAllNetMessageTypes()
Get a vector of all valid message types (see above)
Definition: protocol.cpp:197
std::vector< std::string > serviceFlagsToStr(const uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:234
void SetServiceFlagsIBDCache(bool state)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:162
static const std::vector< std::string > allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes))
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:154
static std::atomic< bool > g_initial_block_download_completed(false)
static std::string serviceFlagToStr(const size_t bit)
Convert a service flag (NODE_*) to a human readable string.
Definition: protocol.cpp:206
static const std::string allNetMessageTypes[]
All known message types.
Definition: protocol.cpp:71
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (Currently 2MB).
Definition: protocol.h:25
@ MSG_TX
Definition: protocol.h:573
@ MSG_FILTERED_BLOCK
Defined in BIP37.
Definition: protocol.h:577
@ MSG_AVA_STAKE_CONTENDER
Definition: protocol.h:581
@ MSG_AVA_PROOF
Definition: protocol.h:580
@ MSG_BLOCK
Definition: protocol.h:574
@ MSG_CMPCT_BLOCK
Defined in BIP152.
Definition: protocol.h:579
ServiceFlags
nServices flags.
Definition: protocol.h:335
@ NODE_NONE
Definition: protocol.h:338
@ NODE_GETUTXO
Definition: protocol.h:347
@ NODE_NETWORK_LIMITED
Definition: protocol.h:365
@ NODE_BLOOM
Definition: protocol.h:352
@ NODE_NETWORK
Definition: protocol.h:342
@ NODE_COMPACT_FILTERS
Definition: protocol.h:360
@ NODE_AVALANCHE
Definition: protocol.h:380
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())