From 46984e10e76e235f76962afb586004b7cefdd721 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 18 May 2020 10:58:25 +0200 Subject: [PATCH 1/7] refacto from complex_proof --- test-support/reference-trie/src/lib.rs | 262 +++++-------- trie-db/fuzz/Cargo.toml | 13 +- trie-db/fuzz/fuzz_targets/no_ext_insert.rs | 2 +- .../fuzz/fuzz_targets/no_ext_insert_rem.rs | 2 +- trie-db/fuzz/fuzz_targets/prefix_iter.rs | 2 +- trie-db/fuzz/fuzz_targets/seek_iter.rs | 2 +- trie-db/fuzz/fuzz_targets/trie_codec_proof.rs | 8 + .../fuzz/fuzz_targets/trie_proof_invalid.rs | 4 +- trie-db/fuzz/fuzz_targets/trie_proof_valid.rs | 4 +- trie-db/fuzz/fuzz_targets/trie_root.rs | 2 +- .../fuzz/fuzz_targets/trie_root_fix_len.rs | 2 +- trie-db/fuzz/fuzz_targets/trie_root_new.rs | 2 +- .../fuzz/fuzz_targets/trie_unhashed_no_ext.rs | 10 - trie-db/fuzz/src/lib.rs | 153 ++++++-- trie-db/src/fatdb.rs | 5 +- trie-db/src/fatdbmut.rs | 13 +- trie-db/src/iter_build.rs | 146 +++----- trie-db/src/iterator.rs | 351 ++++++------------ trie-db/src/recorder.rs | 11 +- trie-db/src/sectriedb.rs | 7 +- trie-db/src/sectriedbmut.rs | 7 +- trie-db/src/triedb.rs | 191 +++------- trie-db/src/triedbmut.rs | 287 ++++++-------- 23 files changed, 558 insertions(+), 928 deletions(-) create mode 100644 trie-db/fuzz/fuzz_targets/trie_codec_proof.rs delete mode 100644 trie-db/fuzz/fuzz_targets/trie_unhashed_no_ext.rs diff --git a/test-support/reference-trie/src/lib.rs b/test-support/reference-trie/src/lib.rs index 34c163f0..87d3dbca 100644 --- a/test-support/reference-trie/src/lib.rs +++ b/test-support/reference-trie/src/lib.rs @@ -37,13 +37,27 @@ pub use trie_db::{ decode_compact, encode_compact, nibble_ops, NibbleSlice, NibbleVec, NodeCodec, proof, Record, Recorder, Trie, TrieConfiguration, TrieDB, TrieDBIterator, TrieDBMut, TrieDBNodeIterator, TrieError, - TrieIterator, TrieLayout, TrieMut, + TrieIterator, TrieLayout, TrieMut, Lookup, }; pub use trie_root::TrieStream; pub mod node { pub use trie_db::node::Node; } +/// Reference hasher is a keccak hasher. +pub type RefHasher = keccak_hasher::KeccakHasher; + +#[macro_export] +macro_rules! test_layouts { + ($test:ident, $test_internal:ident) => { + #[test] + fn $test() { + $test_internal::(); + $test_internal::(); + } + }; +} + /// Trie layout using extension nodes. pub struct ExtensionLayout; @@ -99,9 +113,7 @@ impl Bitmap { } pub type RefTrieDB<'a> = trie_db::TrieDB<'a, ExtensionLayout>; -pub type RefTrieDBNoExt<'a> = trie_db::TrieDB<'a, NoExtensionLayout>; pub type RefTrieDBMut<'a> = trie_db::TrieDBMut<'a, ExtensionLayout>; -pub type RefTrieDBMutNoExt<'a> = trie_db::TrieDBMut<'a, NoExtensionLayout>; pub type RefFatDB<'a> = trie_db::FatDB<'a, ExtensionLayout>; pub type RefFatDBMut<'a> = trie_db::FatDBMut<'a, ExtensionLayout>; pub type RefSecTrieDB<'a> = trie_db::SecTrieDB<'a, ExtensionLayout>; @@ -109,36 +121,38 @@ pub type RefSecTrieDBMut<'a> = trie_db::SecTrieDBMut<'a, ExtensionLayout>; pub type RefLookup<'a, Q> = trie_db::Lookup<'a, ExtensionLayout, Q>; pub type RefLookupNoExt<'a, Q> = trie_db::Lookup<'a, NoExtensionLayout, Q>; -pub fn reference_trie_root(input: I) -> ::Out where - I: IntoIterator, - A: AsRef<[u8]> + Ord + fmt::Debug, - B: AsRef<[u8]> + fmt::Debug, -{ - trie_root::trie_root::(input) -} - -fn reference_trie_root_unhashed(input: I) -> Vec where +pub fn reference_trie_root(input: I) -> ::Out where I: IntoIterator, A: AsRef<[u8]> + Ord + fmt::Debug, B: AsRef<[u8]> + fmt::Debug, { - trie_root::unhashed_trie::(input) + if T::USE_EXTENSION { + trie_root::trie_root::(input) + } else { + trie_root::trie_root_no_extension::(input) + } } -pub fn reference_trie_root_no_extension(input: I) -> ::Out where - I: IntoIterator, - A: AsRef<[u8]> + Ord + fmt::Debug, - B: AsRef<[u8]> + fmt::Debug, +fn data_sorted_unique(input: I) -> Vec<(A, B)> + where + I: IntoIterator, { - trie_root::trie_root_no_extension::(input) + let mut m = std::collections::BTreeMap::new(); + for (k,v) in input { + let _ = m.insert(k,v); // latest value for uniqueness + } + m.into_iter().collect() } -fn reference_trie_root_unhashed_no_extension(input: I) -> Vec where +pub fn reference_trie_root_iter_build(input: I) -> ::Out where + T: TrieLayout, I: IntoIterator, A: AsRef<[u8]> + Ord + fmt::Debug, B: AsRef<[u8]> + fmt::Debug, { - trie_root::unhashed_trie_no_extension::(input) + let mut cb = trie_db::TrieRoot::::default(); + trie_visit::(data_sorted_unique(input), &mut cb); + cb.root.unwrap_or(Default::default()) } const EMPTY_TRIE: u8 = 0; @@ -897,33 +911,32 @@ impl NodeCodec for ReferenceNodeCodecNoExt { .copy_from_slice(&bitmap.as_ref()[..BITMAP_LENGTH]); output } - } /// Compare trie builder and in memory trie. -pub fn compare_implementations + Eq> ( +pub fn compare_implementations ( data: Vec<(Vec, Vec)>, - mut memdb: X, - mut hashdb: X, -) { - let root_new = { - let mut cb = TrieBuilder::new(&mut hashdb); - trie_visit::(data.clone().into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) - }; + mut memdb: DB, + mut hashdb: DB, +) + where + T: TrieLayout, + DB : hash_db::HashDB + Eq, +{ + let root_new = calc_root_build::(data.clone(), &mut hashdb); let root = { let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for i in 0..data.len() { t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); } t.commit(); - *t.root() + t.root().clone() }; if root_new != root { { let db : &dyn hash_db::HashDB<_, _> = &hashdb; - let t = RefTrieDB::new(&db, &root_new).unwrap(); + let t = TrieDB::::new(&db, &root_new).unwrap(); println!("{:?}", t); for a in t.iter().unwrap() { println!("a:{:x?}", a); @@ -931,7 +944,7 @@ pub fn compare_implementations + Eq> } { let db : &dyn hash_db::HashDB<_, _> = &memdb; - let t = RefTrieDB::new(&db, &root).unwrap(); + let t = TrieDB::::new(&db, &root).unwrap(); println!("{:?}", t); for a in t.iter().unwrap() { println!("a:{:x?}", a); @@ -945,188 +958,86 @@ pub fn compare_implementations + Eq> } /// Compare trie builder and trie root implementations. -pub fn compare_root( +pub fn compare_root>( data: Vec<(Vec, Vec)>, - mut memdb: impl hash_db::HashDB, + mut memdb: DB, ) { - let root_new = { - let mut cb = TrieRoot::::default(); - trie_visit::(data.clone().into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) - }; + let root_new = reference_trie_root_iter_build::(data.clone()); let root = { let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = trie_db::TrieDBMut::::new(&mut memdb, &mut root); for i in 0..data.len() { t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); } - *t.root() - }; - - assert_eq!(root, root_new); -} - -/// Compare trie builder and trie root unhashed implementations. -pub fn compare_unhashed( - data: Vec<(Vec, Vec)>, -) { - let root_new = { - let mut cb = trie_db::TrieRootUnhashed::::default(); - trie_visit::(data.clone().into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) + t.root().clone() }; - let root = reference_trie_root_unhashed(data); assert_eq!(root, root_new); } -/// Compare trie builder and trie root unhashed implementations. -/// This uses the variant without extension nodes. -pub fn compare_unhashed_no_extension( - data: Vec<(Vec, Vec)>, -) { - let root_new = { - let mut cb = trie_db::TrieRootUnhashed::::default(); - trie_visit::(data.clone().into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) - }; - let root = reference_trie_root_unhashed_no_extension(data); - - assert_eq!(root, root_new); -} - -/// Trie builder root calculation utility. -pub fn calc_root( - data: I, -) -> ::Out - where - I: IntoIterator, - A: AsRef<[u8]> + Ord + fmt::Debug, - B: AsRef<[u8]> + fmt::Debug, -{ - let mut cb = TrieRoot::::default(); - trie_visit::(data.into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) -} - /// Trie builder root calculation utility. -/// This uses the variant without extension nodes. -pub fn calc_root_no_extension( +pub fn calc_root( data: I, -) -> ::Out +) -> ::Out where + T: TrieLayout, I: IntoIterator, A: AsRef<[u8]> + Ord + fmt::Debug, B: AsRef<[u8]> + fmt::Debug, { - let mut cb = TrieRoot::::default(); - trie_db::trie_visit::(data.into_iter(), &mut cb); + let mut cb = TrieRoot::::default(); + trie_visit::(data.into_iter(), &mut cb); cb.root.unwrap_or(Default::default()) } /// Trie builder trie building utility. -pub fn calc_root_build( +pub fn calc_root_build( data: I, hashdb: &mut DB -) -> ::Out - where - I: IntoIterator, - A: AsRef<[u8]> + Ord + fmt::Debug, - B: AsRef<[u8]> + fmt::Debug, - DB: hash_db::HashDB -{ - let mut cb = TrieBuilder::new(hashdb); - trie_visit::(data.into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) -} - -/// Trie builder trie building utility. -/// This uses the variant without extension nodes. -pub fn calc_root_build_no_extension( - data: I, - hashdb: &mut DB, -) -> ::Out +) -> ::Out where + T: TrieLayout, I: IntoIterator, A: AsRef<[u8]> + Ord + fmt::Debug, B: AsRef<[u8]> + fmt::Debug, - DB: hash_db::HashDB + DB: hash_db::HashDB { let mut cb = TrieBuilder::new(hashdb); - trie_db::trie_visit::(data.into_iter(), &mut cb); + trie_visit::(data.into_iter(), &mut cb); cb.root.unwrap_or(Default::default()) } -/// Compare trie builder and in memory trie. -/// This uses the variant without extension nodes. -pub fn compare_implementations_no_extension( - data: Vec<(Vec, Vec)>, - mut memdb: impl hash_db::HashDB, - mut hashdb: impl hash_db::HashDB, -) { - let root_new = { - let mut cb = TrieBuilder::new(&mut hashdb); - trie_visit::(data.clone().into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) - }; - let root = { - let mut root = Default::default(); - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); - for i in 0..data.len() { - t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); - } - *t.root() - }; - - if root != root_new { - { - let db : &dyn hash_db::HashDB<_, _> = &memdb; - let t = RefTrieDBNoExt::new(&db, &root).unwrap(); - println!("{:?}", t); - for a in t.iter().unwrap() { - println!("a:{:?}", a); - } - } - { - let db : &dyn hash_db::HashDB<_, _> = &hashdb; - let t = RefTrieDBNoExt::new(&db, &root_new).unwrap(); - println!("{:?}", t); - for a in t.iter().unwrap() { - println!("a:{:?}", a); - } - } - } - - assert_eq!(root, root_new); -} - /// `compare_implementations_no_extension` for unordered input (trie_root does /// ordering before running when trie_build expect correct ordering). -pub fn compare_implementations_no_extension_unordered( +pub fn compare_implementations_unordered ( data: Vec<(Vec, Vec)>, - mut memdb: impl hash_db::HashDB, - mut hashdb: impl hash_db::HashDB, -) { + mut memdb: DB, + mut hashdb: DB, +) + where + T: TrieLayout, + DB : hash_db::HashDB + Eq, +{ let mut b_map = std::collections::btree_map::BTreeMap::new(); let root = { let mut root = Default::default(); - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for i in 0..data.len() { t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); b_map.insert(data[i].0.clone(), data[i].1.clone()); } - *t.root() + t.root().clone() }; let root_new = { let mut cb = TrieBuilder::new(&mut hashdb); - trie_visit::(b_map.into_iter(), &mut cb); + trie_visit::(b_map.into_iter(), &mut cb); cb.root.unwrap_or(Default::default()) }; if root != root_new { { let db : &dyn hash_db::HashDB<_, _> = &memdb; - let t = RefTrieDBNoExt::new(&db, &root).unwrap(); + let t = TrieDB::::new(&db, &root).unwrap(); println!("{:?}", t); for a in t.iter().unwrap() { println!("a:{:?}", a); @@ -1134,7 +1045,7 @@ pub fn compare_implementations_no_extension_unordered( } { let db : &dyn hash_db::HashDB<_, _> = &hashdb; - let t = RefTrieDBNoExt::new(&db, &root_new).unwrap(); + let t = TrieDB::::new(&db, &root_new).unwrap(); println!("{:?}", t); for a in t.iter().unwrap() { println!("a:{:?}", a); @@ -1147,21 +1058,26 @@ pub fn compare_implementations_no_extension_unordered( /// Testing utility that uses some periodic removal over /// its input test data. -pub fn compare_no_extension_insert_remove( +pub fn compare_insert_remove>( data: Vec<(bool, Vec, Vec)>, - mut memdb: impl hash_db::HashDB, -) { + mut memdb: DB, +) + where + T: TrieLayout, + DB : hash_db::HashDB + Eq, +{ + let mut data2 = std::collections::BTreeMap::new(); let mut root = Default::default(); let mut a = 0; { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.commit(); } while a < data.len() { // new triemut every 3 element root = { - let mut t = RefTrieDBMutNoExt::from_existing(&mut memdb, &mut root).unwrap(); + let mut t = TrieDBMut::::from_existing(&mut memdb, &mut root).unwrap(); for _ in 0..3 { if data[a].0 { // remove @@ -1182,10 +1098,10 @@ pub fn compare_no_extension_insert_remove( *t.root() }; } - let mut t = RefTrieDBMutNoExt::from_existing(&mut memdb, &mut root).unwrap(); + let mut t = TrieDBMut::::from_existing(&mut memdb, &mut root).unwrap(); // we are testing the RefTrie code here so we do not sort or check uniqueness // before. - assert_eq!(*t.root(), calc_root_no_extension(data2)); + assert_eq!(*t.root(), calc_root::(data2)); } #[cfg(test)] @@ -1216,9 +1132,9 @@ mod tests { fn too_big_nibble_length() { // + 1 for 0 added byte of nibble encode let input = vec![0u8; (NIBBLE_SIZE_BOUND_NO_EXT as usize + 1) / 2 + 1]; - let enc = as NodeCodec> + let enc = as NodeCodec> ::leaf_node(((0, 0), &input), &[1]); - let dec = as NodeCodec> + let dec = as NodeCodec> ::decode(&enc).unwrap(); let o_sl = if let Node::Leaf(sl, _) = dec { Some(sl) diff --git a/trie-db/fuzz/Cargo.toml b/trie-db/fuzz/Cargo.toml index 50ac2501..037e37b2 100644 --- a/trie-db/fuzz/Cargo.toml +++ b/trie-db/fuzz/Cargo.toml @@ -10,8 +10,8 @@ cargo-fuzz = true [dependencies] hash-db = { path = "../../hash-db", version = "0.15.2" } -memory-db = { path = "../../memory-db", version = "0.18.1" } -reference-trie = { path = "../../test-support/reference-trie", version = "0.19.0" } +memory-db = { path = "../../memory-db", version = "0.20.1" } +reference-trie = { path = "../../test-support/reference-trie", version = "0.20.1" } keccak-hasher = { path = "../../test-support/keccak-hasher", version = "0.15.2" } [dependencies.trie-db] @@ -28,11 +28,6 @@ members = ["."] name = "trie_root_new" path = "fuzz_targets/trie_root_new.rs" -[[bin]] -name = "trie_unhashed_no_ext" -path = "fuzz_targets/trie_unhashed_no_ext.rs" - - [[bin]] name = "trie_root" path = "fuzz_targets/trie_root.rs" @@ -61,6 +56,10 @@ path = "fuzz_targets/seek_iter.rs" name = "trie_proof_valid" path = "fuzz_targets/trie_proof_valid.rs" +[[bin]] +name = "trie_codec_proof" +path = "fuzz_targets/trie_codec_proof.rs" + [[bin]] name = "trie_proof_invalid" path = "fuzz_targets/trie_proof_invalid.rs" diff --git a/trie-db/fuzz/fuzz_targets/no_ext_insert.rs b/trie-db/fuzz/fuzz_targets/no_ext_insert.rs index 4922e057..149d59da 100644 --- a/trie-db/fuzz/fuzz_targets/no_ext_insert.rs +++ b/trie-db/fuzz/fuzz_targets/no_ext_insert.rs @@ -5,5 +5,5 @@ use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { // fuzzed code goes here - fuzz_that_no_extension_insert(data); + fuzz_that_no_extension_insert::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/no_ext_insert_rem.rs b/trie-db/fuzz/fuzz_targets/no_ext_insert_rem.rs index 608e25fc..1b40103a 100644 --- a/trie-db/fuzz/fuzz_targets/no_ext_insert_rem.rs +++ b/trie-db/fuzz/fuzz_targets/no_ext_insert_rem.rs @@ -5,5 +5,5 @@ use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { // fuzzed code goes here - fuzz_that_no_extension_insert_remove(data); + fuzz_that_no_extension_insert_remove::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/prefix_iter.rs b/trie-db/fuzz/fuzz_targets/prefix_iter.rs index 612989d2..3c2aa999 100644 --- a/trie-db/fuzz/fuzz_targets/prefix_iter.rs +++ b/trie-db/fuzz/fuzz_targets/prefix_iter.rs @@ -4,5 +4,5 @@ use trie_db_fuzz::fuzz_prefix_iter; use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { - fuzz_prefix_iter(data); + fuzz_prefix_iter::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/seek_iter.rs b/trie-db/fuzz/fuzz_targets/seek_iter.rs index f303b283..ef2116f4 100644 --- a/trie-db/fuzz/fuzz_targets/seek_iter.rs +++ b/trie-db/fuzz/fuzz_targets/seek_iter.rs @@ -4,5 +4,5 @@ use trie_db_fuzz::fuzz_seek_iter; use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { - fuzz_seek_iter(data); + fuzz_seek_iter::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/trie_codec_proof.rs b/trie-db/fuzz/fuzz_targets/trie_codec_proof.rs new file mode 100644 index 00000000..ba7e92b6 --- /dev/null +++ b/trie-db/fuzz/fuzz_targets/trie_codec_proof.rs @@ -0,0 +1,8 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use trie_db_fuzz::fuzz_that_trie_codec_proofs; + +fuzz_target!(|data: &[u8]| { + fuzz_that_trie_codec_proofs::(data); +}); diff --git a/trie-db/fuzz/fuzz_targets/trie_proof_invalid.rs b/trie-db/fuzz/fuzz_targets/trie_proof_invalid.rs index 565e7eb9..3112e353 100644 --- a/trie-db/fuzz/fuzz_targets/trie_proof_invalid.rs +++ b/trie-db/fuzz/fuzz_targets/trie_proof_invalid.rs @@ -5,5 +5,5 @@ use libfuzzer_sys::fuzz_target; use trie_db_fuzz::fuzz_that_verify_rejects_invalid_proofs; fuzz_target!(|data: &[u8]| { - fuzz_that_verify_rejects_invalid_proofs(data); -}); \ No newline at end of file + fuzz_that_verify_rejects_invalid_proofs::(data); +}); diff --git a/trie-db/fuzz/fuzz_targets/trie_proof_valid.rs b/trie-db/fuzz/fuzz_targets/trie_proof_valid.rs index 44f2b120..2c4141a0 100644 --- a/trie-db/fuzz/fuzz_targets/trie_proof_valid.rs +++ b/trie-db/fuzz/fuzz_targets/trie_proof_valid.rs @@ -5,5 +5,5 @@ use libfuzzer_sys::fuzz_target; use trie_db_fuzz::fuzz_that_verify_accepts_valid_proofs; fuzz_target!(|data: &[u8]| { - fuzz_that_verify_accepts_valid_proofs(data); -}); \ No newline at end of file + fuzz_that_verify_accepts_valid_proofs::(data); +}); diff --git a/trie-db/fuzz/fuzz_targets/trie_root.rs b/trie-db/fuzz/fuzz_targets/trie_root.rs index 32b65f83..75178bc2 100644 --- a/trie-db/fuzz/fuzz_targets/trie_root.rs +++ b/trie-db/fuzz/fuzz_targets/trie_root.rs @@ -5,5 +5,5 @@ use trie_db_fuzz::fuzz_that_reference_trie_root; use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { - fuzz_that_reference_trie_root(data); + fuzz_that_reference_trie_root::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/trie_root_fix_len.rs b/trie-db/fuzz/fuzz_targets/trie_root_fix_len.rs index 00763e26..902933a7 100644 --- a/trie-db/fuzz/fuzz_targets/trie_root_fix_len.rs +++ b/trie-db/fuzz/fuzz_targets/trie_root_fix_len.rs @@ -5,5 +5,5 @@ use trie_db_fuzz::fuzz_that_reference_trie_root_fix_length; use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { - fuzz_that_reference_trie_root_fix_length(data); + fuzz_that_reference_trie_root_fix_length::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/trie_root_new.rs b/trie-db/fuzz/fuzz_targets/trie_root_new.rs index 9e9d41fa..05b6e3a3 100644 --- a/trie-db/fuzz/fuzz_targets/trie_root_new.rs +++ b/trie-db/fuzz/fuzz_targets/trie_root_new.rs @@ -6,5 +6,5 @@ use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { // fuzzed code goes here - fuzz_that_compare_implementations(data); + fuzz_that_compare_implementations::(data); }); diff --git a/trie-db/fuzz/fuzz_targets/trie_unhashed_no_ext.rs b/trie-db/fuzz/fuzz_targets/trie_unhashed_no_ext.rs deleted file mode 100644 index dba4cc68..00000000 --- a/trie-db/fuzz/fuzz_targets/trie_unhashed_no_ext.rs +++ /dev/null @@ -1,10 +0,0 @@ - -#![no_main] - -use trie_db_fuzz::fuzz_that_unhashed_no_extension; -use libfuzzer_sys::fuzz_target; - -fuzz_target!(|data: &[u8]| { - // fuzzed code goes here - fuzz_that_unhashed_no_extension(data); -}); diff --git a/trie-db/fuzz/src/lib.rs b/trie-db/fuzz/src/lib.rs index 4b9d0efc..461b2d71 100644 --- a/trie-db/fuzz/src/lib.rs +++ b/trie-db/fuzz/src/lib.rs @@ -14,22 +14,17 @@ use hash_db::Hasher; -use keccak_hasher::KeccakHasher; use memory_db::{HashKey, MemoryDB, PrefixedKey}; use reference_trie::{ - calc_root_no_extension, - compare_no_extension_insert_remove, - ExtensionLayout, - NoExtensionLayout, + calc_root, proof::{generate_proof, verify_proof}, - reference_trie_root, - RefTrieDBMut, - RefTrieDBMutNoExt, - RefTrieDBNoExt, + reference_trie_root_iter_build as reference_trie_root, + TrieDBMut, TrieDBIterator, + compare_insert_remove, }; use std::convert::TryInto; -use trie_db::{DBValue, Trie, TrieDB, TrieDBMut, TrieLayout, TrieMut}; +use trie_db::{DBValue, Trie, TrieDB, TrieLayout, TrieMut}; fn fuzz_to_data(input: &[u8]) -> Vec<(Vec,Vec)> { let mut result = Vec::new(); @@ -94,26 +89,26 @@ fn fuzz_removal(data: Vec<(Vec,Vec)>) -> Vec<(bool, Vec,Vec)> { res } -pub fn fuzz_that_reference_trie_root(input: &[u8]) { +pub fn fuzz_that_reference_trie_root(input: &[u8]) { let data = data_sorted_unique(fuzz_to_data(input)); let mut memdb = MemoryDB::<_, HashKey<_>, _>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for a in 0..data.len() { t.insert(&data[a].0[..], &data[a].1[..]).unwrap(); } - assert_eq!(*t.root(), reference_trie_root(data)); + assert_eq!(*t.root(), reference_trie_root::(data)); } -pub fn fuzz_that_reference_trie_root_fix_length(input: &[u8]) { +pub fn fuzz_that_reference_trie_root_fix_length(input: &[u8]) { let data = data_sorted_unique(fuzz_to_data_fix_length(input)); let mut memdb = MemoryDB::<_, HashKey<_>, _>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for a in 0..data.len() { t.insert(&data[a].0[..], &data[a].1[..]).unwrap(); } - assert_eq!(*t.root(), reference_trie_root(data)); + assert_eq!(*t.root(), reference_trie_root::(data)); } fn fuzz_to_data_fix_length(input: &[u8]) -> Vec<(Vec,Vec)> { @@ -141,25 +136,20 @@ fn data_sorted_unique(input: Vec<(Vec,Vec)>) -> Vec<(Vec,Vec)> { m.into_iter().collect() } -pub fn fuzz_that_compare_implementations(input: &[u8]) { +pub fn fuzz_that_compare_implementations(input: &[u8]) { let data = data_sorted_unique(fuzz_to_data(input)); //println!("data:{:?}", &data); let memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); - let hashdb = MemoryDB::, DBValue>::default(); - reference_trie::compare_implementations(data, memdb, hashdb); + let hashdb = MemoryDB::, DBValue>::default(); + reference_trie::compare_implementations::(data, memdb, hashdb); } -pub fn fuzz_that_unhashed_no_extension(input: &[u8]) { - let data = data_sorted_unique(fuzz_to_data(input)); - reference_trie::compare_unhashed_no_extension(data); -} - -pub fn fuzz_that_no_extension_insert(input: &[u8]) { +pub fn fuzz_that_no_extension_insert(input: &[u8]) { let data = fuzz_to_data(input); //println!("data{:?}", data); let mut memdb = MemoryDB::<_, HashKey<_>, _>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for a in 0..data.len() { t.insert(&data[a].0[..], &data[a].1[..]).unwrap(); } @@ -167,24 +157,24 @@ pub fn fuzz_that_no_extension_insert(input: &[u8]) { // before. let data = data_sorted_unique(fuzz_to_data(input)); //println!("data{:?}", data); - assert_eq!(*t.root(), calc_root_no_extension(data)); + assert_eq!(*t.root(), calc_root::(data)); } -pub fn fuzz_that_no_extension_insert_remove(input: &[u8]) { +pub fn fuzz_that_no_extension_insert_remove(input: &[u8]) { let data = fuzz_to_data(input); let data = fuzz_removal(data); let memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); - compare_no_extension_insert_remove(data, memdb); + compare_insert_remove::(data, memdb); } -pub fn fuzz_seek_iter(input: &[u8]) { +pub fn fuzz_seek_iter(input: &[u8]) { let data = data_sorted_unique(fuzz_to_data_fix_length(input)); let mut memdb = MemoryDB::<_, HashKey<_>, _>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for a in 0..data.len() { t.insert(&data[a].0[..], &data[a].1[..]).unwrap(); } @@ -203,7 +193,7 @@ pub fn fuzz_seek_iter(input: &[u8]) { let mut iter_res = Vec::new(); let mut error = 0; { - let trie = RefTrieDBNoExt::new(&memdb, &root).unwrap(); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = trie.iter().unwrap(); if let Ok(_) = iter.seek(prefix) { } else { @@ -227,13 +217,13 @@ pub fn fuzz_seek_iter(input: &[u8]) { assert_eq!(error, 0); } -pub fn fuzz_prefix_iter(input: &[u8]) { +pub fn fuzz_prefix_iter(input: &[u8]) { let data = data_sorted_unique(fuzz_to_data_fix_length(input)); let mut memdb = MemoryDB::<_, HashKey<_>, _>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for a in 0..data.len() { t.insert(&data[a].0[..], &data[a].1[..]).unwrap(); } @@ -252,7 +242,7 @@ pub fn fuzz_prefix_iter(input: &[u8]) { let mut iter_res = Vec::new(); let mut error = 0; { - let trie = RefTrieDBNoExt::new(&memdb, &root).unwrap(); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let iter = TrieDBIterator::new_prefixed(&trie, prefix).unwrap(); for x in iter { @@ -273,7 +263,7 @@ pub fn fuzz_prefix_iter(input: &[u8]) { assert_eq!(error, 0); } -pub fn fuzz_that_verify_accepts_valid_proofs(input: &[u8]) { +pub fn fuzz_that_verify_accepts_valid_proofs(input: &[u8]) { let mut data = fuzz_to_data(input); // Split data into 3 parts: // - the first 1/3 is added to the trie and not included in the proof @@ -289,11 +279,30 @@ pub fn fuzz_that_verify_accepts_valid_proofs(input: &[u8]) { keys.sort(); keys.dedup(); - let (root, proof, items) = test_generate_proof::(data, keys); - assert!(verify_proof::(&root, &proof, items.iter()).is_ok()); + let (root, proof, items) = test_generate_proof::(data, keys); + assert!(verify_proof::(&root, &proof, items.iter()).is_ok()); } -pub fn fuzz_that_verify_rejects_invalid_proofs(input: &[u8]) { +pub fn fuzz_that_trie_codec_proofs(input: &[u8]) { + let mut data = fuzz_to_data(input); + // Split data into 3 parts: + // - the first 1/3 is added to the trie and not included in the proof + // - the second 1/3 is added to the trie and included in the proof + // - the last 1/3 is not added to the trie and the proof proves non-inclusion of them + let mut keys = data[(data.len() / 3)..] + .iter() + .map(|(key, _)| key.clone()) + .collect::>(); + data.truncate(data.len() * 2 / 3); + + let data = data_sorted_unique(data); + keys.sort(); + keys.dedup(); + + test_trie_codec_proof::(data, keys); +} + +pub fn fuzz_that_verify_rejects_invalid_proofs(input: &[u8]) { if input.len() < 4 { return; } @@ -321,7 +330,7 @@ pub fn fuzz_that_verify_rejects_invalid_proofs(input: &[u8]) { return; } - let (root, proof, mut items) = test_generate_proof::(data, keys); + let (root, proof, mut items) = test_generate_proof::(data, keys); // Make one item at random incorrect. let items_idx = random_int % items.len(); @@ -330,7 +339,7 @@ pub fn fuzz_that_verify_rejects_invalid_proofs(input: &[u8]) { (_, value) if value.is_some() => *value = None, (_, value) => *value = Some(DBValue::new()), } - assert!(verify_proof::(&root, &proof, items.iter()).is_err()); + assert!(verify_proof::(&root, &proof, items.iter()).is_err()); } fn test_generate_proof( @@ -363,3 +372,65 @@ fn test_generate_proof( (root, proof, items) } + +fn test_trie_codec_proof( + entries: Vec<(Vec, Vec)>, + keys: Vec>, +) +{ + use hash_db::{HashDB, EMPTY_PREFIX}; + use reference_trie::{ + Recorder, + encode_compact, decode_compact, + }; + + // Populate DB with full trie from entries. + let (db, root) = { + let mut db = , _>>::default(); + let mut root = Default::default(); + { + let mut trie = >::new(&mut db, &mut root); + for (key, value) in entries { + trie.insert(&key, &value).unwrap(); + } + } + (db, root) + }; + let expected_root = root; + // Lookup items in trie while recording traversed nodes. + let mut recorder = Recorder::new(); + let items = { + let mut items = Vec::with_capacity(keys.len()); + let trie = >::new(&db, &root).unwrap(); + for key in keys { + let value = trie.get_with(key.as_slice(), &mut recorder).unwrap(); + items.push((key, value)); + } + items + }; + + // Populate a partial trie DB with recorded nodes. + let mut partial_db = , _>>::default(); + for record in recorder.drain() { + partial_db.emplace(record.hash, EMPTY_PREFIX, record.data); + } + + // Compactly encode the partial trie DB. + let compact_trie = { + let trie = >::new(&partial_db, &root).unwrap(); + encode_compact::(&trie).unwrap() + }; + + let expected_used = compact_trie.len(); + // Reconstruct the partial DB from the compact encoding. + let mut db = , _>>::default(); + let (root, used) = decode_compact::(&mut db, &compact_trie).unwrap(); + assert_eq!(root, expected_root); + assert_eq!(used, expected_used); + + // Check that lookups for all items succeed. + let trie = >::new(&db, &root).unwrap(); + for (key, expected_value) in items { + assert_eq!(trie.get(key.as_slice()).unwrap(), expected_value); + } +} diff --git a/trie-db/src/fatdb.rs b/trie-db/src/fatdb.rs index b3f43138..7ec93c80 100644 --- a/trie-db/src/fatdb.rs +++ b/trie-db/src/fatdb.rs @@ -130,12 +130,11 @@ where mod test { use memory_db::{MemoryDB, HashKey}; use crate::DBValue; - use keccak_hasher::KeccakHasher; - use reference_trie::{RefFatDBMut, RefFatDB, Trie, TrieMut}; + use reference_trie::{RefFatDBMut, RefFatDB, Trie, TrieMut, RefHasher}; #[test] fn fatdb_to_trie() { - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { let mut t = RefFatDBMut::new(&mut memdb, &mut root); diff --git a/trie-db/src/fatdbmut.rs b/trie-db/src/fatdbmut.rs index cf45448d..dc10238c 100644 --- a/trie-db/src/fatdbmut.rs +++ b/trie-db/src/fatdbmut.rs @@ -111,12 +111,11 @@ where mod test { use memory_db::{MemoryDB, HashKey}; use hash_db::{Hasher, EMPTY_PREFIX}; - use keccak_hasher::KeccakHasher; - use reference_trie::{RefFatDBMut, RefTrieDB, Trie, TrieMut}; + use reference_trie::{RefFatDBMut, RefTrieDB, Trie, TrieMut, RefHasher}; #[test] fn fatdbmut_to_trie() { - let mut memdb = MemoryDB::, _>::default(); + let mut memdb = MemoryDB::, _>::default(); let mut root = Default::default(); { let mut t = RefFatDBMut::new(&mut memdb, &mut root); @@ -124,19 +123,19 @@ mod test { } let t = RefTrieDB::new(&memdb, &root).unwrap(); assert_eq!( - t.get(&KeccakHasher::hash(&[0x01u8, 0x23])), + t.get(&RefHasher::hash(&[0x01u8, 0x23])), Ok(Some(vec![0x01u8, 0x23])), ); } #[test] fn fatdbmut_insert_remove_key_mapping() { - let mut memdb = MemoryDB::, _>::default(); + let mut memdb = MemoryDB::, _>::default(); let mut root = Default::default(); let key = [0x01u8, 0x23]; let val = [0x01u8, 0x24]; - let key_hash = KeccakHasher::hash(&key); - let aux_hash = KeccakHasher::hash(&key_hash); + let key_hash = RefHasher::hash(&key); + let aux_hash = RefHasher::hash(&key_hash); let mut t = RefFatDBMut::new(&mut memdb, &mut root); t.insert(&key, &val).unwrap(); assert_eq!(t.get(&key), Ok(Some(val.to_vec()))); diff --git a/trie-db/src/iter_build.rs b/trie-db/src/iter_build.rs index 0d7e949b..2f7e3b45 100644 --- a/trie-db/src/iter_build.rs +++ b/trie-db/src/iter_build.rs @@ -476,7 +476,8 @@ impl ProcessEncodedNode<::Out> for TrieRootUnhashed { mod test { use crate::DBValue; use memory_db::{MemoryDB, HashKey, PrefixedKey}; - use keccak_hasher::KeccakHasher; + use reference_trie::{RefHasher, TrieLayout, test_layouts}; + use reference_trie::{ExtensionLayout, NoExtensionLayout}; #[test] fn trie_root_empty () { @@ -498,46 +499,20 @@ mod test { ]); } - fn test_iter(data: Vec<(Vec, Vec)>) { - use reference_trie::{RefTrieDBMut, TrieMut, RefTrieDB, Trie}; + fn test_iter(data: Vec<(Vec, Vec)>) { + use reference_trie::{TrieMut, Trie, TrieDBMut, TrieDB}; - let mut db = MemoryDB::, DBValue>::default(); + let mut db = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut db, &mut root); + let mut t = TrieDBMut::::new(&mut db, &mut root); for i in 0..data.len() { let key: &[u8]= &data[i].0; let value: &[u8] = &data[i].1; t.insert(key, value).unwrap(); } } - let t = RefTrieDB::new(&db, &root).unwrap(); - for (i, kv) in t.iter().unwrap().enumerate() { - let (k, v) = kv.unwrap(); - let key: &[u8]= &data[i].0; - let value: &[u8] = &data[i].1; - assert_eq!(k, key); - assert_eq!(v, value); - } - for (k, v) in data.into_iter() { - assert_eq!(&t.get(&k[..]).unwrap().unwrap()[..], &v[..]); - } - } - - fn test_iter_no_extension(data: Vec<(Vec, Vec)>) { - use reference_trie::{RefTrieDBMutNoExt, TrieMut, RefTrieDBNoExt, Trie}; - - let mut db = MemoryDB::, DBValue>::default(); - let mut root = Default::default(); - { - let mut t = RefTrieDBMutNoExt::new(&mut db, &mut root); - for i in 0..data.len() { - let key: &[u8]= &data[i].0; - let value: &[u8] = &data[i].1; - t.insert(key, value).unwrap(); - } - } - let t = RefTrieDBNoExt::new(&db, &root).unwrap(); + let t = TrieDB::::new(&db, &root).unwrap(); for (i, kv) in t.iter().unwrap().enumerate() { let (k, v) = kv.unwrap(); let key: &[u8]= &data[i].0; @@ -551,52 +526,42 @@ mod test { } fn compare_implementations(data: Vec<(Vec, Vec)>) { - test_iter(data.clone()); - test_iter_no_extension(data.clone()); + test_iter::(data.clone()); + test_iter::(data.clone()); compare_implementations_h(data.clone()); compare_implementations_prefixed(data.clone()); - compare_implementations_no_extension(data.clone()); - compare_implementations_no_extension_prefixed(data.clone()); } fn compare_implementations_prefixed(data: Vec<(Vec, Vec)>) { + compare_implementations_prefixed_internal::(data.clone()); + compare_implementations_prefixed_internal::(data.clone()); + } + fn compare_implementations_prefixed_internal(data: Vec<(Vec, Vec)>) { let memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); - let hashdb = MemoryDB::, DBValue>::default(); - reference_trie::compare_implementations(data, memdb, hashdb); + let hashdb = MemoryDB::, DBValue>::default(); + reference_trie::compare_implementations::(data, memdb, hashdb); } fn compare_implementations_h(data: Vec<(Vec, Vec)>) { - let memdb = MemoryDB::<_, HashKey<_>, _>::default(); - let hashdb = MemoryDB::, DBValue>::default(); - reference_trie::compare_implementations(data, memdb, hashdb); + compare_implementations_h_internal::(data.clone()); + compare_implementations_h_internal::(data.clone()); } - fn compare_implementations_no_extension(data: Vec<(Vec, Vec)>) { + fn compare_implementations_h_internal(data: Vec<(Vec, Vec)>) { let memdb = MemoryDB::<_, HashKey<_>, _>::default(); - let hashdb = MemoryDB::, DBValue>::default(); - reference_trie::compare_implementations_no_extension(data, memdb, hashdb); - } - fn compare_implementations_no_extension_prefixed(data: Vec<(Vec, Vec)>) { - let memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); - let hashdb = MemoryDB::, DBValue>::default(); - reference_trie::compare_implementations_no_extension(data, memdb, hashdb); + let hashdb = MemoryDB::, DBValue>::default(); + reference_trie::compare_implementations::(data.clone(), memdb, hashdb); } fn compare_implementations_no_extension_unordered(data: Vec<(Vec, Vec)>) { let memdb = MemoryDB::<_, HashKey<_>, _>::default(); - let hashdb = MemoryDB::, DBValue>::default(); - reference_trie::compare_implementations_no_extension_unordered(data, memdb, hashdb); + let hashdb = MemoryDB::, DBValue>::default(); + reference_trie::compare_implementations_unordered::(data, memdb, hashdb); } - fn compare_no_extension_insert_remove(data: Vec<(bool, Vec, Vec)>) { + fn compare_insert_remove(data: Vec<(bool, Vec, Vec)>) { let memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); - reference_trie::compare_no_extension_insert_remove(data, memdb); + reference_trie::compare_insert_remove::(data, memdb); } - fn compare_root(data: Vec<(Vec, Vec)>) { - let memdb = MemoryDB::<_, HashKey<_>, _>::default(); - reference_trie::compare_root(data, memdb); - } - fn compare_unhashed(data: Vec<(Vec, Vec)>) { - reference_trie::compare_unhashed(data); - } - fn compare_unhashed_no_extension(data: Vec<(Vec, Vec)>) { - reference_trie::compare_unhashed_no_extension(data); + fn compare_root(data: Vec<(Vec, Vec)>) { + let memdb = MemoryDB::, _>::default(); + reference_trie::compare_root::(data, memdb); } // Following tests are a bunch of detected issue here for non regression. @@ -618,31 +583,11 @@ mod test { (vec![1u8, 2u8, 3u8, 5u8, 3u8], vec![7u8;32]), ]); } - #[test] - fn root_extension_bis () { - compare_root(vec![ - (vec![1u8, 2u8, 3u8, 3u8], vec![8u8;32]), - (vec![1u8, 2u8, 3u8, 4u8], vec![7u8;32]), - ]); - } - #[test] - fn root_extension_tierce () { - let d = vec![ - (vec![1u8, 2u8, 3u8, 3u8], vec![8u8;2]), - (vec![1u8, 2u8, 3u8, 4u8], vec![7u8;2]), - ]; - compare_unhashed(d.clone()); - compare_unhashed_no_extension(d); - } - #[test] - fn root_extension_tierce_big () { - // on more content unhashed would hash - compare_unhashed(vec![ + test_layouts!(root_extension_bis, root_extension_bis_internal); + fn root_extension_bis_internal() { + compare_root::(vec![ (vec![1u8, 2u8, 3u8, 3u8], vec![8u8;32]), (vec![1u8, 2u8, 3u8, 4u8], vec![7u8;32]), - (vec![1u8, 6u8, 3u8, 3u8], vec![8u8;32]), - (vec![6u8, 2u8, 3u8, 3u8], vec![8u8;32]), - (vec![6u8, 2u8, 3u8, 13u8], vec![8u8;32]), ]); } #[test] @@ -724,31 +669,31 @@ mod test { } #[test] fn fuzz_no_extension4 () { - compare_implementations_no_extension(vec![ + compare_implementations(vec![ (vec![0x01, 0x56], vec![0x1]), (vec![0x02, 0x42], vec![0x2]), (vec![0x02, 0x50], vec![0x3]), ]); } - #[test] - fn fuzz_no_extension_insert_remove_1 () { + test_layouts!(fuzz_no_extension_insert_remove_1, fuzz_no_extension_insert_remove_1_internal); + fn fuzz_no_extension_insert_remove_1_internal() { let data = vec![ (false, vec![0], vec![251, 255]), (false, vec![0, 1], vec![251, 255]), (false, vec![0, 1, 2], vec![255; 32]), (true, vec![0, 1], vec![0, 251]), ]; - compare_no_extension_insert_remove(data); + compare_insert_remove::(data); } - #[test] - fn fuzz_no_extension_insert_remove_2 () { + test_layouts!(fuzz_no_extension_insert_remove_2, fuzz_no_extension_insert_remove_2_internal); + fn fuzz_no_extension_insert_remove_2_internal() { let data = vec![ (false, vec![0x00], vec![0xfd, 0xff]), (false, vec![0x10, 0x00], vec![1;32]), (false, vec![0x11, 0x10], vec![0;32]), (true, vec![0x10, 0x00], vec![]) ]; - compare_no_extension_insert_remove(data); + compare_insert_remove::(data); } #[test] fn two_bytes_nibble_length () { @@ -756,21 +701,23 @@ mod test { (vec![00u8], vec![0]), (vec![01u8;64], vec![0;32]), ]; - compare_implementations_no_extension(data.clone()); - compare_implementations_no_extension_prefixed(data.clone()); + compare_implementations_prefixed(data.clone()); } #[test] #[should_panic] fn too_big_nibble_length_old () { - compare_implementations_h(vec![ - (vec![01u8;64], vec![0;32]), - ]); + compare_implementations_prefixed_internal::( + vec![(vec![01u8;64], vec![0;32])], + ); } #[test] fn too_big_nibble_length_new () { - compare_implementations_no_extension(vec![ + // this is valid for no_ext code only, + // the other one got maximal length in encoding. + let data = vec![ (vec![01u8;((u16::max_value() as usize + 1) / 2) + 1], vec![0;32]), - ]); + ]; + compare_implementations_prefixed_internal::(data.clone()); } #[test] fn polka_re_test () { @@ -780,5 +727,4 @@ mod test { (vec![105, 97, 48, 77, 101, 105, 121, 101], vec![69, 109, 111, 111, 82, 49, 97, 105]), ]); } - } diff --git a/trie-db/src/iterator.rs b/trie-db/src/iterator.rs index 6c7207d9..43ac1e10 100644 --- a/trie-db/src/iterator.rs +++ b/trie-db/src/iterator.rs @@ -388,37 +388,21 @@ mod tests { use crate::DBValue; use hex_literal::hex; use hash_db::{HashDB, Hasher}; - use keccak_hasher::KeccakHasher; use reference_trie::{ - RefTrieDB, RefTrieDBMut, + TrieDB, TrieDBMut, TrieError, TrieMut, TrieIterator, TrieDBNodeIterator, NibbleSlice, NibbleVec, - node::Node, + node::Node, TrieLayout, test_layouts, }; - use reference_trie::{RefTrieDBNoExt, RefTrieDBMutNoExt}; - type MemoryDB = memory_db::MemoryDB, DBValue>; + type MemoryDB = memory_db::MemoryDB<::Hash, memory_db::PrefixedKey<::Hash>, DBValue>; - fn build_trie_db_with_extension(pairs: &[(Vec, Vec)]) - -> (MemoryDB, ::Out) + fn build_trie_db(pairs: &[(Vec, Vec)]) + -> (MemoryDB, ::Out) { - let mut memdb = MemoryDB::default(); + let mut memdb = MemoryDB::::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); - for (x, y) in pairs.iter() { - t.insert(x, y).unwrap(); - } - } - (memdb, root) - } - - fn build_trie_db_without_extension(pairs: &[(Vec, Vec)]) - -> (MemoryDB, ::Out) - { - let mut memdb = MemoryDB::default(); - let mut root = Default::default(); - { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for (x, y) in pairs.iter() { t.insert(x, y).unwrap(); } @@ -436,113 +420,76 @@ mod tests { v } - #[test] - fn iterator_works_with_extension() { + test_layouts!(iterator_works, iterator_works_internal); + fn iterator_works_internal() { let pairs = vec![ (hex!("01").to_vec(), b"aaaa".to_vec()), (hex!("0123").to_vec(), b"bbbb".to_vec()), (hex!("02").to_vec(), vec![1; 32]), ]; - let (memdb, root) = build_trie_db_with_extension(&pairs); - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let (memdb, root) = build_trie_db::(&pairs); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); - match iter.next() { - Some(Ok((prefix, Some(_), node))) => { - assert_eq!(prefix, nibble_vec(hex!(""), 0)); - match node.node() { - Node::Extension(partial, _) => - assert_eq!(partial, NibbleSlice::new_offset(&hex!("00")[..], 1)), - _ => panic!("unexpected node"), - } - } - _ => panic!("unexpected item"), - } - - match iter.next() { - Some(Ok((prefix, Some(_), node))) => { - assert_eq!(prefix, nibble_vec(hex!("00"), 1)); - match node.node() { - Node::Branch(_, _) => {}, - _ => panic!("unexpected node"), + if T::USE_EXTENSION { + match iter.next() { + Some(Ok((prefix, Some(_), node))) => { + assert_eq!(prefix, nibble_vec(hex!(""), 0)); + match node.node() { + Node::Extension(partial, _) => + assert_eq!(partial, NibbleSlice::new_offset(&hex!("00")[..], 1)), + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), - } - match iter.next() { - Some(Ok((prefix, None, node))) => { - assert_eq!(prefix, nibble_vec(hex!("01"), 2)); - match node.node() { - Node::Branch(_, _) => {}, - _ => panic!("unexpected node"), + match iter.next() { + Some(Ok((prefix, Some(_), node))) => { + assert_eq!(prefix, nibble_vec(hex!("00"), 1)); + match node.node() { + Node::Branch(_, _) => {}, + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), - } - match iter.next() { - Some(Ok((prefix, None, node))) => { - assert_eq!(prefix, nibble_vec(hex!("0120"), 3)); - match node.node() { - Node::Leaf(partial, _) => - assert_eq!(partial, NibbleSlice::new_offset(&hex!("03")[..], 1)), - _ => panic!("unexpected node"), - } - } - _ => panic!("unexpected item"), - } - - match iter.next() { - Some(Ok((prefix, Some(_), node))) => { - assert_eq!(prefix, nibble_vec(hex!("02"), 2)); - match node.node() { - Node::Leaf(partial, _) => - assert_eq!(partial, NibbleSlice::new(&hex!("")[..])), - _ => panic!("unexpected node"), + match iter.next() { + Some(Ok((prefix, None, node))) => { + assert_eq!(prefix, nibble_vec(hex!("01"), 2)); + match node.node() { + Node::Branch(_, _) => {}, + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), - } - - assert!(iter.next().is_none()); - } - - #[test] - fn iterator_works_without_extension() { - let pairs = vec![ - (hex!("01").to_vec(), b"aaaa".to_vec()), - (hex!("0123").to_vec(), b"bbbb".to_vec()), - (hex!("02").to_vec(), vec![1; 32]), - ]; - - let (memdb, root) = build_trie_db_without_extension(&pairs); - let trie = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); - - match iter.next() { - Some(Ok((prefix, Some(_), node))) => { - assert_eq!(prefix, nibble_vec(hex!(""), 0)); - match node.node() { - Node::NibbledBranch(partial, _, _) => - assert_eq!(partial, NibbleSlice::new_offset(&hex!("00")[..], 1)), - _ => panic!("unexpected node"), + } else { + match iter.next() { + Some(Ok((prefix, Some(_), node))) => { + assert_eq!(prefix, nibble_vec(hex!(""), 0)); + match node.node() { + Node::NibbledBranch(partial, _, _) => + assert_eq!(partial, NibbleSlice::new_offset(&hex!("00")[..], 1)), + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), - } - match iter.next() { - Some(Ok((prefix, None, node))) => { - assert_eq!(prefix, nibble_vec(hex!("01"), 2)); - match node.node() { - Node::NibbledBranch(partial, _, _) => - assert_eq!(partial, NibbleSlice::new(&hex!("")[..])), - _ => panic!("unexpected node"), + match iter.next() { + Some(Ok((prefix, None, node))) => { + assert_eq!(prefix, nibble_vec(hex!("01"), 2)); + match node.node() { + Node::NibbledBranch(partial, _, _) => + assert_eq!(partial, NibbleSlice::new(&hex!("")[..])), + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), } match iter.next() { @@ -554,7 +501,6 @@ mod tests { _ => panic!("unexpected node"), } } - _ => panic!("unexpected item"), } @@ -573,10 +519,10 @@ mod tests { assert!(iter.next().is_none()); } - #[test] - fn iterator_over_empty_works() { - let (memdb, root) = build_trie_db_with_extension(&[]); - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + test_layouts!(iterator_over_empty_works, iterator_over_empty_works_internal); + fn iterator_over_empty_works_internal() { + let (memdb, root) = build_trie_db::(&[]); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); match iter.next() { @@ -593,16 +539,16 @@ mod tests { assert!(iter.next().is_none()); } - #[test] - fn seek_works_with_extension() { + test_layouts!(seek_works, seek_works_internal); + fn seek_works_internal() { let pairs = vec![ (hex!("01").to_vec(), b"aaaa".to_vec()), (hex!("0123").to_vec(), b"bbbb".to_vec()), (hex!("02").to_vec(), vec![1; 32]), ]; - let (memdb, root) = build_trie_db_with_extension(&pairs); - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let (memdb, root) = build_trie_db::(&pairs); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); TrieIterator::seek(&mut iter, &hex!("")[..]).unwrap(); @@ -637,55 +583,10 @@ mod tests { assert!(iter.next().is_none()); } - - #[test] - fn seek_works_without_extension() { - let pairs = vec![ - (hex!("01").to_vec(), b"aaaa".to_vec()), - (hex!("0123").to_vec(), b"bbbb".to_vec()), - (hex!("02").to_vec(), vec![1; 32]), - ]; - - let (memdb, root) = build_trie_db_without_extension(&pairs); - let trie = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); - - TrieIterator::seek(&mut iter, &hex!("")[..]).unwrap(); - match iter.next() { - Some(Ok((prefix, _, _))) => - assert_eq!(prefix, nibble_vec(hex!(""), 0)), - _ => panic!("unexpected item"), - } - - TrieIterator::seek(&mut iter, &hex!("00")[..]).unwrap(); - match iter.next() { - Some(Ok((prefix, _, _))) => - assert_eq!(prefix, nibble_vec(hex!("01"), 2)), - _ => panic!("unexpected item"), - } - - TrieIterator::seek(&mut iter, &hex!("01")[..]).unwrap(); - match iter.next() { - Some(Ok((prefix, _, _))) => - assert_eq!(prefix, nibble_vec(hex!("01"), 2)), - _ => panic!("unexpected item"), - } - - TrieIterator::seek(&mut iter, &hex!("02")[..]).unwrap(); - match iter.next() { - Some(Ok((prefix, _, _))) => - assert_eq!(prefix, nibble_vec(hex!("02"), 2)), - _ => panic!("unexpected item"), - } - - TrieIterator::seek(&mut iter, &hex!("03")[..]).unwrap(); - assert!(iter.next().is_none()); - } - - #[test] - fn seek_over_empty_works() { - let (memdb, root) = build_trie_db_with_extension(&[]); - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + test_layouts!(seek_over_empty_works, seek_over_empty_works_internal); + fn seek_over_empty_works_internal() { + let (memdb, root) = build_trie_db::(&[]); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); TrieIterator::seek(&mut iter, &hex!("")[..]).unwrap(); @@ -704,8 +605,8 @@ mod tests { assert!(iter.next().is_none()); } - #[test] - fn iterate_over_incomplete_db() { + test_layouts!(iterate_over_incomplete_db, iterate_over_incomplete_db_internal); + fn iterate_over_incomplete_db_internal() { let pairs = vec![ (hex!("01").to_vec(), b"aaaa".to_vec()), (hex!("0123").to_vec(), b"bbbb".to_vec()), @@ -713,11 +614,11 @@ mod tests { (hex!("03").to_vec(), vec![2; 32]), ]; - let (mut memdb, root) = build_trie_db_with_extension(&pairs); + let (mut memdb, root) = build_trie_db::(&pairs); // Look up the leaf node with prefix "02". let leaf_hash = { - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); TrieIterator::seek(&mut iter, &hex!("02")[..]).unwrap(); @@ -738,23 +639,32 @@ mod tests { // Seek to missing node returns error. { - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); match TrieIterator::seek(&mut iter, &hex!("02")[..]) { - Err(ref err) if **err == TrieError::IncompleteDatabase(leaf_hash) => {}, + Err(e) => { + if let TrieError::IncompleteDatabase(err_hash) = *e { + assert_eq!(err_hash.as_ref(), leaf_hash.as_ref()); + } + }, _ => panic!("expected IncompleteDatabase error"), } } // Iterate over missing node works. { - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); TrieIterator::seek(&mut iter, &hex!("0130")[..]).unwrap(); match iter.next() { - Some(Err(ref err)) if **err == TrieError::IncompleteDatabase(leaf_hash) => {}, + Some(Err(e)) => { + if let TrieError::IncompleteDatabase(err_hash) = *e { + assert_eq!(err_hash.as_ref(), leaf_hash.as_ref()); + } + }, + _ => panic!("expected IncompleteDatabase error"), } match iter.next() { @@ -772,78 +682,43 @@ mod tests { } } - #[test] - fn prefix_works_with_extension() { + test_layouts!(prefix_works, prefix_works_internal); + fn prefix_works_internal() { let pairs = vec![ (hex!("01").to_vec(), b"aaaa".to_vec()), (hex!("0123").to_vec(), b"bbbb".to_vec()), (hex!("02").to_vec(), vec![1; 32]), ]; - let (memdb, root) = build_trie_db_with_extension(&pairs); - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let (memdb, root) = build_trie_db::(&pairs); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); iter.prefix(&hex!("01").to_vec()[..]).unwrap(); - match iter.next() { - Some(Ok((prefix, None, node))) => { - assert_eq!(prefix, nibble_vec(hex!("01"), 2)); - match node.node() { - Node::Branch(_, _) => {}, - _ => panic!("unexpected node"), - } - } - _ => panic!("unexpected item"), - } - - match iter.next() { - Some(Ok((prefix, None, node))) => { - assert_eq!(prefix, nibble_vec(hex!("0120"), 3)); - match node.node() { - Node::Leaf(partial, _) => - assert_eq!(partial, NibbleSlice::new_offset(&hex!("03")[..], 1)), - _ => panic!("unexpected node"), + if T::USE_EXTENSION { + match iter.next() { + Some(Ok((prefix, None, node))) => { + assert_eq!(prefix, nibble_vec(hex!("01"), 2)); + match node.node() { + Node::Branch(_, _) => {}, + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), - } - - assert!(iter.next().is_none()); - - let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); - iter.prefix(&hex!("0010").to_vec()[..]).unwrap(); - assert!(iter.next().is_none()); - let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); - iter.prefix(&hex!("10").to_vec()[..]).unwrap(); - assert!(iter.next().is_none()); - - } - - #[test] - fn prefix_works_without_extension() { - let pairs = vec![ - (hex!("01").to_vec(), b"aaaa".to_vec()), - (hex!("0123").to_vec(), b"bbbb".to_vec()), - (hex!("02").to_vec(), vec![1; 32]), - ]; - - let (memdb, root) = build_trie_db_without_extension(&pairs); - let trie = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); - - iter.prefix(&hex!("01").to_vec()[..]).unwrap(); - - match iter.next() { - Some(Ok((prefix, None, node))) => { - assert_eq!(prefix, nibble_vec(hex!("01"), 2)); - match node.node() { - Node::NibbledBranch(partial, _, _) => - assert_eq!(partial, NibbleSlice::new_offset(&hex!("")[..], 0)), - _ => panic!("unexpected node"), + } else { + match iter.next() { + Some(Ok((prefix, None, node))) => { + assert_eq!(prefix, nibble_vec(hex!("01"), 2)); + match node.node() { + Node::NibbledBranch(partial, _, _) => + assert_eq!(partial, NibbleSlice::new_offset(&hex!("")[..], 0)), + _ => panic!("unexpected node"), + } } + _ => panic!("unexpected item"), } - _ => panic!("unexpected item"), } match iter.next() { @@ -866,13 +741,12 @@ mod tests { let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); iter.prefix(&hex!("10").to_vec()[..]).unwrap(); assert!(iter.next().is_none()); - } - #[test] - fn prefix_over_empty_works() { - let (memdb, root) = build_trie_db_with_extension(&[]); - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + test_layouts!(prefix_over_empty_works, prefix_over_empty_works_internal); + fn prefix_over_empty_works_internal() { + let (memdb, root) = build_trie_db::(&[]); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = TrieDBNodeIterator::new(&trie).unwrap(); iter.prefix(&hex!("")[..]).unwrap(); match iter.next() { @@ -893,4 +767,3 @@ mod tests { assert!(iter.next().is_none()); } } - diff --git a/trie-db/src/recorder.rs b/trie-db/src/recorder.rs index 90057c05..caef1f3b 100644 --- a/trie-db/src/recorder.rs +++ b/trie-db/src/recorder.rs @@ -79,8 +79,7 @@ impl Recorder { mod tests { use memory_db::{MemoryDB, HashKey}; use hash_db::Hasher; - use keccak_hasher::KeccakHasher; - use reference_trie::{RefTrieDB, RefTrieDBMut, Trie, TrieMut, Recorder, Record}; + use reference_trie::{RefTrieDB, RefTrieDBMut, Trie, TrieMut, Recorder, Record, RefHasher}; #[test] fn basic_recorder() { @@ -89,7 +88,7 @@ mod tests { let node1 = vec![1, 2, 3, 4]; let node2 = vec![4, 5, 6, 7, 8, 9, 10]; - let (hash1, hash2) = (KeccakHasher::hash(&node1), KeccakHasher::hash(&node2)); + let (hash1, hash2) = (RefHasher::hash(&node1), RefHasher::hash(&node2)); basic.record(&hash1, &node1, 0); basic.record(&hash2, &node2, 456); @@ -116,8 +115,8 @@ mod tests { let node1 = vec![1, 2, 3, 4]; let node2 = vec![4, 5, 6, 7, 8, 9, 10]; - let hash1 = KeccakHasher::hash(&node1); - let hash2 = KeccakHasher::hash(&node2); + let hash1 = RefHasher::hash(&node1); + let hash2 = RefHasher::hash(&node2); basic.record(&hash1, &node1, 0); basic.record(&hash2, &node2, 456); @@ -134,7 +133,7 @@ mod tests { #[test] fn trie_record() { - let mut db = MemoryDB::, _>::default(); + let mut db = MemoryDB::, _>::default(); let mut root = Default::default(); { let mut x = RefTrieDBMut::new(&mut db, &mut root); diff --git a/trie-db/src/sectriedb.rs b/trie-db/src/sectriedb.rs index a050290c..99c05341 100644 --- a/trie-db/src/sectriedb.rs +++ b/trie-db/src/sectriedb.rs @@ -87,17 +87,16 @@ where mod test { use memory_db::{MemoryDB, HashKey}; use hash_db::Hasher; - use keccak_hasher::KeccakHasher; - use reference_trie::{RefTrieDBMut, RefSecTrieDB, Trie, TrieMut}; + use reference_trie::{RefTrieDBMut, RefSecTrieDB, Trie, TrieMut, RefHasher}; use crate::DBValue; #[test] fn trie_to_sectrie() { - let mut db = MemoryDB::, DBValue>::default(); + let mut db = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { let mut t = RefTrieDBMut::new(&mut db, &mut root); - t.insert(&KeccakHasher::hash(&[0x01u8, 0x23]), &[0x01u8, 0x23]).unwrap(); + t.insert(&RefHasher::hash(&[0x01u8, 0x23]), &[0x01u8, 0x23]).unwrap(); } let t = RefSecTrieDB::new(&db, &root).unwrap(); assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), vec![0x01u8, 0x23]); diff --git a/trie-db/src/sectriedbmut.rs b/trie-db/src/sectriedbmut.rs index 06edf03e..29ac2660 100644 --- a/trie-db/src/sectriedbmut.rs +++ b/trie-db/src/sectriedbmut.rs @@ -92,13 +92,12 @@ where mod test { use memory_db::{MemoryDB, HashKey}; use hash_db::Hasher; - use keccak_hasher::KeccakHasher; - use reference_trie::{RefTrieDB, RefSecTrieDBMut, Trie, TrieMut}; + use reference_trie::{RefTrieDB, RefSecTrieDBMut, Trie, TrieMut, RefHasher}; use crate::DBValue; #[test] fn sectrie_to_trie() { - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { let mut t = RefSecTrieDBMut::new(&mut memdb, &mut root); @@ -106,7 +105,7 @@ mod test { } let t = RefTrieDB::new(&memdb, &root).unwrap(); assert_eq!( - t.get(&KeccakHasher::hash(&[0x01u8, 0x23])).unwrap().unwrap(), + t.get(&RefHasher::hash(&[0x01u8, 0x23])).unwrap().unwrap(), vec![0x01u8, 0x23], ); } diff --git a/trie-db/src/triedb.rs b/trie-db/src/triedb.rs index d05cf035..a8d7723d 100644 --- a/trie-db/src/triedb.rs +++ b/trie-db/src/triedb.rs @@ -332,29 +332,27 @@ impl<'a, L: TrieLayout> Iterator for TrieDBIterator<'a, L> { #[cfg(test)] mod tests { use memory_db::{MemoryDB, PrefixedKey}; - use keccak_hasher::KeccakHasher; use crate::DBValue; - use reference_trie::{RefTrieDB, RefTrieDBMut, RefLookup, Trie, TrieMut, NibbleSlice}; - use reference_trie::{RefTrieDBNoExt, RefTrieDBMutNoExt}; + use reference_trie::{TrieDB, TrieDBMut, Lookup, Trie, TrieMut, NibbleSlice, TrieLayout, test_layouts}; use hex_literal::hex; - #[test] - fn iterator_works() { + test_layouts!(iterator_works, iterator_works_internal); + fn iterator_works_internal() { let pairs = vec![ (hex!("0103000000000000000464").to_vec(), hex!("fffffffffe").to_vec()), (hex!("0103000000000000000469").to_vec(), hex!("ffffffffff").to_vec()), ]; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for (x, y) in &pairs { t.insert(x, y).unwrap(); } } - let trie = RefTrieDB::new(&memdb, &root).unwrap(); + let trie = TrieDB::::new(&memdb, &root).unwrap(); let iter = trie.iter().unwrap(); let mut iter_pairs = Vec::new(); @@ -366,51 +364,23 @@ mod tests { assert_eq!(pairs, iter_pairs); } - #[test] - fn iterator_works_without_extension() { + test_layouts!(iterator_seek_works, iterator_seek_works_internal); + fn iterator_seek_works_internal() { let pairs = vec![ (hex!("0103000000000000000464").to_vec(), hex!("fffffffffe").to_vec()), (hex!("0103000000000000000469").to_vec(), hex!("ffffffffff").to_vec()), ]; - let mut memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for (x, y) in &pairs { t.insert(x, y).unwrap(); } } - let trie = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - - let iter = trie.iter().unwrap(); - let mut iter_pairs = Vec::new(); - for pair in iter { - let (key, value) = pair.unwrap(); - iter_pairs.push((key, value.to_vec())); - } - - assert_eq!(pairs, iter_pairs); - } - - #[test] - fn iterator_seek_works() { - let pairs = vec![ - (hex!("0103000000000000000464").to_vec(), hex!("fffffffffe").to_vec()), - (hex!("0103000000000000000469").to_vec(), hex!("ffffffffff").to_vec()), - ]; - - let mut memdb = MemoryDB::, DBValue>::default(); - let mut root = Default::default(); - { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); - for (x, y) in &pairs { - t.insert(x, y).unwrap(); - } - } - - let t = RefTrieDB::new(&memdb, &root).unwrap(); + let t = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = t.iter().unwrap(); assert_eq!( @@ -437,44 +407,8 @@ mod tests { ); } - #[test] - fn iterator_seek_works_without_extension() { - let pairs = vec![ - (hex!("0103000000000000000464").to_vec(), hex!("fffffffffe").to_vec()), - (hex!("0103000000000000000469").to_vec(), hex!("ffffffffff").to_vec()), - ]; - - let mut memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); - let mut root = Default::default(); - { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); - for (x, y) in &pairs { - t.insert(x, y).unwrap(); - } - } - - let t = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - - let mut iter = t.iter().unwrap(); - assert_eq!( - iter.next().unwrap().unwrap(), - (hex!("0103000000000000000464").to_vec(), hex!("fffffffffe").to_vec()) - ); - iter.seek(&hex!("00")[..]).unwrap(); - assert_eq!( - pairs, - iter.map(|x| x.unwrap()).map(|(k, v)| (k, v[..].to_vec())).collect::>(), - ); - let mut iter = t.iter().unwrap(); - iter.seek(&hex!("0103000000000000000465")[..]).unwrap(); - assert_eq!( - &pairs[1..], - &iter.map(|x| x.unwrap()).map(|(k, v)| (k, v[..].to_vec())).collect::>()[..], - ); - } - - #[test] - fn iterator() { + test_layouts!(iterator, iterator_internal); + fn iterator_internal() { let d = vec![ b"A".to_vec(), b"AA".to_vec(), @@ -482,16 +416,16 @@ mod tests { b"B".to_vec(), ]; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for x in &d { t.insert(x, x).unwrap(); } } - let t = RefTrieDB::new(&memdb, &root).unwrap(); + let t = TrieDB::::new(&memdb, &root).unwrap(); assert_eq!( d.iter() .map(|i| i.clone()) @@ -504,34 +438,8 @@ mod tests { assert_eq!(d, t.iter().unwrap().map(|x| x.unwrap().1).collect::>()); } - #[test] - fn iterator_without_extension() { - let d = vec![ - b"A".to_vec(), - b"AA".to_vec(), - b"AB".to_vec(), - b"B".to_vec(), - ]; - - let mut memdb = MemoryDB::, DBValue>::default(); - let mut root = Default::default(); - { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); - for x in &d { - t.insert(x, x).unwrap(); - } - } - - let t = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - assert_eq!( - d.iter().map(|i| i.clone()).collect::>(), - t.iter().unwrap().map(|x| x.unwrap().0).collect::>(), - ); - assert_eq!(d, t.iter().unwrap().map(|x| x.unwrap().1).collect::>()); - } - - #[test] - fn iterator_seek() { + test_layouts!(iterator_seek, iterator_seek_internal); + fn iterator_seek_internal() { let d = vec![ b"A".to_vec(), b"AA".to_vec(), @@ -539,16 +447,16 @@ mod tests { b"B".to_vec(), ]; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for x in &d { t.insert(x, x).unwrap(); } } - let t = RefTrieDBNoExt::new(&memdb, &root).unwrap(); + let t = TrieDB::::new(&memdb, &root).unwrap(); let mut iter = t.iter().unwrap(); assert_eq!(iter.next().unwrap().unwrap(), (b"A".to_vec(), b"A".to_vec())); iter.seek(b"!").unwrap(); @@ -576,40 +484,24 @@ mod tests { assert_eq!(&d[4..], &iter.map(|x| x.unwrap().1).collect::>()[..]); } - #[test] - fn get_length_with_extension() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(get_length_with_extension, get_length_with_extension_internal); + fn get_length_with_extension_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(b"A", b"ABC").unwrap(); t.insert(b"B", b"ABCBAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); } - let t = RefTrieDB::new(&memdb, &root).unwrap(); + let t = TrieDB::::new(&memdb, &root).unwrap(); assert_eq!(t.get_with(b"A", |x: &[u8]| x.len()).unwrap(), Some(3)); assert_eq!(t.get_with(b"B", |x: &[u8]| x.len()).unwrap(), Some(32)); assert_eq!(t.get_with(b"C", |x: &[u8]| x.len()).unwrap(), None); } - #[test] - fn get_length_without_extension() { - let mut memdb = MemoryDB::, DBValue>::default(); - let mut root = Default::default(); - { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); - t.insert(b"A", b"ABC").unwrap(); - t.insert(b"B", b"ABCBA").unwrap(); - } - - let t = RefTrieDBNoExt::new(&memdb, &root).unwrap(); - assert_eq!(t.get_with(b"A", |x: &[u8]| x.len()).unwrap(), Some(3)); - assert_eq!(t.get_with(b"B", |x: &[u8]| x.len()).unwrap(), Some(5)); - assert_eq!(t.get_with(b"C", |x: &[u8]| x.len()).unwrap(), None); - } - - #[test] - fn debug_output_supports_pretty_print() { + test_layouts!(debug_output_supports_pretty_print, debug_output_supports_pretty_print_internal); + fn debug_output_supports_pretty_print_internal() { let d = vec![ b"A".to_vec(), b"AA".to_vec(), @@ -617,18 +509,19 @@ mod tests { b"B".to_vec(), ]; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); let root = { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); for x in &d { t.insert(x, x).unwrap(); } t.root().clone() }; - let t = RefTrieDB::new(&memdb, &root).unwrap(); + let t = TrieDB::::new(&memdb, &root).unwrap(); - assert_eq!(format!("{:#?}", t), + if T::USE_EXTENSION { + assert_eq!(format!("{:#?}", t), "TrieDB { hash_count: 0, root: Node::Extension { @@ -678,26 +571,28 @@ mod tests { value: None, }, }, -}"); - +}") + } else { + // untested without extension + }; } - #[test] - fn test_lookup_with_corrupt_data_returns_decoder_error() { + test_layouts!(test_lookup_with_corrupt_data_returns_decoder_error, test_lookup_with_corrupt_data_returns_decoder_error_internal); + fn test_lookup_with_corrupt_data_returns_decoder_error_internal() { - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(b"A", b"ABC").unwrap(); t.insert(b"B", b"ABCBA").unwrap(); } - let t = RefTrieDB::new(&memdb, &root).unwrap(); + let t = TrieDB::::new(&memdb, &root).unwrap(); // query for an invalid data type to trigger an error let q = |x: &[u8]| x.len() < 64; - let lookup = RefLookup { db: t.db(), query: q, hash: root }; + let lookup = Lookup:: { db: t.db(), query: q, hash: root }; let query_result = lookup.look_up(NibbleSlice::new(b"A")); assert_eq!(query_result.unwrap().unwrap(), true); } diff --git a/trie-db/src/triedbmut.rs b/trie-db/src/triedbmut.rs index 5ce649af..f9450f90 100644 --- a/trie-db/src/triedbmut.rs +++ b/trie-db/src/triedbmut.rs @@ -1615,17 +1615,16 @@ mod tests { use crate::DBValue; use memory_db::{MemoryDB, PrefixedKey}; use hash_db::{Hasher, HashDB}; - use keccak_hasher::KeccakHasher; - use reference_trie::{RefTrieDBMutNoExt, RefTrieDBMut, TrieMut, NodeCodec, - ReferenceNodeCodec, reference_trie_root, reference_trie_root_no_extension}; + use reference_trie::{TrieDBMut, TrieMut, NodeCodec, test_layouts, + ReferenceNodeCodec, ReferenceNodeCodecNoExt, reference_trie_root_iter_build as reference_trie_root, TrieLayout}; use crate::nibble::BackingByteVec; - fn populate_trie<'db>( - db: &'db mut dyn HashDB, - root: &'db mut ::Out, + fn populate_trie<'db, T: TrieLayout>( + db: &'db mut dyn HashDB, + root: &'db mut ::Out, v: &[(Vec, Vec)] - ) -> RefTrieDBMut<'db> { - let mut t = RefTrieDBMut::new(db, root); + ) -> TrieDBMut<'db, T> { + let mut t = TrieDBMut::::new(db, root); for i in 0..v.len() { let key: &[u8]= &v[i].0; let val: &[u8] = &v[i].1; @@ -1634,41 +1633,23 @@ mod tests { t } - fn unpopulate_trie<'db>(t: &mut RefTrieDBMut<'db>, v: &[(Vec, Vec)]) { + fn unpopulate_trie<'db, T: TrieLayout>(t: &mut TrieDBMut<'db, T>, v: &[(Vec, Vec)]) { for i in v { let key: &[u8]= &i.0; t.remove(key).unwrap(); } } - fn populate_trie_no_extension<'db>( - db: &'db mut dyn HashDB, - root: &'db mut ::Out, - v: &[(Vec, Vec)] - ) -> RefTrieDBMutNoExt<'db> { - let mut t = RefTrieDBMutNoExt::new(db, root); - for i in 0..v.len() { - let key: &[u8]= &v[i].0; - let val: &[u8] = &v[i].1; - t.insert(key, val).unwrap(); - } - t - } - - fn unpopulate_trie_no_extension<'db>(t: &mut RefTrieDBMutNoExt<'db>, v: &[(Vec, Vec)]) { - for i in v { - let key: &[u8]= &i.0; - t.remove(key).unwrap(); + fn reference_hashed_null_node() -> ::Out { + if T::USE_EXTENSION { + as NodeCodec>::hashed_null_node() + } else { + as NodeCodec>::hashed_null_node() } } - fn reference_hashed_null_node() -> ::Out { - as NodeCodec>::hashed_null_node() - } - - #[test] - fn playpen() { - env_logger::init(); + test_layouts!(playpen, playpen_internal); + fn playpen_internal() { let mut seed = Default::default(); for test_i in 0..10 { if test_i % 50 == 0 { @@ -1682,10 +1663,10 @@ mod tests { count: 100, }.make_with(&mut seed); - let real = reference_trie_root(x.clone()); - let mut memdb = MemoryDB::, DBValue>::default(); + let real = reference_trie_root::(x.clone()); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut memtrie = populate_trie(&mut memdb, &mut root, &x); + let mut memtrie = populate_trie::(&mut memdb, &mut root, &x); memtrie.commit(); if *memtrie.root() != real { @@ -1699,50 +1680,7 @@ mod tests { assert_eq!(*memtrie.root(), real); unpopulate_trie(&mut memtrie, &x); memtrie.commit(); - let hashed_null_node = reference_hashed_null_node(); - if *memtrie.root() != hashed_null_node { - println!("- TRIE MISMATCH"); - println!(); - println!("{:#x?} vs {:#x?}", memtrie.root(), hashed_null_node); - for i in &x { - println!("{:#x?} -> {:#x?}", i.0, i.1); - } - } - assert_eq!(*memtrie.root(), hashed_null_node); - } - - // no_extension - let mut seed = Default::default(); - for test_i in 0..10 { - if test_i % 50 == 0 { - debug!("{:?} of 10000 stress tests done", test_i); - } - let x = StandardMap { - alphabet: Alphabet::Custom(b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_".to_vec()), - min_key: 5, - journal_key: 0, - value_mode: ValueMode::Index, - count: 100, - }.make_with(&mut seed); - - let real = reference_trie_root_no_extension(x.clone()); - let mut memdb = MemoryDB::, DBValue>::default(); - let mut root = Default::default(); - let mut memtrie = populate_trie_no_extension(&mut memdb, &mut root, &x); - - memtrie.commit(); - if *memtrie.root() != real { - println!("TRIE MISMATCH"); - println!(); - println!("{:?} vs {:?}", memtrie.root(), real); - for i in &x { - println!("{:#x?} -> {:#x?}", i.0, i.1); - } - } - assert_eq!(*memtrie.root(), real); - unpopulate_trie_no_extension(&mut memtrie, &x); - memtrie.commit(); - let hashed_null_node = reference_hashed_null_node(); + let hashed_null_node = reference_hashed_null_node::(); if *memtrie.root() != hashed_null_node { println!("- TRIE MISMATCH"); println!(); @@ -1755,36 +1693,35 @@ mod tests { } } - #[test] - fn init() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(init, init_internal); + fn init_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); - let hashed_null_node = reference_hashed_null_node(); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); + let hashed_null_node = reference_hashed_null_node::(); assert_eq!(*t.root(), hashed_null_node); } - #[test] - fn insert_on_empty() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_on_empty, insert_on_empty_internal); + fn insert_on_empty_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); assert_eq!( *t.root(), - reference_trie_root(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]) ]), + reference_trie_root::(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]) ]), ); } - #[test] - fn remove_to_empty() { + test_layouts!(remove_to_empty, remove_to_empty_internal); + fn remove_to_empty_internal() { let big_value = b"00000000000000000000000000000000"; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); - + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01], big_value).unwrap(); t.insert(&[0x01, 0x23], big_value).unwrap(); t.insert(&[0x01, 0x34], big_value).unwrap(); @@ -1795,8 +1732,8 @@ mod tests { assert_eq!(memdb.keys().len(), 0); } - #[test] - fn remove_to_empty_no_extension() { + test_layouts!(remove_to_empty_no_extension, remove_to_empty_no_extension_internal); + fn remove_to_empty_no_extension_internal() { let big_value = b"00000000000000000000000000000000"; let big_value2 = b"00000000000000000000000000000002"; let big_value3 = b"00000000000000000000000000000004"; @@ -1804,7 +1741,7 @@ mod tests { let mut memdb = MemoryDB::<_, PrefixedKey<_>, _>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMutNoExt::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01, 0x23], big_value3).unwrap(); t.insert(&[0x01], big_value2).unwrap(); @@ -1812,149 +1749,149 @@ mod tests { t.remove(&[0x01]).unwrap(); // commit on drop } - assert_eq!(&root[..], &reference_trie::calc_root_no_extension(vec![ + assert_eq!(&root, &reference_trie::calc_root::(vec![ (vec![0x01u8, 0x23], big_value3.to_vec()), (vec![0x01u8, 0x34], big_value.to_vec()), - ])[..]); + ])); } - #[test] - fn insert_replace_root() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_replace_root, insert_replace_root_internal); + fn insert_replace_root_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); t.insert(&[0x01u8, 0x23], &[0x23u8, 0x45]).unwrap(); assert_eq!( *t.root(), - reference_trie_root(vec![ (vec![0x01u8, 0x23], vec![0x23u8, 0x45]) ]), + reference_trie_root::(vec![ (vec![0x01u8, 0x23], vec![0x23u8, 0x45]) ]), ); } - #[test] - fn insert_make_branch_root() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_make_branch_root, insert_make_branch_root_internal); + fn insert_make_branch_root_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); t.insert(&[0x11u8, 0x23], &[0x11u8, 0x23]).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x11u8, 0x23], vec![0x11u8, 0x23]) ])); } - #[test] - fn insert_into_branch_root() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_into_branch_root, insert_into_branch_root_internal); + fn insert_into_branch_root_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]).unwrap(); t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x81u8, 0x23], vec![0x81u8, 0x23]), (vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]), ])); } - #[test] - fn insert_value_into_branch_root() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_value_into_branch_root, insert_value_into_branch_root_internal); + fn insert_value_into_branch_root_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); t.insert(&[], &[0x0]).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![], vec![0x0]), (vec![0x01u8, 0x23], vec![0x01u8, 0x23]), ])); } - #[test] - fn insert_split_leaf() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_split_leaf, insert_split_leaf_internal); + fn insert_split_leaf_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); t.insert(&[0x01u8, 0x34], &[0x01u8, 0x34]).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x01u8, 0x34], vec![0x01u8, 0x34]), ])); } - #[test] - fn insert_split_extenstion() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(insert_split_extenstion, insert_split_extenstion_internal); + fn insert_split_extenstion_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01, 0x23, 0x45], &[0x01]).unwrap(); t.insert(&[0x01, 0xf3, 0x45], &[0x02]).unwrap(); t.insert(&[0x01, 0xf3, 0xf5], &[0x03]).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![0x01, 0x23, 0x45], vec![0x01]), (vec![0x01, 0xf3, 0x45], vec![0x02]), (vec![0x01, 0xf3, 0xf5], vec![0x03]), ])); } - #[test] - fn insert_big_value() { + test_layouts!(insert_big_value, insert_big_value_internal); + fn insert_big_value_internal() { let big_value0 = b"00000000000000000000000000000000"; let big_value1 = b"11111111111111111111111111111111"; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], big_value0).unwrap(); t.insert(&[0x11u8, 0x23], big_value1).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![0x01u8, 0x23], big_value0.to_vec()), (vec![0x11u8, 0x23], big_value1.to_vec()) ])); } - #[test] - fn insert_duplicate_value() { + test_layouts!(insert_duplicate_value, insert_duplicate_value_internal); + fn insert_duplicate_value_internal() { let big_value = b"00000000000000000000000000000000"; - let mut memdb = MemoryDB::, DBValue>::default(); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], big_value).unwrap(); t.insert(&[0x11u8, 0x23], big_value).unwrap(); - assert_eq!(*t.root(), reference_trie_root(vec![ + assert_eq!(*t.root(), reference_trie_root::(vec![ (vec![0x01u8, 0x23], big_value.to_vec()), (vec![0x11u8, 0x23], big_value.to_vec()) ])); } - #[test] - fn test_at_empty() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(test_at_empty, test_at_empty_internal); + fn test_at_empty_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let t = RefTrieDBMut::new(&mut memdb, &mut root); + let t = TrieDBMut::::new(&mut memdb, &mut root); assert_eq!(t.get(&[0x5]).unwrap(), None); } - #[test] - fn test_at_one() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(test_at_one, test_at_one_internal); + fn test_at_one_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); assert_eq!(t.get(&[0x1, 0x23]).unwrap().unwrap(), vec![0x1u8, 0x23]); t.commit(); assert_eq!(t.get(&[0x1, 0x23]).unwrap().unwrap(), vec![0x1u8, 0x23]); } - #[test] - fn test_at_three() { - let mut memdb = MemoryDB::, DBValue>::default(); + test_layouts!(test_at_three, test_at_three_internal); + fn test_at_three_internal() { + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut memdb, &mut root); + let mut t = TrieDBMut::::new(&mut memdb, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]).unwrap(); t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]).unwrap(); @@ -1969,8 +1906,8 @@ mod tests { assert_eq!(t.get(&[0x82, 0x23]).unwrap(), None); } - #[test] - fn stress() { + test_layouts!(stress, stress_internal); + fn stress_internal() { let mut seed = Default::default(); for _ in 0..50 { let x = StandardMap { @@ -1981,15 +1918,15 @@ mod tests { count: 4, }.make_with(&mut seed); - let real = reference_trie_root(x.clone()); - let mut memdb = MemoryDB::, DBValue>::default(); + let real = reference_trie_root::(x.clone()); + let mut memdb = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut memtrie = populate_trie(&mut memdb, &mut root, &x); + let mut memtrie = populate_trie::(&mut memdb, &mut root, &x); let mut y = x.clone(); y.sort_by(|ref a, ref b| a.0.cmp(&b.0)); - let mut memdb2 = MemoryDB::, DBValue>::default(); + let mut memdb2 = MemoryDB::, DBValue>::default(); let mut root2 = Default::default(); - let mut memtrie_sorted = populate_trie(&mut memdb2, &mut root2, &y); + let mut memtrie_sorted = populate_trie::(&mut memdb2, &mut root2, &y); if *memtrie.root() != real || *memtrie_sorted.root() != real { println!("TRIE MISMATCH"); println!(); @@ -2007,22 +1944,22 @@ mod tests { } } - #[test] - fn test_trie_existing() { - let mut db = MemoryDB::, DBValue>::default(); + test_layouts!(test_trie_existing, test_trie_existing_internal); + fn test_trie_existing_internal() { + let mut db = MemoryDB::, DBValue>::default(); let mut root = Default::default(); { - let mut t = RefTrieDBMut::new(&mut db, &mut root); + let mut t = TrieDBMut::::new(&mut db, &mut root); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); } { - let _ = RefTrieDBMut::from_existing(&mut db, &mut root); + let _ = TrieDBMut::::from_existing(&mut db, &mut root); } } - #[test] - fn insert_empty() { + test_layouts!(insert_empty, insert_empty_internal); + fn insert_empty_internal() { let mut seed = Default::default(); let x = StandardMap { alphabet: Alphabet::Custom(b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_".to_vec()), @@ -2032,26 +1969,26 @@ mod tests { count: 4, }.make_with(&mut seed); - let mut db = MemoryDB::, DBValue>::default(); + let mut db = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut db, &mut root); + let mut t = TrieDBMut::::new(&mut db, &mut root); for &(ref key, ref value) in &x { t.insert(key, value).unwrap(); } - assert_eq!(*t.root(), reference_trie_root(x.clone())); + assert_eq!(*t.root(), reference_trie_root::(x.clone())); for &(ref key, _) in &x { t.insert(key, &[]).unwrap(); } assert!(t.is_empty()); - let hashed_null_node = reference_hashed_null_node(); + let hashed_null_node = reference_hashed_null_node::(); assert_eq!(*t.root(), hashed_null_node); } - #[test] - fn return_old_values() { + test_layouts!(return_old_values, return_old_values_internal); + fn return_old_values_internal() { let mut seed = Default::default(); let x = StandardMap { alphabet: Alphabet::Custom(b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_".to_vec()), @@ -2061,9 +1998,9 @@ mod tests { count: 2, }.make_with(&mut seed); - let mut db = MemoryDB::, DBValue>::default(); + let mut db = MemoryDB::, DBValue>::default(); let mut root = Default::default(); - let mut t = RefTrieDBMut::new(&mut db, &mut root); + let mut t = TrieDBMut::::new(&mut db, &mut root); for &(ref key, ref value) in &x { assert!(t.insert(key, value).unwrap().is_none()); assert_eq!(t.insert(key, value).unwrap(), Some(value.clone())); From 17eea1aa71339891b2b95c36cdda24c06622f556 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 18 May 2020 11:02:10 +0200 Subject: [PATCH 2/7] iterbuild prefix fix --- trie-db/src/iter_build.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/trie-db/src/iter_build.rs b/trie-db/src/iter_build.rs index 2f7e3b45..119767c6 100644 --- a/trie-db/src/iter_build.rs +++ b/trie-db/src/iter_build.rs @@ -227,19 +227,15 @@ impl CacheAccum debug_assert!(self.0[last].2 == branch_d); // encode branch let v = self.0[last].1.take(); - let nkeyix = nkey.unwrap_or((0, 0)); + let nkeyix = nkey.unwrap_or((branch_d, 0)); let pr = NibbleSlice::new_offset(&key_branch, nkeyix.0); let encoded = T::Codec::branch_node_nibbled( pr.right_range_iter(nkeyix.1), nkeyix.1, self.0[last].0.as_ref().iter(), v.as_ref().map(|v| v.as_ref())); + let result = callback.process(pr.left(), encoded, is_root); self.reset_depth(branch_d); - let ext_length = nkey.as_ref().map(|nkeyix| nkeyix.0).unwrap_or(0); - let pr = NibbleSlice::new_offset( - &key_branch, - branch_d - ext_length, - ); - callback.process(pr.left(), encoded, is_root) + result } } From dfd1c28ffbb5628e3ca6848d0426397608ed9563 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 18 May 2020 11:42:16 +0200 Subject: [PATCH 3/7] compact proof parameterized --- test-support/reference-trie/src/lib.rs | 8 +- trie-db/src/proof/mod.rs | 146 ++++++++++++------------- trie-db/src/trie_codec.rs | 52 ++------- trie-db/src/triedbmut.rs | 7 +- 4 files changed, 89 insertions(+), 124 deletions(-) diff --git a/test-support/reference-trie/src/lib.rs b/test-support/reference-trie/src/lib.rs index 87d3dbca..d9247b0f 100644 --- a/test-support/reference-trie/src/lib.rs +++ b/test-support/reference-trie/src/lib.rs @@ -152,7 +152,7 @@ pub fn reference_trie_root_iter_build(input: I) -> ::default(); trie_visit::(data_sorted_unique(input), &mut cb); - cb.root.unwrap_or(Default::default()) + cb.root.unwrap_or_default() } const EMPTY_TRIE: u8 = 0; @@ -987,7 +987,7 @@ pub fn calc_root( { let mut cb = TrieRoot::::default(); trie_visit::(data.into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) + cb.root.unwrap_or_default() } /// Trie builder trie building utility. @@ -1004,7 +1004,7 @@ pub fn calc_root_build( { let mut cb = TrieBuilder::new(hashdb); trie_visit::(data.into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) + cb.root.unwrap_or_default() } /// `compare_implementations_no_extension` for unordered input (trie_root does @@ -1031,7 +1031,7 @@ pub fn compare_implementations_unordered ( let root_new = { let mut cb = TrieBuilder::new(&mut hashdb); trie_visit::(b_map.into_iter(), &mut cb); - cb.root.unwrap_or(Default::default()) + cb.root.unwrap_or_default() }; if root != root_new { diff --git a/trie-db/src/proof/mod.rs b/trie-db/src/proof/mod.rs index 30f5206d..e4dcdffb 100644 --- a/trie-db/src/proof/mod.rs +++ b/trie-db/src/proof/mod.rs @@ -40,7 +40,7 @@ mod verify; mod tests { use hash_db::Hasher; use reference_trie::{ - ExtensionLayout, NoExtensionLayout, + NoExtensionLayout, test_layouts, proof::{generate_proof, verify_proof, VerifyError}, Trie, TrieDB, TrieDBMut, TrieLayout, TrieMut, }; @@ -95,28 +95,9 @@ mod tests { (root, proof, items) } - #[test] - fn trie_proof_works_with_ext() { - let (root, proof, items) = test_generate_proof::( - test_entries(), - vec![ - b"do", - b"dog", - b"doge", - b"bravo", - b"alfabet", // None, not found under leaf node - b"d", // None, witness is extension node with omitted child - b"do\x10", // None, empty branch child - b"halp", // None, witness is extension node with non-omitted child - ], - ); - - verify_proof::(&root, &proof, items.iter()).unwrap(); - } - - #[test] - fn trie_proof_works_without_ext() { - let (root, proof, items) = test_generate_proof::( + test_layouts!(trie_proof_works, trie_proof_works_internal); + fn trie_proof_works_internal() { + let (root, proof, items) = test_generate_proof::( test_entries(), vec![ b"do", @@ -130,12 +111,12 @@ mod tests { ], ); - verify_proof::(&root, &proof, items.iter()).unwrap(); + verify_proof::(&root, &proof, items.iter()).unwrap(); } - #[test] - fn trie_proof_works_for_empty_trie() { - let (root, proof, items) = test_generate_proof::( + test_layouts!(trie_proof_works_for_empty_trie, trie_proof_works_for_empty_trie_internal); + fn trie_proof_works_for_empty_trie_internal() { + let (root, proof, items) = test_generate_proof::( vec![], vec![ b"alpha", @@ -144,12 +125,12 @@ mod tests { ], ); - verify_proof::(&root, &proof, items.iter()).unwrap(); + verify_proof::(&root, &proof, items.iter()).unwrap(); } - #[test] - fn test_verify_duplicate_keys() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_duplicate_keys, test_verify_duplicate_keys_internal); + fn test_verify_duplicate_keys_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"bravo"], ); @@ -158,15 +139,18 @@ mod tests { (b"bravo", Some(b"bravo")), (b"bravo", Some(b"bravo")), ]; - assert_eq!( - verify_proof::(&root, &proof, items.iter()), - Err(VerifyError::DuplicateKey(b"bravo".to_vec())) + assert!( + if let Err(VerifyError::DuplicateKey(key)) = verify_proof::(&root, &proof, items.iter()) { + key == b"bravo".to_vec() + } else { + false + } ); } - #[test] - fn test_verify_extraneous_node() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_extraneaous_node, test_verify_extraneaous_node_internal); + fn test_verify_extraneaous_node_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"bravo", b"do"], ); @@ -174,15 +158,15 @@ mod tests { let items = vec![ (b"bravo", Some(b"bravo")), ]; - assert_eq!( - verify_proof::(&root, &proof, items.iter()), + assert!(matches!( + verify_proof::(&root, &proof, items.iter()), Err(VerifyError::ExtraneousNode) - ); + )); } - #[test] - fn test_verify_extraneous_value() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_extraneaous_value, test_verify_extraneaous_value_internal); + fn test_verify_extraneaous_value_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"doge"], ); @@ -191,14 +175,18 @@ mod tests { (&b"do"[..], Some(&b"verb"[..])), (&b"doge"[..], Some(&[0; 32][..])), ]; - assert_eq!( - verify_proof::(&root, &proof, items.iter()), - Err(VerifyError::ExtraneousValue(b"do".to_vec())) + assert!( + if let Err(VerifyError::ExtraneousValue(val)) = verify_proof::(&root, &proof, items.iter()) { + val == b"do".to_vec() + } else { + false + } ); } #[test] fn test_verify_extraneous_hash_reference() { + // This is not valid for hybrid let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"do"], @@ -214,9 +202,9 @@ mod tests { } } - #[test] - fn test_verify_invalid_child_reference() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_invalid_child_reference, test_verify_invalid_child_reference_internal); + fn test_verify_invalid_child_reference_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"bravo"], ); @@ -226,15 +214,15 @@ mod tests { let items = vec![ (b"bravo", Some([0; 32])), ]; - match verify_proof::(&root, &proof, items.iter()) { + match verify_proof::(&root, &proof, items.iter()) { Err(VerifyError::InvalidChildReference(_)) => {} result => panic!("expected VerifyError::InvalidChildReference, got {:?}", result), } } - #[test] - fn test_verify_value_mismatch_some_to_none() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_value_mismatch_some_to_none, test_verify_value_mismatch_some_to_none_internal); + fn test_verify_value_mismatch_some_to_none_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"horse"], ); @@ -243,15 +231,18 @@ mod tests { (&b"horse"[..], Some(&b"stallion"[..])), (&b"halp"[..], Some(&b"plz"[..])), ]; - assert_eq!( - verify_proof::(&root, &proof, items.iter()), - Err(VerifyError::ValueMismatch(b"halp".to_vec())) + assert!( + if let Err(VerifyError::ValueMismatch(val)) = verify_proof::(&root, &proof, items.iter()) { + val == b"halp".to_vec() + } else { + false + } ); } - #[test] - fn test_verify_value_mismatch_none_to_some() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_value_mismatch_none_to_some, test_verify_value_mismatch_none_to_some_internal); + fn test_verify_value_mismatch_none_to_some_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"alfa", b"bravo"], ); @@ -260,29 +251,32 @@ mod tests { (&b"alfa"[..], Some(&[0; 32][..])), (&b"bravo"[..], None), ]; - assert_eq!( - verify_proof::(&root, &proof, items.iter()), - Err(VerifyError::ValueMismatch(b"bravo".to_vec())) + assert!( + if let Err(VerifyError::ValueMismatch(val)) = verify_proof::(&root, &proof, items.iter()) { + val == b"bravo".to_vec() + } else { + false + } ); } - #[test] - fn test_verify_incomplete_proof() { - let (root, mut proof, items) = test_generate_proof::( + test_layouts!(test_verify_incomplete_proof, test_verify_incomplete_proof_internal); + fn test_verify_incomplete_proof_internal() { + let (root, mut proof, items) = test_generate_proof::( test_entries(), vec![b"alfa"], ); proof.pop(); - assert_eq!( - verify_proof::(&root, &proof, items.iter()), + assert!(matches!( + verify_proof::(&root, &proof, items.iter()), Err(VerifyError::IncompleteProof) - ); + )); } - #[test] - fn test_verify_root_mismatch() { - let (root, proof, _) = test_generate_proof::( + test_layouts!(test_verify_root_mismatch, test_verify_root_mismatch_internal); + fn test_verify_root_mismatch_internal() { + let (root, proof, _) = test_generate_proof::( test_entries(), vec![b"bravo"], ); @@ -290,21 +284,21 @@ mod tests { let items = vec![ (b"bravo", Some("incorrect")), ]; - match verify_proof::(&root, &proof, items.iter()) { + match verify_proof::(&root, &proof, items.iter()) { Err(VerifyError::RootMismatch(_)) => {} result => panic!("expected VerifyError::RootMismatch, got {:?}", result), } } - #[test] - fn test_verify_decode_error() { - let (root, mut proof, items) = test_generate_proof::( + test_layouts!(test_verify_decode_error, test_verify_decode_error_internal); + fn test_verify_decode_error_internal() { + let (root, mut proof, items) = test_generate_proof::( test_entries(), vec![b"bravo"], ); proof.insert(0, b"this is not a trie node".to_vec()); - match verify_proof::(&root, &proof, items.iter()) { + match verify_proof::(&root, &proof, items.iter()) { Err(VerifyError::DecodeError(_)) => {} result => panic!("expected VerifyError::DecodeError, got {:?}", result), } diff --git a/trie-db/src/trie_codec.rs b/trie-db/src/trie_codec.rs index 7d4783f8..1177e08c 100644 --- a/trie-db/src/trie_codec.rs +++ b/trie-db/src/trie_codec.rs @@ -446,9 +446,8 @@ mod tests { use crate::DBValue; use hash_db::{HashDB, Hasher, EMPTY_PREFIX}; use reference_trie::{ - ExtensionLayout, NoExtensionLayout, Trie, TrieMut, TrieDB, TrieError, TrieDBMut, TrieLayout, Recorder, - encode_compact, decode_compact, + encode_compact, decode_compact, test_layouts, }; type MemoryDB = memory_db::MemoryDB, DBValue>; @@ -517,9 +516,9 @@ mod tests { } } - #[test] - fn trie_compact_encoding_works_with_ext() { - let (root, mut encoded, items) = test_encode_compact::( + test_layouts!(trie_compact_encoding_works, trie_compact_encoding_works_internal); + fn trie_compact_encoding_works_internal() { + let (root, mut encoded, items) = test_encode_compact::( vec![ // "alfa" is at a hash-referenced leaf node. (b"alfa", &[0; 32]), @@ -547,45 +546,12 @@ mod tests { ); encoded.push(Vec::new()); // Add an extra item to ensure it is not read. - test_decode_compact::(&encoded, items, root, encoded.len() - 1); + test_decode_compact::(&encoded, items, root, encoded.len() - 1); } - #[test] - fn trie_compact_encoding_works_without_ext() { - let (root, mut encoded, items) = test_encode_compact::( - vec![ - // "alfa" is at a hash-referenced leaf node. - (b"alfa", &[0; 32]), - // "bravo" is at an inline leaf node. - (b"bravo", b"bravo"), - // "do" is at a hash-referenced branch node. - (b"do", b"verb"), - // "dog" is at an inline leaf node. - (b"dog", b"puppy"), - // "doge" is at a hash-referenced leaf node. - (b"doge", &[0; 32]), - // extension node "o" (plus nibble) to next branch. - (b"horse", b"stallion"), - (b"house", b"building"), - ], - vec![ - b"do", - b"dog", - b"doge", - b"bravo", - b"d", // None, witness is a branch partial - b"do\x10", // None, witness is empty branch child - b"halp", // None, witness is branch partial - ], - ); - - encoded.push(Vec::new()); // Add an extra item to ensure it is not read. - test_decode_compact::(&encoded, items, root, encoded.len() - 1); - } - - #[test] - fn trie_decoding_fails_with_incomplete_database() { - let (_, encoded, _) = test_encode_compact::( + test_layouts!(trie_decoding_fails_with_incomplete_database, trie_decoding_fails_with_incomplete_database_internal); + fn trie_decoding_fails_with_incomplete_database_internal() { + let (_, encoded, _) = test_encode_compact::( vec![ (b"alfa", &[0; 32]), (b"bravo", b"bravo"), @@ -599,7 +565,7 @@ mod tests { // Reconstruct the partial DB from the compact encoding. let mut db = MemoryDB::default(); - match decode_compact::(&mut db, &encoded[..encoded.len() - 1]) { + match decode_compact::(&mut db, &encoded[..encoded.len() - 1]) { Err(err) => match *err { TrieError::IncompleteDatabase(_) => {} _ => panic!("got unexpected TrieError"), diff --git a/trie-db/src/triedbmut.rs b/trie-db/src/triedbmut.rs index f9450f90..67385a25 100644 --- a/trie-db/src/triedbmut.rs +++ b/trie-db/src/triedbmut.rs @@ -1648,7 +1648,12 @@ mod tests { } } - test_layouts!(playpen, playpen_internal); + #[test] + fn playpen() { + env_logger::init(); + playpen_internal::(); + playpen_internal::(); + } fn playpen_internal() { let mut seed = Default::default(); for test_i in 0..10 { From 8bcd01e05d49d42a22d4044d81233db7e511c181 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 18 May 2020 11:47:40 +0200 Subject: [PATCH 4/7] restore clippy change --- test-support/reference-trie/src/lib.rs | 6 +++--- trie-db/src/triedbmut.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test-support/reference-trie/src/lib.rs b/test-support/reference-trie/src/lib.rs index d9247b0f..490ba51f 100644 --- a/test-support/reference-trie/src/lib.rs +++ b/test-support/reference-trie/src/lib.rs @@ -931,7 +931,7 @@ pub fn compare_implementations ( t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); } t.commit(); - t.root().clone() + *t.root() }; if root_new != root { { @@ -969,7 +969,7 @@ pub fn compare_root>( for i in 0..data.len() { t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); } - t.root().clone() + *t.root() }; assert_eq!(root, root_new); @@ -1026,7 +1026,7 @@ pub fn compare_implementations_unordered ( t.insert(&data[i].0[..], &data[i].1[..]).unwrap(); b_map.insert(data[i].0.clone(), data[i].1.clone()); } - t.root().clone() + *t.root() }; let root_new = { let mut cb = TrieBuilder::new(&mut hashdb); diff --git a/trie-db/src/triedbmut.rs b/trie-db/src/triedbmut.rs index 67385a25..a377fedb 100644 --- a/trie-db/src/triedbmut.rs +++ b/trie-db/src/triedbmut.rs @@ -506,7 +506,7 @@ where &mut NibbleFullKey, ) -> Result>, TrieHash, CError>, { - let current_key = key.clone(); + let current_key = *key; Ok(match stored { Stored::New(node) => match inspector(self, node, key)? { Action::Restore(node) => Some((Stored::New(node), false)), @@ -1000,12 +1000,12 @@ where (Node::Branch(children, Some(val)), true) => { *old_val = Some(val); // always replace since we took the value out. - Action::Replace(self.fix(Node::Branch(children, None), key.clone())?) + Action::Replace(self.fix(Node::Branch(children, None), *key)?) }, (Node::NibbledBranch(n, children, Some(val)), true) => { *old_val = Some(val); // always replace since we took the value out. - Action::Replace(self.fix(Node::NibbledBranch(n, children, None), key.clone())?) + Action::Replace(self.fix(Node::NibbledBranch(n, children, None), *key)?) }, (Node::Branch(mut children, value), false) => { let idx = partial.at(0) as usize; @@ -1053,7 +1053,7 @@ where if let Some(val) = value { *old_val = Some(val); - let f = self.fix(Node::NibbledBranch(encoded, children, None), key.clone()); + let f = self.fix(Node::NibbledBranch(encoded, children, None), *key); Action::Replace(f?) } else { Action::Restore(Node::NibbledBranch(encoded, children, None)) From 57553257a4cf3f16188afd864ff61482ef142493 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 18 May 2020 12:04:29 +0200 Subject: [PATCH 5/7] bench update from #81 --- trie-db/benches/bench.rs | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/trie-db/benches/bench.rs b/trie-db/benches/bench.rs index e2a63523..9f74d419 100644 --- a/trie-db/benches/bench.rs +++ b/trie-db/benches/bench.rs @@ -16,6 +16,7 @@ use criterion::{criterion_group, criterion_main, Bencher, black_box, Criterion}; use trie_db::{NibbleSlice, proof::{generate_proof, verify_proof}, Trie}; use trie_standardmap::{Alphabet, StandardMap, ValueMode}; +use reference_trie::ExtensionLayout as Layout; criterion_group!(benches, root_old, @@ -75,7 +76,7 @@ fn root_a_big_v(c: &mut Criterion) { .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data, ); @@ -96,7 +97,7 @@ fn root_b_big_v(c: &mut Criterion) { .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data, ); @@ -118,7 +119,7 @@ fn root_a_small_v(c: &mut Criterion) { .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data, ); @@ -139,7 +140,7 @@ fn root_b_small_v(c: &mut Criterion) { .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data, ); @@ -160,7 +161,7 @@ fn root_old(c: &mut Criterion) { .iter() .map(|v| (&v.0, &v.1)); - reference_trie::reference_trie_root(inputc); + reference_trie::reference_trie_root::(inputc); }), data, ); @@ -185,7 +186,7 @@ fn root_new(c: &mut Criterion) { .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data, ); @@ -287,7 +288,7 @@ fn trie_mut_root_a(c: &mut Criterion) { .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data); } @@ -306,7 +307,7 @@ fn trie_mut_root_b(c: &mut Criterion) { .map(|v| (&v.0, &v.1)) .collect::>(); - reference_trie::calc_root(inputc); + reference_trie::calc_root::(inputc); }), data); } @@ -326,7 +327,7 @@ fn trie_mut_ref_root_a(c: &mut Criterion) { .map(|v| (&v.0, &v.1)) .collect::>(); - reference_trie::reference_trie_root(inputc); + reference_trie::reference_trie_root_iter_build::(inputc); }), data); } @@ -346,7 +347,7 @@ fn trie_mut_ref_root_b(c: &mut Criterion) { .map(|v| (&v.0, &v.1)) .collect::>(); - reference_trie::reference_trie_root(inputc); + reference_trie::reference_trie_root_iter_build::(inputc); }), data); } @@ -366,7 +367,7 @@ fn trie_mut_a(c: &mut Criterion) { let mut root = Default::default(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let mut trie = reference_trie::RefTrieDBMut::new(&mut mdb, &mut root); + let mut trie = reference_trie::TrieDBMut::::new(&mut mdb, &mut root); for (key, value) in datac { trie.insert(&key, &value) .expect("changes trie: insertion to trie is not allowed to fail within runtime"); @@ -390,7 +391,7 @@ fn trie_mut_b(c: &mut Criterion) { let mut root = Default::default(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let mut trie = reference_trie::RefTrieDBMut::new(&mut mdb, &mut root); + let mut trie = reference_trie::TrieDBMut::::new(&mut mdb, &mut root); for (key, value) in datac { trie.insert(&key, &value) .expect("changes trie: insertion to trie is not allowed to fail within runtime"); @@ -416,7 +417,7 @@ fn trie_mut_build_a(c: &mut Criterion) { .collect::>(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - reference_trie::calc_root_build(inputc, &mut mdb); + reference_trie::calc_root_build::(inputc, &mut mdb); }), data); } @@ -438,7 +439,7 @@ fn trie_mut_build_b(c: &mut Criterion) { .collect::>(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - reference_trie::calc_root_build(inputc, &mut mdb); + reference_trie::calc_root_build::(inputc, &mut mdb); }), data); } @@ -449,11 +450,11 @@ fn trie_iteration(c: &mut Criterion) { let input = input2(29, 204800, 32); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let root = reference_trie::calc_root_build(input, &mut mdb); + let root = reference_trie::calc_root_build::(input, &mut mdb); c.bench_function("trie_iteration", move |b: &mut Bencher| b.iter(|| { - let trie = reference_trie::RefTrieDB::new(&mdb, &root).unwrap(); + let trie = reference_trie::TrieDB::::new(&mdb, &root).unwrap(); let mut iter = trie_db::TrieDBNodeIterator::new(&trie).unwrap(); assert!(iter.all(|result| result.is_ok())); }) @@ -475,9 +476,9 @@ fn trie_proof_verification(c: &mut Criterion) { keys.dedup(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let root = reference_trie::calc_root_build(data, &mut mdb); + let root = reference_trie::calc_root_build::(data, &mut mdb); - let trie = reference_trie::RefTrieDB::new(&mdb, &root).unwrap(); + let trie = reference_trie::TrieDB::::new(&mdb, &root).unwrap(); let proof = generate_proof(&trie, keys.iter()).unwrap(); let items = keys.into_iter() .map(|key| { @@ -488,7 +489,7 @@ fn trie_proof_verification(c: &mut Criterion) { c.bench_function("trie_proof_verification", move |b: &mut Bencher| b.iter(|| { - verify_proof::( + verify_proof::( &root, &proof, items.iter() From 32f6b292971fbfcbb869ecdbf4d86f1a60d4b7ab Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 10 Mar 2021 09:33:03 +0100 Subject: [PATCH 6/7] comment --- test-support/reference-trie/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/test-support/reference-trie/src/lib.rs b/test-support/reference-trie/src/lib.rs index 2accc6eb..6f263be5 100644 --- a/test-support/reference-trie/src/lib.rs +++ b/test-support/reference-trie/src/lib.rs @@ -45,6 +45,7 @@ pub mod node { /// Reference hasher is a keccak hasher. pub type RefHasher = keccak_hasher::KeccakHasher; +/// Apply a test method on every test layouts. #[macro_export] macro_rules! test_layouts { ($test:ident, $test_internal:ident) => { From f8e1c25a8aec7a7a16cf3ecefb9303825646b271 Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 10 Mar 2021 10:00:18 +0100 Subject: [PATCH 7/7] fix bench check --- trie-db/test/benches/bench.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/trie-db/test/benches/bench.rs b/trie-db/test/benches/bench.rs index 9f74d419..a69b11e5 100644 --- a/trie-db/test/benches/bench.rs +++ b/trie-db/test/benches/bench.rs @@ -367,7 +367,7 @@ fn trie_mut_a(c: &mut Criterion) { let mut root = Default::default(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let mut trie = reference_trie::TrieDBMut::::new(&mut mdb, &mut root); + let mut trie = trie_db::TrieDBMut::::new(&mut mdb, &mut root); for (key, value) in datac { trie.insert(&key, &value) .expect("changes trie: insertion to trie is not allowed to fail within runtime"); @@ -391,7 +391,7 @@ fn trie_mut_b(c: &mut Criterion) { let mut root = Default::default(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let mut trie = reference_trie::TrieDBMut::::new(&mut mdb, &mut root); + let mut trie = trie_db::TrieDBMut::::new(&mut mdb, &mut root); for (key, value) in datac { trie.insert(&key, &value) .expect("changes trie: insertion to trie is not allowed to fail within runtime"); @@ -454,7 +454,7 @@ fn trie_iteration(c: &mut Criterion) { c.bench_function("trie_iteration", move |b: &mut Bencher| b.iter(|| { - let trie = reference_trie::TrieDB::::new(&mdb, &root).unwrap(); + let trie = trie_db::TrieDB::::new(&mdb, &root).unwrap(); let mut iter = trie_db::TrieDBNodeIterator::new(&trie).unwrap(); assert!(iter.all(|result| result.is_ok())); }) @@ -478,7 +478,7 @@ fn trie_proof_verification(c: &mut Criterion) { let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); let root = reference_trie::calc_root_build::(data, &mut mdb); - let trie = reference_trie::TrieDB::::new(&mdb, &root).unwrap(); + let trie = trie_db::TrieDB::::new(&mdb, &root).unwrap(); let proof = generate_proof(&trie, keys.iter()).unwrap(); let items = keys.into_iter() .map(|key| {