Skip to content

Commit 021dce9

Browse files
committed
Merge #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 #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 #13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as #9608 and #10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201
2 parents 4cef8e0 + fa6c3de commit 021dce9

File tree

1 file changed

+49
-68
lines changed

1 file changed

+49
-68
lines changed

src/net_processing.cpp

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,8 +1618,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
16181618
return true;
16191619
}
16201620

1621-
else if (strCommand == NetMsgType::VERSION)
1622-
{
1621+
if (strCommand == NetMsgType::VERSION) {
16231622
// Each connection can only send one version message
16241623
if (pfrom->nVersion != 0)
16251624
{
@@ -1793,9 +1792,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
17931792
return true;
17941793
}
17951794

1796-
1797-
else if (pfrom->nVersion == 0)
1798-
{
1795+
if (pfrom->nVersion == 0) {
17991796
// Must have a version message before anything else
18001797
LOCK(cs_main);
18011798
Misbehaving(pfrom->GetId(), 1);
@@ -1839,18 +1836,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
18391836
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
18401837
}
18411838
pfrom->fSuccessfullyConnected = true;
1839+
return true;
18421840
}
18431841

1844-
else if (!pfrom->fSuccessfullyConnected)
1845-
{
1842+
if (!pfrom->fSuccessfullyConnected) {
18461843
// Must have a verack message before anything else
18471844
LOCK(cs_main);
18481845
Misbehaving(pfrom->GetId(), 1);
18491846
return false;
18501847
}
18511848

1852-
else if (strCommand == NetMsgType::ADDR)
1853-
{
1849+
if (strCommand == NetMsgType::ADDR) {
18541850
std::vector<CAddress> vAddr;
18551851
vRecv >> vAddr;
18561852

@@ -1897,16 +1893,16 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
18971893
pfrom->fGetAddr = false;
18981894
if (pfrom->fOneShot)
18991895
pfrom->fDisconnect = true;
1896+
return true;
19001897
}
19011898

1902-
else if (strCommand == NetMsgType::SENDHEADERS)
1903-
{
1899+
if (strCommand == NetMsgType::SENDHEADERS) {
19041900
LOCK(cs_main);
19051901
State(pfrom->GetId())->fPreferHeaders = true;
1902+
return true;
19061903
}
19071904

1908-
else if (strCommand == NetMsgType::SENDCMPCT)
1909-
{
1905+
if (strCommand == NetMsgType::SENDCMPCT) {
19101906
bool fAnnounceUsingCMPCTBLOCK = false;
19111907
uint64_t nCMPCTBLOCKVersion = 0;
19121908
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
@@ -1926,11 +1922,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19261922
State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
19271923
}
19281924
}
1925+
return true;
19291926
}
19301927

1931-
1932-
else if (strCommand == NetMsgType::INV)
1933-
{
1928+
if (strCommand == NetMsgType::INV) {
19341929
std::vector<CInv> vInv;
19351930
vRecv >> vInv;
19361931
if (vInv.size() > MAX_INV_SZ)
@@ -1984,11 +1979,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19841979
}
19851980
}
19861981
}
1982+
return true;
19871983
}
19881984

1989-
1990-
else if (strCommand == NetMsgType::GETDATA)
1991-
{
1985+
if (strCommand == NetMsgType::GETDATA) {
19921986
std::vector<CInv> vInv;
19931987
vRecv >> vInv;
19941988
if (vInv.size() > MAX_INV_SZ)
@@ -2006,11 +2000,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20062000

20072001
pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
20082002
ProcessGetData(pfrom, chainparams, connman, interruptMsgProc);
2003+
return true;
20092004
}
20102005

2011-
2012-
else if (strCommand == NetMsgType::GETBLOCKS)
2013-
{
2006+
if (strCommand == NetMsgType::GETBLOCKS) {
20142007
CBlockLocator locator;
20152008
uint256 hashStop;
20162009
vRecv >> locator >> hashStop;
@@ -2075,11 +2068,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20752068
break;
20762069
}
20772070
}
2071+
return true;
20782072
}
20792073

2080-
2081-
else if (strCommand == NetMsgType::GETBLOCKTXN)
2082-
{
2074+
if (strCommand == NetMsgType::GETBLOCKTXN) {
20832075
BlockTransactionsRequest req;
20842076
vRecv >> req;
20852077

@@ -2125,11 +2117,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21252117
assert(ret);
21262118

21272119
SendBlockTransactions(block, req, pfrom, connman);
2120+
return true;
21282121
}
21292122

2130-
2131-
else if (strCommand == NetMsgType::GETHEADERS)
2132-
{
2123+
if (strCommand == NetMsgType::GETHEADERS) {
21332124
CBlockLocator locator;
21342125
uint256 hashStop;
21352126
vRecv >> locator >> hashStop;
@@ -2193,11 +2184,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21932184
// in the SendMessages logic.
21942185
nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
21952186
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
2187+
return true;
21962188
}
21972189

2198-
2199-
else if (strCommand == NetMsgType::TX)
2200-
{
2190+
if (strCommand == NetMsgType::TX) {
22012191
// Stop processing the transaction early if
22022192
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
22032193
if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
@@ -2381,10 +2371,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23812371
Misbehaving(pfrom->GetId(), nDoS);
23822372
}
23832373
}
2374+
return true;
23842375
}
23852376

2386-
2387-
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2377+
if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
23882378
{
23892379
CBlockHeaderAndShortTxIDs cmpctblock;
23902380
vRecv >> cmpctblock;
@@ -2602,10 +2592,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26022592
MarkBlockAsReceived(pblock->GetHash());
26032593
}
26042594
}
2605-
2595+
return true;
26062596
}
26072597

2608-
else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
2598+
if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
26092599
{
26102600
BlockTransactions resp;
26112601
vRecv >> resp;
@@ -2677,10 +2667,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26772667
mapBlockSource.erase(pblock->GetHash());
26782668
}
26792669
}
2670+
return true;
26802671
}
26812672

2682-
2683-
else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
2673+
if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
26842674
{
26852675
std::vector<CBlockHeader> headers;
26862676

@@ -2705,7 +2695,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27052695
return ProcessHeadersMessage(pfrom, connman, headers, chainparams, should_punish);
27062696
}
27072697

2708-
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2698+
if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
27092699
{
27102700
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
27112701
vRecv >> *pblock;
@@ -2731,11 +2721,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27312721
LOCK(cs_main);
27322722
mapBlockSource.erase(pblock->GetHash());
27332723
}
2724+
return true;
27342725
}
27352726

2736-
2737-
else if (strCommand == NetMsgType::GETADDR)
2738-
{
2727+
if (strCommand == NetMsgType::GETADDR) {
27392728
// This asymmetric behavior for inbound and outbound connections was introduced
27402729
// to prevent a fingerprinting attack: an attacker can send specific fake addresses
27412730
// to users' AddrMan and later request them by sending getaddr messages.
@@ -2759,11 +2748,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27592748
FastRandomContext insecure_rand;
27602749
for (const CAddress &addr : vAddr)
27612750
pfrom->PushAddress(addr, insecure_rand);
2751+
return true;
27622752
}
27632753

2764-
2765-
else if (strCommand == NetMsgType::MEMPOOL)
2766-
{
2754+
if (strCommand == NetMsgType::MEMPOOL) {
27672755
if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
27682756
{
27692757
LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
@@ -2780,11 +2768,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27802768

27812769
LOCK(pfrom->cs_inventory);
27822770
pfrom->fSendMempool = true;
2771+
return true;
27832772
}
27842773

2785-
2786-
else if (strCommand == NetMsgType::PING)
2787-
{
2774+
if (strCommand == NetMsgType::PING) {
27882775
if (pfrom->nVersion > BIP0031_VERSION)
27892776
{
27902777
uint64_t nonce = 0;
@@ -2802,11 +2789,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28022789
// return very quickly.
28032790
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
28042791
}
2792+
return true;
28052793
}
28062794

2807-
2808-
else if (strCommand == NetMsgType::PONG)
2809-
{
2795+
if (strCommand == NetMsgType::PONG) {
28102796
int64_t pingUsecEnd = nTimeReceived;
28112797
uint64_t nonce = 0;
28122798
size_t nAvail = vRecv.in_avail();
@@ -2859,11 +2845,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28592845
if (bPingFinished) {
28602846
pfrom->nPingNonceSent = 0;
28612847
}
2848+
return true;
28622849
}
28632850

2864-
2865-
else if (strCommand == NetMsgType::FILTERLOAD)
2866-
{
2851+
if (strCommand == NetMsgType::FILTERLOAD) {
28672852
CBloomFilter filter;
28682853
vRecv >> filter;
28692854

@@ -2880,11 +2865,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28802865
pfrom->pfilter->UpdateEmptyFull();
28812866
pfrom->fRelayTxes = true;
28822867
}
2868+
return true;
28832869
}
28842870

2885-
2886-
else if (strCommand == NetMsgType::FILTERADD)
2887-
{
2871+
if (strCommand == NetMsgType::FILTERADD) {
28882872
std::vector<unsigned char> vData;
28892873
vRecv >> vData;
28902874

@@ -2905,19 +2889,19 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29052889
LOCK(cs_main);
29062890
Misbehaving(pfrom->GetId(), 100);
29072891
}
2892+
return true;
29082893
}
29092894

2910-
2911-
else if (strCommand == NetMsgType::FILTERCLEAR)
2912-
{
2895+
if (strCommand == NetMsgType::FILTERCLEAR) {
29132896
LOCK(pfrom->cs_filter);
29142897
if (pfrom->GetLocalServices() & NODE_BLOOM) {
29152898
pfrom->pfilter.reset(new CBloomFilter());
29162899
}
29172900
pfrom->fRelayTxes = true;
2901+
return true;
29182902
}
29192903

2920-
else if (strCommand == NetMsgType::FEEFILTER) {
2904+
if (strCommand == NetMsgType::FEEFILTER) {
29212905
CAmount newFeeFilter = 0;
29222906
vRecv >> newFeeFilter;
29232907
if (MoneyRange(newFeeFilter)) {
@@ -2927,20 +2911,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29272911
}
29282912
LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId());
29292913
}
2914+
return true;
29302915
}
29312916

2932-
else if (strCommand == NetMsgType::NOTFOUND) {
2917+
if (strCommand == NetMsgType::NOTFOUND) {
29332918
// We do not care about the NOTFOUND message, but logging an Unknown Command
29342919
// message would be undesirable as we transmit it ourselves.
2920+
return true;
29352921
}
29362922

2937-
else {
2938-
// Ignore unknown commands for extensibility
2939-
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
2940-
}
2941-
2942-
2943-
2923+
// Ignore unknown commands for extensibility
2924+
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
29442925
return true;
29452926
}
29462927

0 commit comments

Comments
 (0)