Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/proofpool_tests.cpp
Show All 12 Lines | |||||
#include <avalanche/test/util.h> | #include <avalanche/test/util.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
using namespace avalanche; | using namespace avalanche; | ||||
namespace avalanche { | |||||
namespace { | |||||
struct TestPeerManager { | |||||
static ProofPool &getOrphanPool(PeerManager &pm) { | |||||
return pm.orphanProofPool; | |||||
} | |||||
}; | |||||
} // namespace | |||||
} // namespace avalanche | |||||
BOOST_FIXTURE_TEST_SUITE(proofpool_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(proofpool_tests, TestingSetup) | ||||
BOOST_AUTO_TEST_CASE(add_remove_proof_no_conflict) { | BOOST_AUTO_TEST_CASE(add_remove_proof_no_conflict) { | ||||
ProofPool testPool; | ProofPool testPool; | ||||
std::vector<ProofRef> proofs; | std::vector<ProofRef> proofs; | ||||
for (size_t i = 0; i < 10; i++) { | for (size_t i = 0; i < 10; i++) { | ||||
// Add a bunch of random proofs | // Add a bunch of random proofs | ||||
▲ Show 20 Lines • Show All 322 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(get_lowest_score_proof) { | ||||
} | } | ||||
// Remove the last proof | // Remove the last proof | ||||
BOOST_CHECK(testPool.removeProof(testPool.getLowestScoreProof()->getId())); | BOOST_CHECK(testPool.removeProof(testPool.getLowestScoreProof()->getId())); | ||||
BOOST_CHECK_EQUAL(testPool.getLowestScoreProof(), nullptr); | BOOST_CHECK_EQUAL(testPool.getLowestScoreProof(), nullptr); | ||||
BOOST_CHECK_EQUAL(testPool.countProofs(), 0); | BOOST_CHECK_EQUAL(testPool.countProofs(), 0); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(registration_time) { | |||||
ProofPool testPool; | |||||
// Proofs that are not in the pool have no registration time | |||||
for (size_t i = 0; i < 10; i++) { | |||||
BOOST_CHECK(!testPool.getRegistrationTime(ProofId(GetRandHash()))); | |||||
} | |||||
auto now = GetTime<std::chrono::seconds>(); | |||||
auto mocktime = now; | |||||
auto elapseTime = [&](std::chrono::seconds seconds) { | |||||
mocktime += seconds; | |||||
SetMockTime(mocktime.count()); | |||||
}; | |||||
elapseTime(0s); | |||||
// Add some proofs and check the registration time | |||||
std::vector<ProofRef> proofs; | |||||
for (size_t i = 0; i < 10; i++) { | |||||
auto proof = buildRandomProof(MIN_VALID_PROOF_SCORE); | |||||
BOOST_CHECK(testPool.addProofIfNoConflict(proof)); | |||||
auto registrationTime = testPool.getRegistrationTime(proof->getId()); | |||||
BOOST_CHECK(registrationTime.value_or(0s) == mocktime); | |||||
proofs.push_back(std::move(proof)); | |||||
elapseTime(1s); | |||||
} | |||||
// Removing a proof clears its registration time | |||||
for (size_t i = 0; i < 5; i++) { | |||||
auto &proof = proofs[i]; | |||||
BOOST_CHECK(testPool.removeProof(proof->getId())); | |||||
BOOST_CHECK(!testPool.getRegistrationTime(proof->getId())); | |||||
} | |||||
// Other proofs registration time are unaffected | |||||
for (size_t i = 5; i < 10; i++) { | |||||
auto registrationTime = | |||||
testPool.getRegistrationTime(proofs[i]->getId()); | |||||
BOOST_CHECK(registrationTime.value_or(0s) == now + i * 1s); | |||||
} | |||||
{ | |||||
// Proofs removed during a rescan have their registration time cleared. | |||||
avalanche::PeerManager pm; | |||||
testPool.rescan(pm); | |||||
BOOST_CHECK_EQUAL(testPool.countProofs(), 0); | |||||
for (size_t i = 5; i < 10; i++) { | |||||
BOOST_CHECK(!testPool.getRegistrationTime(proofs[i]->getId())); | |||||
} | |||||
} | |||||
{ | |||||
// Proofs added back during a rescan retain their registration time. | |||||
avalanche::PeerManager pm; | |||||
auto &pool = TestPeerManager::getOrphanPool(pm); | |||||
now = GetTime<std::chrono::seconds>(); | |||||
mocktime = now; | |||||
elapseTime(0s); | |||||
const CKey key = CKey::MakeCompressedKey(); | |||||
// Add some orphan proofs | |||||
std::vector<ProofRef> orphans; | |||||
for (size_t i = 0; i < 10; i++) { | |||||
ProofBuilder pb(0, 0, key); | |||||
BOOST_CHECK(pb.addUTXO({TxId(GetRandHash()), 0}, 10 * COIN, 10, | |||||
false, key)); | |||||
auto proof = pb.build(); | |||||
BOOST_CHECK(proof != nullptr); | |||||
ProofRegistrationState state; | |||||
BOOST_CHECK(!pm.registerProof(proof, state)); | |||||
BOOST_CHECK(state.GetResult() == ProofRegistrationResult::ORPHAN); | |||||
BOOST_CHECK(pm.isOrphan(proof->getId())); | |||||
auto registrationTime = pool.getRegistrationTime(proof->getId()); | |||||
BOOST_CHECK(registrationTime.value_or(0s) == now + i * 1s); | |||||
orphans.push_back(std::move(proof)); | |||||
elapseTime(1s); | |||||
} | |||||
pool.rescan(pm); | |||||
BOOST_CHECK_EQUAL(pool.countProofs(), 10); | |||||
for (size_t i = 0; i < 10; i++) { | |||||
auto registrationTime = | |||||
pool.getRegistrationTime(orphans[i]->getId()); | |||||
BOOST_CHECK(registrationTime.value_or(0s) == now + i * 1s); | |||||
} | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |