Move CODE directly into TestExternalities (#1682)

* Move `CODE` directly into `TestExternalities`

This change allows tests to modify CODE in storage.

* Update more hashes

* Test

* Move `code` back into a field of `TestExternalities`

* Update wasm after master merge
This commit is contained in:
Bastian Köcher
2019-02-05 21:16:11 +01:00
committed by GitHub
parent 7c33728510
commit 471b7431bc
7 changed files with 56 additions and 24 deletions
+28 -14
View File
@@ -32,7 +32,7 @@ pub struct TestExternalities<H: Hasher> where H::Out: HeapSizeOf {
inner: HashMap<Vec<u8>, Vec<u8>>,
changes_trie_storage: ChangesTrieInMemoryStorage<H>,
changes: OverlayedChanges,
code: Vec<u8>,
code: Option<Vec<u8>>,
}
impl<H: Hasher> TestExternalities<H> where H::Out: HeapSizeOf {
@@ -42,7 +42,7 @@ impl<H: Hasher> TestExternalities<H> where H::Out: HeapSizeOf {
}
/// Create a new instance of `TestExternalities`
pub fn new_with_code(code: &[u8], inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
pub fn new_with_code(code: &[u8], mut inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
let mut overlay = OverlayedChanges::default();
super::set_changes_trie_config(
&mut overlay,
@@ -50,11 +50,13 @@ impl<H: Hasher> TestExternalities<H> where H::Out: HeapSizeOf {
false,
).expect("changes trie configuration is correct in test env; qed");
inner.insert(HEAP_PAGES.to_vec(), 8u64.encode());
TestExternalities {
inner,
changes_trie_storage: ChangesTrieInMemoryStorage::new(),
changes: overlay,
code: code.to_vec(),
code: Some(code.to_vec()),
}
}
@@ -79,9 +81,7 @@ impl<H: Hasher> PartialEq for TestExternalities<H> where H::Out: HeapSizeOf {
impl<H: Hasher> FromIterator<(Vec<u8>, Vec<u8>)> for TestExternalities<H> where H::Out: HeapSizeOf {
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
let mut t = Self::new(Default::default());
for i in iter {
t.inner.insert(i.0, i.1);
}
t.inner.extend(iter);
t
}
}
@@ -102,7 +102,7 @@ impl<H: Hasher> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H> where
inner: hashmap,
changes_trie_storage: ChangesTrieInMemoryStorage::new(),
changes: Default::default(),
code: Default::default(),
code: None,
}
}
}
@@ -110,9 +110,8 @@ impl<H: Hasher> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H> where
impl<H: Hasher> Externalities<H> for TestExternalities<H> where H::Out: Ord + HeapSizeOf {
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
match key {
CODE => Some(self.code.clone()),
HEAP_PAGES => Some(8u64.encode()),
_ => self.inner.get(key).map(|x| x.to_vec()),
CODE => self.code.clone(),
_ => self.inner.get(key).cloned(),
}
}
@@ -122,9 +121,14 @@ impl<H: Hasher> Externalities<H> for TestExternalities<H> where H::Out: Ord + He
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
self.changes.set_storage(key.clone(), maybe_value.clone());
match maybe_value {
Some(value) => { self.inner.insert(key, value); }
None => { self.inner.remove(&key); }
match key.as_ref() {
CODE => self.code = maybe_value,
_ => {
match maybe_value {
Some(value) => { self.inner.insert(key, value); }
None => { self.inner.remove(&key); }
}
}
}
}
@@ -171,7 +175,17 @@ mod tests {
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
const ROOT: [u8; 32] = hex!("0b41e488cccbd67d1f1089592c2c235f5c5399b053f7fe9152dd4b5f279914cd");
const ROOT: [u8; 32] = hex!("0b33ed94e74e0f8e92a55923bece1ed02d16cf424e124613ddebc53ac3eeeabe");
assert_eq!(ext.storage_root(), H256::from(ROOT));
}
#[test]
fn set_and_retrieve_code() {
let mut ext = TestExternalities::<Blake2Hasher>::default();
let code = vec![1, 2, 3];
ext.set_storage(CODE.to_vec(), code.clone());
assert_eq!(&ext.storage(CODE).unwrap(), &code);
}
}
+3 -3
View File
@@ -289,7 +289,7 @@ mod tests {
let h = Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("3d6f3663e052a7d325d3ac6cdbd3cd4033132f5bfe5852d51d4e42e7021ee69b").into(),
state_root: hex!("e51369d0b37e4aa1383f1e7a34c2eec75f18ee6b4b199a440f4f2456906e0eb7").into(),
extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
digest: Digest { logs: vec![], },
};
@@ -331,7 +331,7 @@ mod tests {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("c3d2cc317b5897af4c7f65d76b028971ce9fad745678732ff6d42301b4245a9c").into(),
state_root: hex!("f61a14ce70846cd6a1714bbe1b63b2ca1172df1c8c01adfd798bb08bd30dc486").into(),
extrinsics_root: hex!("198205cb7729fec8ccdc2e58571a4858586a4f305898078e0e8bee1dddea7e4b").into(),
digest: Digest { logs: vec![], },
},
@@ -356,7 +356,7 @@ mod tests {
header: Header {
parent_hash: b.header.hash(),
number: 2,
state_root: hex!("2c822d948bb68d7f7a1976d4f827a276a95a3ba1c4c15dbfab3bafbeb85f2b4d").into(),
state_root: hex!("a47383d9a5d6c8c7531386abccdf512c76729a1a19c59b6c2e4f95dde419923a").into(),
extrinsics_root: hex!("041fa8971dda28745967179a9f39e3ca1a595c510682105df1cff74ae6f05e0d").into(),
digest: Digest { logs: vec![], },
},
+10 -1
View File
@@ -1207,7 +1207,6 @@ version = "0.1.0"
dependencies = [
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libsecp256k1 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1217,6 +1216,7 @@ dependencies = [
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-io 0.1.0",
"sr-version 0.1.0",
"substrate-panic-handler 0.1.0",
"substrate-primitives 0.1.0",
"substrate-serializer 0.1.0",
"substrate-state-machine 0.1.0",
@@ -1245,6 +1245,14 @@ dependencies = [
"substrate-primitives 0.1.0",
]
[[package]]
name = "substrate-panic-handler"
version = "0.1.0"
dependencies = [
"backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "substrate-primitives"
version = "0.1.0"
@@ -1287,6 +1295,7 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"substrate-panic-handler 0.1.0",
"substrate-primitives 0.1.0",
"substrate-trie 0.4.0",
"trie-db 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+5 -5
View File
@@ -346,9 +346,9 @@ mod tests {
1,
GENESIS_HASH.into(),
if support_changes_trie {
hex!("cc63808897a07869d3b9103df5ad92f9be2f865ece506df5de0a87b2a95131d5").into()
hex!("d8fff70a10e0a00641458190ec32ca5681e1db38c0da9c18bb5abd76b645bb84").into()
} else {
hex!("fe5275f4d9f8130c8e80d0132f0a718ae0eeea2872c841843192720ad5c3f05a").into()
hex!("f1f00968e622ec6f47be5653b741186ef764653c82c42dab4b80d43d3226fa27").into()
},
if support_changes_trie {
vec![changes_trie_log(
@@ -374,7 +374,7 @@ mod tests {
construct_block(
2,
block1(false).1,
hex!("45b6655508fb524467b5c24184a7509b9ae07db4f95e16052ed425af182f39a8").into(),
hex!("fb05600153a562a78fe12cbbfd97aa18ddf4085505bcacbcfd1d2c0c36bba5ce").into(),
vec![ // session changes here, so we add a grandpa change signal log.
Log::from(::grandpa::RawLog::AuthoritiesChangeSignal(0, vec![
(Keyring::One.to_raw_public().into(), 1),
@@ -403,7 +403,7 @@ mod tests {
construct_block(
1,
GENESIS_HASH.into(),
hex!("ec00658cc2826d3499dde2954e399f0a0b2596eec1b0da9b76bc72394161dc99").into(),
hex!("cd856b66ec5416b8c81d480fa7ed8b8a851afff03fc09c87920f975ae913a581").into(),
vec![],
vec![
CheckedExtrinsic {
@@ -674,7 +674,7 @@ mod tests {
let b = construct_block(
1,
GENESIS_HASH.into(),
hex!("6a4da4ed61c4d9eba0477aa67024d573693df781176dfe7fe903d1088b38b266").into(),
hex!("2c024da59dcdb62f43669081355830f074c32b3bddab7aebd8bcab14d24353b7").into(),
vec![],
vec![
CheckedExtrinsic {
+10 -1
View File
@@ -1577,7 +1577,6 @@ version = "0.1.0"
dependencies = [
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libsecp256k1 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1587,6 +1586,7 @@ dependencies = [
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-io 0.1.0",
"sr-version 0.1.0",
"substrate-panic-handler 0.1.0",
"substrate-primitives 0.1.0",
"substrate-serializer 0.1.0",
"substrate-state-machine 0.1.0",
@@ -1627,6 +1627,14 @@ dependencies = [
"substrate-primitives 0.1.0",
]
[[package]]
name = "substrate-panic-handler"
version = "0.1.0"
dependencies = [
"backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "substrate-primitives"
version = "0.1.0"
@@ -1669,6 +1677,7 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"substrate-panic-handler 0.1.0",
"substrate-primitives 0.1.0",
"substrate-trie 0.4.0",
"trie-db 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",