@@ -47,47 +47,47 @@ const char *WTXIDRELAY="wtxidrelay";
47
47
const char *SENDTXRCNCL=" sendtxrcncl" ;
48
48
} // namespace NetMsgType
49
49
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.
52
52
*/
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 ;
91
91
92
92
CMessageHeader::CMessageHeader (const MessageStartChars& pchMessageStartIn, const char * pszCommand, unsigned int nMessageSizeIn)
93
93
{
@@ -178,9 +178,9 @@ std::string CInv::ToString() const
178
178
}
179
179
}
180
180
181
- const std::vector< std::string> & getAllNetMessageTypes ()
181
+ const std::map< uint8_t , std::string>& getAllNetMessageTypes ()
182
182
{
183
- return allNetMessageTypesVec ;
183
+ return allNetMessageTypes ;
184
184
}
185
185
186
186
/* *
@@ -222,3 +222,27 @@ GenTxid ToGenTxid(const CInv& inv)
222
222
assert (inv.IsGenTxMsg ());
223
223
return inv.IsMsgWtx () ? GenTxid::Wtxid (inv.hash ) : GenTxid::Txid (inv.hash );
224
224
}
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
+ }
0 commit comments