Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251* cfcheckpt is a response to a getcfcheckpt request containing a vector of252* evenly spaced filter headers for blocks on the requested chain.253*/254inline constexpr const char* CFCHECKPT{"cfcheckpt"};255/**256* Indicates that a node prefers to relay transactions via wtxid, rather than257* txid.258* @since protocol version 70016 as described by BIP 339.259*/260inline constexpr const char* WTXIDRELAY{"wtxidrelay"};261/**262* Contains a 4-byte version number and an 8-byte salt.263* The salt is used to compute short txids needed for efficient264* txreconciliation, as described by BIP 330.265*/266inline constexpr const char* SENDTXRCNCL{"sendtxrcncl"};267/**268* BIP 434 Peer feature negotiation269*/270inline constexpr const char* FEATURE{"feature"};271}; // namespace NetMsgType273/** All known message types (see above). Keep this in the same order as the list of messages above. */274inline const std::array ALL_NET_MESSAGE_TYPES{std::to_array<std::string>({275NetMsgType::VERSION,276NetMsgType::VERACK,277NetMsgType::ADDR,278NetMsgType::ADDRV2,279NetMsgType::SENDADDRV2,280NetMsgType::INV,281NetMsgType::GETDATA,282NetMsgType::MERKLEBLOCK,283NetMsgType::GETBLOCKS,284NetMsgType::GETHEADERS,285NetMsgType::TX,286NetMsgType::HEADERS,287NetMsgType::BLOCK,288NetMsgType::GETADDR,289NetMsgType::MEMPOOL,290NetMsgType::PING,291NetMsgType::PONG,292NetMsgType::NOTFOUND,293NetMsgType::FILTERLOAD,294NetMsgType::FILTERADD,295NetMsgType::FILTERCLEAR,296NetMsgType::SENDHEADERS,297NetMsgType::FEEFILTER,298NetMsgType::SENDCMPCT,299NetMsgType::CMPCTBLOCK,300NetMsgType::GETBLOCKTXN,301NetMsgType::BLOCKTXN,302NetMsgType::GETCFILTERS,303NetMsgType::CFILTER,304NetMsgType::GETCFHEADERS,305NetMsgType::CFHEADERS,306NetMsgType::GETCFCHECKPT,307NetMsgType::CFCHECKPT,308NetMsgType::WTXIDRELAY,309NetMsgType::SENDTXRCNCL,310NetMsgType::FEATURE,311})};313inline constexpr size_t MAX_FEATUREID_LENGTH{80};314inline constexpr size_t MAX_FEATUREDATA_LENGTH{512};316namespace NetMsgFeature {317//inline constexpr std::string_view FOO{"BIP-FOO"};318}320/** nServices flags */321enum ServiceFlags : uint64_t {322// NOTE: When adding here, be sure to update serviceFlagToStr too323// Nothing324NODE_NONE = 0,325// NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently326// set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.327NODE_NETWORK = (1 << 0),328// NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.329NODE_BLOOM = (1 << 2),330// NODE_WITNESS indicates that a node can be asked for blocks and transactions including331// witness data.332NODE_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.335NODE_COMPACT_FILTERS = (1 << 6),336// NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only337// serving the last 288 (2 day) blocks338// See BIP159 for details on how this is implemented.339NODE_NETWORK_LIMITED = (1 << 10),341// NODE_P2P_V2 means the node supports BIP324 transport342NODE_P2P_V2 = (1 << 11),344// Bits 24-31 are reserved for temporary experiments. Just pick a bit that345// isn't getting used, or one not being used much, and notify the346// bitcoin-development mailing list. Remember that service bits are just347// unauthenticated advertisements, so your code must be robust against348// collisions and other cases where nodes may be advertising a service they349// do not actually support. Other service bits should be allocated via the350// BIP process.351};353/**354* Convert service flags (a bitmask of NODE_*) to human readable strings.355* It supports unknown service flags which will be returned as "UNKNOWN[...]".356* @param[in] flags multiple NODE_* bitwise-OR-ed together357*/358std::vector<std::string> serviceFlagsToStr(uint64_t flags);360/**361* State independent service flags.362* If the return value is changed, contrib/seeds/makeseeds.py363* should be updated appropriately to filter for nodes with364* desired service flags (compatible with our new flags).365*/366constexpr ServiceFlags SeedsServiceFlags() { return ServiceFlags(NODE_NETWORK | NODE_WITNESS); }368/**369* Service flags we assume for addresses obtained from the DNS seeds and the370* fixed seeds, which don't come with service flags attached.371* BIP324 support can be safely assumed because the vast majority of listening nodes signals NODE_P2P_V2, and if the372* assumption is wrong for a given peer we simply reconnect using v1 transport.373*/374constexpr ServiceFlags SeedsAssumedServiceFlags() { return ServiceFlags(SeedsServiceFlags() | NODE_P2P_V2); }376/**377* Checks if a peer with the given service flags may be capable of having a378* robust address-storage DB.379*/380static inline bool MayHaveUsefulAddressDB(ServiceFlags services)381{382return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);383}385/** A CService with information about it as peer */386class CAddress : public CService387{388static constexpr std::chrono::seconds TIME_INIT{100000000};390/** Historically, CAddress disk serialization stored the CLIENT_VERSION, optionally OR'ed with391* the ADDRV2_FORMAT flag to indicate V2 serialization. The first field has since been392* disentangled from client versioning, and now instead:393* - The low bits (masked by DISK_VERSION_IGNORE_MASK) store the fixed value DISK_VERSION_INIT,394* (in case any code exists that treats it as a client version) but are ignored on395* deserialization.396* - The high bits (masked by ~DISK_VERSION_IGNORE_MASK) store actual serialization information.397* Only 0 or DISK_VERSION_ADDRV2 (equal to the historical ADDRV2_FORMAT) are valid now, and398* any other value triggers a deserialization failure. Other values can be added later if399* needed.400*401* For disk deserialization, ADDRV2_FORMAT in the stream version signals that ADDRV2402* deserialization is permitted, but the actual format is determined by the high bits in the403* stored version field. For network serialization, the stream version having ADDRV2_FORMAT or404* not determines the actual format used (as it has no embedded version number).405*/406static constexpr uint32_t DISK_VERSION_INIT{220000};407static constexpr uint32_t DISK_VERSION_IGNORE_MASK{0b00000000'00000111'11111111'11111111};408/** The version number written in disk serialized addresses to indicate V2 serializations.409* It must be exactly 1<<29, as that is the value that historical versions used for this410* (they used their internal ADDRV2_FORMAT flag here). */411static constexpr uint32_t DISK_VERSION_ADDRV2{1 << 29};412static_assert((DISK_VERSION_INIT & ~DISK_VERSION_IGNORE_MASK) == 0, "DISK_VERSION_INIT must be covered by DISK_VERSION_IGNORE_MASK");413static_assert((DISK_VERSION_ADDRV2 & DISK_VERSION_IGNORE_MASK) == 0, "DISK_VERSION_ADDRV2 must not be covered by DISK_VERSION_IGNORE_MASK");415public:416CAddress() : CService{} {};417CAddress(CService ipIn, ServiceFlags nServicesIn) : CService{ipIn}, nServices{nServicesIn} {};418CAddress(CService ipIn, ServiceFlags nServicesIn, NodeSeconds time) : CService{ipIn}, nTime{time}, nServices{nServicesIn} {};420enum class Format {421Disk,422Network,423};424struct SerParams : CNetAddr::SerParams {425const Format fmt;426SER_PARAMS_OPFUNC427};428static constexpr SerParams V1_NETWORK{{CNetAddr::Encoding::V1}, Format::Network};429static constexpr SerParams V2_NETWORK{{CNetAddr::Encoding::V2}, Format::Network};430static constexpr SerParams V1_DISK{{CNetAddr::Encoding::V1}, Format::Disk};431static constexpr SerParams V2_DISK{{CNetAddr::Encoding::V2}, Format::Disk};433SERIALIZE_METHODS(CAddress, obj)434{435bool use_v2;436auto& params = SER_PARAMS(SerParams);437if (params.fmt == Format::Disk) {438// In the disk serialization format, the encoding (v1 or v2) is determined by a flag version439// that's part of the serialization itself. ADDRV2_FORMAT in the stream version only determines440// whether V2 is chosen/permitted at all.441uint32_t stored_format_version = DISK_VERSION_INIT;442if (params.enc == Encoding::V2) stored_format_version |= DISK_VERSION_ADDRV2;443READWRITE(stored_format_version);444stored_format_version &= ~DISK_VERSION_IGNORE_MASK; // ignore low bits445if (stored_format_version == 0) {446use_v2 = false;447} else if (stored_format_version == DISK_VERSION_ADDRV2 && params.enc == Encoding::V2) {448// Only support v2 deserialization if V2 is set.449use_v2 = true;450} else {451throw std::ios_base::failure("Unsupported CAddress disk format version");452}453} else {454assert(params.fmt == Format::Network);455// In the network serialization format, the encoding (v1 or v2) is determined directly by456// the value of enc in the stream params, as no explicitly encoded version457// exists in the stream.458use_v2 = params.enc == Encoding::V2;459}461READWRITE(Using<LossyChronoFormatter<uint32_t>>(obj.nTime));462// nServices is serialized as CompactSize in V2; as uint64_t in V1.463if (use_v2) {464uint64_t services_tmp;465SER_WRITE(obj, services_tmp = obj.nServices);466READWRITE(Using<CompactSizeFormatter<false>>(services_tmp));467SER_READ(obj, obj.nServices = static_cast<ServiceFlags>(services_tmp));468} else {469READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));470}471// Invoke V1/V2 serializer for CService parent object.472const auto ser_params{use_v2 ? CNetAddr::V2 : CNetAddr::V1};473READWRITE(ser_params(AsBase<CService>(obj)));474}476//! Always included in serialization. The behavior is unspecified if the value is not representable as uint32_t.477NodeSeconds nTime{TIME_INIT};478//! Serialized as uint64_t in V1, and as CompactSize in V2.479ServiceFlags nServices{NODE_NONE};481friend bool operator==(const CAddress& a, const CAddress& b)482{483return a.nTime == b.nTime &&484a.nServices == b.nServices &&485static_cast<const CService&>(a) == static_cast<const CService&>(b);486}487};489/** getdata message type flags */490inline constexpr uint32_t MSG_WITNESS_FLAG = 1 << 30;491inline constexpr uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;493/** getdata / inv message types.494* These numbers are defined by the protocol. When adding a new value, be sure495* to mention it in the respective BIP.496*/497enum GetDataMsg : uint32_t {498UNDEFINED = 0,499MSG_TX = 1,500MSG_BLOCK = 2,