This commit is contained in:
arkpar
2016-02-02 17:00:32 +01:00
26 changed files with 350 additions and 222 deletions

View File

@@ -3,7 +3,7 @@ description = "Ethcore utility library"
homepage = "http://ethcore.io"
license = "GPL-3.0"
name = "ethcore-util"
version = "0.1.0"
version = "0.9.0"
authors = ["Ethcore <admin@ethcore.io>"]
[dependencies]
@@ -28,3 +28,4 @@ sha3 = { path = "sha3" }
serde = "0.6.7"
clippy = "0.0.37"
json-tests = { path = "json-tests" }
target_info = "0.1.0"

View File

@@ -63,9 +63,9 @@ fn bench_stream_nested_empty_lists(b: &mut Bencher) {
b.iter(|| {
// [ [], [[]], [ [], [[]] ] ]
let mut stream = RlpStream::new_list(3);
stream.append_list(0);
stream.append_list(1).append_list(0);
stream.append_list(2).append_list(0).append_list(1).append_list(0);
stream.begin_list(0);
stream.begin_list(1).begin_list(0);
stream.begin_list(2).begin_list(0).begin_list(1).begin_list(0);
let _ = stream.out();
});
}
@@ -89,7 +89,7 @@ fn bench_stream_1000_empty_lists(b: &mut Bencher) {
b.iter(|| {
let mut stream = RlpStream::new_list(1000);
for _ in 0..1000 {
stream.append_list(0);
stream.begin_list(0);
}
let _ = stream.out();
});

View File

@@ -42,6 +42,84 @@ fn random_value(seed: &mut H256) -> Bytes {
}
}
#[bench]
fn trie_insertions_32_mir_1k(b: &mut Bencher) {
let st = StandardMap {
alphabet: Alphabet::All,
min_key: 32,
journal_key: 0,
value_mode: ValueMode::Mirror,
count: 1000,
};
let d = st.make();
let mut hash_count = 0usize;
b.iter(&mut ||{
let mut memdb = MemoryDB::new();
let mut root = H256::new();
let mut t = TrieDBMut::new(&mut memdb, &mut root);
for i in d.iter() {
t.insert(&i.0, &i.1);
}
hash_count = t.hash_count;
});
// println!("hash_count: {}", hash_count);
}
#[bench]
fn triehash_insertions_32_mir_1k(b: &mut Bencher) {
let st = StandardMap {
alphabet: Alphabet::All,
min_key: 32,
journal_key: 0,
value_mode: ValueMode::Mirror,
count: 1000,
};
let d = st.make();
b.iter(&mut ||{
trie_root(d.clone()).clone();
});
}
#[bench]
fn trie_insertions_32_ran_1k(b: &mut Bencher) {
let st = StandardMap {
alphabet: Alphabet::All,
min_key: 32,
journal_key: 0,
value_mode: ValueMode::Random,
count: 1000,
};
let d = st.make();
let mut hash_count = 0usize;
let mut r = H256::new();
b.iter(&mut ||{
let mut memdb = MemoryDB::new();
let mut root = H256::new();
let mut t = TrieDBMut::new(&mut memdb, &mut root);
for i in d.iter() {
t.insert(&i.0, &i.1);
}
hash_count = t.hash_count;
r = t.root().clone();
});
// println!("result: {}", hash_count);
}
#[bench]
fn triehash_insertions_32_ran_1k(b: &mut Bencher) {
let st = StandardMap {
alphabet: Alphabet::All,
min_key: 32,
journal_key: 0,
value_mode: ValueMode::Random,
count: 1000,
};
let d = st.make();
b.iter(&mut ||{
trie_root(d.clone()).clone();
});
}
#[bench]
fn trie_insertions_six_high(b: &mut Bencher) {
let mut d: Vec<(Bytes, Bytes)> = Vec::new();

View File

@@ -1,3 +1,5 @@
//! Utils common types and macros global reexport.
pub use standard::*;
pub use from_json::*;
pub use error::*;

View File

@@ -1,3 +1,5 @@
//! Ethcore crypto.
use hash::*;
use bytes::*;
use uint::*;

View File

@@ -1,3 +1,5 @@
//! Coversion from json.
use standard::*;
#[macro_export]

View File

@@ -1,3 +1,5 @@
//! Calculates heapsize of util types.
use uint::*;
use hash::*;

View File

@@ -1,41 +1,41 @@
/// General IO module.
///
/// Example usage for craeting a network service and adding an IO handler:
///
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::*;
///
/// struct MyHandler;
///
/// #[derive(Clone)]
/// struct MyMessage {
/// data: u32
/// }
///
/// impl IoHandler<MyMessage> for MyHandler {
/// fn initialize(&self, io: &IoContext<MyMessage>) {
/// io.register_timer(0, 1000).unwrap();
/// }
///
/// fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer);
/// }
///
/// fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data);
/// }
/// }
///
/// fn main () {
/// let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
/// service.register_handler(Arc::new(MyHandler)).unwrap();
///
/// // Wait for quit condition
/// // ...
/// // Drop the service
/// }
/// ```
//! General IO module.
//!
//! Example usage for creating a network service and adding an IO handler:
//!
//! ```rust
//! extern crate ethcore_util;
//! use ethcore_util::*;
//!
//! struct MyHandler;
//!
//! #[derive(Clone)]
//! struct MyMessage {
//! data: u32
//! }
//!
//! impl IoHandler<MyMessage> for MyHandler {
//! fn initialize(&self, io: &IoContext<MyMessage>) {
//! io.register_timer(0, 1000).unwrap();
//! }
//!
//! fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
//! println!("Timeout {}", timer);
//! }
//!
//! fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
//! println!("Message {}", message.data);
//! }
//! }
//!
//! fn main () {
//! let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
//! service.register_handler(Arc::new(MyHandler)).unwrap();
//!
//! // Wait for quit condition
//! // ...
//! // Drop the service
//! }
//! ```
mod service;
mod worker;

View File

@@ -8,32 +8,58 @@
//! Ethcore-util library
//!
//! ### Rust version:
//! - beta
//! - nightly
//!
//! ### Supported platforms:
//! - OSX
//! - Linux
//!
//! ### Dependencies:
//! - RocksDB 3.13
//! ### Building:
//!
//! ### Dependencies Installation:
//! - Ubuntu 14.04 and later:
//!
//! ```bash
//! # install rocksdb
//! add-apt-repository "deb http://ppa.launchpad.net/giskou/librocksdb/ubuntu trusty main"
//! apt-get update
//! apt-get install -y --force-yes librocksdb
//!
//! # install multirust
//! curl -sf https://raw.githubusercontent.com/brson/multirust/master/blastoff.sh | sh -s -- --yes
//!
//! # install nightly and make it default
//! multirust update nightly && multirust default nightly
//!
//! # export rust LIBRARY_PATH
//! export LIBRARY_PATH=/usr/local/lib
//!
//! # download and build parity
//! git clone https://github.com/ethcore/parity
//! cd parity
//! cargo build --release
//! ```
//!
//! - OSX:
//!
//! ```bash
//! # install rocksdb && multirust
//! brew update
//! brew install rocksdb
//! ```
//! brew install multirust
//!
//! - From source:
//! # install nightly and make it default
//! multirust update nightly && multirust default nightly
//!
//! ```bash
//! wget https://github.com/facebook/rocksdb/archive/rocksdb-3.13.tar.gz
//! tar xvf rocksdb-3.13.tar.gz && cd rocksdb-rocksdb-3.13 && make shared_lib
//! sudo make install
//! # export rust LIBRARY_PATH
//! export LIBRARY_PATH=/usr/local/lib
//!
//! # download and build parity
//! git clone https://github.com/ethcore/parity
//! cd parity
//! cargo build --release
//! ```
extern crate target_info;
extern crate slab;
extern crate rustc_serialize;
extern crate mio;
@@ -57,46 +83,34 @@ extern crate serde;
#[macro_use]
extern crate log as rlog;
/// TODO [Gav Wood] Please document me
pub mod standard;
#[macro_use]
/// TODO [Gav Wood] Please document me
pub mod from_json;
#[macro_use]
/// TODO [Gav Wood] Please document me
pub mod common;
pub mod error;
pub mod hash;
pub mod uint;
pub mod bytes;
pub mod rlp;
/// TODO [Gav Wood] Please document me
pub mod misc;
/// TODO [Gav Wood] Please document me
pub mod json_aid;
mod json_aid;
pub mod vector;
pub mod sha3;
pub mod hashdb;
pub mod memorydb;
pub mod overlaydb;
pub mod journaldb;
/// TODO [Gav Wood] Please document me
pub mod math;
mod math;
pub mod chainfilter;
/// TODO [Gav Wood] Please document me
pub mod crypto;
pub mod triehash;
/// TODO [Gav Wood] Please document me
pub mod trie;
pub mod nibbleslice;
/// TODO [Gav Wood] Please document me
pub mod heapsizeof;
mod heapsizeof;
pub mod squeeze;
/// TODO [Gav Wood] Please document me
pub mod semantic_version;
/// TODO [Gav Wood] Please document me
pub mod io;
/// TODO [Gav Wood] Please document me
pub mod network;
pub mod log;
@@ -114,7 +128,6 @@ pub use crypto::*;
pub use triehash::*;
pub use trie::*;
pub use nibbleslice::*;
pub use heapsizeof::*;
pub use squeeze::*;
pub use semantic_version::*;
pub use network::*;

View File

@@ -1,4 +1,6 @@
/// log2
//! Common math functions.
/// Returns log2.
pub fn log2(x: usize) -> u32 {
if x <= 1 {
return 0;

View File

@@ -1,3 +1,5 @@
//! Diff misc.
use common::*;
#[derive(Debug,Clone,PartialEq,Eq)]

View File

@@ -7,6 +7,7 @@ use std::ops::*;
use mio::*;
use mio::tcp::*;
use mio::udp::*;
use target_info::Target;
use hash::*;
use crypto::*;
use sha3::Hashable;
@@ -294,7 +295,7 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
config: config,
nonce: H256::random(),
protocol_version: 4,
client_version: "parity".to_owned(),
client_version: format!("Parity/{}/{}-{}-{}", env!("CARGO_PKG_VERSION"), Target::arch(), Target::env(), Target::os()),
listen_port: 0,
capabilities: Vec::new(),
}),

View File

@@ -1,52 +1,53 @@
/// Network and general IO module.
/// Example usage for craeting a network service and adding an IO handler:
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::*;
///
/// struct MyHandler;
///
/// #[derive(Clone)]
/// struct MyMessage {
/// data: u32
/// }
///
/// impl NetworkProtocolHandler<MyMessage> for MyHandler {
/// fn initialize(&self, io: &NetworkContext<MyMessage>) {
/// io.register_timer(0, 1000);
/// }
///
/// fn read(&self, io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
/// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
/// }
///
/// fn connected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Connected {}", peer);
/// }
///
/// fn disconnected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Disconnected {}", peer);
/// }
///
/// fn timeout(&self, io: &NetworkContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer);
/// }
///
/// fn message(&self, io: &NetworkContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data);
/// }
/// }
///
/// fn main () {
/// let mut service = NetworkService::<MyMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
/// service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]);
///
/// // Wait for quit condition
/// // ...
/// // Drop the service
/// }
/// ```
//! Network and general IO module.
//!
//! Example usage for craeting a network service and adding an IO handler:
//!
//! ```rust
//! extern crate ethcore_util as util;
//! use util::*;
//!
//! struct MyHandler;
//!
//! #[derive(Clone)]
//! struct MyMessage {
//! data: u32
//! }
//!
//! impl NetworkProtocolHandler<MyMessage> for MyHandler {
//! fn initialize(&self, io: &NetworkContext<MyMessage>) {
//! io.register_timer(0, 1000);
//! }
//!
//! fn read(&self, io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
//! println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
//! }
//!
//! fn connected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
//! println!("Connected {}", peer);
//! }
//!
//! fn disconnected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
//! println!("Disconnected {}", peer);
//! }
//!
//! fn timeout(&self, io: &NetworkContext<MyMessage>, timer: TimerToken) {
//! println!("Timeout {}", timer);
//! }
//!
//! fn message(&self, io: &NetworkContext<MyMessage>, message: &MyMessage) {
//! println!("Message {}", message.data);
//! }
//! }
//!
//! fn main () {
//! let mut service = NetworkService::<MyMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
//! service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]);
//!
//! // Wait for quit condition
//! // ...
//! // Drop the service
//! }
//! ```
mod host;
mod connection;
mod handshake;

View File

@@ -1,3 +1,5 @@
//! Semantic version formatting and comparing.
/// A version value with strict meaning. Use `to_u32` to convert to a simple integer.
///
/// # Example

View File

@@ -1,3 +1,5 @@
//! Std lib global reexports.
pub use std::io;
pub use std::fs;
pub use std::str;

View File

@@ -1,3 +1,5 @@
//! Trie interface and implementation.
/// TODO [Gav Wood] Please document me
pub mod trietraits;
pub mod standardmap;

View File

@@ -17,12 +17,26 @@ pub enum Alphabet {
Custom(Bytes),
}
/// Means of determining the value.
pub enum ValueMode {
/// Same as the key.
Mirror,
/// Randomly (50:50) 1 or 32 byte randomly string.
Random,
}
/// Standard test map for profiling tries.
pub struct StandardMap {
alphabet: Alphabet,
min_key: usize,
journal_key: usize,
count: usize,
/// The alphabet to use for keys.
pub alphabet: Alphabet,
/// Minimum size of key.
pub min_key: usize,
/// Delta size of key.
pub journal_key: usize,
/// Mode of value generation.
pub value_mode: ValueMode,
/// Number of keys.
pub count: usize,
}
impl StandardMap {
@@ -71,7 +85,7 @@ impl StandardMap {
Alphabet::Mid => Self::random_word(mid, self.min_key, self.journal_key, &mut seed),
Alphabet::Custom(ref a) => Self::random_word(&a, self.min_key, self.journal_key, &mut seed),
};
let v = Self::random_value(&mut seed);
let v = match self.value_mode { ValueMode::Mirror => k.clone(), ValueMode::Random => Self::random_value(&mut seed) };
d.push((k, v))
}
d

View File

@@ -1,40 +1,4 @@
//! vector util functions
use std::ptr;
/// TODO [debris] Please document me
pub trait InsertSlice<T> {
/// TODO [debris] Please document me
fn insert_slice(&mut self, index: usize, elements: &[T]);
}
/// based on `insert` function implementation from standard library
impl<T> InsertSlice<T> for Vec<T> {
fn insert_slice(&mut self, index: usize, elements: &[T]) {
let e_len = elements.len();
if e_len == 0 {
return;
}
let len = self.len();
assert!(index <= len);
// space for the new element
self.reserve(e_len);
unsafe {
{
let p = self.as_mut_ptr().offset(index as isize);
let ep = elements.as_ptr().offset(0);
// shift everything by e_len, to make space
ptr::copy(p, p.offset(e_len as isize), len - index);
// write new element
ptr::copy(ep, p, e_len);
}
self.set_len(len + e_len);
}
}
}
//! Vector extensions.
/// Returns len of prefix shared with elem
///