Kagome
Polkadot Runtime Engine in C++17
misc_extension.cpp
Go to the documentation of this file.
1 
7 
8 #include "primitives/version.hpp"
14 #include "scale/scale.hpp"
15 
16 namespace kagome::host_api {
17 
19  uint64_t chain_id,
20  std::shared_ptr<const crypto::Hasher> hasher,
21  std::shared_ptr<const runtime::MemoryProvider> memory_provider,
22  std::shared_ptr<const runtime::CoreApiFactory> core_factory)
23  : hasher_{std::move(hasher)},
24  memory_provider_{std::move(memory_provider)},
25  core_factory_{std::move(core_factory)},
26  logger_{log::createLogger("MiscExtension", "misc_extension")} {
27  BOOST_ASSERT(hasher_);
28  BOOST_ASSERT(core_factory_);
29  BOOST_ASSERT(memory_provider_);
30  }
31 
33  runtime::WasmSpan data) const {
34  auto [ptr, len] = runtime::splitSpan(data);
35  auto &memory = memory_provider_->getCurrentMemory()->get();
36 
37  auto code = memory.loadN(ptr, len);
38  common::Buffer uncompressed_code;
39  auto uncompress_res =
40  runtime::uncompressCodeIfNeeded(code, uncompressed_code);
41 
42  static const auto kErrorRes =
43  scale::encode<std::optional<primitives::Version>>(std::nullopt).value();
44 
45  if (uncompress_res.has_error()) {
46  SL_ERROR(logger_,
47  "Error uncompressing code: {}",
48  uncompress_res.error().message());
49  return memory.storeBuffer(kErrorRes);
50  }
51 
52  auto core_api = core_factory_->make(hasher_, uncompressed_code.asVector());
53  auto version_res = core_api->version();
54  SL_TRACE_FUNC_CALL(logger_, version_res.has_value(), data);
55 
56  if (version_res.has_value()) {
57  auto enc_version_res = scale::encode(
58  std::make_optional(scale::encode(version_res.value()).value()));
59  if (enc_version_res.has_error()) {
60  logger_->error(
61  "Error encoding ext_misc_runtime_version_version_1 result: {}",
62  enc_version_res.error().message());
63  return memory.storeBuffer(kErrorRes);
64  }
65  auto res_span = memory.storeBuffer(enc_version_res.value());
66  return res_span;
67  }
68  logger_->error("Error inside Core_version: {}",
69  version_res.error().message());
70  return memory.storeBuffer(kErrorRes);
71  }
72 
74  runtime::WasmSpan data) const {
75  auto [ptr, len] = runtime::splitSpan(data);
76  auto buf = memory_provider_->getCurrentMemory()->get().loadN(ptr, len);
77  logger_->info("hex: {}", buf.toHex());
78  }
79 
80  void MiscExtension::ext_misc_print_num_version_1(uint64_t value) const {
81  logger_->info("num: {}", value);
82  }
83 
85  runtime::WasmSpan data) const {
86  auto [ptr, len] = runtime::splitSpan(data);
87  auto buf = memory_provider_->getCurrentMemory()->get().loadN(ptr, len);
88  logger_->info("utf8: {}", buf.toString());
89  }
90 
91 } // namespace kagome::host_api
runtime::WasmSpan ext_misc_runtime_version_version_1(runtime::WasmSpan data) const
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
static constexpr std::pair< WasmPointer, WasmSize > splitSpan(WasmSpan span)
Definition: types.hpp:52
const std::vector< uint8_t > & asVector() const
getter for vector of bytes
Definition: buffer.hpp:135
#define SL_TRACE_FUNC_CALL(logger, ret,...)
Definition: logger.hpp:142
std::shared_ptr< const runtime::CoreApiFactory > core_factory_
std::shared_ptr< const crypto::Hasher > hasher_
void ext_misc_print_utf8_version_1(runtime::WasmSpan data) const
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
MiscExtension(uint64_t chain_id, std::shared_ptr< const crypto::Hasher > hasher, std::shared_ptr< const runtime::MemoryProvider > memory_provider, std::shared_ptr< const runtime::CoreApiFactory > core_provider)
std::shared_ptr< const runtime::MemoryProvider > memory_provider_
void ext_misc_print_num_version_1(uint64_t value) const
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
void ext_misc_print_hex_version_1(runtime::WasmSpan data) const
outcome::result< void > uncompressCodeIfNeeded(common::BufferView buf, common::Buffer &res)