Access child storage over RPC. (#2586)

* Access child storage over RPC.

* Address review grumbles.

* Test happy case in child_storage rpc.

* Remove stray printlns

* Fix line widths.

* Bump runtime again.

* Fix genesis storage root calculation for light clients.

* Don't pass values to full_storage_root child_delta.
This commit is contained in:
Tomasz Drwięga
2019-05-19 19:02:09 +02:00
committed by DemiMarie-parity
parent a827869dfb
commit 98de97e1d9
11 changed files with 248 additions and 24 deletions
+85 -2
View File
@@ -57,7 +57,7 @@ pub trait StateApi<Hash> {
/// Returns the keys with prefix, leave empty to get all the keys
#[rpc(name = "state_getKeys")]
fn storage_keys(&self, key: StorageKey, hash: Option<Hash>) -> Result<Vec<StorageKey>>;
fn storage_keys(&self, prefix: StorageKey, hash: Option<Hash>) -> Result<Vec<StorageKey>>;
/// Returns a storage entry at a specific block's state.
#[rpc(name = "state_getStorage", alias("state_getStorageAt"))]
@@ -71,6 +71,40 @@ pub trait StateApi<Hash> {
#[rpc(name = "state_getStorageSize", alias("state_getStorageSizeAt"))]
fn storage_size(&self, key: StorageKey, hash: Option<Hash>) -> Result<Option<u64>>;
/// Returns the keys with prefix from a child storage, leave empty to get all the keys
#[rpc(name = "state_getChildKeys")]
fn child_storage_keys(
&self,
child_storage_key: StorageKey,
prefix: StorageKey,
hash: Option<Hash>
) -> Result<Vec<StorageKey>>;
/// Returns a child storage entry at a specific block's state.
#[rpc(name = "state_getChildStorage")]
fn child_storage(
&self,
child_storage_key: StorageKey,
key: StorageKey, hash: Option<Hash>
) -> Result<Option<StorageData>>;
/// Returns the hash of a child storage entry at a block's state.
#[rpc(name = "state_getChildStorageHash")]
fn child_storage_hash(
&self,
child_storage_key: StorageKey,
key: StorageKey, hash: Option<Hash>
) -> Result<Option<Hash>>;
/// Returns the size of a child storage entry at a block's state.
#[rpc(name = "state_getChildStorageSize")]
fn child_storage_size(
&self,
child_storage_key: StorageKey,
key: StorageKey,
hash: Option<Hash>
) -> Result<Option<u64>>;
/// Returns the runtime metadata as an opaque blob.
#[rpc(name = "state_getMetadata")]
fn metadata(&self, hash: Option<Hash>) -> Result<Bytes>;
@@ -84,7 +118,12 @@ pub trait StateApi<Hash> {
/// 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).
#[rpc(name = "state_queryStorage")]
fn query_storage(&self, keys: Vec<StorageKey>, block: Hash, hash: Option<Hash>) -> Result<Vec<StorageChangeSet<Hash>>>;
fn query_storage(
&self,
keys: Vec<StorageKey>,
block: Hash,
hash: Option<Hash>
) -> Result<Vec<StorageChangeSet<Hash>>>;
/// New runtime version subscription
#[pubsub(
@@ -324,6 +363,50 @@ impl<B, E, Block, RA> StateApi<Block::Hash> for State<B, E, Block, RA> where
Ok(self.storage(key, block)?.map(|x| x.0.len() as u64))
}
fn child_storage(
&self,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> Result<Option<StorageData>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Querying child storage at {:?} for key {}", block, HexDisplay::from(&key.0));
Ok(self.client.child_storage(&BlockId::Hash(block), &child_storage_key, &key)?)
}
fn child_storage_keys(
&self,
child_storage_key: StorageKey,
key_prefix: StorageKey,
block: Option<Block::Hash>
) -> Result<Vec<StorageKey>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Querying child storage keys at {:?}", block);
Ok(self.client.child_storage_keys(&BlockId::Hash(block), &child_storage_key, &key_prefix)?)
}
fn child_storage_hash(
&self,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> Result<Option<Block::Hash>> {
use runtime_primitives::traits::{Hash, Header as HeaderT};
Ok(
self.child_storage(child_storage_key, key, block)?
.map(|x| <Block::Header as HeaderT>::Hashing::hash(&x.0))
)
}
fn child_storage_size(
&self,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> Result<Option<u64>> {
Ok(self.child_storage(child_storage_key, key, block)?.map(|x| x.0.len() as u64))
}
fn metadata(&self, block: Option<Block::Hash>) -> Result<Bytes> {
let block = self.unwrap_or_best(block)?;
self.client.runtime_api().metadata(&BlockId::Hash(block)).map(Into::into).map_err(Into::into)
+40 -4
View File
@@ -17,9 +17,10 @@
use super::*;
use self::error::{Error, ErrorKind};
use sr_io::blake2_256;
use assert_matches::assert_matches;
use consensus::BlockOrigin;
use primitives::storage::well_known_keys;
use sr_io::blake2_256;
use test_client::{self, runtime, AccountKeyring, TestClient, BlockBuilderExt};
#[test]
@@ -28,11 +29,46 @@ fn should_return_storage() {
let client = Arc::new(test_client::new());
let genesis_hash = client.genesis_hash();
let client = State::new(client, Subscriptions::new(core.executor()));
let key = StorageKey(b":code".to_vec());
assert!(
client.storage(key.clone(), Some(genesis_hash).into())
.map(|x| x.map(|x| x.0.len())).unwrap().unwrap()
> 195_000
);
assert_matches!(
client.storage_hash(key.clone(), Some(genesis_hash).into()).map(|x| x.is_some()),
Ok(true)
);
assert!(
client.storage_size(key.clone(), None).unwrap().unwrap()
> 195_000
);
}
#[test]
fn should_return_child_storage() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::new());
let genesis_hash = client.genesis_hash();
let client = State::new(client, Subscriptions::new(core.executor()));
let child_key = StorageKey(well_known_keys::CHILD_STORAGE_KEY_PREFIX.iter().chain(b"test").cloned().collect());
let key = StorageKey(b"key".to_vec());
assert_matches!(
client.storage(StorageKey(vec![10]), Some(genesis_hash).into()),
Ok(None)
)
client.child_storage(child_key.clone(), key.clone(), Some(genesis_hash).into()),
Ok(Some(StorageData(ref d))) if d[0] == 42 && d.len() == 1
);
assert_matches!(
client.child_storage_hash(child_key.clone(), key.clone(), Some(genesis_hash).into())
.map(|x| x.is_some()),
Ok(true)
);
assert_matches!(
client.child_storage_size(child_key.clone(), key.clone(), None),
Ok(Some(1))
);
}
#[test]