Kagome
Polkadot Runtime Engine in C++17
voter_set.cpp
Go to the documentation of this file.
1 
7 
10  switch (e) {
11  case E::VOTER_ALREADY_EXISTS:
12  return "Voter already exists";
13  case E::INDEX_OUTBOUND:
14  return "Index outbound";
15  }
16  return "Unknown error (invalid VoterSet::Error)";
17 }
18 
20 
21  VoterSet::VoterSet(VoterSetId id_of_set) : id_{id_of_set} {}
22 
23  outcome::result<void> VoterSet::insert(Id voter, size_t weight) {
24  // zero authorities break the mapping logic a bit, but since they must not
25  // be queried it should be fine
26  if (voter == Id{}) {
27  list_.emplace_back(voter, weight);
28  total_weight_ += weight;
29  return outcome::success();
30  }
31  auto r = map_.emplace(voter, map_.size());
32  if (r.second) {
33  list_.emplace_back(r.first->first, weight);
34  total_weight_ += weight;
35  return outcome::success();
36  }
38  }
39 
40  outcome::result<Id> VoterSet::voterId(Index index) const {
41  if (index >= list_.size()) {
42  return Error::INDEX_OUTBOUND;
43  }
44  auto voter = std::get<0>(list_[index]);
45  return voter;
46  }
47 
48  std::optional<std::tuple<VoterSet::Index, VoterSet::Weight>>
49  VoterSet::indexAndWeight(const Id &voter) const {
50  if (voter == Id{}) {
51  return std::nullopt;
52  }
53  auto it = map_.find(voter);
54  if (it == map_.end()) {
55  return std::nullopt;
56  }
57  auto index = it->second;
58  BOOST_ASSERT(index < list_.size());
59  auto weight = std::get<1>(list_[index]);
60  return std::tuple(index, weight);
61  }
62 
63  std::optional<VoterSet::Index> VoterSet::voterIndex(const Id &voter) const {
64  if (voter == Id{}) {
65  return std::nullopt;
66  }
67  auto it = map_.find(voter);
68  if (it == map_.end()) {
69  return std::nullopt;
70  }
71  auto index = it->second;
72  return index;
73  }
74 
75  std::optional<VoterSet::Weight> VoterSet::voterWeight(
76  const Id &voter) const {
77  if (voter == Id{}) {
78  return std::nullopt;
79  }
80  auto it = map_.find(voter);
81  if (it == map_.end()) {
82  return std::nullopt;
83  }
84  auto index = it->second;
85  BOOST_ASSERT(index < list_.size());
86  auto weight = std::get<1>(list_[index]);
87  return weight;
88  }
89 
90  outcome::result<VoterSet::Weight> VoterSet::voterWeight(Index index) const {
91  if (index >= list_.size()) {
92  return Error::INDEX_OUTBOUND;
93  }
94  auto weight = std::get<1>(list_.at(index));
95  return weight;
96  }
97 } // namespace kagome::consensus::grandpa
outcome::result< void > insert(Id voter, Weight weight)
Definition: voter_set.cpp:23
std::optional< Index > voterIndex(const Id &voter) const
Definition: voter_set.cpp:63
std::optional< std::tuple< Index, Weight > > indexAndWeight(const Id &voter) const
Definition: voter_set.cpp:49
crypto::Ed25519PublicKey Id
Definition: common.hpp:19
OUTCOME_CPP_DEFINE_CATEGORY(kagome::consensus::grandpa, VoterSet::Error, e)
Definition: voter_set.cpp:8
std::unordered_map< Id, Index > map_
Definition: voter_set.hpp:82
std::optional< Weight > voterWeight(const Id &voter) const
Definition: voter_set.cpp:75
outcome::result< Id > voterId(Index index) const
Definition: voter_set.cpp:40
std::vector< std::tuple< const Id &, Weight > > list_
Definition: voter_set.hpp:83