diff --git a/Cargo.lock b/Cargo.lock index a9416fdc8..487fea845 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,7 @@ dependencies = [ "ethcore 1.3.0", "ethcore-dapps 1.3.0", "ethcore-devtools 1.3.0", + "ethcore-io 1.3.0", "ethcore-ipc 1.3.0", "ethcore-ipc-codegen 1.3.0", "ethcore-ipc-hypervisor 1.2.0", @@ -91,11 +92,6 @@ name = "bit-vec" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "bitflags" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "bitflags" version = "0.4.0" @@ -106,11 +102,6 @@ name = "bitflags" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "bitflags" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "blastfig" version = "0.3.3" @@ -136,15 +127,6 @@ name = "cfg-if" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "chrono" -version = "0.2.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "clippy" version = "0.0.80" @@ -264,6 +246,7 @@ dependencies = [ "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "ethash 1.3.0", "ethcore-devtools 1.3.0", + "ethcore-io 1.3.0", "ethcore-ipc 1.3.0", "ethcore-ipc-codegen 1.3.0", "ethcore-ipc-nano 1.3.0", @@ -314,6 +297,17 @@ dependencies = [ "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ethcore-io" +version = "1.3.0" +dependencies = [ + "crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.5.1 (git+https://github.com/ethcore/mio?branch=v0.5.x)", + "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ethcore-ipc" version = "1.3.0" @@ -384,6 +378,27 @@ dependencies = [ "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ethcore-network" +version = "1.3.0" +dependencies = [ + "ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "ethcore-devtools 1.3.0", + "ethcore-io 1.3.0", + "ethcore-util 1.3.0", + "igd 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.5.1 (git+https://github.com/ethcore/mio?branch=v0.5.x)", + "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ethcore-rpc" version = "1.3.0" @@ -392,6 +407,7 @@ dependencies = [ "ethash 1.3.0", "ethcore 1.3.0", "ethcore-devtools 1.3.0", + "ethcore-io 1.3.0", "ethcore-ipc 1.3.0", "ethcore-util 1.3.0", "ethjson 0.1.0", @@ -414,6 +430,7 @@ version = "1.3.0" dependencies = [ "clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ethcore-io 1.3.0", "ethcore-rpc 1.3.0", "ethcore-util 1.3.0", "jsonrpc-core 2.0.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -431,21 +448,16 @@ dependencies = [ "ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "arrayvec 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "bigint 0.1.0", - "chrono 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.4.0 (git+https://github.com/ethcore/elastic-array)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "eth-secp256k1 0.5.4 (git+https://github.com/ethcore/rust-secp256k1)", "ethcore-devtools 1.3.0", "heapsize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "igd 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.5.1 (git+https://github.com/ethcore/mio?branch=v0.5.x)", - "nix 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rocksdb 0.4.5 (git+https://github.com/ethcore/rust-rocksdb)", @@ -453,11 +465,9 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "sha3 0.1.0", - "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "table 0.1.0", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", - "tiny-keccak 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "using_queue 0.1.0", "vergen 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -509,9 +519,11 @@ dependencies = [ "clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore 1.3.0", + "ethcore-io 1.3.0", "ethcore-ipc 1.3.0", "ethcore-ipc-codegen 1.3.0", "ethcore-ipc-nano 1.3.0", + "ethcore-network 1.3.0", "ethcore-util 1.3.0", "heapsize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -605,6 +617,26 @@ dependencies = [ "vecio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hyper" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "idna" version = "0.1.0" @@ -617,13 +649,13 @@ dependencies = [ [[package]] name = "igd" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "hyper 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)", - "xml-rs 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)", + "xml-rs 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "xmltree 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1495,18 +1527,10 @@ dependencies = [ [[package]] name = "xml-rs" -version = "0.1.26" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "xml-rs" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1514,7 +1538,7 @@ name = "xmltree" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "xml-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "xml-rs 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] diff --git a/Cargo.toml b/Cargo.toml index f6d1f2122..7d24f8f70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ fdlimit = { path = "util/fdlimit" } ethcore = { path = "ethcore" } ethcore-util = { path = "util" } ethsync = { path = "sync" } +ethcore-io = { path = "util/io" } ethcore-devtools = { path = "devtools" } ethcore-rpc = { path = "rpc" } ethcore-signer = { path = "signer" } @@ -68,3 +69,4 @@ name = "parity" [profile.release] debug = true lto = false + diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 5bdd5bd4b..b87be1087 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -28,6 +28,7 @@ evmjit = { path = "../evmjit", optional = true } clippy = { version = "0.0.80", optional = true} ethash = { path = "../ethash" } ethcore-util = { path = "../util" } +ethcore-io = { path = "../util/io" } ethcore-devtools = { path = "../devtools" } ethjson = { path = "../json" } ethcore-ipc = { path = "../ipc/rpc" } diff --git a/ethcore/src/block_queue.rs b/ethcore/src/block_queue.rs index 51e5058f7..b65a4ad26 100644 --- a/ethcore/src/block_queue.rs +++ b/ethcore/src/block_queue.rs @@ -20,6 +20,7 @@ use std::thread::{JoinHandle, self}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::{Condvar as SCondvar, Mutex as SMutex}; use util::*; +use io::*; use verification::*; use error::*; use engines::Engine; @@ -27,7 +28,6 @@ use views::*; use header::*; use service::*; use client::BlockStatus; -use util::panics::*; pub use types::block_queue_info::BlockQueueInfo; @@ -450,6 +450,7 @@ impl Drop for BlockQueue { #[cfg(test)] mod tests { use util::*; + use io::*; use spec::*; use block_queue::*; use tests::helpers::*; diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index cb03df6b5..1aeca3974 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -27,12 +27,11 @@ use util::{journaldb, rlp, Bytes, View, PerfTimer, Itertools, Mutex, RwLock}; use util::journaldb::JournalDB; use util::rlp::{UntrustedRlp}; use util::numbers::*; -use util::panics::*; -use util::io::*; use util::sha3::*; use util::kvdb::*; // other +use io::*; use views::{BlockView, HeaderView, BodyView}; use error::{ImportError, ExecutionError, CallError, BlockError, ImportResult}; use header::BlockNumber; diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index 61c024443..cdb3deb38 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -17,6 +17,7 @@ //! General error types for use in ethcore. use util::*; +use io::*; use header::BlockNumber; use basic_types::LogBloom; use client::Error as ClientError; @@ -227,8 +228,10 @@ pub enum Error { PowInvalid, /// Error concerning TrieDBs Trie(TrieError), - /// Io error. - Io(::std::io::Error), + /// Io crate error. + Io(IoError), + /// Standard io error. + StdIo(::std::io::Error), /// Snappy error. Snappy(::util::snappy::InvalidInput), } @@ -238,6 +241,7 @@ impl fmt::Display for Error { match *self { Error::Client(ref err) => f.write_fmt(format_args!("{}", err)), Error::Util(ref err) => f.write_fmt(format_args!("{}", err)), + Error::Io(ref err) => f.write_fmt(format_args!("{}", err)), Error::Block(ref err) => f.write_fmt(format_args!("{}", err)), Error::Execution(ref err) => f.write_fmt(format_args!("{}", err)), Error::Transaction(ref err) => f.write_fmt(format_args!("{}", err)), @@ -247,7 +251,7 @@ impl fmt::Display for Error { Error::PowHashInvalid => f.write_str("Invalid or out of date PoW hash."), Error::PowInvalid => f.write_str("Invalid nonce or mishash"), Error::Trie(ref err) => f.write_fmt(format_args!("{}", err)), - Error::Io(ref err) => f.write_fmt(format_args!("{}", err)), + Error::StdIo(ref err) => f.write_fmt(format_args!("{}", err)), Error::Snappy(ref err) => f.write_fmt(format_args!("{}", err)), } } @@ -309,7 +313,7 @@ impl From for Error { impl From for Error { fn from(err: IoError) -> Error { - Error::Util(From::from(err)) + Error::Io(err) } } @@ -321,7 +325,7 @@ impl From for Error { impl From<::std::io::Error> for Error { fn from(err: ::std::io::Error) -> Error { - Error::Io(err) + Error::StdIo(err) } } diff --git a/ethcore/src/json_tests/chain.rs b/ethcore/src/json_tests/chain.rs index 6f218c335..58af928c4 100644 --- a/ethcore/src/json_tests/chain.rs +++ b/ethcore/src/json_tests/chain.rs @@ -24,6 +24,7 @@ use spec::Genesis; use ethjson; use ethjson::blockchain::BlockChain; use miner::Miner; +use io::IoChannel; pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec { init_log(); diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index af99b7554..e883385f6 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -77,6 +77,7 @@ #[macro_use] extern crate log; #[macro_use] extern crate ethcore_util as util; +extern crate ethcore_io as io; #[macro_use] extern crate lazy_static; extern crate rustc_serialize; #[macro_use] extern crate heapsize; diff --git a/ethcore/src/miner/mod.rs b/ethcore/src/miner/mod.rs index 77a372c65..a6cff9f55 100644 --- a/ethcore/src/miner/mod.rs +++ b/ethcore/src/miner/mod.rs @@ -27,7 +27,6 @@ //! extern crate ethcore_util as util; //! extern crate ethcore; //! use std::env; -//! use util::network::{NetworkService, NetworkConfiguration}; //! use ethcore::ethereum; //! use ethcore::client::{Client, ClientConfig}; //! use ethcore::miner::{Miner, MinerService}; diff --git a/ethcore/src/service.rs b/ethcore/src/service.rs index 6e22d9dd7..10c2d99b5 100644 --- a/ethcore/src/service.rs +++ b/ethcore/src/service.rs @@ -17,7 +17,7 @@ //! Creates and registers client and network services. use util::*; -use util::panics::*; +use io::*; use spec::Spec; use error::*; use client::{Client, ClientConfig, ChainNotify}; diff --git a/ethcore/src/tests/client.rs b/ethcore/src/tests/client.rs index bb6fcfdbd..d3677eb50 100644 --- a/ethcore/src/tests/client.rs +++ b/ethcore/src/tests/client.rs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +use io::IoChannel; use client::{BlockChainClient, MiningBlockChainClient, Client, ClientConfig, BlockID}; use block::IsBlock; use tests::helpers::*; diff --git a/ethcore/src/tests/helpers.rs b/ethcore/src/tests/helpers.rs index 57844f129..85d5e3ef9 100644 --- a/ethcore/src/tests/helpers.rs +++ b/ethcore/src/tests/helpers.rs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +use io::*; use client::{self, BlockChainClient, Client, ClientConfig}; use common::*; use spec::*; diff --git a/ethcore/src/tests/rpc.rs b/ethcore/src/tests/rpc.rs index a4d3621bc..5aaea356b 100644 --- a/ethcore/src/tests/rpc.rs +++ b/ethcore/src/tests/rpc.rs @@ -24,7 +24,7 @@ use tests::helpers::*; use devtools::*; use miner::Miner; use crossbeam; -use common::IoChannel; +use io::IoChannel; pub fn run_test_worker(scope: &crossbeam::Scope, stop: Arc, socket_path: &str) { let socket_path = socket_path.to_owned(); diff --git a/parity/blockchain.rs b/parity/blockchain.rs index 90ca809c1..36795edc7 100644 --- a/parity/blockchain.rs +++ b/parity/blockchain.rs @@ -23,7 +23,7 @@ use std::path::Path; use std::sync::Arc; use rustc_serialize::hex::FromHex; use ethcore_logger::{setup_log, Config as LogConfig}; -use util::panics::{PanicHandler, ForwardPanic}; +use io::{PanicHandler, ForwardPanic}; use util::{PayloadInfo, ToPretty}; use ethcore::service::ClientService; use ethcore::client::{Mode, DatabaseCompactionProfile, Switch, VMType, BlockImportError, BlockChainClient, BlockID}; diff --git a/parity/configuration.rs b/parity/configuration.rs index 449ce96f8..1aef086f5 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -21,13 +21,14 @@ use std::path::PathBuf; use std::cmp::max; use cli::{USAGE, Args}; use docopt::{Docopt, Error as DocoptError}; -use util::{Hashable, NetworkConfiguration, U256, Uint, is_valid_node_url, Bytes, version_data, Secret, Address}; -use util::network_settings::NetworkSettings; +use util::{Hashable, U256, Uint, Bytes, version_data, Secret, Address}; use util::log::Colour; +use ethsync::{NetworkConfiguration, is_valid_node_url}; use ethcore::client::{VMType, Mode}; use ethcore::miner::MinerOptions; use rpc::{IpcConfiguration, HttpConfiguration}; +use ethcore_rpc::NetworkSettings; use cache::CacheConfig; use helpers::{to_duration, to_mode, to_block_id, to_u256, to_pending_set, to_price, replace_home, geth_ipc_path, parity_ipc_path, to_bootnodes, to_addresses, to_address}; @@ -397,8 +398,8 @@ impl Configuration { ret.nat_enabled = self.args.flag_nat == "any" || self.args.flag_nat == "upnp"; ret.boot_nodes = try!(to_bootnodes(&self.args.flag_bootnodes)); let (listen, public) = try!(self.net_addresses()); - ret.listen_address = listen; - ret.public_address = public; + ret.listen_address = listen.map(|l| format!("{}", l)); + ret.public_address = public.map(|p| format!("{}", p)); ret.use_secret = self.args.flag_node_key.as_ref().map(|s| s.parse::().unwrap_or_else(|_| s.sha3())); ret.discovery_enabled = !self.args.flag_no_discovery && !self.args.flag_nodiscover; ret.max_peers = self.max_peers(); @@ -408,9 +409,7 @@ impl Configuration { ret.config_path = Some(net_path.to_str().unwrap().to_owned()); ret.reserved_nodes = try!(self.init_reserved_nodes()); - if self.args.flag_reserved_only { - ret.non_reserved_mode = ::util::network::NonReservedPeerMode::Deny; - } + ret.allow_non_reserved = !self.args.flag_reserved_only; Ok(ret) } @@ -552,7 +551,7 @@ mod tests { use super::*; use cli::USAGE; use docopt::Docopt; - use util::network_settings::NetworkSettings; + use ethcore_rpc::NetworkSettings; use ethcore::client::{VMType, BlockID}; use helpers::{replace_home, default_network_config}; use run::RunCmd; diff --git a/parity/dapps.rs b/parity/dapps.rs index 9fb01a30a..6b957a5f1 100644 --- a/parity/dapps.rs +++ b/parity/dapps.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use std::net::SocketAddr; -use util::panics::PanicHandler; +use io::PanicHandler; use rpc_apis; use helpers::replace_home; diff --git a/parity/helpers.rs b/parity/helpers.rs index ad67e1436..d0688d3de 100644 --- a/parity/helpers.rs +++ b/parity/helpers.rs @@ -19,7 +19,7 @@ use std::io::{Write, Read, BufReader, BufRead}; use std::time::Duration; use std::path::Path; use std::fs::File; -use util::{clean_0x, U256, Uint, Address, path, is_valid_node_url, H256, CompactionProfile}; +use util::{clean_0x, U256, Uint, Address, path, H256, CompactionProfile}; use util::journaldb::Algorithm; use ethcore::client::{Mode, BlockID, Switch, VMType, DatabaseCompactionProfile, ClientConfig}; use ethcore::miner::PendingSet; @@ -28,6 +28,7 @@ use dir::Directories; use params::Pruning; use upgrade::upgrade; use migration::migrate; +use ethsync::is_valid_node_url; pub fn to_duration(s: &str) -> Result { to_seconds(s).map(Duration::from_secs) @@ -167,11 +168,11 @@ pub fn to_bootnodes(bootnodes: &Option) -> Result, String> { } #[cfg(test)] -pub fn default_network_config() -> ::util::NetworkConfiguration { - use util::{NetworkConfiguration, NonReservedPeerMode}; +pub fn default_network_config() -> ::ethsync::NetworkConfiguration { + use ethsync::NetworkConfiguration; NetworkConfiguration { config_path: Some(replace_home("$HOME/.parity/network")), - listen_address: Some("0.0.0.0:30303".parse().unwrap()), + listen_address: Some("0.0.0.0:30303".into()), public_address: None, udp_port: None, nat_enabled: true, @@ -181,7 +182,7 @@ pub fn default_network_config() -> ::util::NetworkConfiguration { max_peers: 50, min_peers: 25, reserved_nodes: Vec::new(), - non_reserved_mode: NonReservedPeerMode::Accept, + allow_non_reserved: true, } } diff --git a/parity/io_handler.rs b/parity/io_handler.rs index 7d8e53eea..d60f80f9a 100644 --- a/parity/io_handler.rs +++ b/parity/io_handler.rs @@ -19,7 +19,7 @@ use ethcore::client::Client; use ethcore::service::ClientIoMessage; use ethsync::{SyncProvider, ManageNetwork}; use ethcore::account_provider::AccountProvider; -use util::{TimerToken, IoHandler, IoContext}; +use io::{TimerToken, IoHandler, IoContext}; use informant::Informant; diff --git a/parity/main.rs b/parity/main.rs index 51951d115..cc2810c08 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -40,6 +40,7 @@ extern crate time; extern crate number_prefix; extern crate rpassword; extern crate semver; +extern crate ethcore_io as io; extern crate ethcore_ipc as ipc; extern crate ethcore_ipc_nano as nanoipc; #[macro_use] diff --git a/parity/modules.rs b/parity/modules.rs index 4e2e22ffb..5ae5db231 100644 --- a/parity/modules.rs +++ b/parity/modules.rs @@ -16,9 +16,8 @@ use std::sync::Arc; use ethcore::client::BlockChainClient; -use ethcore; use hypervisor::Hypervisor; -use ethsync::{SyncConfig, NetworkConfiguration}; +use ethsync::{SyncConfig, NetworkConfiguration, NetworkError}; #[cfg(not(feature="ipc"))] use self::no_ipc_deps::*; #[cfg(feature="ipc")] @@ -103,7 +102,7 @@ pub fn sync _client: Arc, log_settings: &LogConfig, ) - -> Result + -> Result { let mut hypervisor = hypervisor_ref.take().expect("There should be hypervisor for ipc configuration"); hypervisor = hypervisor.module(SYNC_MODULE_ID, "parity", sync_arguments(sync_cfg, net_cfg, log_settings)); @@ -128,8 +127,8 @@ pub fn sync client: Arc, _log_settings: &LogConfig, ) - -> Result + -> Result { - let eth_sync = try!(EthSync::new(sync_cfg, client, net_cfg).map_err(ethcore::error::Error::Util)); + let eth_sync = try!(EthSync::new(sync_cfg, client, net_cfg)); Ok((eth_sync.clone() as Arc, eth_sync.clone() as Arc, eth_sync.clone() as Arc)) } diff --git a/parity/rpc.rs b/parity/rpc.rs index b30529c1c..5600a7f06 100644 --- a/parity/rpc.rs +++ b/parity/rpc.rs @@ -17,7 +17,7 @@ use std::fmt; use std::sync::Arc; use std::net::SocketAddr; -use util::panics::PanicHandler; +use io::PanicHandler; use ethcore_rpc::{RpcServerError, RpcServer as Server}; use jsonipc; use rpc_apis; diff --git a/parity/rpc_apis.rs b/parity/rpc_apis.rs index 275d8a1a4..4228743f9 100644 --- a/parity/rpc_apis.rs +++ b/parity/rpc_apis.rs @@ -20,12 +20,11 @@ use std::cmp::PartialEq; use std::str::FromStr; use std::sync::Arc; use util::RotatingLogger; -use util::network_settings::NetworkSettings; use ethcore::miner::{Miner, ExternalMiner}; use ethcore::client::Client; use ethcore::account_provider::AccountProvider; use ethsync::{ManageNetwork, SyncProvider}; -use ethcore_rpc::Extendable; +use ethcore_rpc::{Extendable, NetworkSettings}; pub use ethcore_rpc::ConfirmationsQueue; diff --git a/parity/run.rs b/parity/run.rs index 92a0637ca..d3aeaea2f 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -20,9 +20,10 @@ use std::io::ErrorKind; use ctrlc::CtrlC; use fdlimit::raise_fd_limit; use ethcore_logger::{Config as LogConfig, setup_log}; -use util::network_settings::NetworkSettings; -use util::{Colour, version, NetworkConfiguration, U256}; -use util::panics::{MayPanic, ForwardPanic, PanicHandler}; +use ethcore_rpc::NetworkSettings; +use ethsync::NetworkConfiguration; +use util::{Colour, version, U256}; +use io::{MayPanic, ForwardPanic, PanicHandler}; use ethcore::client::{Mode, Switch, DatabaseCompactionProfile, VMType, ChainNotify}; use ethcore::service::ClientService; use ethcore::account_provider::AccountProvider; diff --git a/parity/signer.rs b/parity/signer.rs index f6b2c3ef9..e61ab8411 100644 --- a/parity/signer.rs +++ b/parity/signer.rs @@ -18,7 +18,7 @@ use std::io; use std::sync::Arc; use std::path::PathBuf; use ansi_term::Colour; -use util::panics::{ForwardPanic, PanicHandler}; +use io::{ForwardPanic, PanicHandler}; use util::path::restrict_permissions_owner; use rpc_apis; use ethcore_signer as signer; diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 927ed37e5..b02241046 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -14,6 +14,7 @@ serde = "0.7.0" serde_json = "0.7.0" jsonrpc-core = "2.0" jsonrpc-http-server = { git = "https://github.com/ethcore/jsonrpc-http-server.git" } +ethcore-io = { path = "../util/io" } ethcore-util = { path = "../util" } ethcore = { path = "../ethcore" } ethash = { path = "../ethash" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 5899d0027..fe3e0f909 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -28,6 +28,7 @@ extern crate jsonrpc_core; extern crate jsonrpc_http_server; #[macro_use] extern crate ethcore_util as util; +extern crate ethcore_io as io; extern crate ethcore; extern crate ethsync; extern crate transient_hashmap; @@ -41,12 +42,12 @@ extern crate ethcore_devtools as devtools; use std::sync::Arc; use std::net::SocketAddr; -use util::panics::PanicHandler; +use io::PanicHandler; use self::jsonrpc_core::{IoHandler, IoDelegate}; pub use jsonrpc_http_server::{ServerBuilder, Server, RpcServerError}; pub mod v1; -pub use v1::{SigningQueue, ConfirmationsQueue}; +pub use v1::{SigningQueue, ConfirmationsQueue, NetworkSettings}; /// An object that can be extended with `IoDelegates` pub trait Extendable { diff --git a/rpc/src/v1/helpers/mod.rs b/rpc/src/v1/helpers/mod.rs index 4d3bd1207..315bb7a08 100644 --- a/rpc/src/v1/helpers/mod.rs +++ b/rpc/src/v1/helpers/mod.rs @@ -18,8 +18,10 @@ mod poll_manager; mod poll_filter; mod requests; mod signing_queue; +mod network_settings; pub use self::poll_manager::PollManager; pub use self::poll_filter::PollFilter; pub use self::requests::{TransactionRequest, FilledTransactionRequest, ConfirmationRequest, ConfirmationPayload, CallRequest}; pub use self::signing_queue::{ConfirmationsQueue, ConfirmationPromise, ConfirmationResult, SigningQueue, QueueEvent}; +pub use self::network_settings::NetworkSettings; diff --git a/util/src/network_settings.rs b/rpc/src/v1/helpers/network_settings.rs similarity index 100% rename from util/src/network_settings.rs rename to rpc/src/v1/helpers/network_settings.rs diff --git a/rpc/src/v1/impls/ethcore.rs b/rpc/src/v1/impls/ethcore.rs index acbb72b3d..4d1d59dd8 100644 --- a/rpc/src/v1/impls/ethcore.rs +++ b/rpc/src/v1/impls/ethcore.rs @@ -16,7 +16,6 @@ //! Ethcore-specific rpc implementation. use util::{RotatingLogger}; -use util::network_settings::NetworkSettings; use util::misc::version_data; use std::sync::{Arc, Weak}; use std::ops::Deref; @@ -26,7 +25,7 @@ use jsonrpc_core::*; use ethcore::miner::MinerService; use v1::traits::Ethcore; use v1::types::{Bytes, U256}; -use v1::helpers::{SigningQueue, ConfirmationsQueue}; +use v1::helpers::{SigningQueue, ConfirmationsQueue, NetworkSettings}; use v1::impls::signer_disabled_error; /// Ethcore implementation. diff --git a/rpc/src/v1/mod.rs b/rpc/src/v1/mod.rs index b4d3693f3..c462bd333 100644 --- a/rpc/src/v1/mod.rs +++ b/rpc/src/v1/mod.rs @@ -27,4 +27,4 @@ pub mod types; pub use self::traits::{Web3, Eth, EthFilter, EthSigning, Personal, PersonalSigner, Net, Ethcore, EthcoreSet, Traces, Rpc}; pub use self::impls::*; -pub use self::helpers::{SigningQueue, ConfirmationsQueue}; +pub use self::helpers::{SigningQueue, ConfirmationsQueue, NetworkSettings}; diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 4f2a5d7f7..8ae58a129 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -29,7 +29,7 @@ use ethcore::miner::{MinerOptions, GasPricer, MinerService, ExternalMiner, Miner use ethcore::account_provider::AccountProvider; use devtools::RandomTempPath; use util::Hashable; -use util::io::IoChannel; +use io::IoChannel; use util::{U256, H256, Uint}; use jsonrpc_core::IoHandler; use ethjson::blockchain::BlockChain; diff --git a/rpc/src/v1/tests/mocked/ethcore.rs b/rpc/src/v1/tests/mocked/ethcore.rs index 73875d131..f4cfec4fd 100644 --- a/rpc/src/v1/tests/mocked/ethcore.rs +++ b/rpc/src/v1/tests/mocked/ethcore.rs @@ -21,7 +21,7 @@ use v1::tests::helpers::TestMinerService; use v1::helpers::ConfirmationsQueue; use ethcore::client::{TestBlockChainClient}; use util::log::RotatingLogger; -use util::network_settings::NetworkSettings; +use v1::helpers::NetworkSettings; fn miner_service() -> Arc { Arc::new(TestMinerService::default()) diff --git a/rpc/src/v1/tests/mocked/manage_network.rs b/rpc/src/v1/tests/mocked/manage_network.rs index 10e947df7..2a1cfb6d4 100644 --- a/rpc/src/v1/tests/mocked/manage_network.rs +++ b/rpc/src/v1/tests/mocked/manage_network.rs @@ -15,7 +15,6 @@ // along with Parity. If not, see . use ethsync::{ManageNetwork, NetworkConfiguration}; -use util; pub struct TestManageNetwork; @@ -27,5 +26,5 @@ impl ManageNetwork for TestManageNetwork { fn add_reserved_peer(&self, _peer: String) -> Result<(), String> { Ok(()) } fn start_network(&self) {} fn stop_network(&self) {} - fn network_config(&self) -> NetworkConfiguration { NetworkConfiguration::from(util::NetworkConfiguration::new_local()) } + fn network_config(&self) -> NetworkConfiguration { NetworkConfiguration::new_local() } } diff --git a/scripts/targets.sh b/scripts/targets.sh index 1570199f5..5a8d41a3b 100644 --- a/scripts/targets.sh +++ b/scripts/targets.sh @@ -8,6 +8,8 @@ export TARGETS=" -p ethcore-rpc \ -p ethcore-signer \ -p ethcore-util \ + -p ethcore-network \ + -p ethcore-io \ -p ethkey \ -p ethstore \ -p ethsync \ diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 393c8033a..5f932451d 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -17,6 +17,7 @@ log = "0.3" env_logger = "0.3" ws = { git = "https://github.com/ethcore/ws-rs.git", branch = "stable" } ethcore-util = { path = "../util" } +ethcore-io = { path = "../util/io" } ethcore-rpc = { path = "../rpc" } parity-dapps-signer = { git = "https://github.com/ethcore/parity-ui.git", version = "0.6", optional = true} diff --git a/signer/src/lib.rs b/signer/src/lib.rs index 246020874..45383c7a1 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -48,6 +48,7 @@ extern crate env_logger; extern crate rand; extern crate ethcore_util as util; +extern crate ethcore_io as io; extern crate ethcore_rpc as rpc; extern crate jsonrpc_core; extern crate ws; diff --git a/signer/src/ws_server/mod.rs b/signer/src/ws_server/mod.rs index e99986205..4ebe96bfa 100644 --- a/signer/src/ws_server/mod.rs +++ b/signer/src/ws_server/mod.rs @@ -24,7 +24,7 @@ use std::default::Default; use std::ops::Drop; use std::sync::Arc; use std::net::SocketAddr; -use util::panics::{PanicHandler, OnPanicListener, MayPanic}; +use io::{PanicHandler, OnPanicListener, MayPanic}; use jsonrpc_core::{IoHandler, IoDelegate}; use rpc::{Extendable, ConfirmationsQueue}; diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 71ee5235a..0fbe241f8 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -13,6 +13,8 @@ ethcore-ipc-codegen = { path = "../ipc/codegen" } [dependencies] ethcore-util = { path = "../util" } +ethcore-network = { path = "../util/network" } +ethcore-io = { path = "../util/io" } ethcore = { path = "../ethcore" } clippy = { version = "0.0.80", optional = true} log = "0.3" diff --git a/sync/src/api.rs b/sync/src/api.rs index 5c0e028e3..c3dc3bf4a 100644 --- a/sync/src/api.rs +++ b/sync/src/api.rs @@ -16,17 +16,19 @@ use std::ops::*; use std::sync::Arc; -use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, - NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode}; -use util::{TimerToken, U256, H256, UtilError, Secret, Populatable}; +use network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, + NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, NetworkError}; +use util::{U256, H256, Secret, Populatable}; +use io::{TimerToken}; use ethcore::client::{BlockChainClient, ChainNotify}; use ethcore::header::BlockNumber; -use io::NetSyncIo; +use sync_io::NetSyncIo; use chain::{ChainSync, SyncStatus}; use std::net::{SocketAddr, AddrParseError}; use ipc::{BinaryConvertable, BinaryConvertError, IpcConfig}; use std::mem; use std::collections::VecDeque; +use std::str::FromStr; use parking_lot::RwLock; /// Ethereum sync protocol @@ -72,7 +74,7 @@ pub struct EthSync { impl EthSync { /// Creates and register protocol with the network service - pub fn new(config: SyncConfig, chain: Arc, network_config: NetworkConfiguration) -> Result, UtilError> { + pub fn new(config: SyncConfig, chain: Arc, network_config: NetworkConfiguration) -> Result, NetworkError> { let chain_sync = ChainSync::new(config, chain.deref()); let service = try!(NetworkService::new(try!(network_config.into_basic()))); let sync = Arc::new(EthSync{ @@ -213,7 +215,7 @@ impl ManageNetwork for EthSync { } } -#[derive(Binary, Debug, Clone)] +#[derive(Binary, Debug, Clone, PartialEq, Eq)] /// Network service configuration pub struct NetworkConfiguration { /// Directory path to store network configuration. None means nothing will be saved @@ -243,8 +245,25 @@ pub struct NetworkConfiguration { } impl NetworkConfiguration { + pub fn new() -> Self { + From::from(BasicNetworkConfiguration::new()) + } + + pub fn new_local() -> Self { + From::from(BasicNetworkConfiguration::new_local()) + } + + fn validate(&self) -> Result<(), AddrParseError> { + if let Some(ref addr) = self.listen_address { + try!(SocketAddr::from_str(&addr)); + } + if let Some(ref addr) = self.public_address { + try!(SocketAddr::from_str(&addr)); + } + Ok(()) + } + pub fn into_basic(self) -> Result { - use std::str::FromStr; Ok(BasicNetworkConfiguration { config_path: self.config_path, diff --git a/sync/src/blocks.rs b/sync/src/blocks.rs index e691f8b76..2e0aa5d4e 100644 --- a/sync/src/blocks.rs +++ b/sync/src/blocks.rs @@ -15,6 +15,7 @@ // along with Parity. If not, see . use util::*; +use network::NetworkError; use ethcore::header::{ Header as BlockHeader}; known_heap_size!(0, HeaderId, SyncBlock); @@ -228,7 +229,7 @@ impl BlockCollection { self.downloading_headers.contains(hash) || self.downloading_bodies.contains(hash) } - fn insert_body(&mut self, b: Bytes) -> Result<(), UtilError> { + fn insert_body(&mut self, b: Bytes) -> Result<(), NetworkError> { let body = UntrustedRlp::new(&b); let tx = try!(body.at(0)); let tx_root = ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here @@ -249,13 +250,13 @@ impl BlockCollection { }, None => { warn!("Got body with no header {}", h); - Err(UtilError::Network(NetworkError::BadProtocol)) + Err(NetworkError::BadProtocol) } } } None => { trace!(target: "sync", "Ignored unknown/stale block body"); - Err(UtilError::Network(NetworkError::BadProtocol)) + Err(NetworkError::BadProtocol) } } } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 1eb1bf2a4..3039b9e54 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -88,13 +88,14 @@ /// use util::*; +use network::*; use std::mem::{replace}; use ethcore::views::{HeaderView, BlockView}; use ethcore::header::{BlockNumber, Header as BlockHeader}; use ethcore::client::{BlockChainClient, BlockStatus, BlockID, BlockChainInfo, BlockImportError}; use ethcore::error::*; use ethcore::block::Block; -use io::SyncIo; +use sync_io::SyncIo; use time; use super::SyncConfig; use blocks::BlockCollection; @@ -1045,7 +1046,7 @@ impl ChainSync { } /// Send Status message - fn send_status(&mut self, io: &mut SyncIo) -> Result<(), UtilError> { + fn send_status(&mut self, io: &mut SyncIo) -> Result<(), NetworkError> { let mut packet = RlpStream::new_list(5); let chain = io.chain().chain_info(); packet.append(&(PROTOCOL_VERSION as u32)); @@ -1191,7 +1192,7 @@ impl ChainSync { fn return_rlp(io: &mut SyncIo, rlp: &UntrustedRlp, peer: PeerId, rlp_func: FRlp, error_func: FError) -> Result<(), PacketDecodeError> where FRlp : Fn(&SyncIo, &UntrustedRlp, PeerId) -> RlpResponseResult, - FError : FnOnce(UtilError) -> String + FError : FnOnce(NetworkError) -> String { let response = rlp_func(io, rlp, peer); match response { diff --git a/sync/src/lib.rs b/sync/src/lib.rs index f6884235b..7f991ee74 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -30,11 +30,12 @@ //! //! ```rust //! extern crate ethcore_util as util; +//! extern crate ethcore_io as io; //! extern crate ethcore; //! extern crate ethsync; //! use std::env; //! use std::sync::Arc; -//! use util::io::IoChannel; +//! use io::IoChannel; //! use ethcore::client::{Client, ClientConfig}; //! use ethsync::{EthSync, SyncConfig, ManageNetwork, NetworkConfiguration}; //! use ethcore::ethereum; @@ -55,7 +56,7 @@ //! miner, //! IoChannel::disconnected() //! ).unwrap(); -//! let sync = EthSync::new(SyncConfig::default(), client, NetworkConfiguration::from(util::NetworkConfiguration::new())).unwrap(); +//! let sync = EthSync::new(SyncConfig::default(), client, NetworkConfiguration::from(NetworkConfiguration::new())).unwrap(); //! sync.start_network(); //! } //! ``` @@ -64,6 +65,8 @@ extern crate log; #[macro_use] extern crate ethcore_util as util; +extern crate ethcore_network as network; +extern crate ethcore_io as io; extern crate ethcore; extern crate env_logger; extern crate time; @@ -77,7 +80,7 @@ extern crate parking_lot; mod chain; mod blocks; -mod io; +mod sync_io; #[cfg(test)] mod tests; @@ -88,6 +91,7 @@ mod api { } pub use api::{EthSync, SyncProvider, SyncClient, NetworkManagerClient, ManageNetwork, SyncConfig, - NetworkConfiguration, ServiceConfiguration}; + ServiceConfiguration, NetworkConfiguration}; pub use chain::{SyncStatus, SyncState}; +pub use network::{is_valid_node_url, NonReservedPeerMode, NetworkError}; diff --git a/sync/src/io.rs b/sync/src/sync_io.rs similarity index 93% rename from sync/src/io.rs rename to sync/src/sync_io.rs index 81c1d02a0..91070adc5 100644 --- a/sync/src/io.rs +++ b/sync/src/sync_io.rs @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use util::{NetworkContext, PeerId, PacketId,}; -use util::error::UtilError; +use network::{NetworkContext, PeerId, PacketId, NetworkError}; use ethcore::client::BlockChainClient; /// IO interface for the syning handler. @@ -27,9 +26,9 @@ pub trait SyncIo { /// Disconnect peer fn disconnect_peer(&mut self, peer_id: PeerId); /// Respond to current request with a packet. Can be called from an IO handler for incoming packet. - fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), UtilError>; + fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), NetworkError>; /// Send a packet to a peer. - fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError>; + fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec) -> Result<(), NetworkError>; /// Get the blockchain fn chain(&self) -> &BlockChainClient; /// Returns peer client identifier string @@ -69,11 +68,11 @@ impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> { self.network.disconnect_peer(peer_id); } - fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), UtilError>{ + fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), NetworkError>{ self.network.respond(packet_id, data) } - fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError>{ + fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec) -> Result<(), NetworkError>{ self.network.send(peer_id, packet_id, data) } diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index 8e4511e54..fba57681d 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -15,9 +15,10 @@ // along with Parity. If not, see . use util::*; +use network::*; use ethcore::client::{TestBlockChainClient, BlockChainClient}; use ethcore::header::BlockNumber; -use io::SyncIo; +use sync_io::SyncIo; use chain::ChainSync; use ::SyncConfig; @@ -48,7 +49,7 @@ impl<'p> SyncIo for TestIo<'p> { false } - fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { + fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), NetworkError> { self.queue.push_back(TestPacket { data: data, packet_id: packet_id, @@ -57,7 +58,7 @@ impl<'p> SyncIo for TestIo<'p> { Ok(()) } - fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { + fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec) -> Result<(), NetworkError> { self.queue.push_back(TestPacket { data: data, packet_id: packet_id, diff --git a/sync/src/tests/rpc.rs b/sync/src/tests/rpc.rs index 05f4f242e..f600130c1 100644 --- a/sync/src/tests/rpc.rs +++ b/sync/src/tests/rpc.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use super::super::NetworkConfiguration; -use util::NetworkConfiguration as BasicNetworkConfiguration; +use network::NetworkConfiguration as BasicNetworkConfiguration; use std::convert::From; use ipc::binary::{serialize, deserialize}; diff --git a/util/Cargo.toml b/util/Cargo.toml index 4246df921..d18fd68ed 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -12,11 +12,8 @@ log = "0.3" env_logger = "0.3" rustc-serialize = "0.3" arrayvec = "0.3" -mio = { git = "https://github.com/ethcore/mio", branch = "v0.5.x" } -nix ="0.5.0" rand = "0.3.12" time = "0.1.34" -tiny-keccak = "1.0" rocksdb = { git = "https://github.com/ethcore/rust-rocksdb" } lazy_static = "0.2" eth-secp256k1 = { git = "https://github.com/ethcore/rust-secp256k1" } @@ -24,17 +21,13 @@ rust-crypto = "0.2.34" elastic-array = { git = "https://github.com/ethcore/elastic-array" } heapsize = { version = "0.3", features = ["unstable"] } itertools = "0.4" -crossbeam = "0.2" -slab = "0.2" sha3 = { path = "sha3" } clippy = { version = "0.0.80", optional = true} -igd = "0.5.0" ethcore-devtools = { path = "../devtools" } libc = "0.2.7" vergen = "0.1" target_info = "0.1" bigint = { path = "bigint" } -chrono = "0.2" parking_lot = "0.2.6" using_queue = { path = "using_queue" } table = { path = "table" } diff --git a/util/io/Cargo.toml b/util/io/Cargo.toml new file mode 100644 index 000000000..7d5038a24 --- /dev/null +++ b/util/io/Cargo.toml @@ -0,0 +1,15 @@ +[package] +description = "Ethcore IO library" +homepage = "http://ethcore.io" +license = "GPL-3.0" +name = "ethcore-io" +version = "1.3.0" +authors = ["Ethcore "] + +[dependencies] +mio = { git = "https://github.com/ethcore/mio", branch = "v0.5.x" } +crossbeam = "0.2" +parking_lot = "0.2.6" +log = "0.3" +slab = "0.2" + diff --git a/util/src/io/mod.rs b/util/io/src/lib.rs similarity index 84% rename from util/src/io/mod.rs rename to util/io/src/lib.rs index 3229c95d5..b2a16e19b 100644 --- a/util/src/io/mod.rs +++ b/util/io/src/lib.rs @@ -19,8 +19,9 @@ //! Example usage for creating a network service and adding an IO handler: //! //! ```rust -//! extern crate ethcore_util; -//! use ethcore_util::*; +//! extern crate ethcore_io; +//! use ethcore_io::*; +//! use std::sync::Arc; //! //! struct MyHandler; //! @@ -52,8 +53,17 @@ //! // Drop the service //! } //! ``` + +extern crate mio; +#[macro_use] +extern crate log as rlog; +extern crate slab; +extern crate crossbeam; +extern crate parking_lot; + mod service; mod worker; +mod panics; use mio::{EventLoop, Token}; use std::fmt; @@ -63,6 +73,8 @@ use std::fmt; pub enum IoError { /// Low level error from mio crate Mio(::std::io::Error), + /// Error concerning the Rust standard library's IO subsystem. + StdIo(::std::io::Error), } impl fmt::Display for IoError { @@ -70,11 +82,18 @@ impl fmt::Display for IoError { // just defer to the std implementation for now. // we can refine the formatting when more variants are added. match *self { - IoError::Mio(ref std_err) => std_err.fmt(f) + IoError::Mio(ref std_err) => std_err.fmt(f), + IoError::StdIo(ref std_err) => std_err.fmt(f), } } } +impl From<::std::io::Error> for IoError { + fn from(err: ::std::io::Error) -> IoError { + IoError::StdIo(err) + } +} + impl From<::mio::NotifyError>> for IoError where Message: Send + Clone { fn from(_err: ::mio::NotifyError>) -> IoError { IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error")) @@ -105,19 +124,20 @@ pub trait IoHandler: Send + Sync where Message: Send + Sync + Clone + ' fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop>) {} } -pub use io::service::TimerToken; -pub use io::service::StreamToken; -pub use io::service::IoContext; -pub use io::service::IoService; -pub use io::service::IoChannel; -pub use io::service::IoManager; -pub use io::service::TOKENS_PER_HANDLER; +pub use service::TimerToken; +pub use service::StreamToken; +pub use service::IoContext; +pub use service::IoService; +pub use service::IoChannel; +pub use service::IoManager; +pub use service::TOKENS_PER_HANDLER; +pub use panics::{PanicHandler, MayPanic, OnPanicListener, ForwardPanic}; #[cfg(test)] mod tests { use std::sync::Arc; - use io::*; + use super::*; struct MyHandler; diff --git a/util/src/panics.rs b/util/io/src/panics.rs similarity index 100% rename from util/src/panics.rs rename to util/io/src/panics.rs diff --git a/util/src/io/service.rs b/util/io/src/service.rs similarity index 95% rename from util/src/io/service.rs rename to util/io/src/service.rs index 6e7dad4bb..a47e84e56 100644 --- a/util/src/io/service.rs +++ b/util/io/src/service.rs @@ -20,11 +20,9 @@ use std::collections::HashMap; use mio::*; use crossbeam::sync::chase_lev; use slab::Slab; -use error::*; -use io::{IoError, IoHandler}; -use io::worker::{Worker, Work, WorkType}; +use {IoError, IoHandler}; +use worker::{Worker, Work, WorkType}; use panics::*; - use parking_lot::{RwLock}; use std::sync::{Condvar as SCondvar, Mutex as SMutex}; @@ -92,7 +90,7 @@ impl IoContext where Message: Send + Clone + 'static { } /// Register a new IO timer. 'IoHandler::timeout' will be called with the token. - pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> { + pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), IoError> { try!(self.channel.send_io(IoMessage::AddTimer { token: token, delay: ms, @@ -102,7 +100,7 @@ impl IoContext where Message: Send + Clone + 'static { } /// Delete a timer. - pub fn clear_timer(&self, token: TimerToken) -> Result<(), UtilError> { + pub fn clear_timer(&self, token: TimerToken) -> Result<(), IoError> { try!(self.channel.send_io(IoMessage::RemoveTimer { token: token, handler_id: self.handler, @@ -111,7 +109,7 @@ impl IoContext where Message: Send + Clone + 'static { } /// Register a new IO stream. - pub fn register_stream(&self, token: StreamToken) -> Result<(), UtilError> { + pub fn register_stream(&self, token: StreamToken) -> Result<(), IoError> { try!(self.channel.send_io(IoMessage::RegisterStream { token: token, handler_id: self.handler, @@ -120,7 +118,7 @@ impl IoContext where Message: Send + Clone + 'static { } /// Deregister an IO stream. - pub fn deregister_stream(&self, token: StreamToken) -> Result<(), UtilError> { + pub fn deregister_stream(&self, token: StreamToken) -> Result<(), IoError> { try!(self.channel.send_io(IoMessage::DeregisterStream { token: token, handler_id: self.handler, @@ -129,7 +127,7 @@ impl IoContext where Message: Send + Clone + 'static { } /// Reregister an IO stream. - pub fn update_registration(&self, token: StreamToken) -> Result<(), UtilError> { + pub fn update_registration(&self, token: StreamToken) -> Result<(), IoError> { try!(self.channel.send_io(IoMessage::UpdateStreamRegistration { token: token, handler_id: self.handler, @@ -138,7 +136,7 @@ impl IoContext where Message: Send + Clone + 'static { } /// Broadcast a message to other IO clients - pub fn message(&self, message: Message) -> Result<(), UtilError> { + pub fn message(&self, message: Message) -> Result<(), IoError> { try!(self.channel.send(message)); Ok(()) } @@ -175,7 +173,7 @@ pub struct IoManager where Message: Send + Sync { impl IoManager where Message: Send + Sync + Clone + 'static { /// Creates a new instance and registers it with the event loop. - pub fn start(panic_handler: Arc, event_loop: &mut EventLoop>) -> Result<(), UtilError> { + pub fn start(panic_handler: Arc, event_loop: &mut EventLoop>) -> Result<(), IoError> { let (worker, stealer) = chase_lev::deque(); let num_workers = 4; let work_ready_mutex = Arc::new(SMutex::new(())); @@ -187,7 +185,7 @@ impl IoManager where Message: Send + Sync + Clone + 'static { IoChannel::new(event_loop.channel()), work_ready.clone(), work_ready_mutex.clone(), - panic_handler.clone() + panic_handler.clone(), ) ).collect(); @@ -360,12 +358,12 @@ impl MayPanic for IoService where Message: Send + Sync + Clone impl IoService where Message: Send + Sync + Clone + 'static { /// Starts IO event loop - pub fn start() -> Result, UtilError> { + pub fn start() -> Result, IoError> { let panic_handler = PanicHandler::new_in_arc(); let mut config = EventLoopConfig::new(); config.messages_per_tick(1024); - let mut event_loop = EventLoop::configured(config).expect("Error creating event loop"); - let channel = event_loop.channel(); + let mut event_loop = EventLoop::configured(config).expect("Error creating event loop"); + let channel = event_loop.channel(); let panic = panic_handler.clone(); let thread = thread::spawn(move || { let p = panic.clone(); diff --git a/util/src/io/worker.rs b/util/io/src/worker.rs similarity index 98% rename from util/src/io/worker.rs rename to util/io/src/worker.rs index c96bdf007..0176c467c 100644 --- a/util/src/io/worker.rs +++ b/util/io/src/worker.rs @@ -19,8 +19,8 @@ use std::mem; use std::thread::{JoinHandle, self}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use crossbeam::sync::chase_lev; -use io::service::{HandlerId, IoChannel, IoContext}; -use io::{IoHandler}; +use service::{HandlerId, IoChannel, IoContext}; +use IoHandler; use panics::*; use std::sync::{Condvar as SCondvar, Mutex as SMutex}; diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml new file mode 100644 index 000000000..d7012cc02 --- /dev/null +++ b/util/network/Cargo.toml @@ -0,0 +1,29 @@ +[package] +description = "Ethcore network library" +homepage = "http://ethcore.io" +license = "GPL-3.0" +name = "ethcore-network" +version = "1.3.0" +authors = ["Ethcore "] + +[dependencies] +log = "0.3" +mio = { git = "https://github.com/ethcore/mio", branch = "v0.5.x" } +rand = "0.3.12" +time = "0.1.34" +tiny-keccak = "1.0" +rust-crypto = "0.2.34" +slab = "0.2" +clippy = { version = "0.0.79", optional = true} +igd = "0.5.0" +libc = "0.2.7" +parking_lot = "0.2.6" +ansi_term = "0.7" +rustc-serialize = "0.3" +ethcore-io = { path = "../io" } +ethcore-util = { path = ".." } +ethcore-devtools = { path = "../../devtools" } + +[features] +default = [] +dev = ["clippy"] diff --git a/util/src/network/connection.rs b/util/network/src/connection.rs similarity index 97% rename from util/src/network/connection.rs rename to util/network/src/connection.rs index cbc9f39d7..8fd0d0948 100644 --- a/util/src/network/connection.rs +++ b/util/network/src/connection.rs @@ -20,17 +20,16 @@ use std::net::SocketAddr; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use mio::{Handler, Token, EventSet, EventLoop, PollOpt, TryRead, TryWrite}; use mio::tcp::*; -use hash::*; -use sha3::*; -use bytes::*; -use rlp::*; +use util::hash::*; +use util::sha3::*; +use util::bytes::*; +use util::rlp::*; use std::io::{self, Cursor, Read, Write}; use error::*; use io::{IoContext, StreamToken}; -use network::error::NetworkError; -use network::handshake::Handshake; -use network::stats::NetworkStats; -use crypto; +use handshake::Handshake; +use stats::NetworkStats; +use util::crypto; use rcrypto::blockmodes::*; use rcrypto::aessafe::*; use rcrypto::symmetriccipher::*; @@ -121,7 +120,7 @@ impl GenericConnection { } /// Writable IO handler. Called when the socket is ready to send. - pub fn writable(&mut self, io: &IoContext) -> Result where Message: Send + Clone { + pub fn writable(&mut self, io: &IoContext) -> Result where Message: Send + Clone { if self.send_queue.is_empty() { return Ok(WriteStatus::Complete) } @@ -288,7 +287,7 @@ pub struct EncryptedConnection { impl EncryptedConnection { /// Create an encrypted connection out of the handshake. - pub fn new(handshake: &mut Handshake) -> Result { + pub fn new(handshake: &mut Handshake) -> Result { let shared = try!(crypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_ephemeral)); let mut nonce_material = H512::new(); if handshake.originated { @@ -341,7 +340,7 @@ impl EncryptedConnection { } /// Send a packet - pub fn send_packet(&mut self, io: &IoContext, payload: &[u8]) -> Result<(), UtilError> where Message: Send + Clone { + pub fn send_packet(&mut self, io: &IoContext, payload: &[u8]) -> Result<(), NetworkError> where Message: Send + Clone { let mut header = RlpStream::new(); let len = payload.len() as usize; header.append_raw(&[(len >> 16) as u8, (len >> 8) as u8, len as u8], 1); @@ -368,7 +367,7 @@ impl EncryptedConnection { } /// Decrypt and authenticate an incoming packet header. Prepare for receiving payload. - fn read_header(&mut self, header: &[u8]) -> Result<(), UtilError> { + fn read_header(&mut self, header: &[u8]) -> Result<(), NetworkError> { if header.len() != ENCRYPTED_HEADER_LEN { return Err(From::from(NetworkError::Auth)); } @@ -398,7 +397,7 @@ impl EncryptedConnection { } /// Decrypt and authenticate packet payload. - fn read_payload(&mut self, payload: &[u8]) -> Result { + fn read_payload(&mut self, payload: &[u8]) -> Result { let padding = (16 - (self.payload_len % 16)) % 16; let full_length = self.payload_len + padding + 16; if payload.len() != full_length { @@ -436,7 +435,7 @@ impl EncryptedConnection { } /// Readable IO handler. Tracker receive status and returns decoded packet if avaialable. - pub fn readable(&mut self, io: &IoContext) -> Result, UtilError> where Message: Send + Clone{ + pub fn readable(&mut self, io: &IoContext) -> Result, NetworkError> where Message: Send + Clone{ try!(io.clear_timer(self.connection.token)); if let EncryptedConnectionState::Header = self.read_state { if let Some(data) = try!(self.connection.readable()) { @@ -459,7 +458,7 @@ impl EncryptedConnection { } /// Writable IO handler. Processes send queeue. - pub fn writable(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Clone { + pub fn writable(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Clone { try!(self.connection.writable(io)); Ok(()) } @@ -467,7 +466,7 @@ impl EncryptedConnection { #[test] pub fn test_encryption() { - use hash::*; + use util::hash::*; use std::str::FromStr; let key = H256::from_str("2212767d793a7a3d66f869ae324dd11bd17044b82c9f463b8a541a4d089efec5").unwrap(); let before = H128::from_str("12532abaec065082a3cf1da7d0136f15").unwrap(); @@ -496,7 +495,7 @@ mod tests { use std::io::{Read, Write, Error, Cursor, ErrorKind}; use mio::{EventSet}; use std::collections::VecDeque; - use bytes::*; + use util::bytes::*; use devtools::*; use io::*; diff --git a/util/src/network/discovery.rs b/util/network/src/discovery.rs similarity index 98% rename from util/src/network/discovery.rs rename to util/network/src/discovery.rs index d71ff1252..2b98f3230 100644 --- a/util/src/network/discovery.rs +++ b/util/network/src/discovery.rs @@ -14,23 +14,23 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use bytes::Bytes; +use util::bytes::Bytes; use std::net::SocketAddr; use std::collections::{HashSet, HashMap, BTreeMap, VecDeque}; use std::mem; use std::default::Default; use mio::*; use mio::udp::*; -use sha3::*; +use util::sha3::*; use time; -use hash::*; -use crypto::*; -use rlp::*; -use network::node_table::*; -use network::error::NetworkError; +use util::hash::*; +use util::crypto::*; +use util::rlp::*; +use node_table::*; +use error::NetworkError; use io::{StreamToken, IoContext}; -use network::PROTOCOL_VERSION; +use PROTOCOL_VERSION; const ADDRESS_BYTES_SIZE: u32 = 32; // Size of address type in bytes. const ADDRESS_BITS: u32 = 8 * ADDRESS_BYTES_SIZE; // Denoted by n in [Kademlia]. @@ -533,10 +533,10 @@ impl Discovery { #[cfg(test)] mod tests { use super::*; - use hash::*; + use util::hash::*; use std::net::*; - use network::node_table::*; - use crypto::KeyPair; + use node_table::*; + use util::crypto::KeyPair; use std::str::FromStr; use rustc_serialize::hex::FromHex; diff --git a/util/src/network/error.rs b/util/network/src/error.rs similarity index 79% rename from util/src/network/error.rs rename to util/network/src/error.rs index e4705fc9e..cbe1638ea 100644 --- a/util/src/network/error.rs +++ b/util/network/src/error.rs @@ -15,8 +15,9 @@ // along with Parity. If not, see . use io::IoError; -use crypto::CryptoError; -use rlp::*; +use util::crypto::CryptoError; +use util::rlp::*; +use util::UtilError; use std::fmt; #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -94,8 +95,16 @@ pub enum NetworkError { PeerNotFound, /// Peer is diconnected. Disconnect(DisconnectReason), + /// Util error. + Util(UtilError), /// Socket IO error. Io(IoError), + /// Error concerning the network address parsing subsystem. + AddressParse(::std::net::AddrParseError), + /// Error concerning the network address resolution subsystem. + AddressResolve(Option<::std::io::Error>), + /// Error concerning the Rust standard library's IO subsystem. + StdIo(::std::io::Error), } impl fmt::Display for NetworkError { @@ -109,6 +118,11 @@ impl fmt::Display for NetworkError { PeerNotFound => "Peer not found".into(), Disconnect(ref reason) => format!("Peer disconnected: {}", reason), Io(ref err) => format!("Socket I/O error: {}", err), + AddressParse(ref err) => format!("{}", err), + AddressResolve(Some(ref err)) => format!("{}", err), + AddressResolve(_) => "Failed to resolve network address.".into(), + StdIo(ref err) => format!("{}", err), + Util(ref err) => format!("{}", err), }; f.write_fmt(format_args!("Network error ({})", msg)) @@ -121,18 +135,36 @@ impl From for NetworkError { } } +impl From<::std::io::Error> for NetworkError { + fn from(err: ::std::io::Error) -> NetworkError { + NetworkError::StdIo(err) + } +} + impl From for NetworkError { fn from(err: IoError) -> NetworkError { NetworkError::Io(err) } } +impl From for NetworkError { + fn from(err: UtilError) -> NetworkError { + NetworkError::Util(err) + } +} + impl From for NetworkError { fn from(_err: CryptoError) -> NetworkError { NetworkError::Auth } } +impl From<::std::net::AddrParseError> for NetworkError { + fn from(err: ::std::net::AddrParseError) -> NetworkError { + NetworkError::AddressParse(err) + } +} + #[test] fn test_errors() { assert_eq!(DisconnectReason::ClientQuit, DisconnectReason::from_u8(8)); diff --git a/util/src/network/handshake.rs b/util/network/src/handshake.rs similarity index 95% rename from util/src/network/handshake.rs rename to util/network/src/handshake.rs index 162ccc618..e87197683 100644 --- a/util/src/network/handshake.rs +++ b/util/network/src/handshake.rs @@ -17,18 +17,17 @@ use std::sync::Arc; use rand::random; use mio::tcp::*; -use hash::*; -use rlp::*; -use sha3::Hashable; -use bytes::Bytes; -use crypto::*; -use crypto; -use network::connection::{Connection}; -use network::host::{HostInfo}; -use network::node_table::NodeId; +use util::hash::*; +use util::rlp::*; +use util::sha3::Hashable; +use util::bytes::Bytes; +use util::crypto::*; +use util::crypto; +use connection::{Connection}; +use host::{HostInfo}; +use node_table::NodeId; use error::*; -use network::error::NetworkError; -use network::stats::NetworkStats; +use stats::NetworkStats; use io::{IoContext, StreamToken}; #[derive(PartialEq, Eq, Debug)] @@ -84,7 +83,7 @@ const ECIES_OVERHEAD: usize = 113; impl Handshake { /// Create a new handshake object - pub fn new(token: StreamToken, id: Option<&NodeId>, socket: TcpStream, nonce: &H256, stats: Arc) -> Result { + pub fn new(token: StreamToken, id: Option<&NodeId>, socket: TcpStream, nonce: &H256, stats: Arc) -> Result { Ok(Handshake { id: if let Some(id) = id { id.clone()} else { NodeId::new() }, connection: Connection::new(token, socket, stats), @@ -107,7 +106,7 @@ impl Handshake { } /// Start a handhsake - pub fn start(&mut self, io: &IoContext, host: &HostInfo, originated: bool) -> Result<(), UtilError> where Message: Send + Clone{ + pub fn start(&mut self, io: &IoContext, host: &HostInfo, originated: bool) -> Result<(), NetworkError> where Message: Send + Clone{ self.originated = originated; io.register_timer(self.connection.token, HANDSHAKE_TIMEOUT).ok(); if originated { @@ -126,7 +125,7 @@ impl Handshake { } /// Readable IO handler. Drives the state change. - pub fn readable(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone { + pub fn readable(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Clone { if !self.expired() { while let Some(data) = try!(self.connection.readable()) { match self.state { @@ -155,14 +154,14 @@ impl Handshake { } /// Writabe IO handler. - pub fn writable(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Clone { + pub fn writable(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Clone { if !self.expired() { try!(self.connection.writable(io)); } Ok(()) } - fn set_auth(&mut self, host_secret: &Secret, sig: &[u8], remote_public: &[u8], remote_nonce: &[u8], remote_version: u64) -> Result<(), UtilError> { + fn set_auth(&mut self, host_secret: &Secret, sig: &[u8], remote_public: &[u8], remote_nonce: &[u8], remote_version: u64) -> Result<(), NetworkError> { self.id.clone_from_slice(remote_public); self.remote_nonce.clone_from_slice(remote_nonce); self.remote_version = remote_version; @@ -173,7 +172,7 @@ impl Handshake { } /// Parse, validate and confirm auth message - fn read_auth(&mut self, io: &IoContext, secret: &Secret, data: &[u8]) -> Result<(), UtilError> where Message: Send + Clone { + fn read_auth(&mut self, io: &IoContext, secret: &Secret, data: &[u8]) -> Result<(), NetworkError> where Message: Send + Clone { 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"); @@ -204,7 +203,7 @@ impl Handshake { Ok(()) } - fn read_auth_eip8(&mut self, io: &IoContext, secret: &Secret, data: &[u8]) -> Result<(), UtilError> where Message: Send + Clone { + fn read_auth_eip8(&mut self, io: &IoContext, secret: &Secret, data: &[u8]) -> Result<(), NetworkError> where Message: Send + Clone { trace!(target: "network", "Received EIP8 handshake auth from {:?}", self.connection.remote_addr_str()); self.auth_cipher.extend_from_slice(data); let auth = try!(ecies::decrypt(secret, &self.auth_cipher[0..2], &self.auth_cipher[2..])); @@ -219,7 +218,7 @@ impl Handshake { } /// Parse and validate ack message - fn read_ack(&mut self, secret: &Secret, data: &[u8]) -> Result<(), UtilError> { + fn read_ack(&mut self, secret: &Secret, data: &[u8]) -> Result<(), NetworkError> { 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"); @@ -247,7 +246,7 @@ impl Handshake { Ok(()) } - fn read_ack_eip8(&mut self, secret: &Secret, data: &[u8]) -> Result<(), UtilError> { + fn read_ack_eip8(&mut self, secret: &Secret, data: &[u8]) -> Result<(), NetworkError> { trace!(target: "network", "Received EIP8 handshake auth from {:?}", self.connection.remote_addr_str()); self.ack_cipher.extend_from_slice(data); let ack = try!(ecies::decrypt(secret, &self.ack_cipher[0..2], &self.ack_cipher[2..])); @@ -260,7 +259,7 @@ impl Handshake { } /// Sends auth message - fn write_auth(&mut self, io: &IoContext, secret: &Secret, public: &Public) -> Result<(), UtilError> where Message: Send + Clone { + fn write_auth(&mut self, io: &IoContext, secret: &Secret, public: &Public) -> Result<(), NetworkError> where Message: Send + Clone { trace!(target: "network", "Sending handshake auth to {:?}", self.connection.remote_addr_str()); let mut data = [0u8; /*Signature::SIZE*/ 65 + /*H256::SIZE*/ 32 + /*Public::SIZE*/ 64 + /*H256::SIZE*/ 32 + 1]; //TODO: use associated constants let len = data.len(); @@ -287,7 +286,7 @@ impl Handshake { } /// Sends ack message - fn write_ack(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Clone { + fn write_ack(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Clone { trace!(target: "network", "Sending handshake ack to {:?}", self.connection.remote_addr_str()); let mut data = [0u8; 1 + /*Public::SIZE*/ 64 + /*H256::SIZE*/ 32]; //TODO: use associated constants let len = data.len(); @@ -306,7 +305,7 @@ impl Handshake { } /// Sends EIP8 ack message - fn write_ack_eip8(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Clone { + fn write_ack_eip8(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Clone { trace!(target: "network", "Sending EIP8 handshake ack to {:?}", self.connection.remote_addr_str()); let mut rlp = RlpStream::new_list(3); rlp.append(self.ecdhe.public()); @@ -335,12 +334,12 @@ mod test { use std::str::FromStr; use rustc_serialize::hex::FromHex; use super::*; - use crypto::*; - use hash::*; + use util::crypto::*; + use util::hash::*; use io::*; use std::net::SocketAddr; use mio::tcp::TcpStream; - use network::stats::NetworkStats; + use stats::NetworkStats; fn check_auth(h: &Handshake, version: u64) { assert_eq!(h.id, Public::from_str("fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877").unwrap()); diff --git a/util/src/network/host.rs b/util/network/src/host.rs similarity index 97% rename from util/src/network/host.rs rename to util/network/src/host.rs index 7b1baf97a..bc5e5e6ca 100644 --- a/util/src/network/host.rs +++ b/util/network/src/host.rs @@ -27,21 +27,20 @@ use std::default::Default; use std::fs; use mio::*; use mio::tcp::*; -use hash::*; -use misc::*; -use crypto::*; -use sha3::Hashable; -use rlp::*; -use network::session::{Session, SessionData}; +use util::hash::*; +use util::crypto::*; +use util::Hashable; +use util::rlp::*; +use util::version; +use session::{Session, SessionData}; use error::*; use io::*; -use network::{NetworkProtocolHandler, NonReservedPeerMode, PROTOCOL_VERSION}; -use network::node_table::*; -use network::stats::NetworkStats; -use network::error::{NetworkError, DisconnectReason}; -use network::discovery::{Discovery, TableUpdates, NodeEntry}; -use network::ip_utils::{map_external_address, select_public_address}; -use path::restrict_permissions_owner; +use {NetworkProtocolHandler, NonReservedPeerMode, PROTOCOL_VERSION}; +use node_table::*; +use stats::NetworkStats; +use discovery::{Discovery, TableUpdates, NodeEntry}; +use ip_utils::{map_external_address, select_public_address}; +use util::path::restrict_permissions_owner; use parking_lot::{Mutex, RwLock}; type Slab = ::slab::Slab; @@ -224,7 +223,7 @@ impl<'s> NetworkContext<'s> { } /// Send a packet over the network to another peer. - pub fn send(&self, peer: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { + pub fn send(&self, peer: PeerId, packet_id: PacketId, data: Vec) -> Result<(), NetworkError> { let session = self.resolve_session(peer); if let Some(session) = session { try!(session.lock().send_packet(self.io, self.protocol, packet_id as u8, &data)); @@ -235,7 +234,7 @@ impl<'s> NetworkContext<'s> { } /// Respond to a current network message. Panics if no there is no packet in the context. If the session is expired returns nothing. - pub fn respond(&self, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { + pub fn respond(&self, packet_id: PacketId, data: Vec) -> Result<(), NetworkError> { assert!(self.session.is_some(), "Respond called without network context"); self.send(self.session_id.unwrap(), packet_id, data) } @@ -264,7 +263,7 @@ impl<'s> NetworkContext<'s> { } /// Register a new IO timer. 'IoHandler::timeout' will be called with the token. - pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> { + pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), NetworkError> { self.io.message(NetworkIoMessage::AddTimer { token: token, delay: ms, @@ -347,7 +346,7 @@ pub struct Host { impl Host { /// Create a new instance - pub fn new(config: NetworkConfiguration, stats: Arc) -> Result { + pub fn new(config: NetworkConfiguration, stats: Arc) -> Result { trace!(target: "host", "Creating new Host object"); let mut listen_address = match config.listen_address { @@ -428,7 +427,7 @@ impl Host { } } - pub fn add_reserved_node(&self, id: &str) -> Result<(), UtilError> { + pub fn add_reserved_node(&self, id: &str) -> Result<(), NetworkError> { let n = try!(Node::from_str(id)); let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() }; @@ -472,7 +471,7 @@ impl Host { } } - pub fn remove_reserved_node(&self, id: &str) -> Result<(), UtilError> { + pub fn remove_reserved_node(&self, id: &str) -> Result<(), NetworkError> { let n = try!(Node::from_str(id)); self.reserved_nodes.write().remove(&n.id); @@ -493,7 +492,7 @@ impl Host { r } - pub fn stop(&self, io: &IoContext) -> Result<(), UtilError> { + pub fn stop(&self, io: &IoContext) -> Result<(), NetworkError> { self.stopping.store(true, AtomicOrdering::Release); let mut to_kill = Vec::new(); for e in self.sessions.write().iter_mut() { @@ -509,7 +508,7 @@ impl Host { Ok(()) } - fn init_public_interface(&self, io: &IoContext) -> Result<(), UtilError> { + fn init_public_interface(&self, io: &IoContext) -> Result<(), NetworkError> { if self.info.read().public_endpoint.is_some() { return Ok(()); } @@ -684,7 +683,7 @@ impl Host { } #[cfg_attr(feature="dev", allow(block_in_if_condition_stmt))] - fn create_connection(&self, socket: TcpStream, id: Option<&NodeId>, io: &IoContext) -> Result<(), UtilError> { + fn create_connection(&self, socket: TcpStream, id: Option<&NodeId>, io: &IoContext) -> Result<(), NetworkError> { let nonce = self.info.write().next_nonce(); let mut sessions = self.sessions.write(); @@ -699,7 +698,7 @@ impl Host { }); match token { - Some(t) => io.register_stream(t), + Some(t) => Ok(try!(From::from(io.register_stream(t)))), None => { debug!(target: "network", "Max sessions reached"); Ok(()) @@ -756,7 +755,7 @@ impl Host { match session_result { Err(e) => { trace!(target: "network", "Session read error: {}:{:?} ({:?}) {:?}", token, s.id(), s.remote_addr(), e); - if let UtilError::Network(NetworkError::Disconnect(DisconnectReason::IncompatibleProtocol)) = e { + if let NetworkError::Disconnect(DisconnectReason::IncompatibleProtocol) = e { if let Some(id) = s.id() { if !self.reserved_nodes.read().contains(id) { self.nodes.write().mark_as_useless(id); diff --git a/util/src/network/ip_utils.rs b/util/network/src/ip_utils.rs similarity index 99% rename from util/src/network/ip_utils.rs rename to util/network/src/ip_utils.rs index 276c3fec0..9bdafb7c7 100644 --- a/util/src/network/ip_utils.rs +++ b/util/network/src/ip_utils.rs @@ -20,7 +20,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV use std::io; use igd::{PortMappingProtocol, search_gateway_from_timeout}; use std::time::Duration; -use network::node_table::{NodeEndpoint}; +use node_table::{NodeEndpoint}; /// Socket address extension for rustc beta. To be replaces with now unstable API pub trait SocketAddrExt { diff --git a/util/src/network/mod.rs b/util/network/src/lib.rs similarity index 80% rename from util/src/network/mod.rs rename to util/network/src/lib.rs index 95ebbf928..47e3a1256 100644 --- a/util/src/network/mod.rs +++ b/util/network/src/lib.rs @@ -19,8 +19,9 @@ //! Example usage for craeting a network service and adding an IO handler: //! //! ```rust -//! extern crate ethcore_util as util; -//! use util::*; +//! extern crate ethcore_network as net; +//! use net::*; +//! use std::sync::Arc; //! //! struct MyHandler; //! @@ -40,10 +41,6 @@ //! fn disconnected(&self, io: &NetworkContext, peer: &PeerId) { //! println!("Disconnected {}", peer); //! } -//! -//! fn timeout(&self, io: &NetworkContext, timer: TimerToken) { -//! println!("Timeout {}", timer); -//! } //! } //! //! fn main () { @@ -56,6 +53,26 @@ //! // Drop the service //! } //! ``` + + +#[macro_use] +extern crate log; +extern crate ethcore_io as io; +extern crate ethcore_util as util; +extern crate parking_lot; +extern crate mio; +extern crate tiny_keccak; +extern crate crypto as rcrypto; +extern crate rand; +extern crate time; +extern crate ansi_term; //TODO: remove this +extern crate rustc_serialize; +extern crate igd; +extern crate libc; +extern crate slab; +#[cfg(test)] +extern crate ethcore_devtools as devtools; + mod host; mod connection; mod handshake; @@ -70,17 +87,17 @@ mod ip_utils; #[cfg(test)] mod tests; -pub use network::host::PeerId; -pub use network::host::PacketId; -pub use network::host::NetworkContext; -pub use network::service::NetworkService; -pub use network::host::NetworkIoMessage; -pub use network::error::NetworkError; -pub use network::host::NetworkConfiguration; -pub use network::stats::NetworkStats; +pub use host::PeerId; +pub use host::PacketId; +pub use host::NetworkContext; +pub use service::NetworkService; +pub use host::NetworkIoMessage; +pub use error::NetworkError; +pub use host::NetworkConfiguration; +pub use stats::NetworkStats; use io::TimerToken; -pub use network::node_table::is_valid_node_url; +pub use node_table::is_valid_node_url; const PROTOCOL_VERSION: u32 = 4; diff --git a/util/src/network/node_table.rs b/util/network/src/node_table.rs similarity index 97% rename from util/src/network/node_table.rs rename to util/network/src/node_table.rs index 5fd2a10e8..31045171e 100644 --- a/util/src/network/node_table.rs +++ b/util/network/src/node_table.rs @@ -25,12 +25,12 @@ use std::path::{PathBuf}; use std::fmt; use std::fs; use std::io::{Read, Write}; -use hash::*; -use rlp::*; +use util::hash::*; +use util::rlp::*; use time::Tm; -use error::*; -use network::discovery::{TableUpdates, NodeEntry}; -use network::ip_utils::*; +use error::NetworkError; +use discovery::{TableUpdates, NodeEntry}; +use ip_utils::*; pub use rustc_serialize::json::Json; /// Node public key @@ -107,18 +107,18 @@ impl NodeEndpoint { } impl FromStr for NodeEndpoint { - type Err = UtilError; + type Err = NetworkError; /// Create endpoint from string. Performs name resolution if given a host name. - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { let address = s.to_socket_addrs().map(|mut i| i.next()); match address { Ok(Some(a)) => Ok(NodeEndpoint { address: a, udp_port: a.port() }), - Ok(_) => Err(UtilError::AddressResolve(None)), - Err(e) => Err(UtilError::AddressResolve(Some(e))) + Ok(_) => Err(NetworkError::AddressResolve(None)), + Err(e) => Err(NetworkError::AddressResolve(Some(e))) } } } @@ -161,7 +161,7 @@ impl Display for Node { } impl FromStr for Node { - type Err = UtilError; + type Err = NetworkError; fn from_str(s: &str) -> Result { let (id, endpoint) = if s.len() > 136 && &s[0..8] == "enode://" && &s[136..137] == "@" { (try!(NodeId::from_str(&s[8..136])), try!(NodeEndpoint::from_str(&s[137..]))) @@ -357,7 +357,7 @@ mod tests { use super::*; use std::str::FromStr; use std::net::*; - use hash::*; + use util::hash::*; use devtools::*; #[test] diff --git a/util/src/network/service.rs b/util/network/src/service.rs similarity index 89% rename from util/src/network/service.rs rename to util/network/src/service.rs index 10f8d3e90..3ab1f0301 100644 --- a/util/src/network/service.rs +++ b/util/network/src/service.rs @@ -14,12 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use error::*; -use panics::*; -use network::{NetworkProtocolHandler, NetworkConfiguration}; -use network::error::NetworkError; -use network::host::{Host, NetworkContext, NetworkIoMessage, ProtocolId}; -use network::stats::NetworkStats; +use {NetworkProtocolHandler, NetworkConfiguration, NonReservedPeerMode}; +use error::NetworkError; +use host::{Host, NetworkContext, NetworkIoMessage, ProtocolId}; +use stats::NetworkStats; use io::*; use parking_lot::RwLock; use std::sync::Arc; @@ -55,7 +53,7 @@ pub struct NetworkService { impl NetworkService { /// Starts IO event loop - pub fn new(config: NetworkConfiguration) -> Result { + pub fn new(config: NetworkConfiguration) -> Result { let host_handler = Arc::new(HostHandler { public_url: RwLock::new(None) }); let panic_handler = PanicHandler::new_in_arc(); let io_service = try!(IoService::::start()); @@ -117,7 +115,7 @@ impl NetworkService { } /// Start network IO - pub fn start(&self) -> Result<(), UtilError> { + pub fn start(&self) -> Result<(), NetworkError> { let mut host = self.host.write(); if host.is_none() { let h = Arc::new(try!(Host::new(self.config.clone(), self.stats.clone()))); @@ -133,7 +131,7 @@ impl NetworkService { } /// Stop network IO - pub fn stop(&self) -> Result<(), UtilError> { + pub fn stop(&self) -> Result<(), NetworkError> { let mut host = self.host.write(); if let Some(ref host) = *host { let io = IoContext::new(self.io_service.channel(), 0); //TODO: take token id from host @@ -144,7 +142,7 @@ impl NetworkService { } /// Try to add a reserved peer. - pub fn add_reserved_peer(&self, peer: &str) -> Result<(), UtilError> { + pub fn add_reserved_peer(&self, peer: &str) -> Result<(), NetworkError> { let host = self.host.read(); if let Some(ref host) = *host { host.add_reserved_node(peer) @@ -154,7 +152,7 @@ impl NetworkService { } /// Try to remove a reserved peer. - pub fn remove_reserved_peer(&self, peer: &str) -> Result<(), UtilError> { + pub fn remove_reserved_peer(&self, peer: &str) -> Result<(), NetworkError> { let host = self.host.read(); if let Some(ref host) = *host { host.remove_reserved_node(peer) @@ -164,7 +162,7 @@ impl NetworkService { } /// Set the non-reserved peer mode. - pub fn set_non_reserved_mode(&self, mode: ::network::NonReservedPeerMode) { + pub fn set_non_reserved_mode(&self, mode: NonReservedPeerMode) { let host = self.host.read(); if let Some(ref host) = *host { let io_ctxt = IoContext::new(self.io_service.channel(), 0); diff --git a/util/src/network/session.rs b/util/network/src/session.rs similarity index 91% rename from util/src/network/session.rs rename to util/network/src/session.rs index d90ecb062..4e6dd1c8a 100644 --- a/util/src/network/session.rs +++ b/util/network/src/session.rs @@ -19,16 +19,15 @@ use std::io; use std::sync::*; use mio::*; use mio::tcp::*; -use rlp::*; -use hash::*; -use network::connection::{EncryptedConnection, Packet, Connection}; -use network::handshake::Handshake; -use error::*; +use util::rlp::*; +use util::hash::*; +use connection::{EncryptedConnection, Packet, Connection}; +use handshake::Handshake; use io::{IoContext, StreamToken}; -use network::error::{NetworkError, DisconnectReason}; -use network::host::*; -use network::node_table::NodeId; -use network::stats::NetworkStats; +use error::{NetworkError, DisconnectReason}; +use host::*; +use node_table::NodeId; +use stats::NetworkStats; use time; const PING_TIMEOUT_SEC: u64 = 30; @@ -125,7 +124,7 @@ impl Session { /// Create a new session out of comepleted handshake. This clones the handshake connection object /// and leaves the handhsake in limbo to be deregistered from the event loop. pub fn new(io: &IoContext, socket: TcpStream, token: StreamToken, id: Option<&NodeId>, - nonce: &H256, stats: Arc, host: &HostInfo) -> Result + nonce: &H256, stats: Arc, host: &HostInfo) -> Result where Message: Send + Clone { let originated = id.is_some(); let mut handshake = Handshake::new(token, id, socket, nonce, stats).expect("Can't create handshake"); @@ -147,7 +146,7 @@ impl Session { }) } - fn complete_handshake(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Sync + Clone { + fn complete_handshake(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone { let connection = if let State::Handshake(ref mut h) = self.state { self.info.id = Some(h.id.clone()); try!(EncryptedConnection::new(h)) @@ -201,7 +200,7 @@ impl Session { } /// Readable IO handler. Returns packet data if available. - pub fn readable(&mut self, io: &IoContext, host: &HostInfo) -> Result where Message: Send + Sync + Clone { + pub fn readable(&mut self, io: &IoContext, host: &HostInfo) -> Result where Message: Send + Sync + Clone { if self.expired() { return Ok(SessionData::None) } @@ -232,7 +231,7 @@ impl Session { } /// Writable IO handler. Sends pending packets. - pub fn writable(&mut self, io: &IoContext, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Sync + Clone { + pub fn writable(&mut self, io: &IoContext, _host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone { match self.state { State::Handshake(ref mut h) => h.writable(io), State::Session(ref mut s) => s.writable(io), @@ -245,7 +244,7 @@ impl Session { } /// Register the session socket with the event loop - pub fn register_socket>(&self, reg: Token, event_loop: &mut EventLoop) -> Result<(), UtilError> { + pub fn register_socket>(&self, reg: Token, event_loop: &mut EventLoop) -> Result<(), NetworkError> { if self.expired() { return Ok(()); } @@ -254,19 +253,19 @@ impl Session { } /// Update registration with the event loop. Should be called at the end of the IO handler. - pub fn update_socket(&self, reg:Token, event_loop: &mut EventLoop) -> Result<(), UtilError> { + pub fn update_socket(&self, reg:Token, event_loop: &mut EventLoop) -> Result<(), NetworkError> { try!(self.connection().update_socket(reg, event_loop)); Ok(()) } /// Delete registration - pub fn deregister_socket(&self, event_loop: &mut EventLoop) -> Result<(), UtilError> { + pub fn deregister_socket(&self, event_loop: &mut EventLoop) -> Result<(), NetworkError> { try!(self.connection().deregister_socket(event_loop)); Ok(()) } /// Send a protocol packet to peer. - pub fn send_packet(&mut self, io: &IoContext, protocol: &str, packet_id: u8, data: &[u8]) -> Result<(), UtilError> + pub fn send_packet(&mut self, io: &IoContext, protocol: &str, packet_id: u8, data: &[u8]) -> Result<(), NetworkError> where Message: Send + Sync + Clone { if self.info.capabilities.is_empty() || !self.had_hello { debug!(target: "network", "Sending to unconfirmed session {}, protocol: {}, packet: {}", self.token(), protocol, packet_id); @@ -313,7 +312,7 @@ impl Session { self.connection().token() } - fn read_packet(&mut self, io: &IoContext, packet: Packet, host: &HostInfo) -> Result + fn read_packet(&mut self, io: &IoContext, packet: Packet, host: &HostInfo) -> Result where Message: Send + Sync + Clone { if packet.data.len() < 2 { return Err(From::from(NetworkError::BadProtocol)); @@ -369,7 +368,7 @@ impl Session { } } - fn write_hello(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Sync + Clone { + fn write_hello(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone { let mut rlp = RlpStream::new(); rlp.append_raw(&[PACKET_HELLO as u8], 0); rlp.begin_list(5) @@ -381,7 +380,7 @@ impl Session { self.send(io, rlp) } - fn read_hello(&mut self, io: &IoContext, rlp: &UntrustedRlp, host: &HostInfo) -> Result<(), UtilError> + fn read_hello(&mut self, io: &IoContext, rlp: &UntrustedRlp, host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone { let protocol = try!(rlp.val_at::(0)); let client_version = try!(rlp.val_at::(1)); @@ -436,14 +435,14 @@ impl Session { } /// Senf ping packet - pub fn send_ping(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Sync + Clone { + pub fn send_ping(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Sync + Clone { try!(self.send(io, try!(Session::prepare(PACKET_PING)))); self.ping_time_ns = time::precise_time_ns(); self.pong_time_ns = None; Ok(()) } - fn send_pong(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Sync + Clone { + fn send_pong(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Sync + Clone { self.send(io, try!(Session::prepare(PACKET_PONG))) } @@ -459,14 +458,14 @@ impl Session { NetworkError::Disconnect(reason) } - fn prepare(packet_id: u8) -> Result { + fn prepare(packet_id: u8) -> Result { let mut rlp = RlpStream::new(); rlp.append(&(packet_id as u32)); rlp.begin_list(0); Ok(rlp) } - fn send(&mut self, io: &IoContext, rlp: RlpStream) -> Result<(), UtilError> where Message: Send + Sync + Clone { + fn send(&mut self, io: &IoContext, rlp: RlpStream) -> Result<(), NetworkError> where Message: Send + Sync + Clone { match self.state { State::Handshake(_) => { warn!(target:"network", "Unexpected send request"); diff --git a/util/src/network/stats.rs b/util/network/src/stats.rs similarity index 100% rename from util/src/network/stats.rs rename to util/network/src/stats.rs diff --git a/util/src/network/tests.rs b/util/network/src/tests.rs similarity index 98% rename from util/src/network/tests.rs rename to util/network/src/tests.rs index 450b5dd00..9c3753813 100644 --- a/util/src/network/tests.rs +++ b/util/network/src/tests.rs @@ -14,13 +14,13 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +use super::*; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::thread; use std::time::*; -use common::*; -use network::*; +use util::common::*; use io::TimerToken; -use crypto::KeyPair; +use util::crypto::KeyPair; pub struct TestProtocol { drop_session: bool, @@ -98,7 +98,7 @@ fn net_service() { #[test] fn net_connect() { - ::log::init_log(); + ::util::log::init_log(); let key1 = KeyPair::create().unwrap(); let mut config1 = NetworkConfiguration::new_local(); config1.use_secret = Some(key1.secret().clone()); diff --git a/util/src/error.rs b/util/src/error.rs index 1460c152c..afc49b27c 100644 --- a/util/src/error.rs +++ b/util/src/error.rs @@ -17,9 +17,7 @@ //! General error types for use in ethcore. use rustc_serialize::hex::FromHexError; -use network::NetworkError; use rlp::DecoderError; -use io; use std::fmt; use hash::H256; @@ -50,18 +48,10 @@ pub enum UtilError { Crypto(::crypto::CryptoError), /// Error concerning the Rust standard library's IO subsystem. StdIo(::std::io::Error), - /// Error concerning our IO utility subsystem. - Io(io::IoError), - /// Error concerning the network address parsing subsystem. - AddressParse(::std::net::AddrParseError), - /// Error concerning the network address resolution subsystem. - AddressResolve(Option<::std::io::Error>), /// Error concerning the hex conversion logic. FromHex(FromHexError), /// Error concerning the database abstraction logic. BaseData(BaseDataError), - /// Error concerning the network subsystem. - Network(NetworkError), /// Error concerning the RLP decoder. Decoder(DecoderError), /// Miscellaneous error described by a string. @@ -77,13 +67,8 @@ impl fmt::Display for UtilError { match *self { UtilError::Crypto(ref err) => f.write_fmt(format_args!("{}", err)), UtilError::StdIo(ref err) => f.write_fmt(format_args!("{}", err)), - UtilError::Io(ref err) => f.write_fmt(format_args!("{}", err)), - UtilError::AddressParse(ref err) => f.write_fmt(format_args!("{}", err)), - UtilError::AddressResolve(Some(ref err)) => f.write_fmt(format_args!("{}", err)), - UtilError::AddressResolve(_) => f.write_str("Failed to resolve network address."), UtilError::FromHex(ref err) => f.write_fmt(format_args!("{}", err)), UtilError::BaseData(ref err) => f.write_fmt(format_args!("{}", err)), - UtilError::Network(ref err) => f.write_fmt(format_args!("{}", err)), UtilError::Decoder(ref err) => f.write_fmt(format_args!("{}", err)), UtilError::SimpleString(ref msg) => f.write_str(msg), UtilError::BadSize => f.write_str("Bad input size."), @@ -143,36 +128,18 @@ impl From for UtilError { } } -impl From for UtilError { - fn from(err: NetworkError) -> UtilError { - UtilError::Network(err) - } -} - impl From<::std::io::Error> for UtilError { fn from(err: ::std::io::Error) -> UtilError { UtilError::StdIo(err) } } -impl From for UtilError { - fn from(err: io::IoError) -> UtilError { - UtilError::Io(err) - } -} - impl From<::crypto::CryptoError> for UtilError { fn from(err: ::crypto::CryptoError) -> UtilError { UtilError::Crypto(err) } } -impl From<::std::net::AddrParseError> for UtilError { - fn from(err: ::std::net::AddrParseError) -> UtilError { - UtilError::AddressParse(err) - } -} - impl From<::rlp::DecoderError> for UtilError { fn from(err: ::rlp::DecoderError) -> UtilError { UtilError::Decoder(err) diff --git a/util/src/lib.rs b/util/src/lib.rs index 89ad98a7f..e0867e093 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -85,12 +85,9 @@ //! cargo build --release //! ``` -extern crate slab; extern crate rustc_serialize; -extern crate mio; extern crate rand; extern crate rocksdb; -extern crate tiny_keccak; #[macro_use] extern crate heapsize; #[macro_use] @@ -98,20 +95,17 @@ extern crate lazy_static; #[macro_use] extern crate itertools; extern crate env_logger; -extern crate time; extern crate crypto as rcrypto; extern crate secp256k1; extern crate arrayvec; extern crate elastic_array; -extern crate crossbeam; #[macro_use] extern crate log as rlog; -extern crate igd; +extern crate time; extern crate ethcore_devtools as devtools; extern crate libc; extern crate target_info; extern crate bigint; -extern crate chrono; extern crate parking_lot; pub extern crate using_queue; pub extern crate table; @@ -144,11 +138,7 @@ pub mod nibbleslice; pub mod nibblevec; mod heapsizeof; pub mod semantic_version; -pub mod io; -pub mod network; pub mod log; -pub mod panics; -pub mod network_settings; pub mod path; pub mod snappy; mod timer; @@ -166,8 +156,6 @@ pub use triehash::*; pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut}; pub use nibbleslice::*; pub use semantic_version::*; -pub use network::*; -pub use io::*; pub use log::*; pub use kvdb::*; pub use timer::*;