mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-23 06:44:35 +00:00
Created Function to Reduce Duplicated Code
This commit is contained in:
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -730,6 +730,10 @@ dependencies = [
|
||||
"eth2_ssz",
|
||||
"ethereum-types 0.12.1",
|
||||
"hex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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<T: EthSpec>(
|
||||
// parse the CLI args into a useable config
|
||||
let config: BootNodeConfig<T> = BootNodeConfig::new(bn_matches, eth2_network_config)?;
|
||||
|
||||
// Dump config if `dump-config` flag is set
|
||||
let dump_config = clap_utils::parse_optional::<PathBuf>(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::<PathBuf>(lh_matches, "dump-chain-config")?
|
||||
{
|
||||
let chain_config =
|
||||
types::Config::from_chain_spec::<T>(ð2_network_config.chain_spec::<T>()?);
|
||||
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::<T>()?,
|
||||
)?;
|
||||
|
||||
// Run the boot node
|
||||
if !lh_matches.is_present("immediate-shutdown") {
|
||||
|
||||
@@ -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"}
|
||||
|
||||
@@ -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<T: Decode>(
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Writes configs to file if `dump-config` or `dump-chain-config` flags are set
|
||||
pub fn check_dump_configs<S, E>(
|
||||
matches: &ArgMatches,
|
||||
config: S,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), String>
|
||||
where
|
||||
S: serde::Serialize,
|
||||
E: EthSpec,
|
||||
{
|
||||
if let Some(dump_path) = parse_optional::<PathBuf>(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::<PathBuf>(matches, "dump-chain-config")? {
|
||||
let chain_config = Config::from_chain_spec::<E>(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(())
|
||||
}
|
||||
|
||||
@@ -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<E: EthSpec>(
|
||||
let executor = context.executor.clone();
|
||||
let config = beacon_node::get_config::<E>(matches, &context)?;
|
||||
let shutdown_flag = matches.is_present("immediate-shutdown");
|
||||
if let Some(dump_path) = clap_utils::parse_optional::<PathBuf>(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::<PathBuf>(matches, "dump-chain-config")?
|
||||
{
|
||||
let chain_config = Config::from_chain_spec::<E>(&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<E: EthSpec>(
|
||||
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::<PathBuf>(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::<PathBuf>(matches, "dump-chain-config")?
|
||||
{
|
||||
let chain_config = Config::from_chain_spec::<E>(&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 {
|
||||
|
||||
Reference in New Issue
Block a user