Skip to content

Commit e5e3572

Browse files
laanwjUdjinM6
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 dbbc511 commit e5e3572

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
@@ -1791,8 +1791,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
17911791
}
17921792
}
17931793

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

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

20312028
pfrom->fSuccessfullyConnected = true;
2029+
return true;
20322030
}
20332031

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

2042-
else if (strCommand == NetMsgType::ADDR)
2043-
{
2039+
if (strCommand == NetMsgType::ADDR) {
20442040
std::vector<CAddress> vAddr;
20452041
vRecv >> vAddr;
20462042

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

2091-
else if (strCommand == NetMsgType::SENDHEADERS)
2092-
{
2088+
if (strCommand == NetMsgType::SENDHEADERS) {
20932089
LOCK(cs_main);
20942090
State(pfrom->GetId())->fPreferHeaders = true;
2091+
return true;
20952092
}
20962093

2097-
2098-
else if (strCommand == NetMsgType::SENDCMPCT)
2099-
{
2094+
if (strCommand == NetMsgType::SENDCMPCT) {
21002095
bool fAnnounceUsingCMPCTBLOCK = false;
21012096
uint64_t nCMPCTBLOCKVersion = 1;
21022097
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
@@ -2106,6 +2101,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21062101
State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
21072102
State(pfrom->GetId())->fSupportsDesiredCmpctVersion = true;
21082103
}
2104+
return true;
21092105
}
21102106

21112107

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

2126-
2127-
else if (strCommand == NetMsgType::INV)
2128-
{
2122+
if (strCommand == NetMsgType::INV) {
21292123
std::vector<CInv> vInv;
21302124
vRecv >> vInv;
21312125
if (vInv.size() > MAX_INV_SZ)
@@ -2219,11 +2213,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22192213
// Track requests for our stuff
22202214
GetMainSignals().Inventory(inv.hash);
22212215
}
2216+
return true;
22222217
}
22232218

2224-
2225-
else if (strCommand == NetMsgType::GETDATA)
2226-
{
2219+
if (strCommand == NetMsgType::GETDATA) {
22272220
std::vector<CInv> vInv;
22282221
vRecv >> vInv;
22292222
if (vInv.size() > MAX_INV_SZ)
@@ -2241,11 +2234,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22412234

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

2246-
2247-
else if (strCommand == NetMsgType::GETBLOCKS)
2248-
{
2240+
if (strCommand == NetMsgType::GETBLOCKS) {
22492241
CBlockLocator locator;
22502242
uint256 hashStop;
22512243
vRecv >> locator >> hashStop;
@@ -2302,11 +2294,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23022294
break;
23032295
}
23042296
}
2297+
return true;
23052298
}
23062299

2307-
2308-
else if (strCommand == NetMsgType::GETBLOCKTXN)
2309-
{
2300+
if (strCommand == NetMsgType::GETBLOCKTXN) {
23102301
BlockTransactionsRequest req;
23112302
vRecv >> req;
23122303

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

23542345
SendBlockTransactions(block, req, pfrom, connman);
2346+
return true;
23552347
}
23562348

2357-
2358-
else if (strCommand == NetMsgType::GETHEADERS)
2359-
{
2349+
if (strCommand == NetMsgType::GETHEADERS) {
23602350
CBlockLocator locator;
23612351
uint256 hashStop;
23622352
vRecv >> locator >> hashStop;
@@ -2414,11 +2404,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
24142404
// in the SendMessages logic.
24152405
nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
24162406
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
2407+
return true;
24172408
}
24182409

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

2611-
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2601+
if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
26122602
{
26132603
CBlockHeaderAndShortTxIDs cmpctblock;
26142604
vRecv >> cmpctblock;
@@ -2820,10 +2810,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28202810
MarkBlockAsReceived(pblock->GetHash());
28212811
}
28222812
}
2823-
2813+
return true;
28242814
}
28252815

2826-
else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
2816+
if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
28272817
{
28282818
BlockTransactions resp;
28292819
vRecv >> resp;
@@ -2896,10 +2886,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28962886
mapBlockSource.erase(pblock->GetHash());
28972887
}
28982888
}
2889+
return true;
28992890
}
29002891

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

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

2927-
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2917+
if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
29282918
{
29292919
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
29302920
vRecv >> *pblock;
@@ -2950,11 +2940,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29502940
LOCK(cs_main);
29512941
mapBlockSource.erase(pblock->GetHash());
29522942
}
2943+
return true;
29532944
}
29542945

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

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

30002988
LOCK(pfrom->cs_inventory);
30012989
pfrom->fSendMempool = true;
2990+
return true;
30022991
}
30032992

3004-
3005-
else if (strCommand == NetMsgType::PING)
3006-
{
2993+
if (strCommand == NetMsgType::PING) {
30072994
if (pfrom->nVersion > BIP0031_VERSION)
30082995
{
30092996
uint64_t nonce = 0;
@@ -3021,11 +3008,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
30213008
// return very quickly.
30223009
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
30233010
}
3011+
return true;
30243012
}
30253013

3026-
3027-
else if (strCommand == NetMsgType::PONG)
3028-
{
3014+
if (strCommand == NetMsgType::PONG) {
30293015
int64_t pingUsecEnd = nTimeReceived;
30303016
uint64_t nonce = 0;
30313017
size_t nAvail = vRecv.in_avail();
@@ -3078,11 +3064,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
30783064
if (bPingFinished) {
30793065
pfrom->nPingNonceSent = 0;
30803066
}
3067+
return true;
30813068
}
30823069

3083-
3084-
else if (strCommand == NetMsgType::FILTERLOAD)
3085-
{
3070+
if (strCommand == NetMsgType::FILTERLOAD) {
30863071
CBloomFilter filter;
30873072
vRecv >> filter;
30883073

@@ -3100,11 +3085,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
31003085
pfrom->pfilter->UpdateEmptyFull();
31013086
pfrom->fRelayTxes = true;
31023087
}
3088+
return true;
31033089
}
31043090

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

@@ -3125,21 +3109,21 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
31253109
LOCK(cs_main);
31263110
Misbehaving(pfrom->GetId(), 100);
31273111
}
3112+
return true;
31283113
}
31293114

3130-
3131-
else if (strCommand == NetMsgType::FILTERCLEAR)
3132-
{
3115+
if (strCommand == NetMsgType::FILTERCLEAR) {
31333116
LOCK(pfrom->cs_filter);
31343117
if (pfrom->GetLocalServices() & NODE_BLOOM) {
31353118
delete pfrom->pfilter;
31363119
pfrom->pfilter = new CBloomFilter();
31373120
}
31383121
pfrom->fRelayTxes = true;
3122+
return true;
31393123
}
31403124

31413125

3142-
else if (strCommand == NetMsgType::GETMNLISTDIFF) {
3126+
if (strCommand == NetMsgType::GETMNLISTDIFF) {
31433127
CGetSimplifiedMNListDiff cmd;
31443128
vRecv >> cmd;
31453129

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

31583143

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

31663152

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

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

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

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

0 commit comments

Comments
 (0)