From 96c0ba375d5489fbf2889300215ffc766377971e Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 18 May 2022 16:58:11 +0300 Subject: [PATCH 01/10] client/api: Make `storage_keys` blocking Signed-off-by: Alexandru Vasile --- client/rpc-api/src/state/mod.rs | 8 ++------ client/rpc/src/state/mod.rs | 6 +++--- client/rpc/src/state/state_full.rs | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/client/rpc-api/src/state/mod.rs b/client/rpc-api/src/state/mod.rs index fba023e830262..ce0ff1a383c41 100644 --- a/client/rpc-api/src/state/mod.rs +++ b/client/rpc-api/src/state/mod.rs @@ -38,13 +38,9 @@ pub trait StateApi { async fn call(&self, name: String, bytes: Bytes, hash: Option) -> RpcResult; /// Returns the keys with prefix, leave empty to get all the keys. - #[method(name = "state_getKeys")] + #[method(name = "state_getKeys", blocking)] #[deprecated(since = "2.0.0", note = "Please use `getKeysPaged` with proper paging support")] - async fn storage_keys( - &self, - prefix: StorageKey, - hash: Option, - ) -> RpcResult>; + fn storage_keys(&self, prefix: StorageKey, hash: Option) -> RpcResult>; /// Returns the keys with prefix, leave empty to get all the keys #[method(name = "state_getPairs")] diff --git a/client/rpc/src/state/mod.rs b/client/rpc/src/state/mod.rs index a45651c5e7990..60998c0e5eee5 100644 --- a/client/rpc/src/state/mod.rs +++ b/client/rpc/src/state/mod.rs @@ -68,7 +68,7 @@ where ) -> Result; /// Returns the keys with prefix, leave empty to get all the keys. - async fn storage_keys( + fn storage_keys( &self, block: Option, prefix: StorageKey, @@ -218,12 +218,12 @@ where self.backend.call(block, method, data).await.map_err(Into::into) } - async fn storage_keys( + fn storage_keys( &self, key_prefix: StorageKey, block: Option, ) -> RpcResult> { - self.backend.storage_keys(block, key_prefix).await.map_err(Into::into) + self.backend.storage_keys(block, key_prefix).map_err(Into::into) } async fn storage_pairs( diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index 48165e912b03a..48966cea0b210 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -212,7 +212,7 @@ where .map_err(client_err) } - async fn storage_keys( + fn storage_keys( &self, block: Option, prefix: StorageKey, From 9790d36d41cbe40e456b2ee32b4cdeea8891b39a Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 18 May 2022 17:24:05 +0300 Subject: [PATCH 02/10] client/api: Ensure `state_*` RPC methods are blocking Signed-off-by: Alexandru Vasile --- client/rpc-api/src/state/mod.rs | 50 +++++++++---------- client/rpc/src/state/mod.rs | 79 +++++++++++++----------------- client/rpc/src/state/state_full.rs | 26 +++++----- 3 files changed, 70 insertions(+), 85 deletions(-) diff --git a/client/rpc-api/src/state/mod.rs b/client/rpc-api/src/state/mod.rs index ce0ff1a383c41..ce325a3089f14 100644 --- a/client/rpc-api/src/state/mod.rs +++ b/client/rpc-api/src/state/mod.rs @@ -34,8 +34,8 @@ pub use self::helpers::ReadProof; #[rpc(client, server)] pub trait StateApi { /// Call a contract at a block's state. - #[method(name = "state_call", aliases = ["state_callAt"])] - async fn call(&self, name: String, bytes: Bytes, hash: Option) -> RpcResult; + #[method(name = "state_call", aliases = ["state_callAt"], blocking)] + fn call(&self, name: String, bytes: Bytes, hash: Option) -> RpcResult; /// Returns the keys with prefix, leave empty to get all the keys. #[method(name = "state_getKeys", blocking)] @@ -43,8 +43,8 @@ pub trait StateApi { fn storage_keys(&self, prefix: StorageKey, hash: Option) -> RpcResult>; /// Returns the keys with prefix, leave empty to get all the keys - #[method(name = "state_getPairs")] - async fn storage_pairs( + #[method(name = "state_getPairs", blocking)] + fn storage_pairs( &self, prefix: StorageKey, hash: Option, @@ -54,7 +54,7 @@ pub trait StateApi { /// Up to `count` keys will be returned. /// If `start_key` is passed, return next keys in storage in lexicographic order. #[method(name = "state_getKeysPaged", aliases = ["state_getKeysPagedAt"])] - async fn storage_keys_paged( + fn storage_keys_paged( &self, prefix: Option, count: u32, @@ -63,32 +63,32 @@ pub trait StateApi { ) -> RpcResult>; /// Returns a storage entry at a specific block's state. - #[method(name = "state_getStorage", aliases = ["state_getStorageAt"])] - async fn storage(&self, key: StorageKey, hash: Option) -> RpcResult>; + #[method(name = "state_getStorage", aliases = ["state_getStorageAt"], blocking)] + fn storage(&self, key: StorageKey, hash: Option) -> RpcResult>; /// Returns the hash of a storage entry at a block's state. - #[method(name = "state_getStorageHash", aliases = ["state_getStorageHashAt"])] - async fn storage_hash(&self, key: StorageKey, hash: Option) -> RpcResult>; + #[method(name = "state_getStorageHash", aliases = ["state_getStorageHashAt"], blocking)] + fn storage_hash(&self, key: StorageKey, hash: Option) -> RpcResult>; /// Returns the size of a storage entry at a block's state. - #[method(name = "state_getStorageSize", aliases = ["state_getStorageSizeAt"])] - async fn storage_size(&self, key: StorageKey, hash: Option) -> RpcResult>; + #[method(name = "state_getStorageSize", aliases = ["state_getStorageSizeAt"], blocking)] + fn storage_size(&self, key: StorageKey, hash: Option) -> RpcResult>; /// Returns the runtime metadata as an opaque blob. - #[method(name = "state_getMetadata")] - async fn metadata(&self, hash: Option) -> RpcResult; + #[method(name = "state_getMetadata", blocking)] + fn metadata(&self, hash: Option) -> RpcResult; /// Get the runtime version. - #[method(name = "state_getRuntimeVersion", aliases = ["chain_getRuntimeVersion"])] - async fn runtime_version(&self, hash: Option) -> RpcResult; + #[method(name = "state_getRuntimeVersion", aliases = ["chain_getRuntimeVersion"], blocking)] + fn runtime_version(&self, hash: Option) -> RpcResult; /// Query historical storage entries (by key) starting from a block given as the second /// parameter. /// /// NOTE This first returned result contains the initial state of storage for all keys. /// Subsequent values in the vector represent changes to the previous state (diffs). - #[method(name = "state_queryStorage")] - async fn query_storage( + #[method(name = "state_queryStorage", blocking)] + fn query_storage( &self, keys: Vec, block: Hash, @@ -96,20 +96,16 @@ pub trait StateApi { ) -> RpcResult>>; /// Query storage entries (by key) starting at block hash given as the second parameter. - #[method(name = "state_queryStorageAt")] - async fn query_storage_at( + #[method(name = "state_queryStorageAt", blocking)] + fn query_storage_at( &self, keys: Vec, at: Option, ) -> RpcResult>>; /// Returns proof of storage entries at a specific block's state. - #[method(name = "state_getReadProof")] - async fn read_proof( - &self, - keys: Vec, - hash: Option, - ) -> RpcResult>; + #[method(name = "state_getReadProof", blocking)] + fn read_proof(&self, keys: Vec, hash: Option) -> RpcResult>; /// New runtime version subscription #[subscription( @@ -281,8 +277,8 @@ pub trait StateApi { /// /// If you are having issues with maximum payload size you can use the flag /// `-ltracing=trace` to get some logging during tracing. - #[method(name = "state_traceBlock")] - async fn trace_block( + #[method(name = "state_traceBlock", blocking)] + fn trace_block( &self, block: Hash, targets: Option, diff --git a/client/rpc/src/state/mod.rs b/client/rpc/src/state/mod.rs index 60998c0e5eee5..a175df30d452e 100644 --- a/client/rpc/src/state/mod.rs +++ b/client/rpc/src/state/mod.rs @@ -60,7 +60,7 @@ where Client: Send + Sync + 'static, { /// Call runtime method at given block. - async fn call( + fn call( &self, block: Option, method: String, @@ -75,14 +75,14 @@ where ) -> Result, Error>; /// Returns the keys with prefix along with their values, leave empty to get all the pairs. - async fn storage_pairs( + fn storage_pairs( &self, block: Option, prefix: StorageKey, ) -> Result, Error>; /// Returns the keys with prefix with pagination support. - async fn storage_keys_paged( + fn storage_keys_paged( &self, block: Option, prefix: Option, @@ -91,14 +91,14 @@ where ) -> Result, Error>; /// Returns a storage entry at a specific block's state. - async fn storage( + fn storage( &self, block: Option, key: StorageKey, ) -> Result, Error>; /// Returns the hash of a storage entry at a block's state. - async fn storage_hash( + fn storage_hash( &self, block: Option, key: StorageKey, @@ -108,24 +108,24 @@ where /// /// If data is available at `key`, it is returned. Else, the sum of values who's key has `key` /// prefix is returned, i.e. all the storage (double) maps that have this prefix. - async fn storage_size( + fn storage_size( &self, block: Option, key: StorageKey, ) -> Result, Error>; /// Returns the runtime metadata as an opaque blob. - async fn metadata(&self, block: Option) -> Result; + fn metadata(&self, block: Option) -> Result; /// Get the runtime version. - async fn runtime_version(&self, block: Option) -> Result; + fn runtime_version(&self, block: Option) -> Result; /// Query historical storage entries (by key) starting from a block given as the second /// parameter. /// /// NOTE This first returned result contains the initial state of storage for all keys. /// Subsequent values in the vector represent changes to the previous state (diffs). - async fn query_storage( + fn query_storage( &self, from: Block::Hash, to: Option, @@ -133,21 +133,21 @@ where ) -> Result>, Error>; /// Query storage entries (by key) starting at block hash given as the second parameter. - async fn query_storage_at( + fn query_storage_at( &self, keys: Vec, at: Option, ) -> Result>, Error>; /// Returns proof of storage entries at a specific block's state. - async fn read_proof( + fn read_proof( &self, block: Option, keys: Vec, ) -> Result, Error>; /// Trace storage changes for block - async fn trace_block( + fn trace_block( &self, block: Block::Hash, targets: Option, @@ -209,13 +209,8 @@ where Block: BlockT + 'static, Client: Send + Sync + 'static, { - async fn call( - &self, - method: String, - data: Bytes, - block: Option, - ) -> RpcResult { - self.backend.call(block, method, data).await.map_err(Into::into) + fn call(&self, method: String, data: Bytes, block: Option) -> RpcResult { + self.backend.call(block, method, data).map_err(Into::into) } fn storage_keys( @@ -226,16 +221,16 @@ where self.backend.storage_keys(block, key_prefix).map_err(Into::into) } - async fn storage_pairs( + fn storage_pairs( &self, key_prefix: StorageKey, block: Option, ) -> RpcResult> { self.deny_unsafe.check_if_safe()?; - self.backend.storage_pairs(block, key_prefix).await.map_err(Into::into) + self.backend.storage_pairs(block, key_prefix).map_err(Into::into) } - async fn storage_keys_paged( + fn storage_keys_paged( &self, prefix: Option, count: u32, @@ -250,66 +245,61 @@ where } self.backend .storage_keys_paged(block, prefix, count, start_key) - .await .map_err(Into::into) } - async fn storage( + fn storage( &self, key: StorageKey, block: Option, ) -> RpcResult> { - self.backend.storage(block, key).await.map_err(Into::into) + self.backend.storage(block, key).map_err(Into::into) } - async fn storage_hash( + fn storage_hash( &self, key: StorageKey, block: Option, ) -> RpcResult> { - self.backend.storage_hash(block, key).await.map_err(Into::into) + self.backend.storage_hash(block, key).map_err(Into::into) } - async fn storage_size( - &self, - key: StorageKey, - block: Option, - ) -> RpcResult> { - self.backend.storage_size(block, key).await.map_err(Into::into) + fn storage_size(&self, key: StorageKey, block: Option) -> RpcResult> { + self.backend.storage_size(block, key).map_err(Into::into) } - async fn metadata(&self, block: Option) -> RpcResult { - self.backend.metadata(block).await.map_err(Into::into) + fn metadata(&self, block: Option) -> RpcResult { + self.backend.metadata(block).map_err(Into::into) } - async fn runtime_version(&self, at: Option) -> RpcResult { - self.backend.runtime_version(at).await.map_err(Into::into) + fn runtime_version(&self, at: Option) -> RpcResult { + self.backend.runtime_version(at).map_err(Into::into) } - async fn query_storage( + fn query_storage( &self, keys: Vec, from: Block::Hash, to: Option, ) -> RpcResult>> { self.deny_unsafe.check_if_safe()?; - self.backend.query_storage(from, to, keys).await.map_err(Into::into) + self.backend.query_storage(from, to, keys).map_err(Into::into) } - async fn query_storage_at( + fn query_storage_at( &self, keys: Vec, at: Option, ) -> RpcResult>> { - self.backend.query_storage_at(keys, at).await.map_err(Into::into) + self.backend.query_storage_at(keys, at).map_err(Into::into) } - async fn read_proof( + fn read_proof( &self, keys: Vec, block: Option, ) -> RpcResult> { - self.backend.read_proof(block, keys).await.map_err(Into::into) + self.backend.read_proof(block, keys).map_err(Into::into) } /// Re-execute the given block with the tracing targets given in `targets` @@ -317,7 +307,7 @@ where /// /// Note: requires the node to run with `--rpc-methods=Unsafe`. /// Note: requires runtimes compiled with wasm tracing support, `--features with-tracing`. - async fn trace_block( + fn trace_block( &self, block: Block::Hash, targets: Option, @@ -327,7 +317,6 @@ where self.deny_unsafe.check_if_safe()?; self.backend .trace_block(block, targets, storage_keys, methods) - .await .map_err(Into::into) } diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index 48966cea0b210..f33564a9cef95 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -190,7 +190,7 @@ where + 'static, Client::Api: Metadata, { - async fn call( + fn call( &self, block: Option, method: String, @@ -222,7 +222,7 @@ where .map_err(client_err) } - async fn storage_pairs( + fn storage_pairs( &self, block: Option, prefix: StorageKey, @@ -232,7 +232,7 @@ where .map_err(client_err) } - async fn storage_keys_paged( + fn storage_keys_paged( &self, block: Option, prefix: Option, @@ -251,7 +251,7 @@ where .map_err(client_err) } - async fn storage( + fn storage( &self, block: Option, key: StorageKey, @@ -261,7 +261,7 @@ where .map_err(client_err) } - async fn storage_size( + fn storage_size( &self, block: Option, key: StorageKey, @@ -290,7 +290,7 @@ where .map_err(client_err) } - async fn storage_hash( + fn storage_hash( &self, block: Option, key: StorageKey, @@ -300,7 +300,7 @@ where .map_err(client_err) } - async fn metadata(&self, block: Option) -> std::result::Result { + fn metadata(&self, block: Option) -> std::result::Result { self.block_or_best(block).map_err(client_err).and_then(|block| { self.client .runtime_api() @@ -310,7 +310,7 @@ where }) } - async fn runtime_version( + fn runtime_version( &self, block: Option, ) -> std::result::Result { @@ -321,7 +321,7 @@ where }) } - async fn query_storage( + fn query_storage( &self, from: Block::Hash, to: Option, @@ -337,16 +337,16 @@ where call_fn() } - async fn query_storage_at( + fn query_storage_at( &self, keys: Vec, at: Option, ) -> std::result::Result>, Error> { let at = at.unwrap_or_else(|| self.client.info().best_hash); - self.query_storage(at, Some(at), keys).await + self.query_storage(at, Some(at), keys) } - async fn read_proof( + fn read_proof( &self, block: Option, keys: Vec, @@ -458,7 +458,7 @@ where .spawn("substrate-rpc-subscription", Some("rpc"), fut.map(drop).boxed()); } - async fn trace_block( + fn trace_block( &self, block: Block::Hash, targets: Option, From 3ec86275cfccf45c1128b2677b9cf57fc974dbc4 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 18 May 2022 17:46:41 +0300 Subject: [PATCH 03/10] client/rpc: Ensure `childstate_*` RPC methods are blocking Signed-off-by: Alexandru Vasile --- client/rpc-api/src/child_state/mod.rs | 28 ++++++++--------- client/rpc/src/state/mod.rs | 45 ++++++++++++--------------- client/rpc/src/state/state_full.rs | 32 ++++++++++--------- 3 files changed, 51 insertions(+), 54 deletions(-) diff --git a/client/rpc-api/src/child_state/mod.rs b/client/rpc-api/src/child_state/mod.rs index a15b1a0e7ee05..be279a97463b0 100644 --- a/client/rpc-api/src/child_state/mod.rs +++ b/client/rpc-api/src/child_state/mod.rs @@ -28,9 +28,9 @@ use sp_core::storage::{PrefixedStorageKey, StorageData, StorageKey}; #[rpc(client, server)] pub trait ChildStateApi { /// Returns the keys with prefix from a child storage, leave empty to get all the keys - #[method(name = "childstate_getKeys")] + #[method(name = "childstate_getKeys", blocking)] #[deprecated(since = "2.0.0", note = "Please use `getKeysPaged` with proper paging support")] - async fn storage_keys( + fn storage_keys( &self, child_storage_key: PrefixedStorageKey, prefix: StorageKey, @@ -40,8 +40,8 @@ pub trait ChildStateApi { /// Returns the keys with prefix from a child storage with pagination support. /// Up to `count` keys will be returned. /// If `start_key` is passed, return next keys in storage in lexicographic order. - #[method(name = "childstate_getKeysPaged", aliases = ["childstate_getKeysPagedAt"])] - async fn storage_keys_paged( + #[method(name = "childstate_getKeysPaged", aliases = ["childstate_getKeysPagedAt"], blocking)] + fn storage_keys_paged( &self, child_storage_key: PrefixedStorageKey, prefix: Option, @@ -51,8 +51,8 @@ pub trait ChildStateApi { ) -> RpcResult>; /// Returns a child storage entry at a specific block's state. - #[method(name = "childstate_getStorage")] - async fn storage( + #[method(name = "childstate_getStorage", blocking)] + fn storage( &self, child_storage_key: PrefixedStorageKey, key: StorageKey, @@ -60,8 +60,8 @@ pub trait ChildStateApi { ) -> RpcResult>; /// Returns child storage entries for multiple keys at a specific block's state. - #[method(name = "childstate_getStorageEntries")] - async fn storage_entries( + #[method(name = "childstate_getStorageEntries", blocking)] + fn storage_entries( &self, child_storage_key: PrefixedStorageKey, keys: Vec, @@ -69,8 +69,8 @@ pub trait ChildStateApi { ) -> RpcResult>>; /// Returns the hash of a child storage entry at a block's state. - #[method(name = "childstate_getStorageHash")] - async fn storage_hash( + #[method(name = "childstate_getStorageHash", blocking)] + fn storage_hash( &self, child_storage_key: PrefixedStorageKey, key: StorageKey, @@ -78,8 +78,8 @@ pub trait ChildStateApi { ) -> RpcResult>; /// Returns the size of a child storage entry at a block's state. - #[method(name = "childstate_getStorageSize")] - async fn storage_size( + #[method(name = "childstate_getStorageSize", blocking)] + fn storage_size( &self, child_storage_key: PrefixedStorageKey, key: StorageKey, @@ -87,8 +87,8 @@ pub trait ChildStateApi { ) -> RpcResult>; /// Returns proof of storage for child key entries at a specific block's state. - #[method(name = "state_getChildReadProof")] - async fn read_child_proof( + #[method(name = "state_getChildReadProof", blocking)] + fn read_child_proof( &self, child_storage_key: PrefixedStorageKey, keys: Vec, diff --git a/client/rpc/src/state/mod.rs b/client/rpc/src/state/mod.rs index a175df30d452e..fb15856c8c5a4 100644 --- a/client/rpc/src/state/mod.rs +++ b/client/rpc/src/state/mod.rs @@ -344,7 +344,7 @@ where Client: Send + Sync + 'static, { /// Returns proof of storage for a child key entries at a specific block's state. - async fn read_child_proof( + fn read_child_proof( &self, block: Option, storage_key: PrefixedStorageKey, @@ -353,7 +353,7 @@ where /// Returns the keys with prefix from a child storage, /// leave prefix empty to get all the keys. - async fn storage_keys( + fn storage_keys( &self, block: Option, storage_key: PrefixedStorageKey, @@ -361,7 +361,7 @@ where ) -> Result, Error>; /// Returns the keys with prefix from a child storage with pagination support. - async fn storage_keys_paged( + fn storage_keys_paged( &self, block: Option, storage_key: PrefixedStorageKey, @@ -371,7 +371,7 @@ where ) -> Result, Error>; /// Returns a child storage entry at a specific block's state. - async fn storage( + fn storage( &self, block: Option, storage_key: PrefixedStorageKey, @@ -379,7 +379,7 @@ where ) -> Result, Error>; /// Returns child storage entries at a specific block's state. - async fn storage_entries( + fn storage_entries( &self, block: Option, storage_key: PrefixedStorageKey, @@ -387,7 +387,7 @@ where ) -> Result>, Error>; /// Returns the hash of a child storage entry at a block's state. - async fn storage_hash( + fn storage_hash( &self, block: Option, storage_key: PrefixedStorageKey, @@ -395,13 +395,13 @@ where ) -> Result, Error>; /// Returns the size of a child storage entry at a block's state. - async fn storage_size( + fn storage_size( &self, block: Option, storage_key: PrefixedStorageKey, key: StorageKey, ) -> Result, Error> { - self.storage(block, storage_key, key).await.map(|x| x.map(|x| x.0.len() as u64)) + self.storage(block, storage_key, key).map(|x| x.map(|x| x.0.len() as u64)) } } @@ -416,19 +416,16 @@ where Block: BlockT + 'static, Client: Send + Sync + 'static, { - async fn storage_keys( + fn storage_keys( &self, storage_key: PrefixedStorageKey, key_prefix: StorageKey, block: Option, ) -> RpcResult> { - self.backend - .storage_keys(block, storage_key, key_prefix) - .await - .map_err(Into::into) + self.backend.storage_keys(block, storage_key, key_prefix).map_err(Into::into) } - async fn storage_keys_paged( + fn storage_keys_paged( &self, storage_key: PrefixedStorageKey, prefix: Option, @@ -438,47 +435,46 @@ where ) -> RpcResult> { self.backend .storage_keys_paged(block, storage_key, prefix, count, start_key) - .await .map_err(Into::into) } - async fn storage( + fn storage( &self, storage_key: PrefixedStorageKey, key: StorageKey, block: Option, ) -> RpcResult> { - self.backend.storage(block, storage_key, key).await.map_err(Into::into) + self.backend.storage(block, storage_key, key).map_err(Into::into) } - async fn storage_entries( + fn storage_entries( &self, storage_key: PrefixedStorageKey, keys: Vec, block: Option, ) -> RpcResult>> { - self.backend.storage_entries(block, storage_key, keys).await.map_err(Into::into) + self.backend.storage_entries(block, storage_key, keys).map_err(Into::into) } - async fn storage_hash( + fn storage_hash( &self, storage_key: PrefixedStorageKey, key: StorageKey, block: Option, ) -> RpcResult> { - self.backend.storage_hash(block, storage_key, key).await.map_err(Into::into) + self.backend.storage_hash(block, storage_key, key).map_err(Into::into) } - async fn storage_size( + fn storage_size( &self, storage_key: PrefixedStorageKey, key: StorageKey, block: Option, ) -> RpcResult> { - self.backend.storage_size(block, storage_key, key).await.map_err(Into::into) + self.backend.storage_size(block, storage_key, key).map_err(Into::into) } - async fn read_child_proof( + fn read_child_proof( &self, child_storage_key: PrefixedStorageKey, keys: Vec, @@ -486,7 +482,6 @@ where ) -> RpcResult> { self.backend .read_child_proof(block, child_storage_key, keys) - .await .map_err(Into::into) } } diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index f33564a9cef95..cce60aeacb597 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -497,7 +497,7 @@ where + 'static, Client::Api: Metadata, { - async fn read_child_proof( + fn read_child_proof( &self, block: Option, storage_key: PrefixedStorageKey, @@ -522,7 +522,7 @@ where .map_err(client_err) } - async fn storage_keys( + fn storage_keys( &self, block: Option, storage_key: PrefixedStorageKey, @@ -540,7 +540,7 @@ where .map_err(client_err) } - async fn storage_keys_paged( + fn storage_keys_paged( &self, block: Option, storage_key: PrefixedStorageKey, @@ -566,7 +566,7 @@ where .map_err(client_err) } - async fn storage( + fn storage( &self, block: Option, storage_key: PrefixedStorageKey, @@ -584,7 +584,7 @@ where .map_err(client_err) } - async fn storage_entries( + fn storage_entries( &self, block: Option, storage_key: PrefixedStorageKey, @@ -599,18 +599,20 @@ where }; let block = self.block_or_best(block).map_err(client_err)?; let client = self.client.clone(); - future::try_join_all(keys.into_iter().map(move |key| { - let res = client - .clone() - .child_storage(&BlockId::Hash(block), &child_info, &key) - .map_err(client_err); - - async move { res } - })) - .await + + keys.into_iter() + .map(move |key| { + let res = client + .clone() + .child_storage(&BlockId::Hash(block), &child_info, &key) + .map_err(client_err); + + res + }) + .collect() } - async fn storage_hash( + fn storage_hash( &self, block: Option, storage_key: PrefixedStorageKey, From 1b2343b07d636597c1d4bfc494a15477220c54b5 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 18 May 2022 18:17:36 +0300 Subject: [PATCH 04/10] client/rpc: `ChainApi` make RPC methods sync Signed-off-by: Alexandru Vasile --- client/rpc-api/src/chain/mod.rs | 4 +- client/rpc/src/chain/chain_full.rs | 4 +- client/rpc/src/chain/mod.rs | 12 +++--- client/rpc/src/state/tests.rs | 60 ++++++++++-------------------- 4 files changed, 30 insertions(+), 50 deletions(-) diff --git a/client/rpc-api/src/chain/mod.rs b/client/rpc-api/src/chain/mod.rs index f5f9524264e34..fc74ff916f11d 100644 --- a/client/rpc-api/src/chain/mod.rs +++ b/client/rpc-api/src/chain/mod.rs @@ -27,11 +27,11 @@ pub mod error; pub trait ChainApi { /// Get header. #[method(name = "chain_getHeader")] - async fn header(&self, hash: Option) -> RpcResult>; + fn header(&self, hash: Option) -> RpcResult>; /// Get header and body of a relay chain block. #[method(name = "chain_getBlock")] - async fn block(&self, hash: Option) -> RpcResult>; + fn block(&self, hash: Option) -> RpcResult>; /// Get hash of the n-th block in the canon chain. /// diff --git a/client/rpc/src/chain/chain_full.rs b/client/rpc/src/chain/chain_full.rs index 9ca6b3edcfe60..f051c46c10dc3 100644 --- a/client/rpc/src/chain/chain_full.rs +++ b/client/rpc/src/chain/chain_full.rs @@ -62,11 +62,11 @@ where &self.client } - async fn header(&self, hash: Option) -> Result, Error> { + fn header(&self, hash: Option) -> Result, Error> { self.client.header(BlockId::Hash(self.unwrap_or_best(hash))).map_err(client_err) } - async fn block(&self, hash: Option) -> Result>, Error> { + fn block(&self, hash: Option) -> Result>, Error> { self.client.block(&BlockId::Hash(self.unwrap_or_best(hash))).map_err(client_err) } diff --git a/client/rpc/src/chain/mod.rs b/client/rpc/src/chain/mod.rs index a79c66e0a18f6..d6c066e2c366c 100644 --- a/client/rpc/src/chain/mod.rs +++ b/client/rpc/src/chain/mod.rs @@ -64,10 +64,10 @@ where } /// Get header of a relay chain block. - async fn header(&self, hash: Option) -> Result, Error>; + fn header(&self, hash: Option) -> Result, Error>; /// Get header and body of a relay chain block. - async fn block(&self, hash: Option) -> Result>, Error>; + fn block(&self, hash: Option) -> Result>, Error>; /// Get hash of the n-th block in the canon chain. /// @@ -134,12 +134,12 @@ where Block::Header: Unpin, Client: HeaderBackend + BlockchainEvents + 'static, { - async fn header(&self, hash: Option) -> RpcResult> { - self.backend.header(hash).await.map_err(Into::into) + fn header(&self, hash: Option) -> RpcResult> { + self.backend.header(hash).map_err(Into::into) } - async fn block(&self, hash: Option) -> RpcResult>> { - self.backend.block(hash).await.map_err(Into::into) + fn block(&self, hash: Option) -> RpcResult>> { + self.backend.block(hash).map_err(Into::into) } fn block_hash( diff --git a/client/rpc/src/state/tests.rs b/client/rpc/src/state/tests.rs index a375a30d2c1a2..53dd8ebf50499 100644 --- a/client/rpc/src/state/tests.rs +++ b/client/rpc/src/state/tests.rs @@ -61,31 +61,23 @@ async fn should_return_storage() { assert_eq!( client .storage(key.clone(), Some(genesis_hash).into()) - .await .map(|x| x.map(|x| x.0.len())) .unwrap() .unwrap() as usize, VALUE.len(), ); assert_matches!( - client - .storage_hash(key.clone(), Some(genesis_hash).into()) - .await - .map(|x| x.is_some()), + client.storage_hash(key.clone(), Some(genesis_hash).into()).map(|x| x.is_some()), Ok(true) ); + assert_eq!(client.storage_size(key.clone(), None).unwrap().unwrap() as usize, VALUE.len(),); assert_eq!( - client.storage_size(key.clone(), None).await.unwrap().unwrap() as usize, - VALUE.len(), - ); - assert_eq!( - client.storage_size(StorageKey(b":map".to_vec()), None).await.unwrap().unwrap() as usize, + client.storage_size(StorageKey(b":map".to_vec()), None).unwrap().unwrap() as usize, 2 + 3, ); assert_eq!( child .storage(prefixed_storage_key(), key, Some(genesis_hash).into()) - .await .map(|x| x.map(|x| x.0.len())) .unwrap() .unwrap() as usize, @@ -114,7 +106,6 @@ async fn should_return_storage_entries() { assert_eq!( child .storage_entries(prefixed_storage_key(), keys.to_vec(), Some(genesis_hash).into()) - .await .map(|x| x.into_iter().map(|x| x.map(|x| x.0.len()).unwrap()).sum::()) .unwrap(), CHILD_VALUE1.len() + CHILD_VALUE2.len() @@ -126,7 +117,6 @@ async fn should_return_storage_entries() { assert_matches!( child .storage_entries(prefixed_storage_key(), failing_keys, Some(genesis_hash).into()) - .await .map(|x| x.iter().all(|x| x.is_some())), Ok(false) ); @@ -150,17 +140,16 @@ async fn should_return_child_storage() { child_key.clone(), key.clone(), Some(genesis_hash).into(), - ).await, + ), Ok(Some(StorageData(ref d))) if d[0] == 42 && d.len() == 1 ); assert_matches!( child .storage_hash(child_key.clone(), key.clone(), Some(genesis_hash).into(),) - .await .map(|x| x.is_some()), Ok(true) ); - assert_matches!(child.storage_size(child_key.clone(), key.clone(), None).await, Ok(Some(1))); + assert_matches!(child.storage_size(child_key.clone(), key.clone(), None), Ok(Some(1))); } #[tokio::test] @@ -179,7 +168,6 @@ async fn should_return_child_storage_entries() { let res = child .storage_entries(child_key.clone(), keys.clone(), Some(genesis_hash).into()) - .await .unwrap(); assert_matches!( @@ -193,18 +181,12 @@ async fn should_return_child_storage_entries() { if d[0] == 43 && d[1] == 44 && d.len() == 2 ); assert_matches!( - executor::block_on(child.storage_hash( - child_key.clone(), - keys[0].clone(), - Some(genesis_hash).into() - )) - .map(|x| x.is_some()), + child + .storage_hash(child_key.clone(), keys[0].clone(), Some(genesis_hash).into()) + .map(|x| x.is_some()), Ok(true) ); - assert_matches!( - child.storage_size(child_key.clone(), keys[0].clone(), None).await, - Ok(Some(1)) - ); + assert_matches!(child.storage_size(child_key.clone(), keys[0].clone(), None), Ok(Some(1))); } #[tokio::test] @@ -216,9 +198,7 @@ async fn should_call_contract() { use jsonrpsee::{core::Error, types::error::CallError}; assert_matches!( - client - .call("balanceOf".into(), Bytes(vec![1, 2, 3]), Some(genesis_hash).into()) - .await, + client.call("balanceOf".into(), Bytes(vec![1, 2, 3]), Some(genesis_hash).into()), Err(Error::Call(CallError::Failed(_))) ) } @@ -347,7 +327,7 @@ async fn should_query_storage() { let keys = (1..6).map(|k| StorageKey(vec![k])).collect::>(); let result = api.query_storage(keys.clone(), genesis_hash, Some(block1_hash).into()); - assert_eq!(result.await.unwrap(), expected); + assert_eq!(result.unwrap(), expected); // Query all changes let result = api.query_storage(keys.clone(), genesis_hash, None.into()); @@ -360,18 +340,18 @@ async fn should_query_storage() { (StorageKey(vec![5]), Some(StorageData(vec![1]))), ], }); - assert_eq!(result.await.unwrap(), expected); + assert_eq!(result.unwrap(), expected); // Query changes up to block2. let result = api.query_storage(keys.clone(), genesis_hash, Some(block2_hash)); - assert_eq!(result.await.unwrap(), expected); + assert_eq!(result.unwrap(), expected); // Inverted range. let result = api.query_storage(keys.clone(), block1_hash, Some(genesis_hash)); assert_eq!( - result.await.map_err(|e| e.to_string()), + result.map_err(|e| e.to_string()), Err(RpcError::Call(RpcCallError::Custom(ErrorObject::owned( 4001, Error::InvalidBlockRange { @@ -392,7 +372,7 @@ async fn should_query_storage() { let result = api.query_storage(keys.clone(), genesis_hash, Some(random_hash1)); assert_eq!( - result.await.map_err(|e| e.to_string()), + result.map_err(|e| e.to_string()), Err(RpcError::Call(RpcCallError::Custom(ErrorObject::owned( 4001, Error::InvalidBlockRange { @@ -413,7 +393,7 @@ async fn should_query_storage() { let result = api.query_storage(keys.clone(), random_hash1, Some(genesis_hash)); assert_eq!( - result.await.map_err(|e| e.to_string()), + result.map_err(|e| e.to_string()), Err(RpcError::Call(RpcCallError::Custom(ErrorObject::owned( 4001, Error::InvalidBlockRange { @@ -434,7 +414,7 @@ async fn should_query_storage() { let result = api.query_storage(keys.clone(), random_hash1, None); assert_eq!( - result.await.map_err(|e| e.to_string()), + result.map_err(|e| e.to_string()), Err(RpcError::Call(RpcCallError::Custom(ErrorObject::owned( 4001, Error::InvalidBlockRange { @@ -455,7 +435,7 @@ async fn should_query_storage() { let result = api.query_storage(keys.clone(), random_hash1, Some(random_hash2)); assert_eq!( - result.await.map_err(|e| e.to_string()), + result.map_err(|e| e.to_string()), Err(RpcError::Call(RpcCallError::Custom(ErrorObject::owned( 4001, Error::InvalidBlockRange { @@ -476,7 +456,7 @@ async fn should_query_storage() { let result = api.query_storage_at(keys.clone(), Some(block1_hash)); assert_eq!( - result.await.unwrap(), + result.unwrap(), vec![StorageChangeSet { block: block1_hash, changes: vec![ @@ -506,7 +486,7 @@ async fn should_return_runtime_version() { [\"0xf78b278be53f454c\",2],[\"0xab3c0572291feb8b\",1],[\"0xbc9d89904f5b923f\",1]],\ \"transactionVersion\":1,\"stateVersion\":1}"; - let runtime_version = api.runtime_version(None.into()).await.unwrap(); + let runtime_version = api.runtime_version(None.into()).unwrap(); let serialized = serde_json::to_string(&runtime_version).unwrap(); assert_eq!(serialized, result); From 4cc9512eb88c0fc5f20d0bca92cca8b5e4d12d65 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Fri, 20 May 2022 19:20:30 +0300 Subject: [PATCH 05/10] Remove unused async-traits Signed-off-by: Alexandru Vasile --- client/rpc/src/state/mod.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/rpc/src/state/mod.rs b/client/rpc/src/state/mod.rs index fb15856c8c5a4..3cf4225c481f0 100644 --- a/client/rpc/src/state/mod.rs +++ b/client/rpc/src/state/mod.rs @@ -53,7 +53,6 @@ use sp_blockchain::{HeaderBackend, HeaderMetadata}; const STORAGE_KEYS_PAGED_MAX_COUNT: u32 = 1000; /// State backend API. -#[async_trait] pub trait StateBackend: Send + Sync + 'static where Block: BlockT + 'static, @@ -203,7 +202,6 @@ pub struct StateApi { deny_unsafe: DenyUnsafe, } -#[async_trait] impl StateApiServer for StateApi where Block: BlockT + 'static, @@ -337,7 +335,6 @@ where } /// Child state backend API. -#[async_trait] pub trait ChildStateBackend: Send + Sync + 'static where Block: BlockT + 'static, @@ -410,7 +407,6 @@ pub struct ChildState { backend: Box>, } -#[async_trait] impl ChildStateApiServer for ChildState where Block: BlockT + 'static, From 1f93cbbe140ab2f2393fd886ce789f3534bbac55 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Fri, 20 May 2022 19:22:57 +0300 Subject: [PATCH 06/10] client/rpc-api: Make chain RPC methods blocking Signed-off-by: Alexandru Vasile --- client/rpc-api/src/chain/mod.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/client/rpc-api/src/chain/mod.rs b/client/rpc-api/src/chain/mod.rs index fc74ff916f11d..e26ddd33c9887 100644 --- a/client/rpc-api/src/chain/mod.rs +++ b/client/rpc-api/src/chain/mod.rs @@ -26,31 +26,32 @@ pub mod error; #[rpc(client, server)] pub trait ChainApi { /// Get header. - #[method(name = "chain_getHeader")] + #[method(name = "chain_getHeader", blocking)] fn header(&self, hash: Option) -> RpcResult>; /// Get header and body of a relay chain block. - #[method(name = "chain_getBlock")] + #[method(name = "chain_getBlock", blocking)] fn block(&self, hash: Option) -> RpcResult>; /// Get hash of the n-th block in the canon chain. /// /// By default returns latest block hash. - #[method(name = "chain_getBlockHash", aliases = ["chain_getHead"])] + #[method(name = "chain_getBlockHash", aliases = ["chain_getHead"], blocking)] fn block_hash( &self, hash: Option>, ) -> RpcResult>>; /// Get hash of the last finalized block in the canon chain. - #[method(name = "chain_getFinalizedHead", aliases = ["chain_getFinalisedHead"])] + #[method(name = "chain_getFinalizedHead", aliases = ["chain_getFinalisedHead"], blocking)] fn finalized_head(&self) -> RpcResult; /// All head subscription. #[subscription( name = "chain_subscribeAllHeads" => "chain_allHead", unsubscribe = "chain_unsubscribeAllHeads", - item = Header + item = Header, + blocking )] fn subscribe_all_heads(&self); @@ -60,7 +61,8 @@ pub trait ChainApi { aliases = ["subscribe_newHead", "chain_subscribeNewHead"], unsubscribe = "chain_unsubscribeNewHeads", unsubscribe_aliases = ["unsubscribe_newHead", "chain_unsubscribeNewHead"], - item = Header + item = Header, + blocking )] fn subscribe_new_heads(&self); @@ -70,7 +72,8 @@ pub trait ChainApi { aliases = ["chain_subscribeFinalisedHeads"], unsubscribe = "chain_unsubscribeFinalizedHeads", unsubscribe_aliases = ["chain_unsubscribeFinalisedHeads"], - item = Header + item = Header, + blocking )] fn subscribe_finalized_heads(&self); } From a5da46fa7755367a57880d1270baa11279ec70cb Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Fri, 20 May 2022 19:24:41 +0300 Subject: [PATCH 07/10] Update client/rpc/src/state/state_full.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/rpc/src/state/state_full.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index cce60aeacb597..89194515a4bd3 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -602,12 +602,10 @@ where keys.into_iter() .map(move |key| { - let res = client + client .clone() .child_storage(&BlockId::Hash(block), &child_info, &key) - .map_err(client_err); - - res + .map_err(client_err) }) .collect() } From 3c30d772adbaf0655ebc6828d330111fec853dcb Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Fri, 20 May 2022 19:28:10 +0300 Subject: [PATCH 08/10] Add `blocking` to `state_getKeysPaged` RPC call Signed-off-by: Alexandru Vasile --- client/rpc-api/src/state/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc-api/src/state/mod.rs b/client/rpc-api/src/state/mod.rs index ce325a3089f14..54bf21674a8bd 100644 --- a/client/rpc-api/src/state/mod.rs +++ b/client/rpc-api/src/state/mod.rs @@ -53,7 +53,7 @@ pub trait StateApi { /// 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. - #[method(name = "state_getKeysPaged", aliases = ["state_getKeysPagedAt"])] + #[method(name = "state_getKeysPaged", aliases = ["state_getKeysPagedAt"], blocking)] fn storage_keys_paged( &self, prefix: Option, From f09158b427e6f04bc956239e8a0b7acbbf87185e Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Fri, 20 May 2022 19:38:40 +0300 Subject: [PATCH 09/10] Fix build and warning Signed-off-by: Alexandru Vasile --- client/rpc-api/src/chain/mod.rs | 9 +++------ client/rpc/src/state/mod.rs | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/client/rpc-api/src/chain/mod.rs b/client/rpc-api/src/chain/mod.rs index e26ddd33c9887..c7cc97463983d 100644 --- a/client/rpc-api/src/chain/mod.rs +++ b/client/rpc-api/src/chain/mod.rs @@ -50,8 +50,7 @@ pub trait ChainApi { #[subscription( name = "chain_subscribeAllHeads" => "chain_allHead", unsubscribe = "chain_unsubscribeAllHeads", - item = Header, - blocking + item = Header )] fn subscribe_all_heads(&self); @@ -61,8 +60,7 @@ pub trait ChainApi { aliases = ["subscribe_newHead", "chain_subscribeNewHead"], unsubscribe = "chain_unsubscribeNewHeads", unsubscribe_aliases = ["unsubscribe_newHead", "chain_unsubscribeNewHead"], - item = Header, - blocking + item = Header )] fn subscribe_new_heads(&self); @@ -72,8 +70,7 @@ pub trait ChainApi { aliases = ["chain_subscribeFinalisedHeads"], unsubscribe = "chain_unsubscribeFinalizedHeads", unsubscribe_aliases = ["chain_unsubscribeFinalisedHeads"], - item = Header, - blocking + item = Header )] fn subscribe_finalized_heads(&self); } diff --git a/client/rpc/src/state/mod.rs b/client/rpc/src/state/mod.rs index 3cf4225c481f0..4b5b74950a486 100644 --- a/client/rpc/src/state/mod.rs +++ b/client/rpc/src/state/mod.rs @@ -28,7 +28,7 @@ use std::sync::Arc; use crate::SubscriptionTaskExecutor; use jsonrpsee::{ - core::{async_trait, Error as JsonRpseeError, RpcResult}, + core::{Error as JsonRpseeError, RpcResult}, ws_server::PendingSubscription, }; From 8c03e14aab799e2042182361d79ea83d0b822301 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Fri, 20 May 2022 20:03:03 +0300 Subject: [PATCH 10/10] Remove `async_trait` tidyup Signed-off-by: Alexandru Vasile --- client/rpc/src/chain/chain_full.rs | 3 +-- client/rpc/src/chain/mod.rs | 7 +------ client/rpc/src/state/state_full.rs | 7 +------ 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/client/rpc/src/chain/chain_full.rs b/client/rpc/src/chain/chain_full.rs index ac2f1ebd4fa6a..c00c6e5875d94 100644 --- a/client/rpc/src/chain/chain_full.rs +++ b/client/rpc/src/chain/chain_full.rs @@ -26,7 +26,7 @@ use futures::{ future::{self, FutureExt}, stream::{self, Stream, StreamExt}, }; -use jsonrpsee::{core::async_trait, PendingSubscription}; +use jsonrpsee::PendingSubscription; use sc_client_api::{BlockBackend, BlockchainEvents}; use sp_blockchain::HeaderBackend; use sp_runtime::{ @@ -51,7 +51,6 @@ impl FullChain { } } -#[async_trait] impl ChainBackend for FullChain where Block: BlockT + 'static, diff --git a/client/rpc/src/chain/mod.rs b/client/rpc/src/chain/mod.rs index d6c066e2c366c..a300c80271fd1 100644 --- a/client/rpc/src/chain/mod.rs +++ b/client/rpc/src/chain/mod.rs @@ -27,10 +27,7 @@ use std::sync::Arc; use crate::SubscriptionTaskExecutor; -use jsonrpsee::{ - core::{async_trait, RpcResult}, - PendingSubscription, -}; +use jsonrpsee::{core::RpcResult, PendingSubscription}; use sc_client_api::BlockchainEvents; use sp_rpc::{list::ListOrValue, number::NumberOrHex}; use sp_runtime::{ @@ -45,7 +42,6 @@ pub use sc_rpc_api::chain::*; use sp_blockchain::HeaderBackend; /// Blockchain backend API -#[async_trait] trait ChainBackend: Send + Sync + 'static where Block: BlockT + 'static, @@ -126,7 +122,6 @@ pub struct Chain { backend: Box>, } -#[async_trait] impl ChainApiServer, Block::Hash, Block::Header, SignedBlock> for Chain where diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index cd7c342f53cb0..c58638c870ab3 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -28,10 +28,7 @@ use super::{ use crate::SubscriptionTaskExecutor; use futures::{future, stream, FutureExt, StreamExt}; -use jsonrpsee::{ - core::{async_trait, Error as JsonRpseeError}, - PendingSubscription, -}; +use jsonrpsee::{core::Error as JsonRpseeError, PendingSubscription}; use sc_client_api::{ Backend, BlockBackend, BlockchainEvents, CallExecutor, ExecutorProvider, ProofProvider, StorageProvider, @@ -170,7 +167,6 @@ where } } -#[async_trait] impl StateBackend for FullState where Block: BlockT + 'static, @@ -474,7 +470,6 @@ where } } -#[async_trait] impl ChildStateBackend for FullState where Block: BlockT + 'static,