Skip to content
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

Add get_storage_keys_paged #403

Merged
merged 5 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}