Fix key collision for child trie (#4162)

* In progress, runtime io must switch to future proof root +
child_specific (unique id) + u32 type.

* Switch interface, sr-io seems ok, rpc could use similar interface to
sr-io, genesis json broken if there is child trie in existing encoding
genesis.

* test from previous implementation.

* fix proving test.

* Restore Keyspacedb from other branch, only apply to child trie.

* Removing unneeded child_info from child root (child info are stored
if things changed, otherwhise the root does not change).

* Switch rpc to use same format as ext: more future proof.

* use root from child info for trie backend essence.

* Breaking long lines.

* Update doc and clean pr a bit.

* fix error type

* Restore removed doc on merge and update sr-io doc.

* Switch child storage api to use directly unique id, if managed id
where to be put in place, the api will change at this time.

* Clean deprecated host interface from child.

* Removing assertion on child info (can fail depending on root
memoization).

* merging child info in the overlay when possible.

* child iteration by prefix using child_info.

* Using ChainInfo in frame support. ChainInfo gets redesign to avoid
buffers allocation on every calls.

* Add length of root to the data of child info.

* comments

* Encode compact.

* Remove child info with root.

* Fix try_update condition.

* Comment Ext child root caching.

* Replace tuples by struct with field

* remove StorageTuple alias.

* Fix doc tests, and remove StorageOverlay and ChildStorageOverlay
aliases.
This commit is contained in:
cheme
2019-12-14 03:11:19 +01:00
committed by Gavin Wood
parent 7121837f84
commit 0ece5d9e17
53 changed files with 2121 additions and 918 deletions
+18 -10
View File
@@ -32,13 +32,13 @@ pub use keyring::{
sr25519::Keyring as Sr25519Keyring,
};
pub use primitives::{Blake2Hasher, traits::BareCryptoStorePtr};
pub use sp_runtime::{StorageOverlay, ChildrenStorageOverlay};
pub use sp_runtime::{Storage, StorageChild};
pub use state_machine::ExecutionStrategy;
use std::sync::Arc;
use std::collections::HashMap;
use hash_db::Hasher;
use primitives::storage::well_known_keys;
use primitives::storage::{well_known_keys, ChildInfo};
use sp_runtime::traits::Block as BlockT;
use client::LocalCallExecutor;
@@ -51,11 +51,11 @@ pub type LightBackend<Block> = client::light::backend::Backend<
/// A genesis storage initialisation trait.
pub trait GenesisInit: Default {
/// Construct genesis storage.
fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay);
fn genesis_storage(&self) -> Storage;
}
impl GenesisInit for () {
fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay) {
fn genesis_storage(&self) -> Storage {
Default::default()
}
}
@@ -64,7 +64,7 @@ impl GenesisInit for () {
pub struct TestClientBuilder<Executor, Backend, G: GenesisInit> {
execution_strategies: ExecutionStrategies,
genesis_init: G,
child_storage_extension: HashMap<Vec<u8>, Vec<(Vec<u8>, Vec<u8>)>>,
child_storage_extension: HashMap<Vec<u8>, StorageChild>,
backend: Arc<Backend>,
_executor: std::marker::PhantomData<Executor>,
keystore: Option<BareCryptoStorePtr>,
@@ -136,10 +136,15 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
mut self,
key: impl AsRef<[u8]>,
child_key: impl AsRef<[u8]>,
child_info: ChildInfo,
value: impl AsRef<[u8]>,
) -> Self {
let entry = self.child_storage_extension.entry(key.as_ref().to_vec()).or_insert_with(Vec::new);
entry.push((child_key.as_ref().to_vec(), value.as_ref().to_vec()));
let entry = self.child_storage_extension.entry(key.as_ref().to_vec())
.or_insert_with(|| StorageChild {
data: Default::default(),
child_info: child_info.to_owned(),
});
entry.data.insert(child_key.as_ref().to_vec(), value.as_ref().to_vec());
self
}
@@ -180,10 +185,13 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
let mut storage = self.genesis_init.genesis_storage();
// Add some child storage keys.
for (key, value) in self.child_storage_extension {
storage.1.insert(
for (key, child_content) in self.child_storage_extension {
storage.children.insert(
well_known_keys::CHILD_STORAGE_KEY_PREFIX.iter().cloned().chain(key).collect(),
value.into_iter().collect(),
StorageChild {
data: child_content.data.into_iter().collect(),
child_info: child_content.child_info,
},
);
}