Skip to content

Commit 2979a7a

Browse files
committed
Merge #19583: p2p: clean up Misbehaving()
a8865f8 [net processing] Tidy up Misbehaving() (John Newbery) d15b3af [net processing] Always supply debug message to Misbehaving() (John Newbery) 634144a [net processing] Fixup MaybeDiscourageAndDisconnect() style (John Newbery) Pull request description: This PR makes a few minor clean-ups to `Misbehaving()` in preparation to move it out of the cs_main lock. There are very minor logging changes but otherwise no functional changes. ACKs for top commit: troygiorshev: tACK a8865f8 jonatack: ACK a8865f8 fjahr: Code review ACK a8865f8 promag: Code review ACK a8865f8. Tree-SHA512: 98fb4f5f76399715545a1ea19290dcebfc8cb4eff72a1d3555dd3de6e184040bb8668c9651dab21db0dfd8e674e53a5977105ef76547146c9f6fa6b4b9d2ba59
2 parents a1da180 + a8865f8 commit 2979a7a

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/net_processing.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUA
157157

158158
void EraseOrphansFor(NodeId peer);
159159

160-
/** Increase a node's misbehavior score. */
161-
void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main);
162-
163160
// Internal stuff
164161
namespace {
165162
/** Number of nodes with fSyncStarted. */
@@ -1062,23 +1059,22 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
10621059
* Increment peer's misbehavior score. If the new value >= DISCOURAGEMENT_THRESHOLD, mark the node
10631060
* to be discouraged, meaning the peer might be disconnected and added to the discouragement filter.
10641061
*/
1065-
void Misbehaving(NodeId pnode, int howmuch, const std::string& message) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1062+
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
10661063
{
1067-
if (howmuch == 0)
1068-
return;
1064+
assert(howmuch > 0);
10691065

1070-
CNodeState *state = State(pnode);
1071-
if (state == nullptr)
1072-
return;
1066+
CNodeState* const state = State(pnode);
1067+
if (state == nullptr) return;
10731068

10741069
state->nMisbehavior += howmuch;
1075-
std::string message_prefixed = message.empty() ? "" : (": " + message);
1070+
const std::string message_prefixed = message.empty() ? "" : (": " + message);
10761071
if (state->nMisbehavior >= DISCOURAGEMENT_THRESHOLD && state->nMisbehavior - howmuch < DISCOURAGEMENT_THRESHOLD)
10771072
{
1078-
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d) DISCOURAGE THRESHOLD EXCEEDED%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
1073+
LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d) DISCOURAGE THRESHOLD EXCEEDED%s\n", pnode, state->nMisbehavior - howmuch, state->nMisbehavior, message_prefixed);
10791074
state->m_should_discourage = true;
1080-
} else
1081-
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d)%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
1075+
} else {
1076+
LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d)%s\n", pnode, state->nMisbehavior - howmuch, state->nMisbehavior, message_prefixed);
1077+
}
10821078
}
10831079

10841080
/**
@@ -1799,7 +1795,7 @@ inline void static SendBlockTransactions(const CBlock& block, const BlockTransac
17991795
for (size_t i = 0; i < req.indexes.size(); i++) {
18001796
if (req.indexes[i] >= block.vtx.size()) {
18011797
LOCK(cs_main);
1802-
Misbehaving(pfrom.GetId(), 100, strprintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom.GetId()));
1798+
Misbehaving(pfrom.GetId(), 100, "getblocktxn with out-of-bounds tx indices");
18031799
return;
18041800
}
18051801
resp.txn[i] = block.vtx[req.indexes[i]];
@@ -1848,7 +1844,7 @@ static void ProcessHeadersMessage(CNode& pfrom, CConnman& connman, ChainstateMan
18481844
UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash());
18491845

18501846
if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
1851-
Misbehaving(pfrom.GetId(), 20);
1847+
Misbehaving(pfrom.GetId(), 20, strprintf("%d non-connecting headers", nodestate->nUnconnectingHeaders));
18521848
}
18531849
return;
18541850
}
@@ -2307,7 +2303,7 @@ void ProcessMessage(
23072303
if (pfrom.nVersion != 0)
23082304
{
23092305
LOCK(cs_main);
2310-
Misbehaving(pfrom.GetId(), 1);
2306+
Misbehaving(pfrom.GetId(), 1, "redundant version message");
23112307
return;
23122308
}
23132309

@@ -2468,7 +2464,7 @@ void ProcessMessage(
24682464
if (pfrom.nVersion == 0) {
24692465
// Must have a version message before anything else
24702466
LOCK(cs_main);
2471-
Misbehaving(pfrom.GetId(), 1);
2467+
Misbehaving(pfrom.GetId(), 1, "non-version message before version handshake");
24722468
return;
24732469
}
24742470

@@ -2535,7 +2531,7 @@ void ProcessMessage(
25352531
if (!pfrom.fSuccessfullyConnected) {
25362532
// Must have a verack message before anything else
25372533
LOCK(cs_main);
2538-
Misbehaving(pfrom.GetId(), 1);
2534+
Misbehaving(pfrom.GetId(), 1, "non-verack message before version handshake");
25392535
return;
25402536
}
25412537

@@ -3203,7 +3199,7 @@ void ProcessMessage(
32033199
ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
32043200
if (status == READ_STATUS_INVALID) {
32053201
MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case Misbehaving does not result in a disconnect
3206-
Misbehaving(pfrom.GetId(), 100, strprintf("Peer %d sent us invalid compact block\n", pfrom.GetId()));
3202+
Misbehaving(pfrom.GetId(), 100, "invalid compact block");
32073203
return;
32083204
} else if (status == READ_STATUS_FAILED) {
32093205
// Duplicate txindexes, the block is now in-flight, so just request it
@@ -3336,7 +3332,7 @@ void ProcessMessage(
33363332
ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
33373333
if (status == READ_STATUS_INVALID) {
33383334
MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case Misbehaving does not result in a disconnect
3339-
Misbehaving(pfrom.GetId(), 100, strprintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom.GetId()));
3335+
Misbehaving(pfrom.GetId(), 100, "invalid compact block/non-matching block transactions");
33403336
return;
33413337
} else if (status == READ_STATUS_FAILED) {
33423338
// Might have collided, fall back to getdata now :(
@@ -3605,7 +3601,7 @@ void ProcessMessage(
36053601
{
36063602
// There is no excuse for sending a too-large filter
36073603
LOCK(cs_main);
3608-
Misbehaving(pfrom.GetId(), 100);
3604+
Misbehaving(pfrom.GetId(), 100, "too-large bloom filter");
36093605
}
36103606
else if (pfrom.m_tx_relay != nullptr)
36113607
{
@@ -3639,7 +3635,7 @@ void ProcessMessage(
36393635
}
36403636
if (bad) {
36413637
LOCK(cs_main);
3642-
Misbehaving(pfrom.GetId(), 100);
3638+
Misbehaving(pfrom.GetId(), 100, "bad filteradd message");
36433639
}
36443640
return;
36453641
}
@@ -3723,32 +3719,32 @@ void ProcessMessage(
37233719
*/
37243720
bool PeerLogicValidation::MaybeDiscourageAndDisconnect(CNode& pnode)
37253721
{
3726-
NodeId peer_id{pnode.GetId()};
3722+
const NodeId peer_id{pnode.GetId()};
37273723
{
37283724
LOCK(cs_main);
3729-
CNodeState &state = *State(peer_id);
3725+
CNodeState& state = *State(peer_id);
37303726

37313727
// There's nothing to do if the m_should_discourage flag isn't set
37323728
if (!state.m_should_discourage) return false;
37333729

3734-
// Reset m_should_discourage
37353730
state.m_should_discourage = false;
37363731
} // cs_main
37373732

37383733
if (pnode.HasPermission(PF_NOBAN)) {
3739-
// Peer has the NOBAN permission flag - log but don't disconnect
3734+
// We never disconnect or discourage peers for bad behavior if they have the NOBAN permission flag
37403735
LogPrintf("Warning: not punishing noban peer %d!\n", peer_id);
37413736
return false;
37423737
}
37433738

37443739
if (pnode.m_manual_connection) {
3745-
// Peer is a manual connection - log but don't disconnect
3740+
// We never disconnect or discourage manual peers for bad behavior
37463741
LogPrintf("Warning: not punishing manually connected peer %d!\n", peer_id);
37473742
return false;
37483743
}
37493744

37503745
if (pnode.addr.IsLocal()) {
3751-
// Peer is on a local address. Disconnect this peer, but don't discourage the local address
3746+
// We disconnect local peers for bad behavior but don't discourage (since that would discourage
3747+
// all peers on the same local address)
37523748
LogPrintf("Warning: disconnecting but not discouraging local peer %d!\n", peer_id);
37533749
pnode.fDisconnect = true;
37543750
return true;

0 commit comments

Comments
 (0)