Kagome
Polkadot Runtime Engine in C++17
voter_set.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_CONSENSUS_GRANDPA_VOTER_SET_HPP
7 #define KAGOME_CORE_CONSENSUS_GRANDPA_VOTER_SET_HPP
8 
9 #include <optional>
10 
11 #include "common/outcome_throw.hpp"
13 
15 
19  struct VoterSet final {
20  public:
21  enum class Error {
24  };
25 
26  using Index = size_t;
27  using Weight = size_t;
28 
29  VoterSet() = default; // for scale codec (in decode)
30 
31  explicit VoterSet(VoterSetId id_of_set);
32 
36  outcome::result<void> insert(Id voter, Weight weight);
37 
41  inline VoterSetId id() const {
42  return id_;
43  }
44 
45  std::optional<std::tuple<Index, Weight>> indexAndWeight(
46  const Id &voter) const;
47 
48  outcome::result<Id> voterId(Index index) const;
49 
53  std::optional<Index> voterIndex(const Id &voter) const;
54 
58  std::optional<Weight> voterWeight(const Id &voter) const;
59 
63  outcome::result<Weight> voterWeight(Index voter_index) const;
64 
65  inline size_t size() const {
66  return list_.size();
67  }
68 
69  inline bool empty() const {
70  return list_.empty();
71  }
72 
76  inline Weight totalWeight() const {
77  return total_weight_;
78  }
79 
80  private:
82  std::unordered_map<Id, Index> map_;
83  std::vector<std::tuple<const Id &, Weight>> list_;
84  size_t total_weight_{0};
85 
86  template <class Stream>
87  friend Stream &operator<<(Stream &s, const VoterSet &voters);
88  template <class Stream>
89  friend Stream &operator>>(Stream &s, VoterSet &voters);
90  };
91 
92  template <class Stream,
93  typename = std::enable_if_t<Stream::is_encoder_stream>>
94  Stream &operator<<(Stream &s, const VoterSet &voters) {
95  return s << voters.list_ << voters.id_;
96  }
97 
98  template <class Stream,
99  typename = std::enable_if_t<Stream::is_decoder_stream>>
101  voters.list_.clear();
102  voters.map_.clear();
103  voters.total_weight_ = 0;
104 
105  std::vector<std::tuple<Id, VoterSet::Weight>> list;
106  s >> list >> voters.id_;
107  for (const auto &[id, weight] : list) {
108  auto r = voters.insert(id, weight);
109  if (r.has_error()) {
110  common::raise(r.as_failure());
111  }
112  }
113  return s;
114  }
115 
116 } // namespace kagome::consensus::grandpa
117 
119 
120 #endif // KAGOME_CORE_CONSENSUS_GRANDPA_VOTER_SET_HPP
OUTCOME_HPP_DECLARE_ERROR(kagome::consensus::grandpa, VoterSet::Error)
void raise(T t)
throws outcome::result error as boost exception
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
friend Stream & operator>>(Stream &s, VoterSet &voters)
Definition: voter_set.hpp:100
std::unordered_map< Id, Index > map_
Definition: voter_set.hpp:82
std::optional< Weight > voterWeight(const Id &voter) const
Definition: voter_set.cpp:75
libp2p::connection::Stream Stream
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
friend Stream & operator<<(Stream &s, const VoterSet &voters)
Definition: voter_set.hpp:94