-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add new host APIs set_storage_or_clear and get_storage_or_zero #7857
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
65b8494
Add new host APIs set_storage_or_clear and get_storage_or_zero
9db8b64
Update from github-actions[bot] running command 'prdoc --audience run…
github-actions[bot] 6cff153
Update prdoc/pr_7857.prdoc
a94b208
make key len fixed for the APIs, additional fixes
c760318
Remove extraneous comments and fix format
1b4ef76
added the license
ce62ea0
fixed format
68e59b3
Fixed additional comments
8001ad5
Remove extraneous comments
36059cf
Merge branch 'master' into castilla-new-uapi
bc21d01
Remove extern crate
80d0964
Fix data clone, and additional comments
92874f0
Ensure new changes do not change previous behavior
7c9dbd7
Improve documentation
0405aa5
Add enum taglines
dfe8fb8
Merge branch 'master' into castilla-new-uapi
aa384d2
Merge branch 'master' into castilla-new-uapi
e6bc054
Covered an edge case in the test.
a3a2723
Merge branch 'master' into castilla-new-uapi
df4a1be
Update substrate/frame/revive/src/wasm/runtime.rs
e1c0125
Update substrate/frame/revive/src/wasm/runtime.rs
862caa3
Address additional comments
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| title: Add new host APIs set_storage_or_clear and get_storage_or_zero | ||
| doc: | ||
| - audience: Runtime Dev | ||
| description: "# Description\n\n*This PR introduces two new storage API functions\u2014\ | ||
| set_storage_or_clear and get_storage_or_zero\u2014which provide fixed\u2011size\ | ||
| \ (32\u2011byte) storage operations. These APIs are an attempt to match Ethereum\u2019\ | ||
| s SSTORE semantics. These APIs provide additional functionality for setting and\ | ||
| \ retrieving storage values and clearing storage when a zero value is provided\ | ||
| \ and returning zero bytes when a key does not exist.*\n\nFixes #6944\n## Review\ | ||
| \ Notes\n\n* Changes in `runtime.rs`\nAdded the set_storage_or_clear function\ | ||
| \ to set storage at a fixed 256-bit key with a fixed 256-bit value. If the provided\ | ||
| \ value is all zeros, the key is cleared.\nAdded the get_storage_or_zero function\ | ||
| \ to read storage at a fixed 256-bit key and write back a fixed 256-bit value.\ | ||
| \ If the key does not exist, 32 bytes of zero are written back.\n* Changes in\ | ||
| \ `storage.rs`\nAdded test cases to cover the new set_storage_or_clear and get_storage_or_zero\ | ||
| \ APIs.\n.\n\n```\n// Example usage of the new set_storage_or_clear function\n\ | ||
| let existing = api::set_storage_or_clear(StorageFlags::empty(), &KEY, &VALUE_A);\n\ | ||
| assert_eq!(existing, None);\n\n// Example usage of the new get_storage_or_zero\ | ||
| \ function\nlet mut stored: [u8; 32] = [0u8; 32];\nlet _ = api::get_storage_or_zero(StorageFlags::empty(),\ | ||
| \ &KEY, &mut stored);\nassert_eq!(stored, VALUE_A);\n```\n\n*All existing tests\ | ||
| \ pass*\n\n# Checklist\n\n* [x] My PR includes a detailed description as outlined\ | ||
| \ in the \"Description\" and its two subsections above.\n* [ ] My PR follows the\ | ||
| \ [labeling requirements](\nhttps://github.com/paritytech/polkadot-sdk/blob/master/docs/contributor/CONTRIBUTING.md#Process\n\ | ||
| ) of this project (at minimum one label for `T` required)\n * External contributors:\ | ||
| \ ask maintainers to put the right label on your PR.\n* [x] I have made corresponding\ | ||
| \ changes to the documentation (if applicable)\n* [x] I have added tests that\ | ||
| \ prove my fix is effective or that my feature works (if applicable)" | ||
| crates: | ||
| - name: pallet-revive-fixtures | ||
| bump: patch | ||
| - name: pallet-revive | ||
| bump: patch | ||
| - name: pallet-revive-uapi | ||
| bump: minor |
83 changes: 83 additions & 0 deletions
83
substrate/frame/revive/fixtures/contracts/clear_storage_on_zero_value.rs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! This contract tests the storage APIs. It sets and clears storage values using the different | ||
| //! versions of the storage APIs. | ||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| include!("../panic_handler.rs"); | ||
| use uapi::{HostFn, HostFnImpl as api, StorageFlags}; | ||
|
|
||
| #[no_mangle] | ||
| #[polkavm_derive::polkavm_export] | ||
| pub extern "C" fn deploy() {} | ||
|
|
||
| fn test_storage_operations(flags: StorageFlags) { | ||
| const KEY: [u8; 32] = [1u8; 32]; | ||
| const VALUE_A: [u8; 32] = [4u8; 32]; | ||
| const ZERO: [u8; 32] = [0u8; 32]; | ||
| let mut small_value_padded = [0u8; 32]; | ||
| small_value_padded[0] = 5; | ||
| small_value_padded[1] = 6; | ||
| small_value_padded[2] = 7; | ||
|
|
||
| api::clear_storage(flags, &KEY); | ||
|
|
||
| assert_eq!(api::contains_storage(flags, &KEY), None); | ||
|
|
||
| let existing = api::set_storage_or_clear(flags, &KEY, &VALUE_A); | ||
| assert_eq!(existing, None); | ||
|
|
||
| let mut stored: [u8; 32] = [0u8; 32]; | ||
| api::get_storage_or_zero(flags, &KEY, &mut stored); | ||
| assert_eq!(stored, VALUE_A); | ||
|
|
||
| let existing = api::set_storage_or_clear(flags, &KEY, &ZERO); | ||
| assert_eq!(existing, Some(32)); | ||
|
|
||
| let mut cleared: [u8; 32] = [1u8; 32]; | ||
| api::get_storage_or_zero(flags, &KEY, &mut cleared); | ||
| assert_eq!(cleared, ZERO); | ||
|
|
||
| assert_eq!(api::contains_storage(flags, &KEY), None); | ||
|
|
||
| // Test retrieving a value smaller than 32 bytes | ||
| api::set_storage_or_clear(flags, &KEY, &small_value_padded); | ||
| let mut retrieved = [255u8; 32]; | ||
| api::get_storage_or_zero(flags, &KEY, &mut retrieved); | ||
|
|
||
| assert_eq!(retrieved[0], 5); | ||
| assert_eq!(retrieved[1], 6); | ||
| assert_eq!(retrieved[2], 7); | ||
| for i in 3..32 { | ||
| assert_eq!(retrieved[i], 0, "Byte at position {} should be zero", i); | ||
| } | ||
|
|
||
| // Clean up | ||
| api::clear_storage(flags, &KEY); | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| #[polkavm_derive::polkavm_export] | ||
| pub extern "C" fn call() { | ||
| // Test with regular storage | ||
| test_storage_operations(StorageFlags::empty()); | ||
|
|
||
| // Test with transient storage | ||
| test_storage_operations(StorageFlags::TRANSIENT); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.