-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathexecutor.hpp
57 lines (49 loc) · 1.64 KB
/
executor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "common/buffer.hpp"
#include "outcome/outcome.hpp"
#include "primitives/common.hpp"
#include "runtime/module_instance.hpp"
#include "runtime/runtime_context.hpp"
#include "runtime/runtime_properties_cache.hpp"
namespace kagome::runtime {
class Executor {
public:
Executor(std::shared_ptr<RuntimeContextFactory> ctx_factory,
std::optional<std::shared_ptr<RuntimePropertiesCache>> cache = {});
const RuntimeContextFactory &ctx() const {
return *ctx_factory_;
}
template <typename Res, typename... Args>
outcome::result<Res> call(RuntimeContext &ctx,
std::string_view name,
const Args &...args) {
auto code_hash = ctx.module_instance->getCodeHash();
auto call = [&]() {
return ctx.module_instance->callAndDecodeExportFunction<Res>(
ctx, name, args...);
};
if (!cache_) {
return call();
}
auto &cache = *cache_;
if constexpr (std::is_same_v<Res, primitives::Version>) {
[[likely]] if (name == "Core_version") {
return cache->getVersion(code_hash, call);
}
}
if constexpr (std::is_same_v<Res, primitives::OpaqueMetadata>) {
[[likely]] if (name == "Metadata_metadata") {
return cache->getMetadata(code_hash, call);
}
}
return call();
}
std::optional<std::shared_ptr<RuntimePropertiesCache>> cache_;
std::shared_ptr<const RuntimeContextFactory> ctx_factory_;
};
} // namespace kagome::runtime