6 #ifndef KAGOME_CORE_RUNTIME_COMMON_EXECUTOR_HPP 7 #define KAGOME_CORE_RUNTIME_COMMON_EXECUTOR_HPP 27 #include "scale/scale.hpp" 32 #if __has_builtin(__builtin_expect) 33 #define likely(x) __builtin_expect((x), 1) 51 Executor(std::shared_ptr<RuntimeEnvironmentFactory> env_factory,
52 std::shared_ptr<RuntimePropertiesCache> cache)
65 template <
typename Result,
typename... Args>
69 std::string_view name,
73 env_factory_->start(block_info, storage_state)->persistent().make());
74 auto res = callInternal<Result>(*
env, name, std::forward<Args>(args)...);
77 if constexpr (std::is_void_v<Result>) {
92 template <
typename Result,
typename... Args>
94 std::string_view name, Args &&...args) {
96 OUTCOME_TRY(
env, env_template->persistent().make());
97 auto res = callInternal<Result>(*
env, name, std::forward<Args>(args)...);
100 if constexpr (std::is_void_v<Result>) {
115 template <
typename Result,
typename... Args>
118 std::string_view name,
120 OUTCOME_TRY(env_template,
env_factory_->start(block_hash));
121 OUTCOME_TRY(
env, env_template->persistent().make());
122 auto res = callInternal<Result>(*
env, name, std::forward<Args>(args)...);
125 if constexpr (std::is_void_v<Result>) {
141 template <
typename Result,
typename... Args>
144 std::string_view name,
146 OUTCOME_TRY(
env,
env_factory_->start(block_info, storage_state)->make());
147 return callMediateInternal<Result>(
148 *
env, name, std::forward<Args>(args)...);
156 template <
typename Result,
typename... Args>
158 std::string_view name,
160 OUTCOME_TRY(env_template,
env_factory_->start(block_hash));
161 OUTCOME_TRY(
env, env_template->make());
162 return callMediateInternal<Result>(
163 *
env, name, std::forward<Args>(args)...);
171 template <
typename Result,
typename... Args>
175 OUTCOME_TRY(
env, env_template->make());
176 return callMediateInternal<Result>(
177 *
env, name, std::forward<Args>(args)...);
182 std::string_view name,
184 OUTCOME_TRY(env_template,
env_factory_->start(block_hash));
185 OUTCOME_TRY(
env, env_template->make());
187 auto &memory =
env->memory_provider->getCurrentMemory()->get();
192 env->module_instance->callExportFunction(name, encoded_args);
195 OUTCOME_TRY(span, result_span);
197 OUTCOME_TRY(
env->module_instance->resetEnvironment());
198 auto result = memory.loadN(span.ptr, span.size);
211 template <
typename Result,
typename... Args>
213 std::string_view name,
215 if constexpr (std::is_same_v<Result, primitives::Version>) {
216 if (
likely(name ==
"Core_version")) {
218 return callInternal<Result>(
env, name, std::forward<Args>(args)...);
223 if constexpr (std::is_same_v<Result, primitives::OpaqueMetadata>) {
224 if (
likely(name ==
"Metadata_metadata")) {
226 return callInternal<Result>(
env, name, std::forward<Args>(args)...);
231 return callInternal<Result>(
env, name, std::forward<Args>(args)...);
241 template <
typename Result,
typename... Args>
243 std::string_view name,
248 if constexpr (
sizeof...(args) > 0) {
249 OUTCOME_TRY(res, scale::encode(std::forward<Args>(args)...));
250 encoded_args.put(std::move(res));
259 OUTCOME_TRY(span, result_span);
263 if constexpr (std::is_void_v<Result>) {
264 return outcome::success();
266 auto result = memory.loadN(span.ptr, span.size);
268 scale::ScaleDecoderStream s(result);
274 "Runtime API call result size exceeds the size of the " 275 "type to initialize {}",
276 typeid(Result).name());
277 return outcome::failure(std::errc::illegal_byte_sequence);
279 return outcome::success(std::move(t));
280 }
catch (std::system_error &e) {
281 return outcome::failure(e.code());
291 "Current batch should always be persistent for a persistent call");
294 OUTCOME_TRY(new_state_root, persistent_batch->commit());
296 "Runtime call committed new state with hash {}",
297 new_state_root.toHex());
299 return std::move(new_state_root);
303 std::shared_ptr<RuntimePropertiesCache>
cache_;
311 #endif // KAGOME_CORE_RUNTIME_COMMON_EXECUTOR_HPP #define KAGOME_PROFILE_END(scope)
Class represents arbitrary (including empty) byte buffer.
outcome::result< Result > callAt(primitives::BlockHash const &block_hash, std::string_view name, Args &&...args)
const std::shared_ptr< const MemoryProvider > memory_provider
outcome::result< std::unique_ptr< PersistentTrieBatch > > persistent_batch(const std::unique_ptr< TrieStorageImpl > &trie, const RootHash &hash)
Executor(std::shared_ptr< RuntimeEnvironmentFactory > env_factory, std::shared_ptr< RuntimePropertiesCache > cache)
outcome::result< Result > callAt(primitives::BlockInfo const &block_info, storage::trie::RootHash const &storage_state, std::string_view name, Args &&...args)
outcome::result< Result > callAtGenesis(std::string_view name, Args &&...args)
static const wasm::Name env
outcome::result< Result > callInternal(RuntimeEnvironment &env, std::string_view name, Args &&...args)
const std::shared_ptr< TrieStorageProvider > storage_provider
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
std::shared_ptr< soralog::Logger > Logger
std::shared_ptr< RuntimeEnvironmentFactory > env_factory_
outcome::result< Result > callMediateInternal(RuntimeEnvironment &env, std::string_view name, Args &&...args)
outcome::result< PersistentResult< Result > > persistentCallAt(primitives::BlockHash const &block_hash, std::string_view name, Args &&...args)
outcome::result< PersistentResult< Result > > persistentCallAtGenesis(std::string_view name, Args &&...args)
outcome::result< common::Buffer > callAtRaw(const primitives::BlockHash &block_hash, std::string_view name, const common::Buffer &encoded_args) override
std::shared_ptr< RuntimePropertiesCache > cache_
#define KAGOME_PROFILE_START(scope)
Logger createLogger(const std::string &tag)
outcome::result< PersistentResult< Result > > persistentCallAt(primitives::BlockInfo const &block_info, storage::trie::RootHash const &storage_state, std::string_view name, Args &&...args)
const std::shared_ptr< ModuleInstance > module_instance
outcome::result< storage::trie::RootHash > commitState(const RuntimeEnvironment &env)