test: add unit test for block-relay-only eviction#23614
test: add unit test for block-relay-only eviction#23614maflcko merged 1 commit intobitcoin:masterfrom
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
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. Coverage
Updated at: 2021-12-06T11:23:42.381149. |
shaavan
left a comment
There was a problem hiding this comment.
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.
4c449a5 to
e0a214f
Compare
|
Thanks for the reviews - I addressed all comments! |
|
reACK e0a214f48ec2b8f45d09f85bf8429d805e6ae411 changes:
|
e0a214f to
4740fe8
Compare
|
e0a214f -> 4740fe8 removed unnecessary call to |
rajarshimaitra
left a comment
There was a problem hiding this comment.
tACK 4740fe8
Just one nit.
| { | ||
| const CChainParams& chainparams = Params(); | ||
| auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman); | ||
| auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr, |
There was a problem hiding this comment.
This has been used before too, but can we name it peerman instead, like connman? Sounds more clear.
There was a problem hiding this comment.
Yes, will do if I push again, though this should be probably done everywhere in this test per scripted-diff.
|
reACK 4740fe8 |
| peerLogic->CheckForStaleTipAndEvictPeers(); | ||
|
|
||
| for (int i = 0; i < max_outbound_block_relay; ++i) { | ||
| BOOST_CHECK(vNodes[i]->fDisconnect == false); |
There was a problem hiding this comment.
If you have a chance to re-touch
| BOOST_CHECK(vNodes[i]->fDisconnect == false); | |
| 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.
| options.m_max_outbound_block_relay = max_outbound_block_relay; | ||
|
|
||
| connman->Init(options); | ||
| std::vector<CNode*> vNodes; |
There was a problem hiding this comment.
in new code you can just use nodes;
jnewbery
left a comment
There was a problem hiding this comment.
ACK 4740fe8
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.
| } | ||
| peerLogic->CheckForStaleTipAndEvictPeers(); | ||
|
|
||
| for (int i = 0; i < max_outbound_block_relay; ++i) { |
There was a problem hiding this comment.
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);| } | ||
|
|
||
| static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman) | ||
| static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType) |
There was a problem hiding this comment.
New parameters/variables should use snake_case:
| static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType) | |
| static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connection_type) |
4740fe8 test: Add test for block relay only eviction (Martin Zumsande) Pull request description: Adds a unit test for block-relay-only eviction logic added in bitcoin#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. ACKs for top commit: glozow: reACK 4740fe8 rajarshimaitra: tACK bitcoin@4740fe8 shaavan: ACK 4740fe8. Great work @ mzumsande! LarryRuane: ACK 4740fe8 Tree-SHA512: 5985afd7d8f7ae311903dbbf6b7d526e16309c83c88ae6dd6551960c0b186156310a6be0cf6b684f82ac1378d0fc5aa3717f0139e078471013fceb6aebe81bf6
… eviction c62f7e5 test: Add test for block relay only eviction (Martin Zumsande) Pull request description: 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. ACKs for top commit: glozow: reACK c62f7e5 rajarshimaitra: tACK bitcoin/bitcoin@c62f7e5 shaavan: ACK c62f7e5. Great work @ mzumsande! LarryRuane: ACK c62f7e5 Tree-SHA512: 5985afd7d8f7ae311903dbbf6b7d526e16309c83c88ae6dd6551960c0b186156310a6be0cf6b684f82ac1378d0fc5aa3717f0139e078471013fceb6aebe81bf6
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_managementunit test, which tests the analogous logic for regular outbound peers.