Bitcoin Core 31.99.0
P2P Digital Currency
protocol.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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 BITCOIN_PROTOCOL_H
7#define BITCOIN_PROTOCOL_H
8
9#include <kernel/messagestartchars.h> // IWYU pragma: export
10#include <netaddress.h>
12#include <serialize.h>
13#include <streams.h>
14#include <uint256.h>
15#include <util/time.h>
16
17#include <array>
18#include <cstdint>
19#include <limits>
20#include <string>
21
29{
30public:
31 static constexpr size_t MESSAGE_TYPE_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 = std::tuple_size_v<MessageStartChars> + MESSAGE_TYPE_SIZE;
36 static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + MESSAGE_TYPE_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
37
38 explicit CMessageHeader() = default;
39
43 CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn);
44
45 std::string GetMessageType() const;
46 bool IsMessageTypeValid() const;
47
48 SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.m_msg_type, obj.nMessageSize, obj.pchChecksum); }
49
52 uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
54};
55
60namespace NetMsgType {
65inline constexpr const char* VERSION{"version"};
70inline constexpr const char* VERACK{"verack"};
75inline constexpr const char* ADDR{"addr"};
81inline constexpr const char* ADDRV2{"addrv2"};
87inline constexpr const char* SENDADDRV2{"sendaddrv2"};
92inline constexpr const char* INV{"inv"};
96inline constexpr const char* GETDATA{"getdata"};
102inline constexpr const char* MERKLEBLOCK{"merkleblock"};
107inline constexpr const char* GETBLOCKS{"getblocks"};
113inline constexpr const char* GETHEADERS{"getheaders"};
117inline constexpr const char* TX{"tx"};
123inline constexpr const char* HEADERS{"headers"};
127inline constexpr const char* BLOCK{"block"};
132inline constexpr const char* GETADDR{"getaddr"};
139inline constexpr const char* MEMPOOL{"mempool"};
144inline constexpr const char* PING{"ping"};
150inline constexpr const char* PONG{"pong"};
156inline constexpr const char* NOTFOUND{"notfound"};
164inline constexpr const char* FILTERLOAD{"filterload"};
172inline constexpr const char* FILTERADD{"filteradd"};
180inline constexpr const char* FILTERCLEAR{"filterclear"};
186inline constexpr const char* SENDHEADERS{"sendheaders"};
192inline constexpr const char* FEEFILTER{"feefilter"};
200inline constexpr const char* SENDCMPCT{"sendcmpct"};
206inline constexpr const char* CMPCTBLOCK{"cmpctblock"};
212inline constexpr const char* GETBLOCKTXN{"getblocktxn"};
218inline constexpr const char* BLOCKTXN{"blocktxn"};
224inline constexpr const char* GETCFILTERS{"getcfilters"};
229inline constexpr const char* CFILTER{"cfilter"};
237inline constexpr const char* GETCFHEADERS{"getcfheaders"};
242inline constexpr const char* CFHEADERS{"cfheaders"};
249inline constexpr const char* GETCFCHECKPT{"getcfcheckpt"};
254inline constexpr const char* CFCHECKPT{"cfcheckpt"};
260inline constexpr const char* WTXIDRELAY{"wtxidrelay"};
266inline constexpr const char* SENDTXRCNCL{"sendtxrcncl"};
270inline constexpr const char* FEATURE{"feature"};
271}; // namespace NetMsgType
272
274inline const std::array ALL_NET_MESSAGE_TYPES{std::to_array<std::string>({
311})};
312
313static constexpr size_t MAX_FEATUREID_LENGTH{80};
314static constexpr size_t MAX_FEATUREDATA_LENGTH{512};
315
316namespace NetMsgFeature {
317//inline constexpr std::string_view FOO{"BIP-FOO"};
318}
319
321enum ServiceFlags : uint64_t {
322 // NOTE: When adding here, be sure to update serviceFlagToStr too
323 // Nothing
325 // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
326 // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.
327 NODE_NETWORK = (1 << 0),
328 // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
329 NODE_BLOOM = (1 << 2),
330 // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
331 // witness data.
332 NODE_WITNESS = (1 << 3),
333 // NODE_COMPACT_FILTERS means the node will service basic block filter requests.
334 // See BIP157 and BIP158 for details on how this is implemented.
336 // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
337 // serving the last 288 (2 day) blocks
338 // See BIP159 for details on how this is implemented.
340
341 // NODE_P2P_V2 means the node supports BIP324 transport
342 NODE_P2P_V2 = (1 << 11),
343
344 // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
345 // isn't getting used, or one not being used much, and notify the
346 // bitcoin-development mailing list. Remember that service bits are just
347 // unauthenticated advertisements, so your code must be robust against
348 // collisions and other cases where nodes may be advertising a service they
349 // do not actually support. Other service bits should be allocated via the
350 // BIP process.
351};
352
358std::vector<std::string> serviceFlagsToStr(uint64_t flags);
359
367
372static inline bool MayHaveUsefulAddressDB(ServiceFlags services)
373{
374 return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
375}
376
378class CAddress : public CService
379{
380 static constexpr std::chrono::seconds TIME_INIT{100000000};
381
398 static constexpr uint32_t DISK_VERSION_INIT{220000};
399 static constexpr uint32_t DISK_VERSION_IGNORE_MASK{0b00000000'00000111'11111111'11111111};
403 static constexpr uint32_t DISK_VERSION_ADDRV2{1 << 29};
404 static_assert((DISK_VERSION_INIT & ~DISK_VERSION_IGNORE_MASK) == 0, "DISK_VERSION_INIT must be covered by DISK_VERSION_IGNORE_MASK");
405 static_assert((DISK_VERSION_ADDRV2 & DISK_VERSION_IGNORE_MASK) == 0, "DISK_VERSION_ADDRV2 must not be covered by DISK_VERSION_IGNORE_MASK");
406
407public:
409 CAddress(CService ipIn, ServiceFlags nServicesIn) : CService{ipIn}, nServices{nServicesIn} {};
410 CAddress(CService ipIn, ServiceFlags nServicesIn, NodeSeconds time) : CService{ipIn}, nTime{time}, nServices{nServicesIn} {};
411
412 enum class Format {
413 Disk,
414 Network,
415 };
417 const Format fmt;
419 };
424
426 {
427 bool use_v2;
428 auto& params = SER_PARAMS(SerParams);
429 if (params.fmt == Format::Disk) {
430 // In the disk serialization format, the encoding (v1 or v2) is determined by a flag version
431 // that's part of the serialization itself. ADDRV2_FORMAT in the stream version only determines
432 // whether V2 is chosen/permitted at all.
433 uint32_t stored_format_version = DISK_VERSION_INIT;
434 if (params.enc == Encoding::V2) stored_format_version |= DISK_VERSION_ADDRV2;
435 READWRITE(stored_format_version);
436 stored_format_version &= ~DISK_VERSION_IGNORE_MASK; // ignore low bits
437 if (stored_format_version == 0) {
438 use_v2 = false;
439 } else if (stored_format_version == DISK_VERSION_ADDRV2 && params.enc == Encoding::V2) {
440 // Only support v2 deserialization if V2 is set.
441 use_v2 = true;
442 } else {
443 throw std::ios_base::failure("Unsupported CAddress disk format version");
444 }
445 } else {
446 assert(params.fmt == Format::Network);
447 // In the network serialization format, the encoding (v1 or v2) is determined directly by
448 // the value of enc in the stream params, as no explicitly encoded version
449 // exists in the stream.
450 use_v2 = params.enc == Encoding::V2;
451 }
452
454 // nServices is serialized as CompactSize in V2; as uint64_t in V1.
455 if (use_v2) {
456 uint64_t services_tmp;
457 SER_WRITE(obj, services_tmp = obj.nServices);
459 SER_READ(obj, obj.nServices = static_cast<ServiceFlags>(services_tmp));
460 } else {
461 READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));
462 }
463 // Invoke V1/V2 serializer for CService parent object.
464 const auto ser_params{use_v2 ? CNetAddr::V2 : CNetAddr::V1};
465 READWRITE(ser_params(AsBase<CService>(obj)));
466 }
467
472
473 friend bool operator==(const CAddress& a, const CAddress& b)
474 {
475 return a.nTime == b.nTime &&
476 a.nServices == b.nServices &&
477 static_cast<const CService&>(a) == static_cast<const CService&>(b);
478 }
479};
480
482const uint32_t MSG_WITNESS_FLAG = 1 << 30;
483const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
484
489enum GetDataMsg : uint32_t {
494 // The following can only occur in getdata. Invs always use TX/WTX or BLOCK.
499 // MSG_FILTERED_WITNESS_BLOCK is defined in BIP144 as reserved for future
500 // use and remains unused.
501 // MSG_FILTERED_WITNESS_BLOCK = MSG_FILTERED_BLOCK | MSG_WITNESS_FLAG,
502};
503
505class CInv
506{
507public:
508 CInv();
509 CInv(uint32_t typeIn, const uint256& hashIn);
510
511 SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }
512
513 friend bool operator<(const CInv& a, const CInv& b);
514
515 std::string GetMessageType() const;
516 std::string ToString() const;
517
518 // Single-message helper methods
519 bool IsMsgTx() const { return type == MSG_TX; }
520 bool IsMsgBlk() const { return type == MSG_BLOCK; }
521 bool IsMsgWtx() const { return type == MSG_WTX; }
522 bool IsMsgFilteredBlk() const { return type == MSG_FILTERED_BLOCK; }
523 bool IsMsgCmpctBlk() const { return type == MSG_CMPCT_BLOCK; }
524 bool IsMsgWitnessBlk() const { return type == MSG_WITNESS_BLOCK; }
525
526 // Combined-message helper methods
527 bool IsGenTxMsg() const
528 {
529 return type == MSG_TX || type == MSG_WTX || type == MSG_WITNESS_TX;
530 }
531 bool IsGenBlkMsg() const
532 {
534 }
535
536 uint32_t type;
538};
539
541GenTxid ToGenTxid(const CInv& inv);
542
543#endif // BITCOIN_PROTOCOL_H
int flags
Definition: bitcoin-tx.cpp:530
A CService with information about it as peer.
Definition: protocol.h:379
static constexpr uint32_t DISK_VERSION_IGNORE_MASK
Definition: protocol.h:399
SERIALIZE_METHODS(CAddress, obj)
Definition: protocol.h:425
CAddress(CService ipIn, ServiceFlags nServicesIn, NodeSeconds time)
Definition: protocol.h:410
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
Definition: protocol.h:471
static constexpr SerParams V1_NETWORK
Definition: protocol.h:420
NodeSeconds nTime
Always included in serialization. The behavior is unspecified if the value is not representable as ui...
Definition: protocol.h:469
static constexpr uint32_t DISK_VERSION_INIT
Historically, CAddress disk serialization stored the CLIENT_VERSION, optionally OR'ed with the ADDRV2...
Definition: protocol.h:398
static constexpr SerParams V2_NETWORK
Definition: protocol.h:421
CAddress()
Definition: protocol.h:408
friend bool operator==(const CAddress &a, const CAddress &b)
Definition: protocol.h:473
static constexpr SerParams V1_DISK
Definition: protocol.h:422
CAddress(CService ipIn, ServiceFlags nServicesIn)
Definition: protocol.h:409
static constexpr std::chrono::seconds TIME_INIT
Definition: protocol.h:380
static constexpr uint32_t DISK_VERSION_ADDRV2
The version number written in disk serialized addresses to indicate V2 serializations.
Definition: protocol.h:403
static constexpr SerParams V2_DISK
Definition: protocol.h:423
inv message data
Definition: protocol.h:506
std::string GetMessageType() const
Definition: protocol.cpp:58
bool IsMsgCmpctBlk() const
Definition: protocol.h:523
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:53
bool IsMsgBlk() const
Definition: protocol.h:520
std::string ToString() const
Definition: protocol.cpp:77
bool IsMsgWtx() const
Definition: protocol.h:521
CInv()
Definition: protocol.cpp:45
uint32_t type
Definition: protocol.h:536
bool IsGenTxMsg() const
Definition: protocol.h:527
bool IsMsgTx() const
Definition: protocol.h:519
bool IsMsgFilteredBlk() const
Definition: protocol.h:522
uint256 hash
Definition: protocol.h:537
SERIALIZE_METHODS(CInv, obj)
Definition: protocol.h:511
bool IsGenBlkMsg() const
Definition: protocol.h:531
bool IsMsgWitnessBlk() const
Definition: protocol.h:524
Message header.
Definition: protocol.h:29
static constexpr size_t MESSAGE_SIZE_OFFSET
Definition: protocol.h:34
static constexpr size_t CHECKSUM_OFFSET
Definition: protocol.h:35
static constexpr size_t MESSAGE_TYPE_SIZE
Definition: protocol.h:31
bool IsMessageTypeValid() const
Definition: protocol.cpp:26
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:33
CMessageHeader()=default
MessageStartChars pchMessageStart
Definition: protocol.h:50
static constexpr size_t MESSAGE_SIZE_SIZE
Definition: protocol.h:32
std::string GetMessageType() const
Definition: protocol.cpp:21
static constexpr size_t HEADER_SIZE
Definition: protocol.h:36
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:53
SERIALIZE_METHODS(CMessageHeader, obj)
Definition: protocol.h:48
uint32_t nMessageSize
Definition: protocol.h:52
char m_msg_type[MESSAGE_TYPE_SIZE]
Definition: protocol.h:51
static constexpr SerParams V1
Definition: netaddress.h:231
@ V2
BIP155 encoding.
static constexpr SerParams V2
Definition: netaddress.h:232
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
256-bit opaque blob.
Definition: uint256.h:196
std::array< uint8_t, 4 > MessageStartChars
Bitcoin protocol message types.
Definition: protocol.h:60
constexpr const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.h:180
constexpr const char * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
Definition: protocol.h:192
constexpr const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.h:186
constexpr const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.h:107
constexpr const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.h:123
constexpr const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.h:75
constexpr const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.h:212
constexpr const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids".
Definition: protocol.h:206
constexpr const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.h:254
constexpr const char * SENDADDRV2
The sendaddrv2 message signals support for receiving ADDRV2 messages (BIP155).
Definition: protocol.h:87
constexpr const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.h:132
constexpr const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
Definition: protocol.h:224
constexpr const char * PONG
The pong message replies to a ping message, proving to the pinging node that the ponging node is stil...
Definition: protocol.h:150
constexpr const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.h:218
constexpr const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
Definition: protocol.h:242
constexpr const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.h:144
constexpr const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.h:164
constexpr const char * SENDTXRCNCL
Contains a 4-byte version number and an 8-byte salt.
Definition: protocol.h:266
constexpr const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message,...
Definition: protocol.h:81
constexpr const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.h:70
constexpr const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.h:113
constexpr const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.h:172
constexpr const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
Definition: protocol.h:229
constexpr const char * FEATURE
BIP 434 Peer feature negotiation.
Definition: protocol.h:270
constexpr const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.h:96
constexpr const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.h:200
constexpr const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.h:249
constexpr const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.h:92
constexpr const char * TX
The tx message transmits a single transaction.
Definition: protocol.h:117
constexpr const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.h:139
constexpr const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.h:156
constexpr const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.h:102
constexpr const char * WTXIDRELAY
Indicates that a node prefers to relay transactions via wtxid, rather than txid.
Definition: protocol.h:260
constexpr const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.h:127
constexpr const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks,...
Definition: protocol.h:237
constexpr const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.h:65
Network
A network type.
Definition: netaddress.h:33
const std::array ALL_NET_MESSAGE_TYPES
All known message types (see above).
Definition: protocol.h:274
GenTxid ToGenTxid(const CInv &inv)
Convert a TX/WITNESS_TX/WTX CInv to a GenTxid.
Definition: protocol.cpp:121
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:108
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:482
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:483
static constexpr size_t MAX_FEATUREDATA_LENGTH
Definition: protocol.h:314
constexpr ServiceFlags SeedsServiceFlags()
State independent service flags.
Definition: protocol.h:366
GetDataMsg
getdata / inv message types.
Definition: protocol.h:489
@ MSG_TX
Definition: protocol.h:491
@ MSG_FILTERED_BLOCK
Defined in BIP37.
Definition: protocol.h:495
@ MSG_WTX
Defined in BIP 339.
Definition: protocol.h:493
@ MSG_BLOCK
Definition: protocol.h:492
@ UNDEFINED
Definition: protocol.h:490
@ MSG_CMPCT_BLOCK
Defined in BIP152.
Definition: protocol.h:496
@ MSG_WITNESS_BLOCK
Defined in BIP144.
Definition: protocol.h:497
@ MSG_WITNESS_TX
Defined in BIP144.
Definition: protocol.h:498
ServiceFlags
nServices flags
Definition: protocol.h:321
@ NODE_NONE
Definition: protocol.h:324
@ NODE_P2P_V2
Definition: protocol.h:342
@ NODE_WITNESS
Definition: protocol.h:332
@ NODE_NETWORK_LIMITED
Definition: protocol.h:339
@ NODE_BLOOM
Definition: protocol.h:329
@ NODE_NETWORK
Definition: protocol.h:327
@ NODE_COMPACT_FILTERS
Definition: protocol.h:335
static constexpr size_t MAX_FEATUREID_LENGTH
Definition: protocol.h:313
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:372
#define SER_WRITE(obj, code)
Definition: serialize.h:150
#define SER_PARAMS(type)
Formatter methods can retrieve parameters attached to a stream using the SER_PARAMS(type) macro as lo...
Definition: serialize.h:209
#define SER_READ(obj, code)
Definition: serialize.h:149
#define SER_PARAMS_OPFUNC
Helper macro for SerParams structs.
Definition: serialize.h:1274
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:491
#define READWRITE(...)
Definition: serialize.h:148
const Format fmt
Definition: protocol.h:417
Formatter for integers in CompactSize format.
Definition: serialize.h:561
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:525
Definition: gen.cpp:103
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:35
assert(!tx.IsCoinBase())