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

Commit

Permalink
Merge branch 'master' of github.com:paritytech/substrate into a-trace…
Browse files Browse the repository at this point in the history
…-storage
  • Loading branch information
arkpar committed Sep 16, 2019
2 parents c03db2d + 203e2c2 commit a4d63fd
Show file tree
Hide file tree
Showing 50 changed files with 657 additions and 634 deletions.
84 changes: 43 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/application-crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl RuntimePublic for Public {
}

fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature> {
runtime_io::ed25519_sign(key_type, self, msg)
runtime_io::ed25519_sign(key_type, self, msg.as_ref())
}

fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion core/application-crypto/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl RuntimePublic for Public {
}

fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature> {
runtime_io::sr25519_sign(key_type, self, msg)
runtime_io::sr25519_sign(key_type, self, msg.as_ref())
}

fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion core/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ mod tests {
let header = Header {
number,
parent_hash,
state_root: BlakeTwo256::trie_root::<_, &[u8], &[u8]>(Vec::new()),
state_root: BlakeTwo256::trie_root(Vec::new()),
digest,
extrinsics_root,
};
Expand Down
2 changes: 1 addition & 1 deletion core/client/src/block_builder/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where
debug_assert_eq!(
self.header.extrinsics_root().clone(),
HashFor::<Block>::ordered_trie_root(
self.extrinsics.iter().map(Encode::encode)
self.extrinsics.iter().map(Encode::encode).collect(),
),
);

Expand Down
46 changes: 29 additions & 17 deletions core/client/src/cht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
//! root has. A correct proof implies that the claimed block is identical to the one
//! we discarded.

use std::collections::HashSet;

use hash_db;
use codec::Encode;
use trie;
Expand Down Expand Up @@ -105,15 +103,10 @@ pub fn build_proof<Header, Hasher, BlocksI, HashesI>(
let mut storage = InMemoryState::<Hasher>::default().update(transaction);
let trie_storage = storage.as_trie_backend()
.expect("InMemoryState::as_trie_backend always returns Some; qed");
let mut total_proof = HashSet::new();
for block in blocks.into_iter() {
debug_assert_eq!(block_to_cht_number(cht_size, block), Some(cht_num));

let (value, proof) = prove_read_on_trie_backend(trie_storage, &encode_cht_key(block))?;
assert!(value.is_some(), "we have just built trie that includes the value for block");
total_proof.extend(proof);
}
Ok(total_proof.into_iter().collect())
prove_read_on_trie_backend(
trie_storage,
blocks.into_iter().map(|number| encode_cht_key(number)),
).map_err(ClientError::Execution)
}

/// Check CHT-based header proof.
Expand All @@ -128,9 +121,21 @@ pub fn check_proof<Header, Hasher>(
Hasher: hash_db::Hasher,
Hasher::Out: Ord,
{
do_check_proof::<Header, Hasher, _>(local_root, local_number, remote_hash, move |local_root, local_cht_key|
read_proof_check::<Hasher>(local_root, remote_proof,
local_cht_key).map_err(|e| ClientError::from(e)))
do_check_proof::<Header, Hasher, _>(
local_root,
local_number,
remote_hash,
move |local_root, local_cht_key|
read_proof_check::<Hasher, _>(
local_root,
remote_proof,
::std::iter::once(local_cht_key),
)
.map(|mut map| map
.remove(local_cht_key)
.expect("checked proof of local_cht_key; qed"))
.map_err(|e| ClientError::from(e)),
)
}

/// Check CHT-based header proof on pre-created proving backend.
Expand All @@ -145,9 +150,16 @@ pub fn check_proof_on_proving_backend<Header, Hasher>(
Hasher: hash_db::Hasher,
Hasher::Out: Ord,
{
do_check_proof::<Header, Hasher, _>(local_root, local_number, remote_hash, |_, local_cht_key|
read_proof_check_on_proving_backend::<Hasher>(
proving_backend, local_cht_key).map_err(|e| ClientError::from(e)))
do_check_proof::<Header, Hasher, _>(
local_root,
local_number,
remote_hash,
|_, local_cht_key|
read_proof_check_on_proving_backend::<Hasher>(
proving_backend,
local_cht_key,
).map_err(|e| ClientError::from(e)),
)
}

/// Check CHT-based header proof using passed checker function.
Expand Down
20 changes: 12 additions & 8 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,24 +436,28 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
}

/// Reads storage value at a given block + key, returning read proof.
pub fn read_proof(&self, id: &BlockId<Block>, key: &[u8]) -> error::Result<Vec<Vec<u8>>> {
pub fn read_proof<I>(&self, id: &BlockId<Block>, keys: I) -> error::Result<Vec<Vec<u8>>> where
I: IntoIterator,
I::Item: AsRef<[u8]>,
{
self.state_at(id)
.and_then(|state| prove_read(state, key)
.map(|(_, proof)| proof)
.and_then(|state| prove_read(state, keys)
.map_err(Into::into))
}

/// Reads child storage value at a given block + storage_key + key, returning
/// read proof.
pub fn read_child_proof(
pub fn read_child_proof<I>(
&self,
id: &BlockId<Block>,
storage_key: &[u8],
key: &[u8]
) -> error::Result<Vec<Vec<u8>>> {
keys: I,
) -> error::Result<Vec<Vec<u8>>> where
I: IntoIterator,
I::Item: AsRef<[u8]>,
{
self.state_at(id)
.and_then(|state| prove_child_read(state, storage_key, key)
.map(|(_, proof)| proof)
.and_then(|state| prove_child_read(state, storage_key, keys)
.map_err(Into::into))
}

Expand Down
2 changes: 1 addition & 1 deletion core/client/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn construct_genesis_block<
state_root: Block::Hash
) -> Block {
let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
std::iter::empty::<(&[u8], &[u8])>(),
Vec::new(),
);

Block::new(
Expand Down
Loading

0 comments on commit a4d63fd

Please sign in to comment.