refactor: introduce MAKE_MSG macro for compile time check to ensure the p2p message name is short enough (#4614)

* refactor: introduce MAKE_MSG macro for compile time check to ensure the p2p message name is short enough

I would like this ofc to not be a macro, but to be as non-invasive as possible, I think a macro is best. Arguably these should all be converted to string_view and have a better abstraction layer over it (kinda like we do with sporks) as opposed to needing to place these names in multiple files.

* optional; also use it for bitcoin's NetMsgType's

* use std::size instead of potentiall non-constexpr strlen

Signed-off-by: pasta <pasta@dashboost.org>
This commit is contained in:
PastaPastaPasta 2021-12-11 17:15:00 -05:00 committed by GitHub
parent 683be4086e
commit 8f36cfe236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,78 +10,82 @@
static std::atomic<bool> g_initial_block_download_completed(false); static std::atomic<bool> g_initial_block_download_completed(false);
#define MAKE_MSG(var_name, p2p_name_str) \
const char* var_name=p2p_name_str; \
static_assert(std::size(p2p_name_str) <= CMessageHeader::COMMAND_SIZE + 1, "p2p_name_str cannot be greater than COMMAND_SIZE"); // Includes +1 for null termination character.
namespace NetMsgType { namespace NetMsgType {
const char *VERSION="version"; MAKE_MSG(VERSION, "version");
const char *VERACK="verack"; MAKE_MSG(VERACK, "verack");
const char *ADDR="addr"; MAKE_MSG(ADDR, "addr");
const char *ADDRV2="addrv2"; MAKE_MSG(ADDRV2, "addrv2");
const char *SENDADDRV2="sendaddrv2"; MAKE_MSG(SENDADDRV2, "sendaddrv2");
const char *INV="inv"; MAKE_MSG(INV, "inv");
const char *GETDATA="getdata"; MAKE_MSG(GETDATA, "getdata");
const char *MERKLEBLOCK="merkleblock"; MAKE_MSG(MERKLEBLOCK, "merkleblock");
const char *GETBLOCKS="getblocks"; MAKE_MSG(GETBLOCKS, "getblocks");
const char *GETHEADERS="getheaders"; MAKE_MSG(GETHEADERS, "getheaders");
const char *TX="tx"; MAKE_MSG(TX, "tx");
const char *HEADERS="headers"; MAKE_MSG(HEADERS, "headers");
const char *BLOCK="block"; MAKE_MSG(BLOCK, "block");
const char *GETADDR="getaddr"; MAKE_MSG(GETADDR, "getaddr");
const char *MEMPOOL="mempool"; MAKE_MSG(MEMPOOL, "mempool");
const char *PING="ping"; MAKE_MSG(PING, "ping");
const char *PONG="pong"; MAKE_MSG(PONG, "pong");
const char *NOTFOUND="notfound"; MAKE_MSG(NOTFOUND, "notfound");
const char *FILTERLOAD="filterload"; MAKE_MSG(FILTERLOAD, "filterload");
const char *FILTERADD="filteradd"; MAKE_MSG(FILTERADD, "filteradd");
const char *FILTERCLEAR="filterclear"; MAKE_MSG(FILTERCLEAR, "filterclear");
const char *REJECT="reject"; MAKE_MSG(REJECT, "reject");
const char *SENDHEADERS="sendheaders"; MAKE_MSG(SENDHEADERS, "sendheaders");
const char *SENDCMPCT="sendcmpct"; MAKE_MSG(SENDCMPCT, "sendcmpct");
const char *CMPCTBLOCK="cmpctblock"; MAKE_MSG(CMPCTBLOCK, "cmpctblock");
const char *GETBLOCKTXN="getblocktxn"; MAKE_MSG(GETBLOCKTXN, "getblocktxn");
const char *BLOCKTXN="blocktxn"; MAKE_MSG(BLOCKTXN, "blocktxn");
const char *GETCFILTERS="getcfilters"; MAKE_MSG(GETCFILTERS, "getcfilters");
const char *CFILTER="cfilter"; MAKE_MSG(CFILTER, "cfilter");
const char *GETCFHEADERS="getcfheaders"; MAKE_MSG(GETCFHEADERS, "getcfheaders");
const char *CFHEADERS="cfheaders"; MAKE_MSG(CFHEADERS, "cfheaders");
const char *GETCFCHECKPT="getcfcheckpt"; MAKE_MSG(GETCFCHECKPT, "getcfcheckpt");
const char *CFCHECKPT="cfcheckpt"; MAKE_MSG(CFCHECKPT, "cfcheckpt");
// Dash message types // Dash message types
const char *LEGACYTXLOCKREQUEST="ix"; MAKE_MSG(LEGACYTXLOCKREQUEST, "ix");
const char *SPORK="spork"; MAKE_MSG(SPORK, "spork");
const char *GETSPORKS="getsporks"; MAKE_MSG(GETSPORKS, "getsporks");
const char *DSACCEPT="dsa"; MAKE_MSG(DSACCEPT, "dsa");
const char *DSVIN="dsi"; MAKE_MSG(DSVIN, "dsi");
const char *DSFINALTX="dsf"; MAKE_MSG(DSFINALTX, "dsf");
const char *DSSIGNFINALTX="dss"; MAKE_MSG(DSSIGNFINALTX, "dss");
const char *DSCOMPLETE="dsc"; MAKE_MSG(DSCOMPLETE, "dsc");
const char *DSSTATUSUPDATE="dssu"; MAKE_MSG(DSSTATUSUPDATE, "dssu");
const char *DSTX="dstx"; MAKE_MSG(DSTX, "dstx");
const char *DSQUEUE="dsq"; MAKE_MSG(DSQUEUE, "dsq");
const char *SENDDSQUEUE="senddsq"; MAKE_MSG(SENDDSQUEUE, "senddsq");
const char *SYNCSTATUSCOUNT="ssc"; MAKE_MSG(SYNCSTATUSCOUNT, "ssc");
const char *MNGOVERNANCESYNC="govsync"; MAKE_MSG(MNGOVERNANCESYNC, "govsync");
const char *MNGOVERNANCEOBJECT="govobj"; MAKE_MSG(MNGOVERNANCEOBJECT, "govobj");
const char *MNGOVERNANCEOBJECTVOTE="govobjvote"; MAKE_MSG(MNGOVERNANCEOBJECTVOTE, "govobjvote");
const char *GETMNLISTDIFF="getmnlistd"; MAKE_MSG(GETMNLISTDIFF, "getmnlistd");
const char *MNLISTDIFF="mnlistdiff"; MAKE_MSG(MNLISTDIFF, "mnlistdiff");
const char *QSENDRECSIGS="qsendrecsigs"; MAKE_MSG(QSENDRECSIGS, "qsendrecsigs");
const char *QFCOMMITMENT="qfcommit"; MAKE_MSG(QFCOMMITMENT, "qfcommit");
const char *QCONTRIB="qcontrib"; MAKE_MSG(QCONTRIB, "qcontrib");
const char *QCOMPLAINT="qcomplaint"; MAKE_MSG(QCOMPLAINT, "qcomplaint");
const char *QJUSTIFICATION="qjustify"; MAKE_MSG(QJUSTIFICATION, "qjustify");
const char *QPCOMMITMENT="qpcommit"; MAKE_MSG(QPCOMMITMENT, "qpcommit");
const char *QWATCH="qwatch"; MAKE_MSG(QWATCH, "qwatch");
const char *QSIGSESANN="qsigsesann"; MAKE_MSG(QSIGSESANN, "qsigsesann");
const char *QSIGSHARESINV="qsigsinv"; MAKE_MSG(QSIGSHARESINV, "qsigsinv");
const char *QGETSIGSHARES="qgetsigs"; MAKE_MSG(QGETSIGSHARES, "qgetsigs");
const char *QBSIGSHARES="qbsigs"; MAKE_MSG(QBSIGSHARES, "qbsigs");
const char *QSIGREC="qsigrec"; MAKE_MSG(QSIGREC, "qsigrec");
const char *QSIGSHARE="qsigshare"; MAKE_MSG(QSIGSHARE, "qsigshare");
const char* QGETDATA = "qgetdata"; MAKE_MSG(QGETDATA, "qgetdata");
const char* QDATA = "qdata"; MAKE_MSG(QDATA, "qdata");
const char *CLSIG="clsig"; MAKE_MSG(CLSIG, "clsig");
const char *ISLOCK="islock"; MAKE_MSG(ISLOCK, "islock");
const char *ISDLOCK="isdlock"; MAKE_MSG(ISDLOCK, "isdlock");
const char *MNAUTH="mnauth"; MAKE_MSG(MNAUTH, "mnauth");
}; // namespace NetMsgType }; // namespace NetMsgType
/** All known message types. Keep this in the same order as the list of /** All known message types. Keep this in the same order as the list of