mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 00:42:42 +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",
|
"eth2_ssz",
|
||||||
"ethereum-types 0.12.1",
|
"ethereum-types 0.12.1",
|
||||||
"hex",
|
"hex",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"serde_yaml",
|
||||||
|
"types",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ use clap::ArgMatches;
|
|||||||
use slog::{o, Drain, Level, Logger};
|
use slog::{o, Drain, Level, Logger};
|
||||||
|
|
||||||
use eth2_network_config::Eth2NetworkConfig;
|
use eth2_network_config::Eth2NetworkConfig;
|
||||||
use std::fs::File;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
mod cli;
|
mod cli;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod server;
|
mod server;
|
||||||
@@ -86,24 +84,13 @@ fn main<T: EthSpec>(
|
|||||||
// parse the CLI args into a useable config
|
// parse the CLI args into a useable config
|
||||||
let config: BootNodeConfig<T> = BootNodeConfig::new(bn_matches, eth2_network_config)?;
|
let config: BootNodeConfig<T> = BootNodeConfig::new(bn_matches, eth2_network_config)?;
|
||||||
|
|
||||||
// Dump config if `dump-config` flag is set
|
// Dump configs if `dump-config` or `dump-chain-config` flags are set
|
||||||
let dump_config = clap_utils::parse_optional::<PathBuf>(lh_matches, "dump-config")?;
|
let config_sz = BootNodeConfigSerialization::from_config_ref(&config);
|
||||||
if let Some(dump_path) = dump_config {
|
clap_utils::check_dump_configs::<_, T>(
|
||||||
let config_sz = BootNodeConfigSerialization::from_config_ref(&config);
|
lh_matches,
|
||||||
let mut file = File::create(dump_path)
|
&config_sz,
|
||||||
.map_err(|e| format!("Failed to create dumped config: {:?}", e))?;
|
ð2_network_config.chain_spec::<T>()?,
|
||||||
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))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the boot node
|
// Run the boot node
|
||||||
if !lh_matches.is_present("immediate-shutdown") {
|
if !lh_matches.is_present("immediate-shutdown") {
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ dirs = "3.0.1"
|
|||||||
eth2_network_config = { path = "../eth2_network_config" }
|
eth2_network_config = { path = "../eth2_network_config" }
|
||||||
eth2_ssz = "0.4.1"
|
eth2_ssz = "0.4.1"
|
||||||
ethereum-types = "0.12.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 ssz::Decode;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use types::{ChainSpec, Config, EthSpec};
|
||||||
|
|
||||||
pub mod flags;
|
pub mod flags;
|
||||||
|
|
||||||
@@ -163,3 +164,29 @@ pub fn parse_ssz_optional<T: Decode>(
|
|||||||
})
|
})
|
||||||
.transpose()
|
.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 lighthouse_version::VERSION;
|
||||||
use malloc_utils::configure_memory_allocator;
|
use malloc_utils::configure_memory_allocator;
|
||||||
use slog::{crit, info, warn};
|
use slog::{crit, info, warn};
|
||||||
use std::fs::File;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use task_executor::ShutdownReason;
|
use task_executor::ShutdownReason;
|
||||||
use types::{Config, EthSpec, EthSpecId};
|
use types::{EthSpec, EthSpecId};
|
||||||
use validator_client::ProductionValidatorClient;
|
use validator_client::ProductionValidatorClient;
|
||||||
|
|
||||||
fn bls_library_name() -> &'static str {
|
fn bls_library_name() -> &'static str {
|
||||||
@@ -495,23 +494,8 @@ fn run<E: EthSpec>(
|
|||||||
let executor = context.executor.clone();
|
let executor = context.executor.clone();
|
||||||
let config = beacon_node::get_config::<E>(matches, &context)?;
|
let config = beacon_node::get_config::<E>(matches, &context)?;
|
||||||
let shutdown_flag = matches.is_present("immediate-shutdown");
|
let shutdown_flag = matches.is_present("immediate-shutdown");
|
||||||
if let Some(dump_path) = clap_utils::parse_optional::<PathBuf>(matches, "dump-config")?
|
// Dump configs if `dump-config` or `dump-chain-config` flags are set
|
||||||
{
|
clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?;
|
||||||
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))?;
|
|
||||||
};
|
|
||||||
|
|
||||||
executor.clone().spawn(
|
executor.clone().spawn(
|
||||||
async move {
|
async move {
|
||||||
if let Err(e) = ProductionBeaconNode::new(context.clone(), config).await {
|
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())
|
let config = validator_client::Config::from_cli(matches, context.log())
|
||||||
.map_err(|e| format!("Unable to initialize validator config: {}", e))?;
|
.map_err(|e| format!("Unable to initialize validator config: {}", e))?;
|
||||||
let shutdown_flag = matches.is_present("immediate-shutdown");
|
let shutdown_flag = matches.is_present("immediate-shutdown");
|
||||||
if let Some(dump_path) = clap_utils::parse_optional::<PathBuf>(matches, "dump-config")?
|
// Dump configs if `dump-config` or `dump-chain-config` flags are set
|
||||||
{
|
clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?;
|
||||||
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))?;
|
|
||||||
};
|
|
||||||
if !shutdown_flag {
|
if !shutdown_flag {
|
||||||
executor.clone().spawn(
|
executor.clone().spawn(
|
||||||
async move {
|
async move {
|
||||||
|
|||||||
Reference in New Issue
Block a user