Bitcoin ABC  0.24.11
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 
17 static constexpr int AVALANCHE_FINALIZATION_SCORE = 128;
18 
22 static constexpr int AVALANCHE_MAX_INFLIGHT_POLL = 10;
23 
24 namespace avalanche {
25 
29 struct VoteRecord {
30 private:
31  // confidence's LSB bit is the result. Higher bits are actual confidence
32  // score.
33  uint16_t confidence = 0;
34 
35  // Historical record of votes.
36  uint8_t votes = 0;
37  // Each bit indicate if the vote is to be considered.
38  uint8_t consider = 0;
39  // How many in flight requests exists for this element.
40  mutable std::atomic<uint8_t> inflight{0};
41 
42  // Seed for pseudorandom operations.
43  const uint32_t seed = 0;
44 
45  // Track how many successful votes occured.
46  uint32_t successfulVotes = 0;
47 
48  // Track the nodes which are part of the quorum.
49  std::array<uint16_t, 8> nodeFilter{{0, 0, 0, 0, 0, 0, 0, 0}};
50 
51 public:
52  explicit VoteRecord(bool accepted) : confidence(accepted) {}
53 
57  VoteRecord(const VoteRecord &other)
58  : confidence(other.confidence), votes(other.votes),
59  consider(other.consider), inflight(other.inflight.load()),
61  }
62 
66  bool isAccepted() const { return confidence & 0x01; }
67 
68  uint16_t getConfidence() const { return confidence >> 1; }
69  bool hasFinalized() const {
71  }
72 
77  bool registerVote(NodeId nodeid, uint32_t error);
78 
84  bool registerPoll() const;
85 
89  bool shouldPoll() const { return inflight < AVALANCHE_MAX_INFLIGHT_POLL; }
90 
94  void clearInflightRequest(uint8_t count = 1) { inflight -= count; }
95 
96 private:
102  bool addNodeToQuorum(NodeId nodeid);
103 };
104 
105 } // namespace avalanche
106 
107 #endif // BITCOIN_AVALANCHE_VOTERECORD_H
avalanche::VoteRecord
Vote history.
Definition: voterecord.h:29
count
static int count
Definition: tests.c:41
avalanche::VoteRecord::confidence
uint16_t confidence
Definition: voterecord.h:33
avalanche::VoteRecord::nodeFilter
std::array< uint16_t, 8 > nodeFilter
Definition: voterecord.h:49
AVALANCHE_MAX_INFLIGHT_POLL
static constexpr int AVALANCHE_MAX_INFLIGHT_POLL
How many inflight requests can exist for one item.
Definition: voterecord.h:22
avalanche::VoteRecord::addNodeToQuorum
bool addNodeToQuorum(NodeId nodeid)
Add the node to the quorum.
Definition: voterecord.cpp:61
avalanche
Definition: avalanche.h:11
avalanche::VoteRecord::seed
const uint32_t seed
Definition: voterecord.h:43
avalanche::VoteRecord::VoteRecord
VoteRecord(const VoteRecord &other)
Copy semantic.
Definition: voterecord.h:57
avalanche::VoteRecord::registerPoll
bool registerPoll() const
Register that a request is being made regarding that item.
Definition: voterecord.cpp:92
avalanche::VoteRecord::getConfidence
uint16_t getConfidence() const
Definition: voterecord.h:68
avalanche::VoteRecord::isAccepted
bool isAccepted() const
Vote accounting facilities.
Definition: voterecord.h:66
avalanche::VoteRecord::shouldPoll
bool shouldPoll() const
Return if this item is in condition to be polled at the moment.
Definition: voterecord.h:89
avalanche::VoteRecord::clearInflightRequest
void clearInflightRequest(uint8_t count=1)
Clear count inflight requests.
Definition: voterecord.h:94
avalanche::VoteRecord::hasFinalized
bool hasFinalized() const
Definition: voterecord.h:69
AVALANCHE_FINALIZATION_SCORE
static constexpr int AVALANCHE_FINALIZATION_SCORE
Finalization score.
Definition: voterecord.h:17
nodeid.h
avalanche::VoteRecord::successfulVotes
uint32_t successfulVotes
Definition: voterecord.h:46
avalanche::VoteRecord::inflight
std::atomic< uint8_t > inflight
Definition: voterecord.h:40
avalanche::VoteRecord::votes
uint8_t votes
Definition: voterecord.h:36
avalanche::VoteRecord::consider
uint8_t consider
Definition: voterecord.h:38
avalanche::VoteRecord::VoteRecord
VoteRecord(bool accepted)
Definition: voterecord.h:52
NodeId
int64_t NodeId
Definition: nodeid.h:10
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
avalanche::VoteRecord::registerVote
bool registerVote(NodeId nodeid, uint32_t error)
Register a new vote for an item and update confidence accordingly.
Definition: voterecord.cpp:13