Kagome
Polkadot Runtime Engine in C++17
vote_weight.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_WEIGHT_HPP
7 #define KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_WEIGHT_HPP
8 
9 #include <numeric>
10 
11 #include <boost/dynamic_bitset.hpp>
12 #include <boost/operators.hpp>
16 
18 
23  class VoteWeight : public boost::equality_comparable<VoteWeight>,
24  public boost::less_than_comparable<VoteWeight> {
25  public:
26  using Weight = size_t;
27 
29  std::vector<bool> flags;
30  Weight sum = 0;
31 
32  void set(size_t index, size_t weight) {
33  if (flags.size() <= index) {
34  flags.resize(index + 1, 0);
35  }
36  if (flags[index]) {
37  return;
38  }
39  flags[index] = true;
40  sum += weight;
41  }
42 
43  void unset(size_t index, size_t weight) {
44  if (flags.size() <= index) {
45  return;
46  }
47  if (not flags[index]) {
48  return;
49  }
50  flags[index] = false;
51  sum -= weight;
52  }
53 
54  Weight total(const std::vector<bool> &equivocators,
55  const VoterSet &voter_set) const {
56  Weight result = sum;
57 
58  for (size_t i = voter_set.size(); i > 0;) {
59  --i;
60 
61  if ((flags.size() <= i or not flags[i]) and equivocators.size() > i
62  and equivocators[i]) {
63  result += voter_set.voterWeight(i).value();
64  }
65  }
66 
67  return result;
68  }
69 
70  void merge(const OneTypeVoteWeight &other,
71  const std::shared_ptr<VoterSet> &voter_set) {
72  for (auto i = other.flags.size(); i > 0;) {
73  --i;
74  if (other.flags[i]) {
75  set(i, voter_set->voterWeight(i).value());
76  }
77  }
78  }
79 
80  bool operator==(const OneTypeVoteWeight &other) const {
81  return sum == other.sum and flags == other.flags;
82  }
83  };
84 
85  Weight sum(VoteType vote_type) const {
86  switch (vote_type) {
87  case VoteType::Prevote:
88  return prevotes_weight.sum;
90  return precommits_weight.sum;
91  }
92  BOOST_UNREACHABLE_RETURN({});
93  }
94 
95  void set(VoteType vote_type, size_t index, size_t weight) {
96  switch (vote_type) {
97  case VoteType::Prevote:
98  return prevotes_weight.set(index, weight);
100  return precommits_weight.set(index, weight);
101  }
102  BOOST_UNREACHABLE_RETURN();
103  }
104 
105  void unset(VoteType vote_type, size_t index, size_t weight) {
106  switch (vote_type) {
107  case VoteType::Prevote:
108  return prevotes_weight.unset(index, weight);
109  case VoteType::Precommit:
110  return precommits_weight.unset(index, weight);
111  }
112  BOOST_UNREACHABLE_RETURN();
113  }
114 
115  Weight total(VoteType vote_type,
116  const std::vector<bool> &equivocators,
117  const VoterSet &voter_set) const {
118  switch (vote_type) {
119  case VoteType::Prevote:
120  return prevotes_weight.total(equivocators, voter_set);
121  case VoteType::Precommit:
122  return precommits_weight.total(equivocators, voter_set);
123  }
124  BOOST_UNREACHABLE_RETURN({});
125  }
126 
127  void merge(const VoteWeight &other,
128  const std::shared_ptr<VoterSet> &voter_set) {
129  prevotes_weight.merge(other.prevotes_weight, voter_set);
130  precommits_weight.merge(other.precommits_weight, voter_set);
131  }
132 
133  bool operator==(const VoteWeight &other) const {
134  return prevotes_weight == other.prevotes_weight
136  }
137 
138  private:
141  };
142 
143 } // namespace kagome::consensus::grandpa
144 
145 #endif // KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_WEIGHT_HPP
bool operator==(const VoteWeight &other) const
void merge(const OneTypeVoteWeight &other, const std::shared_ptr< VoterSet > &voter_set)
Definition: vote_weight.hpp:70
void unset(VoteType vote_type, size_t index, size_t weight)
Weight sum(VoteType vote_type) const
Definition: vote_weight.hpp:85
void merge(const VoteWeight &other, const std::shared_ptr< VoterSet > &voter_set)
Weight total(VoteType vote_type, const std::vector< bool > &equivocators, const VoterSet &voter_set) const
std::optional< Weight > voterWeight(const Id &voter) const
Definition: voter_set.cpp:75
bool operator==(const OneTypeVoteWeight &other) const
Definition: vote_weight.hpp:80
Weight total(const std::vector< bool > &equivocators, const VoterSet &voter_set) const
Definition: vote_weight.hpp:54