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 <net.h> | #include <net.h> | ||||
#include <pubkey.h> | #include <pubkey.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> | ||||
#include <chrono> | #include <chrono> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
#include <vector> | #include <vector> | ||||
using PeerId = uint32_t; | |||||
static constexpr PeerId NO_PEER = -1; | |||||
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 37 Lines | struct Peer { | ||||
uint32_t index; | uint32_t index; | ||||
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 TimePoint = std::chrono::time_point<std::chrono::steady_clock>; | |||||
struct Node { | |||||
NodeId nodeid; | |||||
PeerId peerid; | |||||
TimePoint nextRequestTime; | |||||
CPubKey pubkey; | |||||
Node(NodeId nodeid_, PeerId peerid_, CPubKey pubkey_) | |||||
: nodeid(nodeid_), peerid(peerid_), | |||||
nextRequestTime(std::chrono::steady_clock::now()), | |||||
pubkey(std::move(pubkey_)) {} | |||||
}; | |||||
using NodeSet = boost::multi_index_container< | using NodeSet = boost::multi_index_container< | ||||
Node, | AvalancheNode, | ||||
boost::multi_index::indexed_by< | boost::multi_index::indexed_by< | ||||
// index by nodeid | // index by nodeid | ||||
boost::multi_index::hashed_unique< | boost::multi_index::hashed_unique<boost::multi_index::member< | ||||
boost::multi_index::member<Node, NodeId, &Node::nodeid>>, | AvalancheNode, NodeId, &AvalancheNode::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< | ||||
Node, | AvalancheNode, | ||||
boost::multi_index::member<Node, PeerId, &Node::peerid>, | boost::multi_index::member<AvalancheNode, PeerId, | ||||
boost::multi_index::member<Node, TimePoint, | &AvalancheNode::peerid>, | ||||
&Node::nextRequestTime>>>>>; | boost::multi_index::member< | ||||
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: | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |