Merge bitcoin/bitcoin#22495: p2p: refactor: tidy up `PeerManagerImpl::Misbehaving(...)`

8858e88c84 p2p: refactor: tidy up `PeerManagerImpl::Misbehaving(...)` (Sebastian Falbesoner)

Pull request description:

  This simple refactoring PR has the goal to improve the readability of the `Misbehaving` method by

  - introducing constant variables `score_before` and  `score_now` (to avoid repeatedly calculating the former)
  - deduplicating calls to LogPrint(), eliminates else-branch

ACKs for top commit:
  jnewbery:
    utACK 8858e88c84
  rajarshimaitra:
    tACK 8858e88c84

Tree-SHA512: 1d4dd5ac1d16ee9595edf4fa46e4960915a203641d74e6c33cffaba62ea71328834309a4451256fb45daf759f0cf6f4f199c46815afff6c89c0746e2ad4d4092
pull/22006/head
MarcoFalke 3 years ago
commit f372623807
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

@ -1280,14 +1280,20 @@ void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const s
if (peer == nullptr) return;
LOCK(peer->m_misbehavior_mutex);
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);
if (peer->m_misbehavior_score >= DISCOURAGEMENT_THRESHOLD && peer->m_misbehavior_score - howmuch < DISCOURAGEMENT_THRESHOLD) {
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);
std::string warning;
if (score_now >= DISCOURAGEMENT_THRESHOLD && score_before < DISCOURAGEMENT_THRESHOLD) {
warning = " DISCOURAGE THRESHOLD EXCEEDED";
peer->m_should_discourage = true;
} else {
LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d)%s\n", pnode, peer->m_misbehavior_score - howmuch, peer->m_misbehavior_score, message_prefixed);
}
LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d)%s%s\n",
pnode, score_before, score_now, warning, message_prefixed);
}
bool PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,

Loading…
Cancel
Save