Kagome
Polkadot Runtime Engine in C++17
vote_graph_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_GRAPH_VOTE_GRAPH_IMPL_HPP
7 #define KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_GRAPH_VOTE_GRAPH_IMPL_HPP
8 
10 
11 #include <numeric>
12 #include <unordered_map>
13 #include <unordered_set>
14 
16 
17  class VoteGraphImpl : public VoteGraph {
18  public:
19  VoteGraphImpl(const BlockInfo &base,
20  std::shared_ptr<VoterSet> voter_set,
21  std::shared_ptr<Chain> chain);
22 
28  void adjustBase(const std::vector<BlockHash> &ancestry_proof) override;
29 
31  outcome::result<void> insert(VoteType vote_type,
32  const BlockInfo &block,
33  const Id &voter) override;
34 
36  void remove(VoteType vote_type, const Id &voter) override;
37 
40  std::optional<BlockInfo> findAncestor(
41  VoteType vote_type,
42  const BlockInfo &block,
43  const Condition &condition) const override;
44 
58  std::optional<BlockInfo> findGhost(
59  VoteType vote_type,
60  const std::optional<BlockInfo> &current_best,
61  const Condition &condition) const override;
62 
63  // introduce a branch to given vote-nodes.
64  //
65  // `descendants` is a list of nodes with ancestor-edges containing the given
66  // ancestor.
67  //
68  // This function panics if any member of `descendants` is not a vote-node
69  // or does not have ancestor with given hash and number OR if
70  // `ancestor_hash` is already a known entry.
71  void introduceBranch(const std::vector<primitives::BlockHash> &descendants,
72  const BlockInfo &ancestor);
73 
74  // append a vote-node onto the chain-tree. This should only be called if
75  // no node in the tree keeps the target anyway.
76  outcome::result<void> append(const BlockInfo &block);
77 
78  // given a key, node pair (which must correspond), assuming this node
79  // fulfills the condition, this function will find the highest point at
80  // which its descendants merge, which may be the node itself.
82  VoteType vote_type,
83  const BlockHash &active_node_hash,
84  const Entry &active_node,
85  const std::optional<BlockInfo> &force_constrain,
86  const Condition &condition) const;
87 
88  // attempts to find the containing node keys for the given hash and number.
89  //
90  // returns `None` if there is a node by that key already, and a vector
91  // (potentially empty) of nodes with the given block in its ancestor-edge
92  // otherwise.
93  std::optional<std::vector<primitives::BlockHash>> findContainingNodes(
94  const BlockInfo &block) const;
95 
96  const BlockInfo &getBase() const override {
97  return base_;
98  }
99 
100  const auto &getEntries() const {
101  return entries_;
102  }
103 
104  const auto &getHeads() const {
105  return heads_;
106  }
107 
108  private:
110  std::shared_ptr<VoterSet> voter_set_;
111  std::shared_ptr<Chain> chain_;
113 
114  std::unordered_map<BlockHash, Entry> entries_;
115  std::unordered_set<BlockHash> heads_;
116  };
117 
118 } // namespace kagome::consensus::grandpa
119 
120 #endif // KAGOME_CORE_CONSENSUS_GRANDPA_VOTE_GRAPH_VOTE_GRAPH_IMPL_HPP
std::unordered_map< BlockHash, Entry > entries_
void adjustBase(const std::vector< BlockHash > &ancestry_proof) override
outcome::result< void > append(const BlockInfo &block)
std::unordered_set< BlockHash > heads_
outcome::result< void > insert(VoteType vote_type, const BlockInfo &block, const Id &voter) override
Insert vote {.
std::optional< BlockInfo > findGhost(VoteType vote_type, const std::optional< BlockInfo > &current_best, const Condition &condition) const override
crypto::Ed25519PublicKey Id
Definition: common.hpp:19
Subchain ghostFindMergePoint(VoteType vote_type, const BlockHash &active_node_hash, const Entry &active_node, const std::optional< BlockInfo > &force_constrain, const Condition &condition) const
std::function< bool(const VoteWeight &)> Condition
Definition: vote_graph.hpp:61
void introduceBranch(const std::vector< primitives::BlockHash > &descendants, const BlockInfo &ancestor)
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
const BlockInfo & getBase() const override
std::optional< BlockInfo > findAncestor(VoteType vote_type, const BlockInfo &block, const Condition &condition) const override
VoteGraphImpl(const BlockInfo &base, std::shared_ptr< VoterSet > voter_set, std::shared_ptr< Chain > chain)
std::optional< std::vector< primitives::BlockHash > > findContainingNodes(const BlockInfo &block) const