-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55766e7
Implement ext_default_child_storage_root_version_2
0d48fd6
Implement ext_default_child_storage_storage_kill_version_3
7f49443
Implement ext_default_child_storage_clear_prefix_version_2
3750e8d
Fix mocks
e7e3a80
Fix return types
b34a24c
Throw on error
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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>>( | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?