Branch data Line data Source code
# 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 <primitives/transaction.h>
# 15 : : #include <serialize.h>
# 16 : : #include <uint256.h>
# 17 : : #include <version.h>
# 18 : :
# 19 : : #include <stdint.h>
# 20 : : #include <string>
# 21 : :
# 22 : : /** Message header.
# 23 : : * (4) message start.
# 24 : : * (12) command.
# 25 : : * (4) size.
# 26 : : * (4) checksum.
# 27 : : */
# 28 : : class CMessageHeader
# 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;
# 35 : : static constexpr size_t MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE;
# 36 : : static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
# 37 : : static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
# 38 : : typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
# 39 : :
# 40 : : explicit CMessageHeader();
# 41 : :
# 42 : : /** Construct a P2P message header from message-start characters, a command and the size of the message.
# 43 : : * @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error.
# 44 : : */
# 45 : : CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
# 46 : :
# 47 : : std::string GetCommand() const;
# 48 : : bool IsCommandValid() const;
# 49 : :
# 50 : 177166 : SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
# 51 : :
# 52 : : char pchMessageStart[MESSAGE_START_SIZE];
# 53 : : char pchCommand[COMMAND_SIZE];
# 54 : : uint32_t nMessageSize;
# 55 : : uint8_t pchChecksum[CHECKSUM_SIZE];
# 56 : : };
# 57 : :
# 58 : : /**
# 59 : : * Bitcoin protocol message types. When adding new message types, don't forget
# 60 : : * to update allNetMessageTypes in protocol.cpp.
# 61 : : */
# 62 : : namespace NetMsgType {
# 63 : :
# 64 : : /**
# 65 : : * The version message provides information about the transmitting node to the
# 66 : : * receiving node at the beginning of a connection.
# 67 : : */
# 68 : : extern const char* VERSION;
# 69 : : /**
# 70 : : * The verack message acknowledges a previously-received version message,
# 71 : : * informing the connecting node that it can begin to send other messages.
# 72 : : */
# 73 : : extern const char* VERACK;
# 74 : : /**
# 75 : : * The addr (IP address) message relays connection information for peers on the
# 76 : : * network.
# 77 : : */
# 78 : : extern const char* ADDR;
# 79 : : /**
# 80 : : * The addrv2 message relays connection information for peers on the network just
# 81 : : * like the addr message, but is extended to allow gossiping of longer node
# 82 : : * addresses (see BIP155).
# 83 : : */
# 84 : : extern const char *ADDRv2;
# 85 : : /**
# 86 : : * The sendaddrv2 message signals support for receiving ADDRv2 messages (BIP155).
# 87 : : * It also implies that its sender can encode as ADDRv2 and would send ADDRv2
# 88 : : * instead of ADDR to a peer that has signaled ADDRv2 support by sending SENDADDRv2.
# 89 : : */
# 90 : : extern const char *SENDADDRv2;
# 91 : : /**
# 92 : : * The inv message (inventory message) transmits one or more inventories of
# 93 : : * objects known to the transmitting peer.
# 94 : : */
# 95 : : extern const char* INV;
# 96 : : /**
# 97 : : * The getdata message requests one or more data objects from another node.
# 98 : : */
# 99 : : extern const char* GETDATA;
# 100 : : /**
# 101 : : * The merkleblock message is a reply to a getdata message which requested a
# 102 : : * block using the inventory type MSG_MERKLEBLOCK.
# 103 : : * @since protocol version 70001 as described by BIP37.
# 104 : : */
# 105 : : extern const char* MERKLEBLOCK;
# 106 : : /**
# 107 : : * The getblocks message requests an inv message that provides block header
# 108 : : * hashes starting from a particular point in the block chain.
# 109 : : */
# 110 : : extern const char* GETBLOCKS;
# 111 : : /**
# 112 : : * The getheaders message requests a headers message that provides block
# 113 : : * headers starting from a particular point in the block chain.
# 114 : : * @since protocol version 31800.
# 115 : : */
# 116 : : extern const char* GETHEADERS;
# 117 : : /**
# 118 : : * The tx message transmits a single transaction.
# 119 : : */
# 120 : : extern const char* TX;
# 121 : : /**
# 122 : : * The headers message sends one or more block headers to a node which
# 123 : : * previously requested certain headers with a getheaders message.
# 124 : : * @since protocol version 31800.
# 125 : : */
# 126 : : extern const char* HEADERS;
# 127 : : /**
# 128 : : * The block message transmits a single serialized block.
# 129 : : */
# 130 : : extern const char* BLOCK;
# 131 : : /**
# 132 : : * The getaddr message requests an addr message from the receiving node,
# 133 : : * preferably one with lots of IP addresses of other receiving nodes.
# 134 : : */
# 135 : : extern const char* GETADDR;
# 136 : : /**
# 137 : : * The mempool message requests the TXIDs of transactions that the receiving
# 138 : : * node has verified as valid but which have not yet appeared in a block.
# 139 : : * @since protocol version 60002.
# 140 : : */
# 141 : : extern const char* MEMPOOL;
# 142 : : /**
# 143 : : * The ping message is sent periodically to help confirm that the receiving
# 144 : : * peer is still connected.
# 145 : : */
# 146 : : extern const char* PING;
# 147 : : /**
# 148 : : * The pong message replies to a ping message, proving to the pinging node that
# 149 : : * the ponging node is still alive.
# 150 : : * @since protocol version 60001 as described by BIP31.
# 151 : : */
# 152 : : extern const char* PONG;
# 153 : : /**
# 154 : : * The notfound message is a reply to a getdata message which requested an
# 155 : : * object the receiving node does not have available for relay.
# 156 : : * @since protocol version 70001.
# 157 : : */
# 158 : : extern const char* NOTFOUND;
# 159 : : /**
# 160 : : * The filterload message tells the receiving peer to filter all relayed
# 161 : : * transactions and requested merkle blocks through the provided filter.
# 162 : : * @since protocol version 70001 as described by BIP37.
# 163 : : * Only available with service bit NODE_BLOOM since protocol version
# 164 : : * 70011 as described by BIP111.
# 165 : : */
# 166 : : extern const char* FILTERLOAD;
# 167 : : /**
# 168 : : * The filteradd message tells the receiving peer to add a single element to a
# 169 : : * previously-set bloom filter, such as a new public key.
# 170 : : * @since protocol version 70001 as described by BIP37.
# 171 : : * Only available with service bit NODE_BLOOM since protocol version
# 172 : : * 70011 as described by BIP111.
# 173 : : */
# 174 : : extern const char* FILTERADD;
# 175 : : /**
# 176 : : * The filterclear message tells the receiving peer to remove a previously-set
# 177 : : * bloom filter.
# 178 : : * @since protocol version 70001 as described by BIP37.
# 179 : : * Only available with service bit NODE_BLOOM since protocol version
# 180 : : * 70011 as described by BIP111.
# 181 : : */
# 182 : : extern const char* FILTERCLEAR;
# 183 : : /**
# 184 : : * Indicates that a node prefers to receive new block announcements via a
# 185 : : * "headers" message rather than an "inv".
# 186 : : * @since protocol version 70012 as described by BIP130.
# 187 : : */
# 188 : : extern const char* SENDHEADERS;
# 189 : : /**
# 190 : : * The feefilter message tells the receiving peer not to inv us any txs
# 191 : : * which do not meet the specified min fee rate.
# 192 : : * @since protocol version 70013 as described by BIP133
# 193 : : */
# 194 : : extern const char* FEEFILTER;
# 195 : : /**
# 196 : : * Contains a 1-byte bool and 8-byte LE version number.
# 197 : : * Indicates that a node is willing to provide blocks via "cmpctblock" messages.
# 198 : : * May indicate that a node prefers to receive new block announcements via a
# 199 : : * "cmpctblock" message rather than an "inv", depending on message contents.
# 200 : : * @since protocol version 70014 as described by BIP 152
# 201 : : */
# 202 : : extern const char* SENDCMPCT;
# 203 : : /**
# 204 : : * Contains a CBlockHeaderAndShortTxIDs object - providing a header and
# 205 : : * list of "short txids".
# 206 : : * @since protocol version 70014 as described by BIP 152
# 207 : : */
# 208 : : extern const char* CMPCTBLOCK;
# 209 : : /**
# 210 : : * Contains a BlockTransactionsRequest
# 211 : : * Peer should respond with "blocktxn" message.
# 212 : : * @since protocol version 70014 as described by BIP 152
# 213 : : */
# 214 : : extern const char* GETBLOCKTXN;
# 215 : : /**
# 216 : : * Contains a BlockTransactions.
# 217 : : * Sent in response to a "getblocktxn" message.
# 218 : : * @since protocol version 70014 as described by BIP 152
# 219 : : */
# 220 : : extern const char* BLOCKTXN;
# 221 : : /**
# 222 : : * getcfilters requests compact filters for a range of blocks.
# 223 : : * Only available with service bit NODE_COMPACT_FILTERS as described by
# 224 : : * BIP 157 & 158.
# 225 : : */
# 226 : : extern const char* GETCFILTERS;
# 227 : : /**
# 228 : : * cfilter is a response to a getcfilters request containing a single compact
# 229 : : * filter.
# 230 : : */
# 231 : : extern const char* CFILTER;
# 232 : : /**
# 233 : : * getcfheaders requests a compact filter header and the filter hashes for a
# 234 : : * range of blocks, which can then be used to reconstruct the filter headers
# 235 : : * for those blocks.
# 236 : : * Only available with service bit NODE_COMPACT_FILTERS as described by
# 237 : : * BIP 157 & 158.
# 238 : : */
# 239 : : extern const char* GETCFHEADERS;
# 240 : : /**
# 241 : : * cfheaders is a response to a getcfheaders request containing a filter header
# 242 : : * and a vector of filter hashes for each subsequent block in the requested range.
# 243 : : */
# 244 : : extern const char* CFHEADERS;
# 245 : : /**
# 246 : : * getcfcheckpt requests evenly spaced compact filter headers, enabling
# 247 : : * parallelized download and validation of the headers between them.
# 248 : : * Only available with service bit NODE_COMPACT_FILTERS as described by
# 249 : : * BIP 157 & 158.
# 250 : : */
# 251 : : extern const char* GETCFCHECKPT;
# 252 : : /**
# 253 : : * cfcheckpt is a response to a getcfcheckpt request containing a vector of
# 254 : : * evenly spaced filter headers for blocks on the requested chain.
# 255 : : */
# 256 : : extern const char* CFCHECKPT;
# 257 : : /**
# 258 : : * Indicates that a node prefers to relay transactions via wtxid, rather than
# 259 : : * txid.
# 260 : : * @since protocol version 70016 as described by BIP 339.
# 261 : : */
# 262 : : extern const char* WTXIDRELAY;
# 263 : : }; // namespace NetMsgType
# 264 : :
# 265 : : /* Get a vector of all valid message types (see above) */
# 266 : : const std::vector<std::string>& getAllNetMessageTypes();
# 267 : :
# 268 : : /** nServices flags */
# 269 : : enum ServiceFlags : uint64_t {
# 270 : : // NOTE: When adding here, be sure to update serviceFlagToStr too
# 271 : : // Nothing
# 272 : : NODE_NONE = 0,
# 273 : : // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
# 274 : : // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.
# 275 : : NODE_NETWORK = (1 << 0),
# 276 : : // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
# 277 : : // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
# 278 : : // See BIP 64 for details on how this is implemented.
# 279 : : NODE_GETUTXO = (1 << 1),
# 280 : : // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
# 281 : : // Bitcoin Core nodes used to support this by default, without advertising this bit,
# 282 : : // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
# 283 : : NODE_BLOOM = (1 << 2),
# 284 : : // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
# 285 : : // witness data.
# 286 : : NODE_WITNESS = (1 << 3),
# 287 : : // NODE_COMPACT_FILTERS means the node will service basic block filter requests.
# 288 : : // See BIP157 and BIP158 for details on how this is implemented.
# 289 : : NODE_COMPACT_FILTERS = (1 << 6),
# 290 : : // NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
# 291 : : // serving the last 288 (2 day) blocks
# 292 : : // See BIP159 for details on how this is implemented.
# 293 : : NODE_NETWORK_LIMITED = (1 << 10),
# 294 : :
# 295 : : // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
# 296 : : // isn't getting used, or one not being used much, and notify the
# 297 : : // bitcoin-development mailing list. Remember that service bits are just
# 298 : : // unauthenticated advertisements, so your code must be robust against
# 299 : : // collisions and other cases where nodes may be advertising a service they
# 300 : : // do not actually support. Other service bits should be allocated via the
# 301 : : // BIP process.
# 302 : : };
# 303 : :
# 304 : : /**
# 305 : : * Convert service flags (a bitmask of NODE_*) to human readable strings.
# 306 : : * It supports unknown service flags which will be returned as "UNKNOWN[...]".
# 307 : : * @param[in] flags multiple NODE_* bitwise-OR-ed together
# 308 : : */
# 309 : : std::vector<std::string> serviceFlagsToStr(uint64_t flags);
# 310 : :
# 311 : : /**
# 312 : : * Gets the set of service flags which are "desirable" for a given peer.
# 313 : : *
# 314 : : * These are the flags which are required for a peer to support for them
# 315 : : * to be "interesting" to us, ie for us to wish to use one of our few
# 316 : : * outbound connection slots for or for us to wish to prioritize keeping
# 317 : : * their connection around.
# 318 : : *
# 319 : : * Relevant service flags may be peer- and state-specific in that the
# 320 : : * version of the peer may determine which flags are required (eg in the
# 321 : : * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
# 322 : : * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
# 323 : : * case NODE_NETWORK_LIMITED suffices).
# 324 : : *
# 325 : : * Thus, generally, avoid calling with peerServices == NODE_NONE, unless
# 326 : : * state-specific flags must absolutely be avoided. When called with
# 327 : : * peerServices == NODE_NONE, the returned desirable service flags are
# 328 : : * guaranteed to not change dependent on state - ie they are suitable for
# 329 : : * use when describing peers which we know to be desirable, but for which
# 330 : : * we do not have a confirmed set of service flags.
# 331 : : *
# 332 : : * If the NODE_NONE return value is changed, contrib/seeds/makeseeds.py
# 333 : : * should be updated appropriately to filter for the same nodes.
# 334 : : */
# 335 : : ServiceFlags GetDesirableServiceFlags(ServiceFlags services);
# 336 : :
# 337 : : /** Set the current IBD status in order to figure out the desirable service flags */
# 338 : : void SetServiceFlagsIBDCache(bool status);
# 339 : :
# 340 : : /**
# 341 : : * A shortcut for (services & GetDesirableServiceFlags(services))
# 342 : : * == GetDesirableServiceFlags(services), ie determines whether the given
# 343 : : * set of service flags are sufficient for a peer to be "relevant".
# 344 : : */
# 345 : : static inline bool HasAllDesirableServiceFlags(ServiceFlags services)
# 346 : 4345 : {
# 347 : 4345 : return !(GetDesirableServiceFlags(services) & (~services));
# 348 : 4345 : }
# 349 : :
# 350 : : /**
# 351 : : * Checks if a peer with the given service flags may be capable of having a
# 352 : : * robust address-storage DB.
# 353 : : */
# 354 : : static inline bool MayHaveUsefulAddressDB(ServiceFlags services)
# 355 : 24 : {
# 356 : 24 : return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
# 357 : 24 : }
# 358 : :
# 359 : : /** A CService with information about it as peer */
# 360 : : class CAddress : public CService
# 361 : : {
# 362 : : static constexpr uint32_t TIME_INIT{100000000};
# 363 : :
# 364 : : public:
# 365 : 114466 : CAddress() : CService{} {};
# 366 : 35955 : CAddress(CService ipIn, ServiceFlags nServicesIn) : CService{ipIn}, nServices{nServicesIn} {};
# 367 : 6 : CAddress(CService ipIn, ServiceFlags nServicesIn, uint32_t nTimeIn) : CService{ipIn}, nTime{nTimeIn}, nServices{nServicesIn} {};
# 368 : :
# 369 : : SERIALIZE_METHODS(CAddress, obj)
# 370 : 46443 : {
# 371 : 46443 : SER_READ(obj, obj.nTime = TIME_INIT);
# 372 : 46443 : int nVersion = s.GetVersion();
# 373 : 46443 : if (s.GetType() & SER_DISK) {
# 374 : 35418 : READWRITE(nVersion);
# 375 : 35418 : }
# 376 : 46443 : if ((s.GetType() & SER_DISK) ||
# 377 : 43526 : (nVersion != INIT_PROTO_VERSION && !(s.GetType() & SER_GETHASH))) {
# 378 : : // The only time we serialize a CAddress object without nTime is in
# 379 : : // the initial VERSION messages which contain two CAddress records.
# 380 : : // At that point, the serialization version is INIT_PROTO_VERSION.
# 381 : : // After the version handshake, serialization version is >=
# 382 : : // MIN_PEER_PROTO_VERSION and all ADDR messages are serialized with
# 383 : : // nTime.
# 384 : 43526 : READWRITE(obj.nTime);
# 385 : 43526 : }
# 386 : 46443 : if (nVersion & ADDRV2_FORMAT) {
# 387 : 36471 : uint64_t services_tmp;
# 388 : 36471 : SER_WRITE(obj, services_tmp = obj.nServices);
# 389 : 36471 : READWRITE(COMPACTSIZE(services_tmp));
# 390 : 36471 : SER_READ(obj, obj.nServices = static_cast<ServiceFlags>(services_tmp));
# 391 : 9972 : } else {
# 392 : 9972 : READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));
# 393 : 9972 : }
# 394 : 46443 : READWRITEAS(CService, obj);
# 395 : 46443 : }
# 396 : :
# 397 : : // disk and network only
# 398 : : uint32_t nTime{TIME_INIT};
# 399 : :
# 400 : : ServiceFlags nServices{NODE_NONE};
# 401 : : };
# 402 : :
# 403 : : /** getdata message type flags */
# 404 : : const uint32_t MSG_WITNESS_FLAG = 1 << 30;
# 405 : : const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
# 406 : :
# 407 : : /** getdata / inv message types.
# 408 : : * These numbers are defined by the protocol. When adding a new value, be sure
# 409 : : * to mention it in the respective BIP.
# 410 : : */
# 411 : : enum GetDataMsg : uint32_t {
# 412 : : UNDEFINED = 0,
# 413 : : MSG_TX = 1,
# 414 : : MSG_BLOCK = 2,
# 415 : : MSG_WTX = 5, //!< Defined in BIP 339
# 416 : : // The following can only occur in getdata. Invs always use TX/WTX or BLOCK.
# 417 : : MSG_FILTERED_BLOCK = 3, //!< Defined in BIP37
# 418 : : MSG_CMPCT_BLOCK = 4, //!< Defined in BIP152
# 419 : : MSG_WITNESS_BLOCK = MSG_BLOCK | MSG_WITNESS_FLAG, //!< Defined in BIP144
# 420 : : MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG, //!< Defined in BIP144
# 421 : : // MSG_FILTERED_WITNESS_BLOCK is defined in BIP144 as reserved for future
# 422 : : // use and remains unused.
# 423 : : // MSG_FILTERED_WITNESS_BLOCK = MSG_FILTERED_BLOCK | MSG_WITNESS_FLAG,
# 424 : : };
# 425 : :
# 426 : : /** inv message data */
# 427 : : class CInv
# 428 : : {
# 429 : : public:
# 430 : : CInv();
# 431 : : CInv(uint32_t typeIn, const uint256& hashIn);
# 432 : :
# 433 : 182702 : SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }
# 434 : :
# 435 : : friend bool operator<(const CInv& a, const CInv& b);
# 436 : :
# 437 : : std::string GetCommand() const;
# 438 : : std::string ToString() const;
# 439 : :
# 440 : : // Single-message helper methods
# 441 : 22716 : bool IsMsgTx() const { return type == MSG_TX; }
# 442 : 22902 : bool IsMsgBlk() const { return type == MSG_BLOCK; }
# 443 : 47684 : bool IsMsgWtx() const { return type == MSG_WTX; }
# 444 : 1028 : bool IsMsgFilteredBlk() const { return type == MSG_FILTERED_BLOCK; }
# 445 : 201 : bool IsMsgCmpctBlk() const { return type == MSG_CMPCT_BLOCK; }
# 446 : 10435 : bool IsMsgWitnessBlk() const { return type == MSG_WITNESS_BLOCK; }
# 447 : :
# 448 : : // Combined-message helper methods
# 449 : : bool IsGenTxMsg() const
# 450 : 85527 : {
# 451 : 85527 : return type == MSG_TX || type == MSG_WTX || type == MSG_WITNESS_TX;
# 452 : 85527 : }
# 453 : : bool IsGenBlkMsg() const
# 454 : 15287 : {
# 455 : 15287 : return type == MSG_BLOCK || type == MSG_FILTERED_BLOCK || type == MSG_CMPCT_BLOCK || type == MSG_WITNESS_BLOCK;
# 456 : 15287 : }
# 457 : :
# 458 : : uint32_t type;
# 459 : : uint256 hash;
# 460 : : };
# 461 : :
# 462 : : /** Convert a TX/WITNESS_TX/WTX CInv to a GenTxid. */
# 463 : : GenTxid ToGenTxid(const CInv& inv);
# 464 : :
# 465 : : #endif // BITCOIN_PROTOCOL_H
|