diff --git a/src/avalanche/proofpool.h b/src/avalanche/proofpool.h --- a/src/avalanche/proofpool.h +++ b/src/avalanche/proofpool.h @@ -18,6 +18,8 @@ namespace avalanche { +class PeerManager; + struct ProofPoolEntry { size_t utxoIndex; ProofRef proof; @@ -70,6 +72,8 @@ AddProofStatus addProof(const ProofRef &proof); bool removeProof(ProofRef proof); + void rescan(PeerManager &peerManager); + ProofRef getProof(const ProofId &proofid) const; }; diff --git a/src/avalanche/proofpool.cpp b/src/avalanche/proofpool.cpp --- a/src/avalanche/proofpool.cpp +++ b/src/avalanche/proofpool.cpp @@ -4,6 +4,8 @@ #include +#include + namespace avalanche { ProofPool::AddProofStatus ProofPool::addProof(const ProofRef &proof) { @@ -53,6 +55,15 @@ return poolView.erase(proof->getId()); } +void ProofPool::rescan(PeerManager &peerManager) { + auto previousPool = std::move(pool); + pool.clear(); + + for (auto &entry : previousPool) { + peerManager.registerProof(entry.proof); + } +} + ProofRef ProofPool::getProof(const ProofId &proofid) const { auto &poolView = pool.get(); auto it = poolView.find(proofid); diff --git a/src/avalanche/test/proofpool_tests.cpp b/src/avalanche/test/proofpool_tests.cpp --- a/src/avalanche/test/proofpool_tests.cpp +++ b/src/avalanche/test/proofpool_tests.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -67,6 +68,36 @@ BOOST_CHECK(testPool.pool.empty()); } +BOOST_AUTO_TEST_CASE(rescan) { + ProofPool testPool; + avalanche::PeerManager pm; + + testPool.rescan(pm); + BOOST_CHECK(testPool.pool.empty()); + + // No peer should be created + bool hasPeer = false; + pm.forEachPeer([&](const Peer &p) { hasPeer = true; }); + BOOST_CHECK(!hasPeer); + + std::set poolProofs; + for (size_t i = 0; i < 10; i++) { + auto proof = buildRandomProof(MIN_VALID_PROOF_SCORE); + BOOST_CHECK_EQUAL(testPool.addProof(proof), + ProofPool::AddProofStatus::SUCCEED); + poolProofs.insert(std::move(proof)); + } + + testPool.rescan(pm); + + // All the proofs should be registered as peer + std::set pmProofs; + pm.forEachPeer([&](const Peer &p) { pmProofs.insert(p.proof); }); + BOOST_CHECK_EQUAL_COLLECTIONS(poolProofs.begin(), poolProofs.end(), + pmProofs.begin(), pmProofs.end()); + BOOST_CHECK(testPool.pool.empty()); +} + BOOST_AUTO_TEST_CASE(get_proof) { ProofPool testPool; diff --git a/test/lint/lint-circular-dependencies.sh b/test/lint/lint-circular-dependencies.sh --- a/test/lint/lint-circular-dependencies.sh +++ b/test/lint/lint-circular-dependencies.sh @@ -33,6 +33,7 @@ "pow/aserti32d -> validation -> pow/aserti32d" "pow/aserti32d -> validation -> pow/pow -> pow/aserti32d" "avalanche/orphanproofpool -> avalanche/peermanager -> avalanche/orphanproofpool" + "avalanche/peermanager -> avalanche/proofpool -> avalanche/peermanager" ) EXIT_CODE=0