Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.h
Show First 20 Lines • Show All 241 Lines • ▼ Show 20 Lines | CBlockIndex *getBlockIndex() { | ||||
return reinterpret_cast<CBlockIndex *>(raw & ~MASK); | return reinterpret_cast<CBlockIndex *>(raw & ~MASK); | ||||
} | } | ||||
const CBlockIndex *getBlockIndex() const { | const CBlockIndex *getBlockIndex() const { | ||||
return const_cast<AvalancheBlockUpdate *>(this)->getBlockIndex(); | return const_cast<AvalancheBlockUpdate *>(this)->getBlockIndex(); | ||||
} | } | ||||
}; | }; | ||||
typedef std::map<const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator> | using BlockVoteMap = | ||||
BlockVoteMap; | std::map<const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator>; | ||||
struct next_request_time {}; | struct next_request_time {}; | ||||
struct query_timeout {}; | struct query_timeout {}; | ||||
class AvalancheProcessor { | class AvalancheProcessor { | ||||
private: | private: | ||||
CConnman *connman; | CConnman *connman; | ||||
std::chrono::milliseconds queryTimeoutDuration; | std::chrono::milliseconds queryTimeoutDuration; | ||||
/** | /** | ||||
* Blocks to run avalanche on. | * Blocks to run avalanche on. | ||||
*/ | */ | ||||
RWCollection<BlockVoteMap> vote_records; | RWCollection<BlockVoteMap> vote_records; | ||||
/** | /** | ||||
* Keep track of peers and queries sent. | * Keep track of peers and queries sent. | ||||
*/ | */ | ||||
std::atomic<uint64_t> round; | std::atomic<uint64_t> round; | ||||
typedef std::chrono::time_point<std::chrono::steady_clock> TimePoint; | using TimePoint = std::chrono::time_point<std::chrono::steady_clock>; | ||||
struct Peer { | struct Peer { | ||||
NodeId nodeid; | NodeId nodeid; | ||||
int64_t score; | int64_t score; | ||||
TimePoint nextRequestTime; | TimePoint nextRequestTime; | ||||
CPubKey pubkey; | CPubKey pubkey; | ||||
}; | }; | ||||
typedef boost::multi_index_container< | using PeerSet = boost::multi_index_container< | ||||
Peer, boost::multi_index::indexed_by< | Peer, 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<Peer, NodeId, &Peer::nodeid>>, | boost::multi_index::member<Peer, NodeId, &Peer::nodeid>>, | ||||
// sorted by nextRequestTime | // sorted by 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::member<Peer, TimePoint, | boost::multi_index::member<Peer, TimePoint, | ||||
&Peer::nextRequestTime>>>> | &Peer::nextRequestTime>>>>; | ||||
PeerSet; | |||||
RWCollection<PeerSet> peerSet; | RWCollection<PeerSet> peerSet; | ||||
struct Query { | struct Query { | ||||
NodeId nodeid; | NodeId nodeid; | ||||
uint64_t round; | uint64_t round; | ||||
TimePoint timeout; | TimePoint timeout; | ||||
/** | /** | ||||
* We declare this as mutable so it can be modified in the multi_index. | * We declare this as mutable so it can be modified in the multi_index. | ||||
* This is ok because we do not use this field to index in anyway. | * This is ok because we do not use this field to index in anyway. | ||||
* | * | ||||
* /!\ Do not use any mutable field as index. | * /!\ Do not use any mutable field as index. | ||||
*/ | */ | ||||
mutable std::vector<CInv> invs; | mutable std::vector<CInv> invs; | ||||
}; | }; | ||||
typedef boost::multi_index_container< | using QuerySet = boost::multi_index_container< | ||||
Query, | Query, | ||||
boost::multi_index::indexed_by< | boost::multi_index::indexed_by< | ||||
// index by nodeid/round | // index by nodeid/round | ||||
boost::multi_index::ordered_unique< | boost::multi_index::ordered_unique< | ||||
boost::multi_index::composite_key< | boost::multi_index::composite_key< | ||||
Query, | Query, | ||||
boost::multi_index::member<Query, NodeId, &Query::nodeid>, | boost::multi_index::member<Query, NodeId, &Query::nodeid>, | ||||
boost::multi_index::member<Query, uint64_t, | boost::multi_index::member<Query, uint64_t, | ||||
&Query::round>>>, | &Query::round>>>, | ||||
// sorted by timeout | // sorted by timeout | ||||
boost::multi_index::ordered_non_unique< | boost::multi_index::ordered_non_unique< | ||||
boost::multi_index::tag<query_timeout>, | boost::multi_index::tag<query_timeout>, | ||||
boost::multi_index::member<Query, TimePoint, &Query::timeout>>>> | boost::multi_index::member<Query, TimePoint, | ||||
QuerySet; | &Query::timeout>>>>; | ||||
RWCollection<QuerySet> queries; | RWCollection<QuerySet> queries; | ||||
/** | /** | ||||
* Start stop machinery. | * Start stop machinery. | ||||
*/ | */ | ||||
std::atomic<bool> stopRequest; | std::atomic<bool> stopRequest; | ||||
bool running GUARDED_BY(cs_running); | bool running GUARDED_BY(cs_running); | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |