Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.h
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | private: | ||||
// Historical record of votes. | // Historical record of votes. | ||||
uint8_t votes = 0; | uint8_t votes = 0; | ||||
// Each bit indicate if the vote is to be considered. | // Each bit indicate if the vote is to be considered. | ||||
uint8_t consider = 0; | uint8_t consider = 0; | ||||
// How many in flight requests exists for this element. | // How many in flight requests exists for this element. | ||||
mutable std::atomic<uint8_t> inflight{0}; | mutable std::atomic<uint8_t> inflight{0}; | ||||
// Seed for pseudorandom operations. | |||||
const uint32_t seed = 0; | |||||
// Track how many successful votes occured. | // Track how many successful votes occured. | ||||
uint32_t successfulVotes = 0; | uint32_t successfulVotes = 0; | ||||
// Track the nodes which are part of the quorum. | |||||
std::array<uint16_t, 8> nodeFilter{{0, 0, 0, 0, 0, 0, 0, 0}}; | |||||
public: | public: | ||||
VoteRecord(bool accepted) : confidence(accepted) {} | VoteRecord(bool accepted) : confidence(accepted) {} | ||||
/** | /** | ||||
* Copy semantic | * Copy semantic | ||||
*/ | */ | ||||
VoteRecord(const VoteRecord &other) | VoteRecord(const VoteRecord &other) | ||||
: confidence(other.confidence), votes(other.votes), | : confidence(other.confidence), votes(other.votes), | ||||
consider(other.consider), inflight(other.inflight.load()), | consider(other.consider), inflight(other.inflight.load()), | ||||
successfulVotes(other.successfulVotes) {} | successfulVotes(other.successfulVotes), nodeFilter(other.nodeFilter) { | ||||
} | |||||
/** | /** | ||||
* Vote accounting facilities. | * Vote accounting facilities. | ||||
*/ | */ | ||||
bool isAccepted() const { return confidence & 0x01; } | bool isAccepted() const { return confidence & 0x01; } | ||||
uint16_t getConfidence() const { return confidence >> 1; } | uint16_t getConfidence() const { return confidence >> 1; } | ||||
bool hasFinalized() const { | bool hasFinalized() const { | ||||
return getConfidence() >= AVALANCHE_FINALIZATION_SCORE; | return getConfidence() >= AVALANCHE_FINALIZATION_SCORE; | ||||
} | } | ||||
/** | /** | ||||
* Register a new vote for an item and update confidence accordingly. | * Register a new vote for an item and update confidence accordingly. | ||||
* Returns true if the acceptance or finalization state changed. | * Returns true if the acceptance or finalization state changed. | ||||
*/ | */ | ||||
bool registerVote(uint32_t error); | bool registerVote(NodeId nodeid, uint32_t error); | ||||
/** | /** | ||||
* Register that a request is being made regarding that item. | * Register that a request is being made regarding that item. | ||||
* The method is made const so that it can be accessed via a read only view | * The method is made const so that it can be accessed via a read only view | ||||
* of vote_records. It's not a problem as it is made thread safe. | * of vote_records. It's not a problem as it is made thread safe. | ||||
*/ | */ | ||||
bool registerPoll() const; | bool registerPoll() const; | ||||
/** | /** | ||||
* Return if this item is in condition to be polled at the moment. | * Return if this item is in condition to be polled at the moment. | ||||
*/ | */ | ||||
bool shouldPoll() const { return inflight < AVALANCHE_MAX_INFLIGHT_POLL; } | bool shouldPoll() const { return inflight < AVALANCHE_MAX_INFLIGHT_POLL; } | ||||
/** | /** | ||||
* Clear `count` inflight requests. | * Clear `count` inflight requests. | ||||
*/ | */ | ||||
void clearInflightRequest(uint8_t count = 1) { inflight -= count; } | void clearInflightRequest(uint8_t count = 1) { inflight -= count; } | ||||
private: | |||||
/** | |||||
* Add the node to the quorum. | |||||
* Returns true if the node was added, false if the node already was in the | |||||
* quorum. | |||||
*/ | |||||
bool addNodeToQuorum(NodeId nodeid); | |||||
}; | }; | ||||
class AvalancheVote { | class AvalancheVote { | ||||
uint32_t error; | uint32_t error; | ||||
uint256 hash; | uint256 hash; | ||||
public: | public: | ||||
AvalancheVote() : error(-1), hash() {} | AvalancheVote() : error(-1), hash() {} | ||||
▲ Show 20 Lines • Show All 217 Lines • Show Last 20 Lines |