Bitcoin ABC 0.33.11
P2P Digital Currency
voterecord.cpp
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
6
7#include <util/bitmanip.h>
8
9#include <algorithm>
10#include <cstddef>
11
12namespace avalanche {
13
14bool VoteRecord::registerVote(NodeId nodeid, uint32_t error) {
15 // We just got a new vote, so there is one less inflight request.
17
18 // We want to avoid having the same node voting twice in a quorum.
19 if (!addNodeToQuorum(nodeid)) {
20 return false;
21 }
22
30 votes = (votes << 1) | (error == 0);
31 consider = (consider << 1) | (int32_t(error) >= 0);
32
42 bool yes = countBits(votes & consider & 0xff) > 6;
43 if (!yes) {
44 bool no = countBits(~votes & consider & 0xff) > 6;
45 if (!no) {
46 // The round is inconclusive.
47 return false;
48 }
49 }
50
51 // If the round is in agreement with previous rounds, increase confidence.
52 if (isAccepted() == yes) {
53 confidence += 2;
55 }
56
57 // The round changed our state. We reset the confidence.
58 confidence = yes;
59 return true;
60}
61
63 // MMIX Linear Congruent Generator.
64 const uint64_t r1 =
65 6364136223846793005 * uint64_t(nodeid) + 1442695040888963407;
66 // Fibonacci hashing.
67 const uint64_t r2 = 11400714819323198485ull * (nodeid ^ seed);
68 // Combine and extract hash.
69 const uint16_t h = (r1 + r2) >> 48;
70
74 for (size_t i = 1; i < nodeFilter.size(); i++) {
75 if (nodeFilter[(successfulVotes + i) % nodeFilter.size()] == h) {
76 return false;
77 }
78 }
79
85 return true;
86}
87
89 uint8_t count = inflight.load();
91 if (inflight.compare_exchange_weak(count, count + 1)) {
92 return true;
93 }
94 }
95
96 return false;
97}
98
100 uint8_t dec;
101 uint8_t current = inflight.load();
102 do {
103 dec = std::min(current, count);
104 } while (!inflight.compare_exchange_weak(current, current - dec));
105}
106
107} // namespace avalanche
uint32_t countBits(uint32_t v)
Definition: bitmanip.h:12
int64_t NodeId
Definition: eviction.h:16
uint16_t getConfidence() const
Definition: voterecord.h:88
const uint32_t seed
Definition: voterecord.h:63
std::atomic< uint8_t > inflight
Definition: voterecord.h:60
void clearInflightRequest(uint8_t count=1) const
Clear count inflight requests.
Definition: voterecord.cpp:99
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:14
std::array< uint16_t, 8 > nodeFilter
Definition: voterecord.h:69
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:86
static int count
static constexpr int AVALANCHE_MAX_INFLIGHT_POLL
How many inflight requests can exist for one item.
Definition: voterecord.h:40
static constexpr int AVALANCHE_FINALIZATION_SCORE
Finalization score.
Definition: voterecord.h:17