Kagome
Polkadot Runtime Engine in C++17
core_api_factory_impl.cpp
Go to the documentation of this file.
1 
7 
16 
17 namespace kagome::runtime::binaryen {
18 
19  class OneModuleRepository final : public ModuleRepository {
20  public:
22  const std::vector<uint8_t> &code,
23  std::shared_ptr<const InstanceEnvironmentFactory> env_factory,
24  const common::Hash256 &code_hash)
25  : env_factory_{std::move(env_factory)},
26  code_{code},
27  code_hash_(code_hash) {
28  BOOST_ASSERT(env_factory_);
29  }
30 
31  outcome::result<std::shared_ptr<ModuleInstance>> getInstanceAt(
32  std::shared_ptr<const RuntimeCodeProvider>,
33  const primitives::BlockInfo &,
34  const primitives::BlockHeader &) override {
35  if (instance_ == nullptr) {
36  OUTCOME_TRY(
37  module,
39  OUTCOME_TRY(inst, module->instantiate());
40  instance_ = std::move(inst);
41  }
42  return instance_;
43  }
44 
45  private:
46  std::shared_ptr<ModuleInstance> instance_;
47  std::shared_ptr<const InstanceEnvironmentFactory> env_factory_;
48  const std::vector<uint8_t> &code_;
50  };
51 
52  class OneCodeProvider final : public RuntimeCodeProvider {
53  public:
54  explicit OneCodeProvider(gsl::span<const uint8_t> code) : code_{code} {}
55 
56  virtual outcome::result<gsl::span<const uint8_t>> getCodeAt(
57  const storage::trie::RootHash &) const {
58  return code_;
59  }
60 
61  private:
62  gsl::span<const uint8_t> code_;
63  };
64 
66  std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory,
67  std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo,
68  std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
69  std::shared_ptr<runtime::RuntimePropertiesCache> cache)
70  : instance_env_factory_{std::move(instance_env_factory)},
71  header_repo_{std::move(header_repo)},
72  changes_tracker_{std::move(changes_tracker)},
73  cache_(std::move(cache)) {
74  BOOST_ASSERT(instance_env_factory_ != nullptr);
75  BOOST_ASSERT(header_repo_ != nullptr);
76  BOOST_ASSERT(changes_tracker_ != nullptr);
77  BOOST_ASSERT(cache_ != nullptr);
78  }
79 
80  std::unique_ptr<Core> CoreApiFactoryImpl::make(
81  std::shared_ptr<const crypto::Hasher> hasher,
82  const std::vector<uint8_t> &runtime_code) const {
83  auto code_hash = hasher->sha2_256(runtime_code);
84  auto env_factory = std::make_shared<runtime::RuntimeEnvironmentFactory>(
85  std::make_shared<OneCodeProvider>(runtime_code),
86  std::make_shared<OneModuleRepository>(
87  runtime_code, instance_env_factory_, code_hash),
88  header_repo_);
89  auto executor = std::make_unique<Executor>(env_factory, cache_);
90  return std::make_unique<CoreImpl>(
91  std::move(executor), changes_tracker_, header_repo_);
92  }
93 
94 } // namespace kagome::runtime::binaryen
std::unique_ptr< Core > make(std::shared_ptr< const crypto::Hasher > hasher, const std::vector< uint8_t > &runtime_code) const override
virtual outcome::result< gsl::span< const uint8_t > > getCodeAt(const storage::trie::RootHash &) const
std::shared_ptr< runtime::RuntimePropertiesCache > cache_
std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker_
std::shared_ptr< const InstanceEnvironmentFactory > env_factory_
OneModuleRepository(const std::vector< uint8_t > &code, std::shared_ptr< const InstanceEnvironmentFactory > env_factory, const common::Hash256 &code_hash)
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.
CoreApiFactoryImpl(std::shared_ptr< const InstanceEnvironmentFactory > instance_env_factory, std::shared_ptr< const blockchain::BlockHeaderRepository > header_repo, std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker, std::shared_ptr< runtime::RuntimePropertiesCache > cache)
std::shared_ptr< const blockchain::BlockHeaderRepository > header_repo_
static outcome::result< std::unique_ptr< ModuleImpl > > createFromCode(const std::vector< uint8_t > &code, std::shared_ptr< const InstanceEnvironmentFactory > env_factory_, const common::Hash256 &code_hash)
Definition: module_impl.cpp:46
OneCodeProvider(gsl::span< const uint8_t > code)
std::shared_ptr< const InstanceEnvironmentFactory > instance_env_factory_