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

pull theStack wants to merge 1 commits into bitcoin:master from theStack:202107-net-tidy_up_misbehaving changing 1 files +10 −4
  1. theStack commented at 12:13 PM on July 19, 2021: member

    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
  2. fanquake added the label P2P on Jul 19, 2021
  3. lsilva01 approved
  4. rajarshimaitra approved
  5. rajarshimaitra commented at 6:57 AM on July 24, 2021: contributor

    tACK https://github.com/bitcoin/bitcoin/pull/22495/commits/9459fe5ae7690bf26c410567a16c7139812924ea

    Agreed that the code looks cleaner than before.

    Verified p2p_invalid_messages and p2p_filter test producing expected misbehavior.

  6. in src/net_processing.cpp:1286 in 9459fe5ae7 outdated
    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};
    


    jnewbery commented at 9:39 AM on July 26, 2021:

    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) {
    

    theStack commented at 1:53 PM on July 26, 2021:

    Good idea, done. Agree that it's more logical to only read howmuch only once for the purpose of modifying the score.

  7. in src/net_processing.cpp:1288 in 9459fe5ae7 outdated
    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};
    1289 | +    const int score_now{peer->m_misbehavior_score};
    1290 | +    std::string warning{""};
    


    jnewbery commented at 9:39 AM on July 26, 2021:

    No need to explicitly initialize this to an empty string. std::string's default initializer creates and empty string:

        std::string warning;
    

    theStack commented at 1:53 PM on July 26, 2021:

    Thanks, done.

  8. p2p: refactor: tidy up `PeerManagerImpl::Misbehaving(...)`
    - introduce constant variables `score_before` and
      `score_after` in order to improve readability
    - deduplicate calls to LogPrint(), eliminates else-branch
    8858e88c84
  9. theStack force-pushed on Jul 26, 2021
  10. theStack commented at 1:55 PM on July 26, 2021: member

    Force-pushed with the suggestions by jnewbery (https://github.com/bitcoin/bitcoin/pull/22495#discussion_r676447854, #22495 (review)). Thanks to all reviewers so far!

  11. jnewbery commented at 4:01 PM on July 26, 2021: member

    utACK 8858e88c840197cdcabea07dd1380ef2aa4ece02

  12. MarcoFalke merged this on Jul 27, 2021
  13. MarcoFalke closed this on Jul 27, 2021

  14. sidhujag referenced this in commit 00fd219905 on Jul 28, 2021
  15. theStack deleted the branch on Jul 31, 2021
  16. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 21:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me