Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
Show First 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | public: | ||||
bool follows(uint64_t slot) const { return getStart() > slot; } | bool follows(uint64_t slot) const { return getStart() > slot; } | ||||
}; | }; | ||||
struct Peer { | struct Peer { | ||||
PeerId peerid; | PeerId peerid; | ||||
uint32_t index = -1; | uint32_t index = -1; | ||||
uint32_t node_count = 0; | uint32_t node_count = 0; | ||||
std::shared_ptr<Proof> proof; | ProofRef proof; | ||||
// The network stack uses timestamp in seconds, so we oblige. | // The network stack uses timestamp in seconds, so we oblige. | ||||
std::chrono::seconds registration_time; | std::chrono::seconds registration_time; | ||||
Peer(PeerId peerid_, std::shared_ptr<Proof> proof_) | Peer(PeerId peerid_, ProofRef proof_) | ||||
: peerid(peerid_), proof(std::move(proof_)), | : peerid(peerid_), proof(std::move(proof_)), | ||||
registration_time(GetTime<std::chrono::seconds>()) {} | registration_time(GetTime<std::chrono::seconds>()) {} | ||||
const ProofId &getProofId() const { return proof->getId(); } | const ProofId &getProofId() const { return proof->getId(); } | ||||
uint32_t getScore() const { return proof->getScore(); } | uint32_t getScore() const { return proof->getScore(); } | ||||
}; | }; | ||||
struct proof_index { | struct proof_index { | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | void forEachNode(const Peer &peer, Callable &&func) const { | ||||
for (auto it = range.first; it != range.second; ++it) { | for (auto it = range.first; it != range.second; ++it) { | ||||
func(*it); | func(*it); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Proof and Peer related API. | * Proof and Peer related API. | ||||
*/ | */ | ||||
bool registerProof(const std::shared_ptr<Proof> &proof); | bool registerProof(const ProofRef &proof); | ||||
bool exists(const ProofId &proofid) const { | bool exists(const ProofId &proofid) const { | ||||
return getProof(proofid) != nullptr; | return getProof(proofid) != nullptr; | ||||
} | } | ||||
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); | ||||
Show All 20 Lines | public: | ||||
/**************************************************** | /**************************************************** | ||||
* 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 ProofRef &proof); | ||||
/** | /** | ||||
* Remove an existing peer. | * Remove an existing peer. | ||||
*/ | */ | ||||
bool removePeer(const PeerId peerid); | bool removePeer(const PeerId peerid); | ||||
/** | /** | ||||
* Randomly select a peer to poll. | * Randomly select a peer to poll. | ||||
Show All 10 Lines | public: | ||||
* Perform consistency check on internal data structures. | * Perform consistency check on internal data structures. | ||||
*/ | */ | ||||
bool verify() const; | bool verify() const; | ||||
// Accessors. | // Accessors. | ||||
uint64_t getSlotCount() const { return slotCount; } | uint64_t getSlotCount() const { return slotCount; } | ||||
uint64_t getFragmentation() const { return fragmentation; } | uint64_t getFragmentation() const { return fragmentation; } | ||||
std::shared_ptr<Proof> getProof(const ProofId &proofid) const; | ProofRef getProof(const ProofId &proofid) const; | ||||
bool isOrphan(const ProofId &id) const; | bool isOrphan(const ProofId &id) const; | ||||
std::shared_ptr<Proof> getOrphan(const ProofId &id) const; | ProofRef getOrphan(const ProofId &id) const; | ||||
private: | private: | ||||
PeerSet::iterator fetchOrCreatePeer(const std::shared_ptr<Proof> &proof); | PeerSet::iterator fetchOrCreatePeer(const ProofRef &proof); | ||||
bool addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid); | bool addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid); | ||||
bool addNodeToPeer(const PeerSet::iterator &it); | bool addNodeToPeer(const PeerSet::iterator &it); | ||||
bool removeNodeFromPeer(const PeerSet::iterator &it, uint32_t count = 1); | bool removeNodeFromPeer(const PeerSet::iterator &it, uint32_t count = 1); | ||||
friend struct ::avalanche::TestPeerManager; | friend struct ::avalanche::TestPeerManager; | ||||
}; | }; | ||||
/** | /** | ||||
* This is an internal method that is exposed for testing purposes. | * This is an internal method that is exposed for testing purposes. | ||||
*/ | */ | ||||
PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | ||||
const uint64_t max); | const uint64_t max); | ||||
} // namespace avalanche | } // namespace avalanche | ||||
#endif // BITCOIN_AVALANCHE_PEERMANAGER_H | #endif // BITCOIN_AVALANCHE_PEERMANAGER_H |