Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#29421: net: make the list of known message type…
Browse files Browse the repository at this point in the history
…s a compile time constant

b3efb48 protocol: make message types constexpr (Vasil Dimov)
2fa9de0 net: make the list of known message types a compile time constant (Vasil Dimov)

Pull request description:

  Turn the `std::vector` to `std::array` because it is cheaper and allows us to have the number of the messages as a compile time constant: `ALL_NET_MESSAGE_TYPES.size()` which can be used in future code to build other `std::array`s with that size.

  ---

  This change is part of bitcoin/bitcoin#29418 but it makes sense on its own and would be good to have it, regardless of the fate of bitcoin/bitcoin#29418. Also, if this is merged, that would reduce the size of bitcoin/bitcoin#29418, thus the current standalone PR.

ACKs for top commit:
  achow101:
    ACK b3efb48
  jonatack:
    ACK b3efb48
  maflcko:
    utACK b3efb48 🎊
  willcl-ark:
    ACK b3efb48

Tree-SHA512: 6d3860c138c64514ebab13d97ea67893e2d346dfac30a48c3d9bc769a1970407375ea4170afdb522411ced306a14a9af4eede99e964d1fb1ea3efff5d5eb57af
  • Loading branch information
achow101 committed May 21, 2024
2 parents a786fd2 + b3efb48 commit 6c13b13
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 131 deletions.
3 changes: 2 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3736,8 +3736,9 @@ CNode::CNode(NodeId idIn,
{
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);

for (const std::string &msg : getAllNetMessageTypes())
for (const auto& msg : ALL_NET_MESSAGE_TYPES) {
mapRecvBytesPerMsgType[msg] = 0;
}
mapRecvBytesPerMsgType[NET_MESSAGE_TYPE_OTHER] = 0;

if (fLogIPs) {
Expand Down
86 changes: 0 additions & 86 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,6 @@

#include <common/system.h>

#include <atomic>

namespace NetMsgType {
const char* VERSION = "version";
const char* VERACK = "verack";
const char* ADDR = "addr";
const char* ADDRV2 = "addrv2";
const char* SENDADDRV2 = "sendaddrv2";
const char* INV = "inv";
const char* GETDATA = "getdata";
const char* MERKLEBLOCK = "merkleblock";
const char* GETBLOCKS = "getblocks";
const char* GETHEADERS = "getheaders";
const char* TX = "tx";
const char* HEADERS = "headers";
const char* BLOCK = "block";
const char* GETADDR = "getaddr";
const char* MEMPOOL = "mempool";
const char* PING = "ping";
const char* PONG = "pong";
const char* NOTFOUND = "notfound";
const char* FILTERLOAD = "filterload";
const char* FILTERADD = "filteradd";
const char* FILTERCLEAR = "filterclear";
const char* SENDHEADERS = "sendheaders";
const char* FEEFILTER = "feefilter";
const char* SENDCMPCT = "sendcmpct";
const char* CMPCTBLOCK = "cmpctblock";
const char* GETBLOCKTXN = "getblocktxn";
const char* BLOCKTXN = "blocktxn";
const char* GETCFILTERS = "getcfilters";
const char* CFILTER = "cfilter";
const char* GETCFHEADERS = "getcfheaders";
const char* CFHEADERS = "cfheaders";
const char* GETCFCHECKPT = "getcfcheckpt";
const char* CFCHECKPT = "cfcheckpt";
const char* WTXIDRELAY = "wtxidrelay";
const char* SENDTXRCNCL = "sendtxrcncl";
} // namespace NetMsgType

/** All known message types. Keep this in the same order as the list of
* messages above and in protocol.h.
*/
const static std::vector<std::string> g_all_net_message_types{
NetMsgType::VERSION,
NetMsgType::VERACK,
NetMsgType::ADDR,
NetMsgType::ADDRV2,
NetMsgType::SENDADDRV2,
NetMsgType::INV,
NetMsgType::GETDATA,
NetMsgType::MERKLEBLOCK,
NetMsgType::GETBLOCKS,
NetMsgType::GETHEADERS,
NetMsgType::TX,
NetMsgType::HEADERS,
NetMsgType::BLOCK,
NetMsgType::GETADDR,
NetMsgType::MEMPOOL,
NetMsgType::PING,
NetMsgType::PONG,
NetMsgType::NOTFOUND,
NetMsgType::FILTERLOAD,
NetMsgType::FILTERADD,
NetMsgType::FILTERCLEAR,
NetMsgType::SENDHEADERS,
NetMsgType::FEEFILTER,
NetMsgType::SENDCMPCT,
NetMsgType::CMPCTBLOCK,
NetMsgType::GETBLOCKTXN,
NetMsgType::BLOCKTXN,
NetMsgType::GETCFILTERS,
NetMsgType::CFILTER,
NetMsgType::GETCFHEADERS,
NetMsgType::CFHEADERS,
NetMsgType::GETCFCHECKPT,
NetMsgType::CFCHECKPT,
NetMsgType::WTXIDRELAY,
NetMsgType::SENDTXRCNCL,
};

CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
: pchMessageStart{pchMessageStartIn}
{
Expand Down Expand Up @@ -164,11 +83,6 @@ std::string CInv::ToString() const
}
}

const std::vector<std::string> &getAllNetMessageTypes()
{
return g_all_net_message_types;
}

/**
* Convert a service flag (NODE_*) to a human readable string.
* It supports unknown service flags which will be returned as "UNKNOWN[...]".
Expand Down
Loading

0 comments on commit 6c13b13

Please sign in to comment.