Skip to content
Snippets Groups Projects
Unverified Commit 66bd26d3 authored by Alisher A. Khassanov's avatar Alisher A. Khassanov Committed by GitHub
Browse files

Add `offchain_localStorageClear` RPC method (#7266)


# Description

Closes https://github.com/paritytech/polkadot-sdk/issues/7265.

## Integration

Requires changes in
`https://github.com/polkadot-js/api/packages/{rpc-augment,types-support,types}`
to be visible in Polkadot\Substrate Portal and in other libraries where
we should explicitly state RPC methods.

Accompany PR to `polkadot-js/api`:
https://github.com/polkadot-js/api/pull/6070.

## Review Notes

Please put the right label on my PR.

---------

Co-authored-by: command-bot <>
Co-authored-by: default avatarBastian Köcher <git@kchr.de>
parent 04847d51
No related merge requests found
Pipeline #512904 waiting for manual action with stages
in 30 minutes and 50 seconds
title: Add `offchain_localStorageClear` RPC method
doc:
- audience: Node Operator
description: |-
Adds RPC method `offchain_localStorageClear` to clear the offchain local storage.
crates:
- name: sc-offchain
bump: minor
- name: sc-rpc-api
bump: minor
validate: false
- name: sc-rpc
bump: minor
......@@ -375,7 +375,7 @@ mod tests {
}
#[test]
fn should_set_and_get_local_storage() {
fn should_set_get_and_clear_local_storage() {
// given
let kind = StorageKind::PERSISTENT;
let mut api = offchain_db();
......@@ -387,6 +387,12 @@ mod tests {
// then
assert_eq!(api.local_storage_get(kind, key), Some(b"value".to_vec()));
// when
api.local_storage_clear(kind, key);
// then
assert_eq!(api.local_storage_get(kind, key), None);
}
#[test]
......
......@@ -31,6 +31,10 @@ pub trait OffchainApi {
#[method(name = "offchain_localStorageSet", with_extensions)]
fn set_local_storage(&self, kind: StorageKind, key: Bytes, value: Bytes) -> Result<(), Error>;
/// Clear offchain local storage under given key and prefix.
#[method(name = "offchain_localStorageClear", with_extensions)]
fn clear_local_storage(&self, kind: StorageKind, key: Bytes) -> Result<(), Error>;
/// Get offchain local storage under given key and prefix.
#[method(name = "offchain_localStorageGet", with_extensions)]
fn get_local_storage(&self, kind: StorageKind, key: Bytes) -> Result<Option<Bytes>, Error>;
......
......@@ -66,6 +66,23 @@ impl<T: OffchainStorage + 'static> OffchainApiServer for Offchain<T> {
Ok(())
}
fn clear_local_storage(
&self,
ext: &Extensions,
kind: StorageKind,
key: Bytes,
) -> Result<(), Error> {
check_if_safe(ext)?;
let prefix = match kind {
StorageKind::PERSISTENT => sp_offchain::STORAGE_PREFIX,
StorageKind::LOCAL => return Err(Error::UnavailableStorageKind),
};
self.storage.write().remove(prefix, &key);
Ok(())
}
fn get_local_storage(
&self,
ext: &Extensions,
......
......@@ -35,9 +35,14 @@ fn local_storage_should_work() {
Ok(())
);
assert_matches!(
offchain.get_local_storage(&ext, StorageKind::PERSISTENT, key),
offchain.get_local_storage(&ext, StorageKind::PERSISTENT, key.clone()),
Ok(Some(ref v)) if *v == value
);
assert_matches!(
offchain.clear_local_storage(&ext, StorageKind::PERSISTENT, key.clone()),
Ok(())
);
assert_matches!(offchain.get_local_storage(&ext, StorageKind::PERSISTENT, key), Ok(None));
}
#[test]
......@@ -55,6 +60,12 @@ fn offchain_calls_considered_unsafe() {
assert_eq!(e.to_string(), "RPC call is unsafe to be called externally")
}
);
assert_matches!(
offchain.clear_local_storage(&ext, StorageKind::PERSISTENT, key.clone()),
Err(Error::UnsafeRpcCalled(e)) => {
assert_eq!(e.to_string(), "RPC call is unsafe to be called externally")
}
);
assert_matches!(
offchain.get_local_storage(&ext, StorageKind::PERSISTENT, key),
Err(Error::UnsafeRpcCalled(e)) => {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment