From dbedc93707aab2c862e27110bca1d3755e495c62 Mon Sep 17 00:00:00 2001 From: Mark Mackey Date: Mon, 28 Feb 2022 13:59:43 -0600 Subject: [PATCH] Created Function to Reduce Duplicated Code --- Cargo.lock | 4 ++++ boot_node/src/lib.rs | 27 +++++++----------------- common/clap_utils/Cargo.toml | 4 ++++ common/clap_utils/src/lib.rs | 27 ++++++++++++++++++++++++ lighthouse/src/main.rs | 40 +++++------------------------------- 5 files changed, 47 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b30c138e7..49f0e7e4f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -730,6 +730,10 @@ dependencies = [ "eth2_ssz", "ethereum-types 0.12.1", "hex", + "serde", + "serde_json", + "serde_yaml", + "types", ] [[package]] diff --git a/boot_node/src/lib.rs b/boot_node/src/lib.rs index baa7c69322..f4391f987a 100644 --- a/boot_node/src/lib.rs +++ b/boot_node/src/lib.rs @@ -3,8 +3,6 @@ use clap::ArgMatches; use slog::{o, Drain, Level, Logger}; use eth2_network_config::Eth2NetworkConfig; -use std::fs::File; -use std::path::PathBuf; mod cli; pub mod config; mod server; @@ -86,24 +84,13 @@ fn main( // parse the CLI args into a useable config let config: BootNodeConfig = BootNodeConfig::new(bn_matches, eth2_network_config)?; - // Dump config if `dump-config` flag is set - let dump_config = clap_utils::parse_optional::(lh_matches, "dump-config")?; - if let Some(dump_path) = dump_config { - let config_sz = BootNodeConfigSerialization::from_config_ref(&config); - let mut file = File::create(dump_path) - .map_err(|e| format!("Failed to create dumped config: {:?}", e))?; - serde_json::to_writer(&mut file, &config_sz) - .map_err(|e| format!("Error serializing config: {:?}", e))?; - } - if let Some(dump_path) = clap_utils::parse_optional::(lh_matches, "dump-chain-config")? - { - let chain_config = - types::Config::from_chain_spec::(ð2_network_config.chain_spec::()?); - let mut file = File::create(dump_path) - .map_err(|e| format!("Failed to create dumped chain config: {:?}", e))?; - serde_yaml::to_writer(&mut file, &chain_config) - .map_err(|e| format!("Error serializing chain config: {:?}", e))?; - } + // Dump configs if `dump-config` or `dump-chain-config` flags are set + let config_sz = BootNodeConfigSerialization::from_config_ref(&config); + clap_utils::check_dump_configs::<_, T>( + lh_matches, + &config_sz, + ð2_network_config.chain_spec::()?, + )?; // Run the boot node if !lh_matches.is_present("immediate-shutdown") { diff --git a/common/clap_utils/Cargo.toml b/common/clap_utils/Cargo.toml index 542a13ad4e..af9b4392e9 100644 --- a/common/clap_utils/Cargo.toml +++ b/common/clap_utils/Cargo.toml @@ -13,3 +13,7 @@ dirs = "3.0.1" eth2_network_config = { path = "../eth2_network_config" } eth2_ssz = "0.4.1" ethereum-types = "0.12.1" +serde = "1.0.116" +serde_json = "1.0.59" +serde_yaml = "0.8.13" +types = { path = "../../consensus/types"} diff --git a/common/clap_utils/src/lib.rs b/common/clap_utils/src/lib.rs index ce6cc2f8d5..e8922297e8 100644 --- a/common/clap_utils/src/lib.rs +++ b/common/clap_utils/src/lib.rs @@ -6,6 +6,7 @@ use ethereum_types::U256 as Uint256; use ssz::Decode; use std::path::PathBuf; use std::str::FromStr; +use types::{ChainSpec, Config, EthSpec}; pub mod flags; @@ -163,3 +164,29 @@ pub fn parse_ssz_optional( }) .transpose() } + +/// Writes configs to file if `dump-config` or `dump-chain-config` flags are set +pub fn check_dump_configs( + matches: &ArgMatches, + config: S, + spec: &ChainSpec, +) -> Result<(), String> +where + S: serde::Serialize, + E: EthSpec, +{ + if let Some(dump_path) = parse_optional::(matches, "dump-config")? { + let mut file = std::fs::File::create(dump_path) + .map_err(|e| format!("Failed to open file for writing config: {:?}", e))?; + serde_json::to_writer(&mut file, &config) + .map_err(|e| format!("Error serializing config: {:?}", e))?; + } + if let Some(dump_path) = parse_optional::(matches, "dump-chain-config")? { + let chain_config = Config::from_chain_spec::(spec); + let mut file = std::fs::File::create(dump_path) + .map_err(|e| format!("Failed to open file for writing chain config: {:?}", e))?; + serde_yaml::to_writer(&mut file, &chain_config) + .map_err(|e| format!("Error serializing config: {:?}", e))?; + } + Ok(()) +} diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 890c4520da..b8acd7285c 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -13,11 +13,10 @@ use eth2_network_config::{Eth2NetworkConfig, DEFAULT_HARDCODED_NETWORK, HARDCODE use lighthouse_version::VERSION; use malloc_utils::configure_memory_allocator; use slog::{crit, info, warn}; -use std::fs::File; use std::path::PathBuf; use std::process::exit; use task_executor::ShutdownReason; -use types::{Config, EthSpec, EthSpecId}; +use types::{EthSpec, EthSpecId}; use validator_client::ProductionValidatorClient; fn bls_library_name() -> &'static str { @@ -495,23 +494,8 @@ fn run( let executor = context.executor.clone(); let config = beacon_node::get_config::(matches, &context)?; let shutdown_flag = matches.is_present("immediate-shutdown"); - if let Some(dump_path) = clap_utils::parse_optional::(matches, "dump-config")? - { - let mut file = File::create(dump_path) - .map_err(|e| format!("Failed to create dumped config: {:?}", e))?; - serde_json::to_writer(&mut file, &config) - .map_err(|e| format!("Error serializing config: {:?}", e))?; - }; - if let Some(dump_path) = - clap_utils::parse_optional::(matches, "dump-chain-config")? - { - let chain_config = Config::from_chain_spec::(&context.eth2_config.spec); - let mut file = File::create(dump_path) - .map_err(|e| format!("Failed to create dumped chain config: {:?}", e))?; - serde_yaml::to_writer(&mut file, &chain_config) - .map_err(|e| format!("Error serializing chain config: {:?}", e))?; - }; - + // Dump configs if `dump-config` or `dump-chain-config` flags are set + clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; executor.clone().spawn( async move { if let Err(e) = ProductionBeaconNode::new(context.clone(), config).await { @@ -537,22 +521,8 @@ fn run( let config = validator_client::Config::from_cli(matches, context.log()) .map_err(|e| format!("Unable to initialize validator config: {}", e))?; let shutdown_flag = matches.is_present("immediate-shutdown"); - if let Some(dump_path) = clap_utils::parse_optional::(matches, "dump-config")? - { - let mut file = File::create(dump_path) - .map_err(|e| format!("Failed to create dumped config: {:?}", e))?; - serde_json::to_writer(&mut file, &config) - .map_err(|e| format!("Error serializing config: {:?}", e))?; - }; - if let Some(dump_path) = - clap_utils::parse_optional::(matches, "dump-chain-config")? - { - let chain_config = Config::from_chain_spec::(&context.eth2_config.spec); - let mut file = File::create(dump_path) - .map_err(|e| format!("Failed to create dumped chain config: {:?}", e))?; - serde_yaml::to_writer(&mut file, &chain_config) - .map_err(|e| format!("Error serializing chain config: {:?}", e))?; - }; + // Dump configs if `dump-config` or `dump-chain-config` flags are set + clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; if !shutdown_flag { executor.clone().spawn( async move {