From ec2cd4febe3c841c3bcc2c6786423af084418321 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Thu, 7 Sep 2017 02:47:45 +0800 Subject: [PATCH 1/9] separate trie from util and make its dependencies into libs: * bytes * hashdb * memorydb * nibbleslice * nibblevec --- Cargo.toml | 8 ++++++ src/lib.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d74e47a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "hashdb" +version = "0.1.0" +authors = ["Parity Technologies "] + +[dependencies] +elastic-array = "0.9" +ethcore-bigint = { path = "../bigint" } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..9162670 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,78 @@ +// Copyright 2015-2017 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Database of byte-slices keyed to their Keccak hash. +extern crate elastic_array; +extern crate ethcore_bigint as bigint; + +use bigint::hash::*; +use std::collections::HashMap; +use elastic_array::ElasticArray128; + +/// `HashDB` value type. +pub type DBValue = ElasticArray128; + +/// Trait modelling datastore keyed by a 32-byte Keccak hash. +pub trait HashDB: AsHashDB + Send + Sync { + /// Get the keys in the database together with number of underlying references. + fn keys(&self) -> HashMap; + + /// Look up a given hash into the bytes that hash to it, returning None if the + /// hash is not known. + fn get(&self, key: &H256) -> Option; + + /// Check for the existance of a hash-key. + fn contains(&self, key: &H256) -> bool; + + /// 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 + /// is considered dead. + fn insert(&mut self, value: &[u8]) -> H256; + + /// Like `insert()` , except you provide the key and the data is all moved. + fn emplace(&mut self, key: H256, value: DBValue); + + /// 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. + fn remove(&mut self, key: &H256); +} + +/// Upcast trait. +pub trait AsHashDB { + /// Perform upcast to HashDB for anything that derives from HashDB. + fn as_hashdb(&self) -> &HashDB; + /// Perform mutable upcast to HashDB for anything that derives from HashDB. + fn as_hashdb_mut(&mut self) -> &mut HashDB; +} + +impl AsHashDB for T { + fn as_hashdb(&self) -> &HashDB { + self + } + fn as_hashdb_mut(&mut self) -> &mut HashDB { + self + } +} + +impl<'a> AsHashDB for &'a mut HashDB { + fn as_hashdb(&self) -> &HashDB { + &**self + } + + fn as_hashdb_mut(&mut self) -> &mut HashDB { + &mut **self + } +} From 039f06b343109c903521eab74b7b8f3ad848dff5 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 10 Nov 2017 18:31:31 +0100 Subject: [PATCH 2/9] prepare cargo configuration for upload of crates --- Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d74e47a..bd4fd8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,9 @@ name = "hashdb" version = "0.1.0" authors = ["Parity Technologies "] +description = "trait for hash-keyed databases." +license = "GPL-3.0" [dependencies] elastic-array = "0.9" -ethcore-bigint = { path = "../bigint" } +ethcore-bigint = { version = "0.1.3", path = "../bigint" } From 54e9759d0f94e52f604402a9468889495ac66970 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 10 Nov 2017 18:46:35 +0100 Subject: [PATCH 3/9] update ethcore-bigint version --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd4fd8a..0060afb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "hashdb" -version = "0.1.0" +version = "0.1.1" authors = ["Parity Technologies "] description = "trait for hash-keyed databases." license = "GPL-3.0" [dependencies] elastic-array = "0.9" -ethcore-bigint = { version = "0.1.3", path = "../bigint" } +ethcore-bigint = { version = "0.2.1", path = "../bigint" } From 1f05776b61a8874c066f64bdacc8471923dcb119 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 10 Jan 2018 13:35:18 +0100 Subject: [PATCH 4/9] dissolve util (#7460) * ethereum-types refactor in progress * ethereum-types refactor in progress * ethereum-types refactor in progress * ethereum-types refactor in progress * ethereum-types refactor finished * removed obsolete util/src/lib.rs * removed commented out code --- Cargo.toml | 2 +- src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0060afb..61b1f2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ license = "GPL-3.0" [dependencies] elastic-array = "0.9" -ethcore-bigint = { version = "0.2.1", path = "../bigint" } +ethereum-types = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 9162670..b65f304 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,11 +16,11 @@ //! Database of byte-slices keyed to their Keccak hash. extern crate elastic_array; -extern crate ethcore_bigint as bigint; +extern crate ethereum_types; -use bigint::hash::*; use std::collections::HashMap; use elastic_array::ElasticArray128; +use ethereum_types::H256; /// `HashDB` value type. pub type DBValue = ElasticArray128; From db703f14dae5ec019c9090ad6dc29d4e271e74de Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 9 Feb 2018 09:32:06 +0100 Subject: [PATCH 5/9] ethabi version 5 (#7723) * Refactor updater to use ethabi-derive * Grumble: do_call type alias * Empty commit to trigger test re-run * migration to ethabi-5.0 * migration to ethabi-5.0 in progress * use ethabi_deriven to generate TransactAcl contract * use ethabi_deriven to generate Registry contract * hash-fetch uses ethabi_derive, removed retain cycle from updater, fixed #7720 * node-filter crate uses ethabi_derive to generate peer_set contract interface * use LruCache in node-filter instead of HashMap * validator_set engine uses ethabi_derive * ethcore does not depend on native_contracts * miner does no depend on native_contracts * secret_store does not use native_contracts (in progress) * removed native-contracts * ethcore and updater does not depend on futures * updated ethereum-types * fixed all warnings caused by using new version of ethereum-types * updated ethabi_derive && ethabi_contract to get rid of warnings * removed another retain cycle in updater, fixed following minor version on update * moved contracts out of native_contracts res * updated ethabi_contract * fixed failing test * fixed failing test * there is no need to create two contracts of the same kind any more * simplify updater::ReleaseTrack conversion into u8 and add several tests for it * applied review suggestions * applied review suggestions --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 61b1f2c..de538f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ license = "GPL-3.0" [dependencies] elastic-array = "0.9" -ethereum-types = "0.1" +ethereum-types = "0.2" From efa9db77e27d357a5d3706e6c790328d5811d068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 22 Mar 2018 09:24:46 +0100 Subject: [PATCH 6/9] Avoid allocations when computing triehash. (#8176) * Avoid allocations when computing triehash. * Bump elastic-array to 0.10 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index de538f5..e6250fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,5 @@ description = "trait for hash-keyed databases." license = "GPL-3.0" [dependencies] -elastic-array = "0.9" +elastic-array = "0.10" ethereum-types = "0.2" From a3f900bef8d4e192eef2ca83a7792be7d75c5f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 2 Apr 2018 13:12:52 +0200 Subject: [PATCH 7/9] Bump ethabi & ethereum-types. (#8258) * Bump ethabi & ethereum-types. * Fix test. * Fix hex encodings. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e6250fb..d4e055f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ license = "GPL-3.0" [dependencies] elastic-array = "0.10" -ethereum-types = "0.2" +ethereum-types = "0.3" From fc8debe105f9316e8571d881880391eddaa5a95c Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Mon, 4 Jun 2018 10:19:50 +0200 Subject: [PATCH 8/9] Update `license header` and `scripts` (#8666) * Update `add_license` script * run script * add `remove duplicate lines script` and run it * Revert changes `English spaces` * strip whitespaces * Revert `GPL` in files with `apache/mit license` * don't append `gpl license` in files with other lic * Don't append `gpl header` in files with other lic. * re-ran script * include c and cpp files too * remove duplicate header * rebase nit --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b65f304..182e81c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. +// Copyright 2015-2018 Parity Technologies (UK) Ltd. // This file is part of Parity. // Parity is free software: you can redistribute it and/or modify From 8b968c0609746cc2369966cd02f21fe298d51b03 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 2 Jul 2018 18:50:05 +0200 Subject: [PATCH 9/9] 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. --- Cargo.toml | 4 ++-- src/lib.rs | 69 +++++++++++++++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d4e055f..f5e63fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "hashdb" -version = "0.1.1" +version = "0.2.0" authors = ["Parity Technologies "] description = "trait for hash-keyed databases." license = "GPL-3.0" [dependencies] elastic-array = "0.10" -ethereum-types = "0.3" +heapsize = "0.4" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 182e81c..5961d90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,65 +14,70 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -//! Database of byte-slices keyed to their Keccak hash. +//! Database of byte-slices keyed to their hash. extern crate elastic_array; -extern crate ethereum_types; +extern crate heapsize; -use std::collections::HashMap; 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. pub type DBValue = ElasticArray128; -/// Trait modelling datastore keyed by a 32-byte Keccak hash. -pub trait HashDB: AsHashDB + Send + Sync { +/// Trait modelling datastore keyed by a hash defined by the `Hasher`. +pub trait HashDB: Send + Sync + AsHashDB { /// Get the keys in the database together with number of underlying references. - fn keys(&self) -> HashMap; + fn keys(&self) -> HashMap; /// Look up a given hash into the bytes that hash to it, returning None if the /// hash is not known. - fn get(&self, key: &H256) -> Option; + fn get(&self, key: &H::Out) -> Option; /// 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 /// are counted and the equivalent number of `remove()`s must be performed before the data /// 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. - fn emplace(&mut self, key: H256, value: DBValue); + /// Like `insert()`, except you provide the key and the data is all moved. + 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 /// 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. -pub trait AsHashDB { +pub trait AsHashDB { /// Perform upcast to HashDB for anything that derives from HashDB. - fn as_hashdb(&self) -> &HashDB; + fn as_hashdb(&self) -> &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; } -impl AsHashDB for T { - fn as_hashdb(&self) -> &HashDB { - self - } - fn as_hashdb_mut(&mut self) -> &mut HashDB { - self - } +// NOTE: There used to be a `impl 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 +// This means we need concrete impls of AsHashDB in several places, which somewhat defeats the point of the trait. +impl<'a, H: Hasher> AsHashDB for &'a mut HashDB { + fn as_hashdb(&self) -> &HashDB { &**self } + fn as_hashdb_mut(&mut self) -> &mut HashDB { &mut **self } } -impl<'a> AsHashDB for &'a mut HashDB { - fn as_hashdb(&self) -> &HashDB { - &**self - } - - fn as_hashdb_mut(&mut self) -> &mut HashDB { - &mut **self - } -}