diff --git a/src/avalanche/orphanproofpool.h b/src/avalanche/orphanproofpool.h --- a/src/avalanche/orphanproofpool.h +++ b/src/avalanche/orphanproofpool.h @@ -19,7 +19,7 @@ // Extracts a ProofId from a Proof struct proofid_extractor { using result_type = ProofId; - result_type operator()(const std::shared_ptr &proof) const { + result_type operator()(const ProofRef &proof) const { return proof->getId(); } }; @@ -28,7 +28,7 @@ struct by_proofid {}; using ProofContainer = boost::multi_index_container< - std::shared_ptr, + ProofRef, boost::multi_index::indexed_by< // keep insertion order boost::multi_index::sequenced>, @@ -63,7 +63,7 @@ * Add a proof to the pool. * The caller is responsible for checking the proof. */ - bool addProof(const std::shared_ptr &proof); + bool addProof(const ProofRef &proof); /** Remove a proof from the pool. */ bool removeProof(const ProofId &proofId); @@ -72,7 +72,7 @@ * Get a pointer to a proof by id, or nullptr if the proof is not in the * pool. */ - std::shared_ptr getProof(const ProofId &proofId) const; + ProofRef getProof(const ProofId &proofId) const; /** * Rescan the pool to remove previously orphaned proofs that have become diff --git a/src/avalanche/orphanproofpool.cpp b/src/avalanche/orphanproofpool.cpp --- a/src/avalanche/orphanproofpool.cpp +++ b/src/avalanche/orphanproofpool.cpp @@ -19,7 +19,7 @@ } } -bool OrphanProofPool::addProof(const std::shared_ptr &proof) { +bool OrphanProofPool::addProof(const ProofRef &proof) { size_t nStakesProof = proof->getStakes().size(); if (!proofs.push_back(proof).second) { return false; @@ -40,7 +40,7 @@ return true; } -std::shared_ptr OrphanProofPool::getProof(const ProofId &proofId) const { +ProofRef OrphanProofPool::getProof(const ProofId &proofId) const { auto &proofs_by_proofid = proofs.get(); auto it = proofs_by_proofid.find(proofId); return it == proofs_by_proofid.end() ? nullptr : *it; diff --git a/src/avalanche/peermanager.h b/src/avalanche/peermanager.h --- a/src/avalanche/peermanager.h +++ b/src/avalanche/peermanager.h @@ -77,12 +77,12 @@ uint32_t index = -1; uint32_t node_count = 0; - std::shared_ptr proof; + ProofRef proof; // The network stack uses timestamp in seconds, so we oblige. std::chrono::seconds registration_time; - Peer(PeerId peerid_, std::shared_ptr proof_) + Peer(PeerId peerid_, ProofRef proof_) : peerid(peerid_), proof(std::move(proof_)), registration_time(GetTime()) {} @@ -204,7 +204,7 @@ /** * Proof and Peer related API. */ - bool registerProof(const std::shared_ptr &proof); + bool registerProof(const ProofRef &proof); bool exists(const ProofId &proofid) const { return getProof(proofid) != nullptr; } @@ -241,7 +241,7 @@ * Provide the PeerId associated with the given proof. If the peer does not * exist, then it is created. */ - PeerId getPeerId(const std::shared_ptr &proof); + PeerId getPeerId(const ProofRef &proof); /** * Remove an existing peer. @@ -268,13 +268,13 @@ uint64_t getSlotCount() const { return slotCount; } uint64_t getFragmentation() const { return fragmentation; } - std::shared_ptr getProof(const ProofId &proofid) const; + ProofRef getProof(const ProofId &proofid) const; bool isOrphan(const ProofId &id) const; - std::shared_ptr getOrphan(const ProofId &id) const; + ProofRef getOrphan(const ProofId &id) const; private: - PeerSet::iterator fetchOrCreatePeer(const std::shared_ptr &proof); + PeerSet::iterator fetchOrCreatePeer(const ProofRef &proof); bool addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid); bool addNodeToPeer(const PeerSet::iterator &it); bool removeNodeFromPeer(const PeerSet::iterator &it, uint32_t count = 1); diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -142,7 +142,7 @@ return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); } -bool PeerManager::registerProof(const std::shared_ptr &proof) { +bool PeerManager::registerProof(const ProofRef &proof) { return !getProof(proof->getId()) && getPeerId(proof) != NO_PEER; } @@ -176,7 +176,7 @@ void PeerManager::updatedBlockTip() { std::vector invalidPeers; - std::vector> newOrphans; + std::vector newOrphans; { LOCK(cs_main); @@ -204,13 +204,13 @@ } } -PeerId PeerManager::getPeerId(const std::shared_ptr &proof) { +PeerId PeerManager::getPeerId(const ProofRef &proof) { auto it = fetchOrCreatePeer(proof); return it == peers.end() ? NO_PEER : it->peerid; } -std::shared_ptr PeerManager::getProof(const ProofId &proofid) const { - std::shared_ptr proof = nullptr; +ProofRef PeerManager::getProof(const ProofId &proofid) const { + ProofRef proof = nullptr; forPeer(proofid, [&](const Peer &p) { proof = p.proof; @@ -221,7 +221,7 @@ } PeerManager::PeerSet::iterator -PeerManager::fetchOrCreatePeer(const std::shared_ptr &proof) { +PeerManager::fetchOrCreatePeer(const ProofRef &proof) { assert(proof); const ProofId &proofid = proof->getId(); { @@ -512,7 +512,7 @@ return orphanProofs.getProof(id) != nullptr; } -std::shared_ptr PeerManager::getOrphan(const ProofId &id) const { +ProofRef PeerManager::getOrphan(const ProofId &id) const { return orphanProofs.getProof(id); } diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -76,12 +76,11 @@ }; using BlockUpdate = VoteItemUpdate; -using ProofUpdate = VoteItemUpdate>; +using ProofUpdate = VoteItemUpdate; using BlockVoteMap = std::map; -using ProofVoteMap = - std::map, VoteRecord, ProofComparator>; +using ProofVoteMap = std::map; struct query_timeout {}; @@ -172,12 +171,11 @@ } bool addBlockToReconcile(const CBlockIndex *pindex); - void addProofToReconcile(const std::shared_ptr &proof, - bool isAccepted); + void addProofToReconcile(const ProofRef &proof, bool isAccepted); bool isAccepted(const CBlockIndex *pindex) const; - bool isAccepted(const std::shared_ptr &proof) const; + bool isAccepted(const ProofRef &proof) const; int getConfidence(const CBlockIndex *pindex) const; - int getConfidence(const std::shared_ptr &proof) const; + int getConfidence(const ProofRef &proof) const; // TODO: Refactor the API to remove the dependency on avalanche/protocol.h void sendResponse(CNode *pfrom, Response response) const; @@ -194,7 +192,7 @@ CPubKey getSessionPubKey() const; bool sendHello(CNode *pfrom) const; - std::shared_ptr getLocalProof() const; + ProofRef getLocalProof() const; /* * Return whether the avalanche service flag should be set. diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -106,7 +106,7 @@ } struct Processor::PeerData { - std::shared_ptr proof; + ProofRef proof; Delegation delegation; }; @@ -261,8 +261,7 @@ .second; } -void Processor::addProofToReconcile(const std::shared_ptr &proof, - bool isAccepted) { +void Processor::addProofToReconcile(const ProofRef &proof, bool isAccepted) { // TODO We don't want to accept an infinite number of conflicting proofs. // They should be some rules to make them expensive and/or limited by // design. @@ -280,7 +279,7 @@ return it->second.isAccepted(); } -bool Processor::isAccepted(const std::shared_ptr &proof) const { +bool Processor::isAccepted(const ProofRef &proof) const { auto r = proofVoteRecords.getReadView(); auto it = r->find(proof); if (it == r.end()) { @@ -300,7 +299,7 @@ return it->second.getConfidence(); } -int Processor::getConfidence(const std::shared_ptr &proof) const { +int Processor::getConfidence(const ProofRef &proof) const { auto r = proofVoteRecords.getReadView(); auto it = r->find(proof); if (it == r.end()) { @@ -396,7 +395,7 @@ } std::map responseIndex; - std::map, Vote> responseProof; + std::map responseProof; // At this stage we are certain that invs[i] matches votes[i], so we can use // the inv type to retrieve what is being voted on. @@ -518,7 +517,7 @@ return true; } -std::shared_ptr Processor::getLocalProof() const { +ProofRef Processor::getLocalProof() const { return peerData ? peerData->proof : nullptr; } diff --git a/src/avalanche/proof.h b/src/avalanche/proof.h --- a/src/avalanche/proof.h +++ b/src/avalanche/proof.h @@ -155,6 +155,8 @@ bool verify(ProofValidationState &state, const CCoinsView &view) const; }; +using ProofRef = std::shared_ptr; + } // namespace avalanche #endif // BITCOIN_AVALANCHE_PROOF_H diff --git a/src/avalanche/proofcomparator.h b/src/avalanche/proofcomparator.h --- a/src/avalanche/proofcomparator.h +++ b/src/avalanche/proofcomparator.h @@ -24,8 +24,7 @@ : lhs.getId() < rhs.getId(); } - bool operator()(const std::shared_ptr &lhs, - const std::shared_ptr &rhs) const { + bool operator()(const ProofRef &lhs, const ProofRef &rhs) const { return (*this)(*lhs, *rhs); } }; 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,8 +19,7 @@ BOOST_FIXTURE_TEST_SUITE(delegation_tests, TestingSetup) -static void CheckDelegation(const Delegation &dg, - const std::shared_ptr &p, +static void CheckDelegation(const Delegation &dg, const ProofRef &p, const CPubKey &expected_pubkey) { DelegationState state; CPubKey pubkey; diff --git a/src/avalanche/test/orphanproofpool_tests.cpp b/src/avalanche/test/orphanproofpool_tests.cpp --- a/src/avalanche/test/orphanproofpool_tests.cpp +++ b/src/avalanche/test/orphanproofpool_tests.cpp @@ -19,7 +19,7 @@ BOOST_FIXTURE_TEST_SUITE(orphanproofpool_tests, TestingSetup) /** Make a proof with stakes using random txids */ -static std::shared_ptr makeProof(const size_t nStakes) { +static ProofRef makeProof(const size_t nStakes) { const Amount v = 5 * COIN; const int height = 1234; ProofBuilder pb(0, 0, 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 @@ -643,7 +643,7 @@ // HEIGHT_MISMATCH BOOST_CHECK(pm.isOrphan(proof3->getId())); - const auto isGoodPeer = [&pm](const std::shared_ptr &p) { + const auto isGoodPeer = [&pm](const ProofRef &p) { bool ret = false; pm.forEachPeer([&](const Peer &peer) { if (p->getId() == peer.proof->getId()) { @@ -761,7 +761,7 @@ constexpr int numProofs = 10; - std::vector> proofs; + std::vector proofs; proofs.reserve(numProofs); for (int i = 0; i < numProofs; i++) { proofs.push_back(buildRandomProof(MIN_VALID_PROOF_SCORE)); 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 @@ -119,7 +119,7 @@ } size_t next_coinbase = 0; - std::shared_ptr GetProof() { + ProofRef GetProof() { size_t current_coinbase = next_coinbase++; const CTransaction &coinbase = *m_coinbase_txns[current_coinbase]; ProofBuilder pb(0, 0, masterpriv); @@ -245,9 +245,9 @@ ProofProvider(AvalancheTestingSetup *_fixture) : fixture(_fixture), invType(MSG_AVA_PROOF) {} - std::shared_ptr buildVoteItem() const { return fixture->GetProof(); } + ProofRef buildVoteItem() const { return fixture->GetProof(); } - uint256 getVoteItemId(const std::shared_ptr &proof) const { + uint256 getVoteItemId(const ProofRef &proof) const { return proof->getId(); } @@ -263,14 +263,13 @@ return registerVotes(nodeid, response, error); } - bool addToReconcile(const std::shared_ptr &proof) { + bool addToReconcile(const ProofRef &proof) { fixture->m_processor->addProofToReconcile(proof, true); return true; } - std::vector - buildVotesForItems(uint32_t error, - std::vector> &&items) { + std::vector buildVotesForItems(uint32_t error, + std::vector &&items) { size_t numItems = items.size(); std::vector votes; 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,11 +15,10 @@ constexpr uint32_t MIN_VALID_PROOF_SCORE = 100 * PROOF_DUST_THRESHOLD / COIN; -std::shared_ptr -buildRandomProof(uint32_t score, - const CKey &masterKey = CKey::MakeCompressedKey()); +ProofRef buildRandomProof(uint32_t score, + const CKey &masterKey = CKey::MakeCompressedKey()); -bool hasDustStake(const std::shared_ptr &proof); +bool hasDustStake(const ProofRef &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 { -std::shared_ptr buildRandomProof(uint32_t score, const CKey &masterKey) { +ProofRef buildRandomProof(uint32_t score, const CKey &masterKey) { auto key = CKey::MakeCompressedKey(); const COutPoint o(TxId(GetRandHash()), 0); @@ -38,7 +38,7 @@ return std::make_shared(pb.build()); } -bool hasDustStake(const std::shared_ptr &proof) { +bool hasDustStake(const ProofRef &proof) { for (const SignedStake &s : proof->getStakes()) { if (s.getStake().getAmount() < PROOF_DUST_THRESHOLD) { return true; diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2092,10 +2092,10 @@ //! Determine whether or not a peer can request a proof, and return it (or //! nullptr if not found or not allowed). -static std::shared_ptr +static avalanche::ProofRef FindProofForGetData(const CNode &peer, const avalanche::ProofId &proofid, const std::chrono::seconds now) { - std::shared_ptr proof = nullptr; + avalanche::ProofRef proof = nullptr; bool send_unconditionally = g_avalanche->withPeerManager([&](const avalanche::PeerManager &pm) { diff --git a/src/rpc/avalanche.cpp b/src/rpc/avalanche.cpp --- a/src/rpc/avalanche.cpp +++ b/src/rpc/avalanche.cpp @@ -53,7 +53,7 @@ return HexToPubKey(keyHex); } -static bool registerProofIfNeeded(std::shared_ptr proof) { +static bool registerProofIfNeeded(avalanche::ProofRef proof) { return g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { return pm.getProof(proof->getId()) || pm.registerProof(std::move(proof));