Bitcoin ABC 0.34.2
P2P Digital Currency
voterecord.h
Go to the documentation of this file.
1// Copyright (c) 2021 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_AVALANCHE_VOTERECORD_H
6#define BITCOIN_AVALANCHE_VOTERECORD_H
7
8#include <nodeid.h>
9#include <random.h>
10
11#include <array>
12#include <atomic>
13#include <cstdint>
14
18static constexpr int AVALANCHE_FINALIZATION_SCORE = 128;
19
23static constexpr uint32_t AVALANCHE_VOTE_STALE_THRESHOLD = 4096;
24
29static constexpr uint32_t AVALANCHE_VOTE_STALE_MIN_THRESHOLD = 140;
30
36static constexpr uint32_t AVALANCHE_VOTE_STALE_FACTOR = 64;
37
41static constexpr int AVALANCHE_MAX_INFLIGHT_POLL = 10;
42
43namespace avalanche {
44
45struct TestVoteRecord;
46
50struct VoteRecord {
51private:
52 // confidence's LSB bit is the result. Higher bits are actual confidence
53 // score.
54 uint16_t confidence{0};
55
56 // Historical record of votes.
57 uint8_t votes{0};
58 // Each bit indicate if the vote is to be considered.
59 uint8_t consider{0};
60 // How many in flight requests exists for this element.
61 mutable std::atomic<uint8_t> inflight{0};
62
63 // Seed for pseudorandom operations.
64 const uint32_t seed;
65
66 // Track how many successful votes occured.
67 uint32_t successfulVotes{0};
68
69 // Track the nodes which are part of the quorum.
70 std::array<uint16_t, 8> nodeFilter{{0, 0, 0, 0, 0, 0, 0, 0}};
71
72public:
73 explicit VoteRecord(bool accepted)
74 : VoteRecord(accepted, FastRandomContext().rand<uint32_t>()) {}
75
76 VoteRecord(bool accepted, uint32_t seedIn)
77 : confidence(accepted), seed(seedIn) {}
78
82 VoteRecord(const VoteRecord &other)
83 : confidence(other.confidence), votes(other.votes),
84 consider(other.consider), inflight(other.inflight.load()),
86 nodeFilter(other.nodeFilter) {}
87
91 bool isAccepted() const { return confidence & 0x01; }
92
93 uint16_t getConfidence() const { return confidence >> 1; }
94 bool hasFinalized() const {
96 }
97
98 bool isStale(uint32_t staleThreshold = AVALANCHE_VOTE_STALE_THRESHOLD,
99 uint32_t staleFactor = AVALANCHE_VOTE_STALE_FACTOR) const {
100 return successfulVotes > staleThreshold &&
101 successfulVotes > getConfidence() * staleFactor;
102 }
103
108 bool registerVote(NodeId nodeid, uint32_t error);
109
115 bool registerPoll() const;
116
121
127 void clearInflightRequest(uint8_t count = 1) const;
128
129private:
135 bool addNodeToQuorum(NodeId nodeid);
136
137 friend struct ::avalanche::TestVoteRecord;
138};
139
140} // namespace avalanche
141
142#endif // BITCOIN_AVALANCHE_VOTERECORD_H
Fast randomness source.
Definition: random.h:411
int64_t NodeId
Definition: eviction.h:16
Vote history.
Definition: voterecord.h:50
uint16_t getConfidence() const
Definition: voterecord.h:93
VoteRecord(bool accepted, uint32_t seedIn)
Definition: voterecord.h:76
const uint32_t seed
Definition: voterecord.h:64
VoteRecord(bool accepted)
Definition: voterecord.h:73
bool hasFinalized() const
Definition: voterecord.h:94
std::atomic< uint8_t > inflight
Definition: voterecord.h:61
bool shouldPoll() const
Return if this item is in condition to be polled at the moment.
Definition: voterecord.h:120
void clearInflightRequest(uint8_t count=1) const
Clear count inflight requests.
Definition: voterecord.cpp:99
uint32_t successfulVotes
Definition: voterecord.h:67
bool registerVote(NodeId nodeid, uint32_t error)
Register a new vote for an item and update confidence accordingly.
Definition: voterecord.cpp:14
std::array< uint16_t, 8 > nodeFilter
Definition: voterecord.h:70
bool isStale(uint32_t staleThreshold=AVALANCHE_VOTE_STALE_THRESHOLD, uint32_t staleFactor=AVALANCHE_VOTE_STALE_FACTOR) const
Definition: voterecord.h:98
VoteRecord(const VoteRecord &other)
Copy semantic.
Definition: voterecord.h:82
bool addNodeToQuorum(NodeId nodeid)
Add the node to the quorum.
Definition: voterecord.cpp:62
bool registerPoll() const
Register that a request is being made regarding that item.
Definition: voterecord.cpp:88
bool isAccepted() const
Vote accounting facilities.
Definition: voterecord.h:91
static int count
static constexpr uint32_t AVALANCHE_VOTE_STALE_FACTOR
Scaling factor applied to confidence to determine staleness threshold.
Definition: voterecord.h:36
static constexpr int AVALANCHE_MAX_INFLIGHT_POLL
How many inflight requests can exist for one item.
Definition: voterecord.h:41
static constexpr uint32_t AVALANCHE_VOTE_STALE_MIN_THRESHOLD
Lowest configurable staleness threshold (finalization score + necessary votes to increase confidence ...
Definition: voterecord.h:29
static constexpr uint32_t AVALANCHE_VOTE_STALE_THRESHOLD
Number of votes before a record may be considered as stale.
Definition: voterecord.h:23
static constexpr int AVALANCHE_FINALIZATION_SCORE
Finalization score.
Definition: voterecord.h:18