mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 21:01:05 +00:00
Make use of child storage for testExternalities and basicExternalities (#3009)
* impl test using both storage and child_storage * few fixes * remove unused code * impl PartialEq with children keys * fmt * implementation of basic with children + rename new * assert and test * no panic in runtime * address comments * fix
This commit is contained in:
@@ -394,7 +394,10 @@ mod imp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use self::imp::{StorageOverlay, ChildrenStorageOverlay, with_storage, with_externalities};
|
pub use self::imp::{
|
||||||
|
StorageOverlay, ChildrenStorageOverlay, with_storage, with_storage_and_children,
|
||||||
|
with_externalities
|
||||||
|
};
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub use self::imp::ext::*;
|
pub use self::imp::ext::*;
|
||||||
|
|
||||||
|
|||||||
@@ -435,9 +435,32 @@ pub type ChildrenStorageOverlay = HashMap<Vec<u8>, StorageOverlay>;
|
|||||||
pub fn with_storage<R, F: FnOnce() -> R>(storage: &mut StorageOverlay, f: F) -> R {
|
pub fn with_storage<R, F: FnOnce() -> R>(storage: &mut StorageOverlay, f: F) -> R {
|
||||||
let mut alt_storage = Default::default();
|
let mut alt_storage = Default::default();
|
||||||
rstd::mem::swap(&mut alt_storage, storage);
|
rstd::mem::swap(&mut alt_storage, storage);
|
||||||
let mut ext: BasicExternalities = alt_storage.into();
|
let mut ext = BasicExternalities::new(alt_storage);
|
||||||
let r = ext::using(&mut ext, f);
|
let r = ext::using(&mut ext, f);
|
||||||
*storage = ext.into();
|
*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,
|
||||||
|
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 r = ext::using(&mut ext, f);
|
||||||
|
|
||||||
|
let storage_tuple = ext.into_storages();
|
||||||
|
*storage = storage_tuple.0;
|
||||||
|
*children_storage = storage_tuple.1;
|
||||||
|
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,67 +18,64 @@
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
use crate::backend::{Backend, InMemory};
|
||||||
use hash_db::Hasher;
|
use hash_db::Hasher;
|
||||||
use trie::trie_root;
|
use trie::trie_root;
|
||||||
use primitives::offchain;
|
use primitives::offchain;
|
||||||
use primitives::storage::well_known_keys::{CHANGES_TRIE_CONFIG, CODE, HEAP_PAGES};
|
use primitives::storage::well_known_keys::{HEAP_PAGES, is_child_storage_key};
|
||||||
use parity_codec::Encode;
|
use parity_codec::Encode;
|
||||||
use super::{ChildStorageKey, Externalities, OverlayedChanges};
|
use super::{ChildStorageKey, Externalities};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
|
|
||||||
/// Simple HashMap-based Externalities impl.
|
/// Simple HashMap-based Externalities impl.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct BasicExternalities {
|
pub struct BasicExternalities {
|
||||||
inner: HashMap<Vec<u8>, Vec<u8>>,
|
top: HashMap<Vec<u8>, Vec<u8>>,
|
||||||
changes: OverlayedChanges,
|
children: HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
|
||||||
code: Option<Vec<u8>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BasicExternalities {
|
impl BasicExternalities {
|
||||||
/// Create a new instance of `BasicExternalities`
|
/// Create a new instance of `BasicExternalities`
|
||||||
pub fn new(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
pub fn new(top: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
||||||
Self::new_with_code(&[], inner)
|
Self::new_with_children(top, Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new instance of `BasicExternalities`
|
/// Create a new instance of `BasicExternalities` with children
|
||||||
pub fn new_with_code(code: &[u8], mut inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
pub fn new_with_children(
|
||||||
let mut overlay = OverlayedChanges::default();
|
mut top: HashMap<Vec<u8>, Vec<u8>>,
|
||||||
super::set_changes_trie_config(
|
children: HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
|
||||||
&mut overlay,
|
) -> Self {
|
||||||
inner.get(&CHANGES_TRIE_CONFIG.to_vec()).cloned(),
|
top.insert(HEAP_PAGES.to_vec(), 8u64.encode());
|
||||||
false,
|
|
||||||
).expect("changes trie configuration is correct in test env; qed");
|
|
||||||
|
|
||||||
inner.insert(HEAP_PAGES.to_vec(), 8u64.encode());
|
|
||||||
|
|
||||||
BasicExternalities {
|
BasicExternalities {
|
||||||
inner,
|
top,
|
||||||
changes: overlay,
|
children,
|
||||||
code: Some(code.to_vec()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert key/value
|
/// Insert key/value
|
||||||
pub fn insert(&mut self, k: Vec<u8>, v: Vec<u8>) -> Option<Vec<u8>> {
|
pub fn insert(&mut self, k: Vec<u8>, v: Vec<u8>) -> Option<Vec<u8>> {
|
||||||
self.inner.insert(k, v)
|
self.top.insert(k, v)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ::std::fmt::Debug for BasicExternalities {
|
/// Consume self and returns inner storages
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
pub fn into_storages(self) -> (
|
||||||
write!(f, "{:?}", self.inner)
|
HashMap<Vec<u8>, Vec<u8>>,
|
||||||
|
HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
|
||||||
|
) {
|
||||||
|
(self.top, self.children)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for BasicExternalities {
|
impl PartialEq for BasicExternalities {
|
||||||
fn eq(&self, other: &BasicExternalities) -> bool {
|
fn eq(&self, other: &BasicExternalities) -> bool {
|
||||||
self.inner.eq(&other.inner)
|
self.top.eq(&other.top) && self.children.eq(&other.children)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromIterator<(Vec<u8>, Vec<u8>)> for BasicExternalities {
|
impl FromIterator<(Vec<u8>, Vec<u8>)> for BasicExternalities {
|
||||||
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
|
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
|
||||||
let mut t = Self::new(Default::default());
|
let mut t = Self::default();
|
||||||
t.inner.extend(iter);
|
t.top.extend(iter);
|
||||||
t
|
t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,69 +84,84 @@ impl Default for BasicExternalities {
|
|||||||
fn default() -> Self { Self::new(Default::default()) }
|
fn default() -> Self { Self::new(Default::default()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BasicExternalities> for HashMap<Vec<u8>, Vec<u8>> {
|
impl From<HashMap<Vec<u8>, Vec<u8>>> for BasicExternalities {
|
||||||
fn from(tex: BasicExternalities) -> Self {
|
|
||||||
tex.inner.into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From< HashMap<Vec<u8>, Vec<u8>> > for BasicExternalities {
|
|
||||||
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
||||||
BasicExternalities {
|
BasicExternalities {
|
||||||
inner: hashmap,
|
top: hashmap,
|
||||||
changes: Default::default(),
|
children: Default::default(),
|
||||||
code: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
|
impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
|
||||||
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
|
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
|
||||||
match key {
|
self.top.get(key).cloned()
|
||||||
CODE => self.code.clone(),
|
|
||||||
_ => self.inner.get(key).cloned(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn original_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
|
fn original_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
|
||||||
Externalities::<H>::storage(self, key)
|
Externalities::<H>::storage(self, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn child_storage(&self, _storage_key: ChildStorageKey<H>, _key: &[u8]) -> Option<Vec<u8>> {
|
fn child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
|
||||||
None
|
self.children.get(storage_key.as_ref()).and_then(|child| child.get(key)).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
|
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
|
||||||
self.changes.set_storage(key.clone(), maybe_value.clone());
|
if is_child_storage_key(&key) {
|
||||||
match key.as_ref() {
|
warn!(target: "trie", "Refuse to set child storage key via main storage");
|
||||||
CODE => self.code = maybe_value,
|
return;
|
||||||
_ => {
|
}
|
||||||
match maybe_value {
|
|
||||||
Some(value) => { self.inner.insert(key, value); }
|
match maybe_value {
|
||||||
None => { self.inner.remove(&key); }
|
Some(value) => { self.top.insert(key, value); }
|
||||||
}
|
None => { self.top.remove(&key); }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn place_child_storage(&mut self, _storage_key: ChildStorageKey<H>, _key: Vec<u8>, _value: Option<Vec<u8>>) {
|
fn place_child_storage(
|
||||||
|
&mut self,
|
||||||
|
storage_key: ChildStorageKey<H>,
|
||||||
|
key: Vec<u8>,
|
||||||
|
value: Option<Vec<u8>>
|
||||||
|
) {
|
||||||
|
let child_map = self.children.entry(storage_key.into_owned()).or_default();
|
||||||
|
if let Some(value) = value {
|
||||||
|
child_map.insert(key, value);
|
||||||
|
} else {
|
||||||
|
child_map.remove(&key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kill_child_storage(&mut self, _storage_key: ChildStorageKey<H>) { }
|
fn kill_child_storage(&mut self, storage_key: ChildStorageKey<H>) {
|
||||||
|
self.children.remove(storage_key.as_ref());
|
||||||
|
}
|
||||||
|
|
||||||
fn clear_prefix(&mut self, prefix: &[u8]) {
|
fn clear_prefix(&mut self, prefix: &[u8]) {
|
||||||
self.changes.clear_prefix(prefix);
|
if is_child_storage_key(prefix) {
|
||||||
self.inner.retain(|key, _| !key.starts_with(prefix));
|
warn!(
|
||||||
|
target: "trie",
|
||||||
|
"Refuse to clear prefix that is part of child storage key via main storage"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.top.retain(|key, _| !key.starts_with(prefix));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chain_id(&self) -> u64 { 42 }
|
fn chain_id(&self) -> u64 { 42 }
|
||||||
|
|
||||||
fn storage_root(&mut self) -> H::Out {
|
fn storage_root(&mut self) -> H::Out {
|
||||||
trie_root::<H, _, _, _>(self.inner.clone())
|
trie_root::<H, _, _, _>(self.top.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn child_storage_root(&mut self, _storage_key: ChildStorageKey<H>) -> Vec<u8> {
|
fn child_storage_root(&mut self, storage_key: ChildStorageKey<H>) -> Vec<u8> {
|
||||||
vec![42]
|
if let Some(child) = self.children.get(storage_key.as_ref()) {
|
||||||
|
let delta = child.clone().into_iter().map(|(k, v)| (k, Some(v)));
|
||||||
|
|
||||||
|
InMemory::<H>::default().child_storage_root(storage_key.as_ref(), delta).0
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn storage_changes_root(&mut self, _parent: H::Out) -> Result<Option<H::Out>, ()> {
|
fn storage_changes_root(&mut self, _parent: H::Out) -> Result<Option<H::Out>, ()> {
|
||||||
@@ -165,7 +177,8 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use primitives::{Blake2Hasher, H256};
|
use primitives::{Blake2Hasher, H256, map};
|
||||||
|
use primitives::storage::well_known_keys::CODE;
|
||||||
use hex_literal::hex;
|
use hex_literal::hex;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -189,4 +202,33 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(&ext.storage(CODE).unwrap(), &code);
|
assert_eq!(&ext.storage(CODE).unwrap(), &code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn children_works() {
|
||||||
|
let child_storage = b":child_storage:default:test".to_vec();
|
||||||
|
|
||||||
|
let mut ext = BasicExternalities::new_with_children(
|
||||||
|
Default::default(),
|
||||||
|
map![
|
||||||
|
child_storage.clone() => map![
|
||||||
|
b"doe".to_vec() => b"reindeer".to_vec()
|
||||||
|
]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
let ext = &mut ext as &mut dyn Externalities<Blake2Hasher>;
|
||||||
|
|
||||||
|
let child = || ChildStorageKey::from_vec(child_storage.clone()).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(ext.child_storage(child(), b"doe"), Some(b"reindeer".to_vec()));
|
||||||
|
|
||||||
|
ext.set_child_storage(child(), b"dog".to_vec(), b"puppy".to_vec());
|
||||||
|
assert_eq!(ext.child_storage(child(), b"dog"), Some(b"puppy".to_vec()));
|
||||||
|
|
||||||
|
ext.clear_child_storage(child(), b"dog");
|
||||||
|
assert_eq!(ext.child_storage(child(), b"dog"), None);
|
||||||
|
|
||||||
|
ext.kill_child_storage(child());
|
||||||
|
assert_eq!(ext.child_storage(child(), b"doe"), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
//! Test implementation for Externalities.
|
//! Test implementation for Externalities.
|
||||||
|
|
||||||
use std::collections::{HashMap, BTreeMap};
|
use std::collections::{HashMap};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use hash_db::Hasher;
|
use hash_db::Hasher;
|
||||||
use crate::backend::{InMemory, Backend};
|
use crate::backend::{InMemory, Backend};
|
||||||
@@ -32,6 +32,8 @@ use super::{ChildStorageKey, Externalities, OverlayedChanges};
|
|||||||
|
|
||||||
const EXT_NOT_ALLOWED_TO_FAIL: &str = "Externalities not allowed to fail within runtime";
|
const EXT_NOT_ALLOWED_TO_FAIL: &str = "Externalities not allowed to fail within runtime";
|
||||||
|
|
||||||
|
type StorageTuple = (HashMap<Vec<u8>, Vec<u8>>, HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>);
|
||||||
|
|
||||||
/// Simple HashMap-based Externalities impl.
|
/// Simple HashMap-based Externalities impl.
|
||||||
pub struct TestExternalities<H: Hasher, N: ChangesTrieBlockNumber> {
|
pub struct TestExternalities<H: Hasher, N: ChangesTrieBlockNumber> {
|
||||||
overlay: OverlayedChanges,
|
overlay: OverlayedChanges,
|
||||||
@@ -41,28 +43,46 @@ pub struct TestExternalities<H: Hasher, N: ChangesTrieBlockNumber> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N> {
|
impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N> {
|
||||||
/// Create a new instance of `TestExternalities`.
|
/// Create a new instance of `TestExternalities` with storage.
|
||||||
pub fn new(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
pub fn new(storage: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
||||||
Self::new_with_code(&[], inner)
|
Self::new_with_children((storage, Default::default()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new instance of `TestExternalities`
|
/// Create a new instance of `TestExternalities` with storage and children.
|
||||||
pub fn new_with_code(code: &[u8], mut inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
pub fn new_with_children(storage: StorageTuple) -> Self {
|
||||||
|
Self::new_with_code_with_children(&[], 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 {
|
||||||
let mut overlay = OverlayedChanges::default();
|
let mut overlay = OverlayedChanges::default();
|
||||||
|
|
||||||
|
assert!(storage.0.keys().all(|key| !is_child_storage_key(key)));
|
||||||
|
assert!(storage.1.keys().all(|key| is_child_storage_key(key)));
|
||||||
|
|
||||||
super::set_changes_trie_config(
|
super::set_changes_trie_config(
|
||||||
&mut overlay,
|
&mut overlay,
|
||||||
inner.get(&CHANGES_TRIE_CONFIG.to_vec()).cloned(),
|
storage.0.get(&CHANGES_TRIE_CONFIG.to_vec()).cloned(),
|
||||||
false,
|
false,
|
||||||
).expect("changes trie configuration is correct in test env; qed");
|
).expect("changes trie configuration is correct in test env; qed");
|
||||||
|
|
||||||
inner.insert(HEAP_PAGES.to_vec(), 8u64.encode());
|
storage.0.insert(HEAP_PAGES.to_vec(), 8u64.encode());
|
||||||
inner.insert(CODE.to_vec(), code.to_vec());
|
storage.0.insert(CODE.to_vec(), code.to_vec());
|
||||||
|
|
||||||
|
let backend: HashMap<_, _> = storage.1.into_iter()
|
||||||
|
.map(|(keyspace, map)| (Some(keyspace), map))
|
||||||
|
.chain(Some((None, storage.0)).into_iter())
|
||||||
|
.collect();
|
||||||
|
|
||||||
TestExternalities {
|
TestExternalities {
|
||||||
overlay,
|
overlay,
|
||||||
changes_trie_storage: ChangesTrieInMemoryStorage::new(),
|
changes_trie_storage: ChangesTrieInMemoryStorage::new(),
|
||||||
backend: inner.into(),
|
backend: backend.into(),
|
||||||
offchain: None,
|
offchain: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,17 +92,6 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N> {
|
|||||||
self.backend = self.backend.update(vec![(None, k, Some(v))]);
|
self.backend = self.backend.update(vec![(None, k, Some(v))]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iter to all pairs in key order
|
|
||||||
pub fn iter_pairs_in_order(&self) -> impl Iterator<Item=(Vec<u8>, Vec<u8>)> {
|
|
||||||
self.backend.pairs().iter()
|
|
||||||
.map(|&(ref k, ref v)| (k.to_vec(), Some(v.to_vec())))
|
|
||||||
.chain(self.overlay.committed.top.clone().into_iter().map(|(k, v)| (k, v.value)))
|
|
||||||
.chain(self.overlay.prospective.top.clone().into_iter().map(|(k, v)| (k, v.value)))
|
|
||||||
.collect::<BTreeMap<_, _>>()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|(k, maybe_val)| maybe_val.map(|val| (k, val)))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set offchain externaltiies.
|
/// Set offchain externaltiies.
|
||||||
pub fn set_offchain_externalities(&mut self, offchain: impl offchain::Externalities + 'static) {
|
pub fn set_offchain_externalities(&mut self, offchain: impl offchain::Externalities + 'static) {
|
||||||
self.offchain = Some(Box::new(offchain));
|
self.offchain = Some(Box::new(offchain));
|
||||||
@@ -92,9 +101,26 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N> {
|
|||||||
pub fn changes_trie_storage(&mut self) -> &mut ChangesTrieInMemoryStorage<H, N> {
|
pub fn changes_trie_storage(&mut self) -> &mut ChangesTrieInMemoryStorage<H, N> {
|
||||||
&mut self.changes_trie_storage
|
&mut self.changes_trie_storage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return a new backend with all pending value.
|
||||||
|
pub fn commit_all(&self) -> InMemory<H> {
|
||||||
|
let top = self.overlay.committed.top.clone().into_iter()
|
||||||
|
.chain(self.overlay.prospective.top.clone().into_iter())
|
||||||
|
.map(|(k, v)| (None, k, v.value));
|
||||||
|
|
||||||
|
let children = self.overlay.committed.children.clone().into_iter()
|
||||||
|
.chain(self.overlay.prospective.children.clone().into_iter())
|
||||||
|
.flat_map(|(keyspace, map)| {
|
||||||
|
map.1.into_iter()
|
||||||
|
.map(|(k, v)| (Some(keyspace.clone()), k, v))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
});
|
||||||
|
|
||||||
|
self.backend.update(top.chain(children).collect())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: Hasher, N: ChangesTrieBlockNumber> ::std::fmt::Debug for TestExternalities<H, N> {
|
impl<H: Hasher, N: ChangesTrieBlockNumber> std::fmt::Debug for TestExternalities<H, N> {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
write!(f, "overlay: {:?}\nbackend: {:?}", self.overlay, self.backend.pairs())
|
write!(f, "overlay: {:?}\nbackend: {:?}", self.overlay, self.backend.pairs())
|
||||||
}
|
}
|
||||||
@@ -104,15 +130,13 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> PartialEq for TestExternalities<H, N>
|
|||||||
/// This doesn't test if they are in the same state, only if they contains the
|
/// This doesn't test if they are in the same state, only if they contains the
|
||||||
/// same data at this state
|
/// same data at this state
|
||||||
fn eq(&self, other: &TestExternalities<H, N>) -> bool {
|
fn eq(&self, other: &TestExternalities<H, N>) -> bool {
|
||||||
self.iter_pairs_in_order().eq(other.iter_pairs_in_order())
|
self.commit_all().eq(&other.commit_all())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: Hasher, N: ChangesTrieBlockNumber> FromIterator<(Vec<u8>, Vec<u8>)> 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 {
|
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
|
||||||
let mut t = Self::new(Default::default());
|
Self::new(iter.into_iter().collect())
|
||||||
t.backend = t.backend.update(iter.into_iter().map(|(k, v)| (None, k, Some(v))).collect());
|
|
||||||
t
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,15 +144,15 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> Default for TestExternalities<H, N> {
|
|||||||
fn default() -> Self { Self::new(Default::default()) }
|
fn default() -> Self { Self::new(Default::default()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: Hasher, N: ChangesTrieBlockNumber> From<TestExternalities<H, N>> for HashMap<Vec<u8>, Vec<u8>> {
|
impl<H: Hasher, N: ChangesTrieBlockNumber> From<HashMap<Vec<u8>, Vec<u8>>> for TestExternalities<H, N> {
|
||||||
fn from(tex: TestExternalities<H, N>) -> Self {
|
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
||||||
tex.iter_pairs_in_order().collect()
|
Self::from_iter(hashmap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: Hasher, N: ChangesTrieBlockNumber> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H, N> {
|
impl<H: Hasher, N: ChangesTrieBlockNumber> From<StorageTuple> for TestExternalities<H, N> {
|
||||||
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
fn from(storage: StorageTuple) -> Self {
|
||||||
Self::from_iter(hashmap)
|
Self::new_with_children(storage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -297,7 +297,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalities<Blake2Hasher> {
|
fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalities<Blake2Hasher> {
|
||||||
let mut ext = TestExternalities::new_with_code(code, GenesisConfig {
|
let mut ext = TestExternalities::new_with_code_with_children(code, GenesisConfig {
|
||||||
aura: Some(Default::default()),
|
aura: Some(Default::default()),
|
||||||
system: Some(SystemConfig {
|
system: Some(SystemConfig {
|
||||||
changes_trie_config: if support_changes_trie { Some(ChangesTrieConfiguration {
|
changes_trie_config: if support_changes_trie { Some(ChangesTrieConfiguration {
|
||||||
@@ -353,7 +353,7 @@ mod tests {
|
|||||||
grandpa: Some(GrandpaConfig {
|
grandpa: Some(GrandpaConfig {
|
||||||
authorities: vec![],
|
authorities: vec![],
|
||||||
}),
|
}),
|
||||||
}.build_storage().unwrap().0);
|
}.build_storage().unwrap());
|
||||||
ext.changes_trie_storage().insert(0, GENESIS_HASH.into(), Default::default());
|
ext.changes_trie_storage().insert(0, GENESIS_HASH.into(), Default::default());
|
||||||
ext
|
ext
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -260,26 +260,16 @@ impl ExtBuilder {
|
|||||||
}
|
}
|
||||||
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||||
self.set_associated_consts();
|
self.set_associated_consts();
|
||||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||||
t.extend(
|
balances::GenesisConfig::<Test> {
|
||||||
balances::GenesisConfig::<Test> {
|
balances: vec![],
|
||||||
balances: vec![],
|
vesting: vec![],
|
||||||
vesting: vec![],
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
}
|
GenesisConfig::<Test> {
|
||||||
.build_storage()
|
current_schedule: Default::default(),
|
||||||
.unwrap()
|
gas_price: self.gas_price,
|
||||||
.0,
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
);
|
runtime_io::TestExternalities::new_with_children(t)
|
||||||
t.extend(
|
|
||||||
GenesisConfig::<Test> {
|
|
||||||
current_schedule: Default::default(),
|
|
||||||
gas_price: self.gas_price,
|
|
||||||
}
|
|
||||||
.build_storage()
|
|
||||||
.unwrap()
|
|
||||||
.0,
|
|
||||||
);
|
|
||||||
runtime_io::TestExternalities::new(t)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -238,8 +238,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||||
self.set_associated_consts();
|
self.set_associated_consts();
|
||||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||||
t.extend(balances::GenesisConfig::<Test>{
|
balances::GenesisConfig::<Test>{
|
||||||
balances: vec![
|
balances: vec![
|
||||||
(1, 10 * self.balance_factor),
|
(1, 10 * self.balance_factor),
|
||||||
(2, 20 * self.balance_factor),
|
(2, 20 * self.balance_factor),
|
||||||
@@ -249,8 +249,8 @@ mod tests {
|
|||||||
(6, 60 * self.balance_factor)
|
(6, 60 * self.balance_factor)
|
||||||
],
|
],
|
||||||
vesting: vec![],
|
vesting: vec![],
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
t.extend(seats::GenesisConfig::<Test> {
|
seats::GenesisConfig::<Test> {
|
||||||
active_council: if self.with_council { vec![
|
active_council: if self.with_council { vec![
|
||||||
(1, 10),
|
(1, 10),
|
||||||
(2, 10),
|
(2, 10),
|
||||||
@@ -259,8 +259,8 @@ mod tests {
|
|||||||
desired_seats: 2,
|
desired_seats: 2,
|
||||||
presentation_duration: 2,
|
presentation_duration: 2,
|
||||||
term_duration: 5,
|
term_duration: 5,
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
runtime_io::TestExternalities::new(t)
|
runtime_io::TestExternalities::new_with_children(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1044,13 +1044,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
fn new_test_ext() -> 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(balances::GenesisConfig::<Test>{
|
balances::GenesisConfig::<Test>{
|
||||||
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
|
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
|
||||||
vesting: vec![],
|
vesting: vec![],
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
t.extend(GenesisConfig::default().build_storage().unwrap().0);
|
GenesisConfig::default().assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
runtime_io::TestExternalities::new(t)
|
runtime_io::TestExternalities::new_with_children(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
type System = system::Module<Test>;
|
type System = system::Module<Test>;
|
||||||
|
|||||||
@@ -482,13 +482,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn balance_transfer_dispatch_works() {
|
fn balance_transfer_dispatch_works() {
|
||||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap().0;
|
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||||
t.extend(balances::GenesisConfig::<Runtime> {
|
balances::GenesisConfig::<Runtime> {
|
||||||
balances: vec![(1, 111)],
|
balances: vec![(1, 111)],
|
||||||
vesting: vec![],
|
vesting: vec![],
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
let xt = primitives::testing::TestXt(Some(1), 0, Call::transfer(2, 69));
|
let xt = primitives::testing::TestXt(Some(1), 0, Call::transfer(2, 69));
|
||||||
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new(t);
|
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new_with_children(t);
|
||||||
with_externalities(&mut t, || {
|
with_externalities(&mut t, || {
|
||||||
Executive::initialize_block(&Header::new(
|
Executive::initialize_block(&Header::new(
|
||||||
1,
|
1,
|
||||||
@@ -504,8 +504,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap().0;
|
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||||
t.extend(balances::GenesisConfig::<Runtime>::default().build_storage().unwrap().0);
|
balances::GenesisConfig::<Runtime>::default().assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
t.into()
|
t.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -327,8 +327,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn median_works() {
|
fn median_works() {
|
||||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||||
with_externalities(&mut TestExternalities::new(t), || {
|
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||||
FinalityTracker::update_hint(Some(500));
|
FinalityTracker::update_hint(Some(500));
|
||||||
assert_eq!(FinalityTracker::median(), 250);
|
assert_eq!(FinalityTracker::median(), 250);
|
||||||
assert!(NOTIFICATIONS.with(|n| n.borrow().is_empty()));
|
assert!(NOTIFICATIONS.with(|n| n.borrow().is_empty()));
|
||||||
@@ -337,8 +337,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn notifies_when_stalled() {
|
fn notifies_when_stalled() {
|
||||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||||
with_externalities(&mut TestExternalities::new(t), || {
|
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||||
let mut parent_hash = System::parent_hash();
|
let mut parent_hash = System::parent_hash();
|
||||||
for i in 2..106 {
|
for i in 2..106 {
|
||||||
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
|
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
|
||||||
@@ -356,8 +356,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn recent_notifications_prevent_stalling() {
|
fn recent_notifications_prevent_stalling() {
|
||||||
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||||
with_externalities(&mut TestExternalities::new(t), || {
|
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||||
let mut parent_hash = System::parent_hash();
|
let mut parent_hash = System::parent_hash();
|
||||||
for i in 2..106 {
|
for i in 2..106 {
|
||||||
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
|
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
|
||||||
|
|||||||
@@ -563,17 +563,16 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||||
TEST_SESSION_CHANGED.with(|l| *l.borrow_mut() = false);
|
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
|
timestamp::GenesisConfig::<Test> {
|
||||||
t.extend(timestamp::GenesisConfig::<Test> {
|
|
||||||
minimum_period: 5,
|
minimum_period: 5,
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
t.extend(GenesisConfig::<Test> {
|
GenesisConfig::<Test> {
|
||||||
keys: NEXT_VALIDATORS.with(|l|
|
keys: NEXT_VALIDATORS.with(|l|
|
||||||
l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i))).collect()
|
l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i))).collect()
|
||||||
),
|
),
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
runtime_io::TestExternalities::new(t)
|
runtime_io::TestExternalities::new_with_children(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initialize_block(block: u64) {
|
fn initialize_block(block: u64) {
|
||||||
|
|||||||
@@ -364,12 +364,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn timestamp_works() {
|
fn timestamp_works() {
|
||||||
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> {
|
||||||
minimum_period: 5,
|
minimum_period: 5,
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
|
|
||||||
with_externalities(&mut TestExternalities::new(t), || {
|
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||||
Timestamp::set_timestamp(42);
|
Timestamp::set_timestamp(42);
|
||||||
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
|
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
|
||||||
assert_eq!(Timestamp::now(), 69);
|
assert_eq!(Timestamp::now(), 69);
|
||||||
@@ -379,12 +379,12 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Timestamp must be updated only once in the block")]
|
#[should_panic(expected = "Timestamp must be updated only once in the block")]
|
||||||
fn double_timestamp_should_fail() {
|
fn double_timestamp_should_fail() {
|
||||||
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> {
|
||||||
minimum_period: 5,
|
minimum_period: 5,
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
|
|
||||||
with_externalities(&mut TestExternalities::new(t), || {
|
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||||
Timestamp::set_timestamp(42);
|
Timestamp::set_timestamp(42);
|
||||||
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
|
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
|
||||||
let _ = Timestamp::dispatch(Call::set(70), Origin::NONE);
|
let _ = Timestamp::dispatch(Call::set(70), Origin::NONE);
|
||||||
@@ -394,12 +394,12 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Timestamp must increment by at least <MinimumPeriod> between sequential blocks")]
|
#[should_panic(expected = "Timestamp must increment by at least <MinimumPeriod> between sequential blocks")]
|
||||||
fn block_period_minimum_enforced() {
|
fn block_period_minimum_enforced() {
|
||||||
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> {
|
||||||
minimum_period: 5,
|
minimum_period: 5,
|
||||||
}.build_storage().unwrap().0);
|
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
|
||||||
|
|
||||||
with_externalities(&mut TestExternalities::new(t), || {
|
with_externalities(&mut TestExternalities::new_with_children(t), || {
|
||||||
Timestamp::set_timestamp(42);
|
Timestamp::set_timestamp(42);
|
||||||
let _ = Timestamp::dispatch(Call::set(46), Origin::NONE);
|
let _ = Timestamp::dispatch(Call::set(46), Origin::NONE);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user