Kagome
Polkadot Runtime Engine in C++17
core_api_factory_impl.cpp
Go to the documentation of this file.
1 
7 
20 #include "runtime/wavm/module.hpp"
23 
24 namespace kagome::runtime::wavm {
25 
26  class OneModuleRepository final : public ModuleRepository {
27  public:
29  std::shared_ptr<CompartmentWrapper> compartment,
30  std::shared_ptr<ModuleParams> module_params,
31  std::shared_ptr<IntrinsicModule> intrinsic_module,
32  std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory,
33  gsl::span<const uint8_t> code,
34  const common::Hash256 &code_hash,
35  std::shared_ptr<SingleModuleCache> last_compiled_module)
36  : instance_env_factory_{std::move(instance_env_factory)},
37  compartment_{compartment},
38  module_params_{std::move(module_params)},
39  intrinsic_module_{std::move(intrinsic_module)},
40  code_{code},
41  last_compiled_module_{last_compiled_module} {
42  BOOST_ASSERT(instance_env_factory_);
43  BOOST_ASSERT(compartment_);
44  BOOST_ASSERT(intrinsic_module_);
45  BOOST_ASSERT(last_compiled_module_);
46  }
47 
48  outcome::result<std::shared_ptr<ModuleInstance>> getInstanceAt(
49  std::shared_ptr<const RuntimeCodeProvider>,
50  const primitives::BlockInfo &,
51  const primitives::BlockHeader &) override {
52  if (instance_ == nullptr) {
57  code_,
58  code_hash_);
59  OUTCOME_TRY(inst, module->instantiate());
60  last_compiled_module_->set(std::move(module));
61  instance_ = std::move(inst);
62  }
63  return instance_;
64  }
65 
66  private:
67  std::shared_ptr<ModuleInstance> instance_;
68  std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory_;
69  std::shared_ptr<CompartmentWrapper> compartment_;
70  std::shared_ptr<ModuleParams> module_params_;
71  std::shared_ptr<IntrinsicModule> intrinsic_module_;
72  gsl::span<const uint8_t> code_;
74  std::shared_ptr<SingleModuleCache> last_compiled_module_;
75  };
76 
77  class OneCodeProvider final : public RuntimeCodeProvider {
78  public:
79  explicit OneCodeProvider(gsl::span<const uint8_t> code) : code_{code} {}
80 
81  virtual outcome::result<gsl::span<const uint8_t>> getCodeAt(
82  const storage::trie::RootHash &) const {
83  return code_;
84  }
85 
86  private:
87  gsl::span<const uint8_t> code_;
88  };
89 
91  std::shared_ptr<CompartmentWrapper> compartment,
92  std::shared_ptr<ModuleParams> module_params,
93  std::shared_ptr<IntrinsicModule> intrinsic_module,
94  std::shared_ptr<storage::trie::TrieStorage> storage,
95  std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo,
96  std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory,
97  std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
98  std::shared_ptr<SingleModuleCache> last_compiled_module,
99  std::shared_ptr<runtime::RuntimePropertiesCache> cache)
100  : instance_env_factory_{std::move(instance_env_factory)},
101  compartment_{compartment},
102  module_params_{std::move(module_params)},
103  intrinsic_module_{std::move(intrinsic_module)},
104  storage_{std::move(storage)},
105  block_header_repo_{std::move(block_header_repo)},
106  changes_tracker_{std::move(changes_tracker)},
107  last_compiled_module_{last_compiled_module},
108  cache_(std::move(cache)) {
109  BOOST_ASSERT(compartment_);
110  BOOST_ASSERT(module_params_);
111  BOOST_ASSERT(intrinsic_module_);
112  BOOST_ASSERT(storage_);
113  BOOST_ASSERT(block_header_repo_);
114  BOOST_ASSERT(instance_env_factory_);
115  BOOST_ASSERT(changes_tracker_);
116  BOOST_ASSERT(last_compiled_module_);
117  BOOST_ASSERT(cache_);
118  }
119 
120  std::unique_ptr<Core> CoreApiFactoryImpl::make(
121  std::shared_ptr<const crypto::Hasher> hasher,
122  const std::vector<uint8_t> &runtime_code) const {
123  auto code_hash = hasher->sha2_256(runtime_code);
124  auto env_factory = std::make_shared<runtime::RuntimeEnvironmentFactory>(
125  std::make_shared<OneCodeProvider>(runtime_code),
126  std::make_shared<OneModuleRepository>(
127  compartment_,
131  gsl::span<const uint8_t>{
132  runtime_code.data(),
133  static_cast<gsl::span<const uint8_t>::index_type>(
134  runtime_code.size())},
135  code_hash,
138  auto executor = std::make_unique<runtime::Executor>(env_factory, cache_);
139  return std::make_unique<CoreImpl>(
140  std::move(executor), changes_tracker_, block_header_repo_);
141  }
142 
143 } // namespace kagome::runtime::wavm
virtual outcome::result< gsl::span< const uint8_t > > getCodeAt(const storage::trie::RootHash &) const
std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker_
std::unique_ptr< Core > make(std::shared_ptr< const crypto::Hasher > hasher, const std::vector< uint8_t > &runtime_code) const override
std::shared_ptr< ModuleParams > module_params_
std::shared_ptr< ModuleParams > module_params_
std::shared_ptr< CompartmentWrapper > compartment_
std::shared_ptr< CompartmentWrapper > compartment_
CoreApiFactoryImpl(std::shared_ptr< CompartmentWrapper > compartment, std::shared_ptr< ModuleParams > module_params, std::shared_ptr< IntrinsicModule > intrinsic_module, std::shared_ptr< storage::trie::TrieStorage > storage, std::shared_ptr< blockchain::BlockHeaderRepository > block_header_repo, std::shared_ptr< const InstanceEnvironmentFactory > instance_env_factory, std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker, std::shared_ptr< SingleModuleCache > last_compiled_module, std::shared_ptr< runtime::RuntimePropertiesCache > cache)
std::shared_ptr< runtime::RuntimePropertiesCache > cache_
std::shared_ptr< ModuleInstance > instance_
OneCodeProvider(gsl::span< const uint8_t > code)
static std::unique_ptr< ModuleImpl > compileFrom(std::shared_ptr< CompartmentWrapper > compartment, ModuleParams &module_params, std::shared_ptr< IntrinsicModule > intrinsic_module, std::shared_ptr< const InstanceEnvironmentFactory > env_factory, gsl::span< const uint8_t > code, const common::Hash256 &code_hash)
Definition: module.cpp:23
std::shared_ptr< IntrinsicModule > intrinsic_module_
std::shared_ptr< const InstanceEnvironmentFactory > instance_env_factory_
std::shared_ptr< IntrinsicModule > intrinsic_module_
std::shared_ptr< SingleModuleCache > last_compiled_module_
std::shared_ptr< SingleModuleCache > last_compiled_module_
std::shared_ptr< storage::trie::TrieStorage > storage_
OneModuleRepository(std::shared_ptr< CompartmentWrapper > compartment, std::shared_ptr< ModuleParams > module_params, std::shared_ptr< IntrinsicModule > intrinsic_module, std::shared_ptr< const InstanceEnvironmentFactory > instance_env_factory, gsl::span< const uint8_t > code, const common::Hash256 &code_hash, std::shared_ptr< SingleModuleCache > last_compiled_module)
outcome::result< std::shared_ptr< ModuleInstance > > getInstanceAt(std::shared_ptr< const RuntimeCodeProvider >, const primitives::BlockInfo &, const primitives::BlockHeader &) override
Returns a module instance for runtime at the.
std::shared_ptr< const InstanceEnvironmentFactory > instance_env_factory_
std::shared_ptr< blockchain::BlockHeaderRepository > block_header_repo_