diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -498,6 +498,7 @@ addrman.cpp avalanche/peermanager.cpp avalanche/processor.cpp + avalanche/proof.cpp banman.cpp blockencodings.cpp blockfilter.cpp diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -299,6 +299,7 @@ addrman.cpp \ avalanche/peermanager.cpp \ avalanche/processor.cpp \ + avalanche/proof.cpp \ banman.cpp \ blockencodings.cpp \ blockfilter.cpp \ diff --git a/src/avalanche/peermanager.h b/src/avalanche/peermanager.h --- a/src/avalanche/peermanager.h +++ b/src/avalanche/peermanager.h @@ -55,8 +55,23 @@ bool follows(uint64_t slot) const { return getStart() > slot; } }; -struct proof_index {}; -struct next_request_time {}; +struct Peer { + PeerId peerid; + + uint32_t score; + uint32_t index; + + Proof proof; + + Peer(PeerId peerid_, uint32_t index_, Proof proof_) + : peerid(peerid_), score(proof_.getScore()), index(index_), + proof(std::move(proof_)) {} +}; + +struct proof_index { + using result_type = ProofId; + result_type operator()(const Peer &p) const { return p.proof.getId(); } +}; class SaltedProofIdHasher : private SaltedUint256Hasher { public: @@ -65,6 +80,8 @@ size_t operator()(const ProofId &proofid) const { return hash(proofid); } }; +struct next_request_time {}; + class PeerManager { std::vector slots; uint64_t slotCount = 0; @@ -74,19 +91,6 @@ * Several nodes can make an avalanche peer. In this case, all nodes are * considered interchangeable parts of the same peer. */ - struct Peer { - PeerId peerid; - - uint32_t score; - uint32_t index; - - ProofId proofid; - - Peer(PeerId peerid_, uint32_t score_, uint32_t index_, ProofId proofid_) - : peerid(peerid_), score(score_), index(index_), - proofid(std::move(proofid_)) {} - }; - using PeerSet = boost::multi_index_container< Peer, boost::multi_index::indexed_by< // index by peerid @@ -94,8 +98,7 @@ boost::multi_index::member>, // index by proof boost::multi_index::hashed_unique< - boost::multi_index::tag, - boost::multi_index::member, + boost::multi_index::tag, proof_index, SaltedProofIdHasher>>>; PeerId nextPeerId = 0; diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -11,8 +11,7 @@ namespace avalanche { PeerId PeerManager::addPeer(PeerId p, uint32_t score) { - auto inserted = - peers.emplace(p, score, uint32_t(slots.size()), ProofId(GetRandHash())); + auto inserted = peers.emplace(p, uint32_t(slots.size()), Proof(score)); assert(inserted.second); const uint64_t start = slotCount; diff --git a/src/avalanche/proof.h b/src/avalanche/proof.h --- a/src/avalanche/proof.h +++ b/src/avalanche/proof.h @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2019 The Bitcoin developers +// Copyright (c) 2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -7,6 +7,8 @@ #include +#include + namespace avalanche { struct ProofId : public uint256 { @@ -14,6 +16,17 @@ explicit ProofId(const uint256 &b) : uint256(b) {} }; +class Proof { + ProofId proofid; + uint32_t score; + +public: + explicit Proof(uint32_t score_); + + const ProofId &getId() const { return proofid; } + uint32_t getScore() const { return score; } +}; + } // namespace avalanche #endif // BITCOIN_AVALANCHE_PROOF_H diff --git a/src/avalanche/proof.cpp b/src/avalanche/proof.cpp new file mode 100644 --- /dev/null +++ b/src/avalanche/proof.cpp @@ -0,0 +1,13 @@ +// Copyright (c) 2020 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +namespace avalanche { + +Proof::Proof(uint32_t score_) : proofid(GetRandHash()), score(score_) {} + +} // namespace avalanche