Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
client/api: Ensure state_* RPC methods are blocking
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed May 18, 2022
commit 9790d36d41cbe40e456b2ee32b4cdeea8891b39a
50 changes: 23 additions & 27 deletions client/rpc-api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ pub use self::helpers::ReadProof;
#[rpc(client, server)]
pub trait StateApi<Hash> {
/// 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<Hash>) -> RpcResult<Bytes>;
#[method(name = "state_call", aliases = ["state_callAt"], blocking)]
fn call(&self, name: String, bytes: Bytes, hash: Option<Hash>) -> RpcResult<Bytes>;

/// Returns the keys with prefix, leave empty to get all the keys.
#[method(name = "state_getKeys", blocking)]
#[deprecated(since = "2.0.0", note = "Please use `getKeysPaged` with proper paging support")]
fn storage_keys(&self, prefix: StorageKey, hash: Option<Hash>) -> RpcResult<Vec<StorageKey>>;

/// 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<Hash>,
Expand All @@ -54,7 +54,7 @@ pub trait StateApi<Hash> {
/// 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<StorageKey>,
count: u32,
Expand All @@ -63,53 +63,49 @@ pub trait StateApi<Hash> {
) -> RpcResult<Vec<StorageKey>>;

/// 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<Hash>) -> RpcResult<Option<StorageData>>;
#[method(name = "state_getStorage", aliases = ["state_getStorageAt"], blocking)]
fn storage(&self, key: StorageKey, hash: Option<Hash>) -> RpcResult<Option<StorageData>>;

/// 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<Hash>) -> RpcResult<Option<Hash>>;
#[method(name = "state_getStorageHash", aliases = ["state_getStorageHashAt"], blocking)]
fn storage_hash(&self, key: StorageKey, hash: Option<Hash>) -> RpcResult<Option<Hash>>;

/// 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<Hash>) -> RpcResult<Option<u64>>;
#[method(name = "state_getStorageSize", aliases = ["state_getStorageSizeAt"], blocking)]
fn storage_size(&self, key: StorageKey, hash: Option<Hash>) -> RpcResult<Option<u64>>;

/// Returns the runtime metadata as an opaque blob.
#[method(name = "state_getMetadata")]
async fn metadata(&self, hash: Option<Hash>) -> RpcResult<Bytes>;
#[method(name = "state_getMetadata", blocking)]
fn metadata(&self, hash: Option<Hash>) -> RpcResult<Bytes>;

/// Get the runtime version.
#[method(name = "state_getRuntimeVersion", aliases = ["chain_getRuntimeVersion"])]
async fn runtime_version(&self, hash: Option<Hash>) -> RpcResult<RuntimeVersion>;
#[method(name = "state_getRuntimeVersion", aliases = ["chain_getRuntimeVersion"], blocking)]
fn runtime_version(&self, hash: Option<Hash>) -> RpcResult<RuntimeVersion>;

/// 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<StorageKey>,
block: Hash,
hash: Option<Hash>,
) -> RpcResult<Vec<StorageChangeSet<Hash>>>;

/// 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<StorageKey>,
at: Option<Hash>,
) -> RpcResult<Vec<StorageChangeSet<Hash>>>;

/// Returns proof of storage entries at a specific block's state.
#[method(name = "state_getReadProof")]
async fn read_proof(
&self,
keys: Vec<StorageKey>,
hash: Option<Hash>,
) -> RpcResult<ReadProof<Hash>>;
#[method(name = "state_getReadProof", blocking)]
fn read_proof(&self, keys: Vec<StorageKey>, hash: Option<Hash>) -> RpcResult<ReadProof<Hash>>;

/// New runtime version subscription
#[subscription(
Expand Down Expand Up @@ -281,8 +277,8 @@ pub trait StateApi<Hash> {
///
/// 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<String>,
Expand Down
79 changes: 34 additions & 45 deletions client/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
Client: Send + Sync + 'static,
{
/// Call runtime method at given block.
async fn call(
fn call(
&self,
block: Option<Block::Hash>,
method: String,
Expand All @@ -75,14 +75,14 @@ where
) -> Result<Vec<StorageKey>, 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<Block::Hash>,
prefix: StorageKey,
) -> Result<Vec<(StorageKey, StorageData)>, Error>;

/// Returns the keys with prefix with pagination support.
async fn storage_keys_paged(
fn storage_keys_paged(
&self,
block: Option<Block::Hash>,
prefix: Option<StorageKey>,
Expand All @@ -91,14 +91,14 @@ where
) -> Result<Vec<StorageKey>, Error>;

/// Returns a storage entry at a specific block's state.
async fn storage(
fn storage(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> Result<Option<StorageData>, Error>;

/// Returns the hash of a storage entry at a block's state.
async fn storage_hash(
fn storage_hash(
&self,
block: Option<Block::Hash>,
key: StorageKey,
Expand All @@ -108,46 +108,46 @@ 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<Block::Hash>,
key: StorageKey,
) -> Result<Option<u64>, Error>;

/// Returns the runtime metadata as an opaque blob.
async fn metadata(&self, block: Option<Block::Hash>) -> Result<Bytes, Error>;
fn metadata(&self, block: Option<Block::Hash>) -> Result<Bytes, Error>;

/// Get the runtime version.
async fn runtime_version(&self, block: Option<Block::Hash>) -> Result<RuntimeVersion, Error>;
fn runtime_version(&self, block: Option<Block::Hash>) -> Result<RuntimeVersion, Error>;

/// 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<Block::Hash>,
keys: Vec<StorageKey>,
) -> Result<Vec<StorageChangeSet<Block::Hash>>, 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<StorageKey>,
at: Option<Block::Hash>,
) -> Result<Vec<StorageChangeSet<Block::Hash>>, Error>;

/// Returns proof of storage entries at a specific block's state.
async fn read_proof(
fn read_proof(
&self,
block: Option<Block::Hash>,
keys: Vec<StorageKey>,
) -> Result<ReadProof<Block::Hash>, Error>;

/// Trace storage changes for block
async fn trace_block(
fn trace_block(
&self,
block: Block::Hash,
targets: Option<String>,
Expand Down Expand Up @@ -209,13 +209,8 @@ where
Block: BlockT + 'static,
Client: Send + Sync + 'static,
{
async fn call(
&self,
method: String,
data: Bytes,
block: Option<Block::Hash>,
) -> RpcResult<Bytes> {
self.backend.call(block, method, data).await.map_err(Into::into)
fn call(&self, method: String, data: Bytes, block: Option<Block::Hash>) -> RpcResult<Bytes> {
self.backend.call(block, method, data).map_err(Into::into)
}

fn storage_keys(
Expand All @@ -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<Block::Hash>,
) -> RpcResult<Vec<(StorageKey, StorageData)>> {
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<StorageKey>,
count: u32,
Expand All @@ -250,74 +245,69 @@ 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<Block::Hash>,
) -> RpcResult<Option<StorageData>> {
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<Block::Hash>,
) -> RpcResult<Option<Block::Hash>> {
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<Block::Hash>,
) -> RpcResult<Option<u64>> {
self.backend.storage_size(block, key).await.map_err(Into::into)
fn storage_size(&self, key: StorageKey, block: Option<Block::Hash>) -> RpcResult<Option<u64>> {
self.backend.storage_size(block, key).map_err(Into::into)
}

async fn metadata(&self, block: Option<Block::Hash>) -> RpcResult<Bytes> {
self.backend.metadata(block).await.map_err(Into::into)
fn metadata(&self, block: Option<Block::Hash>) -> RpcResult<Bytes> {
self.backend.metadata(block).map_err(Into::into)
}

async fn runtime_version(&self, at: Option<Block::Hash>) -> RpcResult<RuntimeVersion> {
self.backend.runtime_version(at).await.map_err(Into::into)
fn runtime_version(&self, at: Option<Block::Hash>) -> RpcResult<RuntimeVersion> {
self.backend.runtime_version(at).map_err(Into::into)
}

async fn query_storage(
fn query_storage(
&self,
keys: Vec<StorageKey>,
from: Block::Hash,
to: Option<Block::Hash>,
) -> RpcResult<Vec<StorageChangeSet<Block::Hash>>> {
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<StorageKey>,
at: Option<Block::Hash>,
) -> RpcResult<Vec<StorageChangeSet<Block::Hash>>> {
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<StorageKey>,
block: Option<Block::Hash>,
) -> RpcResult<ReadProof<Block::Hash>> {
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`
/// and capture all state changes.
///
/// 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<String>,
Expand All @@ -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)
}

Expand Down
Loading