test: add unit test for block-relay-only eviction #23614

pull mzumsande wants to merge 1 commits into bitcoin:master from mzumsande:202111_test_blocksonly_eviction changing 1 files +66 −4
  1. mzumsande commented at 5:24 PM on November 27, 2021: member

    Adds a unit test for block-relay-only eviction logic added in #19858, which was not covered by any tests before. The added test is very similar to the existing stale_tip_peer_management unit test, which tests the analogous logic for regular outbound peers.

  2. DrahtBot added the label Tests on Nov 27, 2021
  3. DrahtBot commented at 6:36 PM on November 27, 2021: member

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #23604 (Use Sock in CNode by vasild)
    • #23352 (test: Extend stale_tip_peer_management test by MarcoFalke)
    • #21878 (Make all networking code mockable by vasild)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.<!--2502f1a698b3751726fa55edcda76cd3-->

    Coverage

    Coverage Change (pull 23614, 67fbae357af4d6553d8574824e2ee144e316ad98) Reference (master, d20d6ac545159fb9)
    Lines +0.0362 % 84.0190 %
    Functions +0.1206 % 76.2641 %
    Branches -0.0053 % 52.5739 %

    <sup>Updated at: 2021-12-06T11:23:42.381149.</sup>

  4. in src/test/denialofservice_tests.cpp:256 in 4c449a55c2 outdated
     251 | +        peerLogic->FinalizeNode(*node);
     252 | +    }
     253 | +    connman->ClearTestNodes();
     254 | +}
     255 | +
     256 | +
    


    shaavan commented at 6:11 AM on November 28, 2021:

    nit: You may remove extra space.


    mzumsande commented at 12:59 PM on November 29, 2021:

    done

  5. in src/test/denialofservice_tests.cpp:200 in 4c449a55c2 outdated
     195 | +    const CChainParams& chainparams = Params();
     196 | +    auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman);
     197 | +    auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
     198 | +                                       *m_node.chainman, *m_node.mempool, false);
     199 | +
     200 | +    constexpr int max_outbound_blocksonly = MAX_BLOCK_RELAY_ONLY_CONNECTIONS;
    


    shaavan commented at 6:16 AM on November 28, 2021:

    Suggestion: How about renaming it to:

        constexpr int max_outbound_blocks_only = MAX_BLOCK_RELAY_ONLY_CONNECTIONS;
    

    Since we are following the snake case naming system here, and blocks and only are two different words.


    MarcoFalke commented at 10:37 AM on November 29, 2021:
        constexpr int max_outbound_blockrelay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
    

    Might be better to call this blockrelay to avoid confusion with -blocksonly.


    mzumsande commented at 1:00 PM on November 29, 2021:

    changed to max_outbound_block_relay, as suggested below.


    mzumsande commented at 1:01 PM on November 29, 2021:

    changed to max_outbound_block_relay

  6. shaavan commented at 6:19 AM on November 28, 2021: contributor

    Concept ACK

    The added test looks good and seems logically in line with the original block-relay-only eviction logic. However, I shall take another look before ACKing the PR. In the meantime, I would like to point out two nit suggestions that can be improved upon.

  7. in src/test/denialofservice_tests.cpp:201 in 4c449a55c2 outdated
     196 | +    auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman);
     197 | +    auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
     198 | +                                       *m_node.chainman, *m_node.mempool, false);
     199 | +
     200 | +    constexpr int max_outbound_blocksonly = MAX_BLOCK_RELAY_ONLY_CONNECTIONS;
     201 | +    constexpr int64_t MINIMUM_CONNECT_TIME = 30;
    


    glozow commented at 11:48 AM on November 29, 2021:

    nit

        constexpr int64_t MINIMUM_CONNECT_TIME{30};
    

    mzumsande commented at 1:01 PM on November 29, 2021:

    done

  8. glozow commented at 11:55 AM on November 29, 2021: member

    ACK 4c449a55c29b4b382660852b20800d0ae2bc9e22, very clean :ok_hand:

  9. mzumsande force-pushed on Nov 29, 2021
  10. mzumsande commented at 1:02 PM on November 29, 2021: member

    Thanks for the reviews - I addressed all comments!

  11. glozow commented at 1:03 PM on November 29, 2021: member

    reACK e0a214f48ec2b8f45d09f85bf8429d805e6ae411

    changes:

    • rename s/max_outbound_blocksonly/max_outbound_block_relay
    • braced initialization for constants
    • delete newline
  12. test: Add test for block relay only eviction 4740fe8212
  13. mzumsande force-pushed on Nov 29, 2021
  14. mzumsande commented at 3:22 PM on November 29, 2021: member

    e0a214f -> 4740fe82 removed unnecessary call to UpdateLastBlockAnnounceTime (setting nLastBlockTime is sufficient)

  15. in src/test/denialofservice_tests.cpp:197 in 4740fe8212
     189 | @@ -190,6 +190,68 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
     190 |      connman->ClearTestNodes();
     191 |  }
     192 |  
     193 | +BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
     194 | +{
     195 | +    const CChainParams& chainparams = Params();
     196 | +    auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman);
     197 | +    auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
    


    rajarshimaitra commented at 10:33 AM on November 30, 2021:

    This has been used before too, but can we name it peerman instead, like connman? Sounds more clear.


    mzumsande commented at 11:17 AM on December 2, 2021:

    Yes, will do if I push again, though this should be probably done everywhere in this test per scripted-diff.

  16. rajarshimaitra commented at 10:40 AM on November 30, 2021: contributor
  17. shaavan approved
  18. shaavan commented at 1:11 PM on November 30, 2021: contributor

    ACK 4740fe8212da21e86664355b4c6d0d7d838e4382. Great work @ mzumsande!

  19. in src/test/denialofservice_tests.cpp:217 in 4740fe8212
     212 | +        AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
     213 | +    }
     214 | +    peerLogic->CheckForStaleTipAndEvictPeers();
     215 | +
     216 | +    for (int i = 0; i < max_outbound_block_relay; ++i) {
     217 | +        BOOST_CHECK(vNodes[i]->fDisconnect == false);
    


    LarryRuane commented at 6:13 PM on December 1, 2021:

    If you have a chance to re-touch

            BOOST_CHECK(!vNodes[i]->fDisconnect);
    

    (and similarly below for both true and false.) It's okay as-is, but it's more standard to not compare against (literal) true and false. You can write boolean_expression instead of boolean_expression == true and !boolean_expression instead of boolean_expression == false.

  20. glozow commented at 10:53 AM on December 2, 2021: member

    reACK 4740fe8212da21e86664355b4c6d0d7d838e4382

  21. LarryRuane commented at 11:09 PM on December 3, 2021: contributor

    ACK 4740fe8212da21e86664355b4c6d0d7d838e4382

  22. MarcoFalke merged this on Dec 6, 2021
  23. MarcoFalke closed this on Dec 6, 2021

  24. in src/test/denialofservice_tests.cpp:208 in 4740fe8212
     203 | +    options.nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
     204 | +    options.m_max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
     205 | +    options.m_max_outbound_block_relay = max_outbound_block_relay;
     206 | +
     207 | +    connman->Init(options);
     208 | +    std::vector<CNode*> vNodes;
    


    MarcoFalke commented at 12:33 PM on December 6, 2021:

    in new code you can just use nodes;

  25. in src/test/denialofservice_tests.cpp:216 in 4740fe8212
     211 | +    for (int i = 0; i < max_outbound_block_relay; ++i) {
     212 | +        AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
     213 | +    }
     214 | +    peerLogic->CheckForStaleTipAndEvictPeers();
     215 | +
     216 | +    for (int i = 0; i < max_outbound_block_relay; ++i) {
    


    jnewbery commented at 10:32 AM on December 7, 2021:

    Alternatively, use range-based for loops throughout:

    index ef18ed1385..c74864bd7f 100644
    --- a/src/test/denialofservice_tests.cpp
    +++ b/src/test/denialofservice_tests.cpp
    @@ -207,14 +207,17 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
         connman->Init(options);
         std::vector<CNode*> vNodes;
     
    +    // reset global id counter
    +    id = 0;
    +
         // Add block-relay-only peers up to the limit
         for (int i = 0; i < max_outbound_block_relay; ++i) {
             AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
         }
         peerLogic->CheckForStaleTipAndEvictPeers();
     
    -    for (int i = 0; i < max_outbound_block_relay; ++i) {
    -        BOOST_CHECK(vNodes[i]->fDisconnect == false);
    +    for (CNode* node : vNodes) {
    +        BOOST_CHECK(node->fDisconnect == false);
         }
     
         // Add an extra block-relay-only peer breaking the limit (mocks logic in ThreadOpenConnections)
    @@ -222,17 +225,19 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
         peerLogic->CheckForStaleTipAndEvictPeers();
     
         // The extra peer should only get marked for eviction after MINIMUM_CONNECT_TIME
    -    for (int i = 0; i < max_outbound_block_relay; ++i) {
    -        BOOST_CHECK(vNodes[i]->fDisconnect == false);
    +    for (CNode* node : vNodes) {
    +        BOOST_CHECK(node->fDisconnect == false);
         }
    -    BOOST_CHECK(vNodes.back()->fDisconnect == false);
     
         SetMockTime(GetTime() + MINIMUM_CONNECT_TIME + 1);
         peerLogic->CheckForStaleTipAndEvictPeers();
    -    for (int i = 0; i < max_outbound_block_relay; ++i) {
    -        BOOST_CHECK(vNodes[i]->fDisconnect == false);
    +    for (CNode* node : vNodes) {
    +        if (node->GetId() == max_outbound_block_relay) {
    +            BOOST_CHECK(node->fDisconnect == true);
    +        } else {
    +            BOOST_CHECK(node->fDisconnect == false);
    +        }
         }
    -    BOOST_CHECK(vNodes.back()->fDisconnect == true);
     
         // Update the last block time for the extra peer,
         // and check that the next youngest peer gets evicted.
    @@ -240,11 +245,13 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
         vNodes.back()->nLastBlockTime = GetTime();
     
         peerLogic->CheckForStaleTipAndEvictPeers();
    -    for (int i = 0; i < max_outbound_block_relay - 1; ++i) {
    -        BOOST_CHECK(vNodes[i]->fDisconnect == false);
    +    for (CNode* node : vNodes) {
    +        if (node->GetId() == max_outbound_block_relay - 1) {
    +            BOOST_CHECK(node->fDisconnect == true);
    +        } else {
    +            BOOST_CHECK(node->fDisconnect == false);
    +        }
         }
    -    BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect == true);
    -    BOOST_CHECK(vNodes.back()->fDisconnect == false);
     
         for (const CNode* node : vNodes) {
             peerLogic->FinalizeNode(*node);
    
  26. in src/test/denialofservice_tests.cpp:108 in 4740fe8212
     104 | @@ -105,10 +105,10 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
     105 |      peerLogic->FinalizeNode(dummyNode1);
     106 |  }
     107 |  
     108 | -static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman)
     109 | +static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType)
    


    jnewbery commented at 10:33 AM on December 7, 2021:

    New parameters/variables should use snake_case:

    static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connection_type)
    
  27. jnewbery commented at 10:34 AM on December 7, 2021: member

    ACK 4740fe8212da21e86664355b4c6d0d7d838e4382

    I've left a couple of nits inline. No need to address unless someone is touching this test again with follow-ups.

    There is currently some work being done to overhaul the eviction logic and collect it in a single place. This test will need to be updated to test that new code.

  28. sidhujag referenced this in commit f021c04462 on Dec 7, 2021
  29. mzumsande deleted the branch on Dec 8, 2021
  30. RandyMcMillan referenced this in commit 2697e95e23 on Dec 23, 2021
  31. DrahtBot locked this on Dec 8, 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-17 03:13 UTC

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