Merge branch 'master' into fo-6418-dont-export-bigint

# Conflicts:
#	dapps/src/tests/helpers/registrar.rs
#	ethcore/evm/src/interpreter/shared_cache.rs
#	ethcore/light/src/client/header_chain.rs
#	ethcore/light/src/client/mod.rs
#	ethcore/light/src/net/mod.rs
#	ethcore/light/src/on_demand/request.rs
#	ethcore/light/src/on_demand/tests.rs
#	ethcore/light/src/provider.rs
#	ethcore/node_filter/src/lib.rs
#	ethcore/src/block.rs
#	ethcore/src/blockchain/blockchain.rs
#	ethcore/src/client/test_client.rs
#	ethcore/src/engines/authority_round/mod.rs
#	ethcore/src/engines/basic_authority.rs
#	ethcore/src/engines/mod.rs
#	ethcore/src/engines/tendermint/mod.rs
#	ethcore/src/engines/validator_set/contract.rs
#	ethcore/src/engines/validator_set/multi.rs
#	ethcore/src/engines/validator_set/safe_contract.rs
#	ethcore/src/engines/vote_collector.rs
#	ethcore/src/miner/external.rs
#	ethcore/src/miner/miner.rs
#	ethcore/src/miner/service_transaction_checker.rs
#	ethcore/src/miner/work_notify.rs
#	ethcore/src/pod_account.rs
#	ethcore/src/pod_state.rs
#	ethcore/src/snapshot/block.rs
#	ethcore/src/snapshot/consensus/work.rs
#	ethcore/src/snapshot/mod.rs
#	ethcore/src/snapshot/service.rs
#	ethcore/src/spec/spec.rs
#	ethcore/src/state/backend.rs
#	ethcore/src/trace/db.rs
#	ethcore/src/verification/queue/mod.rs
#	ethcore/src/verification/verification.rs
#	parity/informant.rs
#	rpc/src/v1/helpers/dispatch.rs
#	rpc/src/v1/helpers/light_fetch.rs
#	rpc/src/v1/helpers/signing_queue.rs
#	rpc/src/v1/impls/eth.rs
#	rpc/src/v1/impls/eth_filter.rs
#	rpc/src/v1/impls/eth_pubsub.rs
#	rpc/src/v1/impls/light/eth.rs
#	rpc/src/v1/impls/signing.rs
#	rpc/src/v1/tests/helpers/miner_service.rs
#	rpc/src/v1/tests/helpers/snapshot_service.rs
#	rpc/src/v1/tests/helpers/sync_provider.rs
#	rpc/src/v1/tests/mocked/eth.rs
#	stratum/src/lib.rs
#	sync/src/blocks.rs
#	sync/src/chain.rs
#	sync/src/light_sync/mod.rs
#	sync/src/tests/helpers.rs
#	sync/src/tests/snapshot.rs
#	updater/src/updater.rs
#	util/src/lib.rs
#	util/triehash/src/lib.rs
This commit is contained in:
Fredrik
2017-09-04 18:32:55 +02:00
127 changed files with 375 additions and 260 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;
extern crate ethcore_bigint;
#[macro_use]
@@ -28,7 +29,7 @@ use ethcore_bigint::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

@@ -19,7 +19,8 @@ use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
use std::sync::Arc;
use std::thread;
use std::time::*;
use util::{Bytes, Mutex};
use parking_lot::Mutex;
use util::Bytes;
use io::TimerToken;
use ethkey::{Random, Generator};

View File

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

View File

@@ -20,8 +20,8 @@
///
/// # Example
/// ```
/// extern crate ethcore_util as util;
/// use util::semantic_version::*;
/// extern crate semantic_version;
/// use semantic_version::*;
///
/// fn main() {
/// assert_eq!(SemanticVersion::new(1, 2, 3).as_u32(), 0x010203);

View File

@@ -117,38 +117,27 @@ 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;
pub mod semantic_version;
pub mod snappy;
pub mod cache;
mod timer;
pub use misc::*;
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 semantic_version::*;
pub use kvdb::*;
pub use timer::*;
pub use error::*;
pub use bytes::*;
pub use vector::*;
pub use ansi_term::{Colour, Style};
pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
/// 160-bit integer representing account address
pub type Address = bigint::hash::H160;

View File

@@ -1,51 +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/>.
//! Performance timer with logging
use time::precise_time_ns;
/// Performance timer with logging. Starts measuring time in the constructor, prints
/// elapsed time in the destructor or when `stop` is called.
pub struct PerfTimer {
name: &'static str,
start: u64,
stopped: bool,
}
impl PerfTimer {
/// Create an instance with given name.
pub fn new(name: &'static str) -> PerfTimer {
PerfTimer {
name: name,
start: precise_time_ns(),
stopped: false,
}
}
/// Stop the timer and print elapsed time on trace level with `perf` target.
pub fn stop(&mut self) {
if !self.stopped {
trace!(target: "perf", "{}: {:.2}ms", self.name, (precise_time_ns() - self.start) as f32 / 1000_000.0);
self.stopped = true;
}
}
}
impl Drop for PerfTimer {
fn drop(&mut self) {
self.stop()
}
}

View File

@@ -939,7 +939,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,27 +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 bigint::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;
/// extern crate ethcore_bigint as bigint;
/// use std::str::FromStr;
/// use util::triehash::*;
/// use bigint::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
@@ -62,11 +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;
/// extern crate ethcore_bigint as bigint;
/// use std::str::FromStr;
/// use util::triehash::*;
/// use bigint::hash::*;
/// extern crate triehash;
/// use triehash::trie_root;
///
/// fn main() {
/// let v = vec![
@@ -76,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
@@ -97,11 +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;
/// extern crate ethcore_bigint as bigint;
/// use std::str::FromStr;
/// use util::triehash::*;
/// use bigint::hash::*;
/// extern crate triehash;
/// use triehash::sec_trie_root;
///
/// fn main() {
/// let v = vec![
@@ -111,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 {
@@ -219,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
@@ -291,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 bigint::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]
@@ -351,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);
}
}