1281 | @@ -1282,12 +1282,18 @@ void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const s
1282 | LOCK(peer->m_misbehavior_mutex);
1283 | peer->m_misbehavior_score += howmuch;
1284 | const std::string message_prefixed = message.empty() ? "" : (": " + message);
1285 | - if (peer->m_misbehavior_score >= DISCOURAGEMENT_THRESHOLD && peer->m_misbehavior_score - howmuch < DISCOURAGEMENT_THRESHOLD) {
1286 | - LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d) DISCOURAGE THRESHOLD EXCEEDED%s\n", pnode, peer->m_misbehavior_score - howmuch, peer->m_misbehavior_score, message_prefixed);
1287 | +
1288 | + const int score_before{peer->m_misbehavior_score - howmuch};
How about initializing this variable before incrementing m_misbehavior_score (so you're not adding howmuch and then subtracting it):
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index f89ecc21a8..bee7d5f241 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -1280,11 +1280,12 @@ void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const s
if (peer == nullptr) return;
LOCK(peer->m_misbehavior_mutex);
- peer->m_misbehavior_score += howmuch;
- const std::string message_prefixed = message.empty() ? "" : (": " + message);
- const int score_before{peer->m_misbehavior_score - howmuch};
+ const int score_before{peer->m_misbehavior_score};
+ peer->m_misbehavior_score += howmuch;
const int score_now{peer->m_misbehavior_score};
+
+ const std::string message_prefixed = message.empty() ? "" : (": " + message);
std::string warning{""};
if (score_now >= DISCOURAGEMENT_THRESHOLD && score_before < DISCOURAGEMENT_THRESHOLD) {
Good idea, done. Agree that it's more logical to only read howmuch only once for the purpose of modifying the score.