diff --git a/Cargo.lock b/Cargo.lock index 020304ad..03771972 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -91,7 +91,7 @@ name = "bigint" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "heapsize 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -150,7 +150,7 @@ name = "chain" version = "0.1.0" dependencies = [ "bitcrypto 0.1.0", - "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "heapsize 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "primitives 0.1.0", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "serialization 0.1.0", @@ -198,12 +198,12 @@ dependencies = [ "bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "chain 0.1.0", "elastic-array 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "ethcore-devtools 1.3.0", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "primitives 0.1.0", "rocksdb 0.4.5 (git+https://github.com/ethcore/rust-rocksdb)", "serialization 0.1.0", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "test-data 0.1.0", ] @@ -236,7 +236,7 @@ name = "elastic-array" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "heapsize 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -260,13 +260,6 @@ dependencies = [ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ethcore-devtools" -version = "1.3.0" -dependencies = [ - "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "futures" version = "0.1.12" @@ -291,7 +284,7 @@ dependencies = [ [[package]] name = "heapsize" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -500,7 +493,7 @@ dependencies = [ "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "chain 0.1.0", "db 0.1.0", - "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "heapsize 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "keys 0.1.0", "network 0.1.0", "primitives 0.1.0", @@ -735,7 +728,7 @@ version = "0.1.0" dependencies = [ "bigint 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "heapsize 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1021,6 +1014,14 @@ dependencies = [ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tempdir" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "term_size" version = "0.2.3" @@ -1230,7 +1231,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum futures 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "0ffa9721bfc26c0293ab2195bbf037aefb3951ece8d755e01fb90e0177acba79" "checksum futures-cpupool 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a283c84501e92cade5ea673a2a7ca44f71f209ccdd302a3e0896f50083d2c5ff" "checksum gcc 0.3.45 (registry+https://github.com/rust-lang/crates.io-index)" = "40899336fb50db0c78710f53e87afc54d8c7266fb76262fecc78ca1a7f09deae" -"checksum heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "5a376f7402b85be6e0ba504243ecbc0709c48019ecc6286d0540c2e359050c88" +"checksum heapsize 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "556cd479866cf85c3f671209c85e8a6990211c916d1002c2fcb2e9b7cf60bc36" "checksum httparse 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a6e7a63e511f9edffbab707141fbb8707d1a3098615fb2adbd5769cdfcc9b17d" "checksum hyper 0.10.0-a.0 (git+https://github.com/ethcore/hyper)" = "" "checksum idna 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6ac85ec3f80c8e4e99d9325521337e14ec7555c458a14e377d189659a427f375" @@ -1294,6 +1295,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" "checksum syn 0.11.10 (registry+https://github.com/rust-lang/crates.io-index)" = "171b739972d9a1bfb169e8077238b51f9ebeaae4ff6e08072f7ba386a8802da2" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" +"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" "checksum term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "07b6c1ac5b3fffd75073276bca1ceed01f67a28537097a2a9539e116e50fb21a" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" diff --git a/db/Cargo.toml b/db/Cargo.toml index 01c442d3..e24502f2 100644 --- a/db/Cargo.toml +++ b/db/Cargo.toml @@ -14,5 +14,5 @@ serialization = { path = "../serialization" } chain = { path = "../chain" } [dev-dependencies] -ethcore-devtools = { path = "../devtools" } +tempdir = "0.3" test-data = { path = "../test-data" } diff --git a/db/src/block_chain_db.rs b/db/src/block_chain_db.rs index 8ce06536..836f064b 100644 --- a/db/src/block_chain_db.rs +++ b/db/src/block_chain_db.rs @@ -32,9 +32,9 @@ const COL_TRANSACTIONS: u32 = 4; const COL_TRANSACTIONS_META: u32 = 5; const COL_BLOCK_NUMBERS: u32 = 6; -const KEY_VERSION: &'static[u8] = b"version"; -const KEY_BEST_BLOCK_NUMBER: &'static[u8] = b"best_block_number"; -const KEY_BEST_BLOCK_HASH: &'static[u8] = b"best_block_hash"; +const KEY_VERSION: &'static str = "version"; +const KEY_BEST_BLOCK_NUMBER: &'static str = "best_block_number"; +const KEY_BEST_BLOCK_HASH: &'static str = "best_block_hash"; const DB_VERSION: u32 = 1; const MAX_FORK_ROUTE_PRESET: usize = 2048; @@ -94,8 +94,8 @@ impl BlockChainDatabase { impl BlockChainDatabase where T: KeyValueDatabase { fn read_best_block(db: &T) -> Option { - let best_number = db.get(COL_META.into(), KEY_BEST_BLOCK_NUMBER); - let best_hash = db.get(COL_META.into(), KEY_BEST_BLOCK_HASH); + let best_number = db.get(COL_META.into(), &serialize(&KEY_BEST_BLOCK_NUMBER)); + let best_hash = db.get(COL_META.into(), &serialize(&KEY_BEST_BLOCK_HASH)); match (best_number, best_hash) { (Ok(None), Ok(None)) => None, @@ -246,8 +246,8 @@ impl BlockChainDatabase where T: KeyValueDatabase { let mut update = DBTransaction::new(); update.insert(COL_BLOCK_HASHES.into(), &new_best_block.number, &new_best_block.hash); update.insert(COL_BLOCK_NUMBERS.into(), &new_best_block.hash, &new_best_block.number); - update.insert_raw(COL_META.into(), KEY_BEST_BLOCK_HASH, &serialize(&new_best_block.hash)); - update.insert_raw(COL_META.into(), KEY_BEST_BLOCK_NUMBER, &serialize(&new_best_block.number)); + update.insert(COL_META.into(), &KEY_BEST_BLOCK_HASH, &new_best_block.hash); + update.insert(COL_META.into(), &KEY_BEST_BLOCK_NUMBER, &new_best_block.number); let mut modified_meta: HashMap = HashMap::new(); if let Some(tx) = block.transactions.first() { @@ -309,8 +309,8 @@ impl BlockChainDatabase where T: KeyValueDatabase { let mut update = DBTransaction::new(); update.delete(COL_BLOCK_HASHES.into(), &block_number); update.delete(COL_BLOCK_NUMBERS.into(), &block_hash); - update.insert_raw(COL_META.into(), KEY_BEST_BLOCK_HASH, &serialize(&new_best_block.hash)); - update.insert_raw(COL_META.into(), KEY_BEST_BLOCK_NUMBER, &serialize(&new_best_block.number)); + update.insert(COL_META.into(), &KEY_BEST_BLOCK_HASH, &new_best_block.hash); + update.insert(COL_META.into(), &KEY_BEST_BLOCK_NUMBER, &new_best_block.number); let mut modified_meta: HashMap = HashMap::new(); for tx in block.transactions.iter().skip(1) { diff --git a/db/src/kv/diskdb.rs b/db/src/kv/diskdb.rs index 903eefb7..ff8410d1 100644 --- a/db/src/kv/diskdb.rs +++ b/db/src/kv/diskdb.rs @@ -291,15 +291,15 @@ impl Database { #[cfg(test)] mod tests { - extern crate ethcore_devtools as devtools; + extern crate tempdir; + use self::tempdir::TempDir; use kv::{Transaction, Location}; use super::*; - use self::devtools::*; fn test_db(config: DatabaseConfig) { - let path = RandomTempPath::create_dir(); - let db = Database::open(config, path.as_path().to_str().unwrap()).unwrap(); + let tempdir = TempDir::new("").unwrap(); + let db = Database::open(config, tempdir.path()).unwrap(); let key1 = b"key1"; let key2 = b"key2"; @@ -338,8 +338,8 @@ mod tests { #[test] fn kvdb() { - let path = RandomTempPath::create_dir(); - let _ = Database::open_default(path.as_path().to_str().unwrap()).unwrap(); + let tempdir = TempDir::new("").unwrap(); + let _ = Database::open_default(tempdir.path()).unwrap(); test_db(DatabaseConfig::default()); } } diff --git a/devtools/Cargo.toml b/devtools/Cargo.toml deleted file mode 100644 index a56828b0..00000000 --- a/devtools/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -description = "Ethcore development/test/build tools" -homepage = "http://ethcore.io" -license = "GPL-3.0" -name = "ethcore-devtools" -version = "1.3.0" -authors = ["Ethcore "] - -[dependencies] -rand = "0.3" - -[lib] -path = "src/lib.rs" -test = true diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs deleted file mode 100644 index bf379c2b..00000000 --- a/devtools/src/lib.rs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2015, 2016 Ethcore (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 . - -//! dev-tools - - -extern crate rand; - -mod random_path; - -pub use random_path::*; diff --git a/devtools/src/random_path.rs b/devtools/src/random_path.rs deleted file mode 100644 index bbfc40ac..00000000 --- a/devtools/src/random_path.rs +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2015, 2016 Ethcore (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 . - -//! Random path - -use std::path::PathBuf; -use std::{fs, env, ops}; -use rand::random; - -pub struct RandomTempPath { - path: PathBuf -} - -pub fn random_filename() -> String { - random_str(8) -} - -pub fn random_str(len: usize) -> String { - (0..len).map(|_| ((random::() * 26.0) as u8 + 97) as char).collect() -} - -impl RandomTempPath { - pub fn new() -> RandomTempPath { - let mut dir = env::temp_dir(); - dir.push(random_filename()); - RandomTempPath { - path: dir.clone() - } - } - - pub fn create_dir() -> RandomTempPath { - let mut dir = env::temp_dir(); - dir.push(random_filename()); - fs::create_dir_all(dir.as_path()).unwrap(); - RandomTempPath { - path: dir.clone() - } - } - - pub fn as_path(&self) -> &PathBuf { - &self.path - } - - pub fn as_str(&self) -> &str { - self.path.to_str().unwrap() - } - - pub fn new_in(&self, name: &str) -> String { - let mut path = self.path.clone(); - path.push(name); - path.to_str().unwrap().to_owned() - } -} - -impl Drop for RandomTempPath { - fn drop(&mut self) { - if let Err(e) = fs::remove_dir_all(self.as_path()) { - panic!("Failed to remove temp directory. Here's what prevented this from happening: ({})", e); - } - } -} - -pub struct GuardedTempResult { - pub result: Option, - pub _temp: RandomTempPath -} - -impl GuardedTempResult { - pub fn reference(&self) -> &T { - self.result.as_ref().unwrap() - } - - pub fn reference_mut(&mut self) -> &mut T { - self.result.as_mut().unwrap() - } - - pub fn take(&mut self) -> T { - self.result.take().unwrap() - } -} - -impl ops::Deref for GuardedTempResult { - type Target = T; - - fn deref(&self) -> &T { - self.result.as_ref().unwrap() - } -} - -impl ops::DerefMut for GuardedTempResult { - fn deref_mut(&mut self) -> &mut T { - self.result.as_mut().unwrap() - } -} - -#[test] -fn creates_dir() { - let temp = RandomTempPath::create_dir(); - assert!(fs::metadata(temp.as_path()).unwrap().is_dir()); -} - -#[test] -fn destroys_dir() { - let path_buf = { - let temp = RandomTempPath::create_dir(); - assert!(fs::metadata(temp.as_path()).unwrap().is_dir()); - let path_buf = temp.as_path().to_path_buf(); - path_buf - }; - - assert!(fs::metadata(&path_buf).is_err()); -} - -#[test] -fn provides_random() { - let temp = RandomTempPath::create_dir(); - assert!(temp.as_path().to_str().is_some()); -} diff --git a/serialization/src/impls.rs b/serialization/src/impls.rs index c02d37fa..07ee169b 100644 --- a/serialization/src/impls.rs +++ b/serialization/src/impls.rs @@ -159,6 +159,21 @@ impl Serializable for String { } } +impl<'a> Serializable for &'a str { + fn serialize(&self, stream: &mut Stream) { + let bytes: &[u8] = self.as_bytes(); + stream + .append(&CompactInteger::from(bytes.len())) + .append_slice(bytes); + } + + #[inline] + fn serialized_size(&self) -> usize { + let bytes: &[u8] = self.as_bytes(); + CompactInteger::from(bytes.len()).serialized_size() + bytes.len() + } +} + impl Deserializable for String { fn deserialize(reader: &mut Reader) -> Result where T: io::Read { let bytes: Bytes = try!(reader.read()); diff --git a/tools/clippy.sh b/tools/clippy.sh index 1e45159f..d1236a0d 100755 --- a/tools/clippy.sh +++ b/tools/clippy.sh @@ -17,7 +17,6 @@ cd primitives cargo clippy -p bitcrypto cargo clippy -p chain cargo clippy -p db -cargo clippy -p ethcore-devtools cargo clippy -p import cargo clippy -p keys cargo clippy -p message diff --git a/tools/doc.sh b/tools/doc.sh index 241813b2..170771ef 100755 --- a/tools/doc.sh +++ b/tools/doc.sh @@ -4,7 +4,6 @@ cargo doc --no-deps\ -p bitcrypto\ -p chain\ -p db\ - -p ethcore-devtools\ -p import\ -p keys\ -p message\ diff --git a/tools/test.sh b/tools/test.sh index e2826ffd..3dbf92e9 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -4,7 +4,6 @@ cargo test\ -p bitcrypto\ -p chain\ -p db\ - -p ethcore-devtools\ -p import\ -p keys\ -p message\