net: make the list of known message types a compile time constant

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.
pull/29421/head
Vasil Dimov 12 months ago
parent ba907f96ad
commit 2fa9de06c2
No known key found for this signature in database
GPG Key ID: 54DF06F64B55CBBF

@ -3701,8 +3701,9 @@ CNode::CNode(NodeId idIn,
{ {
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND); 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[msg] = 0;
}
mapRecvBytesPerMsgType[NET_MESSAGE_TYPE_OTHER] = 0; mapRecvBytesPerMsgType[NET_MESSAGE_TYPE_OTHER] = 0;
if (fLogIPs) { if (fLogIPs) {

@ -47,47 +47,6 @@ const char* WTXIDRELAY = "wtxidrelay";
const char* SENDTXRCNCL = "sendtxrcncl"; const char* SENDTXRCNCL = "sendtxrcncl";
} // namespace NetMsgType } // 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) CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
: pchMessageStart{pchMessageStartIn} : pchMessageStart{pchMessageStartIn}
{ {
@ -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. * Convert a service flag (NODE_*) to a human readable string.
* It supports unknown service flags which will be returned as "UNKNOWN[...]". * It supports unknown service flags which will be returned as "UNKNOWN[...]".

@ -55,7 +55,7 @@ public:
/** /**
* Bitcoin protocol message types. When adding new message types, don't forget * Bitcoin protocol message types. When adding new message types, don't forget
* to update allNetMessageTypes in protocol.cpp. * to update ALL_NET_MESSAGE_TYPES below.
*/ */
namespace NetMsgType { namespace NetMsgType {
@ -267,8 +267,44 @@ extern const char* WTXIDRELAY;
extern const char* SENDTXRCNCL; extern const char* SENDTXRCNCL;
}; // namespace NetMsgType }; // namespace NetMsgType
/* Get a vector of all valid message types (see above) */ /** All known message types (see above). Keep this in the same order as the list of messages above. */
const std::vector<std::string>& getAllNetMessageTypes(); inline const std::array ALL_NET_MESSAGE_TYPES{std::to_array<std::string>({
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 */ /** nServices flags */
enum ServiceFlags : uint64_t { enum ServiceFlags : uint64_t {

@ -21,13 +21,12 @@
namespace { namespace {
std::vector<std::string> g_all_messages; auto g_all_messages = ALL_NET_MESSAGE_TYPES;
void initialize_p2p_transport_serialization() void initialize_p2p_transport_serialization()
{ {
ECC_Start(); ECC_Start();
SelectParams(ChainType::REGTEST); SelectParams(ChainType::REGTEST);
g_all_messages = getAllNetMessageTypes();
std::sort(g_all_messages.begin(), g_all_messages.end()); std::sort(g_all_messages.begin(), g_all_messages.end());
} }

@ -37,7 +37,7 @@ void initialize_process_message()
{ {
if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) { if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
LIMIT_TO_MESSAGE_TYPE = val; LIMIT_TO_MESSAGE_TYPE = val;
Assert(std::count(getAllNetMessageTypes().begin(), getAllNetMessageTypes().end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
} }
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>( static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(

@ -205,12 +205,12 @@ def transform_process_message_target(targets, src_dir):
p2p_msg_target = "process_message" p2p_msg_target = "process_message"
if (p2p_msg_target, {}) in targets: if (p2p_msg_target, {}) in targets:
lines = subprocess.run( lines = subprocess.run(
["git", "grep", "--function-context", "g_all_net_message_types{", src_dir / "src" / "protocol.cpp"], ["git", "grep", "--function-context", "ALL_NET_MESSAGE_TYPES{", src_dir / "src" / "protocol.h"],
check=True, check=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
text=True, text=True,
).stdout.splitlines() ).stdout.splitlines()
lines = [l.split("::", 1)[1].split(",")[0].lower() for l in lines if l.startswith("src/protocol.cpp- NetMsgType::")] lines = [l.split("::", 1)[1].split(",")[0].lower() for l in lines if l.startswith("src/protocol.h- NetMsgType::")]
assert len(lines) assert len(lines)
targets += [(p2p_msg_target, {"LIMIT_TO_MESSAGE_TYPE": m}) for m in lines] targets += [(p2p_msg_target, {"LIMIT_TO_MESSAGE_TYPE": m}) for m in lines]
return targets return targets

Loading…
Cancel
Save