Kagome
Polkadot Runtime Engine in C++17
block_tree_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_BLOCK_TREE_IMPL_HPP
7 #define KAGOME_BLOCK_TREE_IMPL_HPP
8 
10 
11 #include <functional>
12 #include <memory>
13 #include <queue>
14 #include <unordered_set>
15 
16 #include <optional>
17 
25 #include "crypto/hasher.hpp"
26 #include "log/logger.hpp"
27 #include "metrics/metrics.hpp"
33 #include "telemetry/service.hpp"
34 
36  class ChangesTracker;
37 }
38 
39 namespace kagome::blockchain {
40 
41  class TreeNode;
42  class CachedTree;
43 
44  class BlockTreeImpl : public BlockTree {
45  public:
47  static outcome::result<std::shared_ptr<BlockTreeImpl>> create(
48  std::shared_ptr<BlockHeaderRepository> header_repo,
49  std::shared_ptr<BlockStorage> storage,
50  std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer,
51  std::shared_ptr<crypto::Hasher> hasher,
54  extrinsic_events_engine,
55  std::shared_ptr<subscription::ExtrinsicEventKeyRepository>
56  extrinsic_event_key_repo,
57  std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
58  std::shared_ptr<const class JustificationStoragePolicy>
59  justification_storage_policy);
60 
62  static outcome::result<void> recover(
63  primitives::BlockId target_block,
64  std::shared_ptr<BlockStorage> storage,
65  std::shared_ptr<BlockHeaderRepository> header_repo,
66  std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
67  std::shared_ptr<blockchain::BlockTree> block_tree);
68 
69  ~BlockTreeImpl() override = default;
70 
71  const primitives::BlockHash &getGenesisBlockHash() const override;
72 
73  outcome::result<bool> hasBlockHeader(
74  const primitives::BlockId &block) const override;
75 
76  outcome::result<primitives::BlockHeader> getBlockHeader(
77  const primitives::BlockId &block) const override;
78 
79  outcome::result<primitives::BlockBody> getBlockBody(
80  const primitives::BlockId &block) const override;
81 
82  outcome::result<primitives::Justification> getBlockJustification(
83  const primitives::BlockId &block) const override;
84 
85  outcome::result<void> addBlockHeader(
86  const primitives::BlockHeader &header) override;
87 
88  outcome::result<void> addBlock(const primitives::Block &block) override;
89 
90  outcome::result<void> removeLeaf(
91  const primitives::BlockHash &block_hash) override;
92 
93  outcome::result<void> addExistingBlock(
94  const primitives::BlockHash &block_hash,
95  const primitives::BlockHeader &block_header) override;
96 
97  outcome::result<void> addBlockBody(
98  primitives::BlockNumber block_number,
99  const primitives::BlockHash &block_hash,
100  const primitives::BlockBody &body) override;
101 
102  outcome::result<void> finalize(
103  const primitives::BlockHash &block_hash,
104  const primitives::Justification &justification) override;
105 
106  BlockHashVecRes getBestChainFromBlock(const primitives::BlockHash &block,
107  uint64_t maximum) const override;
108 
109  BlockHashVecRes getDescendingChainToBlock(
110  const primitives::BlockHash &block, uint64_t maximum) const override;
111 
112  BlockHashVecRes getChainByBlocks(
113  const primitives::BlockHash &ancestor,
114  const primitives::BlockHash &descendant) const override;
115 
116  bool hasDirectChain(const primitives::BlockHash &ancestor,
117  const primitives::BlockHash &descendant) const override;
118 
119  primitives::BlockInfo deepestLeaf() const override;
120 
121  outcome::result<primitives::BlockInfo> getBestContaining(
122  const primitives::BlockHash &target_hash,
123  const std::optional<primitives::BlockNumber> &max_number)
124  const override;
125 
126  std::vector<primitives::BlockHash> getLeaves() const override;
127 
128  BlockHashVecRes getChildren(
129  const primitives::BlockHash &block) const override;
130 
131  primitives::BlockInfo getLastFinalized() const override;
132 
133  private:
139  std::shared_ptr<BlockHeaderRepository> header_repo,
140  std::shared_ptr<BlockStorage> storage,
141  std::unique_ptr<CachedTree> cached_tree,
142  std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer,
143  std::shared_ptr<crypto::Hasher> hasher,
146  extrinsic_events_engine,
147  std::shared_ptr<subscription::ExtrinsicEventKeyRepository>
148  extrinsic_event_key_repo,
149  std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
150  std::shared_ptr<const class JustificationStoragePolicy>
151  justification_storage_policy);
152 
157  outcome::result<primitives::BlockHash> walkBackUntilLess(
158  const primitives::BlockHash &start,
159  const primitives::BlockNumber &limit) const;
160 
164  std::vector<primitives::BlockHash> getLeavesSorted() const;
165 
166  outcome::result<void> prune(
167  const std::shared_ptr<TreeNode> &lastFinalizedNode);
168 
169  outcome::result<void> reorganize();
170 
171  std::shared_ptr<BlockHeaderRepository> header_repo_;
172  std::shared_ptr<BlockStorage> storage_;
173 
174  std::unique_ptr<CachedTree> tree_;
175 
176  std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer_;
177 
178  std::shared_ptr<crypto::Hasher> hasher_;
181  std::shared_ptr<subscription::ExtrinsicEventKeyRepository>
183  std::shared_ptr<storage::changes_trie::ChangesTracker>
185  std::shared_ptr<const class JustificationStoragePolicy>
187 
188  std::optional<primitives::BlockHash> genesis_block_hash_;
189 
190  log::Logger log_ = log::createLogger("BlockTree", "blockchain");
191 
192  // Metrics
198  };
199 } // namespace kagome::blockchain
200 
201 #endif // KAGOME_BLOCK_TREE_IMPL_HPP
std::shared_ptr< BlockStorage > storage_
std::shared_ptr< TelemetryService > createTelemetryService()
Returns preliminary initialized instance of telemetry service.
Definition: service.cpp:69
std::vector< Extrinsic > BlockBody
Definition: block.hpp:14
Block class represents polkadot block primitive.
Definition: block.hpp:19
primitives::events::ExtrinsicSubscriptionEnginePtr extrinsic_events_engine_
std::shared_ptr< crypto::Hasher > hasher_
std::shared_ptr< const class JustificationStoragePolicy > justification_storage_policy_
std::unique_ptr< Registry > RegistryPtr
Definition: metrics.hpp:15
RegistryPtr createRegistry()
uint32_t BlockNumber
Definition: common.hpp:18
std::shared_ptr< ChainSubscriptionEngine > ChainSubscriptionEnginePtr
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
metrics::Gauge * metric_finalized_block_height_
std::unique_ptr< CachedTree > tree_
std::shared_ptr< network::ExtrinsicObserver > extrinsic_observer_
std::shared_ptr< storage::changes_trie::ChangesTracker > trie_changes_tracker_
outcome::result< std::vector< primitives::BlockHash >> BlockHashVecRes
Definition: block_tree.hpp:32
std::optional< primitives::BlockHash > genesis_block_hash_
std::shared_ptr< ExtrinsicSubscriptionEngine > ExtrinsicSubscriptionEnginePtr
primitives::events::ChainSubscriptionEnginePtr chain_events_engine_
std::shared_ptr< subscription::ExtrinsicEventKeyRepository > extrinsic_event_key_repo_
boost::variant< BlockHash, BlockNumber > BlockId
Block id is the variant over BlockHash and BlockNumber.
Definition: block_id.hpp:18
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::shared_ptr< TelemetryService > Telemetry
Definition: service.hpp:86
A gauge metric to represent a value that can arbitrarily go up and down.
Definition: metrics.hpp:49
std::shared_ptr< BlockHeaderRepository > header_repo_