Merge #13946: p2p: Clarify control flow in ProcessMessage
fa6c3dea420b6c50c164ccc34f4e9e8a7d9a8022 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
This commit is contained in:
parent
dbbc51121c
commit
e5e3572e9d
@ -1791,8 +1791,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
}
|
||||
}
|
||||
|
||||
else if (strCommand == NetMsgType::VERSION)
|
||||
{
|
||||
if (strCommand == NetMsgType::VERSION) {
|
||||
// Each connection can only send one version message
|
||||
if (pfrom->nVersion != 0)
|
||||
{
|
||||
@ -1962,9 +1961,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (pfrom->nVersion == 0)
|
||||
{
|
||||
if (pfrom->nVersion == 0) {
|
||||
// Must have a version message before anything else
|
||||
LOCK(cs_main);
|
||||
Misbehaving(pfrom->GetId(), 1);
|
||||
@ -2029,18 +2026,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
}
|
||||
|
||||
pfrom->fSuccessfullyConnected = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
else if (!pfrom->fSuccessfullyConnected)
|
||||
{
|
||||
if (!pfrom->fSuccessfullyConnected) {
|
||||
// Must have a verack message before anything else
|
||||
LOCK(cs_main);
|
||||
Misbehaving(pfrom->GetId(), 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
else if (strCommand == NetMsgType::ADDR)
|
||||
{
|
||||
if (strCommand == NetMsgType::ADDR) {
|
||||
std::vector<CAddress> vAddr;
|
||||
vRecv >> vAddr;
|
||||
|
||||
@ -2086,17 +2082,16 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
pfrom->fGetAddr = false;
|
||||
if (pfrom->fOneShot)
|
||||
pfrom->fDisconnect = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
else if (strCommand == NetMsgType::SENDHEADERS)
|
||||
{
|
||||
if (strCommand == NetMsgType::SENDHEADERS) {
|
||||
LOCK(cs_main);
|
||||
State(pfrom->GetId())->fPreferHeaders = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::SENDCMPCT)
|
||||
{
|
||||
if (strCommand == NetMsgType::SENDCMPCT) {
|
||||
bool fAnnounceUsingCMPCTBLOCK = false;
|
||||
uint64_t nCMPCTBLOCKVersion = 1;
|
||||
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
|
||||
@ -2106,6 +2101,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
|
||||
State(pfrom->GetId())->fSupportsDesiredCmpctVersion = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -2123,9 +2119,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
pfrom->fSendRecSigs = b;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::INV)
|
||||
{
|
||||
if (strCommand == NetMsgType::INV) {
|
||||
std::vector<CInv> vInv;
|
||||
vRecv >> vInv;
|
||||
if (vInv.size() > MAX_INV_SZ)
|
||||
@ -2219,11 +2213,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
// Track requests for our stuff
|
||||
GetMainSignals().Inventory(inv.hash);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::GETDATA)
|
||||
{
|
||||
if (strCommand == NetMsgType::GETDATA) {
|
||||
std::vector<CInv> vInv;
|
||||
vRecv >> vInv;
|
||||
if (vInv.size() > MAX_INV_SZ)
|
||||
@ -2241,11 +2234,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
|
||||
pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
|
||||
ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::GETBLOCKS)
|
||||
{
|
||||
if (strCommand == NetMsgType::GETBLOCKS) {
|
||||
CBlockLocator locator;
|
||||
uint256 hashStop;
|
||||
vRecv >> locator >> hashStop;
|
||||
@ -2302,11 +2294,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::GETBLOCKTXN)
|
||||
{
|
||||
if (strCommand == NetMsgType::GETBLOCKTXN) {
|
||||
BlockTransactionsRequest req;
|
||||
vRecv >> req;
|
||||
|
||||
@ -2352,11 +2343,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
assert(ret);
|
||||
|
||||
SendBlockTransactions(block, req, pfrom, connman);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::GETHEADERS)
|
||||
{
|
||||
if (strCommand == NetMsgType::GETHEADERS) {
|
||||
CBlockLocator locator;
|
||||
uint256 hashStop;
|
||||
vRecv >> locator >> hashStop;
|
||||
@ -2414,11 +2404,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
// in the SendMessages logic.
|
||||
nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
|
||||
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::TX || strCommand == NetMsgType::DSTX || strCommand == NetMsgType::LEGACYTXLOCKREQUEST)
|
||||
{
|
||||
if (strCommand == NetMsgType::TX || strCommand == NetMsgType::DSTX || strCommand == NetMsgType::LEGACYTXLOCKREQUEST) {
|
||||
// Stop processing the transaction early if
|
||||
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
|
||||
if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
|
||||
@ -2606,9 +2595,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
Misbehaving(pfrom->GetId(), nDoS);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
|
||||
if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
|
||||
{
|
||||
CBlockHeaderAndShortTxIDs cmpctblock;
|
||||
vRecv >> cmpctblock;
|
||||
@ -2820,10 +2810,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
MarkBlockAsReceived(pblock->GetHash());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
|
||||
if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
|
||||
{
|
||||
BlockTransactions resp;
|
||||
vRecv >> resp;
|
||||
@ -2896,10 +2886,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
mapBlockSource.erase(pblock->GetHash());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
|
||||
if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
|
||||
{
|
||||
std::vector<CBlockHeader> headers;
|
||||
|
||||
@ -2924,7 +2914,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
return ProcessHeadersMessage(pfrom, connman, headers, chainparams, should_punish);
|
||||
}
|
||||
|
||||
else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
|
||||
if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
|
||||
{
|
||||
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
|
||||
vRecv >> *pblock;
|
||||
@ -2950,11 +2940,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
LOCK(cs_main);
|
||||
mapBlockSource.erase(pblock->GetHash());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::GETADDR)
|
||||
{
|
||||
if (strCommand == NetMsgType::GETADDR) {
|
||||
// This asymmetric behavior for inbound and outbound connections was introduced
|
||||
// to prevent a fingerprinting attack: an attacker can send specific fake addresses
|
||||
// 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
|
||||
FastRandomContext insecure_rand;
|
||||
for (const CAddress &addr : vAddr)
|
||||
pfrom->PushAddress(addr, insecure_rand);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::MEMPOOL)
|
||||
{
|
||||
if (strCommand == NetMsgType::MEMPOOL) {
|
||||
if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
|
||||
{
|
||||
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
|
||||
|
||||
LOCK(pfrom->cs_inventory);
|
||||
pfrom->fSendMempool = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::PING)
|
||||
{
|
||||
if (strCommand == NetMsgType::PING) {
|
||||
if (pfrom->nVersion > BIP0031_VERSION)
|
||||
{
|
||||
uint64_t nonce = 0;
|
||||
@ -3021,11 +3008,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
// return very quickly.
|
||||
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::PONG)
|
||||
{
|
||||
if (strCommand == NetMsgType::PONG) {
|
||||
int64_t pingUsecEnd = nTimeReceived;
|
||||
uint64_t nonce = 0;
|
||||
size_t nAvail = vRecv.in_avail();
|
||||
@ -3078,11 +3064,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
if (bPingFinished) {
|
||||
pfrom->nPingNonceSent = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::FILTERLOAD)
|
||||
{
|
||||
if (strCommand == NetMsgType::FILTERLOAD) {
|
||||
CBloomFilter filter;
|
||||
vRecv >> filter;
|
||||
|
||||
@ -3100,11 +3085,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
pfrom->pfilter->UpdateEmptyFull();
|
||||
pfrom->fRelayTxes = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::FILTERADD)
|
||||
{
|
||||
if (strCommand == NetMsgType::FILTERADD) {
|
||||
std::vector<unsigned char> vData;
|
||||
vRecv >> vData;
|
||||
|
||||
@ -3125,21 +3109,21 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
LOCK(cs_main);
|
||||
Misbehaving(pfrom->GetId(), 100);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::FILTERCLEAR)
|
||||
{
|
||||
if (strCommand == NetMsgType::FILTERCLEAR) {
|
||||
LOCK(pfrom->cs_filter);
|
||||
if (pfrom->GetLocalServices() & NODE_BLOOM) {
|
||||
delete pfrom->pfilter;
|
||||
pfrom->pfilter = new CBloomFilter();
|
||||
}
|
||||
pfrom->fRelayTxes = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::GETMNLISTDIFF) {
|
||||
if (strCommand == NetMsgType::GETMNLISTDIFF) {
|
||||
CGetSimplifiedMNListDiff cmd;
|
||||
vRecv >> cmd;
|
||||
|
||||
@ -3153,57 +3137,57 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
|
||||
LogPrint(BCLog::NET, "getmnlistdiff failed for baseBlockHash=%s, blockHash=%s. error=%s\n", cmd.baseBlockHash.ToString(), cmd.blockHash.ToString(), strError);
|
||||
Misbehaving(pfrom->GetId(), 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::MNLISTDIFF) {
|
||||
if (strCommand == NetMsgType::MNLISTDIFF) {
|
||||
// we have never requested this
|
||||
LOCK(cs_main);
|
||||
Misbehaving(pfrom->GetId(), 100);
|
||||
LogPrint(BCLog::NET, "received not-requested mnlistdiff. peer=%d\n", pfrom->GetId());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
else if (strCommand == NetMsgType::NOTFOUND) {
|
||||
if (strCommand == NetMsgType::NOTFOUND) {
|
||||
// We do not care about the NOTFOUND message, but logging an Unknown Command
|
||||
// message would be undesirable as we transmit it ourselves.
|
||||
return true;
|
||||
}
|
||||
|
||||
else {
|
||||
bool found = false;
|
||||
const std::vector<std::string> &allMessages = getAllNetMessageTypes();
|
||||
for (const std::string msg : allMessages) {
|
||||
if(msg == strCommand) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
bool found = false;
|
||||
const std::vector<std::string> &allMessages = getAllNetMessageTypes();
|
||||
for (const std::string msg : allMessages) {
|
||||
if(msg == strCommand) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
//probably one the extensions
|
||||
if (found)
|
||||
{
|
||||
//probably one the extensions
|
||||
#ifdef ENABLE_WALLET
|
||||
privateSendClient.ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
privateSendClient.ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
#endif // ENABLE_WALLET
|
||||
privateSendServer.ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
sporkManager.ProcessSpork(pfrom, strCommand, vRecv, *connman);
|
||||
masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
|
||||
governance.ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
CMNAuth::ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumBlockProcessor->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumDKGSessionManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumSigSharesManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumSigningManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::chainLocksHandler->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumInstantSendManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ignore unknown commands for extensibility
|
||||
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
|
||||
}
|
||||
privateSendServer.ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
sporkManager.ProcessSpork(pfrom, strCommand, vRecv, *connman);
|
||||
masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
|
||||
governance.ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
CMNAuth::ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumBlockProcessor->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumDKGSessionManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumSigSharesManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumSigningManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::chainLocksHandler->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
llmq::quorumInstantSendManager->ProcessMessage(pfrom, strCommand, vRecv, *connman);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Ignore unknown commands for extensibility
|
||||
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user