Add concept of beacon node configuration TOML

This commit is contained in:
Paul Hauner 2019-06-07 21:00:34 -04:00
parent e73a31c37f
commit f69d9093a3
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
3 changed files with 58 additions and 3 deletions

View File

@ -5,7 +5,9 @@ authors = ["Paul Hauner <paul@paulhauner.com>", "Age Manning <Age@AgeManning.com
edition = "2018"
[dependencies]
dirs = "1.0.3"
types = { path = "../eth2/types" }
toml = "^0.5"
store = { path = "./store" }
client = { path = "client" }
fork_choice = { path = "../eth2/fork_choice" }

View File

@ -8,7 +8,7 @@ use std::path::PathBuf;
/// The core configuration of a Lighthouse beacon node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientConfig {
data_dir: String,
pub data_dir: String,
pub spec: String,
pub db_type: String,
db_name: String,

View File

@ -5,6 +5,13 @@ mod run;
use clap::{App, Arg};
use client::ClientConfig;
use slog::{crit, o, Drain};
use std::fs;
use std::fs::File;
use std::io::prelude::*;
pub const SAMPLE_CONFIG_FILENAME: &str = "beacon_node_config.sample.toml";
pub const CONFIG_FILENAME: &str = "beacon_node_config.toml";
pub const DEFAULT_DATA_DIR: &str = ".lighthouse";
fn main() {
let decorator = slog_term::TermDecorator::new().build();
@ -22,7 +29,8 @@ fn main() {
.long("datadir")
.value_name("DIR")
.help("Data directory for keys and databases.")
.takes_value(true),
.takes_value(true)
.default_value(DEFAULT_DATA_DIR),
)
// network related arguments
.arg(
@ -94,7 +102,13 @@ fn main() {
)
.get_matches();
let mut config = ClientConfig::default();
let mut config = match load_config(matches.value_of("data_dir")) {
Ok(c) => c,
Err(e) => {
crit!(logger, "Failed to load/generate a ChainConfig"; "error" => format!("{:?}", e));
return;
}
};
match config.apply_cli_args(&matches) {
Ok(()) => (),
@ -109,3 +123,42 @@ fn main() {
Err(e) => crit!(logger, "Beacon node failed to start"; "reason" => format!("{:}", e)),
}
}
/// Loads a `ClientConfig` from file. If unable to load from file, generates a default
/// configuration and saves that as a sample file.
fn load_config(data_dir: Option<&str>) -> Result<ClientConfig, String> {
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
)
})?;
toml::from_str(&contents).map_err(|_| format!("Unable to parse {}", CONFIG_FILENAME))
} else {
let mut config = ClientConfig::default();
config.data_dir = data_dir.to_string();
if let Ok(mut file) = File::create(path.join(SAMPLE_CONFIG_FILENAME)) {
let toml_encoded = toml::to_string(&config).map_err(|e| {
format!(
"Failed to write configuration to {}. Error: {:?}",
SAMPLE_CONFIG_FILENAME, e
)
})?;
file.write_all(toml_encoded.as_bytes())
.expect(&format!("Unable to write to {}", SAMPLE_CONFIG_FILENAME));
}
Ok(config)
}
}