Skip to content

Commit fa6c3de

Browse files
author
MarcoFalke
committed
p2p: Clarify control flow in ProcessMessage()
1 parent 1b04b55 commit fa6c3de

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
{
@@ -1796,9 +1795,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
17961795
return true;
17971796
}
17981797

1799-
1800-
else if (pfrom->nVersion == 0)
1801-
{
1798+
if (pfrom->nVersion == 0) {
18021799
// Must have a version message before anything else
18031800
LOCK(cs_main);
18041801
Misbehaving(pfrom->GetId(), 1);
@@ -1842,18 +1839,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
18421839
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
18431840
}
18441841
pfrom->fSuccessfullyConnected = true;
1842+
return true;
18451843
}
18461844

1847-
else if (!pfrom->fSuccessfullyConnected)
1848-
{
1845+
if (!pfrom->fSuccessfullyConnected) {
18491846
// Must have a verack message before anything else
18501847
LOCK(cs_main);
18511848
Misbehaving(pfrom->GetId(), 1);
18521849
return false;
18531850
}
18541851

1855-
else if (strCommand == NetMsgType::ADDR)
1856-
{
1852+
if (strCommand == NetMsgType::ADDR) {
18571853
std::vector<CAddress> vAddr;
18581854
vRecv >> vAddr;
18591855

@@ -1900,16 +1896,16 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19001896
pfrom->fGetAddr = false;
19011897
if (pfrom->fOneShot)
19021898
pfrom->fDisconnect = true;
1899+
return true;
19031900
}
19041901

1905-
else if (strCommand == NetMsgType::SENDHEADERS)
1906-
{
1902+
if (strCommand == NetMsgType::SENDHEADERS) {
19071903
LOCK(cs_main);
19081904
State(pfrom->GetId())->fPreferHeaders = true;
1905+
return true;
19091906
}
19101907

1911-
else if (strCommand == NetMsgType::SENDCMPCT)
1912-
{
1908+
if (strCommand == NetMsgType::SENDCMPCT) {
19131909
bool fAnnounceUsingCMPCTBLOCK = false;
19141910
uint64_t nCMPCTBLOCKVersion = 0;
19151911
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
@@ -1929,11 +1925,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19291925
State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
19301926
}
19311927
}
1928+
return true;
19321929
}
19331930

1934-
1935-
else if (strCommand == NetMsgType::INV)
1936-
{
1931+
if (strCommand == NetMsgType::INV) {
19371932
std::vector<CInv> vInv;
19381933
vRecv >> vInv;
19391934
if (vInv.size() > MAX_INV_SZ)
@@ -1987,11 +1982,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19871982
}
19881983
}
19891984
}
1985+
return true;
19901986
}
19911987

1992-
1993-
else if (strCommand == NetMsgType::GETDATA)
1994-
{
1988+
if (strCommand == NetMsgType::GETDATA) {
19951989
std::vector<CInv> vInv;
19961990
vRecv >> vInv;
19971991
if (vInv.size() > MAX_INV_SZ)
@@ -2009,11 +2003,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20092003

20102004
pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
20112005
ProcessGetData(pfrom, chainparams, connman, interruptMsgProc);
2006+
return true;
20122007
}
20132008

2014-
2015-
else if (strCommand == NetMsgType::GETBLOCKS)
2016-
{
2009+
if (strCommand == NetMsgType::GETBLOCKS) {
20172010
CBlockLocator locator;
20182011
uint256 hashStop;
20192012
vRecv >> locator >> hashStop;
@@ -2078,11 +2071,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20782071
break;
20792072
}
20802073
}
2074+
return true;
20812075
}
20822076

2083-
2084-
else if (strCommand == NetMsgType::GETBLOCKTXN)
2085-
{
2077+
if (strCommand == NetMsgType::GETBLOCKTXN) {
20862078
BlockTransactionsRequest req;
20872079
vRecv >> req;
20882080

@@ -2128,11 +2120,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21282120
assert(ret);
21292121

21302122
SendBlockTransactions(block, req, pfrom, connman);
2123+
return true;
21312124
}
21322125

2133-
2134-
else if (strCommand == NetMsgType::GETHEADERS)
2135-
{
2126+
if (strCommand == NetMsgType::GETHEADERS) {
21362127
CBlockLocator locator;
21372128
uint256 hashStop;
21382129
vRecv >> locator >> hashStop;
@@ -2196,11 +2187,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21962187
// in the SendMessages logic.
21972188
nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
21982189
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
2190+
return true;
21992191
}
22002192

2201-
2202-
else if (strCommand == NetMsgType::TX)
2203-
{
2193+
if (strCommand == NetMsgType::TX) {
22042194
// Stop processing the transaction early if
22052195
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
22062196
if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
@@ -2384,10 +2374,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23842374
Misbehaving(pfrom->GetId(), nDoS);
23852375
}
23862376
}
2377+
return true;
23872378
}
23882379

2389-
2390-
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2380+
if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
23912381
{
23922382
CBlockHeaderAndShortTxIDs cmpctblock;
23932383
vRecv >> cmpctblock;
@@ -2605,10 +2595,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26052595
MarkBlockAsReceived(pblock->GetHash());
26062596
}
26072597
}
2608-
2598+
return true;
26092599
}
26102600

2611-
else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
2601+
if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
26122602
{
26132603
BlockTransactions resp;
26142604
vRecv >> resp;
@@ -2680,10 +2670,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26802670
mapBlockSource.erase(pblock->GetHash());
26812671
}
26822672
}
2673+
return true;
26832674
}
26842675

2685-
2686-
else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
2676+
if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
26872677
{
26882678
std::vector<CBlockHeader> headers;
26892679

@@ -2708,7 +2698,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27082698
return ProcessHeadersMessage(pfrom, connman, headers, chainparams, should_punish);
27092699
}
27102700

2711-
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2701+
if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
27122702
{
27132703
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
27142704
vRecv >> *pblock;
@@ -2734,11 +2724,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27342724
LOCK(cs_main);
27352725
mapBlockSource.erase(pblock->GetHash());
27362726
}
2727+
return true;
27372728
}
27382729

2739-
2740-
else if (strCommand == NetMsgType::GETADDR)
2741-
{
2730+
if (strCommand == NetMsgType::GETADDR) {
27422731
// This asymmetric behavior for inbound and outbound connections was introduced
27432732
// to prevent a fingerprinting attack: an attacker can send specific fake addresses
27442733
// to users' AddrMan and later request them by sending getaddr messages.
@@ -2762,11 +2751,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27622751
FastRandomContext insecure_rand;
27632752
for (const CAddress &addr : vAddr)
27642753
pfrom->PushAddress(addr, insecure_rand);
2754+
return true;
27652755
}
27662756

2767-
2768-
else if (strCommand == NetMsgType::MEMPOOL)
2769-
{
2757+
if (strCommand == NetMsgType::MEMPOOL) {
27702758
if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
27712759
{
27722760
LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
@@ -2783,11 +2771,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
27832771

27842772
LOCK(pfrom->cs_inventory);
27852773
pfrom->fSendMempool = true;
2774+
return true;
27862775
}
27872776

2788-
2789-
else if (strCommand == NetMsgType::PING)
2790-
{
2777+
if (strCommand == NetMsgType::PING) {
27912778
if (pfrom->nVersion > BIP0031_VERSION)
27922779
{
27932780
uint64_t nonce = 0;
@@ -2805,11 +2792,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28052792
// return very quickly.
28062793
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
28072794
}
2795+
return true;
28082796
}
28092797

2810-
2811-
else if (strCommand == NetMsgType::PONG)
2812-
{
2798+
if (strCommand == NetMsgType::PONG) {
28132799
int64_t pingUsecEnd = nTimeReceived;
28142800
uint64_t nonce = 0;
28152801
size_t nAvail = vRecv.in_avail();
@@ -2862,11 +2848,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28622848
if (bPingFinished) {
28632849
pfrom->nPingNonceSent = 0;
28642850
}
2851+
return true;
28652852
}
28662853

2867-
2868-
else if (strCommand == NetMsgType::FILTERLOAD)
2869-
{
2854+
if (strCommand == NetMsgType::FILTERLOAD) {
28702855
CBloomFilter filter;
28712856
vRecv >> filter;
28722857

@@ -2883,11 +2868,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
28832868
pfrom->pfilter->UpdateEmptyFull();
28842869
pfrom->fRelayTxes = true;
28852870
}
2871+
return true;
28862872
}
28872873

2888-
2889-
else if (strCommand == NetMsgType::FILTERADD)
2890-
{
2874+
if (strCommand == NetMsgType::FILTERADD) {
28912875
std::vector<unsigned char> vData;
28922876
vRecv >> vData;
28932877

@@ -2908,19 +2892,19 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29082892
LOCK(cs_main);
29092893
Misbehaving(pfrom->GetId(), 100);
29102894
}
2895+
return true;
29112896
}
29122897

2913-
2914-
else if (strCommand == NetMsgType::FILTERCLEAR)
2915-
{
2898+
if (strCommand == NetMsgType::FILTERCLEAR) {
29162899
LOCK(pfrom->cs_filter);
29172900
if (pfrom->GetLocalServices() & NODE_BLOOM) {
29182901
pfrom->pfilter.reset(new CBloomFilter());
29192902
}
29202903
pfrom->fRelayTxes = true;
2904+
return true;
29212905
}
29222906

2923-
else if (strCommand == NetMsgType::FEEFILTER) {
2907+
if (strCommand == NetMsgType::FEEFILTER) {
29242908
CAmount newFeeFilter = 0;
29252909
vRecv >> newFeeFilter;
29262910
if (MoneyRange(newFeeFilter)) {
@@ -2930,20 +2914,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29302914
}
29312915
LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId());
29322916
}
2917+
return true;
29332918
}
29342919

2935-
else if (strCommand == NetMsgType::NOTFOUND) {
2920+
if (strCommand == NetMsgType::NOTFOUND) {
29362921
// We do not care about the NOTFOUND message, but logging an Unknown Command
29372922
// message would be undesirable as we transmit it ourselves.
2923+
return true;
29382924
}
29392925

2940-
else {
2941-
// Ignore unknown commands for extensibility
2942-
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
2943-
}
2944-
2945-
2946-
2926+
// Ignore unknown commands for extensibility
2927+
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
29472928
return true;
29482929
}
29492930

0 commit comments

Comments
 (0)