-
Notifications
You must be signed in to change notification settings - Fork 37.8k
test: add unit test for block-relay-only eviction #23614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add unit test for block-relay-only eviction #23614
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 4c449a5, very clean 👌
4c449a5
to
e0a214f
Compare
Thanks for the reviews - I addressed all comments! |
reACK e0a214f changes:
|
e0a214f
to
4740fe8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will do if I push again, though this should be probably done everywhere in this test per scripted-diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 4740fe8. Great work @ mzumsande!
reACK 4740fe8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in new code you can just use nodes;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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);
@@ -105,10 +105,10 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) | |||
peerLogic->FinalizeNode(dummyNode1); | |||
} | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_management
unit test, which tests the analogous logic for regular outbound peers.