Skip to content

Commit 2fa9de0

Browse files
committed
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.
1 parent ba907f9 commit 2fa9de0

File tree

6 files changed

+45
-55
lines changed

6 files changed

+45
-55
lines changed

src/net.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3701,8 +3701,9 @@ CNode::CNode(NodeId idIn,
37013701
{
37023702
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
37033703

3704-
for (const std::string &msg : getAllNetMessageTypes())
3704+
for (const auto& msg : ALL_NET_MESSAGE_TYPES) {
37053705
mapRecvBytesPerMsgType[msg] = 0;
3706+
}
37063707
mapRecvBytesPerMsgType[NET_MESSAGE_TYPE_OTHER] = 0;
37073708

37083709
if (fLogIPs) {

src/protocol.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,6 @@ const char* WTXIDRELAY = "wtxidrelay";
4747
const char* SENDTXRCNCL = "sendtxrcncl";
4848
} // namespace NetMsgType
4949

50-
/** All known message types. Keep this in the same order as the list of
51-
* messages above and in protocol.h.
52-
*/
53-
const static std::vector<std::string> g_all_net_message_types{
54-
NetMsgType::VERSION,
55-
NetMsgType::VERACK,
56-
NetMsgType::ADDR,
57-
NetMsgType::ADDRV2,
58-
NetMsgType::SENDADDRV2,
59-
NetMsgType::INV,
60-
NetMsgType::GETDATA,
61-
NetMsgType::MERKLEBLOCK,
62-
NetMsgType::GETBLOCKS,
63-
NetMsgType::GETHEADERS,
64-
NetMsgType::TX,
65-
NetMsgType::HEADERS,
66-
NetMsgType::BLOCK,
67-
NetMsgType::GETADDR,
68-
NetMsgType::MEMPOOL,
69-
NetMsgType::PING,
70-
NetMsgType::PONG,
71-
NetMsgType::NOTFOUND,
72-
NetMsgType::FILTERLOAD,
73-
NetMsgType::FILTERADD,
74-
NetMsgType::FILTERCLEAR,
75-
NetMsgType::SENDHEADERS,
76-
NetMsgType::FEEFILTER,
77-
NetMsgType::SENDCMPCT,
78-
NetMsgType::CMPCTBLOCK,
79-
NetMsgType::GETBLOCKTXN,
80-
NetMsgType::BLOCKTXN,
81-
NetMsgType::GETCFILTERS,
82-
NetMsgType::CFILTER,
83-
NetMsgType::GETCFHEADERS,
84-
NetMsgType::CFHEADERS,
85-
NetMsgType::GETCFCHECKPT,
86-
NetMsgType::CFCHECKPT,
87-
NetMsgType::WTXIDRELAY,
88-
NetMsgType::SENDTXRCNCL,
89-
};
90-
9150
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
9251
: pchMessageStart{pchMessageStartIn}
9352
{
@@ -164,11 +123,6 @@ std::string CInv::ToString() const
164123
}
165124
}
166125

167-
const std::vector<std::string> &getAllNetMessageTypes()
168-
{
169-
return g_all_net_message_types;
170-
}
171-
172126
/**
173127
* Convert a service flag (NODE_*) to a human readable string.
174128
* It supports unknown service flags which will be returned as "UNKNOWN[...]".

src/protocol.h

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CMessageHeader
5555

5656
/**
5757
* Bitcoin protocol message types. When adding new message types, don't forget
58-
* to update allNetMessageTypes in protocol.cpp.
58+
* to update ALL_NET_MESSAGE_TYPES below.
5959
*/
6060
namespace NetMsgType {
6161

@@ -267,8 +267,44 @@ extern const char* WTXIDRELAY;
267267
extern const char* SENDTXRCNCL;
268268
}; // namespace NetMsgType
269269

270-
/* Get a vector of all valid message types (see above) */
271-
const std::vector<std::string>& getAllNetMessageTypes();
270+
/** All known message types (see above). Keep this in the same order as the list of messages above. */
271+
inline const std::array ALL_NET_MESSAGE_TYPES{std::to_array<std::string>({
272+
NetMsgType::VERSION,
273+
NetMsgType::VERACK,
274+
NetMsgType::ADDR,
275+
NetMsgType::ADDRV2,
276+
NetMsgType::SENDADDRV2,
277+
NetMsgType::INV,
278+
NetMsgType::GETDATA,
279+
NetMsgType::MERKLEBLOCK,
280+
NetMsgType::GETBLOCKS,
281+
NetMsgType::GETHEADERS,
282+
NetMsgType::TX,
283+
NetMsgType::HEADERS,
284+
NetMsgType::BLOCK,
285+
NetMsgType::GETADDR,
286+
NetMsgType::MEMPOOL,
287+
NetMsgType::PING,
288+
NetMsgType::PONG,
289+
NetMsgType::NOTFOUND,
290+
NetMsgType::FILTERLOAD,
291+
NetMsgType::FILTERADD,
292+
NetMsgType::FILTERCLEAR,
293+
NetMsgType::SENDHEADERS,
294+
NetMsgType::FEEFILTER,
295+
NetMsgType::SENDCMPCT,
296+
NetMsgType::CMPCTBLOCK,
297+
NetMsgType::GETBLOCKTXN,
298+
NetMsgType::BLOCKTXN,
299+
NetMsgType::GETCFILTERS,
300+
NetMsgType::CFILTER,
301+
NetMsgType::GETCFHEADERS,
302+
NetMsgType::CFHEADERS,
303+
NetMsgType::GETCFCHECKPT,
304+
NetMsgType::CFCHECKPT,
305+
NetMsgType::WTXIDRELAY,
306+
NetMsgType::SENDTXRCNCL,
307+
})};
272308

273309
/** nServices flags */
274310
enum ServiceFlags : uint64_t {

src/test/fuzz/p2p_transport_serialization.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121

2222
namespace {
2323

24-
std::vector<std::string> g_all_messages;
24+
auto g_all_messages = ALL_NET_MESSAGE_TYPES;
2525

2626
void initialize_p2p_transport_serialization()
2727
{
2828
ECC_Start();
2929
SelectParams(ChainType::REGTEST);
30-
g_all_messages = getAllNetMessageTypes();
3130
std::sort(g_all_messages.begin(), g_all_messages.end());
3231
}
3332

src/test/fuzz/process_message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void initialize_process_message()
3737
{
3838
if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
3939
LIMIT_TO_MESSAGE_TYPE = val;
40-
Assert(std::count(getAllNetMessageTypes().begin(), getAllNetMessageTypes().end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
40+
Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
4141
}
4242

4343
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(

test/fuzz/test_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ def transform_process_message_target(targets, src_dir):
205205
p2p_msg_target = "process_message"
206206
if (p2p_msg_target, {}) in targets:
207207
lines = subprocess.run(
208-
["git", "grep", "--function-context", "g_all_net_message_types{", src_dir / "src" / "protocol.cpp"],
208+
["git", "grep", "--function-context", "ALL_NET_MESSAGE_TYPES{", src_dir / "src" / "protocol.h"],
209209
check=True,
210210
stdout=subprocess.PIPE,
211211
text=True,
212212
).stdout.splitlines()
213-
lines = [l.split("::", 1)[1].split(",")[0].lower() for l in lines if l.startswith("src/protocol.cpp- NetMsgType::")]
213+
lines = [l.split("::", 1)[1].split(",")[0].lower() for l in lines if l.startswith("src/protocol.h- NetMsgType::")]
214214
assert len(lines)
215215
targets += [(p2p_msg_target, {"LIMIT_TO_MESSAGE_TYPE": m}) for m in lines]
216216
return targets

0 commit comments

Comments
 (0)