Child storage tests and genesis fix. (#3185)

* Using child storage, (srml-support only), test failing .

* fix simple tests.

* Enumerable by requiring owned struct (previous form only allow
&'static).
Broken tests are from genesis init.

* implement for_child_keys_with_prefix

* indent

* clear_child_prefix fix.

* clear_child_prefix fix 2.

* fix for storage_impl, if/when allowing child and not child this could be
reverted.

* Fix lot of urlinked child genesis, still need to look upon actual
genesis srml module code.
Probably still a lot of broken code needing debugging.

* switch well_known_key to their associated module child trie.
Fix a genesis init (balance).
Complete some testing.
Comment some tests before using.

* fixing test runtime child keys

* latest commit fix broken genesis init

* fix system balances child name.

* Important fix: storage_root from test externalities need children (it is
already the case for ext).

* executive root with child calculation

* Avoid empty trie on test ext.

* Symetric removal of key for system.

* commenting changes related tests.

* Remove child module specifics.

* fix issues.

* fix some formatting

* fix bench and bump runtime

* Remove extend_storage_overlays, assimilate_storage do the same as is
proper considering srml macro.

* Fix warning for assimilate.

* Removing kill as they do not impact any test cases.

* Use tuple of storage map instead of two parameters. This changes the
behavior of decl_storage genesis build closure (breaking api).

* Do not use build storage before assimilate.

* fix error

* Update core/state-machine/src/backend.rs
This commit is contained in:
cheme
2019-08-08 15:05:25 +02:00
committed by Gavin Wood
parent 0067b2d9a2
commit b0e1212d48
59 changed files with 523 additions and 271 deletions
+37 -11
View File
@@ -20,7 +20,7 @@ use std::collections::HashMap;
use std::iter::FromIterator;
use crate::backend::{Backend, InMemory};
use hash_db::Hasher;
use trie::TrieConfiguration;
use trie::{TrieConfiguration, default_child_trie_root};
use trie::trie_types::Layout;
use primitives::offchain;
use primitives::storage::well_known_keys::is_child_storage_key;
@@ -35,13 +35,9 @@ pub struct BasicExternalities {
}
impl BasicExternalities {
/// Create a new instance of `BasicExternalities`
pub fn new(top: HashMap<Vec<u8>, Vec<u8>>) -> Self {
Self::new_with_children(top, Default::default())
}
/// Create a new instance of `BasicExternalities` with children
pub fn new_with_children(
/// Create a new instance of `BasicExternalities`
pub fn new(
top: HashMap<Vec<u8>, Vec<u8>>,
children: HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
) -> Self {
@@ -80,7 +76,7 @@ impl FromIterator<(Vec<u8>, Vec<u8>)> for BasicExternalities {
}
impl Default for BasicExternalities {
fn default() -> Self { Self::new(Default::default()) }
fn default() -> Self { Self::new(Default::default(), Default::default()) }
}
impl From<HashMap<Vec<u8>, Vec<u8>>> for BasicExternalities {
@@ -105,6 +101,10 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
self.children.get(storage_key.as_ref()).and_then(|child| child.get(key)).cloned()
}
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
Externalities::<H>::child_storage(self, storage_key, key)
}
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
@@ -147,9 +147,32 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
self.top.retain(|key, _| !key.starts_with(prefix));
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]) {
if let Some(child) = self.children.get_mut(storage_key.as_ref()) {
child.retain(|key, _| !key.starts_with(prefix));
}
}
fn chain_id(&self) -> u64 { 42 }
fn storage_root(&mut self) -> H::Out {
let mut top = self.top.clone();
let keys: Vec<_> = self.children.keys().map(|k| k.to_vec()).collect();
// Single child trie implementation currently allows using the same child
// empty root for all child trie. Using null storage key until multiple
// type of child trie support.
let empty_hash = default_child_trie_root::<Layout<H>>(&[]);
for storage_key in keys {
let child_root = self.child_storage_root(
ChildStorageKey::<H>::from_slice(storage_key.as_slice())
.expect("Map only feed by valid keys; qed")
);
if &empty_hash[..] == &child_root[..] {
top.remove(&storage_key);
} else {
top.insert(storage_key, child_root);
}
}
Layout::<H>::trie_root(self.top.clone())
}
@@ -159,7 +182,7 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
InMemory::<H>::default().child_storage_root(storage_key.as_ref(), delta).0
} else {
vec![]
default_child_trie_root::<Layout<H>>(storage_key.as_ref())
}
}
@@ -212,7 +235,7 @@ mod tests {
fn children_works() {
let child_storage = b":child_storage:default:test".to_vec();
let mut ext = BasicExternalities::new_with_children(
let mut ext = BasicExternalities::new(
Default::default(),
map![
child_storage.clone() => map![
@@ -240,7 +263,10 @@ mod tests {
#[test]
fn basic_externalities_is_empty() {
// Make sure no values are set by default in `BasicExternalities`.
let (storage, child_storage) = BasicExternalities::new(Default::default()).into_storages();
let (storage, child_storage) = BasicExternalities::new(
Default::default(),
Default::default(),
).into_storages();
assert!(storage.is_empty());
assert!(child_storage.is_empty());
}