triehash is separated from util (#6428)

This commit is contained in:
Marek Kotewicz
2017-09-03 09:11:14 +02:00
committed by Gav Wood
parent 7b8af30590
commit b731ccea18
24 changed files with 124 additions and 139 deletions

View File

@@ -33,6 +33,7 @@ ethcore-bloom-journal = { path = "bloom" }
regex = "0.2"
lru-cache = "0.1.0"
ethcore-logger = { path = "../logger" }
triehash = { path = "triehash" }
[features]
default = []

View File

@@ -17,6 +17,7 @@
#![feature(test)]
extern crate test;
extern crate triehash;
extern crate ethcore_util;
#[macro_use]
extern crate log;
@@ -27,7 +28,7 @@ use ethcore_util::hash::*;
use ethcore_util::bytes::*;
use ethcore_util::trie::*;
use ethcore_util::memorydb::*;
use ethcore_util::triehash::*;
use triehash::*;
use hash::keccak;
fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut H256) -> Vec<u8> {

View File

@@ -117,14 +117,12 @@ pub mod common;
pub mod error;
pub mod bytes;
pub mod misc;
pub mod vector;
pub mod hashdb;
pub mod memorydb;
pub mod migration;
pub mod overlaydb;
pub mod journaldb;
pub mod kvdb;
pub mod triehash;
pub mod trie;
pub mod nibbleslice;
pub mod nibblevec;
@@ -136,12 +134,10 @@ pub use hashdb::*;
pub use memorydb::MemoryDB;
pub use overlaydb::*;
pub use journaldb::JournalDB;
pub use triehash::*;
pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut};
pub use kvdb::*;
pub use error::*;
pub use bytes::*;
pub use vector::*;
pub use bigint::prelude::*;
pub use bigint::hash;

View File

@@ -937,7 +937,8 @@ impl<'a> Drop for TrieDBMut<'a> {
#[cfg(test)]
mod tests {
use triehash::trie_root;
extern crate triehash;
use self::triehash::trie_root;
use hashdb::*;
use memorydb::*;
use super::*;

View File

@@ -1,68 +0,0 @@
// 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 <http://www.gnu.org/licenses/>.
//! Vector extensions.
/// Returns len of prefix shared with elem
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::vector::SharedPrefix;
///
/// fn main () {
/// let a = vec![1,2,3,3,5];
/// let b = vec![1,2,3];
/// assert_eq!(a.shared_prefix_len(&b), 3);
/// }
/// ```
pub trait SharedPrefix<T> {
/// Get common prefix length
fn shared_prefix_len(&self, elem: &[T]) -> usize;
}
impl<T> SharedPrefix<T> for [T] where T: Eq {
fn shared_prefix_len(&self, elem: &[T]) -> usize {
use std::cmp;
let len = cmp::min(self.len(), elem.len());
(0..len).take_while(|&i| self[i] == elem[i]).count()
}
}
#[cfg(test)]
mod test {
use vector::SharedPrefix;
#[test]
fn test_shared_prefix() {
let a = vec![1,2,3,4,5,6];
let b = vec![4,2,3,4,5,6];
assert_eq!(a.shared_prefix_len(&b), 0);
}
#[test]
fn test_shared_prefix2() {
let a = vec![1,2,3,3,5];
let b = vec![1,2,3];
assert_eq!(a.shared_prefix_len(&b), 3);
}
#[test]
fn test_shared_prefix3() {
let a = vec![1,2,3,4,5,6];
let b = vec![1,2,3,4,5,6];
assert_eq!(a.shared_prefix_len(&b), 6);
}
}

9
util/triehash/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "triehash"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
rlp = { path = "../rlp" }
ethcore-bigint = { path = "../bigint" }
hash = { path = "../hash" }

View File

@@ -18,26 +18,31 @@
//!
//! This module should be used to generate trie root hash.
extern crate ethcore_bigint;
extern crate hash;
extern crate rlp;
use std::collections::BTreeMap;
use std::cmp;
use hash::*;
use keccak::keccak;
use rlp;
use ethcore_bigint::hash::H256;
use hash::keccak;
use rlp::RlpStream;
use vector::SharedPrefix;
fn shared_prefix_len<T: Eq>(first: &[T], second: &[T]) -> usize {
let len = cmp::min(first.len(), second.len());
(0..len).take_while(|&i| first[i] == second[i]).count()
}
/// Generates a trie root hash for a vector of values
///
/// ```rust
/// extern crate ethcore_util as util;
/// use std::str::FromStr;
/// use util::triehash::*;
/// use util::hash::*;
/// extern crate triehash;
/// use triehash::ordered_trie_root;
///
/// fn main() {
/// let v = vec![From::from("doe"), From::from("reindeer")];
/// let root = "e766d5d51b89dc39d981b41bda63248d7abce4f0225eefd023792a540bcffee3";
/// assert_eq!(ordered_trie_root(v), H256::from_str(root).unwrap());
/// assert_eq!(ordered_trie_root(v), root.parse().unwrap());
/// }
/// ```
pub fn ordered_trie_root<I>(input: I) -> H256
@@ -61,10 +66,8 @@ pub fn ordered_trie_root<I>(input: I) -> H256
/// Generates a trie root hash for a vector of key-values
///
/// ```rust
/// extern crate ethcore_util as util;
/// use std::str::FromStr;
/// use util::triehash::*;
/// use util::hash::*;
/// extern crate triehash;
/// use triehash::trie_root;
///
/// fn main() {
/// let v = vec![
@@ -74,7 +77,7 @@ pub fn ordered_trie_root<I>(input: I) -> H256
/// ];
///
/// let root = "8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3";
/// assert_eq!(trie_root(v), H256::from_str(root).unwrap());
/// assert_eq!(trie_root(v), root.parse().unwrap());
/// }
/// ```
pub fn trie_root<I>(input: I) -> H256
@@ -95,10 +98,8 @@ pub fn trie_root<I>(input: I) -> H256
/// Generates a key-hashed (secure) trie root hash for a vector of key-values.
///
/// ```rust
/// extern crate ethcore_util as util;
/// use std::str::FromStr;
/// use util::triehash::*;
/// use util::hash::*;
/// extern crate triehash;
/// use triehash::sec_trie_root;
///
/// fn main() {
/// let v = vec![
@@ -108,7 +109,7 @@ pub fn trie_root<I>(input: I) -> H256
/// ];
///
/// let root = "d4cd937e4a4368d7931a9cf51686b7e10abb3dce38a39000fd7902a092b64585";
/// assert_eq!(sec_trie_root(v), H256::from_str(root).unwrap());
/// assert_eq!(sec_trie_root(v), root.parse().unwrap());
/// }
/// ```
pub fn sec_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
@@ -216,7 +217,7 @@ fn hash256rlp(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStre
.skip(1)
// get minimum number of shared nibbles between first and each successive
.fold(key.len(), | acc, &(ref k, _) | {
cmp::min(key.shared_prefix_len(k), acc)
cmp::min(shared_prefix_len(key, k), acc)
});
// if shared prefix is higher than current prefix append its
@@ -288,50 +289,49 @@ fn test_nibbles() {
assert_eq!(as_nibbles(&v), e);
}
#[test]
fn test_hex_prefix_encode() {
let v = vec![0, 0, 1, 2, 3, 4, 5];
let e = vec![0x10, 0x01, 0x23, 0x45];
let h = hex_prefix_encode(&v, false);
assert_eq!(h, e);
let v = vec![0, 1, 2, 3, 4, 5];
let e = vec![0x00, 0x01, 0x23, 0x45];
let h = hex_prefix_encode(&v, false);
assert_eq!(h, e);
let v = vec![0, 1, 2, 3, 4, 5];
let e = vec![0x20, 0x01, 0x23, 0x45];
let h = hex_prefix_encode(&v, true);
assert_eq!(h, e);
let v = vec![1, 2, 3, 4, 5];
let e = vec![0x31, 0x23, 0x45];
let h = hex_prefix_encode(&v, true);
assert_eq!(h, e);
let v = vec![1, 2, 3, 4];
let e = vec![0x00, 0x12, 0x34];
let h = hex_prefix_encode(&v, false);
assert_eq!(h, e);
let v = vec![4, 1];
let e = vec![0x20, 0x41];
let h = hex_prefix_encode(&v, true);
assert_eq!(h, e);
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use hash::H256;
use super::trie_root;
use super::{trie_root, shared_prefix_len, hex_prefix_encode};
#[test]
fn test_hex_prefix_encode() {
let v = vec![0, 0, 1, 2, 3, 4, 5];
let e = vec![0x10, 0x01, 0x23, 0x45];
let h = hex_prefix_encode(&v, false);
assert_eq!(h, e);
let v = vec![0, 1, 2, 3, 4, 5];
let e = vec![0x00, 0x01, 0x23, 0x45];
let h = hex_prefix_encode(&v, false);
assert_eq!(h, e);
let v = vec![0, 1, 2, 3, 4, 5];
let e = vec![0x20, 0x01, 0x23, 0x45];
let h = hex_prefix_encode(&v, true);
assert_eq!(h, e);
let v = vec![1, 2, 3, 4, 5];
let e = vec![0x31, 0x23, 0x45];
let h = hex_prefix_encode(&v, true);
assert_eq!(h, e);
let v = vec![1, 2, 3, 4];
let e = vec![0x00, 0x12, 0x34];
let h = hex_prefix_encode(&v, false);
assert_eq!(h, e);
let v = vec![4, 1];
let e = vec![0x20, 0x41];
let h = hex_prefix_encode(&v, true);
assert_eq!(h, e);
}
#[test]
fn simple_test() {
assert_eq!(trie_root(vec![
(b"A".to_vec(), b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_vec())
]), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap());
]), "d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab".parse().unwrap());
}
#[test]
@@ -348,4 +348,24 @@ mod tests {
]));
}
#[test]
fn test_shared_prefix() {
let a = vec![1,2,3,4,5,6];
let b = vec![4,2,3,4,5,6];
assert_eq!(shared_prefix_len(&a, &b), 0);
}
#[test]
fn test_shared_prefix2() {
let a = vec![1,2,3,3,5];
let b = vec![1,2,3];
assert_eq!(shared_prefix_len(&a, &b), 3);
}
#[test]
fn test_shared_prefix3() {
let a = vec![1,2,3,4,5,6];
let b = vec![1,2,3,4,5,6];
assert_eq!(shared_prefix_len(&a, &b), 6);
}
}