Bitcoin ABC 0.30.7
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
10#include <array>
11#include <atomic>
12#include <cstdint>
13
17static constexpr int AVALANCHE_FINALIZATION_SCORE = 128;
18
22static constexpr uint32_t AVALANCHE_VOTE_STALE_THRESHOLD = 4096;
23
28static constexpr uint32_t AVALANCHE_VOTE_STALE_MIN_THRESHOLD = 140;
29
35static constexpr uint32_t AVALANCHE_VOTE_STALE_FACTOR = 64;
36
40static constexpr int AVALANCHE_MAX_INFLIGHT_POLL = 10;
41
42namespace avalanche {
43
44struct TestVoteRecord;
45
49struct VoteRecord {
50private:
51 // confidence's LSB bit is the result. Higher bits are actual confidence
52 // score.
53 uint16_t confidence = 0;
54
55 // Historical record of votes.
56 uint8_t votes = 0;
57 // Each bit indicate if the vote is to be considered.
58 uint8_t consider = 0;
59 // How many in flight requests exists for this element.
60 mutable std::atomic<uint8_t> inflight{0};
61
62 // Seed for pseudorandom operations.
63 const uint32_t seed = 0;
64
65 // Track how many successful votes occured.
66 uint32_t successfulVotes = 0;
67
68 // Track the nodes which are part of the quorum.
69 std::array<uint16_t, 8> nodeFilter{{0, 0, 0, 0, 0, 0, 0, 0}};
70
71public:
72 explicit VoteRecord(bool accepted) : confidence(accepted) {}
73
77 VoteRecord(const VoteRecord &other)
78 : confidence(other.confidence), votes(other.votes),
79 consider(other.consider), inflight(other.inflight.load()),
81 }
82
86 bool isAccepted() const { return confidence & 0x01; }
87
88 uint16_t getConfidence() const { return confidence >> 1; }
89 bool hasFinalized() const {
91 }
92
93 bool isStale(uint32_t staleThreshold = AVALANCHE_VOTE_STALE_THRESHOLD,
94 uint32_t staleFactor = AVALANCHE_VOTE_STALE_FACTOR) const {
95 return successfulVotes > staleThreshold &&
96 successfulVotes > getConfidence() * staleFactor;
97 }
98
103 bool registerVote(NodeId nodeid, uint32_t error);
104
110 bool registerPoll() const;
111
116
120 void clearInflightRequest(uint8_t count = 1) { inflight -= count; }
121
122private:
128 bool addNodeToQuorum(NodeId nodeid);
129
130 friend struct ::avalanche::TestVoteRecord;
131};
132
133} // namespace avalanche
134
135#endif // BITCOIN_AVALANCHE_VOTERECORD_H
bool error(const char *fmt, const Args &...args)
Definition: logging.h:263
int64_t NodeId
Definition: nodeid.h:10
Vote history.
Definition: voterecord.h:49
uint16_t getConfidence() const
Definition: voterecord.h:88
const uint32_t seed
Definition: voterecord.h:63
void clearInflightRequest(uint8_t count=1)
Clear count inflight requests.
Definition: voterecord.h:120
VoteRecord(bool accepted)
Definition: voterecord.h:72
bool hasFinalized() const
Definition: voterecord.h:89
std::atomic< uint8_t > inflight
Definition: voterecord.h:60
bool shouldPoll() const
Return if this item is in condition to be polled at the moment.
Definition: voterecord.h:115
uint32_t successfulVotes
Definition: voterecord.h:66
bool registerVote(NodeId nodeid, uint32_t error)
Register a new vote for an item and update confidence accordingly.
Definition: voterecord.cpp:13
std::array< uint16_t, 8 > nodeFilter
Definition: voterecord.h:69
bool isStale(uint32_t staleThreshold=AVALANCHE_VOTE_STALE_THRESHOLD, uint32_t staleFactor=AVALANCHE_VOTE_STALE_FACTOR) const
Definition: voterecord.h:93
VoteRecord(const VoteRecord &other)
Copy semantic.
Definition: voterecord.h:77
bool addNodeToQuorum(NodeId nodeid)
Add the node to the quorum.
Definition: voterecord.cpp:61
bool registerPoll() const
Register that a request is being made regarding that item.
Definition: voterecord.cpp:87
bool isAccepted() const
Vote accounting facilities.
Definition: voterecord.h:86
static int count
Definition: tests.c:31
static constexpr uint32_t AVALANCHE_VOTE_STALE_FACTOR
Scaling factor applied to confidence to determine staleness threshold.
Definition: voterecord.h:35
static constexpr int AVALANCHE_MAX_INFLIGHT_POLL
How many inflight requests can exist for one item.
Definition: voterecord.h:40
static constexpr uint32_t AVALANCHE_VOTE_STALE_MIN_THRESHOLD
Lowest configurable staleness threshold (finalization score + necessary votes to increase confidence ...
Definition: voterecord.h:28
static constexpr uint32_t AVALANCHE_VOTE_STALE_THRESHOLD
Number of votes before a record may be considered as stale.
Definition: voterecord.h:22
static constexpr int AVALANCHE_FINALIZATION_SCORE
Finalization score.
Definition: voterecord.h:17