You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add new host APIs set_storage_or_clear and get_storage_or_zero (#7857)
# Description
*This PR introduces two new storage API functions—set_storage_or_clear
and get_storage_or_zero—which provide fixed‑size (32‑byte) storage
operations. These APIs are an attempt to match Ethereum’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.*
Fixes#6944
## Review Notes
* Changes in `runtime.rs`
Added 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.
Added 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.
* Changes in `storage.rs`
Added test cases to cover the new set_storage_or_clear and
get_storage_or_zero APIs.
.
```
// Example usage of the new set_storage_or_clear function
let existing = api::set_storage_or_clear(StorageFlags::empty(), &KEY, &VALUE_A);
assert_eq!(existing, None);
// Example usage of the new get_storage_or_zero function
let mut stored: [u8; 32] = [0u8; 32];
let _ = api::get_storage_or_zero(StorageFlags::empty(), &KEY, &mut stored);
assert_eq!(stored, VALUE_A);
```
*All existing tests pass*
# Checklist
* [x] My PR includes a detailed description as outlined in the
"Description" and its two subsections above.
* [x] My PR follows the [labeling requirements](
https://github.com/paritytech/polkadot-sdk/blob/master/docs/contributor/CONTRIBUTING.md#Process
) of this project (at minimum one label for `T` required)
* External contributors: ask maintainers to put the right label on your
PR.
* [x] I have made corresponding changes to the documentation (if
applicable)
* [x] I have added tests that prove my fix is effective or that my
feature works (if applicable)
---------
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Alexander Theißen <[email protected]>
Co-authored-by: xermicus <[email protected]>
0 commit comments