p2p: Clarify control flow in ProcessMessage()

This commit is contained in:
MarcoFalke 2018-08-12 07:55:01 -04:00
parent 1b04b55f2d
commit fa6c3dea42
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
1 changed files with 49 additions and 68 deletions

View File

@ -1618,8 +1618,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
return true; return true;
} }
else if (strCommand == NetMsgType::VERSION) if (strCommand == NetMsgType::VERSION) {
{
// Each connection can only send one version message // Each connection can only send one version message
if (pfrom->nVersion != 0) if (pfrom->nVersion != 0)
{ {
@ -1796,9 +1795,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
return true; return true;
} }
if (pfrom->nVersion == 0) {
else if (pfrom->nVersion == 0)
{
// Must have a version message before anything else // Must have a version message before anything else
LOCK(cs_main); LOCK(cs_main);
Misbehaving(pfrom->GetId(), 1); Misbehaving(pfrom->GetId(), 1);
@ -1842,18 +1839,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion)); connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
} }
pfrom->fSuccessfullyConnected = true; pfrom->fSuccessfullyConnected = true;
return true;
} }
else if (!pfrom->fSuccessfullyConnected) if (!pfrom->fSuccessfullyConnected) {
{
// Must have a verack message before anything else // Must have a verack message before anything else
LOCK(cs_main); LOCK(cs_main);
Misbehaving(pfrom->GetId(), 1); Misbehaving(pfrom->GetId(), 1);
return false; return false;
} }
else if (strCommand == NetMsgType::ADDR) if (strCommand == NetMsgType::ADDR) {
{
std::vector<CAddress> vAddr; std::vector<CAddress> vAddr;
vRecv >> vAddr; vRecv >> vAddr;
@ -1900,16 +1896,16 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
pfrom->fGetAddr = false; pfrom->fGetAddr = false;
if (pfrom->fOneShot) if (pfrom->fOneShot)
pfrom->fDisconnect = true; pfrom->fDisconnect = true;
return true;
} }
else if (strCommand == NetMsgType::SENDHEADERS) if (strCommand == NetMsgType::SENDHEADERS) {
{
LOCK(cs_main); LOCK(cs_main);
State(pfrom->GetId())->fPreferHeaders = true; State(pfrom->GetId())->fPreferHeaders = true;
return true;
} }
else if (strCommand == NetMsgType::SENDCMPCT) if (strCommand == NetMsgType::SENDCMPCT) {
{
bool fAnnounceUsingCMPCTBLOCK = false; bool fAnnounceUsingCMPCTBLOCK = false;
uint64_t nCMPCTBLOCKVersion = 0; uint64_t nCMPCTBLOCKVersion = 0;
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion; vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
@ -1929,11 +1925,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1); State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
} }
} }
return true;
} }
if (strCommand == NetMsgType::INV) {
else if (strCommand == NetMsgType::INV)
{
std::vector<CInv> vInv; std::vector<CInv> vInv;
vRecv >> vInv; vRecv >> vInv;
if (vInv.size() > MAX_INV_SZ) if (vInv.size() > MAX_INV_SZ)
@ -1987,11 +1982,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
} }
} }
} }
return true;
} }
if (strCommand == NetMsgType::GETDATA) {
else if (strCommand == NetMsgType::GETDATA)
{
std::vector<CInv> vInv; std::vector<CInv> vInv;
vRecv >> vInv; vRecv >> vInv;
if (vInv.size() > MAX_INV_SZ) if (vInv.size() > MAX_INV_SZ)
@ -2009,11 +2003,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end()); pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
ProcessGetData(pfrom, chainparams, connman, interruptMsgProc); ProcessGetData(pfrom, chainparams, connman, interruptMsgProc);
return true;
} }
if (strCommand == NetMsgType::GETBLOCKS) {
else if (strCommand == NetMsgType::GETBLOCKS)
{
CBlockLocator locator; CBlockLocator locator;
uint256 hashStop; uint256 hashStop;
vRecv >> locator >> hashStop; vRecv >> locator >> hashStop;
@ -2078,11 +2071,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
break; break;
} }
} }
return true;
} }
if (strCommand == NetMsgType::GETBLOCKTXN) {
else if (strCommand == NetMsgType::GETBLOCKTXN)
{
BlockTransactionsRequest req; BlockTransactionsRequest req;
vRecv >> req; vRecv >> req;
@ -2128,11 +2120,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
assert(ret); assert(ret);
SendBlockTransactions(block, req, pfrom, connman); SendBlockTransactions(block, req, pfrom, connman);
return true;
} }
if (strCommand == NetMsgType::GETHEADERS) {
else if (strCommand == NetMsgType::GETHEADERS)
{
CBlockLocator locator; CBlockLocator locator;
uint256 hashStop; uint256 hashStop;
vRecv >> locator >> hashStop; vRecv >> locator >> hashStop;
@ -2196,11 +2187,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
// in the SendMessages logic. // in the SendMessages logic.
nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip(); nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders)); connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
return true;
} }
if (strCommand == NetMsgType::TX) {
else if (strCommand == NetMsgType::TX)
{
// Stop processing the transaction early if // Stop processing the transaction early if
// We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off // 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))) if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
@ -2384,10 +2374,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
Misbehaving(pfrom->GetId(), nDoS); Misbehaving(pfrom->GetId(), nDoS);
} }
} }
return true;
} }
if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
{ {
CBlockHeaderAndShortTxIDs cmpctblock; CBlockHeaderAndShortTxIDs cmpctblock;
vRecv >> cmpctblock; vRecv >> cmpctblock;
@ -2605,10 +2595,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
MarkBlockAsReceived(pblock->GetHash()); 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; BlockTransactions resp;
vRecv >> resp; vRecv >> resp;
@ -2680,10 +2670,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
mapBlockSource.erase(pblock->GetHash()); mapBlockSource.erase(pblock->GetHash());
} }
} }
return true;
} }
if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
{ {
std::vector<CBlockHeader> headers; std::vector<CBlockHeader> headers;
@ -2708,7 +2698,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
return ProcessHeadersMessage(pfrom, connman, headers, chainparams, should_punish); 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>(); std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
vRecv >> *pblock; vRecv >> *pblock;
@ -2734,11 +2724,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
LOCK(cs_main); LOCK(cs_main);
mapBlockSource.erase(pblock->GetHash()); mapBlockSource.erase(pblock->GetHash());
} }
return true;
} }
if (strCommand == NetMsgType::GETADDR) {
else if (strCommand == NetMsgType::GETADDR)
{
// This asymmetric behavior for inbound and outbound connections was introduced // This asymmetric behavior for inbound and outbound connections was introduced
// to prevent a fingerprinting attack: an attacker can send specific fake addresses // to prevent a fingerprinting attack: an attacker can send specific fake addresses
// to users' AddrMan and later request them by sending getaddr messages. // 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
FastRandomContext insecure_rand; FastRandomContext insecure_rand;
for (const CAddress &addr : vAddr) for (const CAddress &addr : vAddr)
pfrom->PushAddress(addr, insecure_rand); pfrom->PushAddress(addr, insecure_rand);
return true;
} }
if (strCommand == NetMsgType::MEMPOOL) {
else if (strCommand == NetMsgType::MEMPOOL)
{
if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted) if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
{ {
LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId()); 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
LOCK(pfrom->cs_inventory); LOCK(pfrom->cs_inventory);
pfrom->fSendMempool = true; pfrom->fSendMempool = true;
return true;
} }
if (strCommand == NetMsgType::PING) {
else if (strCommand == NetMsgType::PING)
{
if (pfrom->nVersion > BIP0031_VERSION) if (pfrom->nVersion > BIP0031_VERSION)
{ {
uint64_t nonce = 0; uint64_t nonce = 0;
@ -2805,11 +2792,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
// return very quickly. // return very quickly.
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce)); connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
} }
return true;
} }
if (strCommand == NetMsgType::PONG) {
else if (strCommand == NetMsgType::PONG)
{
int64_t pingUsecEnd = nTimeReceived; int64_t pingUsecEnd = nTimeReceived;
uint64_t nonce = 0; uint64_t nonce = 0;
size_t nAvail = vRecv.in_avail(); size_t nAvail = vRecv.in_avail();
@ -2862,11 +2848,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
if (bPingFinished) { if (bPingFinished) {
pfrom->nPingNonceSent = 0; pfrom->nPingNonceSent = 0;
} }
return true;
} }
if (strCommand == NetMsgType::FILTERLOAD) {
else if (strCommand == NetMsgType::FILTERLOAD)
{
CBloomFilter filter; CBloomFilter filter;
vRecv >> filter; vRecv >> filter;
@ -2883,11 +2868,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
pfrom->pfilter->UpdateEmptyFull(); pfrom->pfilter->UpdateEmptyFull();
pfrom->fRelayTxes = true; pfrom->fRelayTxes = true;
} }
return true;
} }
if (strCommand == NetMsgType::FILTERADD) {
else if (strCommand == NetMsgType::FILTERADD)
{
std::vector<unsigned char> vData; std::vector<unsigned char> vData;
vRecv >> vData; vRecv >> vData;
@ -2908,19 +2892,19 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
LOCK(cs_main); LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100); Misbehaving(pfrom->GetId(), 100);
} }
return true;
} }
if (strCommand == NetMsgType::FILTERCLEAR) {
else if (strCommand == NetMsgType::FILTERCLEAR)
{
LOCK(pfrom->cs_filter); LOCK(pfrom->cs_filter);
if (pfrom->GetLocalServices() & NODE_BLOOM) { if (pfrom->GetLocalServices() & NODE_BLOOM) {
pfrom->pfilter.reset(new CBloomFilter()); pfrom->pfilter.reset(new CBloomFilter());
} }
pfrom->fRelayTxes = true; pfrom->fRelayTxes = true;
return true;
} }
else if (strCommand == NetMsgType::FEEFILTER) { if (strCommand == NetMsgType::FEEFILTER) {
CAmount newFeeFilter = 0; CAmount newFeeFilter = 0;
vRecv >> newFeeFilter; vRecv >> newFeeFilter;
if (MoneyRange(newFeeFilter)) { if (MoneyRange(newFeeFilter)) {
@ -2930,20 +2914,17 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
} }
LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId()); LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), 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 // We do not care about the NOTFOUND message, but logging an Unknown Command
// message would be undesirable as we transmit it ourselves. // message would be undesirable as we transmit it ourselves.
return true;
} }
else { // Ignore unknown commands for extensibility
// Ignore unknown commands for extensibility LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
}
return true; return true;
} }