Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_AVALANCHE_PEERMANAGER_H | #ifndef BITCOIN_AVALANCHE_PEERMANAGER_H | ||||
#define BITCOIN_AVALANCHE_PEERMANAGER_H | #define BITCOIN_AVALANCHE_PEERMANAGER_H | ||||
#include <avalanche/node.h> | #include <avalanche/node.h> | ||||
#include <avalanche/orphanproofpool.h> | #include <avalanche/orphanproofpool.h> | ||||
#include <avalanche/proof.h> | #include <avalanche/proof.h> | ||||
#include <avalanche/voterecord.h> | |||||
#include <coins.h> | #include <coins.h> | ||||
#include <pubkey.h> | #include <pubkey.h> | ||||
#include <rwcollection.h> | |||||
#include <salteduint256hasher.h> | #include <salteduint256hasher.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <boost/multi_index/composite_key.hpp> | #include <boost/multi_index/composite_key.hpp> | ||||
#include <boost/multi_index/hashed_index.hpp> | #include <boost/multi_index/hashed_index.hpp> | ||||
#include <boost/multi_index/member.hpp> | #include <boost/multi_index/member.hpp> | ||||
#include <boost/multi_index/ordered_index.hpp> | #include <boost/multi_index/ordered_index.hpp> | ||||
#include <boost/multi_index_container.hpp> | #include <boost/multi_index_container.hpp> | ||||
Show All 9 Lines | |||||
/** | /** | ||||
* Maximum number of stakes in the orphanProofs. | * Maximum number of stakes in the orphanProofs. | ||||
* Benchmarking on a consumer grade computer shows that 10000 stakes can be | * Benchmarking on a consumer grade computer shows that 10000 stakes can be | ||||
* verified in less than 1 second. | * verified in less than 1 second. | ||||
*/ | */ | ||||
static constexpr size_t AVALANCHE_ORPHANPROOFPOOL_SIZE = 10000; | static constexpr size_t AVALANCHE_ORPHANPROOFPOOL_SIZE = 10000; | ||||
class Delegation; | class Delegation; | ||||
struct VoteRecord; | |||||
namespace { | namespace { | ||||
struct TestPeerManager; | struct TestPeerManager; | ||||
} | } | ||||
struct Slot { | struct Slot { | ||||
private: | private: | ||||
uint64_t start; | uint64_t start; | ||||
▲ Show 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | class PeerManager { | ||||
*/ | */ | ||||
OrphanProofPool orphanProofs{AVALANCHE_ORPHANPROOFPOOL_SIZE}; | OrphanProofPool orphanProofs{AVALANCHE_ORPHANPROOFPOOL_SIZE}; | ||||
/** | /** | ||||
* Track proof ids to broadcast | * Track proof ids to broadcast | ||||
*/ | */ | ||||
std::unordered_set<ProofId, SaltedProofIdHasher> m_unbroadcast_proofids; | std::unordered_set<ProofId, SaltedProofIdHasher> m_unbroadcast_proofids; | ||||
struct ProofSharedPointerComparator { | |||||
bool operator()(const std::shared_ptr<Proof> &lhs, | |||||
const std::shared_ptr<Proof> &rhs) const; | |||||
}; | |||||
using ProofVoteMap = std::map<const std::shared_ptr<Proof>, VoteRecord, | |||||
ProofSharedPointerComparator>; | |||||
/** | |||||
* Proofs to run avalanche on. | |||||
*/ | |||||
RWCollection<ProofVoteMap> conflicting_proofs; | |||||
deadalnix: This is a change in the general locking model of the thread manager. Why? | |||||
public: | public: | ||||
/** | /** | ||||
* Node API. | * Node API. | ||||
*/ | */ | ||||
bool addNode(NodeId nodeid, const ProofId &proofid); | bool addNode(NodeId nodeid, const ProofId &proofid); | ||||
bool removeNode(NodeId nodeid); | bool removeNode(NodeId nodeid); | ||||
// Update when a node is to be polled next. | // Update when a node is to be polled next. | ||||
Show All 27 Lines | public: | ||||
template <typename Callable> | template <typename Callable> | ||||
bool forPeer(const ProofId &proofid, Callable &&func) const { | bool forPeer(const ProofId &proofid, Callable &&func) const { | ||||
auto &pview = peers.get<proof_index>(); | auto &pview = peers.get<proof_index>(); | ||||
auto it = pview.find(proofid); | auto it = pview.find(proofid); | ||||
return it != pview.end() && func(*it); | return it != pview.end() && func(*it); | ||||
} | } | ||||
template <typename Callable> | |||||
bool forPeer(const PeerId &peerid, Callable &&func) const { | |||||
auto it = peers.find(peerid); | |||||
return it != peers.end() && func(*it); | |||||
} | |||||
template <typename Callable> void forEachPeer(Callable &&func) const { | template <typename Callable> void forEachPeer(Callable &&func) const { | ||||
for (const auto &p : peers) { | for (const auto &p : peers) { | ||||
func(p); | func(p); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Update the peer set when a new block is connected. | * Update the peer set when a new block is connected. | ||||
*/ | */ | ||||
void updatedBlockTip(); | void updatedBlockTip(); | ||||
/** | /** | ||||
* Proof broadcast API. | * Proof broadcast API. | ||||
*/ | */ | ||||
void addUnbroadcastProof(const ProofId &proofid); | void addUnbroadcastProof(const ProofId &proofid); | ||||
void removeUnbroadcastProof(const ProofId &proofid); | void removeUnbroadcastProof(const ProofId &proofid); | ||||
auto getUnbroadcastProofs() const { return m_unbroadcast_proofids; } | auto getUnbroadcastProofs() const { return m_unbroadcast_proofids; } | ||||
/** | |||||
* Conflicting proofs API | |||||
*/ | |||||
auto getConflictingProofs() const { | |||||
return conflicting_proofs.getReadView(); | |||||
} | |||||
/**************************************************** | /**************************************************** | ||||
* Functions which are public for testing purposes. * | * Functions which are public for testing purposes. * | ||||
****************************************************/ | ****************************************************/ | ||||
/** | /** | ||||
* Provide the PeerId associated with the given proof. If the peer does not | * Provide the PeerId associated with the given proof. If the peer does not | ||||
* exist, then it is created. | * exist, then it is created. | ||||
*/ | */ | ||||
PeerId getPeerId(const std::shared_ptr<Proof> &proof); | PeerId getPeerId(const std::shared_ptr<Proof> &proof); | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
This is a change in the general locking model of the thread manager. Why?