Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.h
Show All 27 Lines | |||||
class CBlockIndex; | class CBlockIndex; | ||||
class CScheduler; | class CScheduler; | ||||
namespace { | namespace { | ||||
/** | /** | ||||
* Finalization score. | * Finalization score. | ||||
*/ | */ | ||||
static int AVALANCHE_FINALIZATION_SCORE = 128; | static int AVALANCHE_FINALIZATION_SCORE = 128; | ||||
/** | |||||
* Special NodeId that represent no node. | |||||
*/ | |||||
static const NodeId NO_NODE = -1; | |||||
} | } | ||||
/** | /** | ||||
* Vote history. | * Vote history. | ||||
*/ | */ | ||||
struct VoteRecord { | struct VoteRecord { | ||||
private: | private: | ||||
// Historical record of votes. | // Historical record of votes. | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | public: | ||||
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> | typedef std::map<const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator> | ||||
BlockVoteMap; | BlockVoteMap; | ||||
typedef std::map<std::chrono::time_point<std::chrono::steady_clock>, NodeId> | |||||
NodeCooldownMap; | |||||
struct next_request_time {}; | |||||
struct query_timeout {}; | struct query_timeout {}; | ||||
class AvalancheProcessor { | class AvalancheProcessor { | ||||
private: | private: | ||||
CConnman *connman; | CConnman *connman; | ||||
/** | /** | ||||
* 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; | |||||
typedef std::chrono::time_point<std::chrono::steady_clock> TimePoint; | typedef std::chrono::time_point<std::chrono::steady_clock> TimePoint; | ||||
std::atomic<uint64_t> round; | struct Peer { | ||||
RWCollection<std::set<NodeId>> nodeids; | NodeId nodeid; | ||||
RWCollection<NodeCooldownMap> nodecooldown; | int64_t score; | ||||
TimePoint nextRequestTime; | |||||
}; | |||||
typedef boost::multi_index_container< | |||||
Peer, boost::multi_index::indexed_by< | |||||
// index by nodeid | |||||
boost::multi_index::hashed_unique< | |||||
boost::multi_index::member<Peer, NodeId, &Peer::nodeid>>, | |||||
// sorted by nextRequestTime | |||||
boost::multi_index::ordered_non_unique< | |||||
boost::multi_index::tag<next_request_time>, | |||||
boost::multi_index::member<Peer, TimePoint, | |||||
&Peer::nextRequestTime>>>> | |||||
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. | ||||
Show All 37 Lines | public: | ||||
~AvalancheProcessor() { stopEventLoop(); } | ~AvalancheProcessor() { stopEventLoop(); } | ||||
bool addBlockToReconcile(const CBlockIndex *pindex); | bool addBlockToReconcile(const CBlockIndex *pindex); | ||||
bool isAccepted(const CBlockIndex *pindex) const; | bool isAccepted(const CBlockIndex *pindex) const; | ||||
bool registerVotes(NodeId nodeid, const AvalancheResponse &response, | bool registerVotes(NodeId nodeid, const AvalancheResponse &response, | ||||
std::vector<AvalancheBlockUpdate> &updates); | std::vector<AvalancheBlockUpdate> &updates); | ||||
bool addPeer(NodeId nodeid, uint32_t score); | |||||
bool startEventLoop(CScheduler &scheduler); | bool startEventLoop(CScheduler &scheduler); | ||||
bool stopEventLoop(); | bool stopEventLoop(); | ||||
private: | private: | ||||
void runEventLoop(); | void runEventLoop(); | ||||
std::vector<CInv> getInvsForNextPoll() const; | std::vector<CInv> getInvsForNextPoll() const; | ||||
NodeId getSuitableNodeToQuery(); | NodeId getSuitableNodeToQuery(); | ||||
friend struct AvalancheTest; | friend struct AvalancheTest; | ||||
}; | }; | ||||
#endif // BITCOIN_AVALANCHE_H | #endif // BITCOIN_AVALANCHE_H |