mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 10:41:09 +00:00
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:
Generated
+1
@@ -5044,6 +5044,7 @@ dependencies = [
|
||||
name = "substrate-test-runtime-client"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-scale-codec 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-primitives 2.0.0",
|
||||
"substrate-primitives 2.0.0",
|
||||
"substrate-test-client 2.0.0",
|
||||
|
||||
@@ -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>>)>,
|
||||
|
||||
@@ -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>>)>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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"))]
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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()(
|
||||
|
||||
@@ -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,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>>)>,
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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>) {
|
||||
|
||||
@@ -116,7 +116,7 @@ mod tests {
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
system::GenesisConfig::default().build_storage::<Test>().unwrap().0.into()
|
||||
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"protocolId": "fir",
|
||||
"consensusEngine": null,
|
||||
"genesis": {
|
||||
"raw": {
|
||||
"raw": [{
|
||||
"0xbc3717660105a864bd63dcd430de64128d58bd0917fa8dd75aee827cf086e19c": "0x0000c16ff28623000000000000000000",
|
||||
"0x8f9a319405d14f3953657373696f6e204b65794f776e6572343a73657373696f6e3a6b657973a6e391e5d17627fa5aaa7a76b39ebee4b139bff595608fe41aea21aa7ea48053": "0x68655684472b743e456907b398d3a44c113f189e56d1bbfd55e889e295dfde78",
|
||||
"0x7eb7a404bf7e3466c3f6c5914e25edfaab48b1e24fd29ea5a94deaaa1aba80e6": "0x0c0001547ff0ab649283a7ae01dbc2eb73932eba2fb09075e9485ff369082a2ff38d65019c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d12",
|
||||
@@ -99,6 +99,8 @@
|
||||
"0x717a2ee9c64ad3424e10e4461ec08296": "0x0000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000008700000000000000af0000000000000001000000000000000100000000000000040000000000010010000000004000000020000000",
|
||||
"0xf186665804ca50670311307912458ce448d82cb96e7e4fe71df38c283a8720f4": "0x9c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d120f0000c16ff286230f0000c16ff2862300",
|
||||
"0x6e4ab2ac5a7cf9b1829eacc84a75bde0804be01fc31c9419ea72407f50a33384": "0xf26cdb14b5aec7b2789fd5ca80f979cef3761897ae1f37ffb3e154cbcc1c2663"
|
||||
}
|
||||
},
|
||||
{
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn panic_execution_with_foreign_code_gives_error() {
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
69_u128.encode()
|
||||
},
|
||||
@@ -187,7 +187,7 @@ mod tests {
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => {
|
||||
vec![0u8; 32]
|
||||
}
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
@@ -210,7 +210,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn bad_extrinsic_with_native_equivalent_code_gives_error() {
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
69_u128.encode()
|
||||
},
|
||||
@@ -223,7 +223,7 @@ mod tests {
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => {
|
||||
vec![0u8; 32]
|
||||
}
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
@@ -246,7 +246,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn successful_execution_with_native_equivalent_code_gives_ok() {
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
(111 * DOLLARS).encode()
|
||||
},
|
||||
@@ -255,7 +255,7 @@ mod tests {
|
||||
},
|
||||
twox_128(<indices::NextEnumSet<Runtime>>::key()).to_vec() => vec![0u8; 16],
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => vec![0u8; 32]
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
@@ -282,7 +282,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn successful_execution_with_foreign_code_gives_ok() {
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
(111 * DOLLARS).encode()
|
||||
},
|
||||
@@ -291,7 +291,7 @@ mod tests {
|
||||
},
|
||||
twox_128(<indices::NextEnumSet<Runtime>>::key()).to_vec() => vec![0u8; 16],
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => vec![0u8; 32]
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
@@ -328,7 +328,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalities<Blake2Hasher> {
|
||||
let mut ext = TestExternalities::new_with_code_with_children(code, GenesisConfig {
|
||||
let mut ext = TestExternalities::new_with_code(code, GenesisConfig {
|
||||
system: Some(SystemConfig {
|
||||
changes_trie_config: if support_changes_trie { Some(ChangesTrieConfiguration {
|
||||
digest_interval: 2,
|
||||
@@ -875,7 +875,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn panic_execution_gives_error() {
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
0_u128.encode()
|
||||
},
|
||||
@@ -884,7 +884,7 @@ mod tests {
|
||||
},
|
||||
twox_128(<indices::NextEnumSet<Runtime>>::key()).to_vec() => vec![0u8; 16],
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => vec![0u8; 32]
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let r = WasmExecutor::new()
|
||||
.call(&mut t, 8, COMPACT_CODE, "Core_initialize_block", &vec![].and(&from_block_number(1u64)));
|
||||
@@ -897,7 +897,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn successful_execution_gives_ok() {
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
(111 * DOLLARS).encode()
|
||||
},
|
||||
@@ -906,7 +906,7 @@ mod tests {
|
||||
},
|
||||
twox_128(<indices::NextEnumSet<Runtime>>::key()).to_vec() => vec![0u8; 16],
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => vec![0u8; 32]
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let r = WasmExecutor::new()
|
||||
.call(&mut t, 8, COMPACT_CODE, "Core_initialize_block", &vec![].and(&from_block_number(1u64)));
|
||||
@@ -1057,7 +1057,7 @@ mod tests {
|
||||
// - 1 MILLICENTS in substrate node.
|
||||
// - 1 milldot based on current polkadot runtime.
|
||||
// (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`)
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, map![
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, (map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
(100 * DOLLARS).encode()
|
||||
},
|
||||
@@ -1069,7 +1069,7 @@ mod tests {
|
||||
},
|
||||
twox_128(<indices::NextEnumSet<Runtime>>::key()).to_vec() => vec![0u8; 16],
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => vec![0u8; 32]
|
||||
]);
|
||||
], map![]));
|
||||
|
||||
let tip = 1_000_000;
|
||||
let xt = sign(CheckedExtrinsic {
|
||||
|
||||
@@ -80,7 +80,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
// and set impl_version to equal spec_version. If only runtime
|
||||
// implementation changes and behavior does not, then leave spec_version as
|
||||
// is and increment impl_version.
|
||||
spec_version: 135,
|
||||
spec_version: 136,
|
||||
impl_version: 136,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
};
|
||||
|
||||
@@ -288,7 +288,7 @@ mod tests {
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
system::GenesisConfig::default().build_storage::<Test>().unwrap().0.into()
|
||||
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -74,10 +74,10 @@ impl Trait for Test {
|
||||
}
|
||||
|
||||
pub fn new_test_ext(authorities: Vec<u64>) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
t.extend(GenesisConfig::<Test>{
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
GenesisConfig::<Test>{
|
||||
authorities: authorities.into_iter().map(|a| UintAuthorityId(a).to_public_key()).collect(),
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -562,7 +562,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use sr_primitives::{Perbill, traits::{Convert, IdentityLookup}, testing::Header, weights::{DispatchInfo, Weight}};
|
||||
use sr_primitives::{Perbill, traits::{Convert, IdentityLookup}, testing::Header,
|
||||
weights::{DispatchInfo, Weight}};
|
||||
use primitives::{H256, Blake2Hasher};
|
||||
use runtime_io;
|
||||
use srml_support::{impl_outer_origin, parameter_types};
|
||||
@@ -179,8 +180,8 @@ impl ExtBuilder {
|
||||
}
|
||||
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
self.set_associated_consts();
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap().0;
|
||||
t.extend(GenesisConfig::<Runtime> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
GenesisConfig::<Runtime> {
|
||||
balances: if self.monied {
|
||||
vec![
|
||||
(1, 10 * self.existential_deposit),
|
||||
@@ -201,7 +202,7 @@ impl ExtBuilder {
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,7 +433,7 @@ mod tests {
|
||||
phantom: Default::default(),
|
||||
}),
|
||||
collective: None,
|
||||
}.build_storage().unwrap().0.into()
|
||||
}.build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -277,12 +277,12 @@ impl ExtBuilder {
|
||||
balances::GenesisConfig::<Test> {
|
||||
balances: vec![],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
GenesisConfig::<Test> {
|
||||
current_schedule: Default::default(),
|
||||
gas_price: self.gas_price,
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
runtime_io::TestExternalities::new_with_children(t)
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
runtime_io::TestExternalities::new(t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -259,7 +259,7 @@ mod tests {
|
||||
(6, 60 * self.balance_factor)
|
||||
],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
seats::GenesisConfig::<Test> {
|
||||
active_council: if self.with_council { vec![
|
||||
(1, 10),
|
||||
@@ -269,8 +269,8 @@ mod tests {
|
||||
desired_seats: 2,
|
||||
presentation_duration: 2,
|
||||
term_duration: 5,
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
runtime_io::TestExternalities::new_with_children(t)
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
runtime_io::TestExternalities::new(t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1094,9 +1094,9 @@ mod tests {
|
||||
balances::GenesisConfig::<Test>{
|
||||
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
GenesisConfig::default().assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
runtime_io::TestExternalities::new_with_children(t)
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
GenesisConfig::default().assimilate_storage(&mut t).unwrap();
|
||||
runtime_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
type System = system::Module<Test>;
|
||||
|
||||
@@ -1314,7 +1314,7 @@ mod tests {
|
||||
presentation_duration: 2,
|
||||
term_duration: 5,
|
||||
}),
|
||||
}.build_storage().unwrap().0.into()
|
||||
}.build_storage().unwrap().into()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -575,15 +575,15 @@ mod tests {
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
fn new_test_ext() -> sr_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
// We use default for brevity, but you can configure as desired if needed.
|
||||
t.extend(balances::GenesisConfig::<Test>::default().build_storage().unwrap().0);
|
||||
t.extend(GenesisConfig::<Test>{
|
||||
balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
|
||||
GenesisConfig::<Test>{
|
||||
dummy: 42,
|
||||
// we configure the map with (key, value) pairs.
|
||||
bar: vec![(1, 2), (2, 3)],
|
||||
foo: 24,
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -465,10 +465,10 @@ mod tests {
|
||||
balances::GenesisConfig::<Runtime> {
|
||||
balances: vec![(1, 211)],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::transfer(2, 69));
|
||||
let weight = xt.get_dispatch_info().weight as u64;
|
||||
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new_with_children(t);
|
||||
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new(t);
|
||||
with_externalities(&mut t, || {
|
||||
Executive::initialize_block(&Header::new(
|
||||
1,
|
||||
@@ -485,11 +485,11 @@ mod tests {
|
||||
}
|
||||
|
||||
fn new_test_ext(balance_factor: u64) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap().0;
|
||||
t.extend(balances::GenesisConfig::<Runtime> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
balances::GenesisConfig::<Runtime> {
|
||||
balances: vec![(1, 111 * balance_factor)],
|
||||
vesting: vec![],
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -338,7 +338,7 @@ mod tests {
|
||||
#[test]
|
||||
fn median_works() {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||
with_externalities(&mut TestExternalities::new(t), || {
|
||||
FinalityTracker::update_hint(Some(500));
|
||||
assert_eq!(FinalityTracker::median(), 250);
|
||||
assert!(NOTIFICATIONS.with(|n| n.borrow().is_empty()));
|
||||
@@ -348,7 +348,7 @@ mod tests {
|
||||
#[test]
|
||||
fn notifies_when_stalled() {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||
with_externalities(&mut TestExternalities::new(t), || {
|
||||
let mut parent_hash = System::parent_hash();
|
||||
for i in 2..106 {
|
||||
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
|
||||
@@ -367,7 +367,7 @@ mod tests {
|
||||
#[test]
|
||||
fn recent_notifications_prevent_stalling() {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||
with_externalities(&mut TestExternalities::new(t), || {
|
||||
let mut parent_hash = System::parent_hash();
|
||||
for i in 2..106 {
|
||||
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
|
||||
|
||||
@@ -479,12 +479,11 @@ decl_storage! {
|
||||
config(endowed_accounts): Vec<T::AccountId>;
|
||||
|
||||
build(|
|
||||
storage: &mut sr_primitives::StorageOverlay,
|
||||
_: &mut sr_primitives::ChildrenStorageOverlay,
|
||||
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
|
||||
config: &GenesisConfig<T>| {
|
||||
config.assets.iter().for_each(|asset_id| {
|
||||
config.endowed_accounts.iter().for_each(|account_id| {
|
||||
storage.insert(
|
||||
storage.0.insert(
|
||||
<FreeBalance<T>>::key_for(asset_id, account_id),
|
||||
<T::Balance as codec::Encode>::encode(&config.initial_balance)
|
||||
);
|
||||
|
||||
@@ -20,7 +20,11 @@
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use sr_primitives::{Perbill, testing::Header, traits::{BlakeTwo256, IdentityLookup}};
|
||||
use sr_primitives::{
|
||||
Perbill,
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
};
|
||||
use primitives::{Blake2Hasher, H256};
|
||||
use support::{parameter_types, impl_outer_event, impl_outer_origin};
|
||||
|
||||
@@ -114,10 +118,9 @@ impl ExtBuilder {
|
||||
|
||||
// builds genesis config
|
||||
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
|
||||
t.extend(
|
||||
GenesisConfig::<Test> {
|
||||
GenesisConfig::<Test> {
|
||||
assets: vec![self.asset_id],
|
||||
endowed_accounts: self.accounts,
|
||||
initial_balance: self.initial_balance,
|
||||
@@ -125,10 +128,7 @@ impl ExtBuilder {
|
||||
staking_asset_id: 16000,
|
||||
spending_asset_id: 16001,
|
||||
}
|
||||
.build_storage()
|
||||
.unwrap()
|
||||
.0,
|
||||
);
|
||||
.assimilate_storage(&mut t).unwrap();
|
||||
|
||||
t.into()
|
||||
}
|
||||
@@ -140,6 +140,5 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
system::GenesisConfig::default()
|
||||
.build_storage::<Test>()
|
||||
.unwrap()
|
||||
.0
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -82,10 +82,10 @@ pub fn to_authorities(vec: Vec<(u64, u64)>) -> Vec<(AuthorityId, u64)> {
|
||||
}
|
||||
|
||||
pub fn new_test_ext(authorities: Vec<(u64, u64)>) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
t.extend(GenesisConfig {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
GenesisConfig {
|
||||
authorities: to_authorities(authorities),
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -102,10 +102,10 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
for i in 1..5 { h.insert(i); }
|
||||
}
|
||||
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap().0;
|
||||
t.extend(GenesisConfig::<Runtime> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
GenesisConfig::<Runtime> {
|
||||
ids: vec![1, 2, 3, 4]
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -64,8 +64,7 @@ decl_storage! {
|
||||
config(members): Vec<T::AccountId>;
|
||||
config(phantom): sr_std::marker::PhantomData<I>;
|
||||
build(|
|
||||
storage: &mut sr_primitives::StorageOverlay,
|
||||
_: &mut sr_primitives::ChildrenStorageOverlay,
|
||||
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
|
||||
config: &GenesisConfig<T, I>
|
||||
| {
|
||||
sr_io::with_storage(storage, || {
|
||||
@@ -282,12 +281,12 @@ mod tests {
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
fn new_test_ext() -> sr_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
// We use default for brevity, but you can configure as desired if needed.
|
||||
t.extend(GenesisConfig::<Test>{
|
||||
GenesisConfig::<Test>{
|
||||
members: vec![10, 20, 30],
|
||||
.. Default::default()
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
@@ -323,13 +323,12 @@ mod tests {
|
||||
type Historical = Module<Test>;
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
let (storage, _child_storage) = crate::GenesisConfig::<Test> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
crate::GenesisConfig::<Test> {
|
||||
keys: NEXT_VALIDATORS.with(|l|
|
||||
l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i).into())).collect()
|
||||
),
|
||||
}.build_storage().unwrap();
|
||||
t.extend(storage);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
runtime_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
|
||||
@@ -320,8 +320,7 @@ decl_storage! {
|
||||
add_extra_genesis {
|
||||
config(keys): Vec<(T::ValidatorId, T::Keys)>;
|
||||
build(|
|
||||
storage: &mut sr_primitives::StorageOverlay,
|
||||
_: &mut sr_primitives::ChildrenStorageOverlay,
|
||||
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
|
||||
config: &GenesisConfig<T>
|
||||
| {
|
||||
runtime_io::with_storage(storage, || {
|
||||
@@ -581,8 +580,8 @@ mod tests {
|
||||
keys: NEXT_VALIDATORS.with(|l|
|
||||
l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i).into())).collect()
|
||||
),
|
||||
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||
runtime_io::TestExternalities::new_with_children(t)
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
runtime_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
fn initialize_block(block: u64) {
|
||||
|
||||
@@ -634,8 +634,7 @@ decl_storage! {
|
||||
config(stakers):
|
||||
Vec<(T::AccountId, T::AccountId, BalanceOf<T>, StakerStatus<T::AccountId>)>;
|
||||
build(|
|
||||
storage: &mut sr_primitives::StorageOverlay,
|
||||
_: &mut sr_primitives::ChildrenStorageOverlay,
|
||||
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
|
||||
config: &GenesisConfig<T>
|
||||
| {
|
||||
with_storage(storage, || {
|
||||
|
||||
@@ -253,7 +253,7 @@ impl ExtBuilder {
|
||||
}
|
||||
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
self.set_associated_consts();
|
||||
let (mut t, mut c) = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
let mut storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
let balance_factor = if self.existential_deposit > 0 {
|
||||
256
|
||||
} else {
|
||||
@@ -285,7 +285,7 @@ impl ExtBuilder {
|
||||
(999, 1_000_000_000_000),
|
||||
],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t, &mut c);
|
||||
}.assimilate_storage(&mut storage);
|
||||
|
||||
let stake_21 = if self.fair { 1000 } else { 2000 };
|
||||
let stake_31 = if self.validator_pool { balance_factor * 1000 } else { 1 };
|
||||
@@ -310,13 +310,13 @@ impl ExtBuilder {
|
||||
offline_slash: Perbill::from_percent(5),
|
||||
offline_slash_grace: 0,
|
||||
invulnerables: vec![],
|
||||
}.assimilate_storage(&mut t, &mut c);
|
||||
}.assimilate_storage(&mut storage);
|
||||
|
||||
let _ = session::GenesisConfig::<Test> {
|
||||
keys: validators.iter().map(|x| (*x, UintAuthorityId(*x))).collect(),
|
||||
}.assimilate_storage(&mut t, &mut c);
|
||||
}.assimilate_storage(&mut storage);
|
||||
|
||||
let mut ext = t.into();
|
||||
let mut ext = storage.into();
|
||||
runtime_io::with_externalities(&mut ext, || {
|
||||
let validators = Session::validators();
|
||||
SESSION.with(|x|
|
||||
|
||||
@@ -380,7 +380,7 @@ fn decl_store_extra_genesis(
|
||||
}
|
||||
|
||||
let mut has_scall = false;
|
||||
let mut scall = quote!{ ( |_, _, _| {} ) };
|
||||
let mut scall = quote!{ ( |_, _| {} ) };
|
||||
let mut genesis_extrafields = TokenStream2::new();
|
||||
let mut genesis_extrafields_default = TokenStream2::new();
|
||||
|
||||
@@ -536,23 +536,24 @@ fn decl_store_extra_genesis(
|
||||
),
|
||||
String
|
||||
> #fn_where_clause {
|
||||
let mut storage = Default::default();
|
||||
let mut child_storage = Default::default();
|
||||
self.assimilate_storage::<#fn_traitinstance>(&mut storage, &mut child_storage)?;
|
||||
Ok((storage, child_storage))
|
||||
let mut storage = (Default::default(), Default::default());
|
||||
self.assimilate_storage::<#fn_traitinstance>(&mut storage)?;
|
||||
Ok(storage)
|
||||
}
|
||||
|
||||
/// Assimilate the storage for this module into pre-existing overlays.
|
||||
pub fn assimilate_storage #fn_generic (
|
||||
self,
|
||||
r: &mut #scrate::sr_primitives::StorageOverlay,
|
||||
c: &mut #scrate::sr_primitives::ChildrenStorageOverlay,
|
||||
tuple_storage: &mut (
|
||||
#scrate::sr_primitives::StorageOverlay,
|
||||
#scrate::sr_primitives::ChildrenStorageOverlay,
|
||||
),
|
||||
) -> std::result::Result<(), String> #fn_where_clause {
|
||||
let storage = r;
|
||||
let storage = &mut tuple_storage.0;
|
||||
|
||||
#builders
|
||||
|
||||
#scall(storage, c, &self);
|
||||
#scall(tuple_storage, &self);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -564,10 +565,12 @@ fn decl_store_extra_genesis(
|
||||
{
|
||||
fn build_module_genesis_storage(
|
||||
self,
|
||||
r: &mut #scrate::sr_primitives::StorageOverlay,
|
||||
c: &mut #scrate::sr_primitives::ChildrenStorageOverlay,
|
||||
storage: &mut (
|
||||
#scrate::sr_primitives::StorageOverlay,
|
||||
#scrate::sr_primitives::ChildrenStorageOverlay,
|
||||
),
|
||||
) -> std::result::Result<(), String> {
|
||||
self.assimilate_storage::<#fn_traitinstance> (r, c)
|
||||
self.assimilate_storage::<#fn_traitinstance> (storage)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -327,7 +327,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
GenesisConfig::default().build_storage().unwrap().0.into()
|
||||
GenesisConfig::default().build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
type Map = Data;
|
||||
|
||||
@@ -324,7 +324,7 @@ mod tests {
|
||||
StorageMethod : Option<u32>;
|
||||
}
|
||||
add_extra_genesis {
|
||||
build(|_, _, _| {});
|
||||
build(|_, _| {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,7 +379,7 @@ mod tests {
|
||||
COMPLEXTYPE3: ([u32;25]);
|
||||
}
|
||||
add_extra_genesis {
|
||||
build(|_, _, _| {});
|
||||
build(|_, _| {});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -755,7 +755,7 @@ mod test2 {
|
||||
add_extra_genesis {
|
||||
config(_marker) : ::std::marker::PhantomData<T>;
|
||||
config(extra_field) : u32 = 32;
|
||||
build(|_, _, _| {});
|
||||
build(|_, _| {});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ impl Trait for Test {
|
||||
}
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
GenesisConfig::<Test>::default().build_storage().unwrap().0.into()
|
||||
GenesisConfig::<Test>::default().build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -75,7 +75,7 @@ mod module1 {
|
||||
|
||||
add_extra_genesis {
|
||||
config(test) : T::BlockNumber;
|
||||
build(|_, _, config: &Self| {
|
||||
build(|_, config: &Self| {
|
||||
println!("{}", config.test);
|
||||
});
|
||||
}
|
||||
@@ -299,7 +299,7 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
}),
|
||||
module2_Instance2: None,
|
||||
module2_Instance3: None,
|
||||
}.build_storage().unwrap().0.into()
|
||||
}.build_storage().unwrap().into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -82,7 +82,7 @@ impl module::Trait for Runtime {
|
||||
}
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
system::GenesisConfig::default().build_storage::<Runtime>().unwrap().0.into()
|
||||
system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into()
|
||||
}
|
||||
|
||||
fn deposit_events(n: usize) {
|
||||
|
||||
@@ -411,17 +411,16 @@ decl_storage! {
|
||||
config(code): Vec<u8>;
|
||||
|
||||
build(
|
||||
|storage: &mut sr_primitives::StorageOverlay,
|
||||
_: &mut sr_primitives::ChildrenStorageOverlay,
|
||||
|storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
|
||||
config: &GenesisConfig|
|
||||
{
|
||||
use codec::Encode;
|
||||
|
||||
storage.insert(well_known_keys::CODE.to_vec(), config.code.clone());
|
||||
storage.insert(well_known_keys::EXTRINSIC_INDEX.to_vec(), 0u32.encode());
|
||||
storage.0.insert(well_known_keys::CODE.to_vec(), config.code.clone());
|
||||
storage.0.insert(well_known_keys::EXTRINSIC_INDEX.to_vec(), 0u32.encode());
|
||||
|
||||
if let Some(ref changes_trie_config) = config.changes_trie_config {
|
||||
storage.insert(
|
||||
storage.0.insert(
|
||||
well_known_keys::CHANGES_TRIE_CONFIG.to_vec(),
|
||||
changes_trie_config.encode());
|
||||
}
|
||||
@@ -683,11 +682,11 @@ impl<T: Trait> Module<T> {
|
||||
/// Get the basic externalities for this module, useful for tests.
|
||||
#[cfg(any(feature = "std", test))]
|
||||
pub fn externalities() -> TestExternalities<Blake2Hasher> {
|
||||
TestExternalities::new(map![
|
||||
TestExternalities::new((map![
|
||||
twox_128(&<BlockHash<T>>::key_for(T::BlockNumber::zero())).to_vec() => [69u8; 32].encode(),
|
||||
twox_128(<Number<T>>::key()).to_vec() => T::BlockNumber::one().encode(),
|
||||
twox_128(<ParentHash<T>>::key()).to_vec() => [69u8; 32].encode()
|
||||
])
|
||||
], map![]))
|
||||
}
|
||||
|
||||
/// Set the block number to something in particular. Can be used as an alternative to
|
||||
@@ -803,13 +802,15 @@ impl<T: Trait> Module<T> {
|
||||
/// To be called immediately after `note_applied_extrinsic` of the last extrinsic of the block
|
||||
/// has been called.
|
||||
pub fn note_finished_extrinsics() {
|
||||
let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX).unwrap_or_default();
|
||||
let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX)
|
||||
.unwrap_or_default();
|
||||
ExtrinsicCount::put(extrinsic_index);
|
||||
}
|
||||
|
||||
/// Remove all extrinsic data and save the extrinsics trie root.
|
||||
pub fn derive_extrinsics() {
|
||||
let extrinsics = (0..ExtrinsicCount::get().unwrap_or_default()).map(ExtrinsicData::take).collect();
|
||||
let extrinsics = (0..ExtrinsicCount::get().unwrap_or_default())
|
||||
.map(ExtrinsicData::take).collect();
|
||||
let xts_root = extrinsics_data_root::<T::Hashing>(extrinsics);
|
||||
<ExtrinsicsRoot<T>>::put(xts_root);
|
||||
}
|
||||
@@ -1127,7 +1128,7 @@ mod tests {
|
||||
const CALL: &<Test as Trait>::Call = &();
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
GenesisConfig::default().build_storage::<Test>().unwrap().0.into()
|
||||
GenesisConfig::default().build_storage::<Test>().unwrap().into()
|
||||
}
|
||||
|
||||
fn normal_weight_limit() -> Weight {
|
||||
|
||||
@@ -384,7 +384,7 @@ mod tests {
|
||||
#[test]
|
||||
fn timestamp_works() {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||
with_externalities(&mut TestExternalities::new(t), || {
|
||||
Timestamp::set_timestamp(42);
|
||||
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
|
||||
assert_eq!(Timestamp::now(), 69);
|
||||
@@ -395,7 +395,7 @@ mod tests {
|
||||
#[should_panic(expected = "Timestamp must be updated only once in the block")]
|
||||
fn double_timestamp_should_fail() {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||
with_externalities(&mut TestExternalities::new(t), || {
|
||||
Timestamp::set_timestamp(42);
|
||||
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
|
||||
let _ = Timestamp::dispatch(Call::set(70), Origin::NONE);
|
||||
@@ -406,7 +406,7 @@ mod tests {
|
||||
#[should_panic(expected = "Timestamp must increment by at least <MinimumPeriod> between sequential blocks")]
|
||||
fn block_period_minimum_enforced() {
|
||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||
with_externalities(&mut TestExternalities::new(t), || {
|
||||
Timestamp::set_timestamp(42);
|
||||
let _ = Timestamp::dispatch(Call::set(46), Origin::NONE);
|
||||
});
|
||||
|
||||
@@ -439,11 +439,11 @@ mod tests {
|
||||
type Treasury = Module<Test>;
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
||||
t.extend(balances::GenesisConfig::<Test>{
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
balances::GenesisConfig::<Test>{
|
||||
balances: vec![(0, 100), (1, 99), (2, 1)],
|
||||
vesting: vec![],
|
||||
}.build_storage().unwrap().0);
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user