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 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
90 changes: 36 additions & 54 deletions substrate/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,21 @@ build_rpc_trait! {
pub trait StateApi<Hash> {
type Metadata;

/// Returns a storage entry at a specific block's state.
#[rpc(name = "state_getStorageAt")]
fn storage_at(&self, StorageKey, Hash) -> Result<Option<StorageData>>;

/// Call a contract at a block's state.
#[rpc(name = "state_callAt")]
fn call_at(&self, String, Vec<u8>, Hash) -> Result<Vec<u8>>;
#[rpc(name = "state_call", alias = ["state_callAt", ])]
fn call(&self, String, Vec<u8>, Trailing<Hash>) -> Result<Vec<u8>>;

/// Returns a storage entry at a specific block's state.
#[rpc(name = "state_getStorage", alias = ["state_getStorageAt", ])]
fn storage(&self, StorageKey, Trailing<Hash>) -> Result<Option<StorageData>>;

/// Returns the hash of a storage entry at a block's state.
#[rpc(name = "state_getStorageHashAt")]
fn storage_hash_at(&self, StorageKey, Hash) -> Result<Option<Hash>>;
#[rpc(name = "state_getStorageHash", alias = ["state_getStorageHashAt", ])]
fn storage_hash(&self, StorageKey, Trailing<Hash>) -> Result<Option<Hash>>;

/// Returns the size of a storage entry at a block's state.
#[rpc(name = "state_getStorageSizeAt")]
fn storage_size_at(&self, StorageKey, Hash) -> Result<Option<u64>>;

/// Returns the hash of a storage entry at the best block.
#[rpc(name = "state_getStorageHash")]
fn storage_hash(&self, StorageKey) -> Result<Option<Hash>>;

/// Returns the size of a storage entry at the best block.
#[rpc(name = "state_getStorageSize")]
fn storage_size(&self, StorageKey) -> Result<Option<u64>>;

/// Returns a storage entry at the best block.
#[rpc(name = "state_getStorage")]
fn storage(&self, StorageKey) -> Result<Option<StorageData>>;

/// Call a contract at the best block.
#[rpc(name = "state_call")]
fn call(&self, String, Vec<u8>) -> Result<Vec<u8>>;
#[rpc(name = "state_getStorageSize", alias = ["state_getStorageSizeAt", ])]
fn storage_size(&self, StorageKey, Trailing<Hash>) -> Result<Option<u64>>;

#[pubsub(name = "state_storage")] {
/// New storage subscription
Expand Down Expand Up @@ -105,47 +89,45 @@ impl<B, E, Block: BlockT> State<B, E, Block> {
}
}

impl<B, E, Block> State<B, E, Block> where
Block: BlockT + 'static,
B: client::backend::Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + 'static,
{
fn unwrap_or_best(&self, hash: Trailing<Block::Hash>) -> Result<Block::Hash> {
Ok(match hash.into() {
None => self.client.info()?.chain.best_hash,
Some(hash) => hash,
})
}
}

impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
Block: BlockT + 'static,
B: client::backend::Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + 'static,
{
type Metadata = ::metadata::Metadata;

fn storage_at(&self, key: StorageKey, block: Block::Hash) -> Result<Option<StorageData>> {
trace!(target: "rpc", "Querying storage at {:?} for key {}", block, HexDisplay::from(&key.0));
Ok(self.client.storage(&BlockId::Hash(block), &key)?)
}

fn call_at(&self, method: String, data: Vec<u8>, block: Block::Hash) -> Result<Vec<u8>> {
fn call(&self, method: String, data: Vec<u8>, block: Trailing<Block::Hash>) -> Result<Vec<u8>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Calling runtime at {:?} for method {} ({})", block, method, HexDisplay::from(&data));
Ok(self.client.executor().call(&BlockId::Hash(block), &method, &data)?.return_data)
}

fn storage_hash_at(&self, key: StorageKey, block: Block::Hash) -> Result<Option<Block::Hash>> {
use runtime_primitives::traits::{Hash, Header as HeaderT};
Ok(self.storage_at(key, block)?.map(|x| <Block::Header as HeaderT>::Hashing::hash(&x.0)))
}

fn storage_size_at(&self, key: StorageKey, block: Block::Hash) -> Result<Option<u64>> {
Ok(self.storage_at(key, block)?.map(|x| x.0.len() as u64))
}

fn storage_hash(&self, key: StorageKey) -> Result<Option<Block::Hash>> {
self.storage_hash_at(key, self.client.info()?.chain.best_hash)
}

fn storage_size(&self, key: StorageKey) -> Result<Option<u64>> {
self.storage_size_at(key, self.client.info()?.chain.best_hash)
fn storage(&self, key: StorageKey, block: Trailing<Block::Hash>) -> Result<Option<StorageData>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Querying storage at {:?} for key {}", block, HexDisplay::from(&key.0));
Ok(self.client.storage(&BlockId::Hash(block), &key)?)
}

fn storage(&self, key: StorageKey) -> Result<Option<StorageData>> {
self.storage_at(key, self.client.info()?.chain.best_hash)

fn storage_hash(&self, key: StorageKey, block: Trailing<Block::Hash>) -> Result<Option<Block::Hash>> {
use runtime_primitives::traits::{Hash, Header as HeaderT};
Ok(self.storage(key, block)?.map(|x| <Block::Header as HeaderT>::Hashing::hash(&x.0)))
}

fn call(&self, method: String, data: Vec<u8>) -> Result<Vec<u8>> {
self.call_at(method, data, self.client.info()?.chain.best_hash)
fn storage_size(&self, key: StorageKey, block: Trailing<Block::Hash>) -> Result<Option<u64>> {
Ok(self.storage(key, block)?.map(|x| x.0.len() as u64))
}

fn subscribe_storage(
Expand All @@ -166,14 +148,14 @@ impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
// initial values
let initial = stream::iter_result(keys
.map(|keys| {
let block = self.client.info().map(|info| info.chain.best_hash).unwrap_or_default();
let changes = keys
.into_iter()
.map(|key| self.storage(key.clone())
.map(|key| self.storage(key.clone(), Some(block.clone()).into())
.map(|val| (key.clone(), val))
.unwrap_or_else(|_| (key, None))
)
.collect();
let block = self.client.info().map(|info| info.chain.best_hash).unwrap_or_default();
vec![Ok(Ok(StorageChangeSet { block, changes }))]
}).unwrap_or_default());

Expand Down
4 changes: 2 additions & 2 deletions substrate/rpc/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn should_return_storage() {
let client = State::new(client, core.executor());

assert_matches!(
client.storage_at(StorageKey(vec![10]), genesis_hash),
client.storage(StorageKey(vec![10]), Some(genesis_hash).into()),
Ok(None)
)
}
Expand All @@ -44,7 +44,7 @@ fn should_call_contract() {
let client = State::new(client, core.executor());

assert_matches!(
client.call_at("balanceOf".into(), vec![1,2,3], genesis_hash),
client.call("balanceOf".into(), vec![1,2,3], Some(genesis_hash).into()),
Err(Error(ErrorKind::Client(client::error::ErrorKind::Execution(_)), _))
)
}
Expand Down