@@ -157,9 +157,6 @@ std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUA
157
157
158
158
void EraseOrphansFor (NodeId peer);
159
159
160
- /* * Increase a node's misbehavior score. */
161
- void Misbehaving (NodeId nodeid, int howmuch, const std::string& message=" " ) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
162
-
163
160
// Internal stuff
164
161
namespace {
165
162
/* * Number of nodes with fSyncStarted. */
@@ -1062,23 +1059,22 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
1062
1059
* Increment peer's misbehavior score. If the new value >= DISCOURAGEMENT_THRESHOLD, mark the node
1063
1060
* to be discouraged, meaning the peer might be disconnected and added to the discouragement filter.
1064
1061
*/
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)
1066
1063
{
1067
- if (howmuch == 0 )
1068
- return ;
1064
+ assert (howmuch > 0 );
1069
1065
1070
- CNodeState *state = State (pnode);
1071
- if (state == nullptr )
1072
- return ;
1066
+ CNodeState* const state = State (pnode);
1067
+ if (state == nullptr ) return ;
1073
1068
1074
1069
state->nMisbehavior += howmuch;
1075
- std::string message_prefixed = message.empty () ? " " : (" : " + message);
1070
+ const std::string message_prefixed = message.empty () ? " " : (" : " + message);
1076
1071
if (state->nMisbehavior >= DISCOURAGEMENT_THRESHOLD && state->nMisbehavior - howmuch < DISCOURAGEMENT_THRESHOLD)
1077
1072
{
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);
1079
1074
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
+ }
1082
1078
}
1083
1079
1084
1080
/* *
@@ -1799,7 +1795,7 @@ inline void static SendBlockTransactions(const CBlock& block, const BlockTransac
1799
1795
for (size_t i = 0 ; i < req.indexes .size (); i++) {
1800
1796
if (req.indexes [i] >= block.vtx .size ()) {
1801
1797
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" );
1803
1799
return ;
1804
1800
}
1805
1801
resp.txn [i] = block.vtx [req.indexes [i]];
@@ -1848,7 +1844,7 @@ static void ProcessHeadersMessage(CNode& pfrom, CConnman& connman, ChainstateMan
1848
1844
UpdateBlockAvailability (pfrom.GetId (), headers.back ().GetHash ());
1849
1845
1850
1846
if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0 ) {
1851
- Misbehaving (pfrom.GetId (), 20 );
1847
+ Misbehaving (pfrom.GetId (), 20 , strprintf ( " %d non-connecting headers " , nodestate-> nUnconnectingHeaders ) );
1852
1848
}
1853
1849
return ;
1854
1850
}
@@ -2307,7 +2303,7 @@ void ProcessMessage(
2307
2303
if (pfrom.nVersion != 0 )
2308
2304
{
2309
2305
LOCK (cs_main);
2310
- Misbehaving (pfrom.GetId (), 1 );
2306
+ Misbehaving (pfrom.GetId (), 1 , " redundant version message " );
2311
2307
return ;
2312
2308
}
2313
2309
@@ -2468,7 +2464,7 @@ void ProcessMessage(
2468
2464
if (pfrom.nVersion == 0 ) {
2469
2465
// Must have a version message before anything else
2470
2466
LOCK (cs_main);
2471
- Misbehaving (pfrom.GetId (), 1 );
2467
+ Misbehaving (pfrom.GetId (), 1 , " non-version message before version handshake " );
2472
2468
return ;
2473
2469
}
2474
2470
@@ -2535,7 +2531,7 @@ void ProcessMessage(
2535
2531
if (!pfrom.fSuccessfullyConnected ) {
2536
2532
// Must have a verack message before anything else
2537
2533
LOCK (cs_main);
2538
- Misbehaving (pfrom.GetId (), 1 );
2534
+ Misbehaving (pfrom.GetId (), 1 , " non-verack message before version handshake " );
2539
2535
return ;
2540
2536
}
2541
2537
@@ -3203,7 +3199,7 @@ void ProcessMessage(
3203
3199
ReadStatus status = partialBlock.InitData (cmpctblock, vExtraTxnForCompact);
3204
3200
if (status == READ_STATUS_INVALID) {
3205
3201
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" );
3207
3203
return ;
3208
3204
} else if (status == READ_STATUS_FAILED) {
3209
3205
// Duplicate txindexes, the block is now in-flight, so just request it
@@ -3336,7 +3332,7 @@ void ProcessMessage(
3336
3332
ReadStatus status = partialBlock.FillBlock (*pblock, resp.txn );
3337
3333
if (status == READ_STATUS_INVALID) {
3338
3334
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" );
3340
3336
return ;
3341
3337
} else if (status == READ_STATUS_FAILED) {
3342
3338
// Might have collided, fall back to getdata now :(
@@ -3605,7 +3601,7 @@ void ProcessMessage(
3605
3601
{
3606
3602
// There is no excuse for sending a too-large filter
3607
3603
LOCK (cs_main);
3608
- Misbehaving (pfrom.GetId (), 100 );
3604
+ Misbehaving (pfrom.GetId (), 100 , " too-large bloom filter " );
3609
3605
}
3610
3606
else if (pfrom.m_tx_relay != nullptr )
3611
3607
{
@@ -3639,7 +3635,7 @@ void ProcessMessage(
3639
3635
}
3640
3636
if (bad) {
3641
3637
LOCK (cs_main);
3642
- Misbehaving (pfrom.GetId (), 100 );
3638
+ Misbehaving (pfrom.GetId (), 100 , " bad filteradd message " );
3643
3639
}
3644
3640
return ;
3645
3641
}
@@ -3723,32 +3719,32 @@ void ProcessMessage(
3723
3719
*/
3724
3720
bool PeerLogicValidation::MaybeDiscourageAndDisconnect (CNode& pnode)
3725
3721
{
3726
- NodeId peer_id{pnode.GetId ()};
3722
+ const NodeId peer_id{pnode.GetId ()};
3727
3723
{
3728
3724
LOCK (cs_main);
3729
- CNodeState & state = *State (peer_id);
3725
+ CNodeState& state = *State (peer_id);
3730
3726
3731
3727
// There's nothing to do if the m_should_discourage flag isn't set
3732
3728
if (!state.m_should_discourage ) return false ;
3733
3729
3734
- // Reset m_should_discourage
3735
3730
state.m_should_discourage = false ;
3736
3731
} // cs_main
3737
3732
3738
3733
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
3740
3735
LogPrintf (" Warning: not punishing noban peer %d!\n " , peer_id);
3741
3736
return false ;
3742
3737
}
3743
3738
3744
3739
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
3746
3741
LogPrintf (" Warning: not punishing manually connected peer %d!\n " , peer_id);
3747
3742
return false ;
3748
3743
}
3749
3744
3750
3745
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)
3752
3748
LogPrintf (" Warning: disconnecting but not discouraging local peer %d!\n " , peer_id);
3753
3749
pnode.fDisconnect = true ;
3754
3750
return true ;
0 commit comments