diff --git a/.cargo/config b/.cargo/config index 3f07816e3c7..a818c0bf40a 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,3 +1,27 @@ +# NOTE: if you make changes here, remember to also update: +# scripts/test-linux.sh +# scripts/build-linux.sh +# scripts/build-windows.sh + +# Using 'cfg` is broken, see https://github.com/rust-lang/cargo/issues/6858 +#[target.'cfg(target_arch = "x86_64")'] +#rustflags = ["-Ctarget-feature=+aes,+sse2,+ssse3"] + +# …so instead we list all target triples (Tier 1 64-bit platforms) +[target.x86_64-unknown-linux-gnu] +# Enables the aes-ni instructions for RustCrypto dependency. +rustflags = ["-Ctarget-feature=+aes,+sse2,+ssse3"] + +[target.x86_64-pc-windows-gnu] +# Enables the aes-ni instructions for RustCrypto dependency. +rustflags = ["-Ctarget-feature=+aes,+sse2,+ssse3"] + [target.x86_64-pc-windows-msvc] +# Enables the aes-ni instructions for RustCrypto dependency. # Link the C runtime statically ; https://github.com/paritytech/parity-ethereum/issues/6643 -rustflags = ["-Ctarget-feature=+crt-static"] +rustflags = ["-Ctarget-feature=+aes,+sse2,+ssse3", "-Ctarget-feature=+crt-static"] + +[target.x86_64-apple-darwin] +# Enables the aes-ni instructions for RustCrypto dependency. +rustflags = ["-Ctarget-feature=+aes,+sse2,+ssse3"] + diff --git a/Cargo.lock b/Cargo.lock index c3a00e53ff4..3eb1a0c8465 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1013,6 +1013,7 @@ version = "1.12.0" dependencies = [ "bincode 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "common-types 0.1.0", + "derive_more 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore 1.12.0", "ethcore-blockchain 0.1.0", @@ -1106,7 +1107,7 @@ name = "ethcore-network" version = "1.12.0" dependencies = [ "assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "derive_more 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore-io 1.12.0", "ethereum-types 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethkey 0.3.0", @@ -1148,10 +1149,8 @@ dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1243,7 +1242,7 @@ name = "ethcore-service" version = "0.1.0" dependencies = [ "ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", - "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "derive_more 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore 1.12.0", "ethcore-blockchain 0.1.0", "ethcore-db 0.1.0", @@ -1606,11 +1605,6 @@ dependencies = [ "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "gcc" -version = "0.3.55" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "generic-array" version = "0.9.0" @@ -3664,18 +3658,6 @@ name = "rprompt" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "rust-crypto" -version = "0.2.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "rustc-demangle" version = "0.1.9" @@ -3789,6 +3771,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "serde" version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "serde_derive" @@ -4888,7 +4873,6 @@ dependencies = [ "checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" "checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" -"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797" @@ -5061,7 +5045,6 @@ dependencies = [ "checksum rlp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3b0d56c1450bfbef1181fdeb78b902dc1d23178de77c23d705317508e03d1b7c" "checksum rpassword 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b273c91bd242ca03ad6d71c143b6f17a48790e61f21a6c78568fa2b6774a24a4" "checksum rprompt 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1601f32bc5858aae3cbfa1c645c96c4d820cc5c16be0194f089560c00b6eb625" -"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" "checksum rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0ceb8ce7a5e520de349e1fa172baeba4a9e8d5ef06c47471863530bc4972ee1e" "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" diff --git a/ethcore/benches/builtin.rs b/ethcore/benches/builtin.rs index d7ed483dd03..974284bb014 100644 --- a/ethcore/benches/builtin.rs +++ b/ethcore/benches/builtin.rs @@ -29,7 +29,7 @@ use criterion::{Criterion, Bencher}; use bytes::BytesRef; use ethcore::builtin::Builtin; use ethcore::machine::EthereumMachine; -use ethereum_types::U256; +use ethereum_types::H160; use ethcore::ethereum::new_byzantium_test_machine; use rustc_hex::FromHex; @@ -46,8 +46,9 @@ struct BuiltinBenchmark<'a> { impl<'a> BuiltinBenchmark<'a> { fn new(builtin_address: &'static str, input: &str, expected: &str) -> BuiltinBenchmark<'a> { let builtins = BYZANTIUM_MACHINE.builtins(); - - let builtin = builtins.get(&builtin_address.into()).unwrap().clone(); + use std::str::FromStr; + let addr = H160::from_str(builtin_address).unwrap(); + let builtin = builtins.get(&addr).unwrap().clone(); let input = FromHex::from_hex(input).unwrap(); let expected = FromHex::from_hex(expected).unwrap(); @@ -56,10 +57,6 @@ impl<'a> BuiltinBenchmark<'a> { } } - fn gas_cost(&self) -> U256 { - self.builtin.cost(&self.input) - } - fn run(&self, b: &mut Bencher) { let mut output = vec![0; self.expected.len()]; diff --git a/ethcore/blockchain/src/blockchain.rs b/ethcore/blockchain/src/blockchain.rs index c5a04472be6..bc500f3355a 100644 --- a/ethcore/blockchain/src/blockchain.rs +++ b/ethcore/blockchain/src/blockchain.rs @@ -57,7 +57,7 @@ use crate::{CacheSize, ImportRoute, Config}; /// Database backing `BlockChain`. pub trait BlockChainDB: Send + Sync { /// Generic key value store. - fn key_value(&self) -> &Arc; + fn key_value(&self) -> &Arc; /// Header blooms database. fn blooms(&self) -> &blooms_db::Database; @@ -85,7 +85,7 @@ pub trait BlockChainDB: Send + Sync { /// predefined config. pub trait BlockChainDBHandler: Send + Sync { /// Open the predefined key-value database. - fn open(&self, path: &Path) -> io::Result>; + fn open(&self, path: &Path) -> io::Result>; } /// Interface for querying blocks by hash and by number. @@ -228,7 +228,7 @@ pub struct BlockChain { transaction_addresses: RwLock>, block_receipts: RwLock>, - db: Arc, + db: Arc, cache_man: Mutex>, @@ -481,7 +481,7 @@ impl<'a> Iterator for AncestryWithMetadataIter<'a> { /// Returns epoch transitions. pub struct EpochTransitionIter<'a> { chain: &'a BlockChain, - prefix_iter: Box, Box<[u8]>)> + 'a>, + prefix_iter: Box, Box<[u8]>)> + 'a>, } impl<'a> Iterator for EpochTransitionIter<'a> { @@ -521,7 +521,7 @@ impl<'a> Iterator for EpochTransitionIter<'a> { impl BlockChain { /// Create new instance of blockchain from given Genesis. - pub fn new(config: Config, genesis: &[u8], db: Arc) -> BlockChain { + pub fn new(config: Config, genesis: &[u8], db: Arc) -> BlockChain { // 400 is the average size of the key let cache_man = CacheManager::new(config.pref_cache_size, config.max_cache_size, 400); @@ -1592,11 +1592,11 @@ mod tests { _trace_blooms_dir: TempDir, blooms: blooms_db::Database, trace_blooms: blooms_db::Database, - key_value: Arc, + key_value: Arc, } impl BlockChainDB for TestBlockChainDB { - fn key_value(&self) -> &Arc { + fn key_value(&self) -> &Arc { &self.key_value } @@ -1610,7 +1610,7 @@ mod tests { } /// Creates new test instance of `BlockChainDB` - pub fn new_db() -> Arc { + pub fn new_db() -> Arc { let blooms_dir = TempDir::new("").unwrap(); let trace_blooms_dir = TempDir::new("").unwrap(); @@ -1625,15 +1625,15 @@ mod tests { Arc::new(db) } - fn new_chain(genesis: encoded::Block, db: Arc) -> BlockChain { + fn new_chain(genesis: encoded::Block, db: Arc) -> BlockChain { BlockChain::new(Config::default(), genesis.raw(), db) } - fn insert_block(db: &Arc, bc: &BlockChain, block: encoded::Block, receipts: Vec) -> ImportRoute { + fn insert_block(db: &Arc, bc: &BlockChain, block: encoded::Block, receipts: Vec) -> ImportRoute { insert_block_commit(db, bc, block, receipts, true) } - fn insert_block_commit(db: &Arc, bc: &BlockChain, block: encoded::Block, receipts: Vec, commit: bool) -> ImportRoute { + fn insert_block_commit(db: &Arc, bc: &BlockChain, block: encoded::Block, receipts: Vec, commit: bool) -> ImportRoute { let mut batch = db.key_value().transaction(); let res = insert_block_batch(&mut batch, bc, block, receipts); db.key_value().write(batch).unwrap(); diff --git a/ethcore/light/Cargo.toml b/ethcore/light/Cargo.toml index 8f76651d03c..fe0901dd3c5 100644 --- a/ethcore/light/Cargo.toml +++ b/ethcore/light/Cargo.toml @@ -10,6 +10,7 @@ authors = ["Parity Technologies "] log = "0.4" parity-bytes = "0.1" common-types = { path = "../types" } +derive_more = "0.14.0" ethcore = { path = ".."} ethcore-db = { path = "../db" } ethcore-blockchain = { path = "../blockchain" } diff --git a/ethcore/light/src/client/header_chain.rs b/ethcore/light/src/client/header_chain.rs index 2bcf53cb9f7..be52185bc53 100644 --- a/ethcore/light/src/client/header_chain.rs +++ b/ethcore/light/src/client/header_chain.rs @@ -260,7 +260,7 @@ impl HeaderChain { let best_block = { let era = match candidates.get(&curr.best_num) { Some(era) => era, - None => bail!("Database corrupt: highest block referenced but no data."), + None => return Err("Database corrupt: highest block referenced but no data.".into()), }; let best = &era.candidates[0]; @@ -582,7 +582,7 @@ impl HeaderChain { } else { let msg = format!("header of block #{} not found in DB ; database in an \ inconsistent state", h_num); - bail!(msg); + return Err(msg.into()); }; let decoded = header.decode().expect("decoding db value failed"); diff --git a/ethcore/light/src/lib.rs b/ethcore/light/src/lib.rs index 31deecf3123..5221830af90 100644 --- a/ethcore/light/src/lib.rs +++ b/ethcore/light/src/lib.rs @@ -86,8 +86,7 @@ extern crate keccak_hash as hash; extern crate triehash_ethereum as triehash; extern crate kvdb; extern crate memory_cache; -#[macro_use] -extern crate error_chain; +extern crate derive_more; #[cfg(test)] extern crate kvdb_memorydb; diff --git a/ethcore/light/src/on_demand/mod.rs b/ethcore/light/src/on_demand/mod.rs index 7d1f4fabf8e..b54257c2794 100644 --- a/ethcore/light/src/on_demand/mod.rs +++ b/ethcore/light/src/on_demand/mod.rs @@ -66,32 +66,31 @@ pub const DEFAULT_NUM_CONSECUTIVE_FAILED_REQUESTS: usize = 1; /// OnDemand related errors pub mod error { - // Silence: `use of deprecated item 'std::error::Error::cause': replaced by Error::source, which can support downcasting` - // https://github.com/paritytech/parity-ethereum/issues/10302 - #![allow(deprecated)] - use futures::sync::oneshot::Canceled; - error_chain! { - - foreign_links { - ChannelCanceled(Canceled) #[doc = "Canceled oneshot channel"]; - } - - errors { - #[doc = "Timeout bad response"] - BadResponse(err: String) { - description("Max response evaluation time exceeded") - display("{}", err) - } + /// OnDemand Error + #[derive(Debug, derive_more::Display, derive_more::From)] + pub enum Error { + /// Canceled oneshot channel + ChannelCanceled(Canceled), + /// Timeout bad response + BadResponse(String), + /// OnDemand requests limit exceeded + #[display(fmt = "OnDemand request maximum backoff iterations exceeded")] + RequestLimit, + } - #[doc = "OnDemand requests limit exceeded"] - RequestLimit { - description("OnDemand request maximum backoff iterations exceeded") - display("OnDemand request maximum backoff iterations exceeded") + impl std::error::Error for Error { + fn source(&self) -> Option<&(std::error::Error + 'static)> { + match self { + Error::ChannelCanceled(err) => Some(err), + _ => None, } } } + + /// OnDemand Result + pub type Result = std::result::Result; } /// Public interface for performing network requests `OnDemand` @@ -272,7 +271,7 @@ impl Pending { response_err ); - let err = self::error::ErrorKind::BadResponse(err); + let err = self::error::Error::BadResponse(err); if self.sender.send(Err(err.into())).is_err() { debug!(target: "on_demand", "Dropped oneshot channel receiver on no response"); } @@ -280,7 +279,7 @@ impl Pending { // returning a peer discovery timeout during query attempts fn request_limit_reached(self) { - let err = self::error::ErrorKind::RequestLimit; + let err = self::error::Error::RequestLimit; if self.sender.send(Err(err.into())).is_err() { debug!(target: "on_demand", "Dropped oneshot channel receiver on time out"); } diff --git a/ethcore/private-tx/src/lib.rs b/ethcore/private-tx/src/lib.rs index b5d36d35967..b9de9e16735 100644 --- a/ethcore/private-tx/src/lib.rs +++ b/ethcore/private-tx/src/lib.rs @@ -16,10 +16,6 @@ //! Private transactions module. -// Recursion limit required because of -// error_chain foreign_links. -#![recursion_limit="256"] - mod encryptor; mod key_server_keys; mod private_transactions; diff --git a/ethcore/res/ethereum/classic.json b/ethcore/res/ethereum/classic.json index 393129c12f0..682b752991e 100644 --- a/ethcore/res/ethereum/classic.json +++ b/ethcore/res/ethereum/classic.json @@ -8,11 +8,12 @@ "difficultyBoundDivisor": "0x0800", "durationLimit": "0x0d", "blockReward": "0x4563918244F40000", - "homesteadTransition": 1150000, - "ecip1010PauseTransition": 3000000, - "ecip1010ContinueTransition": 5000000, - "ecip1017EraRounds": 5000000, - "bombDefuseTransition": 5900000 + "homesteadTransition": "0x118c30", + "ecip1010PauseTransition": "0x2dc6c0", + "ecip1010ContinueTransition": "0x4c4b40", + "ecip1017EraRounds": "0x4c4b40", + "eip100bTransition": "0x7fffffffffffffff", + "bombDefuseTransition": "0x5a06e0" } } }, @@ -26,11 +27,17 @@ "chainID": "0x3d", "forkBlock": "0x1d4c00", "forkCanonHash": "0x94365e3a8c0b35089c1d1195081fe7489b528a84b22199c916180db8b28ade7f", - "eip150Transition": 2500000, - "eip160Transition": 3000000, + "eip150Transition": "0x2625a0", + "eip160Transition": "0x2dc6c0", "eip161abcTransition": "0x7fffffffffffffff", "eip161dTransition": "0x7fffffffffffffff", - "eip155Transition": 3000000 + "eip155Transition": "0x2dc6c0", + "maxCodeSize": "0x6000", + "maxCodeSizeTransition": "0x7fffffffffffffff", + "eip140Transition": "0x7fffffffffffffff", + "eip211Transition": "0x7fffffffffffffff", + "eip214Transition": "0x7fffffffffffffff", + "eip658Transition": "0x7fffffffffffffff" }, "genesis": { "seal": { @@ -3835,7 +3842,7 @@ "0xc32fd5318214071a41cd8e98499b2b65942c5837c686a06b536146fd0bf294bf", "0xac390c012eecd83fa8f4cc77a59992914b5c95af36b28747e07adea13228acbc" ] - }, + }, "nodes": [ "enode://efd48ad0879eeb7f9cb5e50f33f7bc21e805a72e90361f145baaa22dd75d111e7cd9c93f1b7060dcb30aa1b3e620269336dbf32339fea4c18925a4c15fe642df@18.205.66.229:30303", "enode://5fbfb426fbb46f8b8c1bd3dd140f5b511da558cd37d60844b525909ab82e13a25ee722293c829e52cb65c2305b1637fa9a2ea4d6634a224d5f400bfe244ac0de@162.243.55.45:30303", @@ -3851,10 +3858,97 @@ "enode://5cd218959f8263bc3721d7789070806b0adff1a0ed3f95ec886fb469f9362c7507e3b32b256550b9a7964a23a938e8d42d45a0c34b332bfebc54b29081e83b93@35.187.57.94:30303" ], "accounts": { - "0000000000000000000000000000000000000001": { "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, - "0000000000000000000000000000000000000002": { "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, - "0000000000000000000000000000000000000003": { "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, - "0000000000000000000000000000000000000004": { "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, + "0x0000000000000000000000000000000000000001": { + "builtin": { + "name": "ecrecover", + "pricing": { + "linear": { + "base": 3000, + "word": 0 + } + } + } + }, + "0x0000000000000000000000000000000000000002": { + "builtin": { + "name": "sha256", + "pricing": { + "linear": { + "base": 60, + "word": 12 + } + } + } + }, + "0x0000000000000000000000000000000000000003": { + "builtin": { + "name": "ripemd160", + "pricing": { + "linear": { + "base": 600, + "word": 120 + } + } + } + }, + "0x0000000000000000000000000000000000000004": { + "builtin": { + "name": "identity", + "pricing": { + "linear": { + "base": 15, + "word": 3 + } + } + } + }, + "0x0000000000000000000000000000000000000005": { + "builtin": { + "name": "modexp", + "activate_at": "0x7fffffffffffffff", + "pricing": { + "modexp": { + "divisor": 20 + } + } + } + }, + "0x0000000000000000000000000000000000000006": { + "builtin": { + "name": "alt_bn128_add", + "activate_at": "0x7fffffffffffffff", + "pricing": { + "linear": { + "base": 500, + "word": 0 + } + } + } + }, + "0x0000000000000000000000000000000000000007": { + "builtin": { + "name": "alt_bn128_mul", + "activate_at": "0x7fffffffffffffff", + "pricing": { + "linear": { + "base": 40000, + "word": 0 + } + } + } + }, + "0x0000000000000000000000000000000000000008": { + "builtin": { + "name": "alt_bn128_pairing", + "activate_at": "0x7fffffffffffffff", + "pricing": { + "alt_bn128_pairing": { + "base": 100000, + "pair": 80000 + } + } + } + }, "3282791d6fd713f1e94f4bfd565eaa78b3a0599d": { "balance": "1337000000000000000000" }, diff --git a/ethcore/res/ethereum/kotti.json b/ethcore/res/ethereum/kotti.json index cf531f6a5d9..690269625b0 100644 --- a/ethcore/res/ethereum/kotti.json +++ b/ethcore/res/ethereum/kotti.json @@ -12,12 +12,18 @@ "params": { "accountStartNonce": "0x0", "chainID": "0x6", + "eip140Transition": "0xaef49", "eip150Transition": "0x0", "eip155Transition": "0x0", "eip160Transition": "0x0", - "eip161abcTransition": "0x7fffffffffffffff", - "eip161dTransition": "0x7fffffffffffffff", + "eip161abcTransition": "0xaef49", + "eip161dTransition": "0xaef49", + "eip211Transition": "0xaef49", + "eip214Transition": "0xaef49", + "eip658Transition": "0xaef49", "gasLimitBoundDivisor": "0x400", + "maxCodeSize": "0x6000", + "maxCodeSizeTransition": "0xaef49", "maximumExtraDataSize": "0xffff", "minGasLimit": "0x1388", "networkID": "0x6" @@ -95,16 +101,55 @@ } }, "0x0000000000000000000000000000000000000005": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "modexp", + "activate_at": "0xaef49", + "pricing": { + "modexp": { + "divisor": 20 + } + } + } }, "0x0000000000000000000000000000000000000006": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "alt_bn128_add", + "activate_at": "0xaef49", + "pricing": { + "linear": { + "base": 500, + "word": 0 + } + } + } }, "0x0000000000000000000000000000000000000007": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "alt_bn128_mul", + "activate_at": "0xaef49", + "pricing": { + "linear": { + "base": 40000, + "word": 0 + } + } + } }, "0x0000000000000000000000000000000000000008": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "alt_bn128_pairing", + "activate_at": "0xaef49", + "pricing": { + "alt_bn128_pairing": { + "base": 100000, + "pair": 80000 + } + } + } }, "0x0000000000000000000000000000000000000009": { "balance": "0x1" diff --git a/ethcore/res/ethereum/morden.json b/ethcore/res/ethereum/morden.json index e626152adc9..5ea0f211432 100644 --- a/ethcore/res/ethereum/morden.json +++ b/ethcore/res/ethereum/morden.json @@ -8,11 +8,12 @@ "difficultyBoundDivisor": "0x0800", "durationLimit": "0x0d", "blockReward": "0x4563918244F40000", - "homesteadTransition": 494000, - "ecip1010PauseTransition": 1915000, - "ecip1010ContinueTransition": 3415000, - "ecip1017EraRounds": 2000000, - "bombDefuseTransition": 2300000 + "homesteadTransition": "0x789b0", + "ecip1010PauseTransition": "0x1d3878", + "ecip1010ContinueTransition": "0x341bd8", + "ecip1017EraRounds": "0x1e8480", + "eip100bTransition": "0x4829ba", + "bombDefuseTransition": "0x231860" } } }, @@ -26,11 +27,17 @@ "chainID": "0x3e", "forkBlock": "0x1b34d8", "forkCanonHash": "0xf376243aeff1f256d970714c3de9fd78fa4e63cf63e32a51fe1169e375d98145", - "eip150Transition": 1783000, - "eip160Transition": 1915000, - "eip161abcTransition": "0x7fffffffffffffff", - "eip161dTransition": "0x7fffffffffffffff", - "eip155Transition": 1915000 + "eip150Transition": "0x1b34d8", + "eip160Transition": "0x1d3878", + "eip161abcTransition": "0x4829ba", + "eip161dTransition": "0x4829ba", + "eip155Transition": "0x1d3878", + "maxCodeSize": "0x6000", + "maxCodeSizeTransition": "0x4829ba", + "eip140Transition": "0x4829ba", + "eip211Transition": "0x4829ba", + "eip214Transition": "0x4829ba", + "eip658Transition": "0x4829ba" }, "genesis": { "seal": { @@ -68,6 +75,53 @@ "0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, "0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, "0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, + "0x0000000000000000000000000000000000000005": { + "builtin": { + "name": "modexp", + "activate_at": "0x4829ba", + "pricing": { + "modexp": { + "divisor": 20 + } + } + } + }, + "0x0000000000000000000000000000000000000006": { + "builtin": { + "name": "alt_bn128_add", + "activate_at": "0x4829ba", + "pricing": { + "linear": { + "base": 500, + "word": 0 + } + } + } + }, + "0x0000000000000000000000000000000000000007": { + "builtin": { + "name": "alt_bn128_mul", + "activate_at": "0x4829ba", + "pricing": { + "linear": { + "base": 40000, + "word": 0 + } + } + } + }, + "0x0000000000000000000000000000000000000008": { + "builtin": { + "name": "alt_bn128_pairing", + "activate_at": "0x4829ba", + "pricing": { + "alt_bn128_pairing": { + "base": 100000, + "pair": 80000 + } + } + } + }, "102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" } }, "hardcodedSync": { diff --git a/ethcore/service/Cargo.toml b/ethcore/service/Cargo.toml index 218d4f475d5..be9c3d45377 100644 --- a/ethcore/service/Cargo.toml +++ b/ethcore/service/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Parity Technologies "] [dependencies] ansi_term = "0.10" -error-chain = { version = "0.12", default-features = false } +derive_more = "0.14.0" ethcore = { path = ".." } ethcore-blockchain = { path = "../blockchain" } ethcore-io = { path = "../../util/io" } diff --git a/ethcore/service/src/error.rs b/ethcore/service/src/error.rs index c73cb0dfc16..b2787e97861 100644 --- a/ethcore/service/src/error.rs +++ b/ethcore/service/src/error.rs @@ -14,18 +14,26 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -// Silence: `use of deprecated item 'std::error::Error::cause': replaced by Error::source, which can support downcasting` -// https://github.com/paritytech/parity-ethereum/issues/10302 -#![allow(deprecated)] - use ethcore; use io; use ethcore_private_tx; -error_chain! { - foreign_links { - Ethcore(ethcore::error::Error); - IoError(io::IoError); - PrivateTransactions(ethcore_private_tx::Error); +#[derive(Debug, derive_more::Display, derive_more::From)] +pub enum Error { + /// Ethcore Error + Ethcore(ethcore::error::Error), + /// Io Error + IoError(io::IoError), + /// Private Transactions Error + PrivateTransactions(ethcore_private_tx::Error), +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(std::error::Error + 'static)> { + match self { + Error::Ethcore(err) => Some(err), + Error::IoError(err) => Some(err), + Error::PrivateTransactions(err) => Some(err), + } } } diff --git a/ethcore/service/src/lib.rs b/ethcore/service/src/lib.rs index 7828fff8b44..67292e1d8da 100644 --- a/ethcore/service/src/lib.rs +++ b/ethcore/service/src/lib.rs @@ -23,8 +23,7 @@ extern crate ethcore_sync as sync; extern crate ethereum_types; extern crate kvdb; -#[macro_use] -extern crate error_chain; +extern crate derive_more; #[macro_use] extern crate log; #[macro_use] @@ -42,5 +41,5 @@ mod stop_guard; #[cfg(test)] extern crate kvdb_rocksdb; -pub use error::{Error, ErrorKind}; +pub use error::Error; pub use service::{ClientService, PrivateTxService}; diff --git a/ethcore/service/src/service.rs b/ethcore/service/src/service.rs index c16a071892b..bdc743581ad 100644 --- a/ethcore/service/src/service.rs +++ b/ethcore/service/src/service.rs @@ -30,8 +30,10 @@ use blockchain::{BlockChainDB, BlockChainDBHandler}; use ethcore::client::{Client, ClientConfig, ChainNotify, ClientIoMessage}; use ethcore::miner::Miner; use ethcore::snapshot::service::{Service as SnapshotService, ServiceParams as SnapServiceParams}; -use ethcore::snapshot::{SnapshotService as _SnapshotService, RestorationStatus}; +use ethcore::snapshot::{SnapshotService as _SnapshotService, RestorationStatus, Error as SnapshotError}; use ethcore::spec::Spec; +use ethcore::error::Error as EthcoreError; + use ethcore_private_tx::{self, Importer, Signer}; use Error; @@ -59,7 +61,7 @@ impl PrivateTxHandler for PrivateTxService { Ok(import_result) => Ok(import_result), Err(err) => { warn!(target: "privatetx", "Unable to import private transaction packet: {}", err); - bail!(err.to_string()) + return Err(err.to_string()) } } } @@ -69,7 +71,7 @@ impl PrivateTxHandler for PrivateTxService { Ok(import_result) => Ok(import_result), Err(err) => { warn!(target: "privatetx", "Unable to import signed private transaction packet: {}", err); - bail!(err.to_string()) + return Err(err.to_string()) } } } @@ -197,6 +199,7 @@ impl ClientService { /// Shutdown the Client Service pub fn shutdown(&self) { + trace!(target: "shutdown", "Shutting down Client Service"); self.snapshot.shutdown(); } } @@ -257,7 +260,11 @@ impl IoHandler for ClientIoHandler { let res = thread::Builder::new().name("Periodic Snapshot".into()).spawn(move || { if let Err(e) = snapshot.take_snapshot(&*client, num) { - warn!("Failed to take snapshot at block #{}: {}", num, e); + match e { + EthcoreError::Snapshot(SnapshotError::SnapshotAborted) => info!("Snapshot aborted"), + _ => warn!("Failed to take snapshot at block #{}: {}", num, e), + } + } }); diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index fcc6f74c6cd..bf1380908f8 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -57,7 +57,7 @@ impl Default for Factory { impl Factory { /// Create a read-only accountdb. /// This will panic when write operations are called. - pub fn readonly<'db>(&self, db: &'db HashDB, address_hash: H256) -> Box + 'db> { + pub fn readonly<'db>(&self, db: &'db dyn HashDB, address_hash: H256) -> Box + 'db> { match *self { Factory::Mangled => Box::new(AccountDB::from_hash(db, address_hash)), Factory::Plain => Box::new(Wrapping(db)), @@ -65,7 +65,7 @@ impl Factory { } /// Create a new mutable hashdb. - pub fn create<'db>(&self, db: &'db mut HashDB, address_hash: H256) -> Box + 'db> { + pub fn create<'db>(&self, db: &'db mut dyn HashDB, address_hash: H256) -> Box + 'db> { match *self { Factory::Mangled => Box::new(AccountDBMut::from_hash(db, address_hash)), Factory::Plain => Box::new(WrappingMut(db)), @@ -77,19 +77,19 @@ impl Factory { /// DB backend wrapper for Account trie /// Transforms trie node keys for the database pub struct AccountDB<'db> { - db: &'db HashDB, + db: &'db dyn HashDB, address_hash: H256, } impl<'db> AccountDB<'db> { /// Create a new AccountDB from an address. #[cfg(test)] - pub fn new(db: &'db HashDB, address: &Address) -> Self { + pub fn new(db: &'db dyn HashDB, address: &Address) -> Self { Self::from_hash(db, keccak(address)) } /// Create a new AcountDB from an address' hash. - pub fn from_hash(db: &'db HashDB, address_hash: H256) -> Self { + pub fn from_hash(db: &'db dyn HashDB, address_hash: H256) -> Self { AccountDB { db: db, address_hash: address_hash, @@ -98,8 +98,8 @@ impl<'db> AccountDB<'db> { } impl<'db> AsHashDB for AccountDB<'db> { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } } impl<'db> HashDB for AccountDB<'db> { @@ -132,19 +132,19 @@ impl<'db> HashDB for AccountDB<'db> { /// DB backend wrapper for Account trie pub struct AccountDBMut<'db> { - db: &'db mut HashDB, + db: &'db mut dyn HashDB, address_hash: H256, } impl<'db> AccountDBMut<'db> { /// Create a new AccountDB from an address. #[cfg(test)] - pub fn new(db: &'db mut HashDB, address: &Address) -> Self { + pub fn new(db: &'db mut dyn HashDB, address: &Address) -> Self { Self::from_hash(db, keccak(address)) } /// Create a new AcountDB from an address' hash. - pub fn from_hash(db: &'db mut HashDB, address_hash: H256) -> Self { + pub fn from_hash(db: &'db mut dyn HashDB, address_hash: H256) -> Self { AccountDBMut { db: db, address_hash: address_hash, @@ -200,15 +200,15 @@ impl<'db> HashDB for AccountDBMut<'db>{ } impl<'db> AsHashDB for AccountDBMut<'db> { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } } -struct Wrapping<'db>(&'db HashDB); +struct Wrapping<'db>(&'db dyn HashDB); impl<'db> AsHashDB for Wrapping<'db> { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } } impl<'db> HashDB for Wrapping<'db> { @@ -239,10 +239,10 @@ impl<'db> HashDB for Wrapping<'db> { } } -struct WrappingMut<'db>(&'db mut HashDB); +struct WrappingMut<'db>(&'db mut dyn HashDB); impl<'db> AsHashDB for WrappingMut<'db> { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } } impl<'db> HashDB for WrappingMut<'db>{ diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index fcecc53ab88..4a75034f025 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -61,7 +61,7 @@ use types::receipt::{Receipt, TransactionOutcome}; /// maintain the system `state()`. We also archive execution receipts in preparation for later block creation. pub struct OpenBlock<'x> { block: ExecutedBlock, - engine: &'x EthEngine, + engine: &'x dyn EthEngine, } /// Just like `OpenBlock`, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields, @@ -163,7 +163,7 @@ pub trait Drain { impl<'x> OpenBlock<'x> { /// Create a new `OpenBlock` ready for transaction pushing. pub fn new<'a, I: IntoIterator>( - engine: &'x EthEngine, + engine: &'x dyn EthEngine, factories: Factories, tracing: bool, db: StateDB, @@ -374,7 +374,7 @@ impl ClosedBlock { } /// Given an engine reference, reopen the `ClosedBlock` into an `OpenBlock`. - pub fn reopen(self, engine: &EthEngine) -> OpenBlock { + pub fn reopen(self, engine: &dyn EthEngine) -> OpenBlock { // revert rewards (i.e. set state back at last transaction's state). let mut block = self.block; block.state = self.unclosed_state; @@ -404,7 +404,7 @@ impl LockedBlock { /// Provide a valid seal in order to turn this into a `SealedBlock`. /// /// NOTE: This does not check the validity of `seal` with the engine. - pub fn seal(self, engine: &EthEngine, seal: Vec) -> Result { + pub fn seal(self, engine: &dyn EthEngine, seal: Vec) -> Result { let expected_seal_fields = engine.seal_fields(&self.header); let mut s = self; if seal.len() != expected_seal_fields { @@ -429,7 +429,7 @@ impl LockedBlock { /// TODO(https://github.com/paritytech/parity-ethereum/issues/10407): This is currently only used in POW chain call paths, we should really merge it with seal() above. pub fn try_seal( self, - engine: &EthEngine, + engine: &dyn EthEngine, seal: Vec, ) -> Result { let mut s = self; @@ -472,14 +472,14 @@ pub(crate) fn enact( header: Header, transactions: Vec, uncles: Vec
, - engine: &EthEngine, + engine: &dyn EthEngine, tracing: bool, db: StateDB, parent: &Header, last_hashes: Arc, factories: Factories, is_epoch_begin: bool, - ancestry: &mut Iterator, + ancestry: &mut dyn Iterator, ) -> Result { // For trace log let trace_state = if log_enabled!(target: "enact", ::log::Level::Trace) { @@ -525,14 +525,14 @@ pub(crate) fn enact( /// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header pub fn enact_verified( block: PreverifiedBlock, - engine: &EthEngine, + engine: &dyn EthEngine, tracing: bool, db: StateDB, parent: &Header, last_hashes: Arc, factories: Factories, is_epoch_begin: bool, - ancestry: &mut Iterator, + ancestry: &mut dyn Iterator, ) -> Result { enact( @@ -570,7 +570,7 @@ mod tests { /// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header fn enact_bytes( block_bytes: Vec, - engine: &EthEngine, + engine: &dyn EthEngine, tracing: bool, db: StateDB, parent: &Header, @@ -623,7 +623,7 @@ mod tests { /// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header. Seal the block aferwards fn enact_and_seal( block_bytes: Vec, - engine: &EthEngine, + engine: &dyn EthEngine, tracing: bool, db: StateDB, parent: &Header, diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index b9d79fc3d2b..76d00ba5269 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -157,8 +157,8 @@ impl ModexpPricer { /// /// Unless `is_active` is true, pub struct Builtin { - pricer: Box, - native: Box, + pricer: Box, + native: Box, activate_at: u64, } @@ -177,7 +177,7 @@ impl Builtin { impl From for Builtin { fn from(b: ethjson::spec::Builtin) -> Self { - let pricer: Box = match b.pricing { + let pricer: Box = match b.pricing { ethjson::spec::Pricing::Linear(linear) => { Box::new(Linear { base: linear.base, @@ -211,16 +211,16 @@ impl From for Builtin { } /// Ethereum built-in factory. -pub fn ethereum_builtin(name: &str) -> Box { +pub fn ethereum_builtin(name: &str) -> Box { match name { - "identity" => Box::new(Identity) as Box, - "ecrecover" => Box::new(EcRecover) as Box, - "sha256" => Box::new(Sha256) as Box, - "ripemd160" => Box::new(Ripemd160) as Box, - "modexp" => Box::new(ModexpImpl) as Box, - "alt_bn128_add" => Box::new(Bn128AddImpl) as Box, - "alt_bn128_mul" => Box::new(Bn128MulImpl) as Box, - "alt_bn128_pairing" => Box::new(Bn128PairingImpl) as Box, + "identity" => Box::new(Identity) as Box, + "ecrecover" => Box::new(EcRecover) as Box, + "sha256" => Box::new(Sha256) as Box, + "ripemd160" => Box::new(Ripemd160) as Box, + "modexp" => Box::new(ModexpImpl) as Box, + "alt_bn128_add" => Box::new(Bn128AddImpl) as Box, + "alt_bn128_mul" => Box::new(Bn128MulImpl) as Box, + "alt_bn128_pairing" => Box::new(Bn128PairingImpl) as Box, _ => panic!("invalid builtin name: {}", name), } } @@ -1008,7 +1008,7 @@ mod tests { fn is_active() { let pricer = Box::new(Linear { base: 10, word: 20} ); let b = Builtin { - pricer: pricer as Box, + pricer: pricer as Box, native: ethereum_builtin("identity"), activate_at: 100_000, }; @@ -1022,7 +1022,7 @@ mod tests { fn from_named_linear() { let pricer = Box::new(Linear { base: 10, word: 20 }); let b = Builtin { - pricer: pricer as Box, + pricer: pricer as Box, native: ethereum_builtin("identity"), activate_at: 1, }; diff --git a/ethcore/src/client/ancient_import.rs b/ethcore/src/client/ancient_import.rs index 2a0a970cd69..46cd519a7aa 100644 --- a/ethcore/src/client/ancient_import.rs +++ b/ethcore/src/client/ancient_import.rs @@ -32,13 +32,13 @@ const HEAVY_VERIFY_RATE: f32 = 0.02; /// Ancient block verifier: import an ancient sequence of blocks in order from a starting /// epoch. pub struct AncientVerifier { - cur_verifier: RwLock>>>, - engine: Arc, + cur_verifier: RwLock>>>, + engine: Arc, } impl AncientVerifier { /// Create a new ancient block verifier with the given engine. - pub fn new(engine: Arc) -> Self { + pub fn new(engine: Arc) -> Self { AncientVerifier { cur_verifier: RwLock::new(None), engine, @@ -87,7 +87,7 @@ impl AncientVerifier { } fn initial_verifier(&self, header: &Header, chain: &BlockChain) - -> Result>, ::error::Error> + -> Result>, ::error::Error> { trace!(target: "client", "Initializing ancient block restoration."); let current_epoch_data = chain.epoch_transitions() diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index d22cff0942d..75227af5aa4 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -153,7 +153,7 @@ struct Importer { pub import_lock: Mutex<()>, // FIXME Maybe wrap the whole `Importer` instead? /// Used to verify blocks - pub verifier: Box>, + pub verifier: Box>, /// Queue containing pending blocks pub block_queue: BlockQueue, @@ -165,7 +165,7 @@ struct Importer { pub ancient_verifier: AncientVerifier, /// Ethereum engine to be used during import - pub engine: Arc, + pub engine: Arc, /// A lru cache of recently detected bad blocks pub bad_blocks: bad_blocks::BadBlocks, @@ -187,7 +187,7 @@ pub struct Client { chain: RwLock>, tracedb: RwLock>, - engine: Arc, + engine: Arc, /// Client configuration config: ClientConfig, @@ -196,7 +196,7 @@ pub struct Client { pruning: journaldb::Algorithm, /// Client uses this to store blocks, traces, etc. - db: RwLock>, + db: RwLock>, state_db: RwLock, @@ -210,7 +210,7 @@ pub struct Client { io_channel: RwLock>, /// List of actors to be notified on certain chain events - notify: RwLock>>, + notify: RwLock>>, /// Queued transactions from IO queue_transactions: IoChannelQueue, @@ -232,12 +232,12 @@ pub struct Client { history: u64, /// An action to be done if a mode/spec_name change happens - on_user_defaults_change: Mutex) + 'static + Send>>>, + on_user_defaults_change: Mutex) + 'static + Send>>>, registrar_address: Option
, /// A closure to call when we want to restart the client - exit_handler: Mutex>>, + exit_handler: Mutex>>, importer: Importer, } @@ -245,7 +245,7 @@ pub struct Client { impl Importer { pub fn new( config: &ClientConfig, - engine: Arc, + engine: Arc, message_channel: IoChannel, miner: Arc, ) -> Result { @@ -449,7 +449,7 @@ impl Importer { /// /// The block is guaranteed to be the next best blocks in the /// first block sequence. Does no sealing or transaction validation. - fn import_old_block(&self, unverified: Unverified, receipts_bytes: &[u8], db: &KeyValueDB, chain: &BlockChain) -> EthcoreResult<()> { + fn import_old_block(&self, unverified: Unverified, receipts_bytes: &[u8], db: &dyn KeyValueDB, chain: &BlockChain) -> EthcoreResult<()> { let receipts = ::rlp::decode_list(receipts_bytes); let _import_lock = self.import_lock.lock(); @@ -702,7 +702,7 @@ impl Client { pub fn new( config: ClientConfig, spec: &Spec, - db: Arc, + db: Arc, miner: Arc, message_channel: IoChannel, ) -> Result, ::error::Error> { @@ -764,8 +764,8 @@ impl Client { liveness: AtomicBool::new(awake), mode: Mutex::new(config.mode.clone()), chain: RwLock::new(chain), - tracedb: tracedb, - engine: engine, + tracedb, + engine, pruning: config.pruning.clone(), db: RwLock::new(db.clone()), state_db: RwLock::new(state_db), @@ -778,8 +778,8 @@ impl Client { ancient_blocks_import_lock: Default::default(), queue_consensus_message: IoChannelQueue::new(usize::max_value()), last_hashes: RwLock::new(VecDeque::new()), - factories: factories, - history: history, + factories, + history, on_user_defaults_change: Mutex::new(None), registrar_address, exit_handler: Mutex::new(None), @@ -844,7 +844,7 @@ impl Client { } /// Adds an actor to be notified on certain events - pub fn add_notify(&self, target: Arc) { + pub fn add_notify(&self, target: Arc) { self.notify.write().push(Arc::downgrade(&target)); } @@ -857,11 +857,11 @@ impl Client { } /// Returns engine reference. - pub fn engine(&self) -> &EthEngine { + pub fn engine(&self) -> &dyn EthEngine { &*self.engine } - fn notify(&self, f: F) where F: Fn(&ChainNotify) { + fn notify(&self, f: F) where F: Fn(&dyn ChainNotify) { for np in &*self.notify.read() { if let Some(n) = np.upgrade() { f(&*n); @@ -1071,7 +1071,7 @@ impl Client { } /// Get a copy of the best block's state. - pub fn state(&self) -> Box { + pub fn state(&self) -> Box { Box::new(self.latest_state()) as Box<_> } @@ -1138,7 +1138,12 @@ impl Client { /// Take a snapshot at the given block. /// If the ID given is "latest", this will default to 1000 blocks behind. - pub fn take_snapshot(&self, writer: W, at: BlockId, p: &snapshot::Progress) -> Result<(), EthcoreError> { + pub fn take_snapshot( + &self, + writer: W, + at: BlockId, + p: &snapshot::Progress, + ) -> Result<(), EthcoreError> { let db = self.state_db.read().journal_db().boxed_clone(); let best_block_number = self.chain_info().best_block_number; let block_number = self.block_number(at).ok_or_else(|| snapshot::Error::InvalidStartingBlock(at))?; @@ -1168,8 +1173,16 @@ impl Client { }; let processing_threads = self.config.snapshot.processing_threads; - snapshot::take_snapshot(&*self.engine, &self.chain.read(), start_hash, db.as_hash_db(), writer, p, processing_threads)?; - + let chunker = self.engine.snapshot_components().ok_or(snapshot::Error::SnapshotsUnsupported)?; + snapshot::take_snapshot( + chunker, + &self.chain.read(), + start_hash, + db.as_hash_db(), + writer, + p, + processing_threads, + )?; Ok(()) } @@ -1648,7 +1661,7 @@ impl Call for Client { } impl EngineInfo for Client { - fn engine(&self) -> &EthEngine { + fn engine(&self) -> &dyn EthEngine { Client::engine(self) } } @@ -1668,7 +1681,7 @@ impl BlockChainClient for Client { Ok(self.replay_block_transactions(block, analytics)?.nth(address.index).expect(PROOF).1) } - fn replay_block_transactions(&self, block: BlockId, analytics: CallAnalytics) -> Result>, CallError> { + fn replay_block_transactions(&self, block: BlockId, analytics: CallAnalytics) -> Result>, CallError> { let mut env_info = self.env_info(block).ok_or(CallError::StatePruned)?; let body = self.block_body(block).ok_or(CallError::StatePruned)?; let mut state = self.state_at_beginning(block).ok_or(CallError::StatePruned)?; @@ -2495,7 +2508,7 @@ impl super::traits::EngineClient for Client { self.chain.read().epoch_transition_for(parent_hash) } - fn as_full_client(&self) -> Option<&BlockChainClient> { Some(self) } + fn as_full_client(&self) -> Option<&dyn BlockChainClient> { Some(self) } fn block_number(&self, id: BlockId) -> Option { BlockChainClient::block_number(self, id) diff --git a/ethcore/src/client/io_message.rs b/ethcore/src/client/io_message.rs index 92e2d3e258e..5fcf8f9ff8d 100644 --- a/ethcore/src/client/io_message.rs +++ b/ethcore/src/client/io_message.rs @@ -47,7 +47,7 @@ impl ClientIoMessage { } /// A function to invoke in the client thread. -pub struct Callback(pub Box); +pub struct Callback(pub Box); impl fmt::Debug for Callback { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index c9c81ecb858..1b28ca2cdbd 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -627,7 +627,7 @@ impl StateClient for TestBlockChainClient { } impl EngineInfo for TestBlockChainClient { - fn engine(&self) -> &EthEngine { + fn engine(&self) -> &dyn EthEngine { unimplemented!() } } @@ -661,7 +661,7 @@ impl BlockChainClient for TestBlockChainClient { } } - fn replay_block_transactions(&self, _block: BlockId, _analytics: CallAnalytics) -> Result>, CallError> { + fn replay_block_transactions(&self, _block: BlockId, _analytics: CallAnalytics) -> Result>, CallError> { Ok(Box::new( self.traces .read() @@ -955,7 +955,7 @@ impl super::traits::EngineClient for TestBlockChainClient { None } - fn as_full_client(&self) -> Option<&BlockChainClient> { Some(self) } + fn as_full_client(&self) -> Option<&dyn BlockChainClient> { Some(self) } fn block_number(&self, id: BlockId) -> Option { BlockChainClient::block_number(self, id) diff --git a/ethcore/src/client/trace.rs b/ethcore/src/client/trace.rs index 73563a1d0f6..7be957b33ec 100644 --- a/ethcore/src/client/trace.rs +++ b/ethcore/src/client/trace.rs @@ -23,11 +23,11 @@ use types::BlockNumber; impl TraceDatabaseExtras for BlockChain { fn block_hash(&self, block_number: BlockNumber) -> Option { - (self as &BlockProvider).block_hash(block_number) + (self as &dyn BlockProvider).block_hash(block_number) } fn transaction_hash(&self, block_number: BlockNumber, tx_position: usize) -> Option { - (self as &BlockProvider).block_hash(block_number) + (self as &dyn BlockProvider).block_hash(block_number) .and_then(|block_hash| { let tx_address = TransactionAddress { block_hash: block_hash, diff --git a/ethcore/src/client/traits.rs b/ethcore/src/client/traits.rs index bcfa7417c4b..9d0b27fc709 100644 --- a/ethcore/src/client/traits.rs +++ b/ethcore/src/client/traits.rs @@ -55,7 +55,7 @@ use verification::queue::kind::blocks::Unverified; /// State information to be used during client query pub enum StateOrBlock { /// State to be used, may be pending - State(Box), + State(Box), /// Id of an existing block from a chain to get state from Block(BlockId) @@ -67,8 +67,8 @@ impl From for StateOrBlock { } } -impl From> for StateOrBlock { - fn from(info: Box) -> StateOrBlock { +impl From> for StateOrBlock { + fn from(info: Box) -> StateOrBlock { StateOrBlock::State(info) } } @@ -184,7 +184,7 @@ pub trait Call { /// Provides `engine` method pub trait EngineInfo { /// Get underlying engine object - fn engine(&self) -> &EthEngine; + fn engine(&self) -> &dyn EthEngine; } /// IO operations that should off-load heavy work to another thread. @@ -306,7 +306,7 @@ pub trait BlockChainClient : Sync + Send + AccountData + BlockChain + CallContra fn replay(&self, t: TransactionId, analytics: CallAnalytics) -> Result; /// Replays all the transactions in a given block for inspection. - fn replay_block_transactions(&self, block: BlockId, analytics: CallAnalytics) -> Result>, CallError>; + fn replay_block_transactions(&self, block: BlockId, analytics: CallAnalytics) -> Result>, CallError>; /// Returns traces matching given filter. fn filter_traces(&self, filter: TraceFilter) -> Option>; @@ -441,7 +441,7 @@ pub trait EngineClient: Sync + Send + ChainInfo { fn epoch_transition_for(&self, parent_hash: H256) -> Option<::engines::EpochTransition>; /// Attempt to cast the engine client to a full client. - fn as_full_client(&self) -> Option<&BlockChainClient>; + fn as_full_client(&self) -> Option<&dyn BlockChainClient>; /// Get a block number by ID. fn block_number(&self, id: BlockId) -> Option; diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 8bf838d2dc5..6f2723bba71 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -63,7 +63,7 @@ pub struct AuthorityRoundParams { /// Starting step, pub start_step: Option, /// Valid validators. - pub validators: Box, + pub validators: Box, /// Chain score validation transition block. pub validate_score_transition: u64, /// Monotonic step validation transition block. @@ -222,9 +222,9 @@ impl EpochManager { // Zooms to the epoch after the header with the given hash. Returns true if succeeded, false otherwise. fn zoom_to_after( &mut self, - client: &EngineClient, + client: &dyn EngineClient, machine: &EthereumMachine, - validators: &ValidatorSet, + validators: &dyn ValidatorSet, hash: H256 ) -> bool { let last_was_parent = self.finality_checker.subchain_head() == Some(hash); @@ -324,7 +324,7 @@ impl EmptyStep { EmptyStep { signature, step, parent_hash } } - fn verify(&self, validators: &ValidatorSet) -> Result { + fn verify(&self, validators: &dyn ValidatorSet) -> Result { let message = keccak(empty_step_rlp(self.step, &self.parent_hash)); let correct_proposer = step_proposer(validators, &self.parent_hash, self.step); @@ -419,9 +419,9 @@ struct PermissionedStep { pub struct AuthorityRound { transition_service: IoService<()>, step: Arc, - client: Arc>>>, - signer: RwLock>>, - validators: Box, + client: Arc>>>, + signer: RwLock>>, + validators: Box, validate_score_transition: u64, validate_step_transition: u64, empty_steps: Mutex>, @@ -563,13 +563,13 @@ fn header_empty_steps_signers(header: &Header, empty_steps_transition: u64) -> R } } -fn step_proposer(validators: &ValidatorSet, bh: &H256, step: u64) -> Address { +fn step_proposer(validators: &dyn ValidatorSet, bh: &H256, step: u64) -> Address { let proposer = validators.get(bh, step as usize); trace!(target: "engine", "Fetched proposer for step {}: {}", step, proposer); proposer } -fn is_step_proposer(validators: &ValidatorSet, bh: &H256, step: u64, address: &Address) -> bool { +fn is_step_proposer(validators: &dyn ValidatorSet, bh: &H256, step: u64, address: &Address) -> bool { step_proposer(validators, bh, step) == *address } @@ -597,7 +597,7 @@ fn verify_timestamp(step: &Step, header_step: u64) -> Result<(), BlockError> { } } -fn verify_external(header: &Header, validators: &ValidatorSet, empty_steps_transition: u64) -> Result<(), Error> { +fn verify_external(header: &Header, validators: &dyn ValidatorSet, empty_steps_transition: u64) -> Result<(), Error> { let header_step = header_step(header, empty_steps_transition)?; let proposer_signature = header_signature(header, empty_steps_transition)?; @@ -716,7 +716,7 @@ impl AuthorityRound { // fetch correct validator set for epoch at header, taking into account // finality of previous transitions. - fn epoch_set<'a>(&'a self, header: &Header) -> Result<(CowLike, BlockNumber), Error> { + fn epoch_set<'a>(&'a self, header: &Header) -> Result<(CowLike, BlockNumber), Error> { Ok(if self.immediate_transitions { (CowLike::Borrowed(&*self.validators), header.number()) } else { @@ -802,7 +802,7 @@ impl AuthorityRound { } } - fn report_skipped(&self, header: &Header, current_step: u64, parent_step: u64, validators: &ValidatorSet, set_number: u64) { + fn report_skipped(&self, header: &Header, current_step: u64, parent_step: u64, validators: &dyn ValidatorSet, set_number: u64) { // we're building on top of the genesis block so don't report any skipped steps if header.number() == 1 { return; @@ -825,7 +825,7 @@ impl AuthorityRound { } // Returns the hashes of all ancestor blocks that are finalized by the given `chain_head`. - fn build_finality(&self, chain_head: &Header, ancestry: &mut Iterator) -> Vec { + fn build_finality(&self, chain_head: &Header, ancestry: &mut dyn Iterator) -> Vec { if self.immediate_transitions { return Vec::new() } let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) { @@ -894,7 +894,7 @@ fn unix_now() -> Duration { struct TransitionHandler { step: Arc, - client: Arc>>>, + client: Arc>>>, } const ENGINE_TIMEOUT_TOKEN: TimerToken = 23; @@ -1198,7 +1198,7 @@ impl Engine for AuthorityRound { &self, block: &mut ExecutedBlock, epoch_begin: bool, - _ancestry: &mut Iterator, + _ancestry: &mut dyn Iterator, ) -> Result<(), Error> { // with immediate transitions, we don't use the epoch mechanism anyway. // the genesis is always considered an epoch, but we ignore it intentionally. @@ -1555,12 +1555,12 @@ impl Engine for AuthorityRound { } } - fn register_client(&self, client: Weak) { + fn register_client(&self, client: Weak) { *self.client.write() = Some(client.clone()); self.validators.register_client(client); } - fn set_signer(&self, signer: Box) { + fn set_signer(&self, signer: Box) { *self.signer.write() = Some(signer); } @@ -1572,7 +1572,7 @@ impl Engine for AuthorityRound { ) } - fn snapshot_components(&self) -> Option> { + fn snapshot_components(&self) -> Option> { if self.immediate_transitions { None } else { @@ -1584,7 +1584,7 @@ impl Engine for AuthorityRound { super::total_difficulty_fork_choice(new, current) } - fn ancestry_actions(&self, header: &Header, ancestry: &mut Iterator) -> Vec { + fn ancestry_actions(&self, header: &Header, ancestry: &mut dyn Iterator) -> Vec { let finalized = self.build_finality( header, &mut ancestry.take_while(|e| !e.is_finalized).map(|e| e.header), @@ -1908,14 +1908,14 @@ mod tests { (spec, tap, accounts) } - fn empty_step(engine: &EthEngine, step: u64, parent_hash: &H256) -> EmptyStep { + fn empty_step(engine: &dyn EthEngine, step: u64, parent_hash: &H256) -> EmptyStep { let empty_step_rlp = super::empty_step_rlp(step, parent_hash); let signature = engine.sign(keccak(&empty_step_rlp)).unwrap().into(); let parent_hash = parent_hash.clone(); EmptyStep { step, signature, parent_hash } } - fn sealed_empty_step(engine: &EthEngine, step: u64, parent_hash: &H256) -> SealedEmptyStep { + fn sealed_empty_step(engine: &dyn EthEngine, step: u64, parent_hash: &H256) -> SealedEmptyStep { let empty_step_rlp = super::empty_step_rlp(step, parent_hash); let signature = engine.sign(keccak(&empty_step_rlp)).unwrap().into(); SealedEmptyStep { signature, step } diff --git a/ethcore/src/engines/basic_authority.rs b/ethcore/src/engines/basic_authority.rs index 21652f0a12c..098c3f558a1 100644 --- a/ethcore/src/engines/basic_authority.rs +++ b/ethcore/src/engines/basic_authority.rs @@ -55,7 +55,7 @@ impl super::EpochVerifier for EpochVerifier { } } -fn verify_external(header: &Header, validators: &ValidatorSet) -> Result<(), Error> { +fn verify_external(header: &Header, validators: &dyn ValidatorSet) -> Result<(), Error> { use rlp::Rlp; // Check if the signature belongs to a validator, can depend on parent state. @@ -75,8 +75,8 @@ fn verify_external(header: &Header, validators: &ValidatorSet) -> Result<(), Err /// Engine using `BasicAuthority`, trivial proof-of-authority consensus. pub struct BasicAuthority { machine: EthereumMachine, - signer: RwLock>>, - validators: Box, + signer: RwLock>>, + validators: Box, } impl BasicAuthority { @@ -189,11 +189,11 @@ impl Engine for BasicAuthority { } } - fn register_client(&self, client: Weak) { + fn register_client(&self, client: Weak) { self.validators.register_client(client); } - fn set_signer(&self, signer: Box) { + fn set_signer(&self, signer: Box) { *self.signer.write() = Some(signer); } @@ -205,7 +205,7 @@ impl Engine for BasicAuthority { ) } - fn snapshot_components(&self) -> Option> { + fn snapshot_components(&self) -> Option> { None } diff --git a/ethcore/src/engines/clique/mod.rs b/ethcore/src/engines/clique/mod.rs index 5884ab05d3a..052f0a578ce 100644 --- a/ethcore/src/engines/clique/mod.rs +++ b/ethcore/src/engines/clique/mod.rs @@ -162,10 +162,10 @@ pub struct Clique { epoch_length: u64, period: u64, machine: EthereumMachine, - client: RwLock>>, + client: RwLock>>, block_state_by_hash: RwLock>, proposals: RwLock>, - signer: RwLock>>, + signer: RwLock>>, } #[cfg(test)] @@ -174,10 +174,10 @@ pub struct Clique { pub epoch_length: u64, pub period: u64, pub machine: EthereumMachine, - pub client: RwLock>>, + pub client: RwLock>>, pub block_state_by_hash: RwLock>, pub proposals: RwLock>, - pub signer: RwLock>>, + pub signer: RwLock>>, } impl Clique { @@ -370,7 +370,7 @@ impl Engine for Clique { &self, _block: &mut ExecutedBlock, _epoch_begin: bool, - _ancestry: &mut Iterator, + _ancestry: &mut dyn Iterator, ) -> Result<(), Error> { Ok(()) } @@ -736,12 +736,12 @@ impl Engine for Clique { } } - fn set_signer(&self, signer: Box) { + fn set_signer(&self, signer: Box) { trace!(target: "engine", "set_signer: {}", signer.address()); *self.signer.write() = Some(signer); } - fn register_client(&self, client: Weak) { + fn register_client(&self, client: Weak) { *self.client.write() = Some(client.clone()); } diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index d51e31ea17a..e0db2181e9d 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -175,10 +175,10 @@ pub enum SealingState { } /// A system-calling closure. Enacts calls on a block's state from the system address. -pub type SystemCall<'a> = FnMut(Address, Vec) -> Result, String> + 'a; +pub type SystemCall<'a> = dyn FnMut(Address, Vec) -> Result, String> + 'a; /// A system-calling closure. Enacts calls on a block's state with code either from an on-chain contract, or hard-coded EVM or WASM (if enabled on-chain) codes. -pub type SystemOrCodeCall<'a> = FnMut(SystemOrCodeCallKind, Vec) -> Result, String> + 'a; +pub type SystemOrCodeCall<'a> = dyn FnMut(SystemOrCodeCallKind, Vec) -> Result, String> + 'a; /// Kind of SystemOrCodeCall, this is either an on-chain address, or code. #[derive(PartialEq, Debug, Clone)] @@ -220,10 +220,10 @@ pub fn default_system_or_code_call<'a>(machine: &'a ::machine::EthereumMachine, } /// Type alias for a function we can get headers by hash through. -pub type Headers<'a, H> = Fn(H256) -> Option + 'a; +pub type Headers<'a, H> = dyn Fn(H256) -> Option + 'a; /// Type alias for a function we can query pending transitions by block hash through. -pub type PendingTransitionStore<'a> = Fn(H256) -> Option + 'a; +pub type PendingTransitionStore<'a> = dyn Fn(H256) -> Option + 'a; /// Proof dependent on state. pub trait StateDependentProof: Send + Sync { @@ -240,16 +240,16 @@ pub enum Proof { /// Known proof (extracted from signal) Known(Vec), /// State dependent proof. - WithState(Arc>), + WithState(Arc>), } /// Generated epoch verifier. pub enum ConstructedVerifier<'a, M: Machine> { /// Fully trusted verifier. - Trusted(Box>), + Trusted(Box>), /// Verifier unconfirmed. Check whether given finality proof finalizes given hash /// under previous epoch. - Unconfirmed(Box>, &'a [u8], H256), + Unconfirmed(Box>, &'a [u8], H256), /// Error constructing verifier. Err(Error), } @@ -257,7 +257,7 @@ pub enum ConstructedVerifier<'a, M: Machine> { impl<'a, M: Machine> ConstructedVerifier<'a, M> { /// Convert to a result, indicating that any necessary confirmation has been done /// already. - pub fn known_confirmed(self) -> Result>, Error> { + pub fn known_confirmed(self) -> Result>, Error> { match self { ConstructedVerifier::Trusted(v) | ConstructedVerifier::Unconfirmed(v, _, _) => Ok(v), ConstructedVerifier::Err(e) => Err(e), @@ -303,7 +303,7 @@ pub trait Engine: Sync + Send { &self, _block: &mut ExecutedBlock, _epoch_begin: bool, - _ancestry: &mut Iterator, + _ancestry: &mut dyn Iterator, ) -> Result<(), M::Error> { Ok(()) } @@ -426,7 +426,7 @@ pub trait Engine: Sync + Send { fn handle_message(&self, _message: &[u8]) -> Result<(), EngineError> { Err(EngineError::UnexpectedMessage) } /// Register a component which signs consensus messages. - fn set_signer(&self, _signer: Box) {} + fn set_signer(&self, _signer: Box) {} /// Sign using the EngineSigner, to be used for consensus tx signing. fn sign(&self, _hash: H256) -> Result { unimplemented!() } @@ -439,7 +439,7 @@ pub trait Engine: Sync + Send { /// Create a factory for building snapshot chunks and restoring from them. /// Returning `None` indicates that this engine doesn't support snapshot creation. - fn snapshot_components(&self) -> Option> { + fn snapshot_components(&self) -> Option> { None } @@ -463,7 +463,7 @@ pub trait Engine: Sync + Send { /// Gather all ancestry actions. Called at the last stage when a block is committed. The Engine must guarantee that /// the ancestry exists. - fn ancestry_actions(&self, _header: &Header, _ancestry: &mut Iterator) -> Vec { + fn ancestry_actions(&self, _header: &Header, _ancestry: &mut dyn Iterator) -> Vec { Vec::new() } diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 27138985ad6..42cb9b5b40e 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -99,7 +99,7 @@ impl Engine for NullEngine { Ok(()) } - fn snapshot_components(&self) -> Option> { + fn snapshot_components(&self) -> Option> { Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000))) } diff --git a/ethcore/src/engines/signer.rs b/ethcore/src/engines/signer.rs index 67066e74d7a..196c535fe76 100644 --- a/ethcore/src/engines/signer.rs +++ b/ethcore/src/engines/signer.rs @@ -29,7 +29,7 @@ pub trait EngineSigner: Send + Sync { } /// Creates a new `EngineSigner` from given key pair. -pub fn from_keypair(keypair: ethkey::KeyPair) -> Box { +pub fn from_keypair(keypair: ethkey::KeyPair) -> Box { Box::new(Signer(keypair)) } diff --git a/ethcore/src/engines/validator_set/contract.rs b/ethcore/src/engines/validator_set/contract.rs index dbf92ce4b59..5560447c447 100644 --- a/ethcore/src/engines/validator_set/contract.rs +++ b/ethcore/src/engines/validator_set/contract.rs @@ -37,7 +37,7 @@ use_contract!(validator_report, "res/contracts/validator_report.json"); pub struct ValidatorContract { contract_address: Address, validators: ValidatorSafeContract, - client: RwLock>>, // TODO [keorn]: remove + client: RwLock>>, // TODO [keorn]: remove } impl ValidatorContract { @@ -125,7 +125,7 @@ impl ValidatorSet for ValidatorContract { } } - fn register_client(&self, client: Weak) { + fn register_client(&self, client: Weak) { self.validators.register_client(client.clone()); *self.client.write() = Some(client); } diff --git a/ethcore/src/engines/validator_set/mod.rs b/ethcore/src/engines/validator_set/mod.rs index 915a3f9a15d..8d31937091a 100644 --- a/ethcore/src/engines/validator_set/mod.rs +++ b/ethcore/src/engines/validator_set/mod.rs @@ -44,7 +44,7 @@ use self::multi::Multi; use super::SystemCall; /// Creates a validator set from spec. -pub fn new_validator_set(spec: ValidatorSpec) -> Box { +pub fn new_validator_set(spec: ValidatorSpec) -> Box { match spec { ValidatorSpec::List(list) => Box::new(SimpleList::new(list.into_iter().map(Into::into).collect())), ValidatorSpec::SafeContract(address) => Box::new(ValidatorSafeContract::new(address.into())), @@ -141,5 +141,5 @@ pub trait ValidatorSet: Send + Sync + 'static { /// Notifies about benign misbehaviour. fn report_benign(&self, _validator: &Address, _set_block: BlockNumber, _block: BlockNumber) {} /// Allows blockchain state access. - fn register_client(&self, _client: Weak) {} + fn register_client(&self, _client: Weak) {} } diff --git a/ethcore/src/engines/validator_set/multi.rs b/ethcore/src/engines/validator_set/multi.rs index b9ef6774784..1d115bf5d33 100644 --- a/ethcore/src/engines/validator_set/multi.rs +++ b/ethcore/src/engines/validator_set/multi.rs @@ -30,15 +30,15 @@ use client::EngineClient; use machine::{AuxiliaryData, Call, EthereumMachine}; use super::{SystemCall, ValidatorSet}; -type BlockNumberLookup = Box Result + Send + Sync + 'static>; +type BlockNumberLookup = Box Result + Send + Sync + 'static>; pub struct Multi { - sets: BTreeMap>, + sets: BTreeMap>, block_number: RwLock, } impl Multi { - pub fn new(set_map: BTreeMap>) -> Self { + pub fn new(set_map: BTreeMap>) -> Self { assert!(set_map.get(&0u64).is_some(), "ValidatorSet has to be specified from block 0."); Multi { sets: set_map, @@ -46,7 +46,7 @@ impl Multi { } } - fn correct_set(&self, id: BlockId) -> Option<&ValidatorSet> { + fn correct_set(&self, id: BlockId) -> Option<&dyn ValidatorSet> { match self.block_number.read()(id).map(|parent_block| self.correct_set_by_number(parent_block)) { Ok((_, set)) => Some(set), Err(e) => { @@ -58,7 +58,7 @@ impl Multi { // get correct set by block number, along with block number at which // this set was activated. - fn correct_set_by_number(&self, parent_block: BlockNumber) -> (BlockNumber, &ValidatorSet) { + fn correct_set_by_number(&self, parent_block: BlockNumber) -> (BlockNumber, &dyn ValidatorSet) { let (block, set) = self.sets.iter() .rev() .find(|&(block, _)| *block <= parent_block + 1) @@ -134,7 +134,7 @@ impl ValidatorSet for Multi { self.correct_set_by_number(set_block).1.report_benign(validator, set_block, block); } - fn register_client(&self, client: Weak) { + fn register_client(&self, client: Weak) { for set in self.sets.values() { set.register_client(client.clone()); } @@ -215,7 +215,7 @@ mod tests { fn transition_to_fixed_list_instant() { use super::super::SimpleList; - let mut map: BTreeMap<_, Box> = BTreeMap::new(); + let mut map: BTreeMap<_, Box> = BTreeMap::new(); let list1: Vec<_> = (0..10).map(|_| Address::random()).collect(); let list2 = { let mut list = list1.clone(); diff --git a/ethcore/src/engines/validator_set/safe_contract.rs b/ethcore/src/engines/validator_set/safe_contract.rs index fc89a97ddba..18477d64de0 100644 --- a/ethcore/src/engines/validator_set/safe_contract.rs +++ b/ethcore/src/engines/validator_set/safe_contract.rs @@ -75,7 +75,7 @@ impl ::engines::StateDependentProof for StateProof { pub struct ValidatorSafeContract { contract_address: Address, validators: RwLock>, - client: RwLock>>, // TODO [keorn]: remove + client: RwLock>>, // TODO [keorn]: remove } // first proof is just a state proof call of `getValidators` at header's state. @@ -435,7 +435,7 @@ impl ValidatorSet for ValidatorSafeContract { })) } - fn register_client(&self, client: Weak) { + fn register_client(&self, client: Weak) { trace!(target: "engine", "Setting up contract caller."); *self.client.write() = Some(client); } diff --git a/ethcore/src/engines/validator_set/simple_list.rs b/ethcore/src/engines/validator_set/simple_list.rs index 44b6df4cf34..6d4da2d239c 100644 --- a/ethcore/src/engines/validator_set/simple_list.rs +++ b/ethcore/src/engines/validator_set/simple_list.rs @@ -108,8 +108,8 @@ impl ValidatorSet for SimpleList { } } -impl AsRef for SimpleList { - fn as_ref(&self) -> &ValidatorSet { +impl AsRef for SimpleList { + fn as_ref(&self) -> &dyn ValidatorSet { self } } diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index 5e4229a204f..477f67b26b1 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -162,7 +162,7 @@ pub enum QueueError { } impl error::Error for QueueError { - fn source(&self) -> Option<&(error::Error + 'static)> { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { QueueError::Channel(e) => Some(e), _ => None, @@ -264,7 +264,7 @@ pub enum Error { } impl error::Error for Error { - fn source(&self) -> Option<&(error::Error + 'static)> { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { Error::Io(e) => Some(e), Error::StdIo(e) => Some(e), diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index f2f0d000364..4fcf7f098f2 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -377,7 +377,7 @@ impl Engine for Arc { engines::ConstructedVerifier::Trusted(Box::new(self.clone())) } - fn snapshot_components(&self) -> Option> { + fn snapshot_components(&self) -> Option> { Some(Box::new(::snapshot::PowSnapshot::new(SNAPSHOT_BLOCKS, MAX_SNAPSHOT_BLOCKS))) } diff --git a/ethcore/src/executive.rs b/ethcore/src/executive.rs index 2d96db0705d..e14a43787ec 100644 --- a/ethcore/src/executive.rs +++ b/ethcore/src/executive.rs @@ -201,8 +201,8 @@ enum CallCreateExecutiveKind { CallBuiltin(ActionParams), ExecCall(ActionParams, Substate), ExecCreate(ActionParams, Substate), - ResumeCall(OriginInfo, Box, Substate), - ResumeCreate(OriginInfo, Box, Substate), + ResumeCall(OriginInfo, Box, Substate), + ResumeCreate(OriginInfo, Box, Substate), } /// Executive for a raw call/create action. diff --git a/ethcore/src/factory.rs b/ethcore/src/factory.rs index 06b77da9aa1..574cbeeb39d 100644 --- a/ethcore/src/factory.rs +++ b/ethcore/src/factory.rs @@ -31,7 +31,7 @@ pub struct VmFactory { } impl VmFactory { - pub fn create(&self, params: ActionParams, schedule: &Schedule, depth: usize) -> Box { + pub fn create(&self, params: ActionParams, schedule: &Schedule, depth: usize) -> Box { if schedule.wasm.is_some() && params.code.as_ref().map_or(false, |code| code.len() > 4 && &code[0..4] == WASM_MAGIC_NUMBER) { Box::new(WasmInterpreter::new(params)) } else { diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 315cd0bb5ed..a5f3adf5891 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -54,10 +54,6 @@ //! cargo build --release //! ``` -// Recursion limit required because of -// error_chain foreign_links. -#![recursion_limit="128"] - extern crate ansi_term; extern crate bn; extern crate byteorder; @@ -82,7 +78,11 @@ extern crate journaldb; extern crate keccak_hash as hash; extern crate keccak_hasher; extern crate kvdb; +// Note: in `ethcore` this is only used by tests, so without `#[cfg(test)]` there's a warning. +// However, when building `parity-ethereum` this is needed. So there's something funny going on +// here. extern crate kvdb_memorydb; + extern crate len_caching_lock; extern crate lru_cache; extern crate memory_cache; diff --git a/ethcore/src/machine/impls.rs b/ethcore/src/machine/impls.rs index 4962f1ea25f..ec14f1f95c9 100644 --- a/ethcore/src/machine/impls.rs +++ b/ethcore/src/machine/impls.rs @@ -67,7 +67,7 @@ impl From<::ethjson::spec::EthashParams> for EthashExtensions { } /// Special rules to be applied to the schedule. -pub type ScheduleCreationRules = Fn(&mut Schedule, BlockNumber) + Sync + Send; +pub type ScheduleCreationRules = dyn Fn(&mut Schedule, BlockNumber) + Sync + Send; /// An ethereum-like state machine. pub struct EthereumMachine { @@ -415,7 +415,7 @@ pub struct AuxiliaryData<'a> { /// Type alias for a function we can make calls through synchronously. /// Returns the call result and state proof for each call. -pub type Call<'a> = Fn(Address, Vec) -> Result<(Vec, Vec>), String> + 'a; +pub type Call<'a> = dyn Fn(Address, Vec) -> Result<(Vec, Vec>), String> + 'a; /// Request for auxiliary data of a block. #[derive(Debug, Clone, Copy, PartialEq)] @@ -429,7 +429,7 @@ pub enum AuxiliaryRequest { } impl super::Machine for EthereumMachine { - type EngineClient = ::client::EngineClient; + type EngineClient = dyn (::client::EngineClient); type Error = Error; diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 67ef9e6181c..0bd40d1c8d1 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -203,7 +203,7 @@ pub enum Author { /// Sealing block is external and we only need a reward beneficiary (i.e. PoW) External(Address), /// Sealing is done internally, we need a way to create signatures to seal block (i.e. PoA) - Sealer(Box), + Sealer(Box), } impl Author { @@ -245,8 +245,8 @@ pub struct Miner { options: MinerOptions, // TODO [ToDr] Arc is only required because of price updater transaction_queue: Arc, - engine: Arc, - accounts: Arc, + engine: Arc, + accounts: Arc, io_channel: RwLock>>, service_transaction_checker: Option, } diff --git a/ethcore/src/miner/pool_client.rs b/ethcore/src/miner/pool_client.rs index 60e93dee8ac..98431b45852 100644 --- a/ethcore/src/miner/pool_client.rs +++ b/ethcore/src/miner/pool_client.rs @@ -72,8 +72,8 @@ impl NonceCache { pub struct PoolClient<'a, C: 'a> { chain: &'a C, cached_nonces: CachedNonceClient<'a, C>, - engine: &'a EthEngine, - accounts: &'a LocalAccounts, + engine: &'a dyn EthEngine, + accounts: &'a dyn LocalAccounts, best_block_header: Header, service_transaction_checker: Option<&'a ServiceTransactionChecker>, } @@ -98,8 +98,8 @@ impl<'a, C: 'a> PoolClient<'a, C> where pub fn new( chain: &'a C, cache: &'a NonceCache, - engine: &'a EthEngine, - accounts: &'a LocalAccounts, + engine: &'a dyn EthEngine, + accounts: &'a dyn LocalAccounts, service_transaction_checker: Option<&'a ServiceTransactionChecker>, ) -> Self { let best_block_header = chain.best_block_header(); diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index c02074f95f9..5bdb05f49d0 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -79,7 +79,7 @@ impl PodAccount { } /// Place additional data into given hash DB. - pub fn insert_additional(&self, db: &mut HashDB, factory: &TrieFactory) { + pub fn insert_additional(&self, db: &mut dyn HashDB, factory: &TrieFactory) { match self.code { Some(ref c) if !c.is_empty() => { db.insert(c); } _ => {} diff --git a/ethcore/src/snapshot/account.rs b/ethcore/src/snapshot/account.rs index d18ecacbc3b..2a9ac911f12 100644 --- a/ethcore/src/snapshot/account.rs +++ b/ethcore/src/snapshot/account.rs @@ -24,9 +24,10 @@ use ethtrie::{TrieDB, TrieDBMut}; use hash::{KECCAK_EMPTY, KECCAK_NULL_RLP}; use hash_db::HashDB; use rlp::{RlpStream, Rlp}; -use snapshot::Error; +use snapshot::{Error, Progress}; use std::collections::HashSet; use trie::{Trie, TrieMut}; +use std::sync::atomic::Ordering; // An empty account -- these were replaced with RLP null data for a space optimization in v1. const ACC_EMPTY: BasicAccount = BasicAccount { @@ -65,8 +66,16 @@ impl CodeState { // walk the account's storage trie, returning a vector of RLP items containing the // account address hash, account properties and the storage. Each item contains at most `max_storage_items` // storage records split according to snapshot format definition. -pub fn to_fat_rlps(account_hash: &H256, acc: &BasicAccount, acct_db: &AccountDB, used_code: &mut HashSet, first_chunk_size: usize, max_chunk_size: usize) -> Result, Error> { - let db = &(acct_db as &HashDB<_,_>); +pub fn to_fat_rlps( + account_hash: &H256, + acc: &BasicAccount, + acct_db: &AccountDB, + used_code: &mut HashSet, + first_chunk_size: usize, + max_chunk_size: usize, + p: &Progress, +) -> Result, Error> { + let db = &(acct_db as &dyn HashDB<_,_>); let db = TrieDB::new(db, &acc.storage_root)?; let mut chunks = Vec::new(); let mut db_iter = db.iter()?; @@ -112,6 +121,10 @@ pub fn to_fat_rlps(account_hash: &H256, acc: &BasicAccount, acct_db: &AccountDB, } loop { + if p.abort.load(Ordering::SeqCst) { + trace!(target: "snapshot", "to_fat_rlps: aborting snapshot"); + return Err(Error::SnapshotAborted); + } match db_iter.next() { Some(Ok((k, v))) => { let pair = { @@ -211,6 +224,7 @@ mod tests { use types::basic_account::BasicAccount; use test_helpers::get_temp_state_db; use snapshot::tests::helpers::fill_storage; + use snapshot::Progress; use hash::{KECCAK_EMPTY, KECCAK_NULL_RLP, keccak}; use ethereum_types::{H256, Address}; @@ -236,8 +250,8 @@ mod tests { let thin_rlp = ::rlp::encode(&account); assert_eq!(::rlp::decode::(&thin_rlp).unwrap(), account); - - let fat_rlps = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap(); + let p = Progress::default(); + let fat_rlps = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), usize::max_value(), usize::max_value(), &p).unwrap(); let fat_rlp = Rlp::new(&fat_rlps[0]).at(1).unwrap(); assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account); } @@ -262,7 +276,9 @@ mod tests { let thin_rlp = ::rlp::encode(&account); assert_eq!(::rlp::decode::(&thin_rlp).unwrap(), account); - let fat_rlp = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap(); + let p = Progress::default(); + + let fat_rlp = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), usize::max_value(), usize::max_value(), &p).unwrap(); let fat_rlp = Rlp::new(&fat_rlp[0]).at(1).unwrap(); assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account); } @@ -287,7 +303,8 @@ mod tests { let thin_rlp = ::rlp::encode(&account); assert_eq!(::rlp::decode::(&thin_rlp).unwrap(), account); - let fat_rlps = to_fat_rlps(&keccak(addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), 500, 1000).unwrap(); + let p = Progress::default(); + let fat_rlps = to_fat_rlps(&keccak(addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), 500, 1000, &p).unwrap(); let mut root = KECCAK_NULL_RLP; let mut restored_account = None; for rlp in fat_rlps { @@ -319,20 +336,21 @@ mod tests { nonce: 50.into(), balance: 123456789.into(), storage_root: KECCAK_NULL_RLP, - code_hash: code_hash, + code_hash, }; let account2 = BasicAccount { nonce: 400.into(), balance: 98765432123456789usize.into(), storage_root: KECCAK_NULL_RLP, - code_hash: code_hash, + code_hash, }; let mut used_code = HashSet::new(); - - let fat_rlp1 = to_fat_rlps(&keccak(&addr1), &account1, &AccountDB::new(db.as_hash_db(), &addr1), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); - let fat_rlp2 = to_fat_rlps(&keccak(&addr2), &account2, &AccountDB::new(db.as_hash_db(), &addr2), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); + let p1 = Progress::default(); + let p2 = Progress::default(); + let fat_rlp1 = to_fat_rlps(&keccak(&addr1), &account1, &AccountDB::new(db.as_hash_db(), &addr1), &mut used_code, usize::max_value(), usize::max_value(), &p1).unwrap(); + let fat_rlp2 = to_fat_rlps(&keccak(&addr2), &account2, &AccountDB::new(db.as_hash_db(), &addr2), &mut used_code, usize::max_value(), usize::max_value(), &p2).unwrap(); assert_eq!(used_code.len(), 1); let fat_rlp1 = Rlp::new(&fat_rlp1[0]).at(1).unwrap(); diff --git a/ethcore/src/snapshot/consensus/authority.rs b/ethcore/src/snapshot/consensus/authority.rs index 4423e074019..08a1f8afea0 100644 --- a/ethcore/src/snapshot/consensus/authority.rs +++ b/ethcore/src/snapshot/consensus/authority.rs @@ -127,9 +127,9 @@ impl SnapshotComponents for PoaSnapshot { fn rebuilder( &self, chain: BlockChain, - db: Arc, + db: Arc, manifest: &ManifestData, - ) -> Result, ::error::Error> { + ) -> Result, ::error::Error> { Ok(Box::new(ChunkRebuilder { manifest: manifest.clone(), warp_target: None, @@ -164,14 +164,14 @@ struct ChunkRebuilder { manifest: ManifestData, warp_target: Option
, chain: BlockChain, - db: Arc, + db: Arc, had_genesis: bool, // sorted vectors of unverified first blocks in a chunk // and epoch data from last blocks in chunks. // verification for these will be done at the end. unverified_firsts: Vec<(Header, Bytes, H256)>, - last_epochs: Vec<(Header, Box>)>, + last_epochs: Vec<(Header, Box>)>, } // verified data. @@ -183,9 +183,9 @@ struct Verified { impl ChunkRebuilder { fn verify_transition( &mut self, - last_verifier: &mut Option>>, + last_verifier: &mut Option>>, transition_rlp: Rlp, - engine: &EthEngine, + engine: &dyn EthEngine, ) -> Result { use engines::ConstructedVerifier; @@ -241,7 +241,7 @@ impl Rebuilder for ChunkRebuilder { fn feed( &mut self, chunk: &[u8], - engine: &EthEngine, + engine: &dyn EthEngine, abort_flag: &AtomicBool, ) -> Result<(), ::error::Error> { let rlp = Rlp::new(chunk); @@ -349,7 +349,7 @@ impl Rebuilder for ChunkRebuilder { Ok(()) } - fn finalize(&mut self, _engine: &EthEngine) -> Result<(), ::error::Error> { + fn finalize(&mut self, _engine: &dyn EthEngine) -> Result<(), ::error::Error> { if !self.had_genesis { return Err(Error::WrongChunkFormat("No genesis transition included.".into()).into()); } diff --git a/ethcore/src/snapshot/consensus/mod.rs b/ethcore/src/snapshot/consensus/mod.rs index 907e9c520bb..670700c10cf 100644 --- a/ethcore/src/snapshot/consensus/mod.rs +++ b/ethcore/src/snapshot/consensus/mod.rs @@ -33,7 +33,7 @@ pub use self::authority::*; pub use self::work::*; /// A sink for produced chunks. -pub type ChunkSink<'a> = FnMut(&[u8]) -> ::std::io::Result<()> + 'a; +pub type ChunkSink<'a> = dyn FnMut(&[u8]) -> ::std::io::Result<()> + 'a; /// Components necessary for snapshot creation and restoration. pub trait SnapshotComponents: Send { @@ -63,9 +63,9 @@ pub trait SnapshotComponents: Send { fn rebuilder( &self, chain: BlockChain, - db: Arc, + db: Arc, manifest: &ManifestData, - ) -> Result, ::error::Error>; + ) -> Result, ::error::Error>; /// Minimum supported snapshot version number. fn min_supported_version(&self) -> u64; @@ -83,7 +83,7 @@ pub trait Rebuilder: Send { fn feed( &mut self, chunk: &[u8], - engine: &EthEngine, + engine: &dyn EthEngine, abort_flag: &AtomicBool, ) -> Result<(), ::error::Error>; @@ -92,5 +92,5 @@ pub trait Rebuilder: Send { /// /// This should apply the necessary "glue" between chunks, /// and verify against the restored state. - fn finalize(&mut self, engine: &EthEngine) -> Result<(), ::error::Error>; + fn finalize(&mut self, engine: &dyn EthEngine) -> Result<(), ::error::Error>; } diff --git a/ethcore/src/snapshot/consensus/work.rs b/ethcore/src/snapshot/consensus/work.rs index a3d4da1c2ef..201d528d140 100644 --- a/ethcore/src/snapshot/consensus/work.rs +++ b/ethcore/src/snapshot/consensus/work.rs @@ -81,9 +81,9 @@ impl SnapshotComponents for PowSnapshot { fn rebuilder( &self, chain: BlockChain, - db: Arc, + db: Arc, manifest: &ManifestData, - ) -> Result, ::error::Error> { + ) -> Result, ::error::Error> { PowRebuilder::new(chain, db.key_value().clone(), manifest, self.max_restore_blocks).map(|r| Box::new(r) as Box<_>) } @@ -194,7 +194,7 @@ impl<'a> PowWorker<'a> { /// After all chunks have been submitted, we "glue" the chunks together. pub struct PowRebuilder { chain: BlockChain, - db: Arc, + db: Arc, rng: OsRng, disconnected: Vec<(u64, H256)>, best_number: u64, @@ -206,7 +206,7 @@ pub struct PowRebuilder { impl PowRebuilder { /// Create a new PowRebuilder. - fn new(chain: BlockChain, db: Arc, manifest: &ManifestData, snapshot_blocks: u64) -> Result { + fn new(chain: BlockChain, db: Arc, manifest: &ManifestData, snapshot_blocks: u64) -> Result { Ok(PowRebuilder { chain: chain, db: db, @@ -224,7 +224,7 @@ impl PowRebuilder { impl Rebuilder for PowRebuilder { /// Feed the rebuilder an uncompressed block chunk. /// Returns the number of blocks fed or any errors. - fn feed(&mut self, chunk: &[u8], engine: &EthEngine, abort_flag: &AtomicBool) -> Result<(), ::error::Error> { + fn feed(&mut self, chunk: &[u8], engine: &dyn EthEngine, abort_flag: &AtomicBool) -> Result<(), ::error::Error> { use snapshot::verify_old_block; use ethereum_types::U256; use triehash::ordered_trie_root; @@ -298,7 +298,7 @@ impl Rebuilder for PowRebuilder { } /// Glue together any disconnected chunks and check that the chain is complete. - fn finalize(&mut self, _: &EthEngine) -> Result<(), ::error::Error> { + fn finalize(&mut self, _: &dyn EthEngine) -> Result<(), ::error::Error> { let mut batch = self.db.transaction(); for (first_num, first_hash) in self.disconnected.drain(..) { diff --git a/ethcore/src/snapshot/error.rs b/ethcore/src/snapshot/error.rs index 0eba4725569..8381bd4cb9a 100644 --- a/ethcore/src/snapshot/error.rs +++ b/ethcore/src/snapshot/error.rs @@ -62,6 +62,8 @@ pub enum Error { ChunkTooLarge, /// Snapshots not supported by the consensus engine. SnapshotsUnsupported, + /// Aborted snapshot + SnapshotAborted, /// Bad epoch transition. BadEpochProof(u64), /// Wrong chunk format. @@ -71,7 +73,7 @@ pub enum Error { } impl error::Error for Error { - fn source(&self) -> Option<&(error::Error + 'static)> { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { Error::Trie(e) => Some(e), Error::Decoder(e) => Some(e), @@ -103,6 +105,7 @@ impl fmt::Display for Error { Error::ChunkTooSmall => write!(f, "Chunk size is too small."), Error::ChunkTooLarge => write!(f, "Chunk size is too large."), Error::SnapshotsUnsupported => write!(f, "Snapshots unsupported by consensus engine."), + Error::SnapshotAborted => write!(f, "Snapshot was aborted."), Error::BadEpochProof(i) => write!(f, "Bad epoch proof for transition to epoch {}", i), Error::WrongChunkFormat(ref msg) => write!(f, "Wrong chunk format: {}", msg), Error::UnlinkedAncientBlockChain => write!(f, "Unlinked ancient blocks chain"), diff --git a/ethcore/src/snapshot/io.rs b/ethcore/src/snapshot/io.rs index c5f178cd329..536862e7bee 100644 --- a/ethcore/src/snapshot/io.rs +++ b/ethcore/src/snapshot/io.rs @@ -310,10 +310,7 @@ impl LooseReader { dir.pop(); - Ok(LooseReader { - dir: dir, - manifest: manifest, - }) + Ok(LooseReader { dir, manifest }) } } diff --git a/ethcore/src/snapshot/mod.rs b/ethcore/src/snapshot/mod.rs index 155182ed516..51f6f6de8bb 100644 --- a/ethcore/src/snapshot/mod.rs +++ b/ethcore/src/snapshot/mod.rs @@ -22,7 +22,7 @@ use std::collections::{HashMap, HashSet}; use std::cmp; use std::sync::Arc; -use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY}; use account_db::{AccountDB, AccountDBMut}; @@ -107,7 +107,7 @@ impl Default for SnapshotConfiguration { fn default() -> Self { SnapshotConfiguration { no_periodic: false, - processing_threads: ::std::cmp::max(1, num_cpus::get() / 2), + processing_threads: ::std::cmp::max(1, num_cpus::get_physical() / 2), } } } @@ -117,8 +117,9 @@ impl Default for SnapshotConfiguration { pub struct Progress { accounts: AtomicUsize, blocks: AtomicUsize, - size: AtomicUsize, // Todo [rob] use Atomicu64 when it stabilizes. + size: AtomicU64, done: AtomicBool, + abort: AtomicBool, } impl Progress { @@ -127,6 +128,7 @@ impl Progress { self.accounts.store(0, Ordering::Release); self.blocks.store(0, Ordering::Release); self.size.store(0, Ordering::Release); + self.abort.store(false, Ordering::Release); // atomic fence here to ensure the others are written first? // logs might very rarely get polluted if not. @@ -140,7 +142,7 @@ impl Progress { pub fn blocks(&self) -> usize { self.blocks.load(Ordering::Acquire) } /// Get the written size of the snapshot in bytes. - pub fn size(&self) -> usize { self.size.load(Ordering::Acquire) } + pub fn size(&self) -> u64 { self.size.load(Ordering::Acquire) } /// Whether the snapshot is complete. pub fn done(&self) -> bool { self.done.load(Ordering::Acquire) } @@ -148,27 +150,28 @@ impl Progress { } /// Take a snapshot using the given blockchain, starting block hash, and database, writing into the given writer. pub fn take_snapshot( - engine: &EthEngine, + chunker: Box, chain: &BlockChain, - block_at: H256, - state_db: &HashDB, + block_hash: H256, + state_db: &dyn HashDB, writer: W, p: &Progress, processing_threads: usize, ) -> Result<(), Error> { - let start_header = chain.block_header_data(&block_at) - .ok_or_else(|| Error::InvalidStartingBlock(BlockId::Hash(block_at)))?; + let start_header = chain.block_header_data(&block_hash) + .ok_or_else(|| Error::InvalidStartingBlock(BlockId::Hash(block_hash)))?; let state_root = start_header.state_root(); - let number = start_header.number(); + let block_number = start_header.number(); - info!("Taking snapshot starting at block {}", number); + info!("Taking snapshot starting at block {}", block_number); + let version = chunker.current_version(); let writer = Mutex::new(writer); - let chunker = engine.snapshot_components().ok_or(Error::SnapshotsUnsupported)?; - let snapshot_version = chunker.current_version(); let (state_hashes, block_hashes) = scope(|scope| -> Result<(Vec, Vec), Error> { let writer = &writer; - let block_guard = scope.spawn(move || chunk_secondary(chunker, chain, block_at, writer, p)); + let block_guard = scope.spawn(move || { + chunk_secondary(chunker, chain, block_hash, writer, p) + }); // The number of threads must be between 1 and SNAPSHOT_SUBPARTS assert!(processing_threads >= 1, "Cannot use less than 1 threads for creating snapshots"); @@ -183,7 +186,7 @@ pub fn take_snapshot( for part in (thread_idx..SNAPSHOT_SUBPARTS).step_by(num_threads) { debug!(target: "snapshot", "Chunking part {} in thread {}", part, thread_idx); - let mut hashes = chunk_state(state_db, &state_root, writer, p, Some(part))?; + let mut hashes = chunk_state(state_db, &state_root, writer, p, Some(part), thread_idx)?; chunk_hashes.append(&mut hashes); } @@ -207,12 +210,12 @@ pub fn take_snapshot( info!(target: "snapshot", "produced {} state chunks and {} block chunks.", state_hashes.len(), block_hashes.len()); let manifest_data = ManifestData { - version: snapshot_version, - state_hashes: state_hashes, - block_hashes: block_hashes, - state_root: state_root, - block_number: number, - block_hash: block_at, + version, + state_hashes, + block_hashes, + state_root, + block_number, + block_hash, }; writer.into_inner().finish(manifest_data)?; @@ -228,7 +231,13 @@ pub fn take_snapshot( /// Secondary chunks are engine-specific, but they intend to corroborate the state data /// in the state chunks. /// Returns a list of chunk hashes, with the first having the blocks furthest from the genesis. -pub fn chunk_secondary<'a>(mut chunker: Box, chain: &'a BlockChain, start_hash: H256, writer: &Mutex, progress: &'a Progress) -> Result, Error> { +pub fn chunk_secondary<'a>( + mut chunker: Box, + chain: &'a BlockChain, + start_hash: H256, + writer: &Mutex, + progress: &'a Progress +) -> Result, Error> { let mut chunk_hashes = Vec::new(); let mut snappy_buffer = vec![0; snappy::max_compressed_len(PREFERRED_CHUNK_SIZE)]; @@ -243,7 +252,7 @@ pub fn chunk_secondary<'a>(mut chunker: Box, chain: &'a Bloc trace!(target: "snapshot", "wrote secondary chunk. hash: {:x}, size: {}, uncompressed size: {}", hash, size, raw_data.len()); - progress.size.fetch_add(size, Ordering::SeqCst); + progress.size.fetch_add(size as u64, Ordering::SeqCst); chunk_hashes.push(hash); Ok(()) }; @@ -266,8 +275,9 @@ struct StateChunker<'a> { rlps: Vec, cur_size: usize, snappy_buffer: Vec, - writer: &'a Mutex, + writer: &'a Mutex, progress: &'a Progress, + thread_idx: usize, } impl<'a> StateChunker<'a> { @@ -297,10 +307,10 @@ impl<'a> StateChunker<'a> { let hash = keccak(&compressed); self.writer.lock().write_state_chunk(hash, compressed)?; - trace!(target: "snapshot", "wrote state chunk. size: {}, uncompressed size: {}", compressed_size, raw_data.len()); + trace!(target: "snapshot", "Thread {} wrote state chunk. size: {}, uncompressed size: {}", self.thread_idx, compressed_size, raw_data.len()); self.progress.accounts.fetch_add(num_entries, Ordering::SeqCst); - self.progress.size.fetch_add(compressed_size, Ordering::SeqCst); + self.progress.size.fetch_add(compressed_size as u64, Ordering::SeqCst); self.hashes.push(hash); self.cur_size = 0; @@ -321,7 +331,14 @@ impl<'a> StateChunker<'a> { /// /// Returns a list of hashes of chunks created, or any error it may /// have encountered. -pub fn chunk_state<'a>(db: &HashDB, root: &H256, writer: &Mutex, progress: &'a Progress, part: Option) -> Result, Error> { +pub fn chunk_state<'a>( + db: &dyn HashDB, + root: &H256, + writer: &Mutex, + progress: &'a Progress, + part: Option, + thread_idx: usize, +) -> Result, Error> { let account_trie = TrieDB::new(&db, &root)?; let mut chunker = StateChunker { @@ -329,8 +346,9 @@ pub fn chunk_state<'a>(db: &HashDB, root: &H256, writer: rlps: Vec::new(), cur_size: 0, snappy_buffer: vec![0; snappy::max_compressed_len(PREFERRED_CHUNK_SIZE)], - writer: writer, - progress: progress, + writer, + progress, + thread_idx, }; let mut used_code = HashSet::new(); @@ -365,7 +383,7 @@ pub fn chunk_state<'a>(db: &HashDB, root: &H256, writer: let account = ::rlp::decode(&*account_data)?; let account_db = AccountDB::from_hash(db, account_key_hash); - let fat_rlps = account::to_fat_rlps(&account_key_hash, &account, &account_db, &mut used_code, PREFERRED_CHUNK_SIZE - chunker.chunk_size(), PREFERRED_CHUNK_SIZE)?; + let fat_rlps = account::to_fat_rlps(&account_key_hash, &account, &account_db, &mut used_code, PREFERRED_CHUNK_SIZE - chunker.chunk_size(), PREFERRED_CHUNK_SIZE, progress)?; for (i, fat_rlp) in fat_rlps.into_iter().enumerate() { if i > 0 { chunker.write_chunk()?; @@ -383,7 +401,7 @@ pub fn chunk_state<'a>(db: &HashDB, root: &H256, writer: /// Used to rebuild the state trie piece by piece. pub struct StateRebuilder { - db: Box, + db: Box, state_root: H256, known_code: HashMap, // code hashes mapped to first account with this code. missing_code: HashMap>, // maps code hashes to lists of accounts missing that code. @@ -393,7 +411,7 @@ pub struct StateRebuilder { impl StateRebuilder { /// Create a new state rebuilder to write into the given backing DB. - pub fn new(db: Arc, pruning: Algorithm) -> Self { + pub fn new(db: Arc, pruning: Algorithm) -> Self { StateRebuilder { db: journaldb::new(db.clone(), pruning, ::db::COL_STATE), state_root: KECCAK_NULL_RLP, @@ -468,7 +486,7 @@ impl StateRebuilder { /// Finalize the restoration. Check for accounts missing code and make a dummy /// journal entry. /// Once all chunks have been fed, there should be nothing missing. - pub fn finalize(mut self, era: u64, id: H256) -> Result, ::error::Error> { + pub fn finalize(mut self, era: u64, id: H256) -> Result, ::error::Error> { let missing = self.missing_code.keys().cloned().collect::>(); if !missing.is_empty() { return Err(Error::MissingCode(missing).into()) } @@ -493,7 +511,7 @@ struct RebuiltStatus { // rebuild a set of accounts and their storage. // returns a status detailing newly-loaded code and accounts missing code. fn rebuild_accounts( - db: &mut HashDB, + db: &mut dyn HashDB, account_fat_rlps: Rlp, out_chunk: &mut [(H256, Bytes)], known_code: &HashMap, @@ -560,7 +578,7 @@ const POW_VERIFY_RATE: f32 = 0.02; /// Verify an old block with the given header, engine, blockchain, body. If `always` is set, it will perform /// the fullest verification possible. If not, it will take a random sample to determine whether it will /// do heavy or light verification. -pub fn verify_old_block(rng: &mut OsRng, header: &Header, engine: &EthEngine, chain: &BlockChain, always: bool) -> Result<(), ::error::Error> { +pub fn verify_old_block(rng: &mut OsRng, header: &Header, engine: &dyn EthEngine, chain: &BlockChain, always: bool) -> Result<(), ::error::Error> { engine.verify_block_basic(header)?; if always || rng.gen::() <= POW_VERIFY_RATE { diff --git a/ethcore/src/snapshot/service.rs b/ethcore/src/snapshot/service.rs index 8120f47b81f..a9ff866ebed 100644 --- a/ethcore/src/snapshot/service.rs +++ b/ethcore/src/snapshot/service.rs @@ -76,22 +76,22 @@ struct Restoration { state_chunks_left: HashSet, block_chunks_left: HashSet, state: StateRebuilder, - secondary: Box, + secondary: Box, writer: Option, snappy_buffer: Bytes, final_state_root: H256, guard: Guard, - db: Arc, + db: Arc, } struct RestorationParams<'a> { manifest: ManifestData, // manifest to base restoration on. pruning: Algorithm, // pruning algorithm for the database. - db: Arc, // database + db: Arc, // database writer: Option, // writer for recovered snapshot. genesis: &'a [u8], // genesis block of the chain. guard: Guard, // guard for the restoration directory. - engine: &'a EthEngine, + engine: &'a dyn EthEngine, } impl Restoration { @@ -149,7 +149,7 @@ impl Restoration { } // feeds a block chunk - fn feed_blocks(&mut self, hash: H256, chunk: &[u8], engine: &EthEngine, flag: &AtomicBool) -> Result<(), Error> { + fn feed_blocks(&mut self, hash: H256, chunk: &[u8], engine: &dyn EthEngine, flag: &AtomicBool) -> Result<(), Error> { if self.block_chunks_left.contains(&hash) { let expected_len = snappy::decompressed_len(chunk)?; if expected_len > MAX_CHUNK_SIZE { @@ -170,7 +170,7 @@ impl Restoration { } // finish up restoration. - fn finalize(mut self, engine: &EthEngine) -> Result<(), Error> { + fn finalize(mut self, engine: &dyn EthEngine) -> Result<(), Error> { use trie::TrieError; if !self.is_done() { return Ok(()) } @@ -211,37 +211,37 @@ pub trait SnapshotClient: BlockChainClient + BlockInfo + DatabaseRestore {} /// Snapshot service parameters. pub struct ServiceParams { /// The consensus engine this is built on. - pub engine: Arc, + pub engine: Arc, /// The chain's genesis block. pub genesis_block: Bytes, /// State pruning algorithm. pub pruning: Algorithm, /// Handler for opening a restoration DB. - pub restoration_db_handler: Box, + pub restoration_db_handler: Box, /// Async IO channel for sending messages. pub channel: Channel, /// The directory to put snapshots in. /// Usually "/snapshot" pub snapshot_root: PathBuf, /// A handle for database restoration. - pub client: Arc, + pub client: Arc, } /// `SnapshotService` implementation. /// This controls taking snapshots and restoring from them. pub struct Service { restoration: Mutex>, - restoration_db_handler: Box, + restoration_db_handler: Box, snapshot_root: PathBuf, io_channel: Mutex, pruning: Algorithm, status: Mutex, reader: RwLock>, - engine: Arc, + engine: Arc, genesis_block: Bytes, state_chunks: AtomicUsize, block_chunks: AtomicUsize, - client: Arc, + client: Arc, progress: super::Progress, taking_snapshot: AtomicBool, restoring_snapshot: AtomicBool, @@ -415,7 +415,7 @@ impl Service { _ => break, } - // Writting changes to DB and logging every now and then + // Writing changes to DB and logging every now and then if block_number % 1_000 == 0 { next_db.key_value().write_buffered(batch); next_chain.commit(); @@ -479,16 +479,12 @@ impl Service { let guard = Guard::new(temp_dir.clone()); let res = client.take_snapshot(writer, BlockId::Number(num), &self.progress); - self.taking_snapshot.store(false, Ordering::SeqCst); if let Err(e) = res { if client.chain_info().best_block_number >= num + client.pruning_history() { - // "Cancelled" is mincing words a bit -- what really happened - // is that the state we were snapshotting got pruned out - // before we could finish. - info!("Periodic snapshot failed: block state pruned.\ - Run with a longer `--pruning-history` or with `--no-periodic-snapshot`"); - return Ok(()) + // The state we were snapshotting was pruned before we could finish. + info!("Periodic snapshot failed: block state pruned. Run with a longer `--pruning-history` or with `--no-periodic-snapshot`"); + return Err(e); } else { return Err(e); } @@ -846,14 +842,29 @@ impl SnapshotService for Service { } } + fn abort_snapshot(&self) { + if self.taking_snapshot.load(Ordering::SeqCst) { + trace!(target: "snapshot", "Aborting snapshot – Snapshot under way"); + self.progress.abort.store(true, Ordering::SeqCst); + } + } + fn shutdown(&self) { + trace!(target: "snapshot", "Shut down SnapshotService"); self.abort_restore(); + trace!(target: "snapshot", "Shut down SnapshotService - restore aborted"); + self.abort_snapshot(); + trace!(target: "snapshot", "Shut down SnapshotService - snapshot aborted"); } } impl Drop for Service { fn drop(&mut self) { + trace!(target: "shutdown", "Dropping Service"); self.abort_restore(); + trace!(target: "shutdown", "Dropping Service - restore aborted"); + self.abort_snapshot(); + trace!(target: "shutdown", "Dropping Service - snapshot aborted"); } } diff --git a/ethcore/src/snapshot/tests/helpers.rs b/ethcore/src/snapshot/tests/helpers.rs index a70c7b7e272..96c5c51b023 100644 --- a/ethcore/src/snapshot/tests/helpers.rs +++ b/ethcore/src/snapshot/tests/helpers.rs @@ -62,7 +62,7 @@ impl StateProducer { /// Tick the state producer. This alters the state, writing new data into /// the database. - pub fn tick(&mut self, rng: &mut R, db: &mut HashDB) { + pub fn tick(&mut self, rng: &mut R, db: &mut dyn HashDB) { // modify existing accounts. let mut accounts_to_modify: Vec<_> = { let trie = TrieDB::new(&db, &self.state_root).unwrap(); @@ -132,7 +132,7 @@ pub fn fill_storage(mut db: AccountDBMut, root: &mut H256, seed: &mut H256) { /// Take a snapshot from the given client into a temporary file. /// Return a snapshot reader for it. -pub fn snap(client: &Client) -> (Box, TempDir) { +pub fn snap(client: &Client) -> (Box, TempDir) { use types::ids::BlockId; let tempdir = TempDir::new("").unwrap(); @@ -151,9 +151,9 @@ pub fn snap(client: &Client) -> (Box, TempDir) { /// Restore a snapshot into a given database. This will read chunks from the given reader /// write into the given database. pub fn restore( - db: Arc, - engine: &EthEngine, - reader: &SnapshotReader, + db: Arc, + engine: &dyn EthEngine, + reader: &dyn SnapshotReader, genesis: &[u8], ) -> Result<(), ::error::Error> { use std::sync::atomic::AtomicBool; diff --git a/ethcore/src/snapshot/tests/service.rs b/ethcore/src/snapshot/tests/service.rs index 37a10048abf..515e5992ffa 100644 --- a/ethcore/src/snapshot/tests/service.rs +++ b/ethcore/src/snapshot/tests/service.rs @@ -188,14 +188,15 @@ fn keep_ancient_blocks() { &state_root, &writer, &Progress::default(), - None + None, + 0 ).unwrap(); let manifest = ::snapshot::ManifestData { version: 2, - state_hashes: state_hashes, - state_root: state_root, - block_hashes: block_hashes, + state_hashes, + state_root, + block_hashes, block_number: NUM_BLOCKS, block_hash: best_hash, }; diff --git a/ethcore/src/snapshot/tests/state.rs b/ethcore/src/snapshot/tests/state.rs index bbf3bfa6271..769534e0298 100644 --- a/ethcore/src/snapshot/tests/state.rs +++ b/ethcore/src/snapshot/tests/state.rs @@ -58,7 +58,7 @@ fn snap_and_restore() { let mut state_hashes = Vec::new(); for part in 0..SNAPSHOT_SUBPARTS { - let mut hashes = chunk_state(&old_db, &state_root, &writer, &Progress::default(), Some(part)).unwrap(); + let mut hashes = chunk_state(&old_db, &state_root, &writer, &Progress::default(), Some(part), 0).unwrap(); state_hashes.append(&mut hashes); } @@ -129,8 +129,8 @@ fn get_code_from_prev_chunk() { let mut make_chunk = |acc, hash| { let mut db = journaldb::new_memory_db(); AccountDBMut::from_hash(&mut db, hash).insert(&code[..]); - - let fat_rlp = account::to_fat_rlps(&hash, &acc, &AccountDB::from_hash(&db, hash), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); + let p = Progress::default(); + let fat_rlp = account::to_fat_rlps(&hash, &acc, &AccountDB::from_hash(&db, hash), &mut used_code, usize::max_value(), usize::max_value(), &p).unwrap(); let mut stream = RlpStream::new_list(1); stream.append_raw(&fat_rlp[0], 1); stream.out() @@ -174,13 +174,13 @@ fn checks_flag() { let state_root = producer.state_root(); let writer = Mutex::new(PackedWriter::new(&snap_file).unwrap()); - let state_hashes = chunk_state(&old_db, &state_root, &writer, &Progress::default(), None).unwrap(); + let state_hashes = chunk_state(&old_db, &state_root, &writer, &Progress::default(), None, 0).unwrap(); writer.into_inner().finish(::snapshot::ManifestData { version: 2, - state_hashes: state_hashes, + state_hashes, block_hashes: Vec::new(), - state_root: state_root, + state_root, block_number: 0, block_hash: H256::zero(), }).unwrap(); diff --git a/ethcore/src/snapshot/traits.rs b/ethcore/src/snapshot/traits.rs index bb4ab3b3964..aa61b595bf7 100644 --- a/ethcore/src/snapshot/traits.rs +++ b/ethcore/src/snapshot/traits.rs @@ -55,6 +55,9 @@ pub trait SnapshotService : Sync + Send { /// no-op if currently restoring. fn restore_block_chunk(&self, hash: H256, chunk: Bytes); + /// Abort in-progress snapshotting if there is one. + fn abort_snapshot(&self); + /// Shutdown the Snapshot Service by aborting any ongoing restore fn shutdown(&self); } diff --git a/ethcore/src/snapshot/watcher.rs b/ethcore/src/snapshot/watcher.rs index 9df21c3cd83..06df0c9cc42 100644 --- a/ethcore/src/snapshot/watcher.rs +++ b/ethcore/src/snapshot/watcher.rs @@ -72,8 +72,8 @@ impl Broadcast for Mutex> { /// A `ChainNotify` implementation which will trigger a snapshot event /// at certain block numbers. pub struct Watcher { - oracle: Box, - broadcast: Box, + oracle: Box, + broadcast: Box, period: u64, history: u64, } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index fd00dd56f08..87822226127 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -382,7 +382,7 @@ pub struct Spec { /// User friendly spec name pub name: String, /// What engine are we using for this? - pub engine: Arc, + pub engine: Arc, /// Name of the subdir inside the main data dir to use for chain data and settings. pub data_dir: String, @@ -601,7 +601,7 @@ impl Spec { engine_spec: ethjson::spec::Engine, params: CommonParams, builtins: BTreeMap, - ) -> Arc { + ) -> Arc { let machine = Self::machine(&engine_spec, params, builtins); match engine_spec { diff --git a/ethcore/src/state/account.rs b/ethcore/src/state/account.rs index 36260ca2cdc..5daaa139839 100644 --- a/ethcore/src/state/account.rs +++ b/ethcore/src/state/account.rs @@ -217,7 +217,7 @@ impl Account { /// Get (and cache) the contents of the trie's storage at `key`. /// Takes modified storage into account. - pub fn storage_at(&self, db: &HashDB, key: &H256) -> TrieResult { + pub fn storage_at(&self, db: &dyn HashDB, key: &H256) -> TrieResult { if let Some(value) = self.cached_storage_at(key) { return Ok(value); } @@ -230,7 +230,7 @@ impl Account { /// Get (and cache) the contents of the trie's storage at `key`. /// Does not take modified storage into account. - pub fn original_storage_at(&self, db: &HashDB, key: &H256) -> TrieResult { + pub fn original_storage_at(&self, db: &dyn HashDB, key: &H256) -> TrieResult { if let Some(value) = self.cached_original_storage_at(key) { return Ok(value); } @@ -252,7 +252,7 @@ impl Account { } } - fn get_and_cache_storage(storage_root: &H256, storage_cache: &mut LruCache, db: &HashDB, key: &H256) -> TrieResult { + fn get_and_cache_storage(storage_root: &H256, storage_cache: &mut LruCache, db: &dyn HashDB, key: &H256) -> TrieResult { let db = SecTrieDB::new(&db, storage_root)?; let panicky_decoder = |bytes:&[u8]| ::rlp::decode(&bytes).expect("decoding db value failed"); let item: U256 = db.get_with(key.as_bytes(), panicky_decoder)?.unwrap_or_else(U256::zero); @@ -358,7 +358,7 @@ impl Account { /// Provide a database to get `code_hash`. Should not be called if it is a contract without code. Returns the cached code, if successful. #[must_use] - pub fn cache_code(&mut self, db: &HashDB) -> Option> { + pub fn cache_code(&mut self, db: &dyn HashDB) -> Option> { // TODO: fill out self.code_cache; trace!("Account::cache_code: ic={}; self.code_hash={:?}, self.code_cache={}", self.is_cached(), self.code_hash, self.code_cache.pretty()); @@ -388,7 +388,7 @@ impl Account { /// Provide a database to get `code_size`. Should not be called if it is a contract without code. Returns whether /// the cache succeeds. #[must_use] - pub fn cache_code_size(&mut self, db: &HashDB) -> bool { + pub fn cache_code_size(&mut self, db: &dyn HashDB) -> bool { // TODO: fill out self.code_cache; trace!("Account::cache_code_size: ic={}; self.code_hash={:?}, self.code_cache={}", self.is_cached(), self.code_hash, self.code_cache.pretty()); self.code_size.is_some() || @@ -482,7 +482,7 @@ impl Account { } /// Commit the `storage_changes` to the backing DB and update `storage_root`. - pub fn commit_storage(&mut self, trie_factory: &TrieFactory, db: &mut HashDB) -> TrieResult<()> { + pub fn commit_storage(&mut self, trie_factory: &TrieFactory, db: &mut dyn HashDB) -> TrieResult<()> { let mut t = trie_factory.from_existing(db, &mut self.storage_root)?; for (k, v) in self.storage_changes.drain() { // cast key and value to trait type, @@ -499,7 +499,7 @@ impl Account { } /// Commit any unsaved code. `code_hash` will always return the hash of the `code_cache` after this. - pub fn commit_code(&mut self, db: &mut HashDB) { + pub fn commit_code(&mut self, db: &mut dyn HashDB) { trace!("Commiting code of {:?} - {:?}, {:?}", self, self.code_filth == Filth::Dirty, self.code_cache.is_empty()); match (self.code_filth == Filth::Dirty, self.code_cache.is_empty()) { (true, true) => { @@ -588,7 +588,7 @@ impl Account { /// trie. /// `storage_key` is the hash of the desired storage key, meaning /// this will only work correctly under a secure trie. - pub fn prove_storage(&self, db: &HashDB, storage_key: H256) -> TrieResult<(Vec, H256)> { + pub fn prove_storage(&self, db: &dyn HashDB, storage_key: H256) -> TrieResult<(Vec, H256)> { let mut recorder = Recorder::new(); let trie = TrieDB::new(&db, &self.storage_root)?; diff --git a/ethcore/src/state/backend.rs b/ethcore/src/state/backend.rs index 11e73edb3ab..9d9a2a9b388 100644 --- a/ethcore/src/state/backend.rs +++ b/ethcore/src/state/backend.rs @@ -36,10 +36,10 @@ use journaldb::AsKeyedHashDB; /// State backend. See module docs for more details. pub trait Backend: Send { /// Treat the backend as a read-only hashdb. - fn as_hash_db(&self) -> &HashDB; + fn as_hash_db(&self) -> &dyn HashDB; /// Treat the backend as a writeable hashdb. - fn as_hash_db_mut(&mut self) -> &mut HashDB; + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB; /// Add an account entry to the cache. fn add_to_account_cache(&mut self, addr: Address, data: Option, modified: bool); @@ -114,13 +114,13 @@ impl HashDB for ProofCheck { } impl AsHashDB for ProofCheck { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } } impl Backend for ProofCheck { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } fn add_to_account_cache(&mut self, _addr: Address, _data: Option, _modified: bool) {} fn cache_code(&self, _hash: H256, _code: Arc>) {} fn get_cached_account(&self, _addr: &Address) -> Option> { None } @@ -146,12 +146,12 @@ pub struct Proving { } impl AsKeyedHashDB for Proving { - fn as_keyed_hash_db(&self) -> &journaldb::KeyedHashDB { self } + fn as_keyed_hash_db(&self) -> &dyn journaldb::KeyedHashDB { self } } impl + Send + Sync> AsHashDB for Proving { - fn as_hash_db(&self) -> &HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } } impl journaldb::KeyedHashDB for Proving { @@ -194,9 +194,9 @@ impl + Send + Sync> HashDB + Send + Sync> Backend for Proving { - fn as_hash_db(&self) -> &HashDB { self } + fn as_hash_db(&self) -> &dyn HashDB { self } - fn as_hash_db_mut(&mut self) -> &mut HashDB { self } + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self } fn add_to_account_cache(&mut self, _: Address, _: Option, _: bool) { } @@ -248,11 +248,11 @@ impl + Clone> Clone for Proving { pub struct Basic(pub H); impl + Send + Sync> Backend for Basic { - fn as_hash_db(&self) -> &HashDB { + fn as_hash_db(&self) -> &dyn HashDB { self.0.as_hash_db() } - fn as_hash_db_mut(&mut self) -> &mut HashDB { + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self.0.as_hash_db_mut() } diff --git a/ethcore/src/state/mod.rs b/ethcore/src/state/mod.rs index d0d57287dce..95e31489c84 100644 --- a/ethcore/src/state/mod.rs +++ b/ethcore/src/state/mod.rs @@ -638,7 +638,7 @@ impl State { &self, address: &Address, key: &H256, f_cached_at: FCachedStorageAt, f_at: FStorageAt, ) -> TrieResult where FCachedStorageAt: Fn(&Account, &H256) -> Option, - FStorageAt: Fn(&Account, &HashDB, &H256) -> TrieResult + FStorageAt: Fn(&Account, &dyn HashDB, &H256) -> TrieResult { // Storage key search and update works like this: // 1. If there's an entry for the account in the local cache check for the key and return it if found. @@ -1101,7 +1101,7 @@ impl State { /// Load required account data from the databases. Returns whether the cache succeeds. #[must_use] - fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &HashDB) -> bool { + fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &dyn HashDB) -> bool { if let RequireCache::None = require { return true; } diff --git a/ethcore/src/state_db.rs b/ethcore/src/state_db.rs index 2613411e4e0..132677ab5cf 100644 --- a/ethcore/src/state_db.rs +++ b/ethcore/src/state_db.rs @@ -107,7 +107,7 @@ struct BlockChanges { /// `StateDB` is propagated into the global cache. pub struct StateDB { /// Backing database. - db: Box, + db: Box, /// Shared canonical state cache. account_cache: Arc>, /// DB Code cache. Maps code hashes to shared bytes. @@ -132,7 +132,7 @@ impl StateDB { /// of the LRU cache in bytes. Actual used memory may (read: will) be higher due to bookkeeping. // TODO: make the cache size actually accurate by moving the account storage cache // into the `AccountCache` structure as its own `LruCache<(Address, H256), H256>`. - pub fn new(db: Box, cache_size: usize) -> StateDB { + pub fn new(db: Box, cache_size: usize) -> StateDB { let bloom = Self::load_bloom(&**db.backing()); let acc_cache_size = cache_size * ACCOUNT_CACHE_RATIO / 100; let code_cache_size = cache_size - acc_cache_size; @@ -156,7 +156,7 @@ impl StateDB { /// Loads accounts bloom from the database /// This bloom is used to handle request for the non-existant account fast - pub fn load_bloom(db: &KeyValueDB) -> Bloom { + pub fn load_bloom(db: &dyn KeyValueDB) -> Bloom { let hash_count_entry = db.get(COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_HASHCOUNT_KEY) .expect("Low-level database error"); @@ -313,12 +313,12 @@ impl StateDB { } /// Conversion method to interpret self as `HashDB` reference - pub fn as_hash_db(&self) -> &HashDB { + pub fn as_hash_db(&self) -> &dyn HashDB { self.db.as_hash_db() } /// Conversion method to interpret self as mutable `HashDB` reference - pub fn as_hash_db_mut(&mut self) -> &mut HashDB { + pub fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self.db.as_hash_db_mut() } @@ -368,7 +368,7 @@ impl StateDB { } /// Returns underlying `JournalDB`. - pub fn journal_db(&self) -> &JournalDB { + pub fn journal_db(&self) -> &dyn JournalDB { &*self.db } @@ -407,9 +407,9 @@ impl StateDB { } impl state::Backend for StateDB { - fn as_hash_db(&self) -> &HashDB { self.db.as_hash_db() } + fn as_hash_db(&self) -> &dyn HashDB { self.db.as_hash_db() } - fn as_hash_db_mut(&mut self) -> &mut HashDB { + fn as_hash_db_mut(&mut self) -> &mut dyn HashDB { self.db.as_hash_db_mut() } diff --git a/ethcore/src/test_helpers.rs b/ethcore/src/test_helpers.rs index e59a41d34b4..6bdaeadf9bd 100644 --- a/ethcore/src/test_helpers.rs +++ b/ethcore/src/test_helpers.rs @@ -267,11 +267,11 @@ struct TestBlockChainDB { _trace_blooms_dir: TempDir, blooms: blooms_db::Database, trace_blooms: blooms_db::Database, - key_value: Arc, + key_value: Arc, } impl BlockChainDB for TestBlockChainDB { - fn key_value(&self) -> &Arc { + fn key_value(&self) -> &Arc { &self.key_value } @@ -285,7 +285,7 @@ impl BlockChainDB for TestBlockChainDB { } /// Creates new test instance of `BlockChainDB` -pub fn new_db() -> Arc { +pub fn new_db() -> Arc { let blooms_dir = TempDir::new("").unwrap(); let trace_blooms_dir = TempDir::new("").unwrap(); @@ -301,7 +301,7 @@ pub fn new_db() -> Arc { } /// Creates a new temporary `BlockChainDB` on FS -pub fn new_temp_db(tempdir: &Path) -> Arc { +pub fn new_temp_db(tempdir: &Path) -> Arc { let blooms_dir = TempDir::new("").unwrap(); let trace_blooms_dir = TempDir::new("").unwrap(); let key_value_dir = tempdir.join("key_value"); @@ -321,7 +321,7 @@ pub fn new_temp_db(tempdir: &Path) -> Arc { } /// Creates new instance of KeyValueDBHandler -pub fn restoration_db_handler(config: kvdb_rocksdb::DatabaseConfig) -> Box { +pub fn restoration_db_handler(config: kvdb_rocksdb::DatabaseConfig) -> Box { struct RestorationDBHandler { config: kvdb_rocksdb::DatabaseConfig, } @@ -329,11 +329,11 @@ pub fn restoration_db_handler(config: kvdb_rocksdb::DatabaseConfig) -> Box, + key_value: Arc, } impl BlockChainDB for RestorationDB { - fn key_value(&self) -> &Arc { + fn key_value(&self) -> &Arc { &self.key_value } @@ -347,7 +347,7 @@ pub fn restoration_db_handler(config: kvdb_rocksdb::DatabaseConfig) -> Box io::Result> { + fn open(&self, db_path: &Path) -> io::Result> { let key_value = Arc::new(kvdb_rocksdb::Database::open(&self.config, &db_path.to_string_lossy())?); let blooms_path = db_path.join("blooms"); let trace_blooms_path = db_path.join("trace_blooms"); diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index 2ee521a4ad6..4c8c2cd7f0e 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -64,7 +64,7 @@ pub struct TraceDB where T: DatabaseExtras { /// hashes of cached traces cache_manager: RwLock>, /// db - db: Arc, + db: Arc, /// tracing enabled enabled: bool, /// extras @@ -73,7 +73,7 @@ pub struct TraceDB where T: DatabaseExtras { impl TraceDB where T: DatabaseExtras { /// Creates new instance of `TraceDB`. - pub fn new(config: Config, db: Arc, extras: Arc) -> Self { + pub fn new(config: Config, db: Arc, extras: Arc) -> Self { let mut batch = DBTransaction::new(); let genesis = extras.block_hash(0) .expect("Genesis block is always inserted upon extras db creation qed"); diff --git a/ethcore/src/verification/canon_verifier.rs b/ethcore/src/verification/canon_verifier.rs index 03a1c7155f8..76f37c19df0 100644 --- a/ethcore/src/verification/canon_verifier.rs +++ b/ethcore/src/verification/canon_verifier.rs @@ -32,7 +32,7 @@ impl Verifier for CanonVerifier { &self, header: &Header, parent: &Header, - engine: &EthEngine, + engine: &dyn EthEngine, do_full: Option>, ) -> Result<(), Error> { verification::verify_block_family(header, parent, engine, do_full) @@ -42,7 +42,7 @@ impl Verifier for CanonVerifier { verification::verify_block_final(expected, got) } - fn verify_block_external(&self, header: &Header, engine: &EthEngine) -> Result<(), Error> { + fn verify_block_external(&self, header: &Header, engine: &dyn EthEngine) -> Result<(), Error> { engine.verify_block_external(header) } } diff --git a/ethcore/src/verification/mod.rs b/ethcore/src/verification/mod.rs index 5546bd60c91..37c721ab1b8 100644 --- a/ethcore/src/verification/mod.rs +++ b/ethcore/src/verification/mod.rs @@ -44,7 +44,7 @@ pub enum VerifierType { } /// Create a new verifier based on type. -pub fn new(v: VerifierType) -> Box> { +pub fn new(v: VerifierType) -> Box> { match v { VerifierType::Canon | VerifierType::CanonNoSeal => Box::new(CanonVerifier), VerifierType::Noop => Box::new(NoopVerifier), diff --git a/ethcore/src/verification/noop_verifier.rs b/ethcore/src/verification/noop_verifier.rs index d68f1eb8856..3b646ed9e40 100644 --- a/ethcore/src/verification/noop_verifier.rs +++ b/ethcore/src/verification/noop_verifier.rs @@ -32,7 +32,7 @@ impl Verifier for NoopVerifier { &self, _: &Header, _t: &Header, - _: &EthEngine, + _: &dyn EthEngine, _: Option> ) -> Result<(), Error> { Ok(()) @@ -42,7 +42,7 @@ impl Verifier for NoopVerifier { Ok(()) } - fn verify_block_external(&self, _header: &Header, _engine: &EthEngine) -> Result<(), Error> { + fn verify_block_external(&self, _header: &Header, _engine: &dyn EthEngine) -> Result<(), Error> { Ok(()) } } diff --git a/ethcore/src/verification/queue/kind.rs b/ethcore/src/verification/queue/kind.rs index d8f4dd4034c..ed96ebc9d0e 100644 --- a/ethcore/src/verification/queue/kind.rs +++ b/ethcore/src/verification/queue/kind.rs @@ -58,10 +58,10 @@ pub trait Kind: 'static + Sized + Send + Sync { type Verified: Sized + Send + BlockLike + HeapSizeOf; /// Attempt to create the `Unverified` item from the input. - fn create(input: Self::Input, engine: &EthEngine, check_seal: bool) -> Result; + fn create(input: Self::Input, engine: &dyn EthEngine, check_seal: bool) -> Result; /// Attempt to verify the `Unverified` item using the given engine. - fn verify(unverified: Self::Unverified, engine: &EthEngine, check_seal: bool) -> Result; + fn verify(unverified: Self::Unverified, engine: &dyn EthEngine, check_seal: bool) -> Result; } /// The blocks verification module. @@ -86,7 +86,7 @@ pub mod blocks { type Unverified = Unverified; type Verified = PreverifiedBlock; - fn create(input: Self::Input, engine: &EthEngine, check_seal: bool) -> Result { + fn create(input: Self::Input, engine: &dyn EthEngine, check_seal: bool) -> Result { match verify_block_basic(&input, engine, check_seal) { Ok(()) => Ok(input), Err(Error::Block(BlockError::TemporarilyInvalid(oob))) => { @@ -100,7 +100,7 @@ pub mod blocks { } } - fn verify(un: Self::Unverified, engine: &EthEngine, check_seal: bool) -> Result { + fn verify(un: Self::Unverified, engine: &dyn EthEngine, check_seal: bool) -> Result { let hash = un.hash(); match verify_block_unordered(un, engine, check_seal) { Ok(verified) => Ok(verified), @@ -209,14 +209,14 @@ pub mod headers { type Unverified = Header; type Verified = Header; - fn create(input: Self::Input, engine: &EthEngine, check_seal: bool) -> Result { + fn create(input: Self::Input, engine: &dyn EthEngine, check_seal: bool) -> Result { match verify_header_params(&input, engine, true, check_seal) { Ok(_) => Ok(input), Err(err) => Err((input, err)) } } - fn verify(unverified: Self::Unverified, engine: &EthEngine, check_seal: bool) -> Result { + fn verify(unverified: Self::Unverified, engine: &dyn EthEngine, check_seal: bool) -> Result { match check_seal { true => engine.verify_block_unordered(&unverified,).map(|_| unverified), false => Ok(unverified), diff --git a/ethcore/src/verification/queue/mod.rs b/ethcore/src/verification/queue/mod.rs index 79801019d3f..3f3484ddc44 100644 --- a/ethcore/src/verification/queue/mod.rs +++ b/ethcore/src/verification/queue/mod.rs @@ -138,7 +138,7 @@ struct Sizes { /// A queue of items to be verified. Sits between network or other I/O and the `BlockChain`. /// Keeps them in the same order as inserted, minus invalid items. pub struct VerificationQueue { - engine: Arc, + engine: Arc, more_to_verify: Arc, verification: Arc>, deleting: Arc, @@ -206,7 +206,7 @@ struct Verification { impl VerificationQueue { /// Creates a new queue instance. - pub fn new(config: Config, engine: Arc, message_channel: IoChannel, check_seal: bool) -> Self { + pub fn new(config: Config, engine: Arc, message_channel: IoChannel, check_seal: bool) -> Self { let verification = Arc::new(Verification { unverified: LenCachingMutex::new(VecDeque::new()), verifying: LenCachingMutex::new(VecDeque::new()), @@ -293,7 +293,7 @@ impl VerificationQueue { fn verify( verification: Arc>, - engine: Arc, + engine: Arc, wait: Arc, ready: Arc, empty: Arc, diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index eff7ae406cc..fa458803cd5 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -64,7 +64,7 @@ impl HeapSizeOf for PreverifiedBlock { } /// Phase 1 quick block verification. Only does checks that are cheap. Operates on a single block -pub fn verify_block_basic(block: &Unverified, engine: &EthEngine, check_seal: bool) -> Result<(), Error> { +pub fn verify_block_basic(block: &Unverified, engine: &dyn EthEngine, check_seal: bool) -> Result<(), Error> { verify_header_params(&block.header, engine, true, check_seal)?; verify_block_integrity(block)?; @@ -89,7 +89,7 @@ pub fn verify_block_basic(block: &Unverified, engine: &EthEngine, check_seal: bo /// Phase 2 verification. Perform costly checks such as transaction signatures and block nonce for ethash. /// Still operates on a individual block /// Returns a `PreverifiedBlock` structure populated with transactions -pub fn verify_block_unordered(block: Unverified, engine: &EthEngine, check_seal: bool) -> Result { +pub fn verify_block_unordered(block: Unverified, engine: &dyn EthEngine, check_seal: bool) -> Result { let header = block.header; if check_seal { engine.verify_block_unordered(&header)?; @@ -131,14 +131,14 @@ pub struct FullFamilyParams<'a, C: BlockInfo + CallContract + 'a> { pub block: &'a PreverifiedBlock, /// Block provider to use during verification - pub block_provider: &'a BlockProvider, + pub block_provider: &'a dyn BlockProvider, /// Engine client to use during verification pub client: &'a C, } /// Phase 3 verification. Check block information against parent and uncles. -pub fn verify_block_family(header: &Header, parent: &Header, engine: &EthEngine, do_full: Option>) -> Result<(), Error> { +pub fn verify_block_family(header: &Header, parent: &Header, engine: &dyn EthEngine, do_full: Option>) -> Result<(), Error> { // TODO: verify timestamp verify_parent(&header, &parent, engine)?; engine.verify_block_family(&header, &parent)?; @@ -159,7 +159,7 @@ pub fn verify_block_family(header: &Header, parent: Ok(()) } -fn verify_uncles(block: &PreverifiedBlock, bc: &BlockProvider, engine: &EthEngine) -> Result<(), Error> { +fn verify_uncles(block: &PreverifiedBlock, bc: &dyn BlockProvider, engine: &dyn EthEngine) -> Result<(), Error> { let header = &block.header; let num_uncles = block.uncles.len(); let max_uncles = engine.maximum_uncle_count(header.number()); @@ -267,7 +267,7 @@ pub fn verify_block_final(expected: &Header, got: &Header) -> Result<(), Error> } /// Check basic header parameters. -pub fn verify_header_params(header: &Header, engine: &EthEngine, is_full: bool, check_seal: bool) -> Result<(), Error> { +pub fn verify_header_params(header: &Header, engine: &dyn EthEngine, is_full: bool, check_seal: bool) -> Result<(), Error> { if check_seal { let expected_seal_fields = engine.seal_fields(header); if header.seal().len() != expected_seal_fields { @@ -326,7 +326,7 @@ pub fn verify_header_params(header: &Header, engine: &EthEngine, is_full: bool, } /// Check header parameters agains parent header. -fn verify_parent(header: &Header, parent: &Header, engine: &EthEngine) -> Result<(), Error> { +fn verify_parent(header: &Header, parent: &Header, engine: &dyn EthEngine) -> Result<(), Error> { assert!(header.parent_hash().is_zero() || &parent.hash() == header.parent_hash(), "Parent hash should already have been verified; qed"); @@ -516,12 +516,12 @@ mod tests { } } - fn basic_test(bytes: &[u8], engine: &EthEngine) -> Result<(), Error> { + fn basic_test(bytes: &[u8], engine: &dyn EthEngine) -> Result<(), Error> { let unverified = Unverified::from_rlp(bytes.to_vec())?; verify_block_basic(&unverified, engine, true) } - fn family_test(bytes: &[u8], engine: &EthEngine, bc: &BC) -> Result<(), Error> where BC: BlockProvider { + fn family_test(bytes: &[u8], engine: &dyn EthEngine, bc: &BC) -> Result<(), Error> where BC: BlockProvider { let block = Unverified::from_rlp(bytes.to_vec()).unwrap(); let header = block.header; let transactions: Vec<_> = block.transactions @@ -547,13 +547,13 @@ mod tests { let full_params = FullFamilyParams { block: &block, - block_provider: bc as &BlockProvider, + block_provider: bc as &dyn BlockProvider, client: &client, }; verify_block_family(&block.header, &parent, engine, Some(full_params)) } - fn unordered_test(bytes: &[u8], engine: &EthEngine) -> Result<(), Error> { + fn unordered_test(bytes: &[u8], engine: &dyn EthEngine) -> Result<(), Error> { let un = Unverified::from_rlp(bytes.to_vec())?; verify_block_unordered(un, engine, false)?; Ok(()) diff --git a/ethcore/src/verification/verifier.rs b/ethcore/src/verification/verifier.rs index f7221dae81d..309e596aa78 100644 --- a/ethcore/src/verification/verifier.rs +++ b/ethcore/src/verification/verifier.rs @@ -32,12 +32,12 @@ pub trait Verifier: Send + Sync &self, header: &Header, parent: &Header, - engine: &EthEngine, + engine: &dyn EthEngine, do_full: Option> ) -> Result<(), Error>; /// Do a final verification check for an enacted header vs its expected counterpart. fn verify_block_final(&self, expected: &Header, got: &Header) -> Result<(), Error>; /// Verify a block, inspecting external state. - fn verify_block_external(&self, header: &Header, engine: &EthEngine) -> Result<(), Error>; + fn verify_block_external(&self, header: &Header, engine: &dyn EthEngine) -> Result<(), Error>; } diff --git a/ethcore/sync/src/api.rs b/ethcore/sync/src/api.rs index ddb7542b749..7aa9e71ae0a 100644 --- a/ethcore/sync/src/api.rs +++ b/ethcore/sync/src/api.rs @@ -22,7 +22,7 @@ use std::time::Duration; use bytes::Bytes; use devp2p::NetworkService; use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId, - NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ErrorKind, + NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ConnectionFilter}; use network::client_version::ClientVersion; @@ -593,7 +593,7 @@ impl ChainNotify for EthSync { match self.network.start() { Err((err, listen_address)) => { match err.into() { - ErrorKind::Io(ref e) if e.kind() == io::ErrorKind::AddrInUse => { + Error::Io(ref e) if e.kind() == io::ErrorKind::AddrInUse => { warn!("Network port {:?} is already in use, make sure that another instance of an Ethereum client is not running or change the port using the --port option.", listen_address.expect("Listen address is not set.")) }, err => warn!("Error starting network: {}", err), @@ -983,7 +983,7 @@ impl ManageNetwork for LightSync { match self.network.start() { Err((err, listen_address)) => { match err.into() { - ErrorKind::Io(ref e) if e.kind() == io::ErrorKind::AddrInUse => { + Error::Io(ref e) if e.kind() == io::ErrorKind::AddrInUse => { warn!("Network port {:?} is already in use, make sure that another instance of an Ethereum client is not running or change the port using the --port option.", listen_address.expect("Listen address is not set.")) }, err => warn!("Error starting network: {}", err), diff --git a/ethcore/sync/src/blocks.rs b/ethcore/sync/src/blocks.rs index 7d9ad428a93..2ce2be33348 100644 --- a/ethcore/sync/src/blocks.rs +++ b/ethcore/sync/src/blocks.rs @@ -435,13 +435,13 @@ impl BlockCollection { }, None => { warn!("Got body with no header {}", h); - Err(network::ErrorKind::BadProtocol.into()) + Err(network::Error::BadProtocol) } } } None => { trace!(target: "sync", "Ignored unknown/stale block body. tx_root = {:?}, uncles = {:?}", header_id.transactions_root, header_id.uncles); - Err(network::ErrorKind::BadProtocol.into()) + Err(network::Error::BadProtocol) } } } @@ -463,7 +463,7 @@ impl BlockCollection { }, None => { warn!("Got receipt with no header {}", h); - return Err(network::ErrorKind::BadProtocol.into()) + return Err(network::Error::BadProtocol) } } } @@ -471,7 +471,7 @@ impl BlockCollection { }, hash_map::Entry::Vacant(_) => { trace!(target: "sync", "Ignored unknown/stale block receipt {:?}", receipt_root); - Err(network::ErrorKind::BadProtocol.into()) + Err(network::Error::BadProtocol) } } } diff --git a/ethcore/sync/src/lib.rs b/ethcore/sync/src/lib.rs index aecf8fbb82c..8ed8befc87a 100644 --- a/ethcore/sync/src/lib.rs +++ b/ethcore/sync/src/lib.rs @@ -76,5 +76,5 @@ mod api; pub use api::*; pub use chain::{SyncStatus, SyncState}; pub use devp2p::validate_node_url; -pub use network::{NonReservedPeerMode, Error, ErrorKind, ConnectionFilter, ConnectionDirection}; +pub use network::{NonReservedPeerMode, Error, ConnectionFilter, ConnectionDirection}; pub use private_tx::{PrivateTxHandler, NoopPrivateTxHandler, SimplePrivateTxHandler}; diff --git a/ethcore/sync/src/tests/snapshot.rs b/ethcore/sync/src/tests/snapshot.rs index 60459defb5a..f7a947329aa 100644 --- a/ethcore/sync/src/tests/snapshot.rs +++ b/ethcore/sync/src/tests/snapshot.rs @@ -122,6 +122,8 @@ impl SnapshotService for TestSnapshotService { self.block_restoration_chunks.lock().clear(); } + fn abort_snapshot(&self) {} + fn restore_state_chunk(&self, hash: H256, chunk: Bytes) { if self.restoration_manifest.lock().as_ref().map_or(false, |m| m.state_hashes.iter().any(|h| h == &hash)) { self.state_restoration_chunks.lock().insert(hash, chunk); diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 5babec09871..99f7a293b55 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -41,8 +41,6 @@ extern crate ethabi_contract; #[macro_use] extern crate ethabi_derive; #[macro_use] -extern crate error_chain; -#[macro_use] extern crate log; #[macro_use] extern crate serde_derive; diff --git a/miner/src/pool/verifier.rs b/miner/src/pool/verifier.rs index 1fded37630e..5c8273dd577 100644 --- a/miner/src/pool/verifier.rs +++ b/miner/src/pool/verifier.rs @@ -168,7 +168,7 @@ impl txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier signed.into(), Err(err) => { debug!(target: "txqueue", "[{:?}] Rejected tx {:?}", hash, err); - bail!(err) + return Err(err) }, }, Transaction::Local(tx) => tx, @@ -256,7 +256,7 @@ impl txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier txpool::Verifier for Verifier continue, - Some(sync::ErrorKind::AddressResolve(_)) => return Err(format!("Failed to resolve hostname of a boot node: {}", line)), + Some(sync::Error::AddressResolve(_)) => return Err(format!("Failed to resolve hostname of a boot node: {}", line)), Some(_) => return Err(format!("Invalid node address format given for a boot node: {}", line)), } } @@ -934,7 +934,7 @@ impl Configuration { no_periodic: self.args.flag_no_periodic_snapshot, processing_threads: match self.args.arg_snapshot_threads { Some(threads) if threads > 0 => threads, - _ => ::std::cmp::max(1, num_cpus::get() / 2), + _ => ::std::cmp::max(1, num_cpus::get_physical() / 2), }, }; diff --git a/parity/helpers.rs b/parity/helpers.rs index 2f0046f228d..61947d6bf53 100644 --- a/parity/helpers.rs +++ b/parity/helpers.rs @@ -186,7 +186,7 @@ pub fn to_bootnodes(bootnodes: &Option) -> Result, String> { Some(ref x) if !x.is_empty() => x.split(',').map(|s| { match validate_node_url(s).map(Into::into) { None => Ok(s.to_owned()), - Some(sync::ErrorKind::AddressResolve(_)) => Err(format!("Failed to resolve hostname of a boot node: {}", s)), + Some(sync::Error::AddressResolve(_)) => Err(format!("Failed to resolve hostname of a boot node: {}", s)), Some(_) => Err(format!("Invalid node address format given for a boot node: {}", s)), } }).collect(), diff --git a/parity/lib.rs b/parity/lib.rs index 7d1bf108b79..9dbc8c6eafb 100644 --- a/parity/lib.rs +++ b/parity/lib.rs @@ -15,7 +15,6 @@ // along with Parity Ethereum. If not, see . //! Ethcore client application. - #![warn(missing_docs)] extern crate ansi_term; diff --git a/parity/run.rs b/parity/run.rs index c21399290b8..0652e8f9d99 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -900,17 +900,27 @@ impl RunningClient { // Create a weak reference to the client so that we can wait on shutdown // until it is dropped let weak_client = Arc::downgrade(&client); - // Shutdown and drop the ServiceClient + // Shutdown and drop the ClientService client_service.shutdown(); + trace!(target: "shutdown", "ClientService shut down"); drop(client_service); + trace!(target: "shutdown", "ClientService dropped"); // drop this stuff as soon as exit detected. drop(rpc); + trace!(target: "shutdown", "RPC dropped"); drop(keep_alive); + trace!(target: "shutdown", "KeepAlive dropped"); // to make sure timer does not spawn requests while shutdown is in progress informant.shutdown(); + trace!(target: "shutdown", "Informant shut down"); // just Arc is dropping here, to allow other reference release in its default time drop(informant); + trace!(target: "shutdown", "Informant dropped"); drop(client); + trace!(target: "shutdown", "Client dropped"); + // This may help when debugging ref cycles. Requires nightly-only `#![feature(weak_counts)]` + // trace!(target: "shutdown", "Waiting for refs to Client to shutdown, strong_count={:?}, weak_count={:?}", weak_client.strong_count(), weak_client.weak_count()); + trace!(target: "shutdown", "Waiting for refs to Client to shutdown"); wait_for_drop(weak_client); } } @@ -944,24 +954,30 @@ fn print_running_environment(data_dir: &str, dirs: &Directories, db_dirs: &Datab } fn wait_for_drop(w: Weak) { - let sleep_duration = Duration::from_secs(1); - let warn_timeout = Duration::from_secs(60); - let max_timeout = Duration::from_secs(300); + const SLEEP_DURATION: Duration = Duration::from_secs(1); + const WARN_TIMEOUT: Duration = Duration::from_secs(60); + const MAX_TIMEOUT: Duration = Duration::from_secs(300); let instant = Instant::now(); let mut warned = false; - while instant.elapsed() < max_timeout { + while instant.elapsed() < MAX_TIMEOUT { if w.upgrade().is_none() { return; } - if !warned && instant.elapsed() > warn_timeout { + if !warned && instant.elapsed() > WARN_TIMEOUT { warned = true; warn!("Shutdown is taking longer than expected."); } - thread::sleep(sleep_duration); + thread::sleep(SLEEP_DURATION); + + // When debugging shutdown issues on a nightly build it can help to enable this with the + // `#![feature(weak_counts)]` added to lib.rs (TODO: enable when + // https://github.com/rust-lang/rust/issues/57977 is stable) + // trace!(target: "shutdown", "Waiting for client to drop, strong_count={:?}, weak_count={:?}", w.strong_count(), w.weak_count()); + trace!(target: "shutdown", "Waiting for client to drop"); } warn!("Shutdown timeout reached, exiting uncleanly."); diff --git a/parity/snapshot.rs b/parity/snapshot.rs index 70957762f5a..269965c3355 100644 --- a/parity/snapshot.rs +++ b/parity/snapshot.rs @@ -261,7 +261,7 @@ impl SnapshotCommand { let cur_size = p.size(); if cur_size != last_size { last_size = cur_size; - let bytes = ::informant::format_bytes(p.size()); + let bytes = ::informant::format_bytes(cur_size as usize); info!("Snapshot: {} accounts {} blocks {}", p.accounts(), p.blocks(), bytes); } diff --git a/rpc/src/v1/helpers/errors.rs b/rpc/src/v1/helpers/errors.rs index 023f7df3ccc..3365c5490a0 100644 --- a/rpc/src/v1/helpers/errors.rs +++ b/rpc/src/v1/helpers/errors.rs @@ -25,7 +25,7 @@ use rlp::DecoderError; use types::transaction::Error as TransactionError; use ethcore_private_tx::Error as PrivateTransactionError; use vm::Error as VMError; -use light::on_demand::error::{Error as OnDemandError, ErrorKind as OnDemandErrorKind}; +use light::on_demand::error::{Error as OnDemandError}; use ethcore::client::BlockChainClient; use types::blockchain_info::BlockChainInfo; use v1::types::BlockNumber; @@ -555,10 +555,9 @@ pub fn filter_block_not_found(id: BlockId) -> Error { pub fn on_demand_error(err: OnDemandError) -> Error { match err { - OnDemandError(OnDemandErrorKind::ChannelCanceled(e), _) => on_demand_cancel(e), - OnDemandError(OnDemandErrorKind::RequestLimit, _) => timeout_new_peer(&err), - OnDemandError(OnDemandErrorKind::BadResponse(_), _) => max_attempts_reached(&err), - _ => on_demand_others(&err), + OnDemandError::ChannelCanceled(e) => on_demand_cancel(e), + OnDemandError::RequestLimit => timeout_new_peer(&err), + OnDemandError::BadResponse(_) => max_attempts_reached(&err), } } @@ -583,14 +582,6 @@ pub fn timeout_new_peer(err: &OnDemandError) -> Error { } } -pub fn on_demand_others(err: &OnDemandError) -> Error { - Error { - code: ErrorCode::ServerError(codes::UNKNOWN_ERROR), - message: err.to_string(), - data: None, - } -} - pub fn status_error(has_peers: bool) -> Error { if has_peers { no_work() diff --git a/rpc/src/v1/tests/helpers/snapshot_service.rs b/rpc/src/v1/tests/helpers/snapshot_service.rs index 5450886bb42..881c434e178 100644 --- a/rpc/src/v1/tests/helpers/snapshot_service.rs +++ b/rpc/src/v1/tests/helpers/snapshot_service.rs @@ -48,6 +48,7 @@ impl SnapshotService for TestSnapshotService { fn status(&self) -> RestorationStatus { self.status.lock().clone() } fn begin_restore(&self, _manifest: ManifestData) { } fn abort_restore(&self) { } + fn abort_snapshot(&self) {} fn restore_state_chunk(&self, _hash: H256, _chunk: Bytes) { } fn restore_block_chunk(&self, _hash: H256, _chunk: Bytes) { } fn shutdown(&self) { } diff --git a/scripts/docker/hub/publish-docker.sh b/scripts/docker/hub/publish-docker.sh index 6602d55c23a..84feedb2817 100755 --- a/scripts/docker/hub/publish-docker.sh +++ b/scripts/docker/hub/publish-docker.sh @@ -3,7 +3,9 @@ set -e # fail on any error VERSION=$(cat ./tools/VERSION) +TRACK=$(cat ./tools/TRACK) echo "Parity Ethereum version = ${VERSION}" +echo "Parity Ethereum track = ${TRACK}" test "$Docker_Hub_User_Parity" -a "$Docker_Hub_Pass_Parity" \ || ( echo "no docker credentials provided"; exit 1 ) @@ -44,6 +46,14 @@ case "${SCHEDULE_TAG:-${CI_COMMIT_REF_NAME}}" in --file tools/Dockerfile .; docker push "parity/parity:${VERSION}-${CI_COMMIT_REF_NAME}"; docker push "parity/parity:stable";; + v[0-9]*.[0-9]*) + echo "Docker TAG - 'parity/parity:${VERSION}-${TRACK}'" + docker build --no-cache \ + --build-arg VCS_REF="${CI_COMMIT_SHA}" \ + --build-arg BUILD_DATE="$(date -u '+%Y-%m-%dT%H:%M:%SZ')" \ + --tag "parity/parity:${VERSION}-${TRACK}" \ + --file tools/Dockerfile .; + docker push "parity/parity:${VERSION}-${TRACK}";; *) echo "Docker TAG - 'parity/parity:${VERSION}-${CI_COMMIT_REF_NAME}'" docker build --no-cache \ diff --git a/scripts/gitlab/build-linux.sh b/scripts/gitlab/build-linux.sh index ebd65cd9b39..8f3e6f14ef5 100755 --- a/scripts/gitlab/build-linux.sh +++ b/scripts/gitlab/build-linux.sh @@ -11,15 +11,18 @@ echo "CC: " $CC echo "CXX: " $CXX #strip ON export RUSTFLAGS=" -C link-arg=-s" -# Linker for crosscomile -echo "_____ Linker _____" -cat .cargo/config echo "_____ Building target: "$CARGO_TARGET" _____" if [ "${CARGO_TARGET}" = "armv7-linux-androideabi" ] then time cargo build --target $CARGO_TARGET --verbose --color=always --release -p parity-clib --features final else + if [ "${CARGO_TARGET}" = "x86_64-unknown-linux-gnu" ] || [ "${CARGO_TARGET}" = "x86_64-apple-darwin" ] + then + # NOTE: Enables the aes-ni instructions for RustCrypto dependency. + # If you change this please remember to also update .cargo/config + export RUSTFLAGS="$RUSTFLAGS -Ctarget-feature=+aes,+sse2,+ssse3" + fi time cargo build --target $CARGO_TARGET --verbose --color=always --release --features final time cargo build --target $CARGO_TARGET --verbose --color=always --release -p evmbin time cargo build --target $CARGO_TARGET --verbose --color=always --release -p ethstore-cli diff --git a/scripts/gitlab/build-windows.sh b/scripts/gitlab/build-windows.sh index 76332124d1d..3c32e866aaa 100755 --- a/scripts/gitlab/build-windows.sh +++ b/scripts/gitlab/build-windows.sh @@ -14,6 +14,10 @@ echo "RUSTC_WRAPPER: " $RUSTC_WRAPPER echo "SCCACHE_DIR: " $SCCACHE_DIR echo "_____ Building target: "$CARGO_TARGET" _____" + # NOTE: Enables the aes-ni instructions for RustCrypto dependency. + # If you change this please remember to also update .cargo/config +export RUSTFLAGS=" -Ctarget-feature=+aes,+sse2,+ssse3 -Ctarget-feature=+crt-static" + time cargo build --target $CARGO_TARGET --verbose --release --features final time cargo build --target $CARGO_TARGET --verbose --release -p evmbin time cargo build --target $CARGO_TARGET --verbose --release -p ethstore-cli diff --git a/scripts/gitlab/test-linux.sh b/scripts/gitlab/test-linux.sh index aac007ea63e..2ad2ab9be28 100755 --- a/scripts/gitlab/test-linux.sh +++ b/scripts/gitlab/test-linux.sh @@ -15,4 +15,4 @@ rustup show echo "________Running Parity Full Test Suite________" # Why are we using RUSTFLAGS? See https://github.com/paritytech/parity-ethereum/pull/10719 -CARGO_INCREMENTAL=0 RUSTFLAGS="-C opt-level=3 -C overflow-checks=on -C debuginfo=2" time cargo test $OPTIONS --features "$FEATURES" --locked --all --target $CARGO_TARGET --verbose --color=never -- --test-threads $THREADS +CARGO_INCREMENTAL=0 RUSTFLAGS="-C opt-level=3 -C overflow-checks=on -C debuginfo=2 -Ctarget-feature=+aes,+sse2,+ssse3" time cargo test $OPTIONS --features "$FEATURES" --locked --all --target $CARGO_TARGET --verbose --color=never -- --test-threads $THREADS diff --git a/secret-store/src/key_storage.rs b/secret-store/src/key_storage.rs index a75c94de62d..8ba36f708d2 100644 --- a/secret-store/src/key_storage.rs +++ b/secret-store/src/key_storage.rs @@ -19,7 +19,7 @@ use std::sync::Arc; use serde_json; use tiny_keccak::Keccak; use ethereum_types::{H256, Address}; -use ethkey::{Secret, Public, public_to_address}; +use ethkey::{Secret, Public}; use kvdb::KeyValueDB; use types::{Error, ServerKeyId, NodeId}; use serialization::{SerializablePublic, SerializableSecret, SerializableH256, SerializableAddress}; @@ -28,10 +28,6 @@ use serialization::{SerializablePublic, SerializableSecret, SerializableH256, Se const DB_META_KEY_VERSION: &'static [u8; 7] = b"version"; /// Current db version. const CURRENT_VERSION: u8 = 3; -/// Current type of serialized key shares. -type CurrentSerializableDocumentKeyShare = SerializableDocumentKeyShareV3; -/// Current type of serialized key shares versions. -type CurrentSerializableDocumentKeyVersion = SerializableDocumentKeyShareVersionV3; /// Encrypted key share, stored by key storage on the single key server. #[derive(Debug, Default, Clone, PartialEq)] @@ -89,43 +85,11 @@ pub struct PersistentKeyStorageIterator<'a> { iter: Box, Box<[u8]>)> + 'a>, } -/// V0 of encrypted key share, as it is stored by key storage on the single key server. -#[derive(Serialize, Deserialize)] -pub struct SerializableDocumentKeyShareV0 { - /// Decryption threshold (at least threshold + 1 nodes are required to decrypt data). - pub threshold: usize, - /// Nodes ids numbers. - pub id_numbers: BTreeMap, - /// Node secret share. - pub secret_share: SerializableSecret, - /// Common (shared) encryption point. - pub common_point: SerializablePublic, - /// Encrypted point. - pub encrypted_point: SerializablePublic, -} - -/// V1 of encrypted key share, as it is stored by key storage on the single key server. -#[derive(Serialize, Deserialize)] -struct SerializableDocumentKeyShareV1 { - /// Author of the entry. - pub author: SerializablePublic, - /// Decryption threshold (at least threshold + 1 nodes are required to decrypt data). - pub threshold: usize, - /// Nodes ids numbers. - pub id_numbers: BTreeMap, - /// Node secret share. - pub secret_share: SerializableSecret, - /// Common (shared) encryption point. - pub common_point: Option, - /// Encrypted point. - pub encrypted_point: Option, -} - -/// V2 of encrypted key share, as it is stored by key storage on the single key server. +/// V3 of encrypted key share, as it is stored by key storage on the single key server. #[derive(Serialize, Deserialize)] -struct SerializableDocumentKeyShareV2 { +struct SerializableDocumentKeyShareV3 { /// Author of the entry. - pub author: SerializablePublic, + pub author: SerializableAddress, /// Decryption threshold (at least threshold + 1 nodes are required to decrypt data). pub threshold: usize, /// Server public. @@ -135,12 +99,12 @@ struct SerializableDocumentKeyShareV2 { /// Encrypted point. pub encrypted_point: Option, /// Versions. - pub versions: Vec + pub versions: Vec } -/// V2 of encrypted key share version, as it is stored by key storage on the single key server. +/// V3 of encrypted key share version, as it is stored by key storage on the single key server. #[derive(Serialize, Deserialize)] -struct SerializableDocumentKeyShareVersionV2 { +struct SerializableDocumentKeyShareVersionV3 { /// Version hash. pub hash: SerializableH256, /// Nodes ids numbers. @@ -149,26 +113,6 @@ struct SerializableDocumentKeyShareVersionV2 { pub secret_share: SerializableSecret, } -/// V3 of encrypted key share, as it is stored by key storage on the single key server. -#[derive(Serialize, Deserialize)] -struct SerializableDocumentKeyShareV3 { - /// Author of the entry. - pub author: SerializableAddress, - /// Decryption threshold (at least threshold + 1 nodes are required to decrypt data). - pub threshold: usize, - /// Server public. - pub public: SerializablePublic, - /// Common (shared) encryption point. - pub common_point: Option, - /// Encrypted point. - pub encrypted_point: Option, - /// Versions. - pub versions: Vec -} - -/// V3 of encrypted key share version, as it is stored by key storage on the single key server. -type SerializableDocumentKeyShareVersionV3 = SerializableDocumentKeyShareVersionV2; - impl PersistentKeyStorage { /// Create new persistent document encryption keys storage pub fn new(db: Arc) -> Result { @@ -182,83 +126,22 @@ impl PersistentKeyStorage { fn upgrade_db(db: Arc) -> Result, Error> { let version = db.get(None, DB_META_KEY_VERSION)?; - let version = version.and_then(|v| v.get(0).cloned()).unwrap_or(0); + let version = version.and_then(|v| v.get(0).cloned()); match version { - 0 => { + None => { let mut batch = db.transaction(); batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]); - for (db_key, db_value) in db.iter(None).into_iter().filter(|&(ref k, _)| **k != *DB_META_KEY_VERSION) { - let v0_key = serde_json::from_slice::(&db_value).map_err(|e| Error::Database(e.to_string()))?; - let current_key = CurrentSerializableDocumentKeyShare { - // author is used in separate generation + encrypt sessions. - // in v0 there have been only simultaneous GenEnc sessions. - author: Address::zero().into(), // added in v1 - threshold: v0_key.threshold, - public: Public::default().into(), // addded in v2 - common_point: Some(v0_key.common_point), - encrypted_point: Some(v0_key.encrypted_point), - versions: vec![CurrentSerializableDocumentKeyVersion { - hash: DocumentKeyShareVersion::data_hash(v0_key.id_numbers.iter().map(|(k, v)| (k.as_bytes(), v.as_bytes()))).into(), - id_numbers: v0_key.id_numbers, - secret_share: v0_key.secret_share, - }], - }; - let db_value = serde_json::to_vec(¤t_key).map_err(|e| Error::Database(e.to_string()))?; - batch.put(None, &*db_key, &*db_value); - } db.write(batch)?; Ok(db) }, - 1 => { - let mut batch = db.transaction(); - batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]); - for (db_key, db_value) in db.iter(None).into_iter().filter(|&(ref k, _)| **k != *DB_META_KEY_VERSION) { - let v1_key = serde_json::from_slice::(&db_value).map_err(|e| Error::Database(e.to_string()))?; - let current_key = CurrentSerializableDocumentKeyShare { - author: public_to_address(&v1_key.author).into(), // added in v1 + changed in v3 - threshold: v1_key.threshold, - public: Public::default().into(), // addded in v2 - common_point: v1_key.common_point, - encrypted_point: v1_key.encrypted_point, - versions: vec![CurrentSerializableDocumentKeyVersion { - hash: DocumentKeyShareVersion::data_hash(v1_key.id_numbers.iter().map(|(k, v)| (k.as_bytes(), v.as_bytes()))).into(), - id_numbers: v1_key.id_numbers, - secret_share: v1_key.secret_share, - }], - }; - let db_value = serde_json::to_vec(¤t_key).map_err(|e| Error::Database(e.to_string()))?; - batch.put(None, &*db_key, &*db_value); - } - db.write(batch)?; - Ok(db) - } - 2 => { - let mut batch = db.transaction(); - batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]); - for (db_key, db_value) in db.iter(None).into_iter().filter(|&(ref k, _)| **k != *DB_META_KEY_VERSION) { - let v2_key = serde_json::from_slice::(&db_value).map_err(|e| Error::Database(e.to_string()))?; - let current_key = CurrentSerializableDocumentKeyShare { - author: public_to_address(&v2_key.author).into(), // changed in v3 - threshold: v2_key.threshold, - public: v2_key.public, - common_point: v2_key.common_point, - encrypted_point: v2_key.encrypted_point, - versions: v2_key.versions, - }; - let db_value = serde_json::to_vec(¤t_key).map_err(|e| Error::Database(e.to_string()))?; - batch.put(None, &*db_key, &*db_value); - } - db.write(batch)?; - Ok(db) - }, - 3 => Ok(db), - _ => Err(Error::Database(format!("unsupported SecretStore database version: {}", version))), + Some(CURRENT_VERSION) => Ok(db), + _ => Err(Error::Database(format!("unsupported SecretStore database version: {:?}", version))), } } impl KeyStorage for PersistentKeyStorage { fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> { - let key: CurrentSerializableDocumentKeyShare = key.into(); + let key: SerializableDocumentKeyShareV3 = key.into(); let key = serde_json::to_vec(&key).map_err(|e| Error::Database(e.to_string()))?; let mut batch = self.db.transaction(); batch.put(None, document.as_bytes(), &key); @@ -274,7 +157,7 @@ impl KeyStorage for PersistentKeyStorage { .map_err(|e| Error::Database(e.to_string())) .and_then(|key| match key { None => Ok(None), - Some(key) => serde_json::from_slice::(&key) + Some(key) => serde_json::from_slice::(&key) .map_err(|e| Error::Database(e.to_string())) .map(Into::into) .map(Some), @@ -314,7 +197,7 @@ impl<'a> Iterator for PersistentKeyStorageIterator<'a> { fn next(&mut self) -> Option<(ServerKeyId, DocumentKeyShare)> { self.iter.as_mut().next() - .and_then(|(db_key, db_val)| serde_json::from_slice::(&db_val) + .and_then(|(db_key, db_val)| serde_json::from_slice::(&db_val) .ok() .map(|key| (ServerKeyId::from_slice(&*db_key), key.into()))) } @@ -411,17 +294,12 @@ pub mod tests { use std::collections::HashMap; use std::sync::Arc; - use std::str::FromStr; use parking_lot::RwLock; - use serde_json; use self::tempdir::TempDir; - use ethereum_types::{Address, H256, H512}; - use ethkey::{Random, Generator, Public, Secret, public_to_address}; + use ethkey::{Random, Generator, Public}; use kvdb_rocksdb::Database; use types::{Error, ServerKeyId}; - use super::{DB_META_KEY_VERSION, CURRENT_VERSION, KeyStorage, PersistentKeyStorage, DocumentKeyShare, - DocumentKeyShareVersion, CurrentSerializableDocumentKeyShare, upgrade_db, SerializableDocumentKeyShareV0, - SerializableDocumentKeyShareV1, SerializableDocumentKeyShareV2, SerializableDocumentKeyShareVersionV2}; + use super::{KeyStorage, PersistentKeyStorage, DocumentKeyShare, DocumentKeyShareVersion}; /// In-memory document encryption keys storage #[derive(Default)] @@ -515,137 +393,4 @@ pub mod tests { assert_eq!(key_storage.get(&key2), Ok(Some(value2))); assert_eq!(key_storage.get(&key3), Ok(None)); } - - #[test] - fn upgrade_db_from_0() { - let tempdir = TempDir::new("").unwrap(); - let db = Database::open_default(&tempdir.path().display().to_string()).unwrap(); - - // prepare v0 database - { - let key = serde_json::to_vec(&SerializableDocumentKeyShareV0 { - threshold: 777, - id_numbers: vec![( - H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(), - "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap().into(), - )].into_iter().collect(), - secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::().unwrap().into(), - common_point: H512::from_str("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc").unwrap().into(), - encrypted_point: H512::from_str("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c").unwrap().into(), - }).unwrap(); - let mut batch = db.transaction(); - batch.put(None, &[7], &key); - db.write(batch).unwrap(); - } - - // upgrade database - let db = upgrade_db(Arc::new(db)).unwrap(); - - // check upgrade - assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], CURRENT_VERSION); - let key = serde_json::from_slice::(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap(); - assert_eq!(Address::zero(), key.author.clone().into()); - assert_eq!(777, key.threshold); - assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::().unwrap()), key.common_point.clone().map(Into::into)); - assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::().unwrap()), key.encrypted_point.clone().map(Into::into)); - - assert_eq!(key.versions.len(), 1); - assert_eq!(vec![( - "b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::().unwrap(), - "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap(), - )], key.versions[0].id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::>()); - assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::().unwrap(), key.versions[0].secret_share.clone().into()); - } - - #[test] - fn upgrade_db_from_1() { - let tempdir = TempDir::new("").unwrap(); - let db = Database::open_default(&tempdir.path().display().to_string()).unwrap(); - - // prepare v1 database - { - let key = serde_json::to_vec(&SerializableDocumentKeyShareV1 { - author: H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(), - threshold: 777, - id_numbers: vec![( - H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(), - "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap().into(), - )].into_iter().collect(), - secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::().unwrap().into(), - common_point: Some(H512::from_str("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc").unwrap().into()), - encrypted_point: Some(H512::from_str("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c").unwrap().into()), - }).unwrap(); - let mut batch = db.transaction(); - batch.put(None, DB_META_KEY_VERSION, &[1]); - batch.put(None, &[7], &key); - db.write(batch).unwrap(); - } - - // upgrade database - let db = upgrade_db(Arc::new(db)).unwrap(); - - // check upgrade - assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], CURRENT_VERSION); - let key = serde_json::from_slice::(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap(); - assert_eq!(777, key.threshold); - assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::().unwrap()), key.common_point.clone().map(Into::into)); - assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::().unwrap()), key.encrypted_point.clone().map(Into::into)); - assert_eq!(key.author.0, public_to_address(&H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap())); - - assert_eq!(key.versions.len(), 1); - assert_eq!(vec![( - "b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::().unwrap(), - "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap(), - )], key.versions[0].id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::>()); - - assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::().unwrap(), key.versions[0].secret_share.clone().into()); - } - - #[test] - fn upgrade_db_from_2() { - let tempdir = TempDir::new("").unwrap(); - let db = Database::open_default(&tempdir.path().display().to_string()).unwrap(); - - // prepare v2 database - { - let key = serde_json::to_vec(&SerializableDocumentKeyShareV2 { - author: H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(), - threshold: 777, - common_point: Some(H512::from_str("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc").unwrap().into()), - encrypted_point: Some(H512::from_str("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c").unwrap().into()), - public: H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(), - versions: vec![SerializableDocumentKeyShareVersionV2 { - hash: "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap().into(), - id_numbers: vec![( - H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(), - "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap().into(), - )].into_iter().collect(), - secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::().unwrap().into(), - }], - }).unwrap(); - let mut batch = db.transaction(); - batch.put(None, DB_META_KEY_VERSION, &[2]); - batch.put(None, &[7], &key); - db.write(batch).unwrap(); - } - - // upgrade database - let db = upgrade_db(Arc::new(db)).unwrap(); - - // check upgrade - assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], CURRENT_VERSION); - let key = serde_json::from_slice::(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap(); - assert_eq!(777, key.threshold); - assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::().unwrap()), key.common_point.clone().map(Into::into)); - assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::().unwrap()), key.encrypted_point.clone().map(Into::into)); - assert_eq!(key.author.0, public_to_address(&"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse().unwrap())); - - assert_eq!(key.versions.len(), 1); - assert_eq!(vec![( - "b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::().unwrap(), - "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::().unwrap(), - )], key.versions[0].id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::>()); - - assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::().unwrap(), key.versions[0].secret_share.clone().into()); - } } diff --git a/updater/src/updater.rs b/updater/src/updater.rs index 27daac4edb8..6e84f8281e9 100644 --- a/updater/src/updater.rs +++ b/updater/src/updater.rs @@ -159,7 +159,7 @@ pub struct Updater H256 { + let mut bytes = s.as_bytes().to_vec(); + bytes.resize(H256::len_bytes(), 0); + H256::from_slice(&bytes) } /// Client trait for getting latest release information from operations contract. @@ -1253,4 +1262,11 @@ pub mod tests { // and since our update policy requires consensus, the client should be disabled assert!(client.is_disabled()); } + + #[test] + fn static_hashes_do_not_panic() { + let client_id_hash: H256 = *CLIENT_ID_HASH; + assert_eq!(&format!("{:x}", client_id_hash), "7061726974790000000000000000000000000000000000000000000000000000"); + let _: H256 = *PLATFORM_ID_HASH; + } } diff --git a/util/network-devp2p/Cargo.toml b/util/network-devp2p/Cargo.toml index 95a39831f23..db87f04e86e 100644 --- a/util/network-devp2p/Cargo.toml +++ b/util/network-devp2p/Cargo.toml @@ -5,6 +5,7 @@ license = "GPL-3.0" name = "ethcore-network-devp2p" version = "1.12.0" authors = ["Parity Technologies "] +edition = "2018" [dependencies] log = "0.4" @@ -12,7 +13,6 @@ mio = "0.6.8" bytes = "0.4" rand = "0.6" tiny-keccak = "1.4" -rust-crypto = "0.2.34" slab = "0.2" igd = "0.7" libc = "0.2.7" @@ -21,8 +21,8 @@ ansi_term = "0.10" rustc-hex = "1.0" ethcore-io = { path = "../io", features = ["mio"] } parity-bytes = "0.1" -parity-crypto = "0.4.0" -ethcore-network = { path = "../network" } +crypto = { package = "parity-crypto", version = "0.4.0"} +network = { package = "ethcore-network", path = "../network" } ethereum-types = "0.6.0" ethkey = { path = "../../accounts/ethkey" } rlp = "0.4.0" @@ -30,9 +30,8 @@ parity-path = "0.1" ipnetwork = "0.12.6" keccak-hash = "0.2.0" parity-snappy = "0.1" -serde = "1.0" +serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -serde_derive = "1.0" error-chain = { version = "0.12", default-features = false } lru-cache = "0.1" diff --git a/util/network-devp2p/src/connection.rs b/util/network-devp2p/src/connection.rs index 424e7e17619..a0a6da17156 100644 --- a/util/network-devp2p/src/connection.rs +++ b/util/network-devp2p/src/connection.rs @@ -21,23 +21,22 @@ use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::time::Duration; use bytes::{Buf, BufMut}; +use crypto::aes::{AesCtr256, AesEcb256}; use ethereum_types::{H128, H256, H512}; -use hash::{keccak, write_keccak}; +use keccak_hash::{keccak, write_keccak}; +use log::{debug, trace, warn}; use mio::{PollOpt, Ready, Token}; use mio::deprecated::{EventLoop, Handler, TryRead, TryWrite}; -use mio::tcp::*; -use parity_bytes::*; -use rcrypto::aessafe::*; -use rcrypto::blockmodes::*; -use rcrypto::buffer::*; -use rcrypto::symmetriccipher::*; +use mio::tcp::TcpStream; +use parity_bytes::Bytes; use rlp::{Rlp, RlpStream}; use tiny_keccak::Keccak; -use ethkey::crypto; -use handshake::Handshake; -use io::{IoContext, StreamToken}; -use network::{Error, ErrorKind}; +use ethcore_io::{IoContext, StreamToken}; +use ethkey::{crypto as ethcrypto, Secret}; +use network::Error; + +use crate::handshake::Handshake; const ENCRYPTED_HEADER_LEN: usize = 32; const RECEIVE_PAYLOAD: Duration = Duration::from_secs(30); @@ -279,11 +278,11 @@ pub struct EncryptedConnection { /// Underlying tcp connection pub connection: Connection, /// Egress data encryptor - encoder: CtrMode, + encoder: AesCtr256, /// Ingress data decryptor - decoder: CtrMode, + decoder: AesCtr256, /// Ingress data decryptor - mac_encoder: EcbEncryptor>, + mac_encoder_key: Secret, /// MAC for egress data egress_mac: Keccak, /// MAC for ingress data @@ -296,10 +295,11 @@ pub struct EncryptedConnection { payload_len: usize, } +const NULL_IV : [u8; 16] = [0;16]; impl EncryptedConnection { /// Create an encrypted connection out of the handshake. pub fn new(handshake: &mut Handshake) -> Result { - let shared = crypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_ephemeral)?; + let shared = ethcrypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_ephemeral)?; let mut nonce_material = H512::default(); if handshake.originated { (&mut nonce_material[0..32]).copy_from_slice(handshake.remote_nonce.as_bytes()); @@ -317,14 +317,15 @@ impl EncryptedConnection { let key_material_keccak = keccak(&key_material); (&mut key_material[32..64]).copy_from_slice(key_material_keccak.as_bytes()); - let iv = vec![0u8; 16]; - let encoder = CtrMode::new(AesSafe256Encryptor::new(&key_material[32..64]), iv); - let iv = vec![0u8; 16]; - let decoder = CtrMode::new(AesSafe256Encryptor::new(&key_material[32..64]), iv); - + // Using a 0 IV with CTR is fine as long as the same IV is never reused with the same key. + // This is the case here: ecdh creates a new secret which will be the symmetric key used + // only for this session the 0 IV is only use once with this secret, so we are in the case + // of same IV use for different key. + let encoder = AesCtr256::new(&key_material[32..64], &NULL_IV)?; + let decoder = AesCtr256::new(&key_material[32..64], &NULL_IV)?; let key_material_keccak = keccak(&key_material); (&mut key_material[32..64]).copy_from_slice(key_material_keccak.as_bytes()); - let mac_encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key_material[32..64]), NoPadding); + let mac_encoder_key: Secret = Secret::from_slice(&key_material[32..64]).expect("can create Secret from 32 bytes; qed"); let mut egress_mac = Keccak::new_keccak256(); let mut mac_material = H256::from_slice(&key_material[32..64]) ^ handshake.remote_nonce; @@ -342,7 +343,7 @@ impl EncryptedConnection { connection, encoder, decoder, - mac_encoder, + mac_encoder_key, egress_mac, ingress_mac, read_state: EncryptedConnectionState::Header, @@ -355,56 +356,53 @@ impl EncryptedConnection { /// Send a packet pub fn send_packet(&mut self, io: &IoContext, payload: &[u8]) -> Result<(), Error> where Message: Send + Clone + Sync + 'static { + const HEADER_LEN: usize = 16; let mut header = RlpStream::new(); let len = payload.len(); if len > MAX_PAYLOAD_SIZE { - bail!(ErrorKind::OversizedPacket); + return Err(Error::OversizedPacket); } + header.append_raw(&[(len >> 16) as u8, (len >> 8) as u8, len as u8], 1); header.append_raw(&[0xc2u8, 0x80u8, 0x80u8], 1); - //TODO: get rid of vectors here + let padding = (16 - (len % 16)) % 16; + + let mut packet = vec![0u8; 16 + 16 + len + padding + 16]; let mut header = header.out(); - let padding = (16 - (payload.len() % 16)) % 16; - header.resize(16, 0u8); - - let mut packet = vec![0u8; 32 + payload.len() + padding + 16]; - self.encoder.encrypt(&mut RefReadBuffer::new(&header), &mut RefWriteBuffer::new(&mut packet), false).expect("Invalid length or padding"); - EncryptedConnection::update_mac(&mut self.egress_mac, &mut self.mac_encoder, &packet[0..16]); - self.egress_mac.clone().finalize(&mut packet[16..32]); - self.encoder.encrypt(&mut RefReadBuffer::new(payload), &mut RefWriteBuffer::new(&mut packet[32..(32 + len)]), padding == 0).expect("Invalid length or padding"); + header.resize(HEADER_LEN, 0u8); + &mut packet[..HEADER_LEN].copy_from_slice(&mut header); + self.encoder.encrypt(&mut packet[..HEADER_LEN])?; + EncryptedConnection::update_mac(&mut self.egress_mac, &self.mac_encoder_key, &packet[..HEADER_LEN])?; + self.egress_mac.clone().finalize(&mut packet[HEADER_LEN..32]); + &mut packet[32..32 + len].copy_from_slice(payload); + self.encoder.encrypt(&mut packet[32..32 + len])?; if padding != 0 { - let pad = [0u8; 16]; - self.encoder.encrypt(&mut RefReadBuffer::new(&pad[0..padding]), &mut RefWriteBuffer::new(&mut packet[(32 + len)..(32 + len + padding)]), true).expect("Invalid length or padding"); + self.encoder.encrypt(&mut packet[(32 + len)..(32 + len + padding)])?; } self.egress_mac.update(&packet[32..(32 + len + padding)]); - EncryptedConnection::update_mac(&mut self.egress_mac, &mut self.mac_encoder, &[0u8; 0]); + EncryptedConnection::update_mac(&mut self.egress_mac, &self.mac_encoder_key, &[0u8; 0])?; self.egress_mac.clone().finalize(&mut packet[(32 + len + padding)..]); self.connection.send(io, packet); + Ok(()) } /// Decrypt and authenticate an incoming packet header. Prepare for receiving payload. - fn read_header(&mut self, header: &[u8]) -> Result<(), Error> { + fn read_header(&mut self, mut header: Bytes) -> Result<(), Error> { if header.len() != ENCRYPTED_HEADER_LEN { - return Err(ErrorKind::Auth.into()); + return Err(Error::Auth); } - EncryptedConnection::update_mac(&mut self.ingress_mac, &mut self.mac_encoder, &header[0..16]); + EncryptedConnection::update_mac(&mut self.ingress_mac, &self.mac_encoder_key, &header[0..16])?; let mac = &header[16..]; let mut expected = H256::zero(); self.ingress_mac.clone().finalize(expected.as_bytes_mut()); if mac != &expected[0..16] { - return Err(ErrorKind::Auth.into()); + return Err(Error::Auth); } + self.decoder.decrypt(&mut header[..16])?; - let mut hdec = H128::default(); - self.decoder.decrypt( - &mut RefReadBuffer::new(&header[0..16]), - &mut RefWriteBuffer::new(hdec.as_bytes_mut()), - false, - ).expect("Invalid length or padding"); - - let length = ((((hdec[0] as u32) << 8) + (hdec[1] as u32)) << 8) + (hdec[2] as u32); - let header_rlp = Rlp::new(&hdec[3..6]); + let length = ((((header[0] as u32) << 8) + (header[1] as u32)) << 8) + (header[2] as u32); + let header_rlp = Rlp::new(&header[3..6]); let protocol_id = header_rlp.val_at::(0)?; self.payload_len = length as usize; @@ -413,50 +411,47 @@ impl EncryptedConnection { let padding = (16 - (length % 16)) % 16; let full_length = length + padding + 16; + self.connection.expect(full_length as usize); Ok(()) } /// Decrypt and authenticate packet payload. - fn read_payload(&mut self, payload: &[u8]) -> Result { + fn read_payload(&mut self, mut payload: Bytes) -> Result { let padding = (16 - (self.payload_len % 16)) % 16; let full_length = self.payload_len + padding + 16; if payload.len() != full_length { - return Err(ErrorKind::Auth.into()); + return Err(Error::Auth); } self.ingress_mac.update(&payload[0..payload.len() - 16]); - EncryptedConnection::update_mac(&mut self.ingress_mac, &mut self.mac_encoder, &[0u8; 0]); + EncryptedConnection::update_mac(&mut self.ingress_mac, &self.mac_encoder_key, &[0u8; 0])?; + let mac = &payload[(payload.len() - 16)..]; let mut expected = H128::default(); self.ingress_mac.clone().finalize(expected.as_bytes_mut()); if mac != &expected[..] { - return Err(ErrorKind::Auth.into()); + return Err(Error::Auth); } - - let mut packet = vec![0u8; self.payload_len]; - self.decoder.decrypt(&mut RefReadBuffer::new(&payload[0..self.payload_len]), &mut RefWriteBuffer::new(&mut packet), false).expect("Invalid length or padding"); - let mut pad_buf = [0u8; 16]; - self.decoder.decrypt(&mut RefReadBuffer::new(&payload[self.payload_len..(payload.len() - 16)]), &mut RefWriteBuffer::new(&mut pad_buf), false).expect("Invalid length or padding"); + self.decoder.decrypt(&mut payload[..self.payload_len + padding])?; + payload.truncate(self.payload_len); Ok(Packet { protocol: self.protocol_id, - data: packet + data: payload }) } /// Update MAC after reading or writing any data. - fn update_mac(mac: &mut Keccak, mac_encoder: &mut EcbEncryptor>, seed: &[u8]) { + fn update_mac(mac: &mut Keccak, mac_encoder_key: &Secret, seed: &[u8]) -> Result<(), Error> { let mut prev = H128::default(); mac.clone().finalize(prev.as_bytes_mut()); let mut enc = H128::default(); - mac_encoder.encrypt( - &mut RefReadBuffer::new(prev.as_bytes()), - &mut RefWriteBuffer::new(enc.as_bytes_mut()), - true - ).expect("Error updating MAC"); - mac_encoder.reset(); + &mut enc[..].copy_from_slice(prev.as_bytes()); + let mac_encoder = AesEcb256::new(mac_encoder_key.as_bytes())?; + mac_encoder.encrypt(enc.as_bytes_mut())?; enc = enc ^ if seed.is_empty() { prev } else { H128::from_slice(seed) }; mac.update(enc.as_bytes()); + Ok(()) } /// Readable IO handler. Tracker receive status and returns decoded packet if available. @@ -464,7 +459,7 @@ impl EncryptedConnection { io.clear_timer(self.connection.token)?; if let EncryptedConnectionState::Header = self.read_state { if let Some(data) = self.connection.readable()? { - self.read_header(&data)?; + self.read_header(data)?; io.register_timer(self.connection.token, RECEIVE_PAYLOAD)?; } }; @@ -473,7 +468,7 @@ impl EncryptedConnection { Some(data) => { self.read_state = EncryptedConnectionState::Header; self.connection.expect(ENCRYPTED_HEADER_LEN); - Ok(Some(self.read_payload(&data)?)) + Ok(Some(self.read_payload(data)?)) }, None => Ok(None) } @@ -489,28 +484,6 @@ impl EncryptedConnection { } } -#[test] -pub fn test_encryption() { - use ethereum_types::{H256, H128}; - use std::str::FromStr; - let key = H256::from_str("2212767d793a7a3d66f869ae324dd11bd17044b82c9f463b8a541a4d089efec5").unwrap(); - let before = H128::from_str("12532abaec065082a3cf1da7d0136f15").unwrap(); - let before2 = H128::from_str("7e99f682356fdfbc6b67a9562787b18a").unwrap(); - let after = H128::from_str("89464c6b04e7c99e555c81d3f7266a05").unwrap(); - let after2 = H128::from_str("85c070030589ef9c7a2879b3a8489316").unwrap(); - - let mut got = H128::zero(); - - let mut encoder = EcbEncryptor::new(AesSafe256Encryptor::new(key.as_bytes()), NoPadding); - encoder.encrypt(&mut RefReadBuffer::new(before.as_bytes()), &mut RefWriteBuffer::new(got.as_bytes_mut()), true).unwrap(); - encoder.reset(); - assert_eq!(got, after); - got = H128::zero(); - encoder.encrypt(&mut RefReadBuffer::new(before2.as_bytes()), &mut RefWriteBuffer::new(got.as_bytes_mut()), true).unwrap(); - encoder.reset(); - assert_eq!(got, after2); -} - #[cfg(test)] mod tests { use std::cmp; @@ -521,7 +494,7 @@ mod tests { use mio::Ready; use parity_bytes::Bytes; - use io::*; + use ethcore_io::*; use super::*; @@ -665,6 +638,30 @@ mod tests { IoContext::new(IoChannel::disconnected(), 0) } + #[test] + pub fn test_encryption() { + use ethereum_types::{H256, H128}; + use std::str::FromStr; + let key = H256::from_str("2212767d793a7a3d66f869ae324dd11bd17044b82c9f463b8a541a4d089efec5").unwrap(); + let before = H128::from_str("12532abaec065082a3cf1da7d0136f15").unwrap(); + let before2 = H128::from_str("7e99f682356fdfbc6b67a9562787b18a").unwrap(); + let after = H128::from_str("89464c6b04e7c99e555c81d3f7266a05").unwrap(); + let after2 = H128::from_str("85c070030589ef9c7a2879b3a8489316").unwrap(); + + let mut got = H128::default(); + + let encoder = AesEcb256::new(key.as_bytes()).unwrap(); + got.as_bytes_mut().copy_from_slice(before.as_bytes()); + encoder.encrypt(got.as_bytes_mut()).unwrap(); + assert_eq!(got, after); + + let encoder = AesEcb256::new(key.as_bytes()).unwrap(); + got = H128::default(); + got.as_bytes_mut().copy_from_slice(&before2.as_bytes()); + encoder.encrypt(got.as_bytes_mut()).unwrap(); + assert_eq!(got, after2); + } + #[test] fn connection_expect() { let mut connection = TestConnection::new(); diff --git a/util/network-devp2p/src/discovery.rs b/util/network-devp2p/src/discovery.rs index 2777505b3a9..392c67b8a98 100644 --- a/util/network-devp2p/src/discovery.rs +++ b/util/network-devp2p/src/discovery.rs @@ -21,16 +21,18 @@ use std::net::SocketAddr; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use ethereum_types::{H256, H520}; -use hash::keccak; +use keccak_hash::keccak; +use log::{debug, trace, warn}; use lru_cache::LruCache; use parity_bytes::Bytes; use rlp::{Rlp, RlpStream}; use ethkey::{KeyPair, recover, Secret, sign}; -use network::{Error, ErrorKind}; +use network::Error; use network::IpFilter; -use node_table::*; -use PROTOCOL_VERSION; + +use crate::node_table::*; +use crate::PROTOCOL_VERSION; const ADDRESS_BYTES_SIZE: usize = 32; // Size of address type in bytes. const ADDRESS_BITS: usize = 8 * ADDRESS_BYTES_SIZE; // Denoted by n in [Kademlia]. @@ -169,7 +171,6 @@ pub struct Discovery<'a> { discovery_id: NodeId, discovery_nodes: HashSet, node_buckets: Vec, - // Sometimes we don't want to add nodes to the NodeTable, but still want to // keep track of them to avoid excessive pinging (happens when an unknown node sends // a discovery request to us -- the node might be on a different net). @@ -258,7 +259,7 @@ impl<'a> Discovery<'a> { Ok(()) => None, Err(BucketError::Ourselves) => None, Err(BucketError::NotInTheBucket{node_entry, bucket_distance}) => Some((node_entry, bucket_distance)) - }.map(|(node_entry, bucket_distance)| { + }.and_then(|(node_entry, bucket_distance)| { trace!(target: "discovery", "Adding a new node {:?} into our bucket {}", &node_entry, bucket_distance); let mut added = HashMap::with_capacity(1); @@ -266,7 +267,7 @@ impl<'a> Discovery<'a> { let node_to_ping = { let bucket = &mut self.node_buckets[bucket_distance]; - bucket.nodes.push_front(BucketEntry::new(node_entry)); + bucket.nodes.push_front(BucketEntry::new(node_entry.clone())); if bucket.nodes.len() > BUCKET_SIZE { select_bucket_ping(bucket.nodes.iter()) } else { @@ -276,7 +277,12 @@ impl<'a> Discovery<'a> { if let Some(node) = node_to_ping { self.try_ping(node, PingReason::Default); }; - TableUpdates{added, removed: HashSet::new()} + + if node_entry.endpoint.is_valid_sync_node() { + Some(TableUpdates { added, removed: HashSet::new() }) + } else { + None + } }) } @@ -478,12 +484,12 @@ impl<'a> Discovery<'a> { pub fn on_packet(&mut self, packet: &[u8], from: SocketAddr) -> Result, Error> { // validate packet if packet.len() < 32 + 65 + 4 + 1 { - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } let hash_signed = keccak(&packet[32..]); if hash_signed[..] != packet[0..32] { - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } let signed = &packet[(32 + 65)..]; @@ -508,7 +514,7 @@ impl<'a> Discovery<'a> { let secs_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(); if self.check_timestamps && timestamp < secs_since_epoch { debug!(target: "discovery", "Expired packet"); - return Err(ErrorKind::Expired.into()); + return Err(Error::Expired); } Ok(()) } @@ -519,7 +525,18 @@ impl<'a> Discovery<'a> { fn on_ping(&mut self, rlp: &Rlp, node_id: &NodeId, from: &SocketAddr, echo_hash: &[u8]) -> Result, Error> { trace!(target: "discovery", "Got Ping from {:?}", &from); - let ping_from = NodeEndpoint::from_rlp(&rlp.at(1)?)?; + let ping_from = if let Ok(node_endpoint) = NodeEndpoint::from_rlp(&rlp.at(1)?) { + node_endpoint + } else { + let mut address = from.clone(); + // address here is the node's tcp port. If we are unable to get the `NodeEndpoint` from the `ping_from` + // rlp field then this is most likely a BootNode, set the tcp port to 0 because it can not be used for syncing. + address.set_port(0); + NodeEndpoint { + address, + udp_port: from.port() + } + }; let ping_to = NodeEndpoint::from_rlp(&rlp.at(2)?)?; let timestamp: u64 = rlp.val_at(3)?; self.check_timestamp(timestamp)?; @@ -541,7 +558,7 @@ impl<'a> Discovery<'a> { self.send_packet(PACKET_PONG, from, &response.drain())?; let entry = NodeEntry { id: *node_id, endpoint: pong_to.clone() }; - if !entry.endpoint.is_valid() { + if !entry.endpoint.is_valid_discovery_node() { debug!(target: "discovery", "Got bad address: {:?}", entry); } else if !self.is_allowed(&entry) { debug!(target: "discovery", "Address not allowed: {:?}", entry); @@ -729,7 +746,7 @@ impl<'a> Discovery<'a> { trace!(target: "discovery", "Got {} Neighbours from {:?}", results_count, &from); for r in rlp.at(0)?.iter() { let endpoint = NodeEndpoint::from_rlp(&r)?; - if !endpoint.is_valid() { + if !endpoint.is_valid_discovery_node() { debug!(target: "discovery", "Bad address: {:?}", endpoint); continue; } @@ -885,7 +902,8 @@ mod tests { use rustc_hex::FromHex; use ethkey::{Generator, Random}; - use node_table::{Node, NodeEndpoint, NodeId}; + + use crate::node_table::{Node, NodeEndpoint, NodeId}; use super::*; diff --git a/util/network-devp2p/src/handshake.rs b/util/network-devp2p/src/handshake.rs index aee73d0e8b6..d0aadd313df 100644 --- a/util/network-devp2p/src/handshake.rs +++ b/util/network-devp2p/src/handshake.rs @@ -17,19 +17,21 @@ use std::time::Duration; use ethereum_types::{H256, H520}; -use hash::write_keccak; +use keccak_hash::write_keccak; +use log::{debug, trace}; use mio::tcp::*; use parity_bytes::Bytes; use rand::random; use rlp::{Rlp, RlpStream}; -use connection::Connection; +use ethcore_io::{IoContext, StreamToken}; use ethkey::{Generator, KeyPair, Public, Random, recover, Secret, sign}; use ethkey::crypto::{ecdh, ecies}; -use host::HostInfo; -use io::{IoContext, StreamToken}; -use network::{Error, ErrorKind}; -use node_table::NodeId; +use network::Error; + +use crate::connection::Connection; +use crate::host::HostInfo; +use crate::node_table::NodeId; #[derive(PartialEq, Eq, Debug)] enum HandshakeState { @@ -166,7 +168,7 @@ impl Handshake { trace!(target: "network", "Received handshake auth from {:?}", self.connection.remote_addr_str()); if data.len() != V4_AUTH_PACKET_SIZE { debug!(target: "network", "Wrong auth packet size"); - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } self.auth_cipher = data.to_vec(); match ecies::decrypt(secret, &[], data) { @@ -183,7 +185,7 @@ impl Handshake { let total = ((u16::from(data[0]) << 8 | (u16::from(data[1]))) as usize) + 2; if total < V4_AUTH_PACKET_SIZE { debug!(target: "network", "Wrong EIP8 auth packet size"); - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } let rest = total - data.len(); self.state = HandshakeState::ReadingAuthEip8; @@ -212,7 +214,7 @@ impl Handshake { trace!(target: "network", "Received handshake ack from {:?}", self.connection.remote_addr_str()); if data.len() != V4_ACK_PACKET_SIZE { debug!(target: "network", "Wrong ack packet size"); - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } self.ack_cipher = data.to_vec(); match ecies::decrypt(secret, &[], data) { @@ -226,7 +228,7 @@ impl Handshake { let total = (((u16::from(data[0])) << 8 | (u16::from(data[1]))) as usize) + 2; if total < V4_ACK_PACKET_SIZE { debug!(target: "network", "Wrong EIP8 ack packet size"); - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } let rest = total - data.len(); self.state = HandshakeState::ReadingAckEip8; @@ -320,18 +322,18 @@ impl Handshake { #[cfg(test)] mod test { - use std::str::FromStr; + use std::str::FromStr; - use ethereum_types::{H256, H512}; - use mio::tcp::TcpStream; - use rustc_hex::FromHex; + use ethereum_types::{H256, H512}; + use mio::tcp::TcpStream; + use rustc_hex::FromHex; - use ethkey::Public; - use io::*; + use ethcore_io::*; + use ethkey::Public; - use super::*; + use super::*; - fn check_auth(h: &Handshake, version: u64) { + fn check_auth(h: &Handshake, version: u64) { assert_eq!( h.id, H512::from_str("fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877").unwrap(), diff --git a/util/network-devp2p/src/host.rs b/util/network-devp2p/src/host.rs index cf9f6ac5b38..f409744307e 100644 --- a/util/network-devp2p/src/host.rs +++ b/util/network-devp2p/src/host.rs @@ -27,29 +27,34 @@ use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::time::Duration; use ethereum_types::H256; -use hash::keccak; -use mio::*; -use mio::deprecated::EventLoop; -use mio::tcp::*; -use mio::udp::*; +use keccak_hash::keccak; +use log::{debug, info, trace, warn}; +use mio::{ + deprecated::EventLoop, PollOpt, Ready, tcp::{TcpListener, TcpStream}, + Token, + udp::UdpSocket +}; use parity_path::restrict_permissions_owner; use parking_lot::{Mutex, RwLock}; use rlp::{Encodable, RlpStream}; use rustc_hex::ToHex; -use connection::PAYLOAD_SOFT_LIMIT; -use discovery::{Discovery, MAX_DATAGRAM_SIZE, NodeEntry, TableUpdates}; +use ethcore_io::{IoContext, IoHandler, IoManager, StreamToken, TimerToken}; use ethkey::{Generator, KeyPair, Random, Secret}; -use io::*; -use ip_utils::{map_external_address, select_public_address}; -use network::{NetworkConfiguration, NetworkIoMessage, PacketId, PeerId, ProtocolId}; -use network::{NetworkContext as NetworkContextTrait, NonReservedPeerMode}; -use network::{DisconnectReason, Error, ErrorKind, NetworkProtocolHandler, SessionInfo}; -use network::{ConnectionDirection, ConnectionFilter}; -use network::client_version::ClientVersion; -use node_table::*; -use PROTOCOL_VERSION; -use session::{Session, SessionData}; +use network::{ + client_version::ClientVersion, ConnectionDirection, ConnectionFilter, DisconnectReason, Error, + NetworkConfiguration, NetworkContext as NetworkContextTrait, NetworkIoMessage, NetworkProtocolHandler, + NonReservedPeerMode, PacketId, PeerId, ProtocolId, SessionInfo +}; + +use crate::{ + connection::PAYLOAD_SOFT_LIMIT, + discovery::{Discovery, MAX_DATAGRAM_SIZE, NodeEntry, TableUpdates}, + ip_utils::{map_external_address, select_public_address}, + node_table::*, + PROTOCOL_VERSION, + session::{Session, SessionData} +}; type Slab = ::slab::Slab; @@ -157,7 +162,7 @@ impl<'s> NetworkContextTrait for NetworkContext<'s> { fn respond(&self, packet_id: PacketId, data: Vec) -> Result<(), Error> { assert!(self.session.is_some(), "Respond called without network context"); - self.session_id.map_or_else(|| Err(ErrorKind::Expired.into()), |id| self.send(id, packet_id, data)) + self.session_id.map_or_else(|| Err(Error::Expired), |id| self.send(id, packet_id, data)) } fn disable_peer(&self, peer: PeerId) { @@ -263,17 +268,17 @@ pub struct Host { sessions: Arc>>, discovery: Mutex>>, nodes: RwLock, - handlers: RwLock>>, + handlers: RwLock>>, timers: RwLock>, timer_counter: RwLock, reserved_nodes: RwLock>, stopping: AtomicBool, - filter: Option>, + filter: Option>, } impl Host { /// Create a new instance - pub fn new(mut config: NetworkConfiguration, filter: Option>) -> Result { + pub fn new(mut config: NetworkConfiguration, filter: Option>) -> Result { let mut listen_address = match config.listen_address { None => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), DEFAULT_PORT)), Some(addr) => addr, @@ -719,8 +724,8 @@ impl Host { Err(e) => { let s = session.lock(); trace!(target: "network", "Session read error: {}:{:?} ({:?}) {:?}", token, s.id(), s.remote_addr(), e); - match *e.kind() { - ErrorKind::Disconnect(DisconnectReason::IncompatibleProtocol) | ErrorKind::Disconnect(DisconnectReason::UselessPeer) => { + match e { + Error::Disconnect(DisconnectReason::IncompatibleProtocol) | Error::Disconnect(DisconnectReason::UselessPeer) => { if let Some(id) = s.id() { if !self.reserved_nodes.read().contains(id) { let mut nodes = self.nodes.write(); @@ -924,7 +929,7 @@ impl Host { let mut failure_id = None; let mut deregister = false; let mut expired_session = None; - if let FIRST_SESSION ... LAST_SESSION = token { + if let FIRST_SESSION ..= LAST_SESSION = token { let sessions = self.sessions.read(); if let Some(session) = sessions.get(token).cloned() { expired_session = Some(session.clone()); @@ -978,14 +983,14 @@ impl Host { self.nodes.write().update(node_changes, &*self.reserved_nodes.read()); } - pub fn with_context(&self, protocol: ProtocolId, io: &IoContext, action: F) where F: FnOnce(&NetworkContextTrait) { + pub fn with_context(&self, protocol: ProtocolId, io: &IoContext, action: F) where F: FnOnce(&dyn NetworkContextTrait) { let reserved = { self.reserved_nodes.read() }; let context = NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved); action(&context); } - pub fn with_context_eval(&self, protocol: ProtocolId, io: &IoContext, action: F) -> T where F: FnOnce(&NetworkContextTrait) -> T { + pub fn with_context_eval(&self, protocol: ProtocolId, io: &IoContext, action: F) -> T where F: FnOnce(&dyn NetworkContextTrait) -> T { let reserved = { self.reserved_nodes.read() }; let context = NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved); @@ -1004,7 +1009,7 @@ impl IoHandler for Host { fn stream_hup(&self, io: &IoContext, stream: StreamToken) { trace!(target: "network", "Hup: {}", stream); match stream { - FIRST_SESSION ... LAST_SESSION => self.connection_closed(stream, io), + FIRST_SESSION ..= LAST_SESSION => self.connection_closed(stream, io), _ => warn!(target: "network", "Unexpected hup"), }; } @@ -1014,7 +1019,7 @@ impl IoHandler for Host { return; } match stream { - FIRST_SESSION ... LAST_SESSION => self.session_readable(stream, io), + FIRST_SESSION ..= LAST_SESSION => self.session_readable(stream, io), DISCOVERY => self.discovery_readable(io), TCP_ACCEPT => self.accept(io), _ => panic!("Received unknown readable token"), @@ -1026,7 +1031,7 @@ impl IoHandler for Host { return; } match stream { - FIRST_SESSION ... LAST_SESSION => self.session_writable(stream, io), + FIRST_SESSION ..= LAST_SESSION => self.session_writable(stream, io), DISCOVERY => self.discovery_writable(io), _ => panic!("Received unknown writable token"), } @@ -1038,7 +1043,7 @@ impl IoHandler for Host { } match token { IDLE => self.maintain_network(io), - FIRST_SESSION ... LAST_SESSION => self.connection_timeout(token, io), + FIRST_SESSION ..= LAST_SESSION => self.connection_timeout(token, io), DISCOVERY_REFRESH => { // Run the _slow_ discovery if enough peers are connected if !self.has_enough_peers() { @@ -1146,7 +1151,7 @@ impl IoHandler for Host { fn register_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop>) { match stream { - FIRST_SESSION ... LAST_SESSION => { + FIRST_SESSION ..= LAST_SESSION => { let session = { self.sessions.read().get(stream).cloned() }; if let Some(session) = session { session.lock().register_socket(reg, event_loop).expect("Error registering socket"); @@ -1166,7 +1171,7 @@ impl IoHandler for Host { fn deregister_stream(&self, stream: StreamToken, event_loop: &mut EventLoop>) { match stream { - FIRST_SESSION ... LAST_SESSION => { + FIRST_SESSION ..= LAST_SESSION => { let mut connections = self.sessions.write(); if let Some(connection) = connections.get(stream).cloned() { let c = connection.lock(); @@ -1183,7 +1188,7 @@ impl IoHandler for Host { fn update_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop>) { match stream { - FIRST_SESSION ... LAST_SESSION => { + FIRST_SESSION ..= LAST_SESSION => { let connection = { self.sessions.read().get(stream).cloned() }; if let Some(connection) = connection { connection.lock().update_socket(reg, event_loop).expect("Error updating socket"); diff --git a/util/network-devp2p/src/ip_utils.rs b/util/network-devp2p/src/ip_utils.rs index 75ac0f5300d..0415e7d7d0f 100644 --- a/util/network-devp2p/src/ip_utils.rs +++ b/util/network-devp2p/src/ip_utils.rs @@ -22,8 +22,9 @@ use std::time::Duration; use igd::{PortMappingProtocol, search_gateway_from_timeout}; use ipnetwork::IpNetwork; +use log::debug; -use node_table::NodeEndpoint; +use crate::node_table::NodeEndpoint; /// Socket address extension for rustc beta. To be replaces with now unstable API pub trait SocketAddrExt { @@ -214,13 +215,13 @@ impl SocketAddrExt for IpAddr { #[cfg(not(any(windows, target_os = "android")))] mod getinterfaces { - use std::{io, mem}; - use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + use std::{io, mem}; + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; - use libc::{AF_INET, AF_INET6}; - use libc::{freeifaddrs, getifaddrs, ifaddrs, sockaddr, sockaddr_in, sockaddr_in6}; + use libc::{AF_INET, AF_INET6}; + use libc::{freeifaddrs, getifaddrs, ifaddrs, sockaddr, sockaddr_in, sockaddr_in6}; - fn convert_sockaddr(sa: *mut sockaddr) -> Option { + fn convert_sockaddr(sa: *mut sockaddr) -> Option { if sa.is_null() { return None; } let (addr, _) = match i32::from(unsafe { *sa }.sa_family) { diff --git a/util/network-devp2p/src/lib.rs b/util/network-devp2p/src/lib.rs index a0ded7d6bf6..290ca316a04 100644 --- a/util/network-devp2p/src/lib.rs +++ b/util/network-devp2p/src/lib.rs @@ -19,7 +19,7 @@ //! Example usage for creating a network service and adding an IO handler: //! //! ```rust -//! extern crate ethcore_network as net; +//! extern crate network as net; //! extern crate ethcore_network_devp2p as devp2p; //! use net::*; //! use devp2p::NetworkService; @@ -60,47 +60,8 @@ //TODO: use Poll from mio #![allow(deprecated)] -//TODO: remove this -extern crate ansi_term; -#[cfg(test)] #[macro_use] -extern crate assert_matches; -extern crate bytes; -extern crate crypto as rcrypto; -#[cfg(test)] -extern crate env_logger; -#[macro_use] -extern crate error_chain; -extern crate ethcore_io as io; -extern crate ethcore_network as network; -extern crate ethereum_types; -extern crate ethkey; -extern crate igd; -extern crate ipnetwork; -extern crate keccak_hash as hash; -extern crate libc; -#[macro_use] -extern crate log; -extern crate lru_cache; -extern crate mio; -extern crate parity_bytes; -extern crate parity_crypto as crypto; -extern crate parity_path; -extern crate parity_snappy as snappy; -extern crate parking_lot; -extern crate rand; -extern crate rlp; -extern crate rustc_hex; -extern crate serde; -#[macro_use] -extern crate serde_derive; -extern crate serde_json; -extern crate slab; -#[cfg(test)] -extern crate tempdir; -extern crate tiny_keccak; - +pub use ethcore_io::TimerToken; pub use host::NetworkContext; -pub use io::TimerToken; pub use node_table::{MAX_NODES_IN_TABLE, NodeId, validate_node_url}; pub use service::NetworkService; diff --git a/util/network-devp2p/src/node_table.rs b/util/network-devp2p/src/node_table.rs index 739babfe0a7..6d68a7fd21d 100644 --- a/util/network-devp2p/src/node_table.rs +++ b/util/network-devp2p/src/node_table.rs @@ -25,13 +25,18 @@ use std::str::FromStr; use std::time::{self, Duration, SystemTime}; use ethereum_types::H512; +use log::{debug, warn}; use rand::{self, Rng}; use rlp::{DecoderError, Rlp, RlpStream}; +use serde::{Deserialize, Serialize}; use serde_json; -use discovery::{NodeEntry, TableUpdates}; -use ip_utils::*; -use network::{AllowIP, Error, ErrorKind, IpFilter}; +use network::{AllowIP, Error, IpFilter}; + +use crate::{ + discovery::{NodeEntry, TableUpdates}, + ip_utils::*, +}; /// Node public key pub type NodeId = H512; @@ -106,10 +111,16 @@ impl NodeEndpoint { self.to_rlp(rlp); } - /// Validates that the port is not 0 and address IP is specified - pub fn is_valid(&self) -> bool { - self.udp_port != 0 && self.address.port() != 0 && - match self.address { + /// Validates that the tcp port is not 0 and that the node is a valid discovery node (i.e. `is_valid_discovery_node()` is true). + /// Sync happens over tcp. + pub fn is_valid_sync_node(&self) -> bool { + self.is_valid_discovery_node() && self.address.port() != 0 + } + + /// Validates that the udp port is not 0 and address IP is specified. + /// Peer discovery happens over udp. + pub fn is_valid_discovery_node(&self) -> bool { + self.udp_port != 0 && match self.address { SocketAddr::V4(a) => !a.ip().is_unspecified(), SocketAddr::V6(a) => !a.ip().is_unspecified() } @@ -127,8 +138,8 @@ impl FromStr for NodeEndpoint { address: a, udp_port: a.port() }), - Ok(None) => bail!(ErrorKind::AddressResolve(None)), - Err(_) => Err(ErrorKind::AddressParse.into()) // always an io::Error of InvalidInput kind + Ok(None) => return Err(Error::AddressResolve(None.into())), + Err(_) => Err(Error::AddressParse) // always an io::Error of InvalidInput kind } } } @@ -210,7 +221,7 @@ impl FromStr for Node { type Err = Error; fn from_str(s: &str) -> Result { let (id, endpoint) = if s.len() > 136 && &s[0..8] == "enode://" && &s[136..137] == "@" { - (s[8..136].parse().map_err(|_| ErrorKind::InvalidNodeId)?, NodeEndpoint::from_str(&s[137..])?) + (s[8..136].parse().map_err(|_| Error::InvalidNodeId)?, NodeEndpoint::from_str(&s[137..])?) } else { (NodeId::default(), NodeEndpoint::from_str(s)?) @@ -606,6 +617,8 @@ mod tests { use ipnetwork::IpNetwork; use tempdir::TempDir; + use assert_matches::assert_matches; + use super::*; #[test] @@ -623,21 +636,21 @@ mod tests { fn endpoint_parse_empty_ip_string_returns_error() { let endpoint = NodeEndpoint::from_str(""); assert!(endpoint.is_err()); - assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse); + assert_matches!(endpoint.unwrap_err(), Error::AddressParse); } #[test] fn endpoint_parse_invalid_ip_string_returns_error() { let endpoint = NodeEndpoint::from_str("beef"); assert!(endpoint.is_err()); - assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse); + assert_matches!(endpoint.unwrap_err(), Error::AddressParse); } #[test] fn endpoint_parse_valid_ip_without_port_returns_error() { let endpoint = NodeEndpoint::from_str("123.123.123.123"); assert!(endpoint.is_err()); - assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse); + assert_matches!(endpoint.unwrap_err(), Error::AddressParse); let endpoint = NodeEndpoint::from_str("123.123.123.123:123"); assert!(endpoint.is_ok()) } @@ -662,11 +675,11 @@ mod tests { fn node_parse_fails_for_invalid_urls() { let node = Node::from_str("foo"); assert!(node.is_err()); - assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse); + assert_matches!(node.unwrap_err(), Error::AddressParse); let node = Node::from_str("enode://foo@bar"); assert!(node.is_err()); - assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse); + assert_matches!(node.unwrap_err(), Error::AddressParse); } #[test] diff --git a/util/network-devp2p/src/service.rs b/util/network-devp2p/src/service.rs index 3bf9b1da120..a68528db204 100644 --- a/util/network-devp2p/src/service.rs +++ b/util/network-devp2p/src/service.rs @@ -19,13 +19,17 @@ use std::ops::RangeInclusive; use std::sync::Arc; use ansi_term::Colour; +use log::info; use parking_lot::RwLock; -use host::Host; -use io::*; -use network::{Error, NetworkConfiguration, NetworkProtocolHandler, NonReservedPeerMode}; -use network::{NetworkContext, NetworkIoMessage, PeerId, ProtocolId}; -use network::ConnectionFilter; +use ethcore_io::{IoContext, IoHandler, IoService}; +use network::{ + ConnectionFilter, Error, NetworkConfiguration, NetworkContext, + NetworkIoMessage, NetworkProtocolHandler, NonReservedPeerMode, PeerId, ProtocolId, + +}; + +use crate::host::Host; struct HostHandler { public_url: RwLock> diff --git a/util/network-devp2p/src/session.rs b/util/network-devp2p/src/session.rs index 8087342a15a..fcb8397b060 100644 --- a/util/network-devp2p/src/session.rs +++ b/util/network-devp2p/src/session.rs @@ -20,20 +20,24 @@ use std::net::SocketAddr; use std::time::{Duration, Instant}; use ethereum_types::H256; +use log::{debug, trace, warn}; use mio::*; use mio::deprecated::{EventLoop, Handler}; use mio::tcp::*; +use parity_snappy as snappy; use rlp::{EMPTY_LIST_RLP, Rlp, RlpStream}; -use snappy; -use connection::{Connection, EncryptedConnection, MAX_PAYLOAD_SIZE, Packet}; -use handshake::Handshake; -use host::*; -use io::{IoContext, StreamToken}; -use network::{DisconnectReason, Error, ErrorKind, PeerCapabilityInfo, ProtocolId, SessionInfo}; +use ethcore_io::{IoContext, StreamToken}; +use network::{DisconnectReason, Error, PeerCapabilityInfo, ProtocolId, SessionInfo}; use network::client_version::ClientVersion; use network::SessionCapabilityInfo; -use node_table::NodeId; + +use crate::{ + connection::{Connection, EncryptedConnection, MAX_PAYLOAD_SIZE, Packet}, + handshake::Handshake, + host::HostInfo, + node_table::NodeId, +}; // Timeout must be less than (interval - 1). const PING_TIMEOUT: Duration = Duration::from_secs(60); @@ -256,10 +260,10 @@ impl Session { where Message: Send + Sync + Clone { if protocol.is_some() && (self.info.capabilities.is_empty() || !self.had_hello) { debug!(target: "network", "Sending to unconfirmed session {}, protocol: {:?}, packet: {}", self.token(), protocol.as_ref().map(|p| str::from_utf8(&p[..]).unwrap_or("??")), packet_id); - bail!(ErrorKind::BadProtocol); + return Err(Error::BadProtocol); } if self.expired() { - return Err(ErrorKind::Expired.into()); + return Err(Error::Expired); } let mut i = 0usize; let pid = match protocol { @@ -281,7 +285,7 @@ impl Session { let mut payload = data; // create a reference with local lifetime if self.compression { if payload.len() > MAX_PAYLOAD_SIZE { - bail!(ErrorKind::OversizedPacket); + return Err(Error::OversizedPacket); } let len = snappy::compress_into(&payload, &mut compressed); trace!(target: "network", "compressed {} to {}", payload.len(), len); @@ -331,16 +335,16 @@ impl Session { fn read_packet(&mut self, io: &IoContext, packet: &Packet, host: &HostInfo) -> Result where Message: Send + Sync + Clone { if packet.data.len() < 2 { - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } let packet_id = packet.data[0]; if packet_id != PACKET_HELLO && packet_id != PACKET_DISCONNECT && !self.had_hello { - return Err(ErrorKind::BadProtocol.into()); + return Err(Error::BadProtocol); } let data = if self.compression { let compressed = &packet.data[1..]; if snappy::decompressed_len(&compressed)? > MAX_PAYLOAD_SIZE { - bail!(ErrorKind::OversizedPacket); + return Err(Error::OversizedPacket); } snappy::decompress(&compressed)? } else { @@ -358,7 +362,7 @@ impl Session { if self.had_hello { debug!(target:"network", "Disconnected: {}: {:?}", self.token(), DisconnectReason::from_u8(reason)); } - Err(ErrorKind::Disconnect(DisconnectReason::from_u8(reason)).into()) + Err(Error::Disconnect(DisconnectReason::from_u8(reason))) } PACKET_PING => { self.send_pong(io)?; @@ -372,7 +376,7 @@ impl Session { }, PACKET_GET_PEERS => Ok(SessionData::None), //TODO; PACKET_PEERS => Ok(SessionData::None), - PACKET_USER ... PACKET_LAST => { + PACKET_USER ..= PACKET_LAST => { let mut i = 0usize; while packet_id >= self.info.capabilities[i].id_offset + self.info.capabilities[i].packet_count { i += 1; @@ -500,7 +504,7 @@ impl Session { rlp.append(&(reason as u32)); self.send_packet(io, None, PACKET_DISCONNECT, &rlp.drain()).ok(); } - ErrorKind::Disconnect(reason).into() + Error::Disconnect(reason) } fn send(&mut self, io: &IoContext, data: &[u8]) -> Result<(), Error> where Message: Send + Sync + Clone { diff --git a/util/network-devp2p/tests/tests.rs b/util/network-devp2p/tests/tests.rs index 74094cc46f2..9a0fc72d595 100644 --- a/util/network-devp2p/tests/tests.rs +++ b/util/network-devp2p/tests/tests.rs @@ -14,26 +14,20 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -extern crate env_logger; -extern crate ethcore_io as io; -extern crate ethcore_network; -extern crate ethcore_network_devp2p; -extern crate ethkey; -extern crate parity_bytes; -extern crate parking_lot; - -use std::sync::Arc; -use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering as AtomicOrdering} +}; use std::thread; -use std::time::*; +use std::time::Duration; use parity_bytes::Bytes; use parking_lot::Mutex; -use ethcore_network::*; +use network::{PeerId, NetworkContext, NetworkProtocolHandler, NetworkConfiguration}; use ethcore_network_devp2p::NetworkService; use ethkey::{Generator, Random}; -use io::TimerToken; +use ethcore_io::TimerToken; pub struct TestProtocol { drop_session: bool, @@ -48,7 +42,7 @@ impl TestProtocol { packet: Mutex::new(Vec::new()), got_timeout: AtomicBool::new(false), got_disconnect: AtomicBool::new(false), - drop_session: drop_session, + drop_session, } } /// Creates and register protocol with the network service diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index 018152c72f0..55c15d7d181 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -7,7 +7,7 @@ version = "1.12.0" authors = ["Parity Technologies "] [dependencies] -error-chain = { version = "0.12", default-features = false } +derive_more = "0.14.0" parity-crypto = "0.4.0" ethcore-io = { path = "../io" } ethereum-types = "0.6.0" diff --git a/util/network/src/error.rs b/util/network/src/error.rs index bd48830c1bb..81e3b78b887 100644 --- a/util/network/src/error.rs +++ b/util/network/src/error.rs @@ -14,11 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -// Silence: `use of deprecated item 'std::error::Error::cause': replaced by Error::source, which can support downcasting` -// https://github.com/paritytech/parity-ethereum/issues/10302 -#![allow(deprecated)] - -use std::{io, net, fmt}; +use std::{error, io, net, fmt}; use libc::{ENFILE, EMFILE}; use io::IoError; use {rlp, ethkey, crypto, snappy}; @@ -85,118 +81,127 @@ impl fmt::Display for DisconnectReason { } } -error_chain! { - foreign_links { - SocketIo(IoError) #[doc = "Socket IO error."]; - Decompression(snappy::InvalidInput) #[doc = "Decompression error."]; - Rlp(rlp::DecoderError) #[doc = "Rlp decoder error."]; - } - - errors { - #[doc = "Error concerning the network address parsing subsystem."] - AddressParse { - description("Failed to parse network address"), - display("Failed to parse network address"), - } - - #[doc = "Error concerning the network address resolution subsystem."] - AddressResolve(err: Option) { - description("Failed to resolve network address"), - display("Failed to resolve network address {}", err.as_ref().map_or("".to_string(), |e| e.to_string())), - } - - #[doc = "Authentication failure"] - Auth { - description("Authentication failure"), - display("Authentication failure"), - } - - #[doc = "Unrecognised protocol"] - BadProtocol { - description("Bad protocol"), - display("Bad protocol"), - } - - #[doc = "Expired message"] - Expired { - description("Expired message"), - display("Expired message"), - } +/// Queue error +#[derive(Debug, derive_more::Display)] +pub enum Error { + /// Socket IO error. + SocketIo(IoError), + /// Decompression error. + Decompression(snappy::InvalidInput), + /// Rlp decoder error. + Rlp(rlp::DecoderError), + /// Error concerning the network address parsing subsystem. + #[display(fmt = "Failed to parse network address")] + AddressParse, + /// Error concerning the network address resolution subsystem. + #[display(fmt = "Failed to resolve network address {}", _0)] + AddressResolve(AddressResolveError), + /// Authentication failure + #[display(fmt = "Authentication failure")] + Auth, + /// Unrecognised protocol + #[display(fmt = "Bad protocol")] + BadProtocol, + /// Expired message + #[display(fmt = "Expired message")] + Expired, + /// Peer not found + #[display(fmt = "Peer not found")] + PeerNotFound, + /// Peer is disconnected + #[display(fmt = "Peer disconnected: {}", _0)] + Disconnect(DisconnectReason), + /// Invalid node id + #[display(fmt = "Invalid node id")] + InvalidNodeId, + /// Packet size is over the protocol limit + #[display(fmt = "Packet is too large")] + OversizedPacket, + /// Reached system resource limits for this process + #[display(fmt = "Too many open files in this process. Check your resource limits and restart parity")] + ProcessTooManyFiles, + /// Reached system wide resource limits + #[display(fmt = "Too many open files on system. Consider closing some processes/release some file handlers or increas the system-wide resource limits and restart parity.")] + SystemTooManyFiles, + /// An unknown IO error occurred. + #[display(fmt = "Unexpected IO error: {}", _0)] + Io(io::Error), +} - #[doc = "Peer not found"] - PeerNotFound { - description("Peer not found"), - display("Peer not found"), - } +/// Wraps io::Error for Display impl +#[derive(Debug)] +pub struct AddressResolveError(Option); - #[doc = "Peer is disconnected"] - Disconnect(reason: DisconnectReason) { - description("Peer disconnected"), - display("Peer disconnected: {}", reason), - } +impl fmt::Display for AddressResolveError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "{}", self.0.as_ref().map_or("".to_string(), |e| e.to_string())) + } +} - #[doc = "Invalid node id"] - InvalidNodeId { - description("Invalid node id"), - display("Invalid node id"), - } +impl From> for AddressResolveError { + fn from(err: Option) -> Self { + AddressResolveError(err) + } +} - #[doc = "Packet size is over the protocol limit"] - OversizedPacket { - description("Packet is too large"), - display("Packet is too large"), +impl error::Error for Error { + fn source(&self) -> Option<&(error::Error + 'static)> { + match self { + Error::Decompression(e) => Some(e), + Error::Rlp(e) => Some(e), + _ => None, } + } +} - #[doc = "Reached system resource limits for this process"] - ProcessTooManyFiles { - description("Too many open files in process."), - display("Too many open files in this process. Check your resource limits and restart parity"), - } +impl From for Error { + fn from(err: IoError) -> Self { + Error::SocketIo(err) + } +} - #[doc = "Reached system wide resource limits"] - SystemTooManyFiles { - description("Too many open files on system."), - display("Too many open files on system. Consider closing some processes/release some file handlers or increas the system-wide resource limits and restart parity."), - } +impl From for Error { + fn from(err: snappy::InvalidInput) -> Self { + Error::Decompression(err) + } +} - #[doc = "An unknown IO error occurred."] - Io(err: io::Error) { - description("IO Error"), - display("Unexpected IO error: {}", err), - } +impl From for Error { + fn from(err: rlp::DecoderError) -> Self { + Error::Rlp(err) } } impl From for Error { fn from(err: io::Error) -> Self { match err.raw_os_error() { - Some(ENFILE) => ErrorKind::ProcessTooManyFiles.into(), - Some(EMFILE) => ErrorKind::SystemTooManyFiles.into(), - _ => Error::from_kind(ErrorKind::Io(err)) + Some(ENFILE) => Error::ProcessTooManyFiles, + Some(EMFILE) => Error::SystemTooManyFiles, + _ => Error::Io(err) } } } impl From for Error { fn from(_err: ethkey::Error) -> Self { - ErrorKind::Auth.into() + Error::Auth } } impl From for Error { fn from(_err: ethkey::crypto::Error) -> Self { - ErrorKind::Auth.into() + Error::Auth } } impl From for Error { fn from(_err: crypto::error::SymmError) -> Self { - ErrorKind::Auth.into() + Error::Auth } } impl From for Error { - fn from(_err: net::AddrParseError) -> Self { ErrorKind::AddressParse.into() } + fn from(_err: net::AddrParseError) -> Self { Error::AddressParse } } #[test] @@ -208,13 +213,13 @@ fn test_errors() { } assert_eq!(DisconnectReason::Unknown, r); - match *>::from(rlp::DecoderError::RlpIsTooBig).kind() { - ErrorKind::Rlp(_) => {}, + match >::from(rlp::DecoderError::RlpIsTooBig) { + Error::Rlp(_) => {}, _ => panic!("Unexpected error"), } - match *>::from(ethkey::crypto::Error::InvalidMessage).kind() { - ErrorKind::Auth => {}, + match >::from(ethkey::crypto::Error::InvalidMessage) { + Error::Auth => {}, _ => panic!("Unexpected error"), } } @@ -226,18 +231,18 @@ fn test_io_errors() { assert_matches!( >::from( io::Error::from_raw_os_error(ENFILE) - ).kind(), - ErrorKind::ProcessTooManyFiles); + ), + Error::ProcessTooManyFiles); assert_matches!( >::from( io::Error::from_raw_os_error(EMFILE) - ).kind(), - ErrorKind::SystemTooManyFiles); + ), + Error::SystemTooManyFiles); assert_matches!( >::from( io::Error::from_raw_os_error(0) - ).kind(), - ErrorKind::Io(_)); + ), + Error::Io(_)); } diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index 9e6f71fdd16..be47fa36755 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -32,9 +32,7 @@ extern crate serde_derive; #[cfg(test)] #[macro_use] extern crate assert_matches; - -#[macro_use] -extern crate error_chain; +extern crate derive_more; #[macro_use] extern crate lazy_static; @@ -46,7 +44,7 @@ mod error; pub use connection_filter::{ConnectionFilter, ConnectionDirection}; pub use io::TimerToken; -pub use error::{Error, ErrorKind, DisconnectReason}; +pub use error::{Error, DisconnectReason}; use client_version::ClientVersion; use std::cmp::Ordering;