Skip to content

Commit c261173

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#13946: p2p: Clarify control flow in ProcessMessage
fa6c3de p2p: Clarify control flow in ProcessMessage() (MarcoFalke) Pull request description: `ProcessMessage` is effectively a massive switch case construct. In the past there were attempts to clarify the control flow in `ProcessMessage()` by moving each case into a separate static function (see bitcoin#9608). It was closed because it wasn't clear if moving each case into a function was the right approach. Though, we can quasi treat each case as a function by adding a return statement to each case. (Can be seen as a continuation of bugfix bitcoin#13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as bitcoin#9608 and bitcoin#10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201
1 parent 7f88a67 commit c261173

File tree

1 file changed

+75
-91
lines changed

1 file changed

+75
-91
lines changed

src/net_processing.cpp

Lines changed: 75 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,8 +1790,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
17901790
}
17911791
}
17921792

1793-
else if (strCommand == NetMsgType::VERSION)
1794-
{
1793+
if (strCommand == NetMsgType::VERSION) {
17951794
// Each connection can only send one version message
17961795
if (pfrom->nVersion != 0)
17971796
{
@@ -1961,9 +1960,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19611960
return true;
19621961
}
19631962

1964-
1965-
else if (pfrom->nVersion == 0)
1966-
{
1963+
if (pfrom->nVersion == 0) {
19671964
// Must have a version message before anything else
19681965
LOCK(cs_main);
19691966
Misbehaving(pfrom->GetId(), 1);
@@ -2028,18 +2025,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20282025
}
20292026

20302027
pfrom->fSuccessfullyConnected = true;
2028+
return true;
20312029
}
20322030

2033-
else if (!pfrom->fSuccessfullyConnected)
2034-
{
2031+
if (!pfrom->fSuccessfullyConnected) {
20352032
// Must have a verack message before anything else
20362033
LOCK(cs_main);
20372034
Misbehaving(pfrom->GetId(), 1);
20382035
return false;
20392036
}
20402037

2041-
else if (strCommand == NetMsgType::ADDR)
2042-
{
2038+
if (strCommand == NetMsgType::ADDR) {
20432039
std::vector<CAddress> vAddr;
20442040
vRecv >> vAddr;
20452041

@@ -2085,17 +2081,16 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20852081
pfrom->fGetAddr = false;
20862082
if (pfrom->fOneShot)
20872083
pfrom->fDisconnect = true;
2084+
return true;
20882085
}
20892086

2090-
else if (strCommand == NetMsgType::SENDHEADERS)
2091-
{
2087+
if (strCommand == NetMsgType::SENDHEADERS) {
20922088
LOCK(cs_main);
20932089
State(pfrom->GetId())->fPreferHeaders = true;
2090+
return true;
20942091
}
20952092

2096-
2097-
else if (strCommand == NetMsgType::SENDCMPCT)
2098-
{
2093+
if (strCommand == NetMsgType::SENDCMPCT) {
20992094
bool fAnnounceUsingCMPCTBLOCK = false;
21002095
uint64_t nCMPCTBLOCKVersion = 1;
21012096
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
@@ -2105,6 +2100,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21052100
State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
21062101
State(pfrom->GetId())->fSupportsDesiredCmpctVersion = true;
21072102
}
2103+
return true;
21082104
}
21092105

21102106

@@ -2122,9 +2118,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21222118
pfrom->fSendRecSigs = b;
21232119
}
21242120

2125-
2126-
else if (strCommand == NetMsgType::INV)
2127-
{
2121+
if (strCommand == NetMsgType::INV) {
21282122
std::vector<CInv> vInv;
21292123
vRecv >> vInv;
21302124
if (vInv.size() > MAX_INV_SZ)
@@ -2218,11 +2212,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22182212
// Track requests for our stuff
22192213
GetMainSignals().Inventory(inv.hash);
22202214
}
2215+
return true;
22212216
}
22222217

2223-
2224-
else if (strCommand == NetMsgType::GETDATA)
2225-
{
2218+
if (strCommand == NetMsgType::GETDATA) {
22262219
std::vector<CInv> vInv;
22272220
vRecv >> vInv;
22282221
if (vInv.size() > MAX_INV_SZ)
@@ -2240,11 +2233,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22402233

22412234
pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
22422235
ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
2236+
return true;
22432237
}
22442238

2245-
2246-
else if (strCommand == NetMsgType::GETBLOCKS)
2247-
{
2239+
if (strCommand == NetMsgType::GETBLOCKS) {
22482240
CBlockLocator locator;
22492241
uint256 hashStop;
22502242
vRecv >> locator >> hashStop;
@@ -2301,11 +2293,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23012293
break;
23022294
}
23032295
}
2296+
return true;
23042297
}
23052298

2306-
2307-
else if (strCommand == NetMsgType::GETBLOCKTXN)
2308-
{
2299+
if (strCommand == NetMsgType::GETBLOCKTXN) {
23092300
BlockTransactionsRequest req;
23102301
vRecv >> req;
23112302

@@ -2351,11 +2342,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23512342
assert(ret);
23522343

23532344
SendBlockTransactions(block, req, pfrom, connman);
2345+
return true;
23542346
}
23552347

2356-
2357-
else if (strCommand == NetMsgType::GETHEADERS)
2358-
{
2348+
if (strCommand == NetMsgType::GETHEADERS) {
23592349
CBlockLocator locator;
23602350
uint256 hashStop;
23612351
vRecv >> locator >> hashStop;
@@ -2413,11 +2403,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
24132403
// in the SendMessages logic.
24142404
nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
24152405
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
2406+
return true;
24162407
}
24172408

2418-
2419-
else if (strCommand == NetMsgType::TX || strCommand == NetMsgType::DSTX || strCommand == NetMsgType::LEGACYTXLOCKREQUEST)
2420-
{
2409+
if (strCommand == NetMsgType::TX || strCommand == NetMsgType::DSTX || strCommand == NetMsgType::LEGACYTXLOCKREQUEST) {
24212410
// Stop processing the transaction early if
24222411
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
24232412
if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
@@ -2605,9 +2594,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26052594
Misbehaving(pfrom->GetId(), nDoS);
26062595
}
26072596
}
2597+
return true;
26082598
}
26092599

2610-
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2600+
if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
26112601
{
26122602
CBlockHeaderAndShortTxIDs cmpctblock;
26132603
vRecv >> cmpctblock;
@@ -2819,10 +2809,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28192809
MarkBlockAsReceived(pblock->GetHash());
28202810
}
28212811
}
2822-
2812+
return true;
28232813
}
28242814

2825-
else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
2815+
if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
28262816
{
28272817
BlockTransactions resp;
28282818
vRecv >> resp;
@@ -2895,10 +2885,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28952885
mapBlockSource.erase(pblock->GetHash());
28962886
}
28972887
}
2888+
return true;
28982889
}
28992890

2900-
2901-
else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
2891+
if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
29022892
{
29032893
std::vector<CBlockHeader> headers;
29042894

@@ -2923,7 +2913,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29232913
return ProcessHeadersMessage(pfrom, connman, headers, chainparams, should_punish);
29242914
}
29252915

2926-
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2916+
if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
29272917
{
29282918
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
29292919
vRecv >> *pblock;
@@ -2949,11 +2939,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29492939
LOCK(cs_main);
29502940
mapBlockSource.erase(pblock->GetHash());
29512941
}
2942+
return true;
29522943
}
29532944

2954-
2955-
else if (strCommand == NetMsgType::GETADDR)
2956-
{
2945+
if (strCommand == NetMsgType::GETADDR) {
29572946
// This asymmetric behavior for inbound and outbound connections was introduced
29582947
// to prevent a fingerprinting attack: an attacker can send specific fake addresses
29592948
// to users' AddrMan and later request them by sending getaddr messages.
@@ -2977,11 +2966,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29772966
FastRandomContext insecure_rand;
29782967
for (const CAddress &addr : vAddr)
29792968
pfrom->PushAddress(addr, insecure_rand);
2969+
return true;
29802970
}
29812971

2982-
2983-
else if (strCommand == NetMsgType::MEMPOOL)
2984-
{
2972+
if (strCommand == NetMsgType::MEMPOOL) {
29852973
if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
29862974
{
29872975
LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
@@ -2998,11 +2986,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29982986

29992987
LOCK(pfrom->cs_inventory);
30002988
pfrom->fSendMempool = true;
2989+
return true;
30012990
}
30022991

3003-
3004-
else if (strCommand == NetMsgType::PING)
3005-
{
2992+
if (strCommand == NetMsgType::PING) {
30062993
if (pfrom->nVersion > BIP0031_VERSION)
30072994
{
30082995
uint64_t nonce = 0;
@@ -3020,11 +3007,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
30203007
// return very quickly.
30213008
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
30223009
}
3010+
return true;
30233011
}
30243012

3025-
3026-
else if (strCommand == NetMsgType::PONG)
3027-
{
3013+
if (strCommand == NetMsgType::PONG) {
30283014
int64_t pingUsecEnd = nTimeReceived;
30293015
uint64_t nonce = 0;
30303016
size_t nAvail = vRecv.in_avail();
@@ -3077,11 +3063,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
30773063
if (bPingFinished) {
30783064
pfrom->nPingNonceSent = 0;
30793065
}
3066+
return true;
30803067
}
30813068

3082-
3083-
else if (strCommand == NetMsgType::FILTERLOAD)
3084-
{
3069+
if (strCommand == NetMsgType::FILTERLOAD) {
30853070
CBloomFilter filter;
30863071
vRecv >> filter;
30873072

@@ -3099,11 +3084,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
30993084
pfrom->pfilter->UpdateEmptyFull();
31003085
pfrom->fRelayTxes = true;
31013086
}
3087+
return true;
31023088
}
31033089

3104-
3105-
else if (strCommand == NetMsgType::FILTERADD)
3106-
{
3090+
if (strCommand == NetMsgType::FILTERADD) {
31073091
std::vector<unsigned char> vData;
31083092
vRecv >> vData;
31093093

@@ -3124,21 +3108,21 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
31243108
LOCK(cs_main);
31253109
Misbehaving(pfrom->GetId(), 100);
31263110
}
3111+
return true;
31273112
}
31283113

3129-
3130-
else if (strCommand == NetMsgType::FILTERCLEAR)
3131-
{
3114+
if (strCommand == NetMsgType::FILTERCLEAR) {
31323115
LOCK(pfrom->cs_filter);
31333116
if (pfrom->GetLocalServices() & NODE_BLOOM) {
31343117
delete pfrom->pfilter;
31353118
pfrom->pfilter = new CBloomFilter();
31363119
}
31373120
pfrom->fRelayTxes = true;
3121+
return true;
31383122
}
31393123

31403124

3141-
else if (strCommand == NetMsgType::GETMNLISTDIFF) {
3125+
if (strCommand == NetMsgType::GETMNLISTDIFF) {
31423126
CGetSimplifiedMNListDiff cmd;
31433127
vRecv >> cmd;
31443128

@@ -3152,57 +3136,57 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
31523136
LogPrint(BCLog::NET, "getmnlistdiff failed for baseBlockHash=%s, blockHash=%s. error=%s\n", cmd.baseBlockHash.ToString(), cmd.blockHash.ToString(), strError);
31533137
Misbehaving(pfrom->GetId(), 1);
31543138
}
3139+
return true;
31553140
}
31563141

31573142

3158-
else if (strCommand == NetMsgType::MNLISTDIFF) {
3143+
if (strCommand == NetMsgType::MNLISTDIFF) {
31593144
// we have never requested this
31603145
LOCK(cs_main);
31613146
Misbehaving(pfrom->GetId(), 100);
31623147
LogPrint(BCLog::NET, "received not-requested mnlistdiff. peer=%d\n", pfrom->GetId());
3148+
return true;
31633149
}
31643150

31653151

3166-
else if (strCommand == NetMsgType::NOTFOUND) {
3152+
if (strCommand == NetMsgType::NOTFOUND) {
31673153
// We do not care about the NOTFOUND message, but logging an Unknown Command
31683154
// message would be undesirable as we transmit it ourselves.
3155+
return true;
31693156
}
31703157

3171-
else {
3172-
bool found = false;
3173-
const std::vector<std::string> &allMessages = getAllNetMessageTypes();
3174-
for (const std::string msg : allMessages) {
3175-
if(msg == strCommand) {
3176-
found = true;
3177-
break;
3178-
}
3158+
bool found = false;
3159+
const std::vector<std::string> &allMessages = getAllNetMessageTypes();
3160+
for (const std::string msg : allMessages) {
3161+
if(msg == strCommand) {
3162+
found = true;
3163+
break;
31793164
}
3165+
}
31803166

3181-
if (found)
3182-
{
3183-
//probably one the extensions
3167+
if (found)
3168+
{
3169+
//probably one the extensions
31843170
#ifdef ENABLE_WALLET
3185-
privateSendClient.ProcessMessage(pfrom, strCommand, vRecv, *connman);
3171+
privateSendClient.ProcessMessage(pfrom, strCommand, vRecv, *connman);
31863172
#endif // ENABLE_WALLET
3187-
privateSendServer.ProcessMessage(pfrom, strCommand, vRecv, *connman);
3188-
sporkManager.ProcessSpork(pfrom, strCommand, vRecv, *connman);
3189-
masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
3190-
governance.ProcessMessage(pfrom, strCommand, vRecv, *connman);
3191-
CMNAuth::ProcessMessage(pfrom, strCommand, vRecv, *connman);
3192-
llmq::quorumBlockProcessor->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3193-
llmq::quorumDKGSessionManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3194-
llmq::quorumSigSharesManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3195-
llmq::quorumSigningManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3196-
llmq::chainLocksHandler->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3197-
llmq::quorumInstantSendManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3198-
}
3199-
else
3200-
{
3201-
// Ignore unknown commands for extensibility
3202-
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
3203-
}
3173+
privateSendServer.ProcessMessage(pfrom, strCommand, vRecv, *connman);
3174+
sporkManager.ProcessSpork(pfrom, strCommand, vRecv, *connman);
3175+
masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
3176+
governance.ProcessMessage(pfrom, strCommand, vRecv, *connman);
3177+
CMNAuth::ProcessMessage(pfrom, strCommand, vRecv, *connman);
3178+
llmq::quorumBlockProcessor->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3179+
llmq::quorumDKGSessionManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3180+
llmq::quorumSigSharesManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3181+
llmq::quorumSigningManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3182+
llmq::chainLocksHandler->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3183+
llmq::quorumInstantSendManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
3184+
return true;
32043185
}
32053186

3187+
// Ignore unknown commands for extensibility
3188+
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
3189+
32063190
return true;
32073191
}
32083192

0 commit comments

Comments
 (0)