Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
Show All 14 Lines | |||||
#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> | ||||
#include <chrono> | #include <chrono> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
#include <vector> | #include <vector> | ||||
namespace avalanche { | |||||
struct Slot { | struct Slot { | ||||
private: | private: | ||||
uint64_t start; | uint64_t start; | ||||
uint32_t score; | uint32_t score; | ||||
PeerId peerid; | PeerId peerid; | ||||
public: | public: | ||||
Slot(uint64_t startIn, uint32_t scoreIn, PeerId peeridIn) | Slot(uint64_t startIn, uint32_t scoreIn, PeerId peeridIn) | ||||
Show All 38 Lines | struct Peer { | ||||
Peer(uint32_t score_, uint32_t index_) : score(score_), index(index_) {} | Peer(uint32_t score_, uint32_t index_) : score(score_), index(index_) {} | ||||
}; | }; | ||||
PeerId nextPeerId = 0; | PeerId nextPeerId = 0; | ||||
std::unordered_map<PeerId, Peer> peers; | std::unordered_map<PeerId, Peer> peers; | ||||
using NodeSet = boost::multi_index_container< | using NodeSet = boost::multi_index_container< | ||||
AvalancheNode, | Node, | ||||
boost::multi_index::indexed_by< | boost::multi_index::indexed_by< | ||||
// index by nodeid | // index by nodeid | ||||
boost::multi_index::hashed_unique<boost::multi_index::member< | boost::multi_index::hashed_unique< | ||||
AvalancheNode, NodeId, &AvalancheNode::nodeid>>, | boost::multi_index::member<Node, NodeId, &Node::nodeid>>, | ||||
// sorted by peerid/nextRequestTime | // sorted by peerid/nextRequestTime | ||||
boost::multi_index::ordered_non_unique< | boost::multi_index::ordered_non_unique< | ||||
boost::multi_index::tag<next_request_time>, | boost::multi_index::tag<next_request_time>, | ||||
boost::multi_index::composite_key< | boost::multi_index::composite_key< | ||||
AvalancheNode, | Node, | ||||
boost::multi_index::member<AvalancheNode, PeerId, | boost::multi_index::member<Node, PeerId, &Node::peerid>, | ||||
&AvalancheNode::peerid>, | boost::multi_index::member<Node, TimePoint, | ||||
boost::multi_index::member< | &Node::nextRequestTime>>>>>; | ||||
AvalancheNode, TimePoint, | |||||
&AvalancheNode::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; | ||||
public: | public: | ||||
/** | /** | ||||
* Peer API. | * Peer API. | ||||
*/ | */ | ||||
PeerId addPeer(uint32_t score) { return addPeer(nextPeerId++, score); } | PeerId addPeer(uint32_t score) { return addPeer(nextPeerId++, score); } | ||||
bool removePeer(PeerId p); | bool removePeer(PeerId p); | ||||
bool rescorePeer(PeerId p, uint32_t score); | bool rescorePeer(PeerId p, uint32_t score); | ||||
/** | /** | ||||
* Node API. | * Node API. | ||||
*/ | */ | ||||
bool addNodeToPeer(PeerId peerid, NodeId nodeid, CPubKey pubkey); | bool addNodeToPeer(PeerId peerid, NodeId nodeid, CPubKey pubkey); | ||||
bool removeNode(NodeId nodeid); | bool removeNode(NodeId nodeid); | ||||
bool forNode(NodeId nodeid, | bool forNode(NodeId nodeid, std::function<bool(const Node &n)> func) const; | ||||
std::function<bool(const AvalancheNode &n)> func) const; | |||||
bool updateNextRequestTime(NodeId nodeid, TimePoint timeout); | bool updateNextRequestTime(NodeId nodeid, TimePoint timeout); | ||||
NodeId getSuitableNodeToQuery(); | NodeId getSuitableNodeToQuery(); | ||||
/** | /** | ||||
* Exposed for tests. | * Exposed for tests. | ||||
*/ | */ | ||||
Show All 20 Lines | |||||
}; | }; | ||||
/** | /** | ||||
* 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 | |||||
#endif // BITCOIN_AVALANCHE_PEERMANAGER_H | #endif // BITCOIN_AVALANCHE_PEERMANAGER_H |