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
Merge branch 'master' into ch-ch-trie
  • Loading branch information
cheme committed Sep 2, 2019
commit 54383a92f47f0b7d49202c08d58e7120b9218080
148 changes: 18 additions & 130 deletions core/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,134 +83,34 @@ pub trait StateBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
/// Returns a storage entry at a specific block's state.
fn storage(
&self,
range: &QueryStorageRange<Block>,
keys: &[StorageKey],
last_values: &HashMap<StorageKey, Option<StorageData>>,
changes: &mut Vec<StorageChangeSet<Block::Hash>>,
) -> Result<()> {
let (begin, end) = match range.filtered_range {
Some(ref filtered_range) => (
range.first_number + filtered_range.start.saturated_into(),
BlockId::Hash(range.hashes[filtered_range.end - 1].clone())
),
None => return Ok(()),
};
let mut changes_map: BTreeMap<NumberFor<Block>, StorageChangeSet<Block::Hash>> = BTreeMap::new();
for key in keys {
let mut last_block = None;
let mut last_value = last_values.get(key).cloned().unwrap_or_default();
for (block, _) in self.client.key_changes(begin, end, None, key)
.map_err(client_err)?.into_iter().rev() {
if last_block == Some(block) {
continue;
}

let block_hash = range.hashes[(block - range.first_number).saturated_into::<usize>()].clone();
let id = BlockId::Hash(block_hash);
let value_at_block = self.client.storage(&id, key).map_err(client_err)?;
if last_value == value_at_block {
continue;
}

changes_map.entry(block)
.or_insert_with(|| StorageChangeSet { block: block_hash, changes: Vec::new() })
.changes.push((key.clone(), value_at_block.clone()));
last_block = Some(block);
last_value = value_at_block;
}
}
if let Some(additional_capacity) = changes_map.len().checked_sub(changes.len()) {
changes.reserve(additional_capacity);
}
changes.extend(changes_map.into_iter().map(|(_, cs)| cs));
Ok(())
}
}

impl<B, E, Block, RA> State<B, E, Block, RA> where
Block: BlockT<Hash=H256>,
B: client::backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
{
fn unwrap_or_best(&self, hash: Option<Block::Hash>) -> Result<Block::Hash> {
crate::helpers::unwrap_or_else(|| Ok(self.client.info().chain.best_hash), hash)
}
}

impl<B, E, Block, RA> StateApi<Block::Hash> for State<B, E, Block, RA> where
Block: BlockT<Hash=H256> + 'static,
B: client::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client<B, E, Block, RA>: ProvideRuntimeApi,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api: Metadata<Block>
{
type Metadata = crate::metadata::Metadata;

fn call(&self, method: String, data: Bytes, block: Option<Block::Hash>) -> Result<Bytes> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Calling runtime at {:?} for method {} ({})", block, method, HexDisplay::from(&data.0));
let return_data = self.client
.executor()
.call(
&BlockId::Hash(block),
&method, &data.0, ExecutionStrategy::NativeElseWasm, state_machine::NeverOffchainExt::new(),
)
.map_err(client_err)?;
Ok(Bytes(return_data))
}

fn storage_keys(&self, key_prefix: StorageKey, block: Option<Block::Hash>) -> Result<Vec<StorageKey>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Querying storage keys at {:?}", block);
Ok(self.client.storage_keys(&BlockId::Hash(block), &key_prefix).map_err(client_err)?)
}

fn storage(&self, key: StorageKey, block: Option<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).map_err(client_err)?)
}

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

fn storage_size(&self, key: StorageKey, block: Option<Block::Hash>) -> Result<Option<u64>> {
Ok(self.storage(key, block)?.map(|x| x.0.len() as u64))
}
/// Returns the hash of a storage entry at a block's state.
fn storage_hash(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>>;

fn child_storage(
/// Returns the size of a storage entry at a block's state.
fn storage_size(
&self,
child_storage_key: StorageKey,
block: Option<Block::Hash>,
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)
.map_err(client_err)?
)
) -> FutureResult<Option<u64>> {
Box::new(self.storage(block, key)
.map(|x| x.map(|x| x.0.len() as u64)))
}

/// Returns the keys with prefix from a child storage, leave empty to get all the keys
fn child_storage_keys(
&self,
block: Option<Block::Hash>,
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)
.map_err(client_err)?
)
}
prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>>;

/// Returns a child storage entry at a specific block's state.
fn child_storage(
Expand All @@ -226,19 +126,7 @@ impl<B, E, Block, RA> StateApi<Block::Hash> for State<B, E, Block, RA> where
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> Result<Option<Block::Hash>> {
let block = self.unwrap_or_best(block)?;
trace!(
target: "rpc", "Querying child storage hash at {:?} for key {}",
block,
HexDisplay::from(&key.0),
);
Ok(self.client
.child_storage_hash(&BlockId::Hash(block), &child_storage_key, &key)
.map_err(client_err)?
)
}
) -> FutureResult<Option<Block::Hash>>;

/// Returns the size of a child storage entry at a block's state.
fn child_storage_size(
Expand Down
2 changes: 1 addition & 1 deletion core/rpc/src/state/state_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<B, E, Block: BlockT, RA> FullState<B, E, Block, RA>
for key in keys {
let mut last_block = None;
let mut last_value = last_values.get(key).cloned().unwrap_or_default();
let key_changes = self.client.key_changes(begin, end, key).map_err(client_err)?;
let key_changes = self.client.key_changes(begin, end, None, key).map_err(client_err)?;
for (block, _) in key_changes.into_iter().rev() {
if last_block == Some(block) {
continue;
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.