Kagome
Polkadot Runtime Engine in C++17
module_repository_impl.cpp
Go to the documentation of this file.
1 
7 
11 #include "runtime/module.hpp"
16 
17 namespace kagome::runtime {
19  using soralog::util::getThreadNumber;
20 
22  std::shared_ptr<RuntimeInstancesPool> runtime_instances_pool,
23  std::shared_ptr<RuntimeUpgradeTracker> runtime_upgrade_tracker,
24  std::shared_ptr<const ModuleFactory> module_factory,
25  std::shared_ptr<SingleModuleCache> last_compiled_module)
26  : runtime_instances_pool_{std::move(runtime_instances_pool)},
27  runtime_upgrade_tracker_{std::move(runtime_upgrade_tracker)},
28  module_factory_{std::move(module_factory)},
29  last_compiled_module_{std::move(last_compiled_module)},
30  logger_{log::createLogger("Module Repository", "runtime")} {
31  BOOST_ASSERT(runtime_instances_pool_);
32  BOOST_ASSERT(runtime_upgrade_tracker_);
33  BOOST_ASSERT(module_factory_);
34  BOOST_ASSERT(last_compiled_module_);
35  }
36 
37  outcome::result<std::shared_ptr<ModuleInstance>>
39  std::shared_ptr<const RuntimeCodeProvider> code_provider,
40  const primitives::BlockInfo &block,
41  const primitives::BlockHeader &header) {
42  KAGOME_PROFILE_START(code_retrieval)
43  OUTCOME_TRY(state, runtime_upgrade_tracker_->getLastCodeUpdateState(block));
44  KAGOME_PROFILE_END(code_retrieval)
45 
46  KAGOME_PROFILE_START(module_retrieval) {
47  // Add compiled module if any
48  if (auto module = last_compiled_module_->try_extract();
49  module.has_value()) {
50  runtime_instances_pool_->putModule(state, module.value());
51  }
52 
53  // Compile new module if required
54  if (auto opt_module = runtime_instances_pool_->getModule(state);
55  !opt_module.has_value()) {
56  SL_DEBUG(
57  logger_, "Runtime module cache miss for state {}", state.toHex());
58  auto code = code_provider->getCodeAt(state);
59  if (not code.has_value()) {
60  code = code_provider->getCodeAt(header.state_root);
61  }
62  if (not code.has_value()) {
63  return code.as_failure();
64  }
65  OUTCOME_TRY(new_module, module_factory_->make(code.value()));
66  runtime_instances_pool_->putModule(state, std::move(new_module));
67  }
68  }
69 
70  // Try acquire instance (instantiate if needed)
71  OUTCOME_TRY(runtime_instance, runtime_instances_pool_->tryAcquire(state));
72  KAGOME_PROFILE_END(module_retrieval)
73 
74  return std::move(runtime_instance);
75  }
76 } // namespace kagome::runtime
#define KAGOME_PROFILE_END(scope)
std::shared_ptr< RuntimeUpgradeTracker > runtime_upgrade_tracker_
std::shared_ptr< SingleModuleCache > last_compiled_module_
storage::trie::RootHash state_root
root of the Merkle tree
size_t ThreadNumber
Definition: common.hpp:20
outcome::result< std::shared_ptr< ModuleInstance > > getInstanceAt(std::shared_ptr< const RuntimeCodeProvider > code_provider, const primitives::BlockInfo &block, const primitives::BlockHeader &header) override
Returns a module instance for runtime at the.
ModuleRepositoryImpl(std::shared_ptr< RuntimeInstancesPool > runtime_instances_pool, std::shared_ptr< RuntimeUpgradeTracker > runtime_upgrade_tracker, std::shared_ptr< const ModuleFactory > module_factory, std::shared_ptr< SingleModuleCache > last_compiled_module)
std::shared_ptr< RuntimeInstancesPool > runtime_instances_pool_
#define KAGOME_PROFILE_START(scope)
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::shared_ptr< const ModuleFactory > module_factory_