From 8ea9bb9571d9af0605394c7de1cd19d880e1c2dc Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 11 Apr 2018 14:36:27 +0200 Subject: [PATCH 1/2] Some tweaks to main.rs for parity as a library --- parity/configuration.rs | 33 ++++++++++++++------------------- parity/main.rs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/parity/configuration.rs b/parity/configuration.rs index 96f819cba2c..d921c9377b4 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -95,16 +95,14 @@ pub struct Execute { #[derive(Debug, PartialEq)] pub struct Configuration { pub args: Args, - pub spec_name_override: Option, } impl Configuration { - pub fn parse>(command: &[S], spec_name_override: Option) -> Result { + pub fn parse>(command: &[S]) -> Result { let args = Args::parse(command)?; let config = Configuration { args: args, - spec_name_override: spec_name_override, }; Ok(config) @@ -462,9 +460,7 @@ impl Configuration { } fn chain(&self) -> Result { - let name = if let Some(ref s) = self.spec_name_override { - s.clone() - } else if self.args.flag_testnet { + let name = if self.args.flag_testnet { "testnet".to_owned() } else { self.args.arg_chain.clone() @@ -1264,7 +1260,6 @@ mod tests { fn parse(args: &[&str]) -> Configuration { Configuration { args: Args::parse_without_config(args).unwrap(), - spec_name_override: None, } } @@ -1844,7 +1839,7 @@ mod tests { let filename = tempdir.path().join("peers"); File::create(&filename).unwrap().write_all(b" \n\t\n").unwrap(); let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); assert!(conf.init_reserved_nodes().is_ok()); } @@ -1854,7 +1849,7 @@ mod tests { let filename = tempdir.path().join("peers_comments"); File::create(&filename).unwrap().write_all(b"# Sample comment\nenode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@172.0.0.1:30303\n").unwrap(); let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); let reserved_nodes = conf.init_reserved_nodes(); assert!(reserved_nodes.is_ok()); assert_eq!(reserved_nodes.unwrap().len(), 1); @@ -1863,7 +1858,7 @@ mod tests { #[test] fn test_dev_preset() { let args = vec!["parity", "--config", "dev"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_settings.chain, "dev"); @@ -1877,7 +1872,7 @@ mod tests { #[test] fn test_mining_preset() { let args = vec!["parity", "--config", "mining"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_conf.min_peers, 50); @@ -1899,7 +1894,7 @@ mod tests { #[test] fn test_non_standard_ports_preset() { let args = vec!["parity", "--config", "non-standard-ports"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_settings.network_port, 30305); @@ -1912,7 +1907,7 @@ mod tests { #[test] fn test_insecure_preset() { let args = vec!["parity", "--config", "insecure"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.update_policy.require_consensus, false); @@ -1932,7 +1927,7 @@ mod tests { #[test] fn test_dev_insecure_preset() { let args = vec!["parity", "--config", "dev-insecure"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_settings.chain, "dev"); @@ -1955,7 +1950,7 @@ mod tests { #[test] fn test_override_preset() { let args = vec!["parity", "--config", "mining", "--min-peers=99"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_conf.min_peers, 99); @@ -2078,7 +2073,7 @@ mod tests { #[test] fn should_respect_only_max_peers_and_default() { let args = vec!["parity", "--max-peers=50"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_conf.min_peers, 25); @@ -2091,7 +2086,7 @@ mod tests { #[test] fn should_respect_only_max_peers_less_than_default() { let args = vec!["parity", "--max-peers=5"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_conf.min_peers, 5); @@ -2104,7 +2099,7 @@ mod tests { #[test] fn should_respect_only_min_peers_and_default() { let args = vec!["parity", "--min-peers=5"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_conf.min_peers, 5); @@ -2117,7 +2112,7 @@ mod tests { #[test] fn should_respect_only_min_peers_and_greater_than_default() { let args = vec!["parity", "--min-peers=500"]; - let conf = Configuration::parse(&args, None).unwrap(); + let conf = Configuration::parse(&args).unwrap(); match conf.into_command().unwrap().cmd { Cmd::Run(c) => { assert_eq!(c.net_conf.min_peers, 500); diff --git a/parity/main.rs b/parity/main.rs index 4908f38d9ac..0eab3f7c16c 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -153,7 +153,7 @@ fn print_hash_of(maybe_file: Option) -> Result { } } -enum PostExecutionAction { +pub enum PostExecutionAction { Print(String), Restart(Option), Quit, @@ -181,9 +181,10 @@ fn execute(command: Execute, can_restart: bool) -> Result Result { - let args: Vec = env::args().collect(); - let conf = Configuration::parse(&args, take_spec_name_override()).unwrap_or_else(|e| e.exit()); +fn start(mut args: Vec) -> Result { + args.insert(0, "parity".to_owned()); + let conf = Configuration::parse(&args).unwrap_or_else(|e| e.exit()); + let can_restart = conf.args.flag_can_restart; let deprecated = find_deprecated(&conf.args); for d in deprecated { @@ -277,7 +278,7 @@ const PLEASE_RESTART_EXIT_CODE: i32 = 69; // Run our version of parity. // Returns the exit error code. -fn main_direct(can_restart: bool) -> i32 { +fn main_direct(force_can_restart: bool) -> i32 { global_init(); let mut alt_mains = HashMap::new(); sync_main(&mut alt_mains); @@ -286,7 +287,25 @@ fn main_direct(can_restart: bool) -> i32 { f(); 0 } else { - match start(can_restart) { + let mut args = std::env::args().skip(1).collect::>(); + if force_can_restart && !args.iter().any(|arg| arg == "--can-restart") { + args.push("--can-restart".to_owned()); + } + + if let Some(spec_override) = take_spec_name_override() { + args.retain(|f| f != "--testnet"); + args.retain(|f| !f.starts_with("--chain=")); + while let Some(pos) = args.iter().position(|a| a == "--chain") { + if args.len() > pos + 1 { + args.remove(pos + 1); + } + args.remove(pos); + } + args.push("--chain".to_owned()); + args.push(spec_override); + } + + match start(args) { Ok(result) => match result { PostExecutionAction::Print(s) => { println!("{}", s); 0 }, PostExecutionAction::Restart(spec_name_override) => { @@ -366,7 +385,6 @@ fn main() { } else { trace_main!("Running direct"); // Otherwise, we're presumably running the version we want. Just run and fall-through. - let can_restart = std::env::args().any(|arg| arg == "--can-restart"); - process::exit(main_direct(can_restart)); + process::exit(main_direct(false)); } } From 3ebd4483d4616fb1ab04208634117ac74d9c2340 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 12 Apr 2018 14:04:54 +0200 Subject: [PATCH 2/2] Remove pub from PostExecutionAction --- parity/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parity/main.rs b/parity/main.rs index 818eb0e1f22..d7d5b137e32 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -152,7 +152,7 @@ fn print_hash_of(maybe_file: Option) -> Result { } } -pub enum PostExecutionAction { +enum PostExecutionAction { Print(String), Restart(Option), Quit,