Kagome
Polkadot Runtime Engine in C++17
cached_tree.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_BLOCKCHAIN_TREE_NODE_HPP
7 #define KAGOME_BLOCKCHAIN_TREE_NODE_HPP
8 
9 #include <memory>
10 #include <unordered_set>
11 
14 #include "primitives/block_id.hpp"
16 
17 namespace kagome::blockchain {
18 
24  class TreeNode : public std::enable_shared_from_this<TreeNode> {
25  public:
26  enum class Error { NO_CHAIN_BETWEEN_BLOCKS = 1 };
27 
28  TreeNode(const primitives::BlockHash &hash,
30  bool finalized = false);
31 
32  TreeNode(const primitives::BlockHash &hash,
34  const std::shared_ptr<TreeNode> &parent,
35  bool finalized = false);
36 
39  std::weak_ptr<TreeNode> parent;
40  bool finalized;
41 
42  std::vector<std::shared_ptr<TreeNode>> children{};
43 
48  std::shared_ptr<const TreeNode> findByHash(
49  const primitives::BlockHash &hash) const;
50 
51  std::shared_ptr<TreeNode> findByHash(const primitives::BlockHash &hash) {
52  return std::const_pointer_cast<TreeNode>(
53  std::as_const(*this).findByHash(hash));
54  }
55 
60  enum class ExitToken { EXIT, CONTINUE };
61 
72  outcome::result<void> applyToChain(
73  const primitives::BlockInfo &chain_end,
74  std::function<outcome::result<ExitToken>(TreeNode const &node)> const
75  &op) const;
76 
78  return {depth, block_hash};
79  }
80 
81  bool operator==(const TreeNode &other) const;
82  bool operator!=(const TreeNode &other) const;
83  };
84 
89  struct TreeMeta {
90  explicit TreeMeta(
91  const std::shared_ptr<TreeNode> &subtree_root_node,
92  std::optional<primitives::Justification> last_finalized_justification);
93 
94  TreeMeta(std::unordered_set<primitives::BlockHash> leaves,
95  const std::shared_ptr<TreeNode> &deepest_leaf,
96  const std::shared_ptr<TreeNode> &last_finalized,
97  primitives::Justification last_finalized_justification);
98 
99  std::unordered_set<primitives::BlockHash> leaves;
100  std::weak_ptr<TreeNode> deepest_leaf;
101 
102  std::weak_ptr<TreeNode> last_finalized;
103  std::optional<primitives::Justification> last_finalized_justification;
104  };
105 
109  class CachedTree {
110  public:
111  explicit CachedTree(std::shared_ptr<TreeNode> root,
112  std::shared_ptr<TreeMeta> metadata)
113  : root_{std::move(root)}, metadata_{std::move(metadata)} {
114  BOOST_ASSERT(root_ != nullptr);
115  BOOST_ASSERT(metadata_ != nullptr);
116  }
123  void updateTreeRoot(std::shared_ptr<TreeNode> new_trie_root,
124  primitives::Justification justification);
125 
126  void updateMeta(const std::shared_ptr<TreeNode> &new_node);
127 
134  void removeFromMeta(const std::shared_ptr<TreeNode> &node);
135 
136  TreeNode const &getRoot() const;
137  TreeNode &getRoot();
138 
139  TreeMeta const &getMetadata() const;
140 
141  private:
142  std::shared_ptr<TreeNode> root_;
143  std::shared_ptr<TreeMeta> metadata_;
144  };
145 } // namespace kagome::blockchain
146 
148 
149 #endif // KAGOME_BLOCKCHAIN_TREE_NODE_HPP
bool operator==(const TreeNode &other) const
std::shared_ptr< TreeNode > root_
CachedTree(std::shared_ptr< TreeNode > root, std::shared_ptr< TreeMeta > metadata)
primitives::BlockInfo getBlockInfo() const
Definition: cached_tree.hpp:77
outcome::result< void > applyToChain(const primitives::BlockInfo &chain_end, std::function< outcome::result< ExitToken >(TreeNode const &node)> const &op) const
Definition: cached_tree.cpp:34
primitives::BlockNumber depth
Definition: cached_tree.hpp:38
std::shared_ptr< TreeMeta > metadata_
std::weak_ptr< TreeNode > deepest_leaf
std::vector< std::shared_ptr< TreeNode > > children
Definition: cached_tree.hpp:42
std::shared_ptr< TreeNode > findByHash(const primitives::BlockHash &hash)
Definition: cached_tree.hpp:51
bool operator!=(const TreeNode &other) const
primitives::BlockHash block_hash
Definition: cached_tree.hpp:37
uint32_t BlockNumber
Definition: common.hpp:18
std::weak_ptr< TreeNode > last_finalized
OUTCOME_HPP_DECLARE_ERROR(kagome::blockchain, TreeNode::Error)
std::shared_ptr< const TreeNode > findByHash(const primitives::BlockHash &hash) const
Definition: cached_tree.cpp:88
TreeNode(const primitives::BlockHash &hash, primitives::BlockNumber depth, bool finalized=false)
Definition: cached_tree.cpp:23
std::unordered_set< primitives::BlockHash > leaves
Definition: cached_tree.hpp:99
std::weak_ptr< TreeNode > parent
Definition: cached_tree.hpp:39
std::optional< primitives::Justification > last_finalized_justification