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
+4
View File
@@ -144,6 +144,10 @@ impl<B: BlockT> StateBackend<Blake2Hasher> for RefTrackingState<B> {
self.state.for_keys_in_child_storage(storage_key, 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 storage_root<I>(&self, delta: I) -> (H256, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
@@ -540,6 +540,10 @@ impl<H: Hasher, S: StateBackend<H>, B: BlockT> StateBackend<H> for CachingState<
self.state.for_keys_in_child_storage(storage_key, 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 storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
+4 -13
View File
@@ -45,7 +45,7 @@ mod tests {
use state_machine::{self, OverlayedChanges, ExecutionStrategy, InMemoryChangesTrieStorage};
use state_machine::backend::InMemory;
use test_client::{
runtime::genesismap::{GenesisConfig, additional_storage_with_genesis},
runtime::genesismap::{GenesisConfig, insert_genesis_block},
runtime::{Hash, Transfer, Block, BlockNumber, Header, Digest},
AccountKeyring, Sr25519Keyring,
};
@@ -156,10 +156,7 @@ mod tests {
1000,
None,
).genesis_map();
let state_root = BlakeTwo256::trie_root(storage.clone().into_iter());
let block = construct_genesis_block::<Block>(state_root);
let genesis_hash = block.header.hash();
storage.extend(additional_storage_with_genesis(&block).into_iter());
let genesis_hash = insert_genesis_block(&mut storage);
let backend = InMemory::from(storage);
let (b1data, _b1hash) = block1(genesis_hash, &backend);
@@ -187,10 +184,7 @@ mod tests {
1000,
None,
).genesis_map();
let state_root = BlakeTwo256::trie_root(storage.clone().into_iter());
let block = construct_genesis_block::<Block>(state_root);
let genesis_hash = block.header.hash();
storage.extend(additional_storage_with_genesis(&block).into_iter());
let genesis_hash = insert_genesis_block(&mut storage);
let backend = InMemory::from(storage);
let (b1data, _b1hash) = block1(genesis_hash, &backend);
@@ -218,10 +212,7 @@ mod tests {
68,
None,
).genesis_map();
let state_root = BlakeTwo256::trie_root(storage.clone().into_iter());
let block = construct_genesis_block::<Block>(state_root);
let genesis_hash = block.header.hash();
storage.extend(additional_storage_with_genesis(&block).into_iter());
let genesis_hash = insert_genesis_block(&mut storage);
let backend = InMemory::from(storage);
let (b1data, _b1hash) = block1(genesis_hash, &backend);
@@ -381,6 +381,15 @@ where
// whole state is not available on light node
}
fn for_child_keys_with_prefix<A: FnMut(&[u8])>(
&self,
_storage_key: &[u8],
_prefix: &[u8],
_action: A,
) {
// whole state is not available on light node
}
fn storage_root<I>(&self, _delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
@@ -456,6 +465,20 @@ where
}
}
fn for_child_keys_with_prefix<A: FnMut(&[u8])>(
&self,
storage_key: &[u8],
prefix: &[u8],
action: A,
) {
match *self {
OnDemandOrGenesisState::OnDemand(ref state) =>
StateBackend::<H>::for_child_keys_with_prefix(state, storage_key, prefix, action),
OnDemandOrGenesisState::Genesis(ref state) =>
state.for_child_keys_with_prefix(storage_key, prefix, action),
}
}
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
+21 -4
View File
@@ -281,6 +281,23 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
this.ext.clear_prefix(&prefix);
Ok(())
},
ext_clear_child_prefix(
storage_key_data: *const u8,
storage_key_len: u32,
prefix_data: *const u8,
prefix_len: u32
) => {
let storage_key = this.memory.get(
storage_key_data,
storage_key_len as usize
).map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_prefix")?;
let storage_key = ChildStorageKey::from_vec(storage_key)
.ok_or_else(|| "ext_clear_child_prefix: child storage key is not valid")?;
let prefix = this.memory.get(prefix_data, prefix_len as usize)
.map_err(|_| "Invalid attempt to determine prefix in ext_clear_child_prefix")?;
this.ext.clear_child_prefix(storage_key, &prefix);
Ok(())
},
ext_kill_child_storage(storage_key_data: *const u8, storage_key_len: u32) => {
let storage_key = this.memory.get(
storage_key_data,
@@ -1496,11 +1513,11 @@ mod tests {
assert_eq!(output, b"all ok!".to_vec());
let expected = TestExternalities::new(map![
let expected = TestExternalities::new((map![
b"input".to_vec() => b"Hello world".to_vec(),
b"foo".to_vec() => b"bar".to_vec(),
b"baz".to_vec() => b"bar".to_vec()
]);
], map![]));
assert_eq!(ext, expected);
}
@@ -1519,11 +1536,11 @@ mod tests {
assert_eq!(output, b"all ok!".to_vec());
let expected: TestExternalities<_> = map![
let expected = TestExternalities::new((map![
b"aaa".to_vec() => b"1".to_vec(),
b"aab".to_vec() => b"2".to_vec(),
b"bbb".to_vec() => b"5".to_vec()
];
], map![]));
assert_eq!(expected, ext);
}
+23 -5
View File
@@ -72,10 +72,16 @@ impl<'a, G: RuntimeGenesis> BuildStorage for &'a ChainSpec<G> {
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
match self.genesis.resolve()? {
Genesis::Runtime(gc) => gc.build_storage(),
Genesis::Raw(map) => Ok((map.into_iter().map(|(k, v)| (k.0, v.0)).collect(), Default::default())),
Genesis::Raw(map, children_map) => Ok((
map.into_iter().map(|(k, v)| (k.0, v.0)).collect(),
children_map.into_iter().map(|(sk, map)| (
sk.0,
map.into_iter().map(|(k, v)| (k.0, v.0)).collect(),
)).collect(),
)),
}
}
fn assimilate_storage(self, _: &mut StorageOverlay, _: &mut ChildrenStorageOverlay) -> Result<(), String> {
fn assimilate_storage(self, _: &mut (StorageOverlay, ChildrenStorageOverlay)) -> Result<(), String> {
Err("`assimilate_storage` not implemented for `ChainSpec`.".into())
}
}
@@ -85,7 +91,10 @@ impl<'a, G: RuntimeGenesis> BuildStorage for &'a ChainSpec<G> {
#[serde(deny_unknown_fields)]
enum Genesis<G> {
Runtime(G),
Raw(HashMap<StorageKey, StorageData>),
Raw(
HashMap<StorageKey, StorageData>,
HashMap<StorageKey, HashMap<StorageKey, StorageData>>,
),
}
#[derive(Serialize, Deserialize, Clone)]
@@ -218,11 +227,20 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
};
let genesis = match (raw, self.genesis.resolve()?) {
(true, Genesis::Runtime(g)) => {
let storage = g.build_storage()?.0.into_iter()
let storage = g.build_storage()?;
let top = storage.0.into_iter()
.map(|(k, v)| (StorageKey(k), StorageData(v)))
.collect();
let children = storage.1.into_iter()
.map(|(sk, child)| (
StorageKey(sk),
child.into_iter()
.map(|(k, v)| (StorageKey(k), StorageData(v)))
.collect(),
))
.collect();
Genesis::Raw(storage)
Genesis::Raw(top, children)
},
(_, genesis) => genesis,
};
+4 -1
View File
@@ -141,6 +141,9 @@ export_api! {
/// Clear the storage entries with a key that starts with the given prefix.
fn clear_prefix(prefix: &[u8]);
/// Clear the child storage entries with a key that starts with the given prefix.
fn clear_child_prefix(storage_key: &[u8], prefix: &[u8]);
/// "Commit" all existing operations and compute the resultant storage root.
fn storage_root() -> [u8; 32];
@@ -401,7 +404,7 @@ mod imp {
#[cfg(feature = "std")]
pub use self::imp::{
StorageOverlay, ChildrenStorageOverlay, with_storage, with_storage_and_children,
StorageOverlay, ChildrenStorageOverlay, with_storage,
with_externalities
};
#[cfg(not(feature = "std"))]
+14 -23
View File
@@ -141,6 +141,13 @@ impl StorageApi for () {
);
}
fn clear_child_prefix(storage_key: &[u8], prefix: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.clear_child_prefix(storage_key, prefix)
});
}
fn storage_root() -> [u8; 32] {
ext::with(|ext|
ext.storage_root()
@@ -455,36 +462,20 @@ pub type StorageOverlay = HashMap<Vec<u8>, Vec<u8>>;
/// A set of key value pairs for children storage;
pub type ChildrenStorageOverlay = HashMap<Vec<u8>, StorageOverlay>;
/// Execute the given closure with global functions available whose functionality routes into
/// externalities that draw from and populate `storage`. Forwards the value that the closure returns.
pub fn with_storage<R, F: FnOnce() -> R>(storage: &mut StorageOverlay, f: F) -> R {
let mut alt_storage = Default::default();
rstd::mem::swap(&mut alt_storage, storage);
let mut ext = BasicExternalities::new(alt_storage);
let r = ext::using(&mut ext, f);
*storage = ext.into_storages().0;
r
}
/// Execute the given closure with global functions available whose functionality routes into
/// externalities that draw from and populate `storage` and `children_storage`.
/// Forwards the value that the closure returns.
pub fn with_storage_and_children<R, F: FnOnce() -> R>(
storage: &mut StorageOverlay,
children_storage: &mut ChildrenStorageOverlay,
pub fn with_storage<R, F: FnOnce() -> R>(
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
f: F
) -> R {
let mut alt_storage = Default::default();
let mut alt_children_storage = Default::default();
rstd::mem::swap(&mut alt_storage, storage);
rstd::mem::swap(&mut alt_children_storage, children_storage);
let mut ext = BasicExternalities::new_with_children(alt_storage, alt_children_storage);
let mut ext = BasicExternalities::new(alt_storage.0, alt_storage.1);
let r = ext::using(&mut ext, f);
let storage_tuple = ext.into_storages();
*storage = storage_tuple.0;
*children_storage = storage_tuple.1;
*storage = ext.into_storages();
r
}
@@ -524,7 +515,7 @@ mod std_tests {
true
}));
t = BasicExternalities::new(map![b"foo".to_vec() => b"bar".to_vec()]);
t = BasicExternalities::new(map![b"foo".to_vec() => b"bar".to_vec()], map![]);
assert!(!with_externalities(&mut t, || {
assert_eq!(storage(b"hello"), None);
@@ -537,7 +528,7 @@ mod std_tests {
fn read_storage_works() {
let mut t = BasicExternalities::new(map![
b":test".to_vec() => b"\x0b\0\0\0Hello world".to_vec()
]);
], map![]);
with_externalities(&mut t, || {
let mut v = [0u8; 4];
@@ -556,7 +547,7 @@ mod std_tests {
b":abcd".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
b":abc".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
b":abdd".to_vec() => b"\x0b\0\0\0Hello world".to_vec()
]);
], map![]);
with_externalities(&mut t, || {
clear_prefix(b":abc");
+16
View File
@@ -213,6 +213,13 @@ pub mod ext {
fn ext_exists_storage(key_data: *const u8, key_len: u32) -> u32;
/// Remove storage entries which key starts with given prefix.
fn ext_clear_prefix(prefix_data: *const u8, prefix_len: u32);
/// Remove child storage entries which key starts with given prefix.
fn ext_clear_child_prefix(
storage_key_data: *const u8,
storage_key_len: u32,
prefix_data: *const u8,
prefix_len: u32
);
/// Gets the value of the given key from storage.
///
/// The host allocates the memory for storing the value.
@@ -703,6 +710,15 @@ impl StorageApi for () {
}
}
fn clear_child_prefix(storage_key: &[u8], prefix: &[u8]) {
unsafe {
ext_clear_child_prefix.get()(
storage_key.as_ptr(), storage_key.len() as u32,
prefix.as_ptr(), prefix.len() as u32
);
}
}
fn kill_child_storage(storage_key: &[u8]) {
unsafe {
ext_kill_child_storage.get()(
+20 -39
View File
@@ -113,16 +113,14 @@ pub use serde::{Serialize, Deserialize, de::DeserializeOwned};
pub trait BuildStorage: Sized {
/// Build the storage out of this builder.
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
let mut storage = Default::default();
let mut child_storage = Default::default();
self.assimilate_storage(&mut storage, &mut child_storage)?;
Ok((storage, child_storage))
let mut storage = (Default::default(), Default::default());
self.assimilate_storage(&mut storage)?;
Ok(storage)
}
/// Assimilate the storage for this module into pre-existing overlays.
fn assimilate_storage(
self,
storage: &mut StorageOverlay,
child_storage: &mut ChildrenStorageOverlay
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
) -> Result<(), String>;
}
@@ -132,26 +130,10 @@ pub trait BuildModuleGenesisStorage<T, I>: Sized {
/// Create the module genesis storage into the given `storage` and `child_storage`.
fn build_module_genesis_storage(
self,
storage: &mut StorageOverlay,
child_storage: &mut ChildrenStorageOverlay
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
) -> Result<(), String>;
}
#[cfg(feature = "std")]
impl BuildStorage for StorageOverlay {
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
Ok((self, Default::default()))
}
fn assimilate_storage(
self,
storage: &mut StorageOverlay,
_child_storage: &mut ChildrenStorageOverlay
) -> Result<(), String> {
storage.extend(self);
Ok(())
}
}
#[cfg(feature = "std")]
impl BuildStorage for (StorageOverlay, ChildrenStorageOverlay) {
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
@@ -159,11 +141,16 @@ impl BuildStorage for (StorageOverlay, ChildrenStorageOverlay) {
}
fn assimilate_storage(
self,
storage: &mut StorageOverlay,
child_storage: &mut ChildrenStorageOverlay
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
)-> Result<(), String> {
storage.extend(self.0);
child_storage.extend(self.1);
storage.0.extend(self.0);
for (k, other_map) in self.1.into_iter() {
if let Some(map) = storage.1.get_mut(&k) {
map.extend(other_map);
} else {
storage.1.insert(k, other_map);
}
}
Ok(())
}
}
@@ -773,8 +760,7 @@ macro_rules! impl_outer_config {
impl $crate::BuildStorage for $main {
fn assimilate_storage(
self,
top: &mut $crate::StorageOverlay,
children: &mut $crate::ChildrenStorageOverlay
storage: &mut ($crate::StorageOverlay, $crate::ChildrenStorageOverlay),
) -> std::result::Result<(), String> {
$(
if let Some(extra) = self.[< $snake $(_ $instance )? >] {
@@ -784,8 +770,7 @@ macro_rules! impl_outer_config {
$snake;
$( $instance )?;
extra;
top;
children;
storage;
}
}
)*
@@ -799,13 +784,11 @@ macro_rules! impl_outer_config {
$module:ident;
$instance:ident;
$extra:ident;
$top:ident;
$children:ident;
$storage:ident;
) => {
$crate::BuildModuleGenesisStorage::<$runtime, $module::$instance>::build_module_genesis_storage(
$extra,
$top,
$children,
$storage,
)?;
};
(@CALL_FN
@@ -813,13 +796,11 @@ macro_rules! impl_outer_config {
$module:ident;
;
$extra:ident;
$top:ident;
$children:ident;
$storage:ident;
) => {
$crate::BuildModuleGenesisStorage::<$runtime, $module::__InherentHiddenInstance>::build_module_genesis_storage(
$extra,
$top,
$children,
$storage,
)?;
}
}
+30 -6
View File
@@ -70,10 +70,14 @@ pub trait Backend<H: Hasher> {
/// Retrieve all entries keys of child storage and call `f` for each of those keys.
fn for_keys_in_child_storage<F: FnMut(&[u8])>(&self, storage_key: &[u8], f: F);
/// Retrieve all entries keys of which start with the given prefix and
/// Retrieve all entries keys which start with the given prefix and
/// call `f` for each of those keys.
fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F);
/// Retrieve all child entries keys which start with the given prefix and
/// call `f` for each of those keys.
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F);
/// Calculate the storage root, with given delta over what is already stored in
/// the backend, and produce a "transaction" that can be used to commit.
/// Does not include child storage updates.
@@ -103,11 +107,7 @@ pub trait Backend<H: Hasher> {
/// Get all keys of child storage with given prefix
fn child_keys(&self, child_storage_key: &[u8], prefix: &[u8]) -> Vec<Vec<u8>> {
let mut all = Vec::new();
self.for_keys_in_child_storage(child_storage_key, |k| {
if k.starts_with(prefix) {
all.push(k.to_vec());
}
});
self.for_child_keys_with_prefix(child_storage_key, prefix, |k| all.push(k.to_vec()));
all
}
@@ -248,6 +248,25 @@ impl<H: Hasher> From<HashMap<Option<Vec<u8>>, HashMap<Vec<u8>, Vec<u8>>>> for In
}
}
impl<H: Hasher> From<(
HashMap<Vec<u8>, Vec<u8>>,
HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
)> for InMemory<H> {
fn from(inners: (
HashMap<Vec<u8>, Vec<u8>>,
HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
)) -> Self {
let mut inner: HashMap<Option<Vec<u8>>, HashMap<Vec<u8>, Vec<u8>>>
= inners.1.into_iter().map(|(k, v)| (Some(k), v)).collect();
inner.insert(None, inners.0);
InMemory {
inner: inner,
trie: None,
_hasher: PhantomData,
}
}
}
impl<H: Hasher> From<HashMap<Vec<u8>, Vec<u8>>> for InMemory<H> {
fn from(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
let mut expanded = HashMap::new();
@@ -306,6 +325,11 @@ impl<H: Hasher> Backend<H> for InMemory<H> {
self.inner.get(&Some(storage_key.to_vec())).map(|map| map.keys().for_each(|k| f(&k)));
}
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
self.inner.get(&Some(storage_key.to_vec()))
.map(|map| map.keys().filter(|key| key.starts_with(prefix)).map(|k| &**k).for_each(f));
}
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
+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());
}
+26 -1
View File
@@ -204,6 +204,22 @@ where
self.backend.child_storage(storage_key.as_ref(), key).expect(EXT_NOT_ALLOWED_TO_FAIL))
}
fn child_storage_hash(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<H::Out> {
let _guard = panic_handler::AbortGuard::force_abort();
self.overlay.child_storage(storage_key.as_ref(), key).map(|x| x.map(|x| H::hash(x))).unwrap_or_else(||
self.backend.storage_hash(key).expect(EXT_NOT_ALLOWED_TO_FAIL))
}
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
let _guard = panic_handler::AbortGuard::force_abort();
self.backend.child_storage(storage_key.as_ref(), key).expect(EXT_NOT_ALLOWED_TO_FAIL)
}
fn original_child_storage_hash(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<H::Out> {
let _guard = panic_handler::AbortGuard::force_abort();
self.backend.child_storage_hash(storage_key.as_ref(), key).expect(EXT_NOT_ALLOWED_TO_FAIL)
}
fn exists_storage(&self, key: &[u8]) -> bool {
let _guard = panic_handler::AbortGuard::force_abort();
match self.overlay.storage(key) {
@@ -263,6 +279,16 @@ where
});
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]) {
let _guard = panic_handler::AbortGuard::force_abort();
self.mark_dirty();
self.overlay.clear_child_prefix(storage_key.as_ref(), prefix);
self.backend.for_child_keys_with_prefix(storage_key.as_ref(), prefix, |key| {
self.overlay.set_child_storage(storage_key.as_ref().to_vec(), key.to_vec(), None);
});
}
fn chain_id(&self) -> u64 {
42
}
@@ -276,7 +302,6 @@ where
let child_storage_keys =
self.overlay.prospective.children.keys()
.chain(self.overlay.committed.children.keys());
let child_delta_iter = child_storage_keys.map(|storage_key|
(storage_key.clone(), self.overlay.committed.children.get(storage_key)
.into_iter()
+21
View File
@@ -155,15 +155,33 @@ pub trait Externalities<H: Hasher> {
self.storage(key).map(|v| H::hash(&v))
}
/// Get child storage value hash. This may be optimized for large values.
fn child_storage_hash(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<H::Out> {
self.child_storage(storage_key, key).map(|v| H::hash(&v))
}
/// Read original runtime storage, ignoring any overlayed changes.
fn original_storage(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Read original runtime child storage, ignoring any overlayed changes.
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>>;
/// Get original storage value hash, ignoring any overlayed changes.
/// This may be optimized for large values.
fn original_storage_hash(&self, key: &[u8]) -> Option<H::Out> {
self.original_storage(key).map(|v| H::hash(&v))
}
/// Get original child storage value hash, ignoring any overlayed changes.
/// This may be optimized for large values.
fn original_child_storage_hash(
&self,
storage_key: ChildStorageKey<H>,
key: &[u8],
) -> Option<H::Out> {
self.original_child_storage(storage_key, key).map(|v| H::hash(&v))
}
/// Read child runtime storage.
fn child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>>;
@@ -203,6 +221,9 @@ pub trait Externalities<H: Hasher> {
/// Clear storage entries which keys are start with the given prefix.
fn clear_prefix(&mut self, prefix: &[u8]);
/// Clear child storage entries which keys are start with the given prefix.
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]);
/// Set or clear a storage entry (`key`) of current contract being called (effective immediately).
fn place_storage(&mut self, key: Vec<u8>, value: Option<Vec<u8>>);
@@ -220,6 +220,38 @@ impl OverlayedChanges {
}
}
pub(crate) fn clear_child_prefix(&mut self, storage_key: &[u8], prefix: &[u8]) {
let extrinsic_index = self.extrinsic_index();
let map_entry = self.prospective.children.entry(storage_key.to_vec()).or_default();
for (key, entry) in map_entry.1.iter_mut() {
if key.starts_with(prefix) {
*entry = None;
if let Some(extrinsic) = extrinsic_index {
map_entry.0.get_or_insert_with(Default::default)
.insert(extrinsic);
}
}
}
if let Some(child_committed) = self.committed.children.get(storage_key) {
// Then do the same with keys from commited changes.
// NOTE that we are making changes in the prospective change set.
for key in child_committed.1.keys() {
if key.starts_with(prefix) {
let entry = map_entry.1.entry(key.clone()).or_default();
*entry = None;
if let Some(extrinsic) = extrinsic_index {
map_entry.0.get_or_insert_with(Default::default)
.insert(extrinsic);
}
}
}
}
}
/// Discard prospective changes to state.
pub fn discard_prospective(&mut self) {
self.prospective.clear();
@@ -174,6 +174,10 @@ impl<'a, S, H> Backend<H> for ProvingBackend<'a, S, H>
self.backend.for_keys_with_prefix(prefix, f)
}
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
self.backend.for_child_keys_with_prefix(storage_key, prefix, f)
}
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
self.backend.pairs()
}
+43 -30
View File
@@ -17,7 +17,6 @@
//! Test implementation for Externalities.
use std::collections::{HashMap};
use std::iter::FromIterator;
use hash_db::Hasher;
use crate::backend::{InMemory, Backend};
use primitives::storage::well_known_keys::is_child_storage_key;
@@ -46,22 +45,12 @@ pub struct TestExternalities<H: Hasher, N: ChangesTrieBlockNumber> {
impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N> {
/// Create a new instance of `TestExternalities` with storage.
pub fn new(storage: HashMap<Vec<u8>, Vec<u8>>) -> Self {
Self::new_with_children((storage, Default::default()))
}
/// Create a new instance of `TestExternalities` with storage and children.
pub fn new_with_children(storage: StorageTuple) -> Self {
Self::new_with_code_with_children(&[], storage)
pub fn new(storage: StorageTuple) -> Self {
Self::new_with_code(&[], storage)
}
/// Create a new instance of `TestExternalities` with code and storage.
pub fn new_with_code(code: &[u8], storage: HashMap<Vec<u8>, Vec<u8>>) -> Self {
Self::new_with_code_with_children(code, (storage, Default::default()))
}
/// Create a new instance of `TestExternalities` with code, storage and children.
pub fn new_with_code_with_children(code: &[u8], mut storage: StorageTuple) -> Self {
pub fn new_with_code(code: &[u8], mut storage: StorageTuple) -> Self {
let mut overlay = OverlayedChanges::default();
assert!(storage.0.keys().all(|key| !is_child_storage_key(key)));
@@ -137,25 +126,13 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> PartialEq for TestExternalities<H, N>
}
}
impl<H: Hasher, N: ChangesTrieBlockNumber> FromIterator<(Vec<u8>, Vec<u8>)> for TestExternalities<H, N> {
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
Self::new(iter.into_iter().collect())
}
}
impl<H: Hasher, N: ChangesTrieBlockNumber> Default for TestExternalities<H, N> {
fn default() -> Self { Self::new(Default::default()) }
}
impl<H: Hasher, N: ChangesTrieBlockNumber> From<HashMap<Vec<u8>, Vec<u8>>> for TestExternalities<H, N> {
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
Self::from_iter(hashmap)
}
}
impl<H: Hasher, N: ChangesTrieBlockNumber> From<StorageTuple> for TestExternalities<H, N> {
fn from(storage: StorageTuple) -> Self {
Self::new_with_children(storage)
Self::new(storage)
}
}
@@ -184,6 +161,13 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
)
}
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
self.backend
.child_storage(storage_key.as_ref(), key)
.map(|x| x.map(|x| x.to_vec()))
.expect(EXT_NOT_ALLOWED_TO_FAIL)
}
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
if is_child_storage_key(&key) {
panic!("Refuse to directly set child storage key");
@@ -225,20 +209,45 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
});
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]) {
self.overlay.clear_child_prefix(storage_key.as_ref(), prefix);
let backend = &self.backend;
let overlay = &mut self.overlay;
backend.for_child_keys_with_prefix(storage_key.as_ref(), prefix, |key| {
overlay.set_child_storage(storage_key.as_ref().to_vec(), key.to_vec(), None);
});
}
fn chain_id(&self) -> u64 { 42 }
fn storage_root(&mut self) -> H::Out {
let child_storage_keys =
self.overlay.prospective.children.keys()
.chain(self.overlay.committed.children.keys());
let child_delta_iter = child_storage_keys.map(|storage_key|
(storage_key.clone(), self.overlay.committed.children.get(storage_key)
.into_iter()
.flat_map(|map| map.1.iter().map(|(k, v)| (k.clone(), v.clone())))
.chain(self.overlay.prospective.children.get(storage_key)
.into_iter()
.flat_map(|map| map.1.iter().map(|(k, v)| (k.clone(), v.clone()))))));
// compute and memoize
let delta = self.overlay.committed.top.iter().map(|(k, v)| (k.clone(), v.value.clone()))
.chain(self.overlay.prospective.top.iter().map(|(k, v)| (k.clone(), v.value.clone())));
self.backend.full_storage_root(delta, child_delta_iter).0
self.backend.storage_root(delta).0
}
fn child_storage_root(&mut self, storage_key: ChildStorageKey<H>) -> Vec<u8> {
let storage_key = storage_key.as_ref();
let (root, _, _) = {
let (root, is_empty, _) = {
let delta = self.overlay.committed.children.get(storage_key)
.into_iter()
.flat_map(|map| map.1.iter().map(|(k, v)| (k.clone(), v.clone())))
@@ -248,7 +257,11 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
self.backend.child_storage_root(storage_key, delta)
};
self.overlay.set_storage(storage_key.into(), Some(root.clone()));
if is_empty {
self.overlay.set_storage(storage_key.into(), None);
} else {
self.overlay.set_storage(storage_key.into(), Some(root.clone()));
}
root
}
@@ -82,6 +82,10 @@ impl<S: TrieBackendStorage<H>, H: Hasher> Backend<H> for TrieBackend<S, H> where
self.essence.for_keys_in_child_storage(storage_key, f)
}
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
self.essence.for_child_keys_with_prefix(storage_key, prefix, f)
}
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
let mut read_overlay = S::Overlay::default();
let eph = Ephemeral::new(self.essence.backend_storage(), &mut read_overlay);
@@ -119,7 +119,27 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> {
}
/// Execute given closure for all keys starting with prefix.
pub fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], mut f: F) {
pub fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
let root_vec = match self.storage(storage_key) {
Ok(v) => v.unwrap_or(default_child_trie_root::<Layout<H>>(storage_key)),
Err(e) => {
debug!(target: "trie", "Error while iterating child storage: {}", e);
return;
}
};
let mut root = H::Out::default();
root.as_mut().copy_from_slice(&root_vec);
self.keys_with_prefix_inner(&root, prefix, f)
}
/// Execute given closure for all keys starting with prefix.
pub fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F) {
self.keys_with_prefix_inner(&self.root, prefix, f)
}
fn keys_with_prefix_inner<F: FnMut(&[u8])>(&self, root: &H::Out, prefix: &[u8], mut f: F) {
let mut read_overlay = S::Overlay::default();
let eph = Ephemeral {
storage: &self.storage,
@@ -127,7 +147,7 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> {
};
let mut iter = move || -> Result<(), Box<TrieError<H::Out>>> {
let trie = TrieDB::<H>::new(&eph, &self.root)?;
let trie = TrieDB::<H>::new(&eph, root)?;
let mut iter = trie.iter()?;
iter.seek(prefix)?;
@@ -149,6 +169,7 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> {
debug!(target: "trie", "Error while iterating by prefix: {}", e);
}
}
}
pub(crate) struct Ephemeral<'a, S: 'a + TrieBackendStorage<H>, H: 'a + Hasher> {
@@ -8,7 +8,8 @@ edition = "2018"
generic-test-client = { package = "substrate-test-client", path = "../../test-client" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
runtime = { package = "substrate-test-runtime", path = "../../test-runtime", default-features = false }
sr-primitives = { path = "../../sr-primitives" }
sr-primitives = { path = "../../sr-primitives" }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
[features]
default = [
+10 -3
View File
@@ -101,15 +101,22 @@ pub struct GenesisParameters {
impl generic_test_client::GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay) {
use codec::Encode;
let mut storage = genesis_config(self.support_changes_trie, self.heap_pages_override).genesis_map();
let child_roots = storage.1.iter().map(|(sk, child_map)| {
let state_root = <<<runtime::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_map.clone().into_iter()
);
(sk.clone(), state_root.encode())
});
let state_root = <<<runtime::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.clone().into_iter()
storage.0.clone().into_iter().chain(child_roots)
);
let block: runtime::Block = client::genesis::construct_genesis_block(state_root);
storage.extend(additional_storage_with_genesis(&block));
storage.0.extend(additional_storage_with_genesis(&block));
(storage, Default::default())
storage
}
}
+28 -3
View File
@@ -21,7 +21,7 @@ use runtime_io::{blake2_256, twox_128};
use super::{AuthorityId, AccountId, WASM_BINARY};
use codec::{Encode, KeyedVec, Joiner};
use primitives::{ChangesTrieConfiguration, map, storage::well_known_keys};
use sr_primitives::traits::Block;
use sr_primitives::traits::{Block as BlockT, Hash as HashT, Header as HeaderT};
/// Configuration of a general Substrate test genesis block.
pub struct GenesisConfig {
@@ -50,7 +50,10 @@ impl GenesisConfig {
}
}
pub fn genesis_map(&self) -> HashMap<Vec<u8>, Vec<u8>> {
pub fn genesis_map(&self) -> (
HashMap<Vec<u8>, Vec<u8>>,
HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
) {
let wasm_runtime = WASM_BINARY.to_vec();
let mut map: HashMap<Vec<u8>, Vec<u8>> = self.balances.iter()
.map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance)))
@@ -67,10 +70,32 @@ impl GenesisConfig {
map.insert(well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), changes_trie_config.encode());
}
map.insert(twox_128(&b"sys:auth"[..])[..].to_vec(), self.authorities.encode());
map
(map, Default::default())
}
}
pub fn insert_genesis_block(
storage: &mut (
HashMap<Vec<u8>, Vec<u8>>,
HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
)
) -> primitives::hash::H256 {
let child_roots = storage.1.iter().map(|(sk, child_map)| {
let state_root = <<<crate::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_map.clone().into_iter()
);
(sk.clone(), state_root.encode())
});
let state_root = <<<crate::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.0.clone().into_iter().chain(child_roots)
);
let block: crate::Block = substrate_client::genesis::construct_genesis_block(state_root);
let genesis_hash = block.header.hash();
storage.0.extend(additional_storage_with_genesis(&block));
genesis_hash
}
pub fn additional_storage_with_genesis(genesis_block: &crate::Block) -> HashMap<Vec<u8>, Vec<u8>> {
map![
twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().as_fixed_bytes().to_vec()
+3 -4
View File
@@ -201,13 +201,12 @@ pub fn finalize_block() -> Header {
let txs: Vec<_> = (0..extrinsic_index).map(ExtrinsicData::take).collect();
let txs = txs.iter().map(Vec::as_slice).collect::<Vec<_>>();
let extrinsics_root = enumerated_trie_root::<Blake2Hasher>(&txs).into();
// let mut digest = Digest::default();
let number = <Number>::take().expect("Number is set by `initialize_block`");
let parent_hash = <ParentHash>::take();
let mut digest = <StorageDigest>::take().expect("StorageDigest is set by `initialize_block`");
let o_new_authorities = <NewAuthorities>::take();
// This MUST come after all changes to storage are done. Otherwise we will fail the
// This MUST come after all changes to storage are done. Otherwise we will fail the
// “Storage root does not match that calculated” assertion.
let storage_root = BlakeTwo256::storage_root();
let storage_changes_root = BlakeTwo256::storage_changes_root(parent_hash);
@@ -323,13 +322,13 @@ mod tests {
Sr25519Keyring::Bob.to_raw_public(),
Sr25519Keyring::Charlie.to_raw_public()
];
TestExternalities::new(map![
TestExternalities::new((map![
twox_128(b"latest").to_vec() => vec![69u8; 32],
twox_128(b"sys:auth").to_vec() => authorities.encode(),
blake2_256(&AccountKeyring::Alice.to_raw_public().to_keyed_vec(b"balance:")).to_vec() => {
vec![111u8, 0, 0, 0, 0, 0, 0, 0]
}
])
], map![]))
}
fn block_import_works<F>(block_executor: F) where F: Fn(Block, &mut TestExternalities<Blake2Hasher>) {