Changeset View
Changeset View
Standalone View
Standalone View
src/test/denialofservice_tests.cpp
Show First 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(stale_tip_peer_management) { | ||||
connman->Init(options); | connman->Init(options); | ||||
std::vector<CNode *> vNodes; | std::vector<CNode *> vNodes; | ||||
// Mock some outbound peers | // Mock some outbound peers | ||||
for (int i = 0; i < max_outbound_full_relay; ++i) { | for (int i = 0; i < max_outbound_full_relay; ++i) { | ||||
AddRandomOutboundPeer(config, vNodes, *peerLogic, connman.get()); | AddRandomOutboundPeer(config, vNodes, *peerLogic, connman.get()); | ||||
} | } | ||||
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams); | peerLogic->CheckForStaleTipAndEvictPeers(); | ||||
// No nodes should be marked for disconnection while we have no extra peers | // No nodes should be marked for disconnection while we have no extra peers | ||||
for (const CNode *node : vNodes) { | for (const CNode *node : vNodes) { | ||||
BOOST_CHECK(node->fDisconnect == false); | BOOST_CHECK(node->fDisconnect == false); | ||||
} | } | ||||
SetMockTime(GetTime() + 3 * consensusParams.nPowTargetSpacing + 1); | SetMockTime(GetTime() + 3 * consensusParams.nPowTargetSpacing + 1); | ||||
// Now tip should definitely be stale, and we should look for an extra | // Now tip should definitely be stale, and we should look for an extra | ||||
// outbound peer | // outbound peer | ||||
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams); | peerLogic->CheckForStaleTipAndEvictPeers(); | ||||
BOOST_CHECK(connman->GetTryNewOutboundPeer()); | BOOST_CHECK(connman->GetTryNewOutboundPeer()); | ||||
// Still no peers should be marked for disconnection | // Still no peers should be marked for disconnection | ||||
for (const CNode *node : vNodes) { | for (const CNode *node : vNodes) { | ||||
BOOST_CHECK(node->fDisconnect == false); | BOOST_CHECK(node->fDisconnect == false); | ||||
} | } | ||||
// If we add one more peer, something should get marked for eviction | // If we add one more peer, something should get marked for eviction | ||||
// on the next check (since we're mocking the time to be in the future, the | // on the next check (since we're mocking the time to be in the future, the | ||||
// required time connected check should be satisfied). | // required time connected check should be satisfied). | ||||
AddRandomOutboundPeer(config, vNodes, *peerLogic, connman.get()); | AddRandomOutboundPeer(config, vNodes, *peerLogic, connman.get()); | ||||
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams); | peerLogic->CheckForStaleTipAndEvictPeers(); | ||||
for (int i = 0; i < max_outbound_full_relay; ++i) { | for (int i = 0; i < max_outbound_full_relay; ++i) { | ||||
BOOST_CHECK(vNodes[i]->fDisconnect == false); | BOOST_CHECK(vNodes[i]->fDisconnect == false); | ||||
} | } | ||||
// Last added node should get marked for eviction | // Last added node should get marked for eviction | ||||
BOOST_CHECK(vNodes.back()->fDisconnect == true); | BOOST_CHECK(vNodes.back()->fDisconnect == true); | ||||
vNodes.back()->fDisconnect = false; | vNodes.back()->fDisconnect = false; | ||||
// Update the last announced block time for the last | // Update the last announced block time for the last | ||||
// peer, and check that the next newest node gets evicted. | // peer, and check that the next newest node gets evicted. | ||||
UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime()); | UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime()); | ||||
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams); | peerLogic->CheckForStaleTipAndEvictPeers(); | ||||
for (int i = 0; i < max_outbound_full_relay - 1; ++i) { | for (int i = 0; i < max_outbound_full_relay - 1; ++i) { | ||||
BOOST_CHECK(vNodes[i]->fDisconnect == false); | BOOST_CHECK(vNodes[i]->fDisconnect == false); | ||||
} | } | ||||
BOOST_CHECK(vNodes[max_outbound_full_relay - 1]->fDisconnect == true); | BOOST_CHECK(vNodes[max_outbound_full_relay - 1]->fDisconnect == true); | ||||
BOOST_CHECK(vNodes.back()->fDisconnect == false); | BOOST_CHECK(vNodes.back()->fDisconnect == false); | ||||
bool dummy; | bool dummy; | ||||
for (const CNode *node : vNodes) { | for (const CNode *node : vNodes) { | ||||
▲ Show 20 Lines • Show All 198 Lines • Show Last 20 Lines |