diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -412,16 +412,20 @@ // Check proof pool consistency for (const auto &ss : p.proof->getStakes()) { - auto it = validProofPool.pool.find(ss.getStake().getUTXO()); + const COutPoint &outpoint = ss.getStake().getUTXO(); + auto proof = validProofPool.getProof(outpoint); - if (it == validProofPool.pool.end()) { + if (!proof) { + // Missing utxo return false; } - if (it->proof->getId() != p.getProofId()) { + if (proof->getId() != p.getProofId()) { + // Wrong proof return false; } - if (!peersUtxos.emplace(it->getUTXO()).second) { + if (!peersUtxos.emplace(outpoint).second) { + // Duplicated utxo return false; } } @@ -462,11 +466,10 @@ } } - // Check there is no dangling utxo - for (const auto &entry : validProofPool.pool) { - if (!peersUtxos.count(entry.getUTXO())) { - return false; - } + // We checked the utxo consistency for all our peers utxos already, so if + // the pool size differs from the expected one there are dangling utxos. + if (validProofPool.size() != peersUtxos.size()) { + return false; } return true; diff --git a/src/avalanche/proofpool.h b/src/avalanche/proofpool.h --- a/src/avalanche/proofpool.h +++ b/src/avalanche/proofpool.h @@ -16,6 +16,8 @@ #include +class COutPoint; + namespace avalanche { struct ProofPoolEntry { @@ -45,7 +47,7 @@ /** * Map a proof to each utxo. A proof can be mapped with several utxos. */ -struct ProofPool { +class ProofPool { boost::multi_index_container< ProofPoolEntry, bmi::indexed_by< @@ -61,6 +63,7 @@ SaltedProofIdHasher>>> pool; +public: enum AddProofStatus { REJECTED = 0, //!< Rejected due to conflicts SUCCEED = 1, //!< Added successfully @@ -71,6 +74,9 @@ bool removeProof(ProofRef proof); ProofRef getProof(const ProofId &proofid) const; + ProofRef getProof(const COutPoint &outpoint) const; + + size_t size() const { return pool.size(); } }; } // namespace avalanche 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) { @@ -59,4 +61,9 @@ return it == poolView.end() ? nullptr : it->proof; } +ProofRef ProofPool::getProof(const COutPoint &outpoint) const { + auto it = pool.find(outpoint); + return it == pool.end() ? nullptr : it->proof; +} + } // namespace avalanche 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 @@ -64,7 +64,7 @@ for (auto proof : proofs) { BOOST_CHECK(testPool.removeProof(proof)); } - BOOST_CHECK(testPool.pool.empty()); + BOOST_CHECK_EQUAL(testPool.size(), 0); } BOOST_AUTO_TEST_CASE(get_proof) {