Kagome
Polkadot Runtime Engine in C++17
runtime_upgrade_tracker_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_RUNTIME_WAVM_IMPL_RUNTIME_UPGRADE_TRACKER_HPP
7 #define KAGOME_CORE_RUNTIME_WAVM_IMPL_RUNTIME_UPGRADE_TRACKER_HPP
8 
10 
11 #include <memory>
12 
14 #include "log/logger.hpp"
15 #include "outcome/outcome.hpp"
17 #include "scale/tie.hpp"
19 #include "storage/trie/types.hpp"
20 
21 namespace kagome::blockchain {
22  class BlockHeaderRepository;
23  class BlockTree;
24  class BlockStorage;
25 } // namespace kagome::blockchain
26 
27 namespace kagome::runtime {
28 
30  public:
35  static outcome::result<std::unique_ptr<RuntimeUpgradeTrackerImpl>> create(
36  std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo,
37  std::shared_ptr<storage::BufferStorage> storage,
38  std::shared_ptr<const primitives::CodeSubstituteBlockIds>
39  code_substitutes,
40  std::shared_ptr<blockchain::BlockStorage> block_storage);
41 
43  SCALE_TIE(2);
44 
45  RuntimeUpgradeData() = default;
46 
47  template <typename BlockInfo, typename RootHash>
49  : block{std::forward<BlockInfo>(block)},
50  state{std::forward<RootHash>(state)} {}
51 
54  };
55 
56  void subscribeToBlockchainEvents(
57  std::shared_ptr<primitives::events::ChainSubscriptionEngine>
58  chain_sub_engine,
59  std::shared_ptr<const blockchain::BlockTree> block_tree);
60 
61  outcome::result<storage::trie::RootHash> getLastCodeUpdateState(
62  const primitives::BlockInfo &block) override;
63 
64  outcome::result<primitives::BlockInfo> getLastCodeUpdateBlockInfo(
65  const storage::trie::RootHash &state) const override;
66 
67  private:
69  std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo,
70  std::shared_ptr<storage::BufferStorage> storage,
71  std::shared_ptr<const primitives::CodeSubstituteBlockIds>
72  code_substitutes,
73  std::vector<RuntimeUpgradeData> &&saved_data,
74  std::shared_ptr<blockchain::BlockStorage> block_storage);
75 
76  outcome::result<bool> isStateInChain(
77  const primitives::BlockInfo &state,
78  const primitives::BlockInfo &chain_end) const noexcept;
79 
80  outcome::result<std::optional<storage::trie::RootHash>> findProperFork(
81  const primitives::BlockInfo &block,
82  std::vector<RuntimeUpgradeData>::const_reverse_iterator
83  latest_upgrade_it) const;
84 
85  bool hasCodeSubstitute(
86  const kagome::primitives::BlockInfo &block_info) const;
87 
88  outcome::result<storage::trie::RootHash> push(
89  const primitives::BlockHash &hash);
90 
91  void save();
92 
93  // assumption: insertions in the middle should be extremely rare, if any
94  // assumption: runtime upgrades are rare
95  std::vector<RuntimeUpgradeData> runtime_upgrades_;
96 
97  std::shared_ptr<primitives::events::ChainEventSubscriber>
99  std::shared_ptr<const blockchain::BlockTree> block_tree_;
100  std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo_;
101  std::shared_ptr<storage::BufferStorage> storage_;
102  std::shared_ptr<const primitives::CodeSubstituteBlockIds>
104  std::shared_ptr<blockchain::BlockStorage> block_storage_;
106  };
107 
108 } // namespace kagome::runtime
109 
110 #endif // KAGOME_CORE_RUNTIME_WAVM_IMPL_RUNTIME_UPGRADE_TRACKER_HPP
std::shared_ptr< primitives::events::ChainEventSubscriber > chain_subscription_
#define SCALE_TIE(N)
Definition: tie.hpp:11
primitives::BlockInfo BlockInfo
Definition: structs.hpp:29
std::shared_ptr< storage::BufferStorage > storage_
std::shared_ptr< const blockchain::BlockHeaderRepository > header_repo_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< const primitives::CodeSubstituteBlockIds > known_code_substitutes_
std::shared_ptr< blockchain::BlockStorage > block_storage_
std::shared_ptr< const blockchain::BlockTree > block_tree_
common::Hash256 RootHash
Definition: types.hpp:13