mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 04:48:21 +00:00
Add eth2_config crate, integrate into val client
This commit is contained in:
@@ -7,11 +7,19 @@ mod service;
|
||||
mod signer;
|
||||
|
||||
use crate::config::Config as ValidatorClientConfig;
|
||||
use std::fs;
|
||||
use crate::service::Service as ValidatorService;
|
||||
use clap::{App, Arg};
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use eth2_config::{read_from_file, write_to_file, Eth2Config};
|
||||
use protos::services_grpc::ValidatorServiceClient;
|
||||
use slog::{error, info, o, Drain};
|
||||
use types::Keypair;
|
||||
use slog::{crit, error, info, o, Drain};
|
||||
use std::path::PathBuf;
|
||||
use types::{Keypair, MainnetEthSpec, MinimalEthSpec};
|
||||
|
||||
pub const DEFAULT_SPEC: &str = "minimal";
|
||||
pub const DEFAULT_DATA_DIR: &str = ".lighthouse-validator";
|
||||
pub const CLIENT_CONFIG_FILENAME: &str = "client_config.toml";
|
||||
pub const ETH2_CONFIG_FILENAME: &str = "eth2_config.toml";
|
||||
|
||||
fn main() {
|
||||
// Logging
|
||||
@@ -30,7 +38,16 @@ fn main() {
|
||||
.long("datadir")
|
||||
.value_name("DIR")
|
||||
.help("Data directory for keys and databases.")
|
||||
.takes_value(true),
|
||||
.takes_value(true)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("eth-config")
|
||||
.long("eth-config")
|
||||
.short("e")
|
||||
.value_name("DIR")
|
||||
.help(&format!("Directory containing {}.", ETH2_CONFIG_FILENAME))
|
||||
.takes_value(true)
|
||||
.default_value(ETH2_CONFIG_FILENAME),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("server")
|
||||
@@ -40,24 +57,139 @@ fn main() {
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("spec")
|
||||
.long("spec")
|
||||
.value_name("spec")
|
||||
Arg::with_name("spec-constants")
|
||||
.long("spec-constants")
|
||||
.value_name("TITLE")
|
||||
.short("s")
|
||||
.help("Configuration of Beacon Chain")
|
||||
.help("The title of the spec constants for chain config.")
|
||||
.takes_value(true)
|
||||
.possible_values(&["mainnet", "minimal"])
|
||||
.default_value("minimal"),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let config = ValidatorClientConfig::parse_args(&matches, &log)
|
||||
.expect("Unable to build a configuration for the validator client.");
|
||||
let data_dir = match get_data_dir(&matches) {
|
||||
Ok(dir) => dir,
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to initialize data dir"; "error" => format!("{:?}", e));
|
||||
return
|
||||
}
|
||||
};
|
||||
|
||||
let client_config_path = data_dir.join(CLIENT_CONFIG_FILENAME);
|
||||
|
||||
// Attempt to lead the `ClientConfig` from disk.
|
||||
//
|
||||
// If file doesn't exist, create a new, default one.
|
||||
let mut client_config = match read_from_file::<ValidatorClientConfig>(
|
||||
client_config_path.clone(),
|
||||
) {
|
||||
Ok(Some(c)) => c,
|
||||
Ok(None) => {
|
||||
let default = ValidatorClientConfig::default();
|
||||
if let Err(e) = write_to_file(client_config_path.clone(), &default) {
|
||||
crit!(log, "Failed to write default ClientConfig to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
default
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to load a ChainConfig file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Ensure the `data_dir` in the config matches that supplied to the CLI.
|
||||
client_config.data_dir = data_dir.clone();
|
||||
|
||||
// Update the client config with any CLI args.
|
||||
match client_config.apply_cli_args(&matches) {
|
||||
Ok(()) => (),
|
||||
Err(s) => {
|
||||
crit!(log, "Failed to parse ClientConfig CLI arguments"; "error" => s);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let eth2_config_path: PathBuf = matches
|
||||
.value_of("eth-config")
|
||||
.and_then(|s| Some(PathBuf::from(s)))
|
||||
.unwrap_or_else(|| data_dir.join(ETH2_CONFIG_FILENAME));
|
||||
|
||||
// Attempt to load the `Eth2Config` from file.
|
||||
//
|
||||
// If the file doesn't exist, create a default one depending on the CLI flags.
|
||||
let mut eth2_config = match read_from_file::<Eth2Config>(
|
||||
eth2_config_path.clone()
|
||||
) {
|
||||
Ok(Some(c)) => c,
|
||||
Ok(None) => {
|
||||
let default = match matches.value_of("spec-constants") {
|
||||
Some("mainnet") => Eth2Config::mainnet(),
|
||||
Some("minimal") => Eth2Config::minimal(),
|
||||
_ => unreachable!(), // Guarded by slog.
|
||||
};
|
||||
if let Err(e) = write_to_file(eth2_config_path, &default) {
|
||||
crit!(log, "Failed to write default Eth2Config to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
default
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to instantiate an Eth2Config"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Update the eth2 config with any CLI flags.
|
||||
match eth2_config.apply_cli_args(&matches) {
|
||||
Ok(()) => (),
|
||||
Err(s) => {
|
||||
crit!(log, "Failed to parse Eth2Config CLI arguments"; "error" => s);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
info!(
|
||||
log,
|
||||
"Starting validator client";
|
||||
"datadir" => client_config.data_dir.to_str(),
|
||||
"spec_constants" => ð2_config.spec_constants,
|
||||
);
|
||||
|
||||
let result = match eth2_config.spec_constants.as_str() {
|
||||
"mainnet" => ValidatorService::<ValidatorServiceClient, Keypair>::start::<MainnetEthSpec>(
|
||||
client_config,
|
||||
eth2_config,
|
||||
log.clone(),
|
||||
),
|
||||
"minimal" => ValidatorService::<ValidatorServiceClient, Keypair>::start::<MinimalEthSpec>(
|
||||
client_config,
|
||||
eth2_config,
|
||||
log.clone(),
|
||||
),
|
||||
other => {
|
||||
crit!(log, "Unknown spec constants"; "title" => other);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// start the validator service.
|
||||
// this specifies the GRPC and signer type to use as the duty manager beacon node.
|
||||
match ValidatorService::<ValidatorServiceClient, Keypair>::start(config, log.clone()) {
|
||||
match result {
|
||||
Ok(_) => info!(log, "Validator client shutdown successfully."),
|
||||
Err(e) => error!(log, "Validator exited due to: {}", e.to_string()),
|
||||
Err(e) => crit!(log, "Validator client exited with error"; "error" => e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_data_dir(args: &ArgMatches) -> Result<PathBuf, &'static str> {
|
||||
if let Some(data_dir) = args.value_of("data_dir") {
|
||||
Ok(PathBuf::from(data_dir))
|
||||
} else {
|
||||
let path = dirs::home_dir()
|
||||
.ok_or_else(|| "Unable to locate home directory")?
|
||||
.join(&DEFAULT_DATA_DIR);
|
||||
fs::create_dir_all(&path).map_err(|_| "Unable to create data_dir")?;
|
||||
Ok(path)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user