diff --git a/Cargo.toml b/Cargo.toml index 83f64ca449..8b2620ab7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ hex = "0.4.0" sp-rpc = { version = "2.0.0-alpha.7", package = "sp-rpc" } sp-core = { version = "2.0.0-alpha.7", package = "sp-core" } sp-transaction-pool = { version = "2.0.0-alpha.7", package = "sp-transaction-pool" } +sc-rpc-api = { version = "0.8.0-alpha.7", package = "sc-rpc-api" } [dev-dependencies] async-std = { version = "1.5.0", features = ["attributes"] } diff --git a/src/lib.rs b/src/lib.rs index 67b521f5a4..2a62cb8225 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ use codec::{ }; use futures::future; use jsonrpsee::client::Subscription; +use sc_rpc_api::state::ReadProof; use sp_core::{ storage::{ StorageChangeSet, @@ -263,6 +264,19 @@ impl Client { Ok(block) } + /// Get proof of storage entries at a specific block's state. + pub async fn read_proof( + &self, + keys: Vec, + hash: Option, + ) -> Result, Error> + where + H: Into + 'static, + { + let proof = self.rpc.read_proof(keys, hash.map(|h| h.into())).await?; + Ok(proof) + } + /// Create and submit an extrinsic and return corresponding Hash if successful pub async fn submit_extrinsic( &self, @@ -519,6 +533,10 @@ impl codec::Encode for Encoded { #[cfg(test)] mod tests { + use sp_core::storage::{ + well_known_keys, + StorageKey, + }; use sp_keyring::{ AccountKeyring, Ed25519Keyring, @@ -575,6 +593,23 @@ mod tests { client.block(block_hash).await.unwrap(); } + #[async_std::test] + #[ignore] // requires locally running substrate node + async fn test_getting_read_proof() { + let client = test_client().await; + let block_hash = client.block_hash(None).await.unwrap(); + client + .read_proof( + vec![ + StorageKey(well_known_keys::HEAP_PAGES.to_vec()), + StorageKey(well_known_keys::EXTRINSIC_INDEX.to_vec()), + ], + block_hash, + ) + .await + .unwrap(); + } + #[async_std::test] #[ignore] // requires locally running substrate node async fn test_state_total_issuance() { diff --git a/src/rpc.rs b/src/rpc.rs index a3c0661072..76f7a8aec2 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -38,6 +38,7 @@ use jsonrpsee::{ use num_traits::bounds::Bounded; use frame_metadata::RuntimeMetadataPrefixed; +use sc_rpc_api::state::ReadProof; use serde::Serialize; use sp_core::{ storage::{ @@ -230,6 +231,17 @@ impl Rpc { Ok(block) } + /// Get proof of storage entries at a specific block's state. + pub async fn read_proof( + &self, + keys: Vec, + hash: Option, + ) -> Result, Error> { + let params = Params::Array(vec![to_json_value(keys)?, to_json_value(hash)?]); + let proof = self.client.request("state_getReadProof", params).await?; + Ok(proof) + } + /// Fetch the runtime version pub async fn runtime_version( &self,