Skip to content

add storage_double_map_key_prefix #568

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 4 commits into from
May 23, 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
17 changes: 17 additions & 0 deletions examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//! Very simple example that shows how to get some simple storage values.

use frame_system::AccountInfo as GenericAccountInfo;
use kitchensink_runtime::{AccountId, Runtime, Signature};
use pallet_staking::Exposure;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_primitives::{Config, ExtrinsicSigner, SubstrateKitchensinkConfig},
Expand Down Expand Up @@ -83,4 +85,19 @@ async fn main() {
api.get_storage_by_key(storage_key.clone(), None).unwrap().unwrap();
println!("Retrieved data {:?}", storage_data);
}

let storage_double_map_key_prefix =
api.get_storage_double_map_key_prefix("Staking", "ErasStakers", 0).unwrap();
let double_map_storage_keys = api
.get_storage_keys_paged(Some(storage_double_map_key_prefix), max_keys, None, None)
.unwrap();

// Get the storage values that belong to the retrieved storage keys.
for storage_key in double_map_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: Exposure<AccountId, u128> =
api.get_storage_by_key(storage_key.clone(), None).unwrap().unwrap();
println!("Retrieved data {:?}", storage_data);
}
}
11 changes: 11 additions & 0 deletions node-api/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ impl Metadata {
self.pallet(pallet)?.storage(storage_item)?.get_map_prefix(pallet)
}

pub fn storage_double_map_key_prefix<K: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
first: K,
) -> Result<StorageKey, MetadataError> {
self.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_double_map_prefix::<K>(storage_prefix, first)
}

pub fn storage_double_map_key<K: Encode, Q: Encode>(
&self,
pallet: &'static str,
Expand Down
27 changes: 27 additions & 0 deletions node-api/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ pub trait GetStorageTypes {
fn get_map<K: Encode>(&self, pallet_prefix: &str) -> Result<StorageMap<K>, MetadataError>;
fn get_map_prefix(&self, pallet_prefix: &str) -> Result<StorageKey, MetadataError>;
fn get_value(&self, pallet_prefix: &str) -> Result<StorageValue, MetadataError>;
fn get_double_map_prefix<K: Encode>(
&self,
pallet_prefix: &str,
key1: K,
) -> Result<StorageKey, MetadataError>;
}

impl GetStorageTypes for StorageEntryMetadata<PortableForm> {
Expand Down Expand Up @@ -145,6 +150,28 @@ impl GetStorageTypes for StorageEntryMetadata<PortableForm> {
}
}

fn get_double_map_prefix<K: Encode>(
&self,
pallet_prefix: &str,
key1: K,
) -> Result<StorageKey, MetadataError> {
match &self.ty {
StorageEntryType::Map { hashers, .. } => {
let module_prefix = pallet_prefix.as_bytes();
let storage_prefix = self.name.as_bytes();

let hasher1 = hashers.get(0).ok_or(MetadataError::StorageTypeError)?;

let mut bytes = sp_core::twox_128(module_prefix).to_vec();
bytes.extend(&sp_core::twox_128(storage_prefix)[..]);
bytes.extend(key_hash(&key1, hasher1));

Ok(StorageKey(bytes))
},
_ => Err(MetadataError::StorageTypeError),
}
}

fn get_value(&self, pallet_prefix: &str) -> Result<StorageValue, MetadataError> {
match &self.ty {
StorageEntryType::Plain { .. } => {
Expand Down
18 changes: 18 additions & 0 deletions src/api/rpc_api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ pub trait GetStorage {
storage_item: &'static str,
) -> Result<StorageKey>;

async fn get_storage_double_map_key_prefix<K: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
first: K,
) -> Result<StorageKey>;

/// Retrieve the storage value from a double map for the given keys: `first_double_map_key` and `second_double_map_key`.
///
/// `at_block`: the state is queried at this block, set to `None` to get the state from the latest known block.
Expand Down Expand Up @@ -215,6 +222,17 @@ where
self.get_storage_by_key(storagekey, at_block).await
}

async fn get_storage_double_map_key_prefix<K: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
first: K,
) -> Result<StorageKey> {
self.metadata()
.storage_double_map_key_prefix(storage_prefix, storage_key_name, first)
.map_err(|e| e.into())
}

async fn get_storage_by_key<V: Decode>(
&self,
storage_key: StorageKey,
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/jsonrpsee_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl JsonrpseeClient {
Self::new("ws://127.0.0.1:9944")
}

async fn async_new(url: &str) -> Result<Self> {
pub async fn async_new(url: &str) -> Result<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, thanks !

let uri: Uri = url.parse().map_err(|e| Error::Client(Box::new(e)))?;
let (tx, rx) = WsTransportClientBuilder::default()
.build(uri)
Expand Down