Skip to content

Commit

Permalink
Add get_storage_keys_paged (#403)
Browse files Browse the repository at this point in the history
* add storage keys paged

* add storage fetch
  • Loading branch information
haerdib authored Jan 3, 2023
1 parent c6cc7a9 commit 199dbc0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ async fn main() {
let signer = AccountKeyring::Alice.pair();
api.set_signer(signer);
println!("[+] Alice's Account Nonce is {}", api.get_nonce().unwrap());

// Get an vector of storage keys, numbering up to the given max keys and that start with the (optionally) given storage key prefix.
let storage_key_prefix = api.get_storage_map_key_prefix("System", "Account").unwrap();
let max_keys = 3;
let storage_keys = api
.get_storage_keys_paged(Some(storage_key_prefix), max_keys, None, None)
.unwrap();
assert_eq!(storage_keys.len() as u32, max_keys);
// Get the storage values that belong to the retrieved storage keys.
for storage_key in storage_keys.iter() {
println!("Retrieving value for key {:?}", storage_key);
// We're expecting account info as return value because we fetch added a prefix of "System" + "Account".
let storage_data: AccountInfo =
api.get_storage_by_key_hash(storage_key.clone(), None).unwrap().unwrap();
println!("Retrieved data {:?}", storage_data);
}
}
24 changes: 24 additions & 0 deletions src/api/rpc_api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ pub trait GetStorage<Hash> {
at_block: Option<Hash>,
) -> Result<Option<V>>;

/// Returns the keys with prefix with pagination support.
/// Up to `count` keys will be returned.
/// If `start_key` is passed, return next keys in storage in lexicographic order.
fn get_storage_keys_paged(
&self,
prefix: Option<StorageKey>,
count: u32,
start_key: Option<StorageKey>,
at_block: Option<Hash>,
) -> Result<Vec<StorageKey>>;

fn get_opaque_storage_by_key_hash(
&self,
key: StorageKey,
Expand Down Expand Up @@ -175,6 +186,19 @@ where
}
}

fn get_storage_keys_paged(
&self,
prefix: Option<StorageKey>,
count: u32,
start_key: Option<StorageKey>,
at_block: Option<Runtime::Hash>,
) -> Result<Vec<StorageKey>> {
let storage = self
.client()
.request("state_getKeysPaged", rpc_params![prefix, count, start_key, at_block])?;
Ok(storage)
}

fn get_opaque_storage_by_key_hash(
&self,
key: StorageKey,
Expand Down
10 changes: 10 additions & 0 deletions testing/examples/state_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ async fn main() {
api.get_storage_proof_by_keys(vec![storage_key.clone()], None).unwrap().unwrap();
let _keys = api.get_keys(storage_key, None).unwrap().unwrap();
let _constants: Balance = api.get_constant("Balances", "ExistentialDeposit").unwrap();

let max_keys = 3;
let storage_keys = api
.get_storage_keys_paged(Some(storage_key_prefix), max_keys, None, None)
.unwrap();
assert_eq!(storage_keys.len() as u32, max_keys);

let max_keys = 20;
let storage_keys = api.get_storage_keys_paged(None, max_keys, None, None).unwrap();
assert_eq!(storage_keys.len() as u32, max_keys);
}

0 comments on commit 199dbc0

Please sign in to comment.