diff --git a/src/net_processing.h b/src/net_processing.h --- a/src/net_processing.h +++ b/src/net_processing.h @@ -126,6 +126,6 @@ /** Get statistics from node state */ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats); /** Increase a node's misbehavior score. */ -void Misbehaving(NodeId nodeid, int howmuch, const std::string &reason = ""); +void Misbehaving(NodeId nodeid, int howmuch, const std::string &message = ""); #endif // BITCOIN_NET_PROCESSING_H diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -935,7 +935,7 @@ /** * Mark a misbehaving peer to be banned depending upon the value of `-banscore`. */ -void Misbehaving(NodeId pnode, int howmuch, const std::string &reason) +void Misbehaving(NodeId pnode, int howmuch, const std::string &message) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { if (howmuch == 0) { return; @@ -948,24 +948,25 @@ state->nMisbehavior += howmuch; int banscore = gArgs.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD); + std::string message_prefixed = message.empty() ? "" : (": " + message); if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore) { - LogPrintf( - "%s: %s peer=%d (%d -> %d) reason: %s BAN THRESHOLD EXCEEDED\n", - __func__, state->name, pnode, state->nMisbehavior - howmuch, - state->nMisbehavior, reason.c_str()); + LogPrint(BCLog::NET, + "%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED%s\n", + __func__, state->name, pnode, state->nMisbehavior - howmuch, + state->nMisbehavior, message_prefixed); state->fShouldBan = true; } else { - LogPrintf("%s: %s peer=%d (%d -> %d) reason: %s\n", __func__, - state->name, pnode, state->nMisbehavior - howmuch, - state->nMisbehavior, reason.c_str()); + LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d)%s\n", __func__, + state->name, pnode, state->nMisbehavior - howmuch, + state->nMisbehavior, message_prefixed); } } // overloaded variant of above to operate on CNode*s -static void Misbehaving(CNode *node, int howmuch, const std::string &reason) +static void Misbehaving(CNode *node, int howmuch, const std::string &message) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { - Misbehaving(node->GetId(), howmuch, reason); + Misbehaving(node->GetId(), howmuch, message); } ////////////////////////////////////////////////////////////////////////////// @@ -1556,10 +1557,10 @@ for (size_t i = 0; i < req.indices.size(); i++) { if (req.indices[i] >= block.vtx.size()) { LOCK(cs_main); - Misbehaving(pfrom, 100, "out-of-bound-tx-index"); - LogPrintf( - "Peer %d sent us a getblocktxn with out-of-bounds tx indices\n", - pfrom->GetId()); + Misbehaving(pfrom->GetId(), 100, + strprintf("Peer %d sent us a getblocktxn with " + "out-of-bounds tx indices", + pfrom->GetId())); return; } resp.txn[i] = block.vtx[req.indices[i]]; @@ -1629,8 +1630,9 @@ for (const CBlockHeader &header : headers) { if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) { - Misbehaving(pfrom, 20, "disconnected-header"); - return error("non-continuous headers sequence"); + Misbehaving(pfrom->GetId(), 20, + "non-continuous headers sequence"); + return false; } hashLastBlock = header.GetHash(); } @@ -1650,7 +1652,12 @@ if (state.IsInvalid(nDoS)) { LOCK(cs_main); if (nDoS > 0) { - Misbehaving(pfrom, nDoS, state.GetRejectReason()); + Misbehaving(pfrom->GetId(), nDoS, + strprintf("invalid header received (%s)", + state.GetRejectReason())); + } else { + LogPrint(BCLog::NET, "peer=%d: invalid header received (%s)\n", + pfrom->GetId(), state.GetRejectReason()); } if (punish_duplicate_invalid && LookupBlockIndex(first_invalid_header.GetHash())) { @@ -1687,7 +1694,7 @@ // not just the duplicate-invalid case. pfrom->fDisconnect = true; } - return error("invalid header received"); + return false; } } @@ -2168,8 +2175,9 @@ } if (vAddr.size() > 1000) { LOCK(cs_main); - Misbehaving(pfrom, 20, "oversized-addr"); - return error("message addr size() = %u", vAddr.size()); + Misbehaving(pfrom->GetId(), 20, + strprintf("message addr size() = %u", vAddr.size())); + return false; } // Store the new addresses @@ -2244,8 +2252,9 @@ vRecv >> vInv; if (vInv.size() > MAX_INV_SZ) { LOCK(cs_main); - Misbehaving(pfrom, 20, "oversized-inv"); - return error("message inv size() = %u", vInv.size()); + Misbehaving(pfrom->GetId(), 20, + strprintf("message inv size() = %u", vInv.size())); + return false; } bool fBlocksOnly = !fRelayTxes; @@ -2308,8 +2317,9 @@ vRecv >> vInv; if (vInv.size() > MAX_INV_SZ) { LOCK(cs_main); - Misbehaving(pfrom, 20, "too-many-inv"); - return error("message getdata size() = %u", vInv.size()); + Misbehaving(pfrom->GetId(), 20, + strprintf("message getdata size() = %u", vInv.size())); + return false; } LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", @@ -2777,14 +2787,17 @@ int nDoS; if (state.IsInvalid(nDoS)) { if (nDoS > 0) { - LogPrintf("Peer %d sent us invalid header via cmpctblock\n", - pfrom->GetId()); LOCK(cs_main); - Misbehaving(pfrom, nDoS, state.GetRejectReason()); + Misbehaving(pfrom->GetId(), nDoS, + strprintf("Peer %d sent us invalid header via " + "cmpctblock (%s)\n", + pfrom->GetId(), + state.GetRejectReason())); } else { - LogPrint(BCLog::NET, - "Peer %d sent us invalid header via cmpctblock\n", - pfrom->GetId()); + LogPrint( + BCLog::NET, + "Peer %d sent us invalid header via cmpctblock (%s)\n", + pfrom->GetId(), state.GetRejectReason()); } return true; } @@ -2889,9 +2902,10 @@ if (status == READ_STATUS_INVALID) { // Reset in-flight state in case of whitelist MarkBlockAsReceived(pindex->GetBlockHash()); - Misbehaving(pfrom, 100, "invalid-cmpctblk"); - LogPrintf("Peer %d sent us invalid compact block\n", - pfrom->GetId()); + Misbehaving( + pfrom->GetId(), 100, + strprintf("Peer %d sent us invalid compact block\n", + pfrom->GetId())); return true; } else if (status == READ_STATUS_FAILED) { // Duplicate txindices, the block is now in-flight, so @@ -3045,10 +3059,10 @@ if (status == READ_STATUS_INVALID) { // Reset in-flight state in case of whitelist. MarkBlockAsReceived(resp.blockhash); - Misbehaving(pfrom, 100, "invalid-cmpctblk-txns"); - LogPrintf("Peer %d sent us invalid compact block/non-matching " - "block transactions\n", - pfrom->GetId()); + Misbehaving(pfrom->GetId(), 100, + strprintf("Peer %d sent us invalid compact " + "block/non-matching block transactions\n", + pfrom->GetId())); return true; } else if (status == READ_STATUS_FAILED) { // Might have collided, fall back to getdata now :( @@ -3116,8 +3130,9 @@ unsigned int nCount = ReadCompactSize(vRecv); if (nCount > MAX_HEADERS_RESULTS) { LOCK(cs_main); - Misbehaving(pfrom, 20, "too-many-headers"); - return error("headers message size = %u", nCount); + Misbehaving(pfrom->GetId(), 20, + strprintf("headers message size = %u", nCount)); + return false; } headers.resize(nCount); for (unsigned int n = 0; n < nCount; n++) {