diff --git a/Cargo.toml b/Cargo.toml index 31c1465..e0a09f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cita_trie" -version = "1.0.1" +version = "2.0.0" authors = ["yejiayu "] description = "Modified Patricia Tree (aka Trie)." license = "Apache-2.0" @@ -14,8 +14,8 @@ documentation = "https://docs.rs/cita_trie" [dependencies] parking_lot = "0.8" rlp = "0.3.0" -tiny-keccak = "1.4.2" hashbrown = "0.3.0" +hasher = { version = "0.1", features = ["hash-keccak"] } [dev-dependencies] rand = "0.6.3" diff --git a/README.md b/README.md index 208c25d..648fec9 100644 --- a/README.md +++ b/README.md @@ -19,18 +19,20 @@ The implementation is strongly inspired by [go-ethereum trie](https://github.com ```rust use std::sync::Arc; +use hasher::{Hasher, HasherKeccak}; // https://crates.io/crates/hasher + use cita_trie::MemoryDB; use cita_trie::{PatriciaTrie, Trie}; -use cita_trie::Keccak256Hash; fn main() { let memdb = Arc::new(MemoryDB::new(true)); + let hahser = Arc::new(HasherKeccak::new()); let key = "test-key".as_bytes(); let value = "test-value".as_bytes(); let root = { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::clone(&hasher)); trie.insert(key, value).unwrap(); let v = trie.get(key).unwrap(); @@ -38,7 +40,7 @@ fn main() { trie.root().unwrap() }; - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie = PatriciaTrie::from(Arc::clone(&memdb), Arc::clone(&hasher), &root); let exists = trie.contains(key).unwrap(); assert_eq!(exists, true); let removed = trie.remove(key).unwrap(); @@ -96,8 +98,7 @@ Found 11 outliers among 100 measurements (11.00%) ``` ### Custom hash algorithm - -[Refer](https://github.com/cryptape/cita-trie/blob/master/src/lib.rs) +See: https://crates.io/crates/hasher ### Custom storage diff --git a/benches/insert_benchmark.rs b/benches/insert_benchmark.rs new file mode 100644 index 0000000..ce14f63 --- /dev/null +++ b/benches/insert_benchmark.rs @@ -0,0 +1,68 @@ +use std::sync::Arc; + +use criterion::{criterion_group, criterion_main, Criterion}; + +use hasher::HasherKeccak; +use uuid::Uuid; + +use cita_trie::MemoryDB; +use cita_trie::{PatriciaTrie, Trie}; + +fn insert_worse_case_benchmark(c: &mut Criterion) { + c.bench_function("cita-trie insert one", |b| { + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); + + b.iter(|| { + let key = Uuid::new_v4().as_bytes().to_vec(); + let value = Uuid::new_v4().as_bytes().to_vec(); + trie.insert(key, value).unwrap() + }) + }); + + c.bench_function("cita-trie insert 1k", |b| { + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); + + let (keys, values) = random_data(1000); + b.iter(|| { + for i in 0..keys.len() { + trie.insert(keys[i].clone(), values[i].clone()).unwrap() + } + }); + }); + + c.bench_function("cita-trie insert 10k", |b| { + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); + + let (keys, values) = random_data(10000); + b.iter(|| { + for i in 0..keys.len() { + trie.insert(keys[i].clone(), values[i].clone()).unwrap() + } + }); + }); +} + +fn random_data(n: usize) -> (Vec>, Vec>) { + let mut keys = Vec::with_capacity(n); + let mut values = Vec::with_capacity(n); + for _ in 0..n { + let key = Uuid::new_v4().as_bytes().to_vec(); + let value = Uuid::new_v4().as_bytes().to_vec(); + keys.push(key); + values.push(value); + } + + (keys, values) +} + +criterion_group!(benches, insert_worse_case_benchmark); +criterion_main!(benches); diff --git a/benches/trie.rs b/benches/trie.rs index 1c11873..04dc8ec 100644 --- a/benches/trie.rs +++ b/benches/trie.rs @@ -2,15 +2,18 @@ use std::sync::Arc; use criterion::{criterion_group, criterion_main, Criterion}; +use hasher::HasherKeccak; use uuid::Uuid; -use cita_trie::Keccak256Hash; use cita_trie::MemoryDB; use cita_trie::{PatriciaTrie, Trie}; fn insert_worse_case_benchmark(c: &mut Criterion) { c.bench_function("insert one", |b| { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::new(MemoryDB::new(false))); + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); b.iter(|| { let key = Uuid::new_v4().as_bytes().to_vec(); @@ -20,7 +23,10 @@ fn insert_worse_case_benchmark(c: &mut Criterion) { }); c.bench_function("insert 1k", |b| { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::new(MemoryDB::new(false))); + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); let (keys, values) = random_data(1000); b.iter(|| { @@ -31,7 +37,10 @@ fn insert_worse_case_benchmark(c: &mut Criterion) { }); c.bench_function("insert 10k", |b| { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::new(MemoryDB::new(false))); + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); let (keys, values) = random_data(10000); b.iter(|| { @@ -42,7 +51,10 @@ fn insert_worse_case_benchmark(c: &mut Criterion) { }); c.bench_function("get based 10k", |b| { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::new(MemoryDB::new(false))); + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); let (keys, values) = random_data(10000); for i in 0..keys.len() { @@ -56,7 +68,10 @@ fn insert_worse_case_benchmark(c: &mut Criterion) { }); c.bench_function("remove 1k", |b| { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::new(MemoryDB::new(false))); + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); let (keys, values) = random_data(1000); for i in 0..keys.len() { @@ -71,7 +86,10 @@ fn insert_worse_case_benchmark(c: &mut Criterion) { }); c.bench_function("remove 10k", |b| { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::new(MemoryDB::new(false))); + let mut trie = PatriciaTrie::new( + Arc::new(MemoryDB::new(false)), + Arc::new(HasherKeccak::new()), + ); let (keys, values) = random_data(10000); for i in 0..keys.len() { diff --git a/src/lib.rs b/src/lib.rs index 3411687..10b5446 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,19 +2,21 @@ //! //! ```rust //! use std::sync::Arc; - +//! +//! use hasher::{Hasher, HasherKeccak}; // https://crates.io/crates/hasher +//! //! use cita_trie::MemoryDB; //! use cita_trie::{PatriciaTrie, Trie}; -//! use cita_trie::Keccak256Hash; //! fn main() { //! let memdb = Arc::new(MemoryDB::new(true)); +//! let hasher = Arc::new(HasherKeccak::new()); //! //! let key = "test-key".as_bytes(); //! let value = "test-value".as_bytes(); //! //! let root = { -//! let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); +//! let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::clone(&hasher)); //! trie.insert(key.to_vec(), value.to_vec()).unwrap(); //! //! let v = trie.get(key).unwrap(); @@ -22,7 +24,7 @@ //! trie.root().unwrap() //! }; //! -//! let mut trie = PatriciaTrie::<_, Keccak256Hash>::from(Arc::clone(&memdb), &root).unwrap(); +//! let mut trie = PatriciaTrie::from(Arc::clone(&memdb), Arc::clone(&hasher), &root).unwrap(); //! let exists = trie.contains(key).unwrap(); //! assert_eq!(exists, true); //! let removed = trie.remove(key).unwrap(); @@ -44,19 +46,3 @@ mod trie; pub use db::{MemoryDB, DB}; pub use errors::{MemDBError, TrieError}; pub use trie::{PatriciaTrie, Trie}; - -pub trait Hasher { - const LENGTH: usize; - - fn digest(data: &[u8]) -> Vec; -} - -pub struct Keccak256Hash; - -impl Hasher for Keccak256Hash { - const LENGTH: usize = 32; - - fn digest(data: &[u8]) -> Vec { - tiny_keccak::keccak256(data).to_vec() - } -} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index d24c301..6a37bcb 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -4,20 +4,22 @@ mod trie_tests { use rand::Rng; use std::sync::Arc; + use hasher::HasherKeccak; + use crate::db::MemoryDB; use crate::trie::{PatriciaTrie, Trie}; - use crate::Keccak256Hash; fn assert_root(data: Vec<(&[u8], &[u8])>, hash: &str) { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); for (k, v) in data.into_iter() { trie.insert(k.to_vec(), v.to_vec()).unwrap(); } let r = trie.root().unwrap(); let rs = format!("0x{}", hex::encode(r.clone())); assert_eq!(rs.as_str(), hash); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::from(Arc::clone(&memdb), &r).unwrap(); + let mut trie = + PatriciaTrie::from(Arc::clone(&memdb), Arc::new(HasherKeccak::new()), &r).unwrap(); let r2 = trie.root().unwrap(); let rs2 = format!("0x{}", hex::encode(r2)); assert_eq!(rs2.as_str(), hash); @@ -550,7 +552,7 @@ mod trie_tests { #[test] fn test_proof_basic() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie.insert(b"doe".to_vec(), b"reindeer".to_vec()).unwrap(); trie.insert(b"dog".to_vec(), b"puppy".to_vec()).unwrap(); trie.insert(b"dogglesworth".to_vec(), b"cat".to_vec()) @@ -611,7 +613,7 @@ mod trie_tests { #[test] fn test_proof_random() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); let mut rng = rand::thread_rng(); let mut keys = vec![]; for _ in 0..100 { @@ -636,7 +638,7 @@ mod trie_tests { #[test] fn test_proof_empty_trie() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie.root().unwrap(); let proof = trie.get_proof(b"not-exist").unwrap(); assert_eq!(proof.len(), 0); @@ -645,7 +647,7 @@ mod trie_tests { #[test] fn test_proof_one_element() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie.insert(b"k".to_vec(), b"v".to_vec()).unwrap(); let root = trie.root().unwrap(); let proof = trie.get_proof(b"k").unwrap(); diff --git a/src/trie.rs b/src/trie.rs index ed56f57..3f430d1 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -1,16 +1,15 @@ use std::cell::RefCell; -use std::marker::PhantomData; use std::rc::Rc; use std::sync::Arc; use hashbrown::{HashMap, HashSet}; +use hasher::Hasher; use rlp::{Prototype, Rlp, RlpStream}; use crate::db::{MemoryDB, DB}; use crate::errors::TrieError; use crate::nibbles::Nibbles; use crate::node::{empty_children, BranchNode, Node}; -use crate::Hasher; pub type TrieResult = Result; @@ -59,12 +58,11 @@ where root_hash: Vec, db: Arc, + hasher: Arc, cache: RefCell, Vec>>, passing_keys: RefCell>>, gen_keys: RefCell>>, - - _phantom_hasher: PhantomData, } impl PatriciaTrie @@ -72,22 +70,21 @@ where D: DB, H: Hasher, { - pub fn new(db: Arc) -> Self { + pub fn new(db: Arc, hasher: Arc) -> Self { Self { root: Node::Empty, - root_hash: H::digest(&rlp::NULL_RLP.to_vec()), + root_hash: hasher.digest(&rlp::NULL_RLP.to_vec()), cache: RefCell::new(HashMap::new()), passing_keys: RefCell::new(HashSet::new()), gen_keys: RefCell::new(HashSet::new()), db, - - _phantom_hasher: PhantomData::, + hasher, } } - pub fn from(db: Arc, root: &[u8]) -> TrieResult { + pub fn from(db: Arc, hasher: Arc, root: &[u8]) -> TrieResult { match db.get(&root).map_err(|e| TrieError::DB(e.to_string()))? { Some(data) => { let mut trie = Self { @@ -99,8 +96,7 @@ where gen_keys: RefCell::new(HashSet::new()), db, - - _phantom_hasher: PhantomData::, + hasher, }; trie.root = trie.decode_node(&data)?; @@ -179,14 +175,14 @@ where ) -> TrieResult>> { let memdb = Arc::new(MemoryDB::new(true)); for node_encoded in proof.into_iter() { - let hash = H::digest(&node_encoded); + let hash = self.hasher.digest(&node_encoded); if root_hash.eq(&hash) || node_encoded.len() >= H::LENGTH { memdb.insert(hash, node_encoded).unwrap(); } } - let trie = - PatriciaTrie::<_, H>::from(memdb, &root_hash).or(Err(TrieError::InvalidProof))?; + let trie = PatriciaTrie::from(memdb, Arc::clone(&self.hasher), &root_hash) + .or(Err(TrieError::InvalidProof))?; trie.get(key).or(Err(TrieError::InvalidProof)) } } @@ -508,7 +504,7 @@ where fn commit(&mut self) -> TrieResult> { let encoded = self.encode_node(self.root.clone()); let root_hash = if encoded.len() < H::LENGTH { - let hash = H::digest(&encoded); + let hash = self.hasher.digest(&encoded); self.cache.borrow_mut().insert(hash.clone(), encoded); hash } else { @@ -557,7 +553,7 @@ where if data.len() < H::LENGTH { data } else { - let hash = H::digest(&data); + let hash = self.hasher.digest(&data); self.cache.borrow_mut().insert(hash.clone(), data); self.gen_keys.borrow_mut().insert(hash.clone()); @@ -675,22 +671,22 @@ mod tests { use std::sync::Arc; use ethereum_types; + use hasher::{Hasher, HasherKeccak}; use super::{PatriciaTrie, Trie}; use crate::db::{MemoryDB, DB}; - use crate::{Hasher, Keccak256Hash}; #[test] fn test_trie_insert() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); } #[test] fn test_trie_get() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); let v = trie.get(b"test").unwrap(); @@ -700,7 +696,7 @@ mod tests { #[test] fn test_trie_random_insert() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); for _ in 0..1000 { let rand_str: String = thread_rng().sample_iter(&Alphanumeric).take(30).collect(); @@ -715,7 +711,7 @@ mod tests { #[test] fn test_trie_contains() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); assert_eq!(true, trie.contains(b"test").unwrap()); assert_eq!(false, trie.contains(b"test2").unwrap()); @@ -724,7 +720,7 @@ mod tests { #[test] fn test_trie_remove() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); let removed = trie.remove(b"test").unwrap(); assert_eq!(true, removed) @@ -733,7 +729,7 @@ mod tests { #[test] fn test_trie_random_remove() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); for _ in 0..1000 { let rand_str: String = thread_rng().sample_iter(&Alphanumeric).take(30).collect(); @@ -749,7 +745,7 @@ mod tests { fn test_trie_from_root() { let memdb = Arc::new(MemoryDB::new(true)); let root = { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test1".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test2".to_vec(), b"test".to_vec()).unwrap(); @@ -759,7 +755,8 @@ mod tests { trie.root().unwrap() }; - let mut trie = PatriciaTrie::<_, Keccak256Hash>::from(Arc::clone(&memdb), &root).unwrap(); + let mut trie = + PatriciaTrie::from(Arc::clone(&memdb), Arc::new(HasherKeccak::new()), &root).unwrap(); let v1 = trie.get(b"test33").unwrap(); assert_eq!(Some(b"test".to_vec()), v1); let v2 = trie.get(b"test44").unwrap(); @@ -772,7 +769,7 @@ mod tests { fn test_trie_from_root_and_insert() { let memdb = Arc::new(MemoryDB::new(true)); let root = { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test1".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test2".to_vec(), b"test".to_vec()).unwrap(); @@ -782,7 +779,8 @@ mod tests { trie.commit().unwrap() }; - let mut trie = PatriciaTrie::<_, Keccak256Hash>::from(Arc::clone(&memdb), &root).unwrap(); + let mut trie = + PatriciaTrie::from(Arc::clone(&memdb), Arc::new(HasherKeccak::new()), &root).unwrap(); trie.insert(b"test55".to_vec(), b"test55".to_vec()).unwrap(); trie.commit().unwrap(); let v = trie.get(b"test55").unwrap(); @@ -793,7 +791,7 @@ mod tests { fn test_trie_from_root_and_delete() { let memdb = Arc::new(MemoryDB::new(true)); let root = { - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test1".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test2".to_vec(), b"test".to_vec()).unwrap(); @@ -803,7 +801,8 @@ mod tests { trie.commit().unwrap() }; - let mut trie = PatriciaTrie::<_, Keccak256Hash>::from(Arc::clone(&memdb), &root).unwrap(); + let mut trie = + PatriciaTrie::from(Arc::clone(&memdb), Arc::new(HasherKeccak::new()), &root).unwrap(); let removed = trie.remove(b"test44").unwrap(); assert_eq!(true, removed); let removed = trie.remove(b"test33").unwrap(); @@ -820,7 +819,7 @@ mod tests { let root1 = { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(k0.as_bytes().to_vec(), v.as_bytes().to_vec()) .unwrap(); trie.root().unwrap() @@ -828,7 +827,7 @@ mod tests { let root2 = { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(k0.as_bytes().to_vec(), v.as_bytes().to_vec()) .unwrap(); trie.insert(k1.as_bytes().to_vec(), v.as_bytes().to_vec()) @@ -840,7 +839,7 @@ mod tests { let root3 = { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie1 = PatriciaTrie::<_, Keccak256Hash>::new(Arc::clone(&memdb)); + let mut trie1 = PatriciaTrie::new(Arc::clone(&memdb), Arc::new(HasherKeccak::new())); trie1 .insert(k0.as_bytes().to_vec(), v.as_bytes().to_vec()) .unwrap(); @@ -850,7 +849,8 @@ mod tests { trie1.root().unwrap(); let root = trie1.root().unwrap(); let mut trie2 = - PatriciaTrie::<_, Keccak256Hash>::from(Arc::clone(&memdb), &root).unwrap(); + PatriciaTrie::from(Arc::clone(&memdb), Arc::new(HasherKeccak::new()), &root) + .unwrap(); trie2.remove(&k1.as_bytes().to_vec()).unwrap(); trie2.root().unwrap() }; @@ -862,7 +862,7 @@ mod tests { #[test] fn test_delete_stale_keys_with_random_insert_and_delete() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); let mut rng = rand::thread_rng(); let mut keys = vec![]; @@ -883,7 +883,7 @@ mod tests { } trie.commit().unwrap(); - let empty_node_key = Keccak256Hash::digest(&rlp::NULL_RLP); + let empty_node_key = HasherKeccak::new().digest(&rlp::NULL_RLP); let value = trie.db.get(empty_node_key.as_ref()).unwrap().unwrap(); assert_eq!(value, &rlp::NULL_RLP) } @@ -891,7 +891,7 @@ mod tests { #[test] fn insert_full_branch() { let memdb = Arc::new(MemoryDB::new(true)); - let mut trie = PatriciaTrie::<_, Keccak256Hash>::new(memdb); + let mut trie = PatriciaTrie::new(memdb, Arc::new(HasherKeccak::new())); trie.insert(b"test".to_vec(), b"test".to_vec()).unwrap(); trie.insert(b"test1".to_vec(), b"test".to_vec()).unwrap();