LCOV - code coverage report
Current view: top level - src - protocol.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 24 31 77.4 %
Date: 2020-06-05 16:23:38 Functions: 7 155 4.5 %
Legend: Lines: hit, modifiedhit, not modified not hit, modifiednot hit, not modified

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

Generated by: LCOV version 1.14