diff --git a/memory-db/Cargo.toml b/memory-db/Cargo.toml index 7d381189..a25d06db 100644 --- a/memory-db/Cargo.toml +++ b/memory-db/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "memory-db" -version = "0.33.0" +version = "0.34.0" authors = ["Parity Technologies "] description = "In-memory implementation of hash-db, useful for tests" repository = "https://github.com/paritytech/trie" @@ -10,6 +10,7 @@ edition = "2018" [dependencies] hash-db = { version = "0.16.0", path = "../hash-db", default-features = false } hashbrown = "0.15.3" +foldhash = "0.1.5" [dev-dependencies] keccak-hasher = { path = "../test-support/keccak-hasher" } diff --git a/memory-db/src/lib.rs b/memory-db/src/lib.rs index 3d79bca3..4c45b793 100644 --- a/memory-db/src/lib.rs +++ b/memory-db/src/lib.rs @@ -19,6 +19,7 @@ #[cfg(not(feature = "std"))] extern crate alloc; +use core::hash::BuildHasher; use hash_db::{ AsHashDB, AsPlainDB, HashDB, HashDBRef, Hasher as KeyHasher, MaybeDebug, PlainDB, PlainDBRef, Prefix, @@ -26,12 +27,15 @@ use hash_db::{ #[cfg(feature = "std")] use std::{ borrow::Borrow, cmp::Eq, collections::hash_map::Entry, collections::HashMap as Map, hash, - marker::PhantomData, mem, + hash::RandomState, marker::PhantomData, mem, }; #[cfg(not(feature = "std"))] use hashbrown::{hash_map::Entry, HashMap as Map}; +#[cfg(not(feature = "std"))] +use foldhash::quality::RandomState; + #[cfg(not(feature = "std"))] use core::{borrow::Borrow, cmp::Eq, hash, marker::PhantomData, mem}; @@ -80,22 +84,23 @@ use alloc::vec::Vec; /// m.remove(&k, EMPTY_PREFIX); /// assert!(!m.contains(&k, EMPTY_PREFIX)); /// ``` -pub struct MemoryDB +pub struct MemoryDB where H: KeyHasher, KF: KeyFunction, { - data: Map, + data: Map, hashed_null_node: H::Out, null_node_data: T, _kf: PhantomData, } -impl Clone for MemoryDB +impl Clone for MemoryDB where H: KeyHasher, KF: KeyFunction, T: Clone, + S: Clone, { fn clone(&self) -> Self { Self { @@ -107,13 +112,14 @@ where } } -impl PartialEq> for MemoryDB +impl PartialEq> for MemoryDB where H: KeyHasher, KF: KeyFunction, T: Eq + MaybeDebug, + S: BuildHasher, { - fn eq(&self, other: &MemoryDB) -> bool { + fn eq(&self, other: &MemoryDB) -> bool { for a in self.data.iter() { match other.data.get(a.0) { Some(v) if v != a.1 => return false, @@ -125,11 +131,12 @@ where } } -impl Eq for MemoryDB +impl Eq for MemoryDB where H: KeyHasher, KF: KeyFunction, T: Eq + MaybeDebug, + S: BuildHasher, { } @@ -238,7 +245,7 @@ pub fn legacy_prefixed_key(key: &H::Out, prefix: Prefix) -> Vec Default for MemoryDB +impl Default for MemoryDB where H: KeyHasher, T: for<'a> From<&'a [u8]>, @@ -250,11 +257,12 @@ where } /// Create a new `MemoryDB` from a given null key/data -impl MemoryDB +impl MemoryDB where H: KeyHasher, T: Default, KF: KeyFunction, + S: BuildHasher + Default, { /// Remove an element and delete it from storage if reference count reaches zero. /// If the value was purged, return the old value. @@ -290,16 +298,22 @@ where } } -impl MemoryDB +impl MemoryDB where H: KeyHasher, T: for<'a> From<&'a [u8]>, KF: KeyFunction, + S: BuildHasher + Default, { /// Create a new `MemoryDB` from a given null key/data pub fn from_null_node(null_key: &[u8], null_node_data: T) -> Self { + Self::from_null_node_with_hasher(null_key, null_node_data, S::default()) + } + + /// Create a new `MemoryDB` from a given null key/data with a custom hasher. + pub fn from_null_node_with_hasher(null_key: &[u8], null_node_data: T, hasher: S) -> Self { MemoryDB { - data: Map::default(), + data: Map::with_hasher(hasher), hashed_null_node: H::hash(null_key), null_node_data, _kf: Default::default(), @@ -313,12 +327,17 @@ where /// Create a new default instance of `Self` and returns `Self` and the root hash. pub fn default_with_root() -> (Self, H::Out) { - let db = Self::default(); + let db = Self::new(&[0u8][..]); let root = db.hashed_null_node; (db, root) } + /// Create a new instance of `Self` with a custom hasher. + pub fn with_hasher(hasher: S) -> Self { + Self::from_null_node_with_hasher(&[0u8][..], [0u8][..].into(), hasher) + } + /// Clear all data from the database. /// /// # Examples @@ -353,7 +372,7 @@ where } /// Return the internal key-value Map, clearing the current state. - pub fn drain(&mut self) -> Map { + pub fn drain(&mut self) -> Map { mem::take(&mut self.data) } @@ -396,12 +415,13 @@ where } } -impl PlainDB for MemoryDB +impl PlainDB for MemoryDB where H: KeyHasher, T: Default + PartialEq + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: Send + Sync + KeyFunction, KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>, + S: BuildHasher + Default, { fn get(&self, key: &H::Out) -> Option { match self.data.get(key.as_ref()) { @@ -446,12 +466,13 @@ where } } -impl PlainDBRef for MemoryDB +impl PlainDBRef for MemoryDB where H: KeyHasher, T: Default + PartialEq + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: Send + Sync + KeyFunction, KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>, + S: BuildHasher + Default, { fn get(&self, key: &H::Out) -> Option { PlainDB::get(self, key) @@ -461,11 +482,12 @@ where } } -impl HashDB for MemoryDB +impl HashDB for MemoryDB where H: KeyHasher, T: Default + PartialEq + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction + Send + Sync, + S: BuildHasher + Default, { fn get(&self, key: &H::Out, prefix: Prefix) -> Option { if key == &self.hashed_null_node { @@ -540,11 +562,12 @@ where } } -impl HashDBRef for MemoryDB +impl HashDBRef for MemoryDB where H: KeyHasher, T: Default + PartialEq + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction + Send + Sync, + S: BuildHasher + Default, { fn get(&self, key: &H::Out, prefix: Prefix) -> Option { HashDB::get(self, key, prefix) @@ -554,12 +577,13 @@ where } } -impl AsPlainDB for MemoryDB +impl AsPlainDB for MemoryDB where H: KeyHasher, T: Default + PartialEq + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction + Send + Sync, KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>, + S: BuildHasher + Default, { fn as_plain_db(&self) -> &dyn PlainDB { self @@ -569,11 +593,12 @@ where } } -impl AsHashDB for MemoryDB +impl AsHashDB for MemoryDB where H: KeyHasher, T: Default + PartialEq + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction + Send + Sync, + S: BuildHasher + Default, { fn as_hash_db(&self) -> &dyn HashDB { self diff --git a/test-support/trie-bench/Cargo.toml b/test-support/trie-bench/Cargo.toml index 8244d27b..32104ffa 100644 --- a/test-support/trie-bench/Cargo.toml +++ b/test-support/trie-bench/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "trie-bench" description = "Standard benchmarking suite for tries" -version = "0.41.0" +version = "0.42.0" authors = ["Parity Technologies "] repository = "https://github.com/paritytech/trie/" license = "Apache-2.0" @@ -11,7 +11,7 @@ edition = "2018" keccak-hasher = { path = "../keccak-hasher", version = "0.16.0" } trie-standardmap = { path = "../trie-standardmap", version = "0.16.0" } hash-db = { path = "../../hash-db" , version = "0.16.0"} -memory-db = { path = "../../memory-db", version = "0.33.0" } +memory-db = { path = "../../memory-db", version = "0.34.0" } trie-root = { path = "../../trie-root", version = "0.18.0" } trie-db = { path = "../../trie-db", version = "0.30.0" } criterion = "0.5.1" diff --git a/trie-db/test/Cargo.toml b/trie-db/test/Cargo.toml index 3adcc457..aac2df62 100644 --- a/trie-db/test/Cargo.toml +++ b/trie-db/test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trie-db-test" -version = "0.30.0" +version = "0.31.0" authors = ["Parity Technologies "] description = "Tests for trie-db crate" repository = "https://github.com/paritytech/trie" @@ -14,7 +14,7 @@ harness = false [dependencies] trie-db = { path = "..", version = "0.30.0"} hash-db = { path = "../../hash-db", version = "0.16.0"} -memory-db = { path = "../../memory-db", version = "0.33.0" } +memory-db = { path = "../../memory-db", version = "0.34.0" } rand = { version = "0.8", default-features = false, features = ["small_rng"] } trie-standardmap = { path = "../../test-support/trie-standardmap", version = "0.16.0" } reference-trie = { path = "../../test-support/reference-trie", version = "0.29.0" } diff --git a/trie-eip1186/test/Cargo.toml b/trie-eip1186/test/Cargo.toml index 28049e9b..222a8bc6 100644 --- a/trie-eip1186/test/Cargo.toml +++ b/trie-eip1186/test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trie-eip1186-test" -version = "0.6.0" +version = "0.7.0" authors = ["Parity Technologies "] description = "Tests for trie-eip1186 crate" repository = "https://github.com/paritytech/trie" @@ -12,4 +12,4 @@ trie-eip1186 = { path = "..", version = "0.5.0"} trie-db = { path = "../../trie-db", version = "0.30.0"} hash-db = { path = "../../hash-db", version = "0.16.0"} reference-trie = { path = "../../test-support/reference-trie", version = "0.29.0" } -memory-db = { path = "../../memory-db", version = "0.33.0" } +memory-db = { path = "../../memory-db", version = "0.34.0" }