All these plus the NUM_NET_MESSAGE_TYPES constant can be avoided if the list of all message types is turned into a std::array. Consider this:
<details>
<summary>[patch] make the list of known message types a compile time constant</summary>
diff --git a/src/net.cpp b/src/net.cpp
index a2f80cbcf7..133abae117 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -3670,13 +3670,13 @@ CNode::CNode(NodeId idIn,
nLocalHostNonce{nLocalHostNonceIn},
m_recv_flood_size{node_opts.recv_flood_size},
m_i2p_sam_session{std::move(node_opts.i2p_sam_session)}
{
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;
if (fLogIPs) {
LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", m_addr_name, id);
} else {
diff --git a/src/protocol.cpp b/src/protocol.cpp
index 27a0a2ffc1..0b22cc47aa 100644
--- a/src/protocol.cpp
+++ b/src/protocol.cpp
@@ -46,53 +46,12 @@ 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}
{
// Copy the command name
size_t i = 0;
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
@@ -175,17 +134,12 @@ std::string CInv::ToString() const
return strprintf("%s %s", GetCommand(), hash.ToString());
} catch(const std::out_of_range &) {
return strprintf("0x%08x %s", type, hash.ToString());
}
}
-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[...]".
* [@param](/bitcoin-bitcoin/contributor/param/)[in] bit the service flag is calculated as (1 << bit)
*/
static std::string serviceFlagToStr(size_t bit)
diff --git a/src/protocol.h b/src/protocol.h
index e405253632..2a6b85dea5 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -264,14 +264,50 @@ extern const char* WTXIDRELAY;
* The salt is used to compute short txids needed for efficient
* txreconciliation, as described by BIP 330.
*/
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 {
// NOTE: When adding here, be sure to update serviceFlagToStr too
// Nothing
NODE_NONE = 0,
diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp
index 21d8dab536..853661b4d4 100644
--- a/src/test/fuzz/p2p_transport_serialization.cpp
+++ b/src/test/fuzz/p2p_transport_serialization.cpp
@@ -18,19 +18,18 @@
#include <limits>
#include <optional>
#include <vector>
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());
}
} // namespace
FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serialization)
@@ -147,13 +146,13 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
if (c < ' ' || c > 0x7E) break;
ret += c;
}
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()]};
}
};
// Function to construct a CSerializedNetMsg to send.
auto make_msg_fn = [&](bool first) {
CSerializedNetMsg msg;
diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp
index d38d1bb40e..24c711f421 100644
--- a/src/test/fuzz/process_message.cpp
+++ b/src/test/fuzz/process_message.cpp
@@ -42,13 +42,13 @@ std::string_view LIMIT_TO_MESSAGE_TYPE{};
} // namespace
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>(
/*chain_type=*/ChainType::REGTEST,
/*extra_args=*/{"-txreconciliation"});
g_setup = testing_setup.get();
</details>