From db5e6712d7204bed683bc0cc1d23e86d0a7f444a Mon Sep 17 00:00:00 2001 From: Juan Aguilar Date: Mon, 12 Aug 2019 16:13:02 +0200 Subject: [PATCH] Remove enumerate_trie_root in favour of ordered_trie_root #2382 (#3360) --- .../core/executor/runtime-test/src/lib.rs | 6 ++-- substrate/core/executor/src/wasm_executor.rs | 4 +-- substrate/core/sr-io/src/lib.rs | 9 ------ substrate/core/sr-io/with_std.rs | 8 ------ substrate/core/sr-io/without_std.rs | 28 +++++++++++-------- substrate/core/sr-primitives/src/traits.rs | 12 ++------ substrate/core/test-runtime/src/system.rs | 6 ++-- substrate/srml/system/src/lib.rs | 2 +- 8 files changed, 29 insertions(+), 46 deletions(-) diff --git a/substrate/core/executor/runtime-test/src/lib.rs b/substrate/core/executor/runtime-test/src/lib.rs index 49612c271e..5e276a8814 100644 --- a/substrate/core/executor/runtime-test/src/lib.rs +++ b/substrate/core/executor/runtime-test/src/lib.rs @@ -9,7 +9,7 @@ use rstd::{vec::Vec, slice, vec}; use runtime_io::{ set_storage, storage, clear_prefix, print, blake2_128, blake2_256, - twox_128, twox_256, ed25519_verify, sr25519_verify, enumerated_trie_root + twox_128, twox_256, ed25519_verify, sr25519_verify, ordered_trie_root }; use primitives::{ed25519, sr25519}; @@ -93,8 +93,8 @@ impl_stubs!( let msg = b"all ok!"; [sr25519_verify(&sr25519::Signature(sig), &msg[..], &sr25519::Public(pubkey)) as u8].to_vec() }, - test_enumerated_trie_root => |_| { - enumerated_trie_root::( + test_ordered_trie_root => |_| { + ordered_trie_root::( &[ &b"zero"[..], &b"one"[..], diff --git a/substrate/core/executor/src/wasm_executor.rs b/substrate/core/executor/src/wasm_executor.rs index 185aaba513..3eed549773 100644 --- a/substrate/core/executor/src/wasm_executor.rs +++ b/substrate/core/executor/src/wasm_executor.rs @@ -1660,12 +1660,12 @@ mod tests { } #[test] - fn enumerated_trie_root_should_work() { + fn ordered_trie_root_should_work() { let mut ext = TestExternalities::::default(); let trie_input = vec![b"zero".to_vec(), b"one".to_vec(), b"two".to_vec()]; let test_code = WASM_BINARY; assert_eq!( - WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_enumerated_trie_root", &[]).unwrap(), + WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_ordered_trie_root", &[]).unwrap(), Layout::::ordered_trie_root(trie_input.iter()).as_fixed_bytes().encode() ); } diff --git a/substrate/core/sr-io/src/lib.rs b/substrate/core/sr-io/src/lib.rs index 425d9a714c..91b27efba0 100644 --- a/substrate/core/sr-io/src/lib.rs +++ b/substrate/core/sr-io/src/lib.rs @@ -153,15 +153,6 @@ export_api! { /// "Commit" all existing operations and get the resultant storage change root. fn storage_changes_root(parent_hash: [u8; 32]) -> Option<[u8; 32]>; - /// A trie root formed from the enumerated items. - /// TODO [#2382] remove (just use `ordered_trie_root` (NOTE currently not implemented for without_std)) - fn enumerated_trie_root(input: &[&[u8]]) -> H::Out - where - H: Hasher, - H: self::imp::HasherBounds, - H::Out: Ord - ; - /// A trie root formed from the iterated items. fn trie_root(input: I) -> H::Out where diff --git a/substrate/core/sr-io/with_std.rs b/substrate/core/sr-io/with_std.rs index ec9d8f06f0..167011608f 100644 --- a/substrate/core/sr-io/with_std.rs +++ b/substrate/core/sr-io/with_std.rs @@ -167,14 +167,6 @@ impl StorageApi for () { ).unwrap_or(Ok(None)).expect("Invalid parent hash passed to storage_changes_root") } - fn enumerated_trie_root(input: &[&[u8]]) -> H::Out - where - H: Hasher, - H::Out: Ord, - { - Layout::::ordered_trie_root(input) - } - fn trie_root(input: I) -> H::Out where I: IntoIterator, diff --git a/substrate/core/sr-io/without_std.rs b/substrate/core/sr-io/without_std.rs index 118a91819a..954eccc9cf 100644 --- a/substrate/core/sr-io/without_std.rs +++ b/substrate/core/sr-io/without_std.rs @@ -128,8 +128,11 @@ pub mod ext { /// Ensures we use the right crypto when calling into native pub trait ExternTrieCrypto: Hasher { - /// Calculate enumerated trie root. - fn enumerated_trie_root(values: &[&[u8]]) -> Self::Out; + /// A trie root formed from the enumerated items. + fn ordered_trie_root< + A: AsRef<[u8]>, + I: IntoIterator + >(values: I) -> Self::Out; } /// Additional bounds for Hasher trait for without_std. @@ -138,9 +141,16 @@ pub mod ext { // Ensures we use a Blake2_256-flavored Hasher when calling into native impl ExternTrieCrypto for Blake2Hasher { - fn enumerated_trie_root(values: &[&[u8]]) -> Self::Out { - let lengths = values.iter().map(|v| (v.len() as u32).to_le()).collect::>(); - let values = values.iter().fold(Vec::new(), |mut acc, sl| { acc.extend_from_slice(sl); acc }); + fn ordered_trie_root< + A: AsRef<[u8]>, + I: IntoIterator + >(items: I) -> Self::Out { + let mut values = Vec::new(); + let mut lengths = Vec::new(); + for v in items.into_iter() { + values.extend_from_slice(v.as_ref()); + lengths.push((v.as_ref().len() as u32).to_le()); + } let mut result: [u8; 32] = Default::default(); unsafe { ext_blake2_256_enumerated_trie_root.get()( @@ -767,10 +777,6 @@ impl StorageApi for () { } } - fn enumerated_trie_root(values: &[&[u8]]) -> H::Out { - H::enumerated_trie_root(values) - } - fn trie_root< H: Hasher + ExternTrieCrypto, I: IntoIterator, @@ -784,8 +790,8 @@ impl StorageApi for () { H: Hasher + ExternTrieCrypto, I: IntoIterator, A: AsRef<[u8]> - >(_input: I) -> H::Out { - unimplemented!() + >(values: I) -> H::Out { + H::ordered_trie_root(values) } } diff --git a/substrate/core/sr-primitives/src/traits.rs b/substrate/core/sr-primitives/src/traits.rs index de700ffec3..5fc0cb5c57 100644 --- a/substrate/core/sr-primitives/src/traits.rs +++ b/substrate/core/sr-primitives/src/traits.rs @@ -466,12 +466,9 @@ pub trait Hash: 'static + MaybeSerializeDebug + Clone + Eq + PartialEq { // Stup Encode::using_encoded(s, Self::hash) } - /// Produce the trie-db root of a mapping from indices to byte slices. - fn enumerated_trie_root(items: &[&[u8]]) -> Self::Output; - - /// Iterator-based version of `enumerated_trie_root`. + /// Iterator-based version of `ordered_trie_root`. fn ordered_trie_root< - I: IntoIterator + Iterator, + I: IntoIterator, A: AsRef<[u8]> >(input: I) -> Self::Output; @@ -500,9 +497,6 @@ impl Hash for BlakeTwo256 { fn hash(s: &[u8]) -> Self::Output { runtime_io::blake2_256(s).into() } - fn enumerated_trie_root(items: &[&[u8]]) -> Self::Output { - runtime_io::enumerated_trie_root::(items).into() - } fn trie_root< I: IntoIterator, A: AsRef<[u8]> + Ord, @@ -511,7 +505,7 @@ impl Hash for BlakeTwo256 { runtime_io::trie_root::(input).into() } fn ordered_trie_root< - I: IntoIterator + Iterator, + I: IntoIterator, A: AsRef<[u8]> >(input: I) -> Self::Output { runtime_io::ordered_trie_root::(input).into() diff --git a/substrate/core/test-runtime/src/system.rs b/substrate/core/test-runtime/src/system.rs index 44673aa5b8..f3c890cf79 100644 --- a/substrate/core/test-runtime/src/system.rs +++ b/substrate/core/test-runtime/src/system.rs @@ -18,7 +18,7 @@ //! and depositing logs. use rstd::prelude::*; -use runtime_io::{storage_root, enumerated_trie_root, storage_changes_root, twox_128, blake2_256}; +use runtime_io::{storage_root, ordered_trie_root, storage_changes_root, twox_128, blake2_256}; use runtime_support::storage::{self, StorageValue, StorageMap}; use runtime_support::storage_items; use sr_primitives::traits::{Hash as HashT, BlakeTwo256, Header as _}; @@ -96,7 +96,7 @@ fn execute_block_with_state_root_handler( // check transaction trie root represents the transactions. let txs = block.extrinsics.iter().map(Encode::encode).collect::>(); let txs = txs.iter().map(Vec::as_slice).collect::>(); - let txs_root = enumerated_trie_root::(&txs).into(); + let txs_root = ordered_trie_root::(&txs).into(); info_expect_equal_hash(&txs_root, &header.extrinsics_root); if let Mode::Overwrite = mode { header.extrinsics_root = txs_root; @@ -200,7 +200,7 @@ pub fn finalize_block() -> Header { let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX).unwrap(); let txs: Vec<_> = (0..extrinsic_index).map(ExtrinsicData::take).collect(); let txs = txs.iter().map(Vec::as_slice).collect::>(); - let extrinsics_root = enumerated_trie_root::(&txs).into(); + let extrinsics_root = ordered_trie_root::(&txs).into(); let number = ::take().expect("Number is set by `initialize_block`"); let parent_hash = ::take(); let mut digest = ::take().expect("StorageDigest is set by `initialize_block`"); diff --git a/substrate/srml/system/src/lib.rs b/substrate/srml/system/src/lib.rs index eb4c4a907a..684af7c6b8 100644 --- a/substrate/srml/system/src/lib.rs +++ b/substrate/srml/system/src/lib.rs @@ -147,7 +147,7 @@ pub fn extrinsics_root(extrinsics: &[E]) -> H::Output /// Compute the trie root of a list of extrinsics. pub fn extrinsics_data_root(xts: Vec>) -> H::Output { let xts = xts.iter().map(Vec::as_slice).collect::>(); - H::enumerated_trie_root(&xts) + H::ordered_trie_root(&xts) } pub trait Trait: 'static + Eq + Clone {