Add concept of beacon node configuration TOML
This commit is contained in:
parent
e73a31c37f
commit
f69d9093a3
@ -5,7 +5,9 @@ authors = ["Paul Hauner <paul@paulhauner.com>", "Age Manning <Age@AgeManning.com
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
dirs = "1.0.3"
|
||||||
types = { path = "../eth2/types" }
|
types = { path = "../eth2/types" }
|
||||||
|
toml = "^0.5"
|
||||||
store = { path = "./store" }
|
store = { path = "./store" }
|
||||||
client = { path = "client" }
|
client = { path = "client" }
|
||||||
fork_choice = { path = "../eth2/fork_choice" }
|
fork_choice = { path = "../eth2/fork_choice" }
|
||||||
|
@ -8,7 +8,7 @@ use std::path::PathBuf;
|
|||||||
/// The core configuration of a Lighthouse beacon node.
|
/// The core configuration of a Lighthouse beacon node.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ClientConfig {
|
pub struct ClientConfig {
|
||||||
data_dir: String,
|
pub data_dir: String,
|
||||||
pub spec: String,
|
pub spec: String,
|
||||||
pub db_type: String,
|
pub db_type: String,
|
||||||
db_name: String,
|
db_name: String,
|
||||||
|
@ -5,6 +5,13 @@ mod run;
|
|||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use client::ClientConfig;
|
use client::ClientConfig;
|
||||||
use slog::{crit, o, Drain};
|
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() {
|
fn main() {
|
||||||
let decorator = slog_term::TermDecorator::new().build();
|
let decorator = slog_term::TermDecorator::new().build();
|
||||||
@ -22,7 +29,8 @@ fn main() {
|
|||||||
.long("datadir")
|
.long("datadir")
|
||||||
.value_name("DIR")
|
.value_name("DIR")
|
||||||
.help("Data directory for keys and databases.")
|
.help("Data directory for keys and databases.")
|
||||||
.takes_value(true),
|
.takes_value(true)
|
||||||
|
.default_value(DEFAULT_DATA_DIR),
|
||||||
)
|
)
|
||||||
// network related arguments
|
// network related arguments
|
||||||
.arg(
|
.arg(
|
||||||
@ -94,7 +102,13 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.get_matches();
|
.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) {
|
match config.apply_cli_args(&matches) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
@ -109,3 +123,42 @@ fn main() {
|
|||||||
Err(e) => crit!(logger, "Beacon node failed to start"; "reason" => format!("{:}", e)),
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user