From eb7b62a61c969993b97a2d10ac19a48822216f24 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 13:36:32 +0200 Subject: [PATCH] move rlp to separate crate, port ethcore-util to it --- Cargo.lock | 18 +++++- ethcore/Cargo.toml | 1 + ethcore/src/account_db.rs | 1 + ethcore/src/lib.rs | 24 +++++-- scripts/targets.sh | 1 + util/Cargo.toml | 3 +- util/rlp/Cargo.toml | 12 ++++ util/{src/rlp => rlp/src}/bytes.rs | 2 +- util/{src/rlp => rlp/src}/commonrlps.rs | 60 +---------------- util/{src/rlp/mod.rs => rlp/src/lib.rs} | 27 ++++---- util/{src/rlp => rlp/src}/rlpcompression.rs | 30 ++------- util/{src/rlp => rlp/src}/rlperrors.rs | 2 +- util/{src/rlp => rlp/src}/rlpin.rs | 8 ++- util/{src/rlp => rlp/src}/rlpstream.rs | 7 +- util/{src/rlp => rlp/src}/rlptraits.rs | 72 ++++++++++----------- util/{src/rlp => rlp/src}/tests.rs | 31 ++++----- util/{src/rlp => rlp/src}/untrusted_rlp.rs | 7 +- util/src/lib.rs | 25 +++---- util/src/sha3.rs | 7 +- util/src/trie/mod.rs | 2 +- util/src/trie/triedb.rs | 2 +- util/src/trie/triedbmut.rs | 9 +-- 22 files changed, 159 insertions(+), 192 deletions(-) create mode 100644 util/rlp/Cargo.toml rename util/{src/rlp => rlp/src}/bytes.rs (99%) rename util/{src/rlp => rlp/src}/commonrlps.rs (76%) rename util/{src/rlp/mod.rs => rlp/src/lib.rs} (77%) rename util/{src/rlp => rlp/src}/rlpcompression.rs (91%) rename util/{src/rlp => rlp/src}/rlperrors.rs (98%) rename util/{src/rlp => rlp/src}/rlpin.rs (91%) rename util/{src/rlp => rlp/src}/rlpstream.rs (98%) rename util/{src/rlp => rlp/src}/rlptraits.rs (88%) rename util/{src/rlp => rlp/src}/tests.rs (92%) rename util/{src/rlp => rlp/src}/untrusted_rlp.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 79bb6dba9..86db0c97a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -205,8 +205,8 @@ dependencies = [ [[package]] name = "elastic-array" -version = "0.4.0" -source = "git+https://github.com/ethcore/elastic-array#9a9bebd6ea291c58e4d6b44dd5dc18368638fefe" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "env_logger" @@ -278,6 +278,7 @@ dependencies = [ "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -492,7 +493,7 @@ dependencies = [ "arrayvec 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "bigint 0.1.0", "clippy 0.0.85 (registry+https://github.com/rust-lang/crates.io-index)", - "elastic-array 0.4.0 (git+https://github.com/ethcore/elastic-array)", + "elastic-array 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "eth-secp256k1 0.5.4 (git+https://github.com/ethcore/rust-secp256k1)", "ethcore-devtools 1.4.0", @@ -503,6 +504,7 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rocksdb 0.4.5 (git+https://github.com/ethcore/rust-rocksdb)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1265,6 +1267,16 @@ name = "regex-syntax" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rlp" +version = "0.1.0" +dependencies = [ + "bigint 0.1.0", + "elastic-array 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rocksdb" version = "0.4.5" diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index a34116df5..8acba2266 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -35,6 +35,7 @@ ethcore-ipc = { path = "../ipc/rpc" } ethstore = { path = "../ethstore" } ethkey = { path = "../ethkey" } ethcore-ipc-nano = { path = "../ipc/nano" } +rlp = { path = "../util/rlp" } rand = "0.3" [dependencies.hyper] diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index 15042403d..32c7acbb7 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -16,6 +16,7 @@ //! DB backend wrapper for Account trie use util::*; +use util::sha3::SHA3_NULL_RLP; static NULL_RLP_STATIC: [u8; 1] = [0x80; 1]; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index b8233ea26..5344381f0 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -79,12 +79,9 @@ //! cargo build --release //! ``` -#[macro_use] extern crate log; -#[macro_use] extern crate ethcore_util as util; + extern crate ethcore_io as io; -#[macro_use] extern crate lazy_static; extern crate rustc_serialize; -#[macro_use] extern crate heapsize; extern crate crypto; extern crate time; extern crate env_logger; @@ -92,19 +89,32 @@ extern crate num_cpus; extern crate crossbeam; extern crate ethjson; extern crate bloomchain; -#[macro_use] extern crate ethcore_ipc as ipc; extern crate rayon; extern crate hyper; extern crate ethash; extern crate ethkey; -pub extern crate ethstore; extern crate semver; extern crate ethcore_ipc_nano as nanoipc; extern crate ethcore_devtools as devtools; extern crate rand; extern crate bit_set; +extern crate rlp; -#[cfg(feature = "jit" )] extern crate evmjit; +#[macro_use] +extern crate log; +#[macro_use] +extern crate ethcore_util as util; +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate heapsize; +#[macro_use] +extern crate ethcore_ipc as ipc; + +#[cfg(feature = "jit" )] +extern crate evmjit; + +pub extern crate ethstore; pub mod account_provider; pub mod engines; diff --git a/scripts/targets.sh b/scripts/targets.sh index 5a8d41a3b..009b8ad1d 100644 --- a/scripts/targets.sh +++ b/scripts/targets.sh @@ -2,6 +2,7 @@ export TARGETS=" -p bigint\ + -p rlp\ -p ethash \ -p ethcore \ -p ethcore-dapps \ diff --git a/util/Cargo.toml b/util/Cargo.toml index 719e4c255..106251a3f 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -18,7 +18,8 @@ rocksdb = { git = "https://github.com/ethcore/rust-rocksdb" } lazy_static = "0.2" eth-secp256k1 = { git = "https://github.com/ethcore/rust-secp256k1" } rust-crypto = "0.2.34" -elastic-array = { git = "https://github.com/ethcore/elastic-array" } +elastic-array = "0.5" +rlp = { path = "rlp" } heapsize = { version = "0.3", features = ["unstable"] } itertools = "0.4" sha3 = { path = "sha3" } diff --git a/util/rlp/Cargo.toml b/util/rlp/Cargo.toml new file mode 100644 index 000000000..eba3a2842 --- /dev/null +++ b/util/rlp/Cargo.toml @@ -0,0 +1,12 @@ +[package] +description = "Recursive-length prefix encoding, decoding, and compression" +license = "GPL-3.0" +name = "rlp" +version = "0.1.0" +authors = ["Ethcore "] + +[dependencies] +elastic-array = "0.5" +bigint = { path = "../bigint" } +lazy_static = "0.2" +rustc-serialize = "0.3" \ No newline at end of file diff --git a/util/src/rlp/bytes.rs b/util/rlp/src/bytes.rs similarity index 99% rename from util/src/rlp/bytes.rs rename to util/rlp/src/bytes.rs index 9b98be6c6..5940d21d2 100644 --- a/util/src/rlp/bytes.rs +++ b/util/rlp/src/bytes.rs @@ -22,7 +22,7 @@ use std::fmt; use std::cmp::Ordering; use std::error::Error as StdError; use bigint::uint::{Uint, U128, U256}; -use hash::{H64, H128, H160, H256, H512, H520, H2048}; +use bigint::hash::{H64, H128, H160, H256, H512, H520, H2048}; use elastic_array::*; /// Vector like object diff --git a/util/src/rlp/commonrlps.rs b/util/rlp/src/commonrlps.rs similarity index 76% rename from util/src/rlp/commonrlps.rs rename to util/rlp/src/commonrlps.rs index 856c2fc3a..239117245 100644 --- a/util/src/rlp/commonrlps.rs +++ b/util/rlp/src/commonrlps.rs @@ -16,7 +16,7 @@ //! Contains RLPs used for compression. -use rlp::rlpcompression::InvalidRlpSwapper; +use rlpcompression::InvalidRlpSwapper; lazy_static! { /// Swapper for snapshot compression. @@ -47,60 +47,4 @@ static COMMON_RLPS: &'static [&'static [u8]] = &[ &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; -static INVALID_RLPS: &'static [&'static [u8]] = &[&[0x81, 0x0], &[0x81, 0x1], &[0x81, 0x2], &[0x81, 0x3], &[0x81, 0x4], &[0x81, 0x5], &[0x81, 0x6], &[0x81, 0x7], &[0x81, 0x8], &[0x81, 0x9], &[0x81, 0xa], &[0x81, 0xb], &[0x81, 0xc], &[0x81, 0xd], &[0x81, 0xe], &[0x81, 0xf], &[0x81, 0x10], &[0x81, 0x11], &[0x81, 0x12], &[0x81, 0x13], &[0x81, 0x14], &[0x81, 0x15], &[0x81, 0x16], &[0x81, 0x17], &[0x81, 0x18], &[0x81, 0x19], &[0x81, 0x1a], &[0x81, 0x1b], &[0x81, 0x1c], &[0x81, 0x1d], &[0x81, 0x1e], &[0x81, 0x1f], &[0x81, 0x20], &[0x81, 0x21], &[0x81, 0x22], &[0x81, 0x23], &[0x81, 0x24], &[0x81, 0x25], &[0x81, 0x26], &[0x81, 0x27], &[0x81, 0x28], &[0x81, 0x29], &[0x81, 0x2a], &[0x81, 0x2b], &[0x81, 0x2c], &[0x81, 0x2d], &[0x81, 0x2e], &[0x81, 0x2f], &[0x81, 0x30], &[0x81, 0x31], &[0x81, 0x32], &[0x81, 0x33], &[0x81, 0x34], &[0x81, 0x35], &[0x81, 0x36], &[0x81, 0x37], &[0x81, 0x38], &[0x81, 0x39], &[0x81, 0x3a], &[0x81, 0x3b], &[0x81, 0x3c], &[0x81, 0x3d], &[0x81, 0x3e], &[0x81, 0x3f], &[0x81, 0x40], &[0x81, 0x41], &[0x81, 0x42], &[0x81, 0x43], &[0x81, 0x44], &[0x81, 0x45], &[0x81, 0x46], &[0x81, 0x47], &[0x81, 0x48], &[0x81, 0x49], &[0x81, 0x4a], &[0x81, 0x4b], &[0x81, 0x4c], &[0x81, 0x4d], &[0x81, 0x4e], &[0x81, 0x4f], &[0x81, 0x50], &[0x81, 0x51], &[0x81, 0x52], &[0x81, 0x53], &[0x81, 0x54], &[0x81, 0x55], &[0x81, 0x56], &[0x81, 0x57], &[0x81, 0x58], &[0x81, 0x59], &[0x81, 0x5a], &[0x81, 0x5b], &[0x81, 0x5c], &[0x81, 0x5d], &[0x81, 0x5e], &[0x81, 0x5f], &[0x81, 0x60], &[0x81, 0x61], &[0x81, 0x62], &[0x81, 0x63], &[0x81, 0x64], &[0x81, 0x65], &[0x81, 0x66], &[0x81, 0x67], &[0x81, 0x68], &[0x81, 0x69], &[0x81, 0x6a], &[0x81, 0x6b], &[0x81, 0x6c], &[0x81, 0x6d], &[0x81, 0x6e], &[0x81, 0x6f], &[0x81, 0x70], &[0x81, 0x71], &[0x81, 0x72], &[0x81, 0x73], &[0x81, 0x74], &[0x81, 0x75], &[0x81, 0x76], &[0x81, 0x77], &[0x81, 0x78], &[0x81, 0x79], &[0x81, 0x7a], &[0x81, 0x7b], &[0x81, 0x7c], &[0x81, 0x7d], &[0x81, 0x7e]]; - -#[cfg(test)] -mod tests { - #[test] - #[ignore] - fn analyze_db() { - use rlp::{UntrustedRlp, View}; - use std::collections::HashMap; - use kvdb::*; - - let path = "db path".to_string(); - let values: Vec<_> = Database::open_default(&path).unwrap().iter(Some(2)).map(|(_, v)| v).collect(); - let mut rlp_counts: HashMap<_, u32> = HashMap::new(); - let mut rlp_sizes: HashMap<_, u32> = HashMap::new(); - - fn flat_rlp<'a>(acc: &mut Vec>, rlp: UntrustedRlp<'a>) { - match rlp.is_data() { - true => if rlp.size()>=70 { - match rlp.data() { - Ok(x) => flat_rlp(acc, UntrustedRlp::new(x)), - _ => acc.push(rlp), - } - } else { - acc.push(rlp); - }, - false => for r in rlp.iter() { flat_rlp(acc, r); }, - } - } - - fn space_saving(bytes: &[u8]) -> u32 { - let l = bytes.len() as u32; - match l >= 2 { - true => l-2, - false => 0, - } - } - - for v in &values { - let rlp = UntrustedRlp::new(v); - let mut flat = Vec::new(); - flat_rlp(&mut flat, rlp); - for r in &flat { - *rlp_counts.entry(r.as_raw()).or_insert(0) += 1; - *rlp_sizes.entry(r.as_raw()).or_insert(0) += space_saving(r.as_raw()); - } - } - let mut size_vec: Vec<_> = rlp_sizes.iter().collect(); - size_vec.sort_by(|a, b| b.1.cmp(a.1)); - - // Exclude rare large RLPs. - for v in size_vec.iter().filter(|v| rlp_counts.get(v.0).unwrap()>&100).take(20) { - println!("{:?}, {:?}", v, rlp_counts.get(v.0).unwrap()); - } - println!("DONE"); - } -} +static INVALID_RLPS: &'static [&'static [u8]] = &[&[0x81, 0x0], &[0x81, 0x1], &[0x81, 0x2], &[0x81, 0x3], &[0x81, 0x4], &[0x81, 0x5], &[0x81, 0x6], &[0x81, 0x7], &[0x81, 0x8], &[0x81, 0x9], &[0x81, 0xa], &[0x81, 0xb], &[0x81, 0xc], &[0x81, 0xd], &[0x81, 0xe], &[0x81, 0xf], &[0x81, 0x10], &[0x81, 0x11], &[0x81, 0x12], &[0x81, 0x13], &[0x81, 0x14], &[0x81, 0x15], &[0x81, 0x16], &[0x81, 0x17], &[0x81, 0x18], &[0x81, 0x19], &[0x81, 0x1a], &[0x81, 0x1b], &[0x81, 0x1c], &[0x81, 0x1d], &[0x81, 0x1e], &[0x81, 0x1f], &[0x81, 0x20], &[0x81, 0x21], &[0x81, 0x22], &[0x81, 0x23], &[0x81, 0x24], &[0x81, 0x25], &[0x81, 0x26], &[0x81, 0x27], &[0x81, 0x28], &[0x81, 0x29], &[0x81, 0x2a], &[0x81, 0x2b], &[0x81, 0x2c], &[0x81, 0x2d], &[0x81, 0x2e], &[0x81, 0x2f], &[0x81, 0x30], &[0x81, 0x31], &[0x81, 0x32], &[0x81, 0x33], &[0x81, 0x34], &[0x81, 0x35], &[0x81, 0x36], &[0x81, 0x37], &[0x81, 0x38], &[0x81, 0x39], &[0x81, 0x3a], &[0x81, 0x3b], &[0x81, 0x3c], &[0x81, 0x3d], &[0x81, 0x3e], &[0x81, 0x3f], &[0x81, 0x40], &[0x81, 0x41], &[0x81, 0x42], &[0x81, 0x43], &[0x81, 0x44], &[0x81, 0x45], &[0x81, 0x46], &[0x81, 0x47], &[0x81, 0x48], &[0x81, 0x49], &[0x81, 0x4a], &[0x81, 0x4b], &[0x81, 0x4c], &[0x81, 0x4d], &[0x81, 0x4e], &[0x81, 0x4f], &[0x81, 0x50], &[0x81, 0x51], &[0x81, 0x52], &[0x81, 0x53], &[0x81, 0x54], &[0x81, 0x55], &[0x81, 0x56], &[0x81, 0x57], &[0x81, 0x58], &[0x81, 0x59], &[0x81, 0x5a], &[0x81, 0x5b], &[0x81, 0x5c], &[0x81, 0x5d], &[0x81, 0x5e], &[0x81, 0x5f], &[0x81, 0x60], &[0x81, 0x61], &[0x81, 0x62], &[0x81, 0x63], &[0x81, 0x64], &[0x81, 0x65], &[0x81, 0x66], &[0x81, 0x67], &[0x81, 0x68], &[0x81, 0x69], &[0x81, 0x6a], &[0x81, 0x6b], &[0x81, 0x6c], &[0x81, 0x6d], &[0x81, 0x6e], &[0x81, 0x6f], &[0x81, 0x70], &[0x81, 0x71], &[0x81, 0x72], &[0x81, 0x73], &[0x81, 0x74], &[0x81, 0x75], &[0x81, 0x76], &[0x81, 0x77], &[0x81, 0x78], &[0x81, 0x79], &[0x81, 0x7a], &[0x81, 0x7b], &[0x81, 0x7c], &[0x81, 0x7d], &[0x81, 0x7e]]; \ No newline at end of file diff --git a/util/src/rlp/mod.rs b/util/rlp/src/lib.rs similarity index 77% rename from util/src/rlp/mod.rs rename to util/rlp/src/lib.rs index 4dc14c8a3..2cc8c3bd8 100644 --- a/util/src/rlp/mod.rs +++ b/util/rlp/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -//! Rlp serialization module +//! Recursive Length Prefix serialization crate. //! //! Allows encoding, decoding, and view onto rlp-slice //! @@ -64,27 +64,29 @@ pub use self::untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, P pub use self::rlpin::{Rlp, RlpIterator}; pub use self::rlpstream::RlpStream; pub use self::rlpcompression::RlpType; -pub use elastic_array::ElasticArray1024; -use super::hash::H256; + +extern crate bigint; +extern crate elastic_array; +extern crate rustc_serialize; + +#[macro_use] +extern crate lazy_static; + +use elastic_array::ElasticArray1024; /// The RLP encoded empty data (used to mean "null value"). pub const NULL_RLP: [u8; 1] = [0x80; 1]; /// The RLP encoded empty list. pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1]; -/// The SHA3 of the RLP encoding of empty data. -pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] ); -/// The SHA3 of the RLP encoding of empty list. -pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] ); /// Shortcut function to decode trusted rlp /// /// ```rust -/// extern crate ethcore_util as util; -/// use util::rlp::*; +/// extern crate rlp; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; -/// let animals: Vec = decode(&data); +/// let animals: Vec = rlp::decode(&data); /// assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]); /// } /// ``` @@ -96,12 +98,11 @@ pub fn decode(bytes: &[u8]) -> T where T: RlpDecodable { /// Shortcut function to encode structure into rlp. /// /// ```rust -/// extern crate ethcore_util as util; -/// use util::rlp::*; +/// extern crate rlp; /// /// fn main () { /// let animal = "cat"; -/// let out = encode(&animal).to_vec(); +/// let out = rlp::encode(&animal).to_vec(); /// assert_eq!(out, vec![0x83, b'c', b'a', b't']); /// } /// ``` diff --git a/util/src/rlp/rlpcompression.rs b/util/rlp/src/rlpcompression.rs similarity index 91% rename from util/src/rlp/rlpcompression.rs rename to util/rlp/src/rlpcompression.rs index 64f2aac39..3113d8b83 100644 --- a/util/src/rlp/rlpcompression.rs +++ b/util/rlp/src/rlpcompression.rs @@ -14,9 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use rlp::{UntrustedRlp, View, Compressible, encode, ElasticArray1024, Stream, RlpStream}; -use rlp::commonrlps::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER}; +use ::{UntrustedRlp, View, Compressible, encode, Stream, RlpStream}; +use commonrlps::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER}; + use std::collections::HashMap; +use elastic_array::ElasticArray1024; /// Stores RLPs used for compression pub struct InvalidRlpSwapper<'a> { @@ -169,8 +171,8 @@ impl<'a> Compressible for UntrustedRlp<'a> { #[cfg(test)] mod tests { - use rlp::{UntrustedRlp, Compressible, View, RlpType}; - use rlp::rlpcompression::InvalidRlpSwapper; + use ::{UntrustedRlp, Compressible, View, RlpType}; + use rlpcompression::InvalidRlpSwapper; #[test] fn invalid_rlp_swapper() { @@ -222,24 +224,4 @@ mod tests { let malformed_rlp = UntrustedRlp::new(&malformed); assert_eq!(malformed_rlp.decompress(RlpType::Blocks).to_vec(), malformed); } - - #[test] - #[ignore] - fn test_compression() { - use kvdb::*; - let path = "db to test".to_string(); - let values: Vec<_> = Database::open_default(&path).unwrap().iter(Some(2)).map(|(_, v)| v).collect(); - let mut decomp_size = 0; - let mut comp_size = 0; - - for v in &values { - let rlp = UntrustedRlp::new(v); - let compressed = rlp.compress(RlpType::Blocks).to_vec(); - comp_size += compressed.len(); - let decompressed = rlp.decompress(RlpType::Blocks).to_vec(); - decomp_size += decompressed.len(); - } - println!("Decompressed bytes {:?}, compressed bytes: {:?}", decomp_size, comp_size); - assert!(decomp_size > comp_size); - } } diff --git a/util/src/rlp/rlperrors.rs b/util/rlp/src/rlperrors.rs similarity index 98% rename from util/src/rlp/rlperrors.rs rename to util/rlp/src/rlperrors.rs index 93a82187e..dc60afc9a 100644 --- a/util/src/rlp/rlperrors.rs +++ b/util/rlp/src/rlperrors.rs @@ -16,7 +16,7 @@ use std::fmt; use std::error::Error as StdError; -use rlp::bytes::FromBytesError; +use bytes::FromBytesError; #[derive(Debug, PartialEq, Eq)] /// Error concerning the RLP decoder. diff --git a/util/src/rlp/rlpin.rs b/util/rlp/src/rlpin.rs similarity index 91% rename from util/src/rlp/rlpin.rs rename to util/rlp/src/rlpin.rs index fb8395c0c..a096cdbe8 100644 --- a/util/src/rlp/rlpin.rs +++ b/util/rlp/src/rlpin.rs @@ -16,7 +16,7 @@ use std::fmt; use rustc_serialize::hex::ToHex; -use rlp::{View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable}; +use ::{View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable}; impl<'a> From> for Rlp<'a> { fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> { @@ -162,8 +162,10 @@ impl<'a, 'view> Iterator for RlpIterator<'a, 'view> { #[test] fn break_it() { - use common::*; - let h: Bytes = FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap(); + use rustc_serialize::hex::FromHex; + use bigint::uint::U256; + + let h: Vec = FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap(); let r: Rlp = Rlp::new(&h); let u: U256 = r.val_at(1); assert_eq!(format!("{}", u), "19526463837540678066"); diff --git a/util/src/rlp/rlpstream.rs b/util/rlp/src/rlpstream.rs similarity index 98% rename from util/src/rlp/rlpstream.rs rename to util/rlp/src/rlpstream.rs index eb70a7c13..ec85e8ee5 100644 --- a/util/src/rlp/rlpstream.rs +++ b/util/rlp/src/rlpstream.rs @@ -15,9 +15,10 @@ // along with Parity. If not, see . use elastic_array::*; -use rlp::bytes::{ToBytes, VecLike}; -use rlp::{Stream, Encoder, Encodable}; -use rlp::rlptraits::{ByteEncodable, RlpEncodable}; + +use ::{Stream, Encoder, Encodable}; +use bytes::{ToBytes, VecLike}; +use rlptraits::{ByteEncodable, RlpEncodable}; #[derive(Debug, Copy, Clone)] struct ListInfo { diff --git a/util/src/rlp/rlptraits.rs b/util/rlp/src/rlptraits.rs similarity index 88% rename from util/src/rlp/rlptraits.rs rename to util/rlp/src/rlptraits.rs index 5eca7ca03..0f3fb35d7 100644 --- a/util/src/rlp/rlptraits.rs +++ b/util/rlp/src/rlptraits.rs @@ -15,12 +15,11 @@ // along with Parity. If not, see . //! Common RLP traits -use rlp::bytes::VecLike; -use rlp::{DecoderError, UntrustedRlp}; -use rlp::rlpstream::RlpStream; +use ::{DecoderError, UntrustedRlp}; +use bytes::VecLike; +use rlpstream::RlpStream; + use elastic_array::ElasticArray1024; -use hash::H256; -use sha3::*; /// Type is able to decode RLP. pub trait Decoder: Sized { @@ -65,8 +64,8 @@ pub trait View<'a, 'view>: Sized { /// The raw data of the RLP as slice. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -89,8 +88,8 @@ pub trait View<'a, 'view>: Sized { /// Returns number of RLP items. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -105,8 +104,8 @@ pub trait View<'a, 'view>: Sized { /// Returns the number of bytes in the data, or zero if it isn't data. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -124,8 +123,8 @@ pub trait View<'a, 'view>: Sized { /// slices is faster. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -138,8 +137,8 @@ pub trait View<'a, 'view>: Sized { /// No value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![]; @@ -152,8 +151,8 @@ pub trait View<'a, 'view>: Sized { /// Contains a zero-length string or zero-length list. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc0]; @@ -166,8 +165,8 @@ pub trait View<'a, 'view>: Sized { /// List value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -180,8 +179,8 @@ pub trait View<'a, 'view>: Sized { /// String value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -194,8 +193,8 @@ pub trait View<'a, 'view>: Sized { /// Int value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc1, 0x10]; @@ -209,8 +208,8 @@ pub trait View<'a, 'view>: Sized { /// Get iterator over rlp-slices /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -254,9 +253,6 @@ pub trait Encodable { self.rlp_append(&mut s); s.drain() } - - /// Get the hash or RLP encoded representation - fn rlp_sha3(&self) -> H256 { (&*self.rlp_bytes()).sha3() } } /// Encodable wrapper trait required to handle special case of encoding a &[u8] as string and not as list @@ -277,8 +273,8 @@ pub trait Stream: Sized { /// Apends value to the end of stream, chainable. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); @@ -292,8 +288,8 @@ pub trait Stream: Sized { /// Declare appending the list of given size, chainable. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); @@ -308,8 +304,8 @@ pub trait Stream: Sized { /// Apends null to the end of stream, chainable. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); @@ -326,8 +322,8 @@ pub trait Stream: Sized { /// Clear the output stream so far. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(3); @@ -342,8 +338,8 @@ pub trait Stream: Sized { /// Returns true if stream doesnt expect any more items. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); diff --git a/util/src/rlp/tests.rs b/util/rlp/src/tests.rs similarity index 92% rename from util/src/rlp/tests.rs rename to util/rlp/src/tests.rs index 5cb8094c6..1d98e4972 100644 --- a/util/src/rlp/tests.rs +++ b/util/rlp/src/tests.rs @@ -16,8 +16,7 @@ use std::{fmt, cmp}; use std::str::FromStr; -use rlp; -use rlp::{UntrustedRlp, RlpStream, View, Stream, DecoderError}; +use ::{Encodable, RlpDecodable, UntrustedRlp, RlpStream, View, Stream, DecoderError}; use bigint::uint::U256; #[test] @@ -26,26 +25,22 @@ fn rlp_at() { { let rlp = UntrustedRlp::new(&data); assert!(rlp.is_list()); - //let animals = as rlp::RlpDecodable>::decode_untrusted(&rlp).unwrap(); let animals: Vec = rlp.as_val().unwrap(); assert_eq!(animals, vec!["cat".to_owned(), "dog".to_owned()]); let cat = rlp.at(0).unwrap(); assert!(cat.is_data()); assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']); - //assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_owned()); assert_eq!(cat.as_val::().unwrap(), "cat".to_owned()); let dog = rlp.at(1).unwrap(); assert!(dog.is_data()); assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']); - //assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_owned()); assert_eq!(dog.as_val::().unwrap(), "dog".to_owned()); let cat_again = rlp.at(0).unwrap(); assert!(cat_again.is_data()); assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']); - //assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_owned()); assert_eq!(cat_again.as_val::().unwrap(), "cat".to_owned()); } } @@ -58,10 +53,10 @@ fn rlp_at_err() { assert!(rlp.is_list()); let cat_err = rlp.at(0).unwrap_err(); - assert_eq!(cat_err, rlp::DecoderError::RlpIsTooShort); + assert_eq!(cat_err, DecoderError::RlpIsTooShort); let dog_err = rlp.at(1).unwrap_err(); - assert_eq!(dog_err, rlp::DecoderError::RlpIsTooShort); + assert_eq!(dog_err, DecoderError::RlpIsTooShort); } } @@ -89,13 +84,13 @@ fn rlp_iter() { } } -struct ETestPair(T, Vec) where T: rlp::Encodable; +struct ETestPair(T, Vec) where T: Encodable; fn run_encode_tests(tests: Vec>) - where T: rlp::Encodable + where T: Encodable { for t in &tests { - let res = rlp::encode(&t.0); + let res = super::encode(&t.0); assert_eq!(&res[..], &t.1[..]); } } @@ -165,7 +160,7 @@ fn encode_str() { #[test] fn encode_address() { - use hash::*; + use bigint::hash::H160; let tests = vec![ ETestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"), @@ -206,11 +201,11 @@ fn encode_vector_str() { run_encode_tests(tests); } -struct DTestPair(T, Vec) where T: rlp::RlpDecodable + fmt::Debug + cmp::Eq; +struct DTestPair(T, Vec) where T: RlpDecodable + fmt::Debug + cmp::Eq; -fn run_decode_tests(tests: Vec>) where T: rlp::RlpDecodable + fmt::Debug + cmp::Eq { +fn run_decode_tests(tests: Vec>) where T: RlpDecodable + fmt::Debug + cmp::Eq { for t in &tests { - let res: T = rlp::decode(&t.1); + let res: T = super::decode(&t.1); assert_eq!(res, t.0); } } @@ -301,7 +296,7 @@ fn decode_untrusted_str() { #[test] fn decode_untrusted_address() { - use hash::*; + use bigint::hash::H160; let tests = vec![ DTestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"), @@ -340,8 +335,8 @@ fn decode_untrusted_vector_of_vectors_str() { #[test] fn test_decoding_array() { let v = vec![5u16, 2u16]; - let res = rlp::encode(&v); - let arr: [u16; 2] = rlp::decode(&res); + let res = super::encode(&v); + let arr: [u16; 2] = super::decode(&res); assert_eq!(arr[0], 5); assert_eq!(arr[1], 2); } diff --git a/util/src/rlp/untrusted_rlp.rs b/util/rlp/src/untrusted_rlp.rs similarity index 98% rename from util/src/rlp/untrusted_rlp.rs rename to util/rlp/src/untrusted_rlp.rs index fdf584211..8f954e3e6 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/rlp/src/untrusted_rlp.rs @@ -17,8 +17,9 @@ use std::cell::Cell; use std::fmt; use rustc_serialize::hex::ToHex; -use rlp::bytes::{FromBytes, FromBytesResult, FromBytesError}; -use rlp::{View, Decoder, Decodable, DecoderError, RlpDecodable}; + +use bytes::{FromBytes, FromBytesResult, FromBytesError}; +use ::{View, Decoder, Decodable, DecoderError, RlpDecodable}; /// rlp offset #[derive(Copy, Clone, Debug)] @@ -490,7 +491,7 @@ impl RlpDecodable for u8 { #[cfg(test)] mod tests { - use rlp::{UntrustedRlp, View}; + use ::{UntrustedRlp, View}; #[test] fn test_rlp_display() { use rustc_serialize::hex::FromHex; diff --git a/util/src/lib.rs b/util/src/lib.rs index f7459615e..57ea9c152 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -90,29 +90,32 @@ extern crate rustc_serialize; extern crate rand; extern crate rocksdb; -#[macro_use] -extern crate heapsize; -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate itertools; extern crate env_logger; extern crate crypto as rcrypto; extern crate secp256k1; extern crate arrayvec; extern crate elastic_array; -#[macro_use] -extern crate log as rlog; extern crate time; extern crate ethcore_devtools as devtools; extern crate libc; extern crate target_info; extern crate bigint; extern crate parking_lot; -pub extern crate using_queue; -pub extern crate table; extern crate ansi_term; extern crate tiny_keccak; +extern crate rlp; + +#[macro_use] +extern crate heapsize; +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate itertools; +#[macro_use] +extern crate log as rlog; + +pub extern crate using_queue; +pub extern crate table; pub mod bloom; pub mod standard; @@ -122,7 +125,6 @@ pub mod from_json; pub mod common; pub mod error; pub mod bytes; -pub mod rlp; pub mod misc; pub mod vector; pub mod sha3; @@ -144,7 +146,6 @@ mod timer; pub use common::*; pub use misc::*; -pub use rlp::*; pub use hashdb::*; pub use memorydb::*; pub use overlaydb::*; diff --git a/util/src/sha3.rs b/util/src/sha3.rs index 0dcde2ccb..5ab55257d 100644 --- a/util/src/sha3.rs +++ b/util/src/sha3.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -//! Wrapper around tiny-keccak crate. +//! Wrapper around tiny-keccak crate as well as common hash constants. extern crate sha3 as sha3_ext; use std::io; @@ -27,6 +27,11 @@ use self::sha3_ext::*; /// Get the SHA3 (i.e. Keccak) hash of the empty bytes string. pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] ); +/// The SHA3 of the RLP encoding of empty data. +pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] ); + +/// The SHA3 of the RLP encoding of empty list. +pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] ); /// Types implementing this trait are sha3able. /// diff --git a/util/src/trie/mod.rs b/util/src/trie/mod.rs index 886ccd724..b71f6a5e2 100644 --- a/util/src/trie/mod.rs +++ b/util/src/trie/mod.rs @@ -84,7 +84,7 @@ pub trait Trie { fn root(&self) -> &H256; /// Is the trie empty? - fn is_empty(&self) -> bool { *self.root() == ::rlp::SHA3_NULL_RLP } + fn is_empty(&self) -> bool { *self.root() == ::sha3::SHA3_NULL_RLP } /// Does the trie contain a given key? fn contains(&self, key: &[u8]) -> Result { diff --git a/util/src/trie/triedb.rs b/util/src/trie/triedb.rs index 8e6cb98fa..88a3399e7 100644 --- a/util/src/trie/triedb.rs +++ b/util/src/trie/triedb.rs @@ -32,11 +32,11 @@ use super::{Trie, TrieItem, TrieError}; /// # Example /// ``` /// extern crate ethcore_util as util; +/// /// use util::trie::*; /// use util::hashdb::*; /// use util::memorydb::*; /// use util::hash::*; -/// use util::rlp::*; /// /// fn main() { /// let mut memdb = MemoryDB::new(); diff --git a/util/src/trie/triedbmut.rs b/util/src/trie/triedbmut.rs index 8d3d7d3d3..5e2ef6b79 100644 --- a/util/src/trie/triedbmut.rs +++ b/util/src/trie/triedbmut.rs @@ -19,10 +19,11 @@ use super::{TrieError, TrieMut}; use super::node::Node as RlpNode; -use ::{Bytes, HashDB, H256, SHA3_NULL_RLP}; +use ::{Bytes, HashDB, H256}; use ::bytes::ToPretty; use ::nibbleslice::NibbleSlice; use ::rlp::{Rlp, RlpStream, View, Stream}; +use ::sha3::SHA3_NULL_RLP; use elastic_array::ElasticArray1024; @@ -261,18 +262,18 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage { /// # Example /// ``` /// extern crate ethcore_util as util; +/// /// use util::trie::*; /// use util::hashdb::*; /// use util::memorydb::*; /// use util::hash::*; -/// use util::rlp::*; /// /// fn main() { /// let mut memdb = MemoryDB::new(); /// let mut root = H256::new(); /// let mut t = TrieDBMut::new(&mut memdb, &mut root); /// assert!(t.is_empty()); -/// assert_eq!(*t.root(), SHA3_NULL_RLP); +/// assert_eq!(*t.root(), ::util::sha3::SHA3_NULL_RLP); /// t.insert(b"foo", b"bar").unwrap(); /// assert!(t.contains(b"foo").unwrap()); /// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar"); @@ -951,8 +952,8 @@ mod tests { use hashdb::*; use memorydb::*; use super::*; - use rlp::*; use bytes::ToPretty; + use sha3::SHA3_NULL_RLP; use super::super::TrieMut; use super::super::standardmap::*;