Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.h
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | static uint32_t countBits(uint32_t value) { | ||||
// Clear the rightmost bit set. | // Clear the rightmost bit set. | ||||
value &= (value - 1); | value &= (value - 1); | ||||
} | } | ||||
return count; | return count; | ||||
} | } | ||||
public: | public: | ||||
VoteRecord() : votes(0xaaaa), confidence(0) {} | VoteRecord(bool accepted) : votes(0xaaaa), confidence(accepted) {} | ||||
bool isValid() 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; | ||||
} | } | ||||
bool registerVote(bool vote) { | bool registerVote(bool vote) { | ||||
votes = (votes << 1) | vote; | votes = (votes << 1) | vote; | ||||
auto bits = countBits(votes & 0xff); | auto bits = countBits(votes & 0xff); | ||||
bool yes = bits > 6; | bool yes = bits > 6; | ||||
bool no = bits < 2; | bool no = bits < 2; | ||||
if (!yes && !no) { | if (!yes && !no) { | ||||
// The vote is inconclusive. | // The vote is inconclusive. | ||||
return false; | return false; | ||||
} | } | ||||
if (isValid() == yes) { | if (isAccepted() == yes) { | ||||
// If the vote is in agreement with our internal status, increase | // If the vote is in agreement with our internal status, increase | ||||
// confidence. | // confidence. | ||||
confidence += 2; | confidence += 2; | ||||
} else { | } else { | ||||
// The vote did not agree with our internal state, in that case, | // The vote did not agree with our internal state, in that case, | ||||
// reset confidence. | // reset confidence. | ||||
confidence = yes; | confidence = yes; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 139 Lines • Show Last 20 Lines |