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

avoid key collision on child trie and proof on child trie #2209

Closed
wants to merge 142 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
f6c4bb2
Straight forward move of trie related only change from child-trie-soft
cheme Mar 26, 2019
878a7ae
merge backend stuff from old branch, add insertion of child trie root in
cheme Mar 26, 2019
a464936
commit before wasm_executor change (we will keep old interface for now:
cheme Mar 26, 2019
4de73d7
tabify
cheme Mar 26, 2019
be0e340
Things compile (account change is unoptimized), not for wasm.
cheme Mar 26, 2019
7f3a282
Make SutrieField non public
cheme Mar 27, 2019
4d208db
Compile no std child trie with subtrie query (super costy: do we trust
cheme Mar 27, 2019
6d7f355
Make the single child trie test pass.
cheme Mar 27, 2019
25bcb4c
Proving synch child trie content (TODO it requires deletion tests).
cheme Mar 28, 2019
9eaef35
restore test
cheme Apr 1, 2019
37d9536
Merge branch 'master' into child-trie-soft-min
cheme Apr 1, 2019
67c03a2
Fix test error (wrong vec alloc).
cheme Apr 2, 2019
99dbb5a
tests for no key collision
cheme Apr 3, 2019
3be1802
Merge branch 'master' into child-trie-soft-min
cheme Apr 3, 2019
fec73d0
Revert `storage_root` using child, create a variant for it: see
cheme Apr 4, 2019
20d168d
indentation and remove comment
cheme Apr 4, 2019
d797bd0
Remove some TODOs, fix compile error
cheme Apr 4, 2019
27096aa
plaindb does not need to be keyspaceddb
cheme Apr 4, 2019
3560acc
Merge branch 'master' into child-trie-soft-min
cheme Apr 5, 2019
c3fc432
bump impl_version
cheme Apr 8, 2019
3e417eb
Merge branch 'master' into child-trie-soft-min
cheme Apr 16, 2019
5af490f
fix and 2299
cheme Apr 16, 2019
5a8dddd
Merge branch 'master' into child-trie-soft-min
cheme Apr 16, 2019
8f04f00
Merge branch 'master' into child-trie-soft-min
cheme Apr 26, 2019
8051179
fix keyspace insert and start proof (missing in higher level test case a
cheme Apr 29, 2019
8eac118
remove todo of test
cheme Apr 30, 2019
a65f9f2
Merge branch 'master' into child-trie-soft-min
cheme Apr 30, 2019
d495ef5
Merge branch 'master' into child-trie-soft-min &
cheme May 14, 2019
197d77a
- refactor api, use noderef of read only query (require and additional
cheme May 15, 2019
2ea3c89
Remove SubTrieNode struct
cheme May 16, 2019
99c45ea
put extension in encoding/decoding properly
cheme May 16, 2019
3553ab2
SubTrie struct renaming (and single encode point)
cheme May 16, 2019
47984fe
remove contract prefix, and make use of a child trie prefix.
cheme May 16, 2019
2bf2d7b
fix for overlay before reverting
cheme May 16, 2019
bc7165c
fix2 for prefix in overlay
cheme May 16, 2019
21c3acf
revert (will probably need two variant, but for rpc pr)
cheme May 16, 2019
98b2fc3
break some lines
cheme May 16, 2019
432cb10
Merge branch 'master' into child-trie-soft-min
cheme May 16, 2019
e266dfe
Add module specific seed to generator (accountid is undefined)
cheme May 16, 2019
7f64652
Update core/primitives/src/subtrie.rs
cheme May 17, 2019
999a26e
Merge branch 'master' into child-trie-soft-min and adapt rpc test
cheme May 21, 2019
7bbd681
Merge branch 'child-trie-soft-min' of github.com:cheme/substrate into…
cheme May 21, 2019
87f03b7
child trie additional prefix is bad design for primitive, removing it.
cheme May 21, 2019
f18e002
safer encoding of version.
cheme May 24, 2019
0eaeca0
Merge branch 'master' into child-trie-soft-min (untested)
cheme May 24, 2019
b57319d
missing import
cheme May 24, 2019
1f848d2
bump impl version
cheme May 24, 2019
403df51
Merge branch 'master' into child-trie-soft-min
cheme May 24, 2019
305b60a
Merge branch 'master' into child-trie-soft-min
cheme May 28, 2019
423cfb1
correct version
cheme May 28, 2019
a0ffa31
fix typing
cheme May 28, 2019
6e3bed7
Merge branch 'master' into child-trie-soft-min
cheme May 29, 2019
7411146
misnamed storage key variables.
cheme May 29, 2019
331be51
fix error messages.
cheme May 29, 2019
bc2935c
remove convenience constructor, remove unneeded pub on field.
cheme May 29, 2019
5a87b6a
Merge branch 'master' into child-trie-soft-min
cheme May 29, 2019
f40400b
minor tweaks before reworking docs
cheme May 30, 2019
9bc1ab7
First pass of documentation improvement.
cheme May 30, 2019
c073b21
do not refer to parent_key when it is ParentTrie:
cheme May 30, 2019
d15ca49
child trie renaming did exceed 100 character line.
cheme May 30, 2019
0736b96
Link github issue for TODOs
cheme May 31, 2019
9e0485d
Merge branch 'master' into child-trie-soft-min
cheme May 31, 2019
586b50e
comment incorrect variable
cheme May 31, 2019
65d7485
Merge branch 'master' into child-trie-soft-min
cheme Jun 6, 2019
95a69b2
Makes child trie instantiation a bisschen more explicit.
cheme Jun 6, 2019
d089693
Merge branch 'master' into child-trie-soft-min
cheme Jun 12, 2019
ec69ae0
rename fetch and init trie function.
cheme Jun 12, 2019
76ea14d
moved_child is a bad idea, just delete in top, if it is reinserted this
cheme Jun 14, 2019
b2050c8
moved map is correct (fix usage of child trie to return option<option<)
cheme Jun 14, 2019
5a0cbe1
Add missing wasm boilerplate.
cheme Jun 14, 2019
6e84810
remove moved_ct from overlay in favor to optional in pending.
cheme Jun 14, 2019
7f5694f
Merge branch 'master' into child-trie-soft-min
cheme Jun 14, 2019
5006d73
Use keyspace for storage_cache.
cheme Jun 17, 2019
b85508e
Comment of unsafety of `set_child` if directly exposed.
cheme Jun 17, 2019
0c14777
Merge branch 'master' into child-trie-soft-min
cheme Jun 17, 2019
10c4f58
use named fields.
cheme Jun 17, 2019
453927b
Merge branch 'master' into child-trie-soft-min
cheme Jun 21, 2019
349f9a5
Merge branch 'master' into child-trie-soft-min
cheme Jun 25, 2019
fc034fb
Refacto of keyspace to use static creation unique info.
cheme Jun 27, 2019
45cfbd6
put child trie content under their own prefix (with common root with
cheme Jun 28, 2019
4f9717e
Revert to storing keyspace in state approach.
cheme Jun 28, 2019
7cb2d84
invalid asumption for `child_delta_trie_root`.
cheme Jun 28, 2019
c821a08
clean unused child storage key relative methods.
cheme Jun 28, 2019
c3ba830
Merge branch 'master' into child-trie-soft-min and
cheme Jun 28, 2019
acf9641
invalid comment
cheme Jun 28, 2019
0050457
indent and remove deprecated comments/struct
cheme Jun 28, 2019
c36b91b
Make top trie write through keyspace db, this is not going fine because
cheme Jul 2, 2019
9d3d9e2
Fix issue: prefixed_key already got the keyspace in prefix.
cheme Jul 2, 2019
3db4da8
Merge branch 'master' into child-trie-soft-min
cheme Jul 2, 2019
4348d70
Address review comment:
cheme Jul 4, 2019
134a4bf
Merge branch 'master' into child-trie-soft-min
cheme Jul 4, 2019
5325621
Restore auth version.
cheme Jul 4, 2019
97118e8
Avoid confusing Option<Option< in result return of overlay.
cheme Jul 4, 2019
0ed7f80
Missing fix from review on testing ext for set child trie (code from
cheme Jul 4, 2019
8a6986a
Use fields for change trie storage.
cheme Jul 4, 2019
7652de9
switch MapTransaction to a struct.
cheme Jul 4, 2019
94c629e
Merge branch 'master' into child-trie-soft-min
cheme Jul 4, 2019
7675740
Apply same encoding for parent root (compatible with implementation of a
cheme Jul 1, 2019
90fba8a
Fix previous cherry pick
cheme Jul 4, 2019
d8c58c6
Addressing nits.
cheme Jul 4, 2019
661ba2e
Avoid unused creation of child trie.
cheme Jul 4, 2019
833e9ff
Create child trie id from additional storage state.
cheme Jul 5, 2019
71bda12
update default encoding.
cheme Jul 5, 2019
08b3062
Handle possible switch to multiple trie layer.
cheme Jul 5, 2019
89f3cd5
Remove a redundancy on keyspaceddb instantiation.
cheme Jul 5, 2019
834f52a
actually remove redundancy correctly
cheme Jul 5, 2019
0445228
comment
cheme Jul 5, 2019
75add99
Merge branch 'child-trie-soft-min-old' into child-trie-soft-min
cheme Jul 5, 2019
e70edab
Merge branch 'master' into child-trie-soft-min and apply review
cheme Jul 9, 2019
b8a0cd4
Merge branch 'master' into child-trie-soft-min
cheme Jul 10, 2019
1514171
Merge branch 'master' into child-trie-soft-min
cheme Jul 10, 2019
a7da811
Fix new test cases.
cheme Jul 10, 2019
b96c523
Merge branch 'master' into child-trie-soft-min
cheme Jul 11, 2019
865672d
Merge branch 'master' into child-trie-soft-min
cheme Jul 12, 2019
b6d7705
bump runtime version
cheme Jul 12, 2019
b45344c
Merge branch 'master' into child-trie-soft-min
cheme Jul 18, 2019
517f95c
Merge branch 'master' into child-trie-soft-min
cheme Jul 31, 2019
79a07de
Merge branch 'master' into child-trie-soft-min
cheme Aug 2, 2019
a027fb0
Some doc enhancement.
cheme Aug 2, 2019
b92655c
ChildRead correctly defined (previous definition was wrong).
cheme Aug 2, 2019
8a875f4
child_trie 'is_updatable_with' to avoid already broken redundancy.
cheme Aug 2, 2019
fcd8bdb
fix system bench error.
cheme Aug 2, 2019
3241fc4
Fix client doc rust code.
cheme Aug 2, 2019
ecac03b
Merge branch 'master' into child-trie-soft-min
cheme Aug 2, 2019
10369b9
Merge branch 'master' into child-trie-soft-min
cheme Aug 7, 2019
15a2967
Merge branch 'master' into child-trie-soft-min
cheme Aug 8, 2019
acc94e4
Merge branch 'master' into child-trie-soft-min
cheme Aug 8, 2019
7d96338
Remove unused method (in favor of assimilate).
cheme Aug 8, 2019
ba7bdcb
Merge branch 'child-trie-soft-min' of github.com:cheme/polkadot into …
cheme Aug 14, 2019
aacea85
Merge branch 'master' into child-trie-soft-min
cheme Aug 14, 2019
6789641
merge fix.
cheme Aug 14, 2019
2b37160
Rename MapTransaction to StorageContent.
cheme Aug 14, 2019
b1183bd
Get subscription stuff from child-deletion branch:
cheme Aug 14, 2019
bc5653c
Merge branch 'master' into child-trie-soft-min
cheme Aug 22, 2019
5ecfd19
update to master.
cheme Aug 22, 2019
baf89c8
Cast explicitelly for readability.
cheme Aug 26, 2019
0f8bff9
bump spec version.
cheme Aug 26, 2019
5a8576b
Merge branch 'master' into child-trie-soft-min
cheme Aug 29, 2019
045ee32
Merge branch 'master' into child-trie-soft-min
cheme Aug 29, 2019
58e6e41
Fix compilation.
cheme Aug 29, 2019
70555c5
Merge branch 'master' into child-trie-soft-min with conflicts.
cheme Sep 5, 2019
130e5e4
build passing, regression on extrinsics for a set_child (see new TODO).
cheme Sep 6, 2019
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
57 changes: 32 additions & 25 deletions core/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ use hash_db::{Hasher, Prefix};
use kvdb::{KeyValueDB, DBTransaction};
use trie::{MemoryDB, PrefixedMemoryDB, prefixed_key};
use parking_lot::{Mutex, RwLock};
use primitives::{H256, Blake2Hasher, ChangesTrieConfiguration, convert_hash};
use primitives::{H256, Blake2Hasher, ChangesTrieConfiguration, convert_hash,
child_trie::{ChildTrie, ChildTrieReadRef}};
use primitives::storage::well_known_keys;
use sr_primitives::{
generic::{BlockId, DigestItem}, Justification, StorageOverlay, ChildrenStorageOverlay,
BuildStorage
};
use sr_primitives::traits::{
Block as BlockT, Header as HeaderT, NumberFor, Zero, One, SaturatedConversion
Block as BlockT, Header as HeaderT, NumberFor, Zero, One, SaturatedConversion,
};
use state_machine::backend::Backend as StateBackend;
use executor::RuntimeInfo;
Expand Down Expand Up @@ -109,7 +110,7 @@ impl<B: BlockT> Drop for RefTrackingState<B> {
}

impl<B: BlockT> StateBackend<Blake2Hasher> for RefTrackingState<B> {
type Error = <DbState as StateBackend<Blake2Hasher>>::Error;
type Error = <DbState as StateBackend<Blake2Hasher>>::Error;
type Transaction = <DbState as StateBackend<Blake2Hasher>>::Transaction;
type TrieBackendStorage = <DbState as StateBackend<Blake2Hasher>>::TrieBackendStorage;

Expand All @@ -121,16 +122,20 @@ impl<B: BlockT> StateBackend<Blake2Hasher> for RefTrackingState<B> {
self.state.storage_hash(key)
}

fn child_storage(&self, storage_key: &[u8], key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
self.state.child_storage(storage_key, key)
fn child_trie(&self, storage_key: &[u8]) -> Result<Option<ChildTrie>, Self::Error> {
self.state.child_trie(storage_key)
}

fn child_storage(&self, child_trie: ChildTrieReadRef, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
self.state.child_storage(child_trie, key)
}

fn exists_storage(&self, key: &[u8]) -> Result<bool, Self::Error> {
self.state.exists_storage(key)
}

fn exists_child_storage(&self, storage_key: &[u8], key: &[u8]) -> Result<bool, Self::Error> {
self.state.exists_child_storage(storage_key, key)
fn exists_child_storage(&self, child_trie: ChildTrieReadRef, key: &[u8]) -> Result<bool, Self::Error> {
self.state.exists_child_storage(child_trie, key)
}

fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F) {
Expand All @@ -141,12 +146,17 @@ impl<B: BlockT> StateBackend<Blake2Hasher> for RefTrackingState<B> {
self.state.for_key_values_with_prefix(prefix, f)
}

fn for_keys_in_child_storage<F: FnMut(&[u8])>(&self, storage_key: &[u8], f: F) {
self.state.for_keys_in_child_storage(storage_key, f)
fn for_keys_in_child_storage<F: FnMut(&[u8])>(&self, child_trie: ChildTrieReadRef, f: F) {
self.state.for_keys_in_child_storage(child_trie, f)
}

fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
self.state.for_child_keys_with_prefix(storage_key, prefix, f)
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(
&self,
child_trie: ChildTrieReadRef,
prefix: &[u8],
f: F,
) {
self.state.for_child_keys_with_prefix(child_trie, prefix, f)
}

fn storage_root<I>(&self, delta: I) -> (H256, Self::Transaction)
Expand All @@ -156,11 +166,11 @@ impl<B: BlockT> StateBackend<Blake2Hasher> for RefTrackingState<B> {
self.state.storage_root(delta)
}

fn child_storage_root<I>(&self, storage_key: &[u8], delta: I) -> (Vec<u8>, bool, Self::Transaction)
fn child_storage_root<I>(&self, child_trie: &ChildTrie, delta: I) -> (Vec<u8>, bool, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
{
self.state.child_storage_root(storage_key, delta)
self.state.child_storage_root(child_trie, delta)
}

fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
Expand All @@ -171,8 +181,8 @@ impl<B: BlockT> StateBackend<Blake2Hasher> for RefTrackingState<B> {
self.state.keys(prefix)
}

fn child_keys(&self, child_key: &[u8], prefix: &[u8]) -> Vec<Vec<u8>> {
self.state.child_keys(child_key, prefix)
fn child_keys(&self, child_trie: ChildTrieReadRef, prefix: &[u8]) -> Vec<Vec<u8>> {
self.state.child_keys(child_trie, prefix)
}

fn as_trie_backend(&mut self) -> Option<&state_machine::TrieBackend<Self::TrieBackendStorage, Blake2Hasher>> {
Expand Down Expand Up @@ -469,15 +479,10 @@ where Block: BlockT<Hash=H256>,
return Err(client::error::Error::GenesisInvalid.into());
}

for child_key in children.keys() {
if !well_known_keys::is_child_storage_key(&child_key) {
return Err(client::error::Error::GenesisInvalid.into());
}
}

let child_delta = children.into_iter()
.map(|(storage_key, child_overlay)|
(storage_key, child_overlay.into_iter().map(|(k, v)| (k, Some(v)))));
.map(|(_storage_key, (child_overlay, subtrie))|
(subtrie, child_overlay.into_iter().map(|(k, v)| (k, Some(v)))));

let (root, transaction) = self.old_state.full_storage_root(
top.into_iter().map(|(k, v)| (k, Some(v))),
Expand Down Expand Up @@ -553,8 +558,10 @@ struct DbGenesisStorage(pub H256);
impl DbGenesisStorage {
pub fn new() -> Self {
let mut root = H256::default();
let mut mdb = MemoryDB::<Blake2Hasher>::default();
state_machine::TrieDBMut::<Blake2Hasher>::new(&mut mdb, &mut root);
{
let mut mdb = MemoryDB::<Blake2Hasher>::default();
state_machine::TrieDBMut::<Blake2Hasher>::new(&mut mdb, &mut root);
}
DbGenesisStorage(root)
}
}
Expand Down Expand Up @@ -1713,6 +1720,7 @@ mod tests {
).unwrap();

backend.commit_operation(op).unwrap();

assert_eq!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
Expand Down Expand Up @@ -1787,7 +1795,6 @@ mod tests {

backend.commit_operation(op).unwrap();


assert!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
Expand Down
36 changes: 22 additions & 14 deletions core/client/db/src/storage_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use hash_db::Hasher;
use sr_primitives::traits::{Block as BlockT, Header};
use state_machine::{backend::Backend as StateBackend, TrieBackend};
use log::trace;
use primitives::child_trie::ChildTrie;
use primitives::child_trie::ChildTrieReadRef;

use super::{StorageCollection, ChildStorageCollection};
use std::hash::Hash as StdHash;
const STATE_CACHE_BLOCKS: usize = 12;
Expand All @@ -39,6 +42,7 @@ pub struct Cache<B: BlockT, H: Hasher> {
/// Storage hashes cache. `None` indicates that key is known to be missing.
lru_hashes: LRUMap<StorageKey, OptionHOut<H::Out>>,
/// Storage cache for child trie. `None` indicates that key is known to be missing.
/// Key is a pair of child trie `KeySpace` and `Key` for value in the child trie.
lru_child_storage: LRUMap<ChildStorageKey, Option<StorageValue>>,
/// Information on the modifications in recently committed blocks; specifically which keys
/// changed in which block. Ordered by block number.
Expand Down Expand Up @@ -149,7 +153,7 @@ impl<B: BlockT, H: Hasher> Cache<B, H> {
pub fn used_storage_cache_size(&self) -> usize {
self.lru_storage.used_size()
+ self.lru_child_storage.used_size()
// ignore small hashes storage and self.lru_hashes.used_size()
// ignore small hashes storage and self.lru_hashes.used_size()
}

/// Synchronize the shared cache with the best block state.
Expand Down Expand Up @@ -504,8 +508,12 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
Ok(hash)
}

fn child_storage(&self, storage_key: &[u8], key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
let key = (storage_key.to_vec(), key.to_vec());
fn child_storage(
&self,
child_trie: ChildTrieReadRef,
key: &[u8],
) -> Result<Option<Vec<u8>>, Self::Error> {
let key = (child_trie.keyspace().to_vec(), key.to_vec());
let local_cache = self.cache.local_cache.upgradable_read();
if let Some(entry) = local_cache.child_storage.get(&key).cloned() {
trace!("Found in local cache: {:?}", key);
Expand All @@ -519,7 +527,7 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
}
}
trace!("Cache miss: {:?}", key);
let value = self.state.child_storage(storage_key, &key.1[..])?;
let value = self.state.child_storage(child_trie, &key.1[..])?;
RwLockUpgradableReadGuard::upgrade(local_cache).child_storage.insert(key, value.clone());
Ok(value)
}
Expand All @@ -528,8 +536,8 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
Ok(self.storage(key)?.is_some())
}

fn exists_child_storage(&self, storage_key: &[u8], key: &[u8]) -> Result<bool, Self::Error> {
self.state.exists_child_storage(storage_key, key)
fn exists_child_storage(&self, child_trie: ChildTrieReadRef, key: &[u8]) -> Result<bool, Self::Error> {
self.state.exists_child_storage(child_trie, key)
}

fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F) {
Expand All @@ -540,12 +548,12 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
self.state.for_key_values_with_prefix(prefix, f)
}

fn for_keys_in_child_storage<F: FnMut(&[u8])>(&self, storage_key: &[u8], f: F) {
self.state.for_keys_in_child_storage(storage_key, f)
fn for_keys_in_child_storage<F: FnMut(&[u8])>(&self, child_trie: ChildTrieReadRef, f: F) {
self.state.for_keys_in_child_storage(child_trie, f)
}

fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
self.state.for_child_keys_with_prefix(storage_key, prefix, f)
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, child_trie: ChildTrieReadRef, prefix: &[u8], f: F) {
self.state.for_child_keys_with_prefix(child_trie, prefix, f)
}

fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
Expand All @@ -556,12 +564,12 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
self.state.storage_root(delta)
}

fn child_storage_root<I>(&self, storage_key: &[u8], delta: I) -> (Vec<u8>, bool, Self::Transaction)
fn child_storage_root<I>(&self, child_trie: &ChildTrie, delta: I) -> (Vec<u8>, bool, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
H::Out: Ord
{
self.state.child_storage_root(storage_key, delta)
self.state.child_storage_root(child_trie, delta)
}

fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
Expand All @@ -572,8 +580,8 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
self.state.keys(prefix)
}

fn child_keys(&self, child_key: &[u8], prefix: &[u8]) -> Vec<Vec<u8>> {
self.state.child_keys(child_key, prefix)
fn child_keys(&self, child_trie: ChildTrieReadRef, prefix: &[u8]) -> Vec<Vec<u8>> {
self.state.child_keys(child_trie, prefix)
}

fn as_trie_backend(&mut self) -> Option<&TrieBackend<Self::TrieBackendStorage, H>> {
Expand Down
45 changes: 30 additions & 15 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use primitives::{
NeverNativeValue, ExecutionContext, NativeOrEncoded,
storage::{StorageKey, StorageData, well_known_keys},
offchain,
child_trie::{ChildTrie, ChildTrieReadRef},
storage::StorageKeySpace,
};
use substrate_telemetry::{telemetry, SUBSTRATE_INFO};
use sr_primitives::{
Expand Down Expand Up @@ -145,7 +147,7 @@ pub trait BlockchainEvents<Block: BlockT> {
fn storage_changes_notification_stream(
&self,
filter_keys: Option<&[StorageKey]>,
child_filter_keys: Option<&[(StorageKey, Option<Vec<StorageKey>>)]>,
child_filter_keys: Option<&[(StorageKeySpace, Option<Vec<StorageKey>>)]>,
) -> error::Result<StorageEventStream<Block::Hash>>;
}

Expand Down Expand Up @@ -308,10 +310,10 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
execution_strategies: ExecutionStrategies
) -> error::Result<Self> {
if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() {
let (genesis_storage, children_genesis_storage) = build_genesis_storage.build_storage()?;
let genesis_storage = build_genesis_storage.build_storage()?;
let mut op = backend.begin_operation()?;
backend.begin_state_operation(&mut op, BlockId::Hash(Default::default()))?;
let state_root = op.reset_storage(genesis_storage, children_genesis_storage)?;
let state_root = op.reset_storage(genesis_storage.top, genesis_storage.children)?;
let genesis_block = genesis::construct_genesis_block::<Block>(state_root.into());
info!("Initializing Genesis block/state (state: {}, header-hash: {})",
genesis_block.header().state_root(),
Expand Down Expand Up @@ -371,6 +373,18 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
)
}

/// Given a `BlockId` and a child storage key,
/// return the matching child storage keys.
pub fn child_trie(
&self,
id: &BlockId<Block>,
child_key: &StorageKey
) -> error::Result<Option<ChildTrie>> {
self.state_at(id)?
.child_trie(&child_key.0[..])
.map_err(|e| error::Error::from_state(Box::new(e)))
}

/// Given a `BlockId` and a key, return the value under the hash in that block.
pub fn storage_hash(&self, id: &BlockId<Block>, key: &StorageKey)
-> error::Result<Option<Block::Hash>> {
Expand All @@ -383,38 +397,39 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
pub fn child_storage_keys(
&self,
id: &BlockId<Block>,
child_storage_key: &StorageKey,
child_trie: ChildTrieReadRef,
key_prefix: &StorageKey
) -> error::Result<Vec<StorageKey>> {
let keys = self.state_at(id)?
.child_keys(&child_storage_key.0, &key_prefix.0)
.child_keys(child_trie, &key_prefix.0)
.into_iter()
.map(StorageKey)
.collect();
Ok(keys)
}

/// Given a `BlockId`, a key and a child storage key, return the value under the key in that block.
/// Given a `BlockId`, a key and a child trie reference,
/// return the value under the key in that block.
pub fn child_storage(
&self,
id: &BlockId<Block>,
child_storage_key: &StorageKey,
child_trie: ChildTrieReadRef,
key: &StorageKey
) -> error::Result<Option<StorageData>> {
Ok(self.state_at(id)?
.child_storage(&child_storage_key.0, &key.0).map_err(|e| error::Error::from_state(Box::new(e)))?
.child_storage(child_trie, &key.0).map_err(|e| error::Error::from_state(Box::new(e)))?
.map(StorageData))
}

/// Given a `BlockId`, a key and a child storage key, return the hash under the key in that block.
pub fn child_storage_hash(
&self,
id: &BlockId<Block>,
child_storage_key: &StorageKey,
child_trie: ChildTrieReadRef,
key: &StorageKey
) -> error::Result<Option<Block::Hash>> {
Ok(self.state_at(id)?
.child_storage_hash(&child_storage_key.0, &key.0).map_err(|e| error::Error::from_state(Box::new(e)))?
.child_storage_hash(child_trie, &key.0).map_err(|e| error::Error::from_state(Box::new(e)))?
)
}

Expand Down Expand Up @@ -443,16 +458,16 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
.map_err(Into::into))
}

/// Reads child storage value at a given block + storage_key + key, returning
/// Reads child storage value at a given block + child trie reference + key, returning
/// read proof.
pub fn read_child_proof(
&self,
id: &BlockId<Block>,
storage_key: &[u8],
child_trie: ChildTrieReadRef,
key: &[u8]
) -> error::Result<Vec<Vec<u8>>> {
self.state_at(id)
.and_then(|state| prove_child_read(state, storage_key, key)
.and_then(|state| prove_child_read(state, child_trie, key)
.map(|(_, proof)| proof)
.map_err(Into::into))
}
Expand Down Expand Up @@ -1058,7 +1073,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
overlay.commit_prospective();

let (top, children) = overlay.into_committed();
let children = children.map(|(sk, it)| (sk, it.collect())).collect();
let children = children.map(|(ks, it)| (ks, it.collect())).collect();
if import_headers.post().state_root() != &storage_update.1 {
return Err(error::Error::InvalidStateRoot);
}
Expand Down Expand Up @@ -1607,7 +1622,7 @@ where
fn storage_changes_notification_stream(
&self,
filter_keys: Option<&[StorageKey]>,
child_filter_keys: Option<&[(StorageKey, Option<Vec<StorageKey>>)]>,
child_filter_keys: Option<&[(StorageKeySpace, Option<Vec<StorageKey>>)]>,
) -> error::Result<StorageEventStream<Block::Hash>> {
Ok(self.storage_notifications.lock().listen(filter_keys, child_filter_keys))
}
Expand Down
Loading