Make HashDB generic (#8739)

The `patricia_trie` crate is generic over the hasher (by way of HashDB) and node encoding scheme. Adds a new `patricia_trie_ethereum` crate with concrete impls for Keccak/RLP.
This commit is contained in:
David 2018-07-02 18:50:05 +02:00 committed by GitHub
parent fc8debe105
commit 8b968c0609
2 changed files with 39 additions and 34 deletions

View File

@ -1,10 +1,10 @@
[package] [package]
name = "hashdb" name = "hashdb"
version = "0.1.1" version = "0.2.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
description = "trait for hash-keyed databases." description = "trait for hash-keyed databases."
license = "GPL-3.0" license = "GPL-3.0"
[dependencies] [dependencies]
elastic-array = "0.10" elastic-array = "0.10"
ethereum-types = "0.3" heapsize = "0.4"

View File

@ -14,65 +14,70 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Database of byte-slices keyed to their Keccak hash. //! Database of byte-slices keyed to their hash.
extern crate elastic_array; extern crate elastic_array;
extern crate ethereum_types; extern crate heapsize;
use std::collections::HashMap;
use elastic_array::ElasticArray128; use elastic_array::ElasticArray128;
use ethereum_types::H256; use heapsize::HeapSizeOf;
use std::collections::HashMap;
use std::{fmt::Debug, hash::Hash};
/// Trait describing an object that can hash a slice of bytes. Used to abstract
/// other types over the hashing algorithm. Defines a single `hash` method and an
/// `Out` associated type with the necessary bounds.
pub trait Hasher: Sync + Send {
/// The output type of the `Hasher`
type Out: AsRef<[u8]> + AsMut<[u8]> + Default + HeapSizeOf + Debug + PartialEq + Eq + Hash + Send + Sync + Clone + Copy;
/// What to use to build `HashMap`s with this `Hasher`
type StdHasher: Sync + Send + Default + std::hash::Hasher;
/// The length in bytes of the `Hasher` output
const LENGTH: usize;
/// Compute the hash of the provided slice of bytes returning the `Out` type of the `Hasher`
fn hash(x: &[u8]) -> Self::Out;
}
/// `HashDB` value type. /// `HashDB` value type.
pub type DBValue = ElasticArray128<u8>; pub type DBValue = ElasticArray128<u8>;
/// Trait modelling datastore keyed by a 32-byte Keccak hash. /// Trait modelling datastore keyed by a hash defined by the `Hasher`.
pub trait HashDB: AsHashDB + Send + Sync { pub trait HashDB<H: Hasher>: Send + Sync + AsHashDB<H> {
/// Get the keys in the database together with number of underlying references. /// Get the keys in the database together with number of underlying references.
fn keys(&self) -> HashMap<H256, i32>; fn keys(&self) -> HashMap<H::Out, i32>;
/// Look up a given hash into the bytes that hash to it, returning None if the /// Look up a given hash into the bytes that hash to it, returning None if the
/// hash is not known. /// hash is not known.
fn get(&self, key: &H256) -> Option<DBValue>; fn get(&self, key: &H::Out) -> Option<DBValue>;
/// Check for the existance of a hash-key. /// Check for the existance of a hash-key.
fn contains(&self, key: &H256) -> bool; fn contains(&self, key: &H::Out) -> bool;
/// Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions /// Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions
/// are counted and the equivalent number of `remove()`s must be performed before the data /// are counted and the equivalent number of `remove()`s must be performed before the data
/// is considered dead. /// is considered dead.
fn insert(&mut self, value: &[u8]) -> H256; fn insert(&mut self, value: &[u8]) -> H::Out;
/// Like `insert()` , except you provide the key and the data is all moved. /// Like `insert()`, except you provide the key and the data is all moved.
fn emplace(&mut self, key: H256, value: DBValue); fn emplace(&mut self, key: H::Out, value: DBValue);
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may /// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may
/// happen without the data being eventually being inserted into the DB. It can be "owed" more than once. /// happen without the data being eventually being inserted into the DB. It can be "owed" more than once.
fn remove(&mut self, key: &H256); fn remove(&mut self, key: &H::Out);
} }
/// Upcast trait. /// Upcast trait.
pub trait AsHashDB { pub trait AsHashDB<H: Hasher> {
/// Perform upcast to HashDB for anything that derives from HashDB. /// Perform upcast to HashDB for anything that derives from HashDB.
fn as_hashdb(&self) -> &HashDB; fn as_hashdb(&self) -> &HashDB<H>;
/// Perform mutable upcast to HashDB for anything that derives from HashDB. /// Perform mutable upcast to HashDB for anything that derives from HashDB.
fn as_hashdb_mut(&mut self) -> &mut HashDB; fn as_hashdb_mut(&mut self) -> &mut HashDB<H>;
} }
impl<T: HashDB> AsHashDB for T { // NOTE: There used to be a `impl<T> AsHashDB for T` but that does not work with generics. See https://stackoverflow.com/questions/48432842/implementing-a-trait-for-reference-and-non-reference-types-causes-conflicting-im
fn as_hashdb(&self) -> &HashDB { // This means we need concrete impls of AsHashDB in several places, which somewhat defeats the point of the trait.
self impl<'a, H: Hasher> AsHashDB<H> for &'a mut HashDB<H> {
} fn as_hashdb(&self) -> &HashDB<H> { &**self }
fn as_hashdb_mut(&mut self) -> &mut HashDB { fn as_hashdb_mut(&mut self) -> &mut HashDB<H> { &mut **self }
self
}
} }
impl<'a> AsHashDB for &'a mut HashDB {
fn as_hashdb(&self) -> &HashDB {
&**self
}
fn as_hashdb_mut(&mut self) -> &mut HashDB {
&mut **self
}
}