Kagome
Polkadot Runtime Engine in C++17
vote_graph.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_GRAPH_HPP
7 #define KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_GRAPH_HPP
8 
9 #include <boost/operators.hpp>
10 
14 
16 
21  struct VoteGraph {
22  // graph entry
23  struct Entry : public boost::equality_comparable<Entry> {
27  std::vector<BlockHash> ancestors{};
28  std::vector<BlockHash> descendants{};
30 
31  bool operator==(const Entry &o) const {
32  // clang-format off
33  return number == o.number
34  && ancestors == o.ancestors
35  && descendants == o.descendants
36  && cumulative_vote == o.cumulative_vote;
37  // clang-format on
38  }
39 
40  // Get ancestor block by number. Returns `None` if there is no block
41  // by that number in the direct ancestry.
42  std::optional<BlockHash> getAncestorBlockBy(BlockNumber n) const {
43  if (n >= number) {
44  return std::nullopt;
45  }
46  const auto offset = number - n - 1u;
47  if (offset >= ancestors.size()) {
48  return std::nullopt;
49  }
50  return ancestors[offset];
51  }
52  };
53 
54  struct Subchain {
55  std::vector<BlockHash> hashes;
57  };
58 
59  virtual ~VoteGraph() = default;
60 
61  using Condition = std::function<bool(const VoteWeight &)>;
62  using Comparator =
63  std::function<bool(const VoteWeight &, const VoteWeight &)>;
64 
65  virtual const BlockInfo &getBase() const = 0;
66 
72  virtual void adjustBase(const std::vector<BlockHash> &ancestry_proof) = 0;
73 
75  virtual outcome::result<void> insert(VoteType vote_type,
76  const BlockInfo &block,
77  const Id &voter) = 0;
78 
80  virtual void remove(VoteType vote_type, const Id &voter) = 0;
81 
84  virtual std::optional<BlockInfo> findAncestor(
85  VoteType vote_type,
86  const BlockInfo &block,
87  const Condition &condition) const = 0;
88 
102  virtual std::optional<BlockInfo> findGhost(
103  VoteType vote_type,
104  const std::optional<BlockInfo> &current_best,
105  const Condition &condition) const = 0;
106  };
107 
108 } // namespace kagome::consensus::grandpa
109 
110 #endif // KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_GRAPH_HPP
Definition: vote_graph.hpp:23
VoteWeight cumulative_vote
Definition: vote_graph.hpp:29
virtual const BlockInfo & getBase() const =0
crypto::Ed25519PublicKey Id
Definition: common.hpp:19
virtual std::optional< BlockInfo > findAncestor(VoteType vote_type, const BlockInfo &block, const Condition &condition) const =0
std::optional< BlockHash > getAncestorBlockBy(BlockNumber n) const
Definition: vote_graph.hpp:42
std::function< bool(const VoteWeight &)> Condition
Definition: vote_graph.hpp:61
virtual void adjustBase(const std::vector< BlockHash > &ancestry_proof)=0
bool operator==(const Entry &o) const
Definition: vote_graph.hpp:31
primitives::BlockNumber BlockNumber
Definition: common.hpp:24
std::vector< BlockHash > descendants
Definition: vote_graph.hpp:28
std::vector< BlockHash > ancestors
Definition: vote_graph.hpp:27
virtual std::optional< BlockInfo > findGhost(VoteType vote_type, const std::optional< BlockInfo > &current_best, const Condition &condition) const =0
std::function< bool(const VoteWeight &, const VoteWeight &)> Comparator
Definition: vote_graph.hpp:63
BlockNumber number
Definition: vote_graph.hpp:24
virtual outcome::result< void > insert(VoteType vote_type, const BlockInfo &block, const Id &voter)=0
Insert vote {.