Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Make genesis state locally-available on light client (#1622)
Browse files Browse the repository at this point in the history
* make genesis state available on light client

* RemoteOrLocalCallExecutor

* code_is_executed_locally_or_remotely

* OnDemandOrGenesisState tests

* some comments
  • Loading branch information
svyatonik authored and gavofyork committed Feb 1, 2019
1 parent e6d9814 commit d680881
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 79 deletions.
5 changes: 4 additions & 1 deletion core/client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@ pub trait RemoteBackend<Block, H>: Backend<Block, H>
where
Block: BlockT,
H: Hasher<Out=Block::Hash>,
{}
{
/// Returns true if the state for given block is available locally.
fn is_local_state_available(&self, block: &BlockId<Block>) -> bool;
}
2 changes: 1 addition & 1 deletion core/client/src/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<B, E> Clone for LocalCallExecutor<B, E> where E: Clone {

impl<B, E, Block> CallExecutor<Block, Blake2Hasher> for LocalCallExecutor<B, E>
where
B: backend::LocalBackend<Block, Blake2Hasher>,
B: backend::Backend<Block, Blake2Hasher>,
E: CodeExecutor<Blake2Hasher> + RuntimeInfo,
Block: BlockT<Hash=H256>,
{
Expand Down
34 changes: 27 additions & 7 deletions core/client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,11 @@ where
}

fn reset_storage(&mut self, mut top: StorageMap, children: ChildrenStorageMap) -> error::Result<H::Out> {
if top.iter().any(|(k, _)| well_known_keys::is_child_storage_key(k)) {
return Err(error::ErrorKind::GenesisInvalid.into());
}
check_genesis_storage(&top, &children)?;

let mut transaction: Vec<(Option<Vec<u8>>, Vec<u8>, Option<Vec<u8>>)> = Default::default();

for (child_key, child_map) in children {
if !well_known_keys::is_child_storage_key(&child_key) {
return Err(error::ErrorKind::GenesisInvalid.into());
}

let (root, is_default, update) = self.old_state.child_storage_root(&child_key, child_map.into_iter().map(|(k, v)| (k, Some(v))));
transaction.consolidate(update);

Expand Down Expand Up @@ -662,6 +656,19 @@ where
H::Out: HeapSizeOf + Ord,
{}

impl<Block, H> backend::RemoteBackend<Block, H> for Backend<Block, H>
where
Block: BlockT,
H: Hasher<Out=Block::Hash>,
H::Out: HeapSizeOf + Ord,
{
fn is_local_state_available(&self, block: &BlockId<Block>) -> bool {
self.blockchain.expect_block_number_from_id(block)
.map(|num| num.is_zero())
.unwrap_or(false)
}
}

impl<Block: BlockT> Cache<Block> {
fn insert(&self, at: Block::Hash, authorities: Option<Vec<AuthorityIdFor<Block>>>) {
self.authorities_at.write().insert(at, authorities);
Expand Down Expand Up @@ -708,6 +715,19 @@ pub fn cache_authorities_at<Block: BlockT>(
blockchain.cache.insert(at, authorities);
}

/// Check that genesis storage is valid.
pub fn check_genesis_storage(top: &StorageMap, children: &ChildrenStorageMap) -> error::Result<()> {
if top.iter().any(|(k, _)| well_known_keys::is_child_storage_key(k)) {
return Err(error::ErrorKind::GenesisInvalid.into());
}

if children.keys().any(|child_key| !well_known_keys::is_child_storage_key(&child_key)) {
return Err(error::ErrorKind::GenesisInvalid.into());
}

Ok(())
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
Expand Down
Loading

0 comments on commit d680881

Please sign in to comment.