separate trie from util and make its dependencies into libs:

* bytes
* hashdb
* memorydb
* nibbleslice
* nibblevec
This commit is contained in:
Hawstein
2017-09-07 02:47:45 +08:00
parent 79659bdc76
commit ade37be25b
182 changed files with 497 additions and 251 deletions

View File

@@ -30,6 +30,12 @@ lru-cache = "0.1.0"
ethcore-logger = { path = "../logger" }
triehash = { path = "triehash" }
error-chain = "0.11.0-rc.2"
hashdb = { path = "hashdb" }
trie = { path = "trie" }
nibbleslice = { path = "nibbleslice" }
nibblevec = { path = "nibblevec" }
ethcore-bytes = { path = "bytes" }
memorydb = { path = "memorydb" }
[features]
default = []

View File

@@ -19,16 +19,19 @@
extern crate test;
extern crate triehash;
extern crate ethcore_util;
extern crate ethcore_bytes;
extern crate ethcore_bigint;
extern crate memorydb;
extern crate trie;
#[macro_use]
extern crate log;
extern crate hash;
use test::{Bencher, black_box};
use ethcore_bigint::hash::*;
use ethcore_util::bytes::*;
use ethcore_util::trie::*;
use ethcore_util::memorydb::*;
use ethcore_bytes::*;
use trie::*;
use memorydb::*;
use triehash::*;
use hash::keccak;

6
util/bytes/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "ethcore-bytes"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]

8
util/hashdb/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "hashdb"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
elastic-array = "0.9"
ethcore-bigint = { path = "../bigint" }

View File

@@ -15,6 +15,9 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! 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;
@@ -29,59 +32,14 @@ pub trait HashDB: AsHashDB + Send + Sync {
/// Look up a given hash into the bytes that hash to it, returning None if the
/// hash is not known.
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let hello_bytes = "Hello world!".as_bytes();
/// let hash = m.insert(hello_bytes);
/// assert_eq!(m.get(&hash).unwrap(), hello_bytes);
/// }
/// ```
fn get(&self, key: &H256) -> Option<DBValue>;
/// Check for the existance of a hash-key.
///
/// # Examples
/// ```rust
/// extern crate hash;
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// use hash::keccak;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let hello_bytes = "Hello world!".as_bytes();
/// assert!(!m.contains(&keccak(hello_bytes)));
/// let key = m.insert(hello_bytes);
/// assert!(m.contains(&key));
/// m.remove(&key);
/// assert!(!m.contains(&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.
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// extern crate ethcore_bigint;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// use ethcore_bigint::hash::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let key = m.insert("Hello world!".as_bytes());
/// assert!(m.contains(&key));
/// }
/// ```
fn insert(&mut self, value: &[u8]) -> H256;
/// Like `insert()` , except you provide the key and the data is all moved.
@@ -89,30 +47,6 @@ pub trait HashDB: AsHashDB + Send + Sync {
/// 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.
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// extern crate hash;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// use hash::keccak;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let d = "Hello world!".as_bytes();
/// let key = &keccak(d);
/// m.remove(key); // OK - we now owe an insertion.
/// assert!(!m.contains(key));
/// m.remove(key); // OK - we now owe two insertions.
/// assert!(!m.contains(key));
/// m.insert(d); // OK - still owed.
/// assert!(!m.contains(key));
/// m.insert(d); // OK - now it's "empty" again.
/// assert!(!m.contains(key));
/// m.insert(d); // OK - now we've
/// assert_eq!(m.get(key).unwrap(), d);
/// }
/// ```
fn remove(&mut self, key: &H256);
}

13
util/memorydb/Cargo.toml Normal file
View File

@@ -0,0 +1,13 @@
[package]
name = "memorydb"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
bigint = "4.0"
elastic-array = "0.9"
heapsize = "0.4"
ethcore-bigint = { path = "../bigint", features = ["heapsizeof"] }
hash = { path = "../hash" }
hashdb = { path = "../hashdb" }
rlp = { path = "../rlp" }

View File

@@ -15,6 +15,11 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Reference-counted memory-based `HashDB` implementation.
extern crate heapsize;
extern crate ethcore_bigint as bigint;
extern crate rlp;
extern crate hash as keccak;
extern crate hashdb;
use std::mem;
use std::collections::HashMap;
@@ -34,9 +39,10 @@ use hashdb::*;
///
/// # Example
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// extern crate hashdb;
/// extern crate memorydb;
/// use hashdb::*;
/// use memorydb::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let d = "Hello world!".as_bytes();
@@ -85,9 +91,10 @@ impl MemoryDB {
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// extern crate hashdb;
/// extern crate memorydb;
/// use hashdb::*;
/// use memorydb::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let hello_bytes = "Hello world!".as_bytes();

View File

@@ -25,6 +25,7 @@ rustc-serialize = "0.3"
ethcore-io = { path = "../io" }
ethcore-util = { path = ".." }
ethcore-bigint = { path = "../bigint" }
ethcore-bytes = { path = "../bytes" }
ethcore-devtools = { path = "../../devtools" }
ethkey = { path = "../../ethkey" }
ethcrypto = { path = "../../ethcrypto" }

View File

@@ -23,7 +23,7 @@ use mio::{Token, Ready, PollOpt};
use mio::deprecated::{Handler, EventLoop, TryRead, TryWrite};
use mio::tcp::*;
use bigint::hash::*;
use util::bytes::*;
use ethcore_bytes::*;
use rlp::*;
use std::io::{self, Cursor, Read, Write};
use error::*;
@@ -510,7 +510,7 @@ mod tests {
use std::io::{Read, Write, Error, Cursor, ErrorKind};
use mio::{Ready};
use std::collections::VecDeque;
use util::bytes::Bytes;
use ethcore_bytes::Bytes;
use devtools::*;
use io::*;

View File

@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use util::bytes::Bytes;
use ethcore_bytes::Bytes;
use std::net::SocketAddr;
use std::collections::{HashSet, HashMap, BTreeMap, VecDeque};
use std::mem;

View File

@@ -19,7 +19,7 @@ use rand::random;
use hash::write_keccak;
use mio::tcp::*;
use bigint::hash::*;
use util::bytes::Bytes;
use ethcore_bytes::Bytes;
use rlp::*;
use connection::{Connection};
use host::{HostInfo};

View File

@@ -60,6 +60,7 @@
extern crate ethcore_io as io;
extern crate ethcore_util as util;
extern crate ethcore_bigint as bigint;
extern crate ethcore_bytes;
extern crate parking_lot;
extern crate mio;
extern crate tiny_keccak;

View File

@@ -20,7 +20,7 @@ use std::sync::Arc;
use std::thread;
use std::time::*;
use parking_lot::Mutex;
use util::Bytes;
use ethcore_bytes::Bytes;
use io::TimerToken;
use ethkey::{Random, Generator};

View File

@@ -0,0 +1,7 @@
[package]
name = "nibbleslice"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
elastic-array = "0.9"

View File

@@ -15,6 +15,8 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Nibble-orientated view onto byte-slice, allowing nibble-precision offsets.
extern crate elastic_array;
use std::cmp::*;
use std::fmt;
use elastic_array::ElasticArray36;
@@ -25,8 +27,8 @@ use elastic_array::ElasticArray36;
///
/// # Example
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::nibbleslice::*;
/// extern crate nibbleslice;
/// use nibbleslice::*;
/// fn main() {
/// let d1 = &[0x01u8, 0x23, 0x45];
/// let d2 = &[0x34u8, 0x50, 0x12];

View File

@@ -0,0 +1,8 @@
[package]
name = "nibblevec"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
elastic-array = "0.9"
nibbleslice = { path = "../nibbleslice" }

View File

@@ -16,6 +16,8 @@
//! An owning, nibble-oriented byte vector.
extern crate nibbleslice;
extern crate elastic_array;
use nibbleslice::NibbleSlice;
use elastic_array::ElasticArray36;

View File

@@ -21,13 +21,13 @@ use std::collections::hash_map::Entry;
use std::sync::Arc;
use rlp::*;
use hashdb::*;
use memorydb::*;
use super::super::memorydb::*;
use super::{DB_PREFIX_LEN, LATEST_ERA_KEY};
use super::traits::JournalDB;
use kvdb::{KeyValueDB, DBTransaction};
use bigint::hash::H256;
use error::{BaseDataError, UtilError};
use {Bytes};
use bytes::Bytes;
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
/// and latent-removal semantics.

View File

@@ -30,7 +30,7 @@ use super::traits::JournalDB;
use kvdb::{KeyValueDB, DBTransaction};
use bigint::hash::H256;
use error::{BaseDataError, UtilError};
use {Bytes};
use bytes::Bytes;
#[derive(Clone, PartialEq, Eq)]
struct RefInfo {

View File

@@ -29,7 +29,7 @@ use kvdb::{KeyValueDB, DBTransaction};
use super::JournalDB;
use bigint::hash::{H256, H256FastMap};
use error::{BaseDataError, UtilError};
use {Bytes};
use bytes::Bytes;
/// Implementation of the `JournalDB` trait for a disk-backed database with a memory overlay
/// and, possibly, latent-removal semantics.

View File

@@ -27,7 +27,8 @@ use super::{DB_PREFIX_LEN, LATEST_ERA_KEY};
use super::traits::JournalDB;
use kvdb::{KeyValueDB, DBTransaction};
use bigint::hash::H256;
use {UtilError, Bytes};
use UtilError;
use bytes::Bytes;
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
/// and latent-removal semantics.

View File

@@ -20,7 +20,8 @@ use std::sync::Arc;
use hashdb::*;
use kvdb::{self, DBTransaction};
use bigint::hash::H256;
use {Bytes, UtilError};
use UtilError;
use bytes::Bytes;
/// A `HashDB` which can manage a short-term journal potentially containing many forks of mutually
/// exclusive actions.

View File

@@ -28,7 +28,8 @@ use hashdb::DBValue;
use rlp::{UntrustedRlp, RlpType, Compressible};
use rocksdb::{DB, Writable, WriteBatch, WriteOptions, IteratorMode, DBIterator,
Options, DBCompactionStyle, BlockBasedOptions, Direction, Cache, Column, ReadOptions};
use {UtilError, Bytes};
use UtilError;
use bytes::Bytes;
#[cfg(target_os = "linux")]

View File

@@ -96,6 +96,7 @@ extern crate ethcore_devtools as devtools;
extern crate libc;
extern crate target_info;
extern crate ethcore_bigint as bigint;
extern crate ethcore_bytes as bytes;
extern crate parking_lot;
extern crate tiny_keccak;
extern crate rlp;
@@ -104,6 +105,9 @@ extern crate lru_cache;
extern crate heapsize;
extern crate ethcore_logger;
extern crate hash as keccak;
extern crate hashdb;
extern crate memorydb;
extern crate trie;
#[macro_use]
extern crate error_chain;
@@ -114,17 +118,11 @@ extern crate log as rlog;
#[macro_use]
pub mod common;
pub mod error;
pub mod bytes;
pub mod misc;
pub mod hashdb;
pub mod memorydb;
pub mod migration;
pub mod overlaydb;
pub mod journaldb;
pub mod kvdb;
pub mod trie;
pub mod nibbleslice;
pub mod nibblevec;
pub mod snappy;
pub mod cache;
@@ -133,10 +131,8 @@ pub use hashdb::*;
pub use memorydb::MemoryDB;
pub use overlaydb::*;
pub use journaldb::JournalDB;
pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut};
pub use kvdb::*;
pub use error::UtilError;
pub use bytes::*;
/// 160-bit integer representing account address
pub type Address = bigint::hash::H160;

View File

@@ -18,7 +18,7 @@
use rlp::RlpStream;
use target_info::Target;
use Bytes;
use bytes::Bytes;
include!(concat!(env!("OUT_DIR"), "/version.rs"));
include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));

21
util/trie/Cargo.toml Normal file
View File

@@ -0,0 +1,21 @@
[package]
name = "trie"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
elastic-array = "0.9"
log = "0.3"
rand = "0.3"
ethcore-bytes = { path = "../bytes" }
ethcore-bigint = { path = "../bigint" }
hash = { path = "../hash" }
hashdb = { path = "../hashdb" }
rlp = { path = "../rlp" }
nibbleslice = { path = "../nibbleslice" }
nibblevec = { path = "../nibblevec" }
triehash = { path = "../triehash" }
memorydb = { path = "../memorydb" }
ethcore-logger = { path = "../../logger" }

View File

@@ -105,7 +105,8 @@ impl<'db> Iterator for FatDBIterator<'db> {
fn fatdb_to_trie() {
use memorydb::MemoryDB;
use hashdb::DBValue;
use trie::{FatDBMut, TrieMut};
use super::fatdbmut::FatDBMut;
use super::TrieMut;
let mut memdb = MemoryDB::new();
let mut root = H256::default();

View File

@@ -15,6 +15,20 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Trie interface and implementation.
extern crate rand;
extern crate ethcore_bigint as bigint;
extern crate hash as keccak;
extern crate rlp;
extern crate hashdb;
extern crate ethcore_bytes as bytes;
extern crate nibbleslice;
extern crate nibblevec;
extern crate elastic_array;
extern crate memorydb;
extern crate ethcore_logger;
#[macro_use]
extern crate log;
use std::fmt;
use bigint::hash::H256;

View File

@@ -18,7 +18,7 @@
use keccak::keccak;
use bigint::hash::H256;
use Bytes;
use bytes::Bytes;
/// A record of a visited node.
#[derive(PartialEq, Eq, Debug, Clone)]
@@ -136,7 +136,7 @@ mod tests {
#[test]
fn trie_record() {
use trie::{TrieDB, TrieDBMut, Trie, TrieMut};
use super::super::{TrieDB, TrieDBMut, Trie, TrieMut};
use memorydb::MemoryDB;
let mut db = MemoryDB::new();

View File

@@ -71,7 +71,7 @@ fn trie_to_sectrie() {
use memorydb::MemoryDB;
use hashdb::DBValue;
use super::triedbmut::TrieDBMut;
use super::super::TrieMut;
use super::TrieMut;
let mut memdb = MemoryDB::new();
let mut root = H256::default();

View File

@@ -22,7 +22,7 @@ use super::node::{Node, OwnedNode};
use super::lookup::Lookup;
use super::{Trie, TrieItem, TrieError, TrieIterator, Query};
use bigint::hash::H256;
use {ToPretty, Bytes};
use bytes::{ToPretty, Bytes};
/// A `Trie` implementation using a generic `HashDB` backing database.
///
@@ -31,12 +31,14 @@ use {ToPretty, Bytes};
///
/// # Example
/// ```
/// extern crate ethcore_util as util;
/// extern crate trie;
/// extern crate hashdb;
/// extern crate memorydb;
/// extern crate ethcore_bigint as bigint;
///
/// use util::trie::*;
/// use util::hashdb::*;
/// use util::memorydb::*;
/// use trie::*;
/// use hashdb::*;
/// use memorydb::*;
/// use bigint::hash::*;
///
/// fn main() {

View File

@@ -21,10 +21,10 @@ use super::lookup::Lookup;
use super::node::Node as RlpNode;
use super::node::NodeKey;
use ::HashDB;
use ::bytes::ToPretty;
use ::nibbleslice::NibbleSlice;
use ::rlp::{Rlp, RlpStream};
use hashdb::HashDB;
use bytes::ToPretty;
use nibbleslice::NibbleSlice;
use rlp::{Rlp, RlpStream};
use hashdb::DBValue;
use std::collections::{HashSet, VecDeque};
@@ -261,14 +261,16 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage {
///
/// # Example
/// ```
/// extern crate ethcore_util as util;
/// extern crate trie;
/// extern crate hashdb;
/// extern crate memorydb;
/// extern crate ethcore_bigint as bigint;
/// extern crate hash;
///
/// use hash::KECCAK_NULL_RLP;
/// use util::trie::*;
/// use util::hashdb::*;
/// use util::memorydb::*;
/// use trie::*;
/// use hashdb::*;
/// use memorydb::*;
/// use bigint::hash::*;
///
/// fn main() {