mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-19 21:04:41 +00:00
Add eth2_config crate, integrate into val client
This commit is contained in:
@@ -2,12 +2,12 @@ extern crate slog;
|
||||
|
||||
mod run;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use client::{ClientConfig, Eth2Config};
|
||||
use eth2_config::{read_from_file, write_to_file};
|
||||
use slog::{crit, o, Drain};
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub const DEFAULT_DATA_DIR: &str = ".lighthouse";
|
||||
|
||||
@@ -105,7 +105,8 @@ fn main() {
|
||||
Arg::with_name("spec-constants")
|
||||
.long("spec-constants")
|
||||
.value_name("TITLE")
|
||||
.help("The title of the spec constants for chain config..")
|
||||
.short("s")
|
||||
.help("The title of the spec constants for chain config.")
|
||||
.takes_value(true)
|
||||
.possible_values(&["mainnet", "minimal"])
|
||||
.default_value("minimal"),
|
||||
@@ -113,34 +114,44 @@ fn main() {
|
||||
.arg(
|
||||
Arg::with_name("recent-genesis")
|
||||
.long("recent-genesis")
|
||||
.short("r")
|
||||
.help("When present, genesis will be within 30 minutes prior. Only for testing"),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
// Attempt to lead the `ClientConfig` from disk. If it fails, write
|
||||
let mut client_config = match read_from_file::<ClientConfig>(
|
||||
matches.value_of("data_dir"),
|
||||
CLIENT_CONFIG_FILENAME,
|
||||
) {
|
||||
let data_dir = match get_data_dir(&matches) {
|
||||
Ok(dir) => dir,
|
||||
Err(e) => {
|
||||
crit!(logger, "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::<ClientConfig>(client_config_path.clone()) {
|
||||
Ok(Some(c)) => c,
|
||||
Ok(None) => {
|
||||
let default = ClientConfig::default();
|
||||
if let Err(e) = write_to_file(matches.value_of("data_dir"), CLIENT_CONFIG_FILENAME, &default) {
|
||||
if let Err(e) = write_to_file(client_config_path, &default) {
|
||||
crit!(logger, "Failed to write default ClientConfig to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
default
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(logger, "Failed to load a ChainConfig file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(data_dir) = matches.value_of("data_dir") {
|
||||
client_config.data_dir = data_dir.to_string();
|
||||
}
|
||||
// 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) => {
|
||||
@@ -149,10 +160,12 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
let mut eth2_config = match read_from_file::<Eth2Config>(
|
||||
matches.value_of("data_dir"),
|
||||
ETH2_CONFIG_FILENAME,
|
||||
) {
|
||||
let eth2_config_path = 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") {
|
||||
@@ -160,7 +173,7 @@ fn main() {
|
||||
Some("minimal") => Eth2Config::minimal(),
|
||||
_ => unreachable!(), // Guarded by slog.
|
||||
};
|
||||
if let Err(e) = write_to_file(matches.value_of("data_dir"), ETH2_CONFIG_FILENAME, &default) {
|
||||
if let Err(e) = write_to_file(eth2_config_path, &default) {
|
||||
crit!(logger, "Failed to write default Eth2Config to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
@@ -172,6 +185,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
// Update the eth2 config with any CLI flags.
|
||||
match eth2_config.apply_cli_args(&matches) {
|
||||
Ok(()) => (),
|
||||
Err(s) => {
|
||||
@@ -186,59 +200,14 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a configuration to file.
|
||||
fn write_to_file<T>(data_dir: Option<&str>, config_filename: &str, config: &T) -> Result<(), String>
|
||||
where
|
||||
T: Default + serde::de::DeserializeOwned + serde::Serialize,
|
||||
{
|
||||
let data_dir = data_dir.unwrap_or_else(|| DEFAULT_DATA_DIR);
|
||||
|
||||
let path = dirs::home_dir()
|
||||
.ok_or_else(|| "Unable to locate home directory")?
|
||||
.join(&data_dir);
|
||||
fs::create_dir_all(&path).map_err(|_| "Unable to open data_dir")?;
|
||||
|
||||
if let Ok(mut file) = File::create(path.join(config_filename)) {
|
||||
let toml_encoded = toml::to_string(&config).map_err(|e| {
|
||||
format!(
|
||||
"Failed to write configuration to {}. Error: {:?}",
|
||||
config_filename, e
|
||||
)
|
||||
})?;
|
||||
file.write_all(toml_encoded.as_bytes())
|
||||
.expect(&format!("Unable to write to {}", config_filename));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Loads a `ClientConfig` from file. If unable to load from file, generates a default
|
||||
/// configuration and saves that as a sample file.
|
||||
fn read_from_file<T>(data_dir: Option<&str>, config_filename: &str) -> Result<Option<T>, String>
|
||||
where
|
||||
T: Default + serde::de::DeserializeOwned + serde::Serialize,
|
||||
{
|
||||
let data_dir = data_dir.unwrap_or_else(|| DEFAULT_DATA_DIR);
|
||||
|
||||
let path = dirs::home_dir()
|
||||
.ok_or_else(|| "Unable to locate home directory")?
|
||||
.join(&data_dir);
|
||||
fs::create_dir_all(&path).map_err(|_| "Unable to open data_dir")?;
|
||||
|
||||
if let Ok(mut file) = File::open(path.join(config_filename)) {
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).map_err(|e| {
|
||||
format!(
|
||||
"Unable to read existing {}. Error: {:?}",
|
||||
config_filename, e
|
||||
)
|
||||
})?;
|
||||
|
||||
let config = toml::from_str(&contents)
|
||||
.map_err(|e| format!("Unable to parse {}: {:?}", config_filename, e))?;
|
||||
|
||||
Ok(Some(config))
|
||||
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 {
|
||||
Ok(None)
|
||||
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