Skip to content

Commit

Permalink
net: make the list of known message types a compile time constant
Browse files Browse the repository at this point in the history
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: `g_all_net_message_types.size()` which can be used in
future code to build other `std::array`s with that size.
  • Loading branch information
vasild committed Feb 14, 2024
1 parent 7143d43 commit 808fe4e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3701,7 +3701,7 @@ CNode::CNode(NodeId idIn,
{
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);

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

Expand Down
46 changes: 0 additions & 46 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,6 @@ 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 +123,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
42 changes: 39 additions & 3 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CMessageHeader

/**
* Bitcoin protocol message types. When adding new message types, don't forget
* to update allNetMessageTypes in protocol.cpp.
* to update g_all_net_message_types below.
*/
namespace NetMsgType {

Expand Down Expand Up @@ -267,8 +267,44 @@ extern const char* WTXIDRELAY;
extern const char* SENDTXRCNCL;
}; // namespace NetMsgType

/* Get a vector of all valid message types (see above) */
const std::vector<std::string>& getAllNetMessageTypes();
/** All known message types (see above). Keep this in the same order as the list of messages above. */
static const std::array 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,
};

/** nServices flags */
enum ServiceFlags : uint64_t {
Expand Down
5 changes: 2 additions & 3 deletions src/test/fuzz/p2p_transport_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

namespace {

std::vector<std::string> g_all_messages;
auto g_all_messages = g_all_net_message_types;

void initialize_p2p_transport_serialization()
{
ECC_Start();
SelectParams(ChainType::REGTEST);
g_all_messages = getAllNetMessageTypes();
std::sort(g_all_messages.begin(), g_all_messages.end());
}

Expand Down Expand Up @@ -150,7 +149,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
return ret;
} else {
// Otherwise, use it as index into the list of known messages.
return g_all_messages[v % g_all_messages.size()];
return std::string{g_all_messages[v % g_all_messages.size()]};
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/process_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void initialize_process_message()
{
if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
LIMIT_TO_MESSAGE_TYPE = val;
Assert(std::count(getAllNetMessageTypes().begin(), getAllNetMessageTypes().end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
Assert(std::count(g_all_net_message_types.begin(), g_all_net_message_types.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
}

static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
Expand Down

0 comments on commit 808fe4e

Please sign in to comment.