Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | struct TestPeerManager { | ||||
auto &peerView = pm.peers.get<by_score>(); | auto &peerView = pm.peers.get<by_score>(); | ||||
for (const Peer &peer : peerView) { | for (const Peer &peer : peerView) { | ||||
scores.push_back(peer.getScore()); | scores.push_back(peer.getScore()); | ||||
} | } | ||||
return scores; | return scores; | ||||
} | } | ||||
static void cleanup(PeerManager &pm) { pm.cleanup(); } | |||||
}; | }; | ||||
static void addCoin(const COutPoint &outpoint, const CKey &key, | static void addCoin(const COutPoint &outpoint, const CKey &key, | ||||
const Amount amount = 10 * COIN, uint32_t height = 100, | const Amount amount = 10 * COIN, uint32_t height = 100, | ||||
bool is_coinbase = false) { | bool is_coinbase = false) { | ||||
CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); | CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
▲ Show 20 Lines • Show All 1,764 Lines • ▼ Show 20 Lines | for (NodeId nodeid = 0; nodeid < 10; nodeid++) { | ||||
addNode(nodeid); | addNode(nodeid); | ||||
BOOST_CHECK(pm.latchAvaproofsSent(nodeid)); | BOOST_CHECK(pm.latchAvaproofsSent(nodeid)); | ||||
// The flag is already set | // The flag is already set | ||||
BOOST_CHECK(!pm.latchAvaproofsSent(nodeid)); | BOOST_CHECK(!pm.latchAvaproofsSent(nodeid)); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(cleanup) { | |||||
avalanche::PeerManager pm; | |||||
auto buildOrphan = [&]() { | |||||
const CKey key = CKey::MakeCompressedKey(); | |||||
return buildProofWithOutpoints(key, {{TxId(GetRandHash()), 0}}, | |||||
10 * COIN); | |||||
}; | |||||
auto setMockTime = [&](const std::chrono::seconds mocktime) { | |||||
SetMockTime(mocktime.count()); | |||||
}; | |||||
auto now = GetTime<std::chrono::seconds>(); | |||||
auto checkOrphan = [&](const ProofRef &proof, bool expectOrphan) { | |||||
BOOST_CHECK_EQUAL(pm.isOrphan(proof->getId()), expectOrphan); | |||||
BOOST_CHECK_EQUAL(pm.exists(proof->getId()), expectOrphan); | |||||
BOOST_CHECK_EQUAL(!!pm.getProofRegistrationTime(proof->getId()), | |||||
expectOrphan); | |||||
}; | |||||
std::vector<ProofRef> proofs; | |||||
for (size_t i = 0; i < 10; i++) { | |||||
setMockTime(now + i * 1s); | |||||
auto proof = buildOrphan(); | |||||
BOOST_CHECK(proof != nullptr); | |||||
BOOST_CHECK(!pm.registerProof(proof)); | |||||
checkOrphan(proof, true); | |||||
proofs.push_back(std::move(proof)); | |||||
} | |||||
// The orphans are not expired yet | |||||
TestPeerManager::cleanup(pm); | |||||
for (const auto &proof : proofs) { | |||||
checkOrphan(proof, true); | |||||
} | |||||
setMockTime(now + 30min); | |||||
// The orphans are not expired yet | |||||
TestPeerManager::cleanup(pm); | |||||
for (const auto &proof : proofs) { | |||||
checkOrphan(proof, true); | |||||
} | |||||
setMockTime(now + 30min + 5s); | |||||
// The first half of the orphans are now expired | |||||
TestPeerManager::cleanup(pm); | |||||
for (size_t i = 0; i < 10; i++) { | |||||
checkOrphan(proofs[i], i > 4); | |||||
} | |||||
setMockTime(now + 30min + 10s); | |||||
// Now they're all expired | |||||
TestPeerManager::cleanup(pm); | |||||
for (const auto &proof : proofs) { | |||||
checkOrphan(proof, false); | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |