From b58aa1d1481b4b7104032c48e30a5de99aed7a20 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 26 Aug 2019 15:47:03 +1000 Subject: [PATCH] Add custom config options to testnet sub-cmd --- beacon_node/src/config.rs | 61 ++++++++++++++++++++++++++++----------- beacon_node/src/main.rs | 20 +++++++++++-- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index 9fac9b49a4..c8a9299a58 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -67,14 +67,28 @@ fn process_testnet_subcommand( builder.clean_datadir()?; } + if let Some(path_string) = cli_args.value_of("eth2-config") { + let path = path_string + .parse::() + .map_err(|e| format!("Unable to parse eth2-config path: {:?}", e))?; + builder.load_eth2_config(path)?; + } else { + builder.update_spec_from_subcommand(&cli_args)?; + } + + if let Some(path_string) = cli_args.value_of("config") { + let path = path_string + .parse::() + .map_err(|e| format!("Unable to parse config path: {:?}", e))?; + builder.load_client_config(path)?; + } + info!( log, "Creating new datadir"; "path" => format!("{:?}", builder.data_dir) ); - builder.update_spec_from_subcommand(&cli_args)?; - // Start matching on the second subcommand (e.g., `testnet bootstrap ...`) match cli_args.subcommand() { ("bootstrap", Some(cli_args)) => { @@ -82,7 +96,7 @@ fn process_testnet_subcommand( .value_of("server") .ok_or_else(|| "No bootstrap server specified")?; let port: Option = cli_args - .value_of("port") + .value_of("libp2p-port") .and_then(|s| s.parse::().ok()); builder.import_bootstrap_libp2p_address(server, port)?; @@ -306,11 +320,8 @@ impl<'a> ConfigBuilder<'a> { )); } else { // Write the config to a TOML file in the datadir. - write_to_file( - self.data_dir.join(ETH2_CONFIG_FILENAME), - &self.client_config, - ) - .map_err(|e| format!("Unable to write {} file: {:?}", ETH2_CONFIG_FILENAME, e))?; + write_to_file(self.data_dir.join(ETH2_CONFIG_FILENAME), &self.eth2_config) + .map_err(|e| format!("Unable to write {} file: {:?}", ETH2_CONFIG_FILENAME, e))?; } Ok(()) @@ -339,20 +350,36 @@ impl<'a> ConfigBuilder<'a> { .unwrap_or_else(|| true) { return Err( - "No database found in datadir. Use the 'testnet -f' sub-command to overwrite the \ - existing datadir, or specify a different `--datadir`." + "No database found in datadir. Use 'testnet -f' to overwrite the existing \ + datadir, or specify a different `--datadir`." .into(), ); } - self.eth2_config = read_from_file::(self.data_dir.join(ETH2_CONFIG_FILENAME)) - .map_err(|e| format!("Unable to parse {} file: {:?}", ETH2_CONFIG_FILENAME, e))? - .ok_or_else(|| format!("{} file does not exist", ETH2_CONFIG_FILENAME))?; + self.load_eth2_config(self.data_dir.join(ETH2_CONFIG_FILENAME))?; + self.load_client_config(self.data_dir.join(CLIENT_CONFIG_FILENAME))?; - self.client_config = - read_from_file::(self.data_dir.join(CLIENT_CONFIG_FILENAME)) - .map_err(|e| format!("Unable to parse {} file: {:?}", CLIENT_CONFIG_FILENAME, e))? - .ok_or_else(|| format!("{} file does not exist", ETH2_CONFIG_FILENAME))?; + Ok(()) + } + + /// Attempts to load the client config from `path`. + /// + /// Returns an error if any files are not found or are invalid. + pub fn load_client_config(&mut self, path: PathBuf) -> Result<()> { + self.client_config = read_from_file::(path) + .map_err(|e| format!("Unable to parse ClientConfig file: {:?}", e))? + .ok_or_else(|| "ClientConfig file does not exist".to_string())?; + + Ok(()) + } + + /// Attempts to load the eth2 config from `path`. + /// + /// Returns an error if any files are not found or are invalid. + pub fn load_eth2_config(&mut self, path: PathBuf) -> Result<()> { + self.eth2_config = read_from_file::(path) + .map_err(|e| format!("Unable to parse Eth2Config file: {:?}", e))? + .ok_or_else(|| "Eth2Config file does not exist".to_string())?; Ok(()) } diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index 4430db1287..a9659362ca 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -198,6 +198,22 @@ fn main() { .takes_value(true) .required(true) .possible_values(&["mainnet", "minimal", "interop"]) + .default_value("minimal") + ) + .arg( + Arg::with_name("eth2-config") + .long("eth2-config") + .value_name("TOML_FILE") + .help("A existing eth2_spec TOML file (e.g., eth2_spec.toml).") + .takes_value(true) + .conflicts_with("spec") + ) + .arg( + Arg::with_name("config") + .long("config") + .value_name("TOML_FILE") + .help("An existing beacon_node TOML file (e.g., beacon_node.toml).") + .takes_value(true) ) .arg( Arg::with_name("random-datadir") @@ -210,8 +226,8 @@ fn main() { Arg::with_name("force") .long("force") .short("f") - .help("If present, will backup any existing config files before creating new ones. Cannot be \ - used when specifying --random-datadir (logic error).") + .help("If present, will create new config and database files and move the any existing to a \ + backup directory.") .conflicts_with("random-datadir") ) /*