diff --git a/src/avalanche/test/delegation_tests.cpp b/src/avalanche/test/delegation_tests.cpp --- a/src/avalanche/test/delegation_tests.cpp +++ b/src/avalanche/test/delegation_tests.cpp @@ -19,7 +19,8 @@ BOOST_FIXTURE_TEST_SUITE(delegation_tests, TestingSetup) -static void CheckDelegation(const Delegation &dg, const Proof &p, +static void CheckDelegation(const Delegation &dg, + const std::shared_ptr &p, const CPubKey &expected_pubkey) { DelegationState state; CPubKey pubkey; @@ -27,19 +28,19 @@ BOOST_CHECK(state.GetResult() == DelegationResult::NONE); BOOST_CHECK(pubkey == expected_pubkey); - BOOST_CHECK(dg.getProofId() == p.getId()); + BOOST_CHECK(dg.getProofId() == p->getId()); } BOOST_AUTO_TEST_CASE(verify_random) { auto key = CKey::MakeCompressedKey(); - const Proof p = buildRandomProof(123456, key); - DelegationBuilder dgb(p); + auto p = buildRandomProof(123456, key); + DelegationBuilder dgb(*p); { Delegation dg = dgb.build(); - BOOST_CHECK_EQUAL(dg.getId(), p.getId()); - CheckDelegation(dg, p, p.getMaster()); + BOOST_CHECK_EQUAL(dg.getId(), p->getId()); + CheckDelegation(dg, p, p->getMaster()); } auto l1key = CKey::MakeCompressedKey(); diff --git a/src/avalanche/test/peermanager_tests.cpp b/src/avalanche/test/peermanager_tests.cpp --- a/src/avalanche/test/peermanager_tests.cpp +++ b/src/avalanche/test/peermanager_tests.cpp @@ -173,13 +173,9 @@ } } -static std::shared_ptr getRandomProofPtr(uint32_t score) { - return std::make_shared(buildRandomProof(score)); -} - static void addNodeWithScore(avalanche::PeerManager &pm, NodeId node, uint32_t score) { - auto proof = getRandomProofPtr(score); + auto proof = buildRandomProof(score); BOOST_CHECK_NE(pm.getPeerId(proof), NO_PEER); BOOST_CHECK(pm.addNode(node, proof->getId())); }; @@ -228,7 +224,7 @@ // Add 4 peers. std::array peerids; for (int i = 0; i < 4; i++) { - auto p = getRandomProofPtr(100); + auto p = buildRandomProof(100); peerids[i] = pm.getPeerId(p); BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); } @@ -260,7 +256,7 @@ // Add 4 more peers. for (int i = 0; i < 4; i++) { - auto p = getRandomProofPtr(100); + auto p = buildRandomProof(100); peerids[i + 4] = pm.getPeerId(p); BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); } @@ -302,7 +298,7 @@ // Add 4 peers. std::array peerids; for (int i = 0; i < 4; i++) { - auto p = getRandomProofPtr(100); + auto p = buildRandomProof(100); peerids[i] = pm.getPeerId(p); BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); } @@ -329,7 +325,7 @@ avalanche::PeerManager pm; // Create one peer. - auto proof = getRandomProofPtr(10000000 * MIN_VALID_PROOF_SCORE); + auto proof = buildRandomProof(10000000 * MIN_VALID_PROOF_SCORE); BOOST_CHECK_NE(pm.getPeerId(proof), NO_PEER); BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); @@ -388,7 +384,7 @@ BOOST_AUTO_TEST_CASE(node_binding) { avalanche::PeerManager pm; - auto proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); + auto proof = buildRandomProof(MIN_VALID_PROOF_SCORE); const ProofId &proofid = proof->getId(); // Add a bunch of nodes with no associated peer @@ -420,7 +416,7 @@ BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid)); } - auto alt_proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); + auto alt_proof = buildRandomProof(MIN_VALID_PROOF_SCORE); const ProofId &alt_proofid = alt_proof->getId(); // Update some nodes from a known proof to an unknown proof @@ -430,7 +426,7 @@ BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid)); } - auto alt2_proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); + auto alt2_proof = buildRandomProof(MIN_VALID_PROOF_SCORE); const ProofId &alt2_proofid = alt2_proof->getId(); // Update some nodes from an unknown proof to another unknown proof @@ -718,7 +714,7 @@ BOOST_AUTO_TEST_CASE(dangling_node) { avalanche::PeerManager pm; - auto proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); + auto proof = buildRandomProof(MIN_VALID_PROOF_SCORE); PeerId peerid = pm.getPeerId(proof); BOOST_CHECK_NE(peerid, NO_PEER); @@ -740,7 +736,7 @@ } // Build a new one - proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); + proof = buildRandomProof(MIN_VALID_PROOF_SCORE); peerid = pm.getPeerId(proof); BOOST_CHECK_NE(peerid, NO_PEER); @@ -768,7 +764,7 @@ std::vector> proofs; proofs.reserve(numProofs); for (int i = 0; i < numProofs; i++) { - proofs.push_back(getRandomProofPtr(MIN_VALID_PROOF_SCORE)); + proofs.push_back(buildRandomProof(MIN_VALID_PROOF_SCORE)); } for (int i = 0; i < numProofs; i++) { diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -1109,7 +1109,7 @@ uint32_t score = MIN_VALID_PROOF_SCORE; auto addProofToReconcile = [&](uint32_t proofScore) { - auto proof = std::make_shared(buildRandomProof(proofScore)); + auto proof = buildRandomProof(proofScore); m_processor->addProofToReconcile(proof, GetRandInt(1)); return proof; }; diff --git a/src/avalanche/test/proof_tests.cpp b/src/avalanche/test/proof_tests.cpp --- a/src/avalanche/test/proof_tests.cpp +++ b/src/avalanche/test/proof_tests.cpp @@ -23,15 +23,15 @@ BOOST_AUTO_TEST_CASE(proof_random) { for (int i = 0; i < 1000; i++) { const uint32_t score = InsecureRand32(); - const Proof p = buildRandomProof(score); - BOOST_CHECK_EQUAL(p.getScore(), score); + auto p = buildRandomProof(score); + BOOST_CHECK_EQUAL(p->getScore(), score); ProofValidationResult expected_state = hasDustStake(p) ? ProofValidationResult::DUST_THRESOLD : ProofValidationResult::NONE; ProofValidationState state; - BOOST_CHECK_EQUAL(p.verify(state), + BOOST_CHECK_EQUAL(p->verify(state), state.GetResult() == ProofValidationResult::NONE); BOOST_CHECK(state.GetResult() == expected_state); } diff --git a/src/avalanche/test/proofcomparator_tests.cpp b/src/avalanche/test/proofcomparator_tests.cpp --- a/src/avalanche/test/proofcomparator_tests.cpp +++ b/src/avalanche/test/proofcomparator_tests.cpp @@ -22,18 +22,15 @@ BOOST_AUTO_TEST_CASE(proof_shared_pointer_comparator) { uint32_t score = MIN_VALID_PROOF_SCORE; - auto proofMinScore = - std::make_shared(buildRandomProof(MIN_VALID_PROOF_SCORE)); - auto proofMaxScore = std::make_shared( - buildRandomProof(std::numeric_limits::max())); + auto proofMinScore = buildRandomProof(MIN_VALID_PROOF_SCORE); + auto proofMaxScore = buildRandomProof(std::numeric_limits::max()); const ProofSharedPointerComparator comparator; auto prevProof = proofMinScore; for (size_t i = 0; i < 100; i++) { score += 1000 + GetRandInt(10000); - auto higherScoreProof = - std::make_shared(buildRandomProof(score)); + auto higherScoreProof = buildRandomProof(score); BOOST_CHECK(comparator(higherScoreProof, proofMinScore)); BOOST_CHECK(comparator(higherScoreProof, prevProof)); BOOST_CHECK(!comparator(higherScoreProof, proofMaxScore)); @@ -44,7 +41,7 @@ // the score reached the minimal value. for (size_t i = 0; i < 100; i++) { score -= 1 + GetRandInt(100); - auto lowerScoreProof = std::make_shared(buildRandomProof(score)); + auto lowerScoreProof = buildRandomProof(score); BOOST_CHECK(comparator(lowerScoreProof, proofMinScore)); BOOST_CHECK(!comparator(lowerScoreProof, prevProof)); BOOST_CHECK(!comparator(lowerScoreProof, proofMaxScore)); @@ -52,8 +49,7 @@ } for (size_t i = 0; i < 100; i++) { - auto anotherProofMinScore = - std::make_shared(buildRandomProof(MIN_VALID_PROOF_SCORE)); + auto anotherProofMinScore = buildRandomProof(MIN_VALID_PROOF_SCORE); BOOST_CHECK_EQUAL(comparator(anotherProofMinScore, proofMinScore), anotherProofMinScore->getId() < proofMinScore->getId()); diff --git a/src/avalanche/test/util.h b/src/avalanche/test/util.h --- a/src/avalanche/test/util.h +++ b/src/avalanche/test/util.h @@ -15,10 +15,11 @@ constexpr uint32_t MIN_VALID_PROOF_SCORE = 100 * PROOF_DUST_THRESHOLD / COIN; -Proof buildRandomProof(uint32_t score, - const CKey &masterKey = CKey::MakeCompressedKey()); +std::shared_ptr +buildRandomProof(uint32_t score, + const CKey &masterKey = CKey::MakeCompressedKey()); -bool hasDustStake(const Proof &proof); +bool hasDustStake(const std::shared_ptr &proof); struct TestProofBuilder { static ProofId getReverseOrderProofId(ProofBuilder &pb); diff --git a/src/avalanche/test/util.cpp b/src/avalanche/test/util.cpp --- a/src/avalanche/test/util.cpp +++ b/src/avalanche/test/util.cpp @@ -17,7 +17,7 @@ namespace avalanche { -Proof buildRandomProof(uint32_t score, const CKey &masterKey) { +std::shared_ptr buildRandomProof(uint32_t score, const CKey &masterKey) { auto key = CKey::MakeCompressedKey(); const COutPoint o(TxId(GetRandHash()), 0); @@ -35,11 +35,11 @@ ProofBuilder pb(0, std::numeric_limits::max(), masterKey); BOOST_CHECK(pb.addUTXO(o, v, height, is_coinbase, std::move(key))); - return pb.build(); + return std::make_shared(pb.build()); } -bool hasDustStake(const Proof &proof) { - for (const SignedStake &s : proof.getStakes()) { +bool hasDustStake(const std::shared_ptr &proof) { + for (const SignedStake &s : proof->getStakes()) { if (s.getStake().getAmount() < PROOF_DUST_THRESHOLD) { return true; }