Kagome
Polkadot Runtime Engine in C++17
storage_code_provider.cpp
Go to the documentation of this file.
1 
7 
8 #include <boost/assert.hpp>
9 
10 #include "log/logger.hpp"
15 
16 namespace kagome::runtime {
17 
19  std::shared_ptr<const storage::trie::TrieStorage> storage,
20  std::shared_ptr<RuntimeUpgradeTracker> runtime_upgrade_tracker,
21  std::shared_ptr<const primitives::CodeSubstituteBlockIds>
22  code_substitutes,
23  std::shared_ptr<application::ChainSpec> chain_spec)
24  : storage_{std::move(storage)},
25  runtime_upgrade_tracker_{std::move(runtime_upgrade_tracker)},
26  known_code_substitutes_{std::move(code_substitutes)},
27  chain_spec_{std::move(chain_spec)},
28  logger_{log::createLogger("StorageCodeProvider", "runtime")} {
29  BOOST_ASSERT(storage_ != nullptr);
30  BOOST_ASSERT(runtime_upgrade_tracker_ != nullptr);
31  }
32 
33  outcome::result<gsl::span<const uint8_t>> StorageCodeProvider::getCodeAt(
34  const storage::trie::RootHash &state) const {
35  if (last_state_root_ != state) {
36  last_state_root_ = state;
37 
38  auto block_info =
39  runtime_upgrade_tracker_->getLastCodeUpdateBlockInfo(state);
40  if (block_info.has_value()) {
41  if (known_code_substitutes_->contains(block_info.value())) {
42  OUTCOME_TRY(
43  code,
44  chain_spec_->fetchCodeSubstituteByBlockInfo(block_info.value()));
45  OUTCOME_TRY(uncompressCodeIfNeeded(code, cached_code_));
46  return cached_code_;
47  }
48  }
49  OUTCOME_TRY(batch, storage_->getEphemeralBatchAt(state));
50  OUTCOME_TRY(setCodeFromBatch(*batch.get()));
51  }
52  return cached_code_;
53  }
54 
56  const storage::trie::EphemeralTrieBatch &batch) const {
57  OUTCOME_TRY(code, batch.get(storage::kRuntimeCodeKey));
58  OUTCOME_TRY(uncompressCodeIfNeeded(code.get(), cached_code_));
59  return outcome::success();
60  }
61 } // namespace kagome::runtime
virtual outcome::result< ConstValueView > get(const Key &key) const =0
Get value by key.
StorageCodeProvider(std::shared_ptr< const storage::trie::TrieStorage > storage, std::shared_ptr< RuntimeUpgradeTracker > runtime_upgrade_tracker, std::shared_ptr< const CodeSubstituteBlockIds > code_substitutes, std::shared_ptr< application::ChainSpec > chain_spec)
outcome::result< void > setCodeFromBatch(const storage::trie::EphemeralTrieBatch &batch) const
std::shared_ptr< const storage::trie::TrieStorage > storage_
std::shared_ptr< RuntimeUpgradeTracker > runtime_upgrade_tracker_
std::shared_ptr< application::ChainSpec > chain_spec_
std::shared_ptr< const CodeSubstituteBlockIds > known_code_substitutes_
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
const common::Buffer kRuntimeCodeKey
outcome::result< gsl::span< const uint8_t > > getCodeAt(const storage::trie::RootHash &state) const override
outcome::result< void > uncompressCodeIfNeeded(common::BufferView buf, common::Buffer &res)