Skip to content

Commit ed85091

Browse files
dhruvjonasschnelli
andcommitted
Add BIP324 short-IDs to protocol.cpp
Co-authored-by: Jonas Schnelli <[email protected]>
1 parent 2112bed commit ed85091

File tree

3 files changed

+80
-46
lines changed

3 files changed

+80
-46
lines changed

src/net.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -2753,8 +2753,9 @@ CNode::CNode(NodeId idIn,
27532753
{
27542754
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
27552755

2756-
for (const std::string &msg : getAllNetMessageTypes())
2757-
mapRecvBytesPerMsgType[msg] = 0;
2756+
for (const auto& msg : getAllNetMessageTypes()) {
2757+
mapRecvBytesPerMsgType[msg.second] = 0;
2758+
}
27582759
mapRecvBytesPerMsgType[NET_MESSAGE_TYPE_OTHER] = 0;
27592760

27602761
if (fLogIPs) {

src/protocol.cpp

+66-42
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,47 @@ 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.
50+
/** All known message types including the short-ID (as initially defined in BIP324).
51+
* Keep this in the same order as the list of messages above and in protocol.h.
5252
*/
53-
const static std::string allNetMessageTypes[] = {
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-
const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));
53+
const static std::map<uint8_t, std::string> allNetMessageTypes = {
54+
{37, NetMsgType::VERSION},
55+
{36, NetMsgType::VERACK},
56+
{13, NetMsgType::ADDR},
57+
{45, NetMsgType::ADDRV2},
58+
{46, NetMsgType::SENDADDRV2},
59+
{27, NetMsgType::INV},
60+
{24, NetMsgType::GETDATA},
61+
{29, NetMsgType::MERKLEBLOCK},
62+
{22, NetMsgType::GETBLOCKS},
63+
{25, NetMsgType::GETHEADERS},
64+
{35, NetMsgType::TX},
65+
{26, NetMsgType::HEADERS},
66+
{14, NetMsgType::BLOCK},
67+
{21, NetMsgType::GETADDR},
68+
{28, NetMsgType::MEMPOOL},
69+
{31, NetMsgType::PING},
70+
{32, NetMsgType::PONG},
71+
{30, NetMsgType::NOTFOUND},
72+
{20, NetMsgType::FILTERLOAD},
73+
{18, NetMsgType::FILTERADD},
74+
{19, NetMsgType::FILTERCLEAR},
75+
{34, NetMsgType::SENDHEADERS},
76+
{17, NetMsgType::FEEFILTER},
77+
{33, NetMsgType::SENDCMPCT},
78+
{16, NetMsgType::CMPCTBLOCK},
79+
{23, NetMsgType::GETBLOCKTXN},
80+
{15, NetMsgType::BLOCKTXN},
81+
{38, NetMsgType::GETCFILTERS},
82+
{39, NetMsgType::CFILTER},
83+
{40, NetMsgType::GETCFHEADERS},
84+
{41, NetMsgType::CFHEADERS},
85+
{42, NetMsgType::GETCFCHECKPT},
86+
{43, NetMsgType::CFCHECKPT},
87+
{44, NetMsgType::WTXIDRELAY},
88+
{47, NetMsgType::SENDTXRCNCL}};
89+
90+
static std::map<std::string, uint8_t> msgTypeShortIDs;
9191

9292
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
9393
{
@@ -178,9 +178,9 @@ std::string CInv::ToString() const
178178
}
179179
}
180180

181-
const std::vector<std::string> &getAllNetMessageTypes()
181+
const std::map<uint8_t, std::string>& getAllNetMessageTypes()
182182
{
183-
return allNetMessageTypesVec;
183+
return allNetMessageTypes;
184184
}
185185

186186
/**
@@ -222,3 +222,27 @@ GenTxid ToGenTxid(const CInv& inv)
222222
assert(inv.IsGenTxMsg());
223223
return inv.IsMsgWtx() ? GenTxid::Wtxid(inv.hash) : GenTxid::Txid(inv.hash);
224224
}
225+
226+
std::optional<uint8_t> GetShortIDFromMessageType(const std::string& message_type)
227+
{
228+
if (msgTypeShortIDs.size() != allNetMessageTypes.size()) {
229+
for (const std::pair<uint8_t, std::string> entry : allNetMessageTypes) {
230+
msgTypeShortIDs[entry.second] = entry.first;
231+
}
232+
}
233+
234+
auto it = msgTypeShortIDs.find(message_type);
235+
if (it != msgTypeShortIDs.end()) {
236+
return it->second;
237+
}
238+
return {};
239+
}
240+
241+
std::optional<std::string> GetMessageTypeFromShortID(const uint8_t shortID)
242+
{
243+
auto it = allNetMessageTypes.find(shortID);
244+
if (it != allNetMessageTypes.end()) {
245+
return it->second;
246+
}
247+
return {};
248+
}

src/protocol.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,17 @@ extern const char* WTXIDRELAY;
266266
extern const char* SENDTXRCNCL;
267267
}; // namespace NetMsgType
268268

269-
/* Get a vector of all valid message types (see above) */
270-
const std::vector<std::string>& getAllNetMessageTypes();
269+
/* Get a map of all valid message types (see above) */
270+
const std::map<uint8_t, std::string>& getAllNetMessageTypes();
271+
272+
/** Short message type IDs are a low bandwidth representations of a message type
273+
* The mapping is a peer to peer agreement (initially defined in BIP324)
274+
*
275+
* returns the short ID for a message type (if known) */
276+
std::optional<uint8_t> GetShortIDFromMessageType(const std::string& message_type);
277+
278+
/** returns the message type (string) from a short ID (as initially defined in BIP324) */
279+
std::optional<std::string> GetMessageTypeFromShortID(const uint8_t shortID);
271280

272281
/** nServices flags */
273282
enum ServiceFlags : uint64_t {

0 commit comments

Comments
 (0)