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
13 changes: 6 additions & 7 deletions substrate/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,16 @@ impl<B, E, Block> Client<B, E, Block> where
}

/// Return single storage entry of contract under given address in state in a block of given hash.
pub fn storage(&self, id: &BlockId<Block>, key: &StorageKey) -> error::Result<StorageData> {
Ok(StorageData(self.state_at(id)?
pub fn storage(&self, id: &BlockId<Block>, key: &StorageKey) -> error::Result<Option<StorageData>> {
Ok(self.state_at(id)?
.storage(&key.0).map_err(|e| error::Error::from_state(Box::new(e)))?
.ok_or_else(|| error::ErrorKind::NoValueForKey(key.0.clone()))?
.to_vec()))
.map(StorageData))
}

/// Get the code at a given block.
pub fn code_at(&self, id: &BlockId<Block>) -> error::Result<Vec<u8>> {
self.storage(id, &StorageKey(b":code".to_vec())).map(|data| data.0)
Ok(self.storage(id, &StorageKey(b":code".to_vec()))?
.expect("None is returned if there's no value stored for the given key; ':code' key is always defined; qed").0)
}

/// Get the set of authorities at a given block.
Expand Down Expand Up @@ -580,13 +580,12 @@ impl<B, E, Block> BlockBody<Block> for Client<B, E, Block>
#[cfg(test)]
mod tests {
use super::*;
use codec::Encode;
use keyring::Keyring;
use test_client::{self, TestClient};
use test_client::client::BlockOrigin;
use test_client::client::backend::Backend as TestBackend;
use test_client::{runtime as test_runtime, BlockBuilderExt};
use test_client::runtime::{Transfer, Extrinsic};
use test_client::runtime::Transfer;

#[test]
fn client_initialises_from_genesis_ok() {
Expand Down
7 changes: 0 additions & 7 deletions substrate/client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use std;
use state_machine;
use primitives::hexdisplay::HexDisplay;
use runtime_primitives::ApplyError;

error_chain! {
Expand Down Expand Up @@ -53,12 +52,6 @@ error_chain! {
display("Blockchain: {}", e),
}

/// Attempt to read storage yet nothing set for that key.
NoValueForKey(key: Vec<u8>) {
description("storage doesn't contain key"),
display("Storage does not contain the key entry: {}", HexDisplay::from(key)),
}

/// Invalid state data.
AuthLenEmpty {
description("authority count state error"),
Expand Down
30 changes: 15 additions & 15 deletions substrate/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,31 @@ build_rpc_trait! {

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

/// Returns the hash of a storage entry at a block's state.
#[rpc(name = "state_getStorageHashAt")]
fn storage_hash_at(&self, StorageKey, Hash) -> Result<Hash>;
fn storage_hash_at(&self, StorageKey, 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<u64>;
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<Hash>;
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<u64>;
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<StorageData>;
fn storage(&self, StorageKey) -> Result<Option<StorageData>>;

/// Call a contract at the best block.
#[rpc(name = "state_call")]
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
{
type Metadata = ::metadata::Metadata;

fn storage_at(&self, key: StorageKey, block: Block::Hash) -> Result<StorageData> {
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)?)
}
Expand All @@ -122,24 +122,24 @@ impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
Ok(self.client.executor().call(&BlockId::Hash(block), &method, &data)?.return_data)
}

fn storage_hash_at(&self, key: StorageKey, block: Block::Hash) -> Result<Block::Hash> {
fn storage_hash_at(&self, key: StorageKey, block: Block::Hash) -> Result<Option<Block::Hash>> {
use runtime_primitives::traits::{Hash, Header as HeaderT};
self.storage_at(key, block).map(|x| <Block::Header as HeaderT>::Hashing::hash(&x.0))
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<u64> {
self.storage_at(key, block).map(|x| x.0.len() as u64)
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<Block::Hash> {
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<u64> {
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) -> Result<StorageData> {
fn storage(&self, key: StorageKey) -> Result<Option<StorageData>> {
self.storage_at(key, self.client.info()?.chain.best_hash)

}
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
let changes = keys
.into_iter()
.map(|key| self.storage(key.clone())
.map(|val| (key.clone(), Some(val)))
.map(|val| (key.clone(), val))
.unwrap_or_else(|_| (key, None))
)
.collect();
Expand Down
2 changes: 1 addition & 1 deletion substrate/rpc/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn should_return_storage() {

assert_matches!(
client.storage_at(StorageKey(vec![10]), genesis_hash),
Err(Error(ErrorKind::Client(client::error::ErrorKind::NoValueForKey(ref k)), _)) if *k == vec![10]
Ok(None)
)
}

Expand Down