From 2cc690f31f27adb5e761cc676d90b7c94c1afec9 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 19 Feb 2016 12:54:51 +0100 Subject: [PATCH 1/3] Better user errors. Fixed up README. --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ README.md | 2 +- parity/main.rs | 64 ++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b69736c2e..34b618c2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,7 +12,9 @@ dependencies = [ "ethcore-util 0.9.99", "ethsync 0.9.99", "fdlimit 0.1.0", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 5b59b26f1..f4163253b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ ethcore-rpc = { path = "rpc", optional = true } fdlimit = { path = "util/fdlimit" } target_info = "0.1" daemonize = "0.2" +regex = "0.1" +lazy_static = "0.1" [features] default = ["rpc"] diff --git a/README.md b/README.md index b840195d8..4e238fd6f 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ cd .. curl -sf https://raw.githubusercontent.com/brson/multirust/master/blastoff.sh | sudo sh -s -- --yes # install beta and make it default -sudo multirust default beta +multirust default beta # download and build parity git clone https://github.com/ethcore/parity diff --git a/parity/main.rs b/parity/main.rs index 3279e1fed..aca2b9c03 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -26,17 +26,21 @@ extern crate ethcore; extern crate ethsync; #[macro_use] extern crate log as rlog; +#[macro_use] +extern crate lazy_static; extern crate env_logger; extern crate ctrlc; extern crate fdlimit; extern crate target_info; extern crate daemonize; +extern crate regex; #[cfg(feature = "rpc")] extern crate ethcore_rpc as rpc; use std::net::{SocketAddr}; use std::env; +use std::process::exit; use rlog::{LogLevelFilter}; use env_logger::LogBuilder; use ctrlc::CtrlC; @@ -51,6 +55,7 @@ use ethsync::EthSync; use docopt::Docopt; use target_info::Target; use daemonize::Daemonize; +use regex::Regex; const USAGE: &'static str = " Parity. Ethereum Client. @@ -150,6 +155,16 @@ By Wood/Paronyan/Kotewicz/Drwięga/Volf.\ ", env!("CARGO_PKG_VERSION"), Target::arch(), Target::env(), Target::os()); } +fn die_with_message(msg: &str) -> ! { + println!("ERROR: {}", msg); + exit(1); +} + +#[macro_export] +macro_rules! die { + ($($arg:tt)*) => (die_with_message(&format!("{}", format_args!($($arg)*)))); +} + struct Configuration { args: Args } @@ -174,7 +189,17 @@ impl Configuration { "frontier" | "mainnet" => ethereum::new_frontier(), "morden" | "testnet" => ethereum::new_morden(), "olympic" => ethereum::new_olympic(), - f => Spec::from_json_utf8(contents(f).expect("Couldn't read chain specification file. Sure it exists?").as_ref()), + f => Spec::from_json_utf8(contents(f).unwrap_or_else(|_| die!("{}: Couldn't read chain specification file. Sure it exists?", f)).as_ref()), + } + } + + fn normalize_enode(e: &str) -> Option { + lazy_static! { + static ref RE: Regex = Regex::new(r"^enode://([0-9a-fA-F]{64})@(\d+\.\d+\.\d+\.\d+):(\d+)$").unwrap(); + } + match RE.is_match(e) { + true => Some(e.to_owned()), + false => None, } } @@ -182,7 +207,7 @@ impl Configuration { if self.args.flag_no_bootstrap { Vec::new() } else { match self.args.arg_enode.len() { 0 => spec.nodes().clone(), - _ => self.args.arg_enode.clone(), // TODO check format first. + _ => self.args.arg_enode.iter().map(|s| Self::normalize_enode(s).unwrap_or_else(||die!("{}: Invalid node address format given for a boot node.", s))).collect(), } } } @@ -197,7 +222,7 @@ impl Configuration { public_address = SocketAddr::from_str(self.args.flag_public_address.as_ref()).expect("Invalid public address given with --public-address"); } Some(ref a) => { - public_address = SocketAddr::from_str(a.as_ref()).expect("Invalid listen/public address given with --address"); + public_address = SocketAddr::from_str(a.as_ref()).unwrap_or_else(|_|die!("{}: Invalid listen/public address given with --address", a)); listen_address = public_address; } }; @@ -205,17 +230,28 @@ impl Configuration { (listen_address, public_address) } + fn net_settings(&self, spec: &Spec) -> NetworkConfiguration { + let mut ret = NetworkConfiguration::new(); + ret.nat_enabled = self.args.flag_upnp; + ret.boot_nodes = self.init_nodes(spec); + let (listen, public) = self.net_addresses(); + ret.listen_address = listen; + ret.public_address = public; + ret.use_secret = self.args.flag_node_key.as_ref().map(|s| Secret::from_str(&s).unwrap_or_else(|_| s.as_bytes().sha3())); + ret + } + fn execute(&self) { if self.args.flag_version { print_version(); return; } if self.args.cmd_daemon { - let daemonize = Daemonize::new().pid_file(self.args.arg_pid_file.clone()).chown_pid_file(true); - match daemonize.start() { - Ok(_) => info!("Daemonized"), - Err(e) => { error!("{}", e); return; }, - } + Daemonize::new() + .pid_file(self.args.arg_pid_file.clone()) + .chown_pid_file(true) + .start() + .unwrap_or_else(|e| die!("Couldn't daemonize; {}", e)); } self.execute_client(); } @@ -227,15 +263,7 @@ impl Configuration { unsafe { ::fdlimit::raise_fd_limit(); } let spec = self.spec(); - - // Configure network - let mut net_settings = NetworkConfiguration::new(); - net_settings.nat_enabled = self.args.flag_upnp; - net_settings.boot_nodes = self.init_nodes(&spec); - let (listen, public) = self.net_addresses(); - net_settings.listen_address = listen; - net_settings.public_address = public; - net_settings.use_secret = self.args.flag_node_key.as_ref().map(|s| Secret::from_str(&s).expect("Invalid key string")); + let net_settings = self.net_settings(&spec); // Build client let mut service = ClientService::start(spec, net_settings, &Path::new(&self.path())).unwrap(); @@ -265,11 +293,13 @@ impl Configuration { fn wait_for_exit(client_service: &ClientService) { let exit = Arc::new(Condvar::new()); + // Handle possible exits let e = exit.clone(); CtrlC::set_handler(move || { e.notify_all(); }); let e = exit.clone(); client_service.on_panic(move |_reason| { e.notify_all(); }); + // Wait for signal let mutex = Mutex::new(()); let _ = exit.wait(mutex.lock().unwrap()).unwrap(); From 6c82e405ddae5de03b784d2608c66a9dc4f8b683 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 19 Feb 2016 19:42:23 +0100 Subject: [PATCH 2/3] Remove regex &c., use network code for enode ID. --- Cargo.lock | 1 - Cargo.toml | 2 -- parity/main.rs | 12 +++--------- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 34b618c2b..8891e4658 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,7 +12,6 @@ dependencies = [ "ethcore-util 0.9.99", "ethsync 0.9.99", "fdlimit 0.1.0", - "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index f4163253b..5b59b26f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,6 @@ ethcore-rpc = { path = "rpc", optional = true } fdlimit = { path = "util/fdlimit" } target_info = "0.1" daemonize = "0.2" -regex = "0.1" -lazy_static = "0.1" [features] default = ["rpc"] diff --git a/parity/main.rs b/parity/main.rs index aca2b9c03..0f1f77606 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -26,8 +26,6 @@ extern crate ethcore; extern crate ethsync; #[macro_use] extern crate log as rlog; -#[macro_use] -extern crate lazy_static; extern crate env_logger; extern crate ctrlc; extern crate fdlimit; @@ -40,11 +38,13 @@ extern crate ethcore_rpc as rpc; use std::net::{SocketAddr}; use std::env; +use std::from_str::FromStr; use std::process::exit; use rlog::{LogLevelFilter}; use env_logger::LogBuilder; use ctrlc::CtrlC; use util::*; +use util::network::node::Node; use util::panics::MayPanic; use ethcore::spec::*; use ethcore::client::*; @@ -194,13 +194,7 @@ impl Configuration { } fn normalize_enode(e: &str) -> Option { - lazy_static! { - static ref RE: Regex = Regex::new(r"^enode://([0-9a-fA-F]{64})@(\d+\.\d+\.\d+\.\d+):(\d+)$").unwrap(); - } - match RE.is_match(e) { - true => Some(e.to_owned()), - false => None, - } + Node::from_str(e).ok().map(|_| e.to_owned()) } fn init_nodes(&self, spec: &Spec) -> Vec { From dc3ceeb5bb89d731d9ad2b0bf2431d76cabd84c0 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 19 Feb 2016 20:02:23 +0100 Subject: [PATCH 3/3] Use new is_valid_node_url function. --- Cargo.lock | 1 - parity/main.rs | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d475f81e1..250acb5a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,6 @@ dependencies = [ "ethsync 0.9.99", "fdlimit 0.1.0", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/parity/main.rs b/parity/main.rs index 8930c60e9..58d3a6f4c 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -31,21 +31,18 @@ extern crate ctrlc; extern crate fdlimit; extern crate target_info; extern crate daemonize; -extern crate regex; #[cfg(feature = "rpc")] extern crate ethcore_rpc as rpc; use std::net::{SocketAddr}; use std::env; -use std::from_str::FromStr; use std::process::exit; use std::path::PathBuf; use rlog::{LogLevelFilter}; use env_logger::LogBuilder; use ctrlc::CtrlC; use util::*; -use util::network::node::Node; use util::panics::MayPanic; use ethcore::spec::*; use ethcore::client::*; @@ -56,7 +53,6 @@ use ethsync::EthSync; use docopt::Docopt; use target_info::Target; use daemonize::Daemonize; -use regex::Regex; const USAGE: &'static str = " Parity. Ethereum Client. @@ -199,7 +195,10 @@ impl Configuration { } fn normalize_enode(e: &str) -> Option { - Node::from_str(e).ok().map(|_| e.to_owned()) + match is_valid_node_url(e) { + true => Some(e.to_owned()), + false => None, + } } fn init_nodes(&self, spec: &Spec) -> Vec { @@ -244,6 +243,7 @@ impl Configuration { let mut net_path = PathBuf::from(&self.path()); net_path.push("network"); ret.config_path = Some(net_path.to_str().unwrap().to_owned()); + ret } fn execute(&self) {