Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
Show First 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | |||||
namespace bmi = boost::multi_index; | namespace bmi = boost::multi_index; | ||||
class PeerManager { | class PeerManager { | ||||
std::vector<Slot> slots; | std::vector<Slot> slots; | ||||
uint64_t slotCount = 0; | uint64_t slotCount = 0; | ||||
uint64_t fragmentation = 0; | uint64_t fragmentation = 0; | ||||
OrphanProofPool orphanProofs{AVALANCHE_ORPHANPROOFPOOL_SIZE}; | |||||
/** | |||||
* Track proof ids to broadcast | |||||
*/ | |||||
std::unordered_set<ProofId, SaltedProofIdHasher> m_unbroadcast_proofids; | |||||
/** | /** | ||||
* Several nodes can make an avalanche peer. In this case, all nodes are | * Several nodes can make an avalanche peer. In this case, all nodes are | ||||
* considered interchangeable parts of the same peer. | * considered interchangeable parts of the same peer. | ||||
*/ | */ | ||||
using PeerSet = boost::multi_index_container< | using PeerSet = boost::multi_index_container< | ||||
Peer, bmi::indexed_by< | Peer, bmi::indexed_by< | ||||
// index by peerid | // index by peerid | ||||
bmi::hashed_unique<bmi::member<Peer, PeerId, &Peer::peerid>>, | bmi::hashed_unique<bmi::member<Peer, PeerId, &Peer::peerid>>, | ||||
Show All 18 Lines | using NodeSet = boost::multi_index_container< | ||||
Node, bmi::member<Node, PeerId, &Node::peerid>, | Node, bmi::member<Node, PeerId, &Node::peerid>, | ||||
bmi::member<Node, TimePoint, &Node::nextRequestTime>>>>>; | bmi::member<Node, TimePoint, &Node::nextRequestTime>>>>>; | ||||
NodeSet nodes; | NodeSet nodes; | ||||
static constexpr int SELECT_PEER_MAX_RETRY = 3; | static constexpr int SELECT_PEER_MAX_RETRY = 3; | ||||
static constexpr int SELECT_NODE_MAX_RETRY = 3; | static constexpr int SELECT_NODE_MAX_RETRY = 3; | ||||
/** | |||||
* Tracks proof which for which the UTXO are unavailable. | |||||
*/ | |||||
OrphanProofPool orphanProofs{AVALANCHE_ORPHANPROOFPOOL_SIZE}; | |||||
/** | |||||
* Track proof ids to broadcast | |||||
*/ | |||||
std::unordered_set<ProofId, SaltedProofIdHasher> m_unbroadcast_proofids; | |||||
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); | ||||
bool forNode(NodeId nodeid, std::function<bool(const Node &n)> func) const; | bool forNode(NodeId nodeid, std::function<bool(const Node &n)> func) const; | ||||
bool updateNextRequestTime(NodeId nodeid, TimePoint timeout); | bool updateNextRequestTime(NodeId nodeid, TimePoint timeout); | ||||
/** | // Randomly select a node to poll. | ||||
* Randomly select a node to poll. | |||||
*/ | |||||
NodeId selectNode(); | NodeId selectNode(); | ||||
/** | /** | ||||
* 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. | |||||
*/ | |||||
void addUnbroadcastProof(const ProofId &proofid); | |||||
void removeUnbroadcastProof(const ProofId &proofid); | |||||
void broadcastProofs(const CConnman &connman); | |||||
/**************************************************** | /**************************************************** | ||||
* 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 All 27 Lines | public: | ||||
std::vector<NodeId> getNodeIdsForPeer(PeerId peerId) const; | std::vector<NodeId> getNodeIdsForPeer(PeerId peerId) const; | ||||
std::shared_ptr<Proof> getProof(const ProofId &proofid) const; | std::shared_ptr<Proof> getProof(const ProofId &proofid) const; | ||||
std::chrono::seconds getProofRegistrationTime(const ProofId &proofid) const; | std::chrono::seconds getProofRegistrationTime(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; | std::shared_ptr<Proof> getOrphan(const ProofId &id) const; | ||||
void addUnbroadcastProof(const ProofId &proofid); | |||||
void removeUnbroadcastProof(const ProofId &proofid); | |||||
void broadcastProofs(const CConnman &connman); | |||||
private: | private: | ||||
PeerSet::iterator fetchOrCreatePeer(const std::shared_ptr<Proof> &proof); | PeerSet::iterator fetchOrCreatePeer(const std::shared_ptr<Proof> &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); | ||||
}; | }; | ||||
/** | /** | ||||
* 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 |