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 support for RPC state_getReadProof #106

Merged
merged 3 commits into from
May 7, 2020
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use codec::{
};
use futures::future;
use jsonrpsee::client::Subscription;
use sc_rpc_api::state::ReadProof;
use sp_core::{
storage::{
StorageChangeSet,
Expand Down Expand Up @@ -263,6 +264,19 @@ impl<T: System, S, E> Client<T, S, E> {
Ok(block)
}

/// Get proof of storage entries at a specific block's state.
pub async fn read_proof<H>(
&self,
keys: Vec<StorageKey>,
hash: Option<H>,
) -> Result<ReadProof<T::Hash>, Error>
where
H: Into<T::Hash> + '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<X: Encode>(
&self,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -230,6 +231,17 @@ impl<T: System> Rpc<T> {
Ok(block)
}

/// Get proof of storage entries at a specific block's state.
pub async fn read_proof(
&self,
keys: Vec<StorageKey>,
hash: Option<T::Hash>,
) -> Result<ReadProof<T::Hash>, 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,
Expand Down