Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <protocol.h>8#include <common/system.h>10CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)11: pchMessageStart{pchMessageStartIn}12{13// Copy the message type name14size_t i = 0;15for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i) m_msg_type[i] = msg_type[i];16assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE18nMessageSize = nMessageSizeIn;19}21std::string CMessageHeader::GetMessageType() const22{23return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));24}26bool CMessageHeader::IsMessageTypeValid() const27{28// Check the message type string for errors29for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {30if (*p1 == 0) {31// Must be all zeros after the first zero32for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {33if (*p1 != 0) {34return false;35}36}37} else if (*p1 < ' ' || *p1 > 0x7E) {38return false;39}40}42return true;43}45CInv::CInv()46{47type = 0;48hash.SetNull();49}51CInv::CInv(uint32_t typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}53bool operator<(const CInv& a, const CInv& b)54{55return (a.type < b.type || (a.type == b.type && a.hash < b.hash));56}58std::string CInv::GetMessageType() const59{60std::string cmd;61if (type & MSG_WITNESS_FLAG)62cmd.append("witness-");63int masked = type & MSG_TYPE_MASK;64switch (masked)65{66case MSG_TX: return cmd.append(NetMsgType::TX);67// WTX is not a message type, just an inv type68case MSG_WTX: return cmd.append("wtx");69case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);70case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);71case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);72default:73throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));74}75}77std::string CInv::ToString() const78{79try {80return strprintf("%s %s", GetMessageType(), hash.ToString());81} catch(const std::out_of_range &) {82return strprintf("0x%08x %s", type, hash.ToString());83}84}86/**87* Convert a service flag (NODE_*) to a human readable string.88* It supports unknown service flags which will be returned as "UNKNOWN[...]".89* @param[in] bit the service flag is calculated as (1 << bit)90*/91static std::string serviceFlagToStr(size_t bit)92{93const uint64_t service_flag = 1ULL << bit;94switch ((ServiceFlags)service_flag) {95case NODE_NONE: abort(); // impossible96case NODE_NETWORK: return "NETWORK";97case NODE_BLOOM: return "BLOOM";98case NODE_WITNESS: return "WITNESS";99case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";100case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";101case NODE_P2P_V2: return "P2P_V2";102// Not using default, so we get warned when a case is missing103}105return strprintf("UNKNOWN[2^%u]", bit);106}108std::vector<std::string> serviceFlagsToStr(uint64_t flags)109{110std::vector<std::string> str_flags;112for (size_t i = 0; i < sizeof(flags) * 8; ++i) {113if (flags & (1ULL << i)) {114str_flags.emplace_back(serviceFlagToStr(i));115}116}118return str_flags;119}121GenTxid ToGenTxid(const CInv& inv)122{123assert(inv.IsGenTxMsg());124return inv.IsMsgWtx() ? GenTxid{Wtxid::FromUint256(inv.hash)} : GenTxid{Txid::FromUint256(inv.hash)};125}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.