Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/missing host apis #1545

Merged
merged 6 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions core/host_api/host_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,9 @@ namespace kagome::host_api {
* @return a pointer-size indicating the SCALE encoded Option containing the
* value.
*/
virtual runtime::WasmSpan ext_default_child_storage_get_version_1(
runtime::WasmSpan child_storage_key, runtime::WasmSpan key) const = 0;
[[nodiscard]] virtual runtime::WasmSpan
ext_default_child_storage_get_version_1(runtime::WasmSpan child_storage_key,
runtime::WasmSpan key) const = 0;

/**
* @brief Clears the storage of the given key and its value from the child
Expand All @@ -617,7 +618,8 @@ namespace kagome::host_api {
* the next key in lexicographic order.
* Returns None if the entry cannot be found.
*/
virtual runtime::WasmSpan ext_default_child_storage_next_key_version_1(
[[nodiscard]] virtual runtime::WasmSpan
ext_default_child_storage_next_key_version_1(
runtime::WasmSpan child_storage_key, runtime::WasmSpan key) const = 0;

/**
Expand All @@ -626,9 +628,22 @@ namespace kagome::host_api {
* @param child_storage_key a pointer-size indicating the child storage key
* @return a pointer-size indicating the SCALE encoded storage root.
*/
virtual runtime::WasmSpan ext_default_child_storage_root_version_1(
[[nodiscard]] virtual runtime::WasmSpan
ext_default_child_storage_root_version_1(
runtime::WasmSpan child_storage_key) const = 0;

/**
* @brief Commits all existing operations and computes the resulting child
* storage root.
* @param child_storage_key a pointer-size indicating the child storage key
* @param state_version a way to calculate the root
* @return a pointer-size indicating the SCALE encoded storage root.
*/
[[nodiscard]] virtual runtime::WasmSpan
ext_default_child_storage_root_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmI32 state_version) const = 0;

/**
* @brief Clears the child storage of each key/value pair where the key
* starts with the given prefix.
Expand All @@ -638,6 +653,19 @@ namespace kagome::host_api {
virtual void ext_default_child_storage_clear_prefix_version_1(
runtime::WasmSpan child_storage_key, runtime::WasmSpan prefix) = 0;

/**
* @brief Clears the child storage of each key/value pair where the key
* starts with the given prefix.
* @param child_storage_key a pointer-size indicating the child storage key
* @param limit is an optional number of records to remove
* @param prefix a pointer-size indicating the prefix
* @return pointer to number of records removed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see an enum in Rust, is it encoded to just the number of records?

*/
virtual runtime::WasmSpan ext_default_child_storage_clear_prefix_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan prefix,
runtime::WasmSpan limit) = 0;

/**
* @brief Gets the given key from storage, placing the value into a buffer
* and returning the number of bytes that the entry in storage has beyond
Expand Down Expand Up @@ -677,6 +705,15 @@ namespace kagome::host_api {
*/
virtual void ext_default_child_storage_storage_kill_version_1(
runtime::WasmSpan child_storage_key) = 0;

/**
* @brief Clears child storage
* @param child_storage_key a pointer-size indicating the child storage key
* @param limit is an optional number of records allowed to remove
* @return pointer to int32 with a number of records removed
*/
virtual runtime::WasmSpan ext_default_child_storage_storage_kill_version_3(
runtime::WasmSpan child_storage_key, runtime::WasmSpan limit) = 0;
};
} // namespace kagome::host_api

Expand Down
2 changes: 2 additions & 0 deletions core/host_api/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ kagome_install(misc_extension)

add_library(storage_extension
storage_extension.cpp
storage_util.cpp
)
target_link_libraries(storage_extension
blob
Expand All @@ -71,6 +72,7 @@ kagome_install(storage_extension)

add_library(child_storage_extension
child_storage_extension.cpp
storage_util.cpp
)
target_link_libraries(child_storage_extension
blob
Expand Down
99 changes: 96 additions & 3 deletions core/host_api/impl/child_storage_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "host_api/impl/child_storage_extension.hpp"

#include "common/monadic_utils.hpp"
#include "host_api/impl/storage_util.hpp"
#include "runtime/common/runtime_transaction_error.hpp"
#include "runtime/memory_provider.hpp"
#include "runtime/ptr_size.hpp"
Expand Down Expand Up @@ -207,22 +208,31 @@ namespace kagome::host_api {
runtime::WasmSpan
ChildStorageExtension::ext_default_child_storage_root_version_1(
runtime::WasmSpan child_storage_key) const {
return ext_default_child_storage_root_version_2(child_storage_key,
runtime::WasmI32(0));
}

runtime::WasmSpan
ChildStorageExtension::ext_default_child_storage_root_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmI32 state_version) const {
auto &memory = memory_provider_->getCurrentMemory()->get();
auto child_key_buffer = loadBuffer(memory, child_storage_key);
auto prefixed_child_key = make_prefixed_child_storage_key(child_key_buffer);
auto child_batch =
storage_provider_->getMutableChildBatchAt(prefixed_child_key.value())
.value();

auto res = child_batch.get().commit(storage::trie::StateVersion::V0);
auto version = detail::toStateVersion(state_version);
auto res = child_batch.get().commit(version);

if (res.has_error()) {
logger_->error(
"ext_default_child_storage_root resulted with an error: {}",
res.error());
}
const auto &root = res.value();
SL_TRACE_FUNC_CALL(logger_, root, child_key_buffer);
SL_TRACE_FUNC_CALL(logger_, root, child_key_buffer, state_version);
return memory.storeBuffer(root);
}

Expand All @@ -249,6 +259,51 @@ namespace kagome::host_api {
}
}

runtime::WasmSpan
ChildStorageExtension::ext_default_child_storage_clear_prefix_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan prefix,
runtime::WasmSpan limit) {
auto &memory = memory_provider_->getCurrentMemory()->get();
auto [child_key_buffer, prefix_buffer] =
loadBuffer(memory, child_storage_key, prefix);

auto [limit_ptr, limit_size] = runtime::PtrSize(limit);
auto enc_limit = memory.loadN(limit_ptr, limit_size);
auto limit_res = scale::decode<std::optional<uint32_t>>(enc_limit);

if (!limit_res) {
auto msg = fmt::format(
"ext_default_child_storage_clear_prefix_version_2 failed at decoding "
"second argument: {}",
limit_res.error());
logger_->error(msg);
throw std::runtime_error(msg);
}
auto limit_opt = std::move(limit_res.value());
auto result = executeOnMutChildStorage<std::tuple<bool, uint32_t>>(
child_key_buffer,
[limit_opt](auto &child_batch, auto &prefix) {
return child_batch.clearPrefix(prefix, limit_opt);
},
prefix_buffer);

if (!result) {
logger_->error(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not abort the execution of this runtime call with an exception?

"ext_default_child_storage_clear_prefix_version_2 failed with "
"reason: {}",
result.error());
}
uint32_t removed = std::get<1>(result.value());
if (limit_opt) {
SL_TRACE_FUNC_CALL(logger_, removed, child_key_buffer, limit_opt.value());
} else {
SL_TRACE_FUNC_CALL(
logger_, removed, child_key_buffer, std::string_view{"none"});
}
return memory.storeBuffer(scale::encode(removed).value());
}

runtime::WasmSpan
ChildStorageExtension::ext_default_child_storage_read_version_1(
runtime::WasmSpan child_storage_key,
Expand Down Expand Up @@ -326,7 +381,6 @@ namespace kagome::host_api {
runtime::WasmSpan child_storage_key) {
auto &memory = memory_provider_->getCurrentMemory()->get();
auto child_key_buffer = loadBuffer(memory, child_storage_key);

SL_TRACE_VOID_FUNC_CALL(logger_, child_key_buffer);

auto result = executeOnMutChildStorage<std::tuple<bool, uint32_t>>(
Expand All @@ -342,4 +396,43 @@ namespace kagome::host_api {
}
}

runtime::WasmSpan
ChildStorageExtension::ext_default_child_storage_storage_kill_version_3(
runtime::WasmSpan child_storage_key, runtime::WasmSpan limit) {
auto &memory = memory_provider_->getCurrentMemory()->get();
auto child_key_buffer = loadBuffer(memory, child_storage_key);

auto [limit_ptr, limit_size] = runtime::PtrSize(limit);
auto enc_limit = memory.loadN(limit_ptr, limit_size);
auto limit_res = scale::decode<std::optional<uint32_t>>(enc_limit);

if (!limit_res) {
auto msg = fmt::format(
"ext_default_child_storage_storage_kill_version_3 failed at decoding "
"second argument: {}",
limit_res.error());
logger_->error(msg);
throw std::runtime_error(msg);
}
auto limit_opt = std::move(limit_res.value());
auto result = executeOnMutChildStorage<std::tuple<bool, uint32_t>>(
child_key_buffer, [limit_opt](auto &child_batch) {
return child_batch.clearPrefix({}, limit_opt);
});
if (!result) {
logger_->error(
"ext_default_child_storage_storage_kill_version_3 failed with "
"reason: {}",
result.error());
}
uint32_t removed = std::get<1>(result.value());
if (limit_opt) {
SL_TRACE_FUNC_CALL(logger_, removed, child_key_buffer, limit_opt.value());
} else {
SL_TRACE_FUNC_CALL(
logger_, removed, child_key_buffer, std::string_view{"none"});
}
return memory.storeBuffer(scale::encode(removed).value());
}

} // namespace kagome::host_api
21 changes: 21 additions & 0 deletions core/host_api/impl/child_storage_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,27 @@ namespace kagome::host_api {
runtime::WasmSpan ext_default_child_storage_root_version_1(
runtime::WasmSpan child_storage_key) const;

/**
* @see HostApi::ext_default_child_storage_root_version_2
*/
runtime::WasmSpan ext_default_child_storage_root_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmI32 state_version) const;

/**
* @see HostApi::ext_default_child_storage_clear_prefix_version_1
*/
void ext_default_child_storage_clear_prefix_version_1(
runtime::WasmSpan child_storage_key, runtime::WasmSpan prefix);

/**
* @see HostApi::ext_default_child_storage_clear_prefix_version_2
*/
runtime::WasmSpan ext_default_child_storage_clear_prefix_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan prefix,
runtime::WasmSpan limit);

/**
* @see HostApi::ext_default_child_storage_read_version_1
*/
Expand All @@ -89,6 +104,12 @@ namespace kagome::host_api {
void ext_default_child_storage_storage_kill_version_1(
runtime::WasmSpan child_storage_key);

/**
* @see HostApi::ext_default_child_storage_storage_kill_version_3
*/
runtime::WasmSpan ext_default_child_storage_storage_kill_version_3(
runtime::WasmSpan child_storage_key, runtime::WasmSpan limit);

private:
std::shared_ptr<runtime::TrieStorageProvider> storage_provider_;
std::shared_ptr<const runtime::MemoryProvider> memory_provider_;
Expand Down
23 changes: 23 additions & 0 deletions core/host_api/impl/host_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,28 @@ namespace kagome::host_api {
child_storage_key);
}

runtime::WasmSpan HostApiImpl::ext_default_child_storage_root_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmI32 state_version) const {
return child_storage_ext_.ext_default_child_storage_root_version_2(
child_storage_key, state_version);
}

void HostApiImpl::ext_default_child_storage_clear_prefix_version_1(
runtime::WasmSpan child_storage_key, runtime::WasmSpan prefix) {
return child_storage_ext_.ext_default_child_storage_clear_prefix_version_1(
child_storage_key, prefix);
}

runtime::WasmSpan
HostApiImpl::ext_default_child_storage_clear_prefix_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan prefix,
runtime::WasmSpan limit) {
return child_storage_ext_.ext_default_child_storage_clear_prefix_version_2(
child_storage_key, prefix, limit);
}

runtime::WasmSpan HostApiImpl::ext_default_child_storage_read_version_1(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan key,
Expand All @@ -533,4 +549,11 @@ namespace kagome::host_api {
child_storage_key);
}

runtime::WasmSpan
HostApiImpl::ext_default_child_storage_storage_kill_version_3(
runtime::WasmSpan child_storage_key, runtime::WasmSpan limit) {
return child_storage_ext_.ext_default_child_storage_storage_kill_version_3(
child_storage_key, limit);
}

} // namespace kagome::host_api
12 changes: 12 additions & 0 deletions core/host_api/impl/host_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,18 @@ namespace kagome::host_api {
runtime::WasmSpan ext_default_child_storage_root_version_1(
runtime::WasmSpan child_storage_key) const override;

runtime::WasmSpan ext_default_child_storage_root_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmI32 state_version) const override;

void ext_default_child_storage_clear_prefix_version_1(
runtime::WasmSpan child_storage_key, runtime::WasmSpan prefix) override;

runtime::WasmSpan ext_default_child_storage_clear_prefix_version_2(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan prefix,
runtime::WasmSpan limit) override;

virtual runtime::WasmSpan ext_default_child_storage_read_version_1(
runtime::WasmSpan child_storage_key,
runtime::WasmSpan key,
Expand All @@ -333,6 +342,9 @@ namespace kagome::host_api {
virtual void ext_default_child_storage_storage_kill_version_1(
runtime::WasmSpan child_storage_key) override;

virtual runtime::WasmSpan ext_default_child_storage_storage_kill_version_3(
runtime::WasmSpan child_storage_key, runtime::WasmSpan limit) override;

private:
static constexpr uint64_t DEFAULT_CHAIN_ID = 42;

Expand Down
Loading