2019-03-22 06:04:55 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use slog::{error, info};
|
2019-02-14 01:09:18 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
2019-03-01 17:19:08 +00:00
|
|
|
use types::ChainSpec;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
/// Stores the core configuration for this validator instance.
|
|
|
|
#[derive(Clone)]
|
2019-03-22 06:27:07 +00:00
|
|
|
pub struct Config {
|
2019-02-14 01:09:18 +00:00
|
|
|
pub data_dir: PathBuf,
|
|
|
|
pub server: String,
|
2019-03-01 17:19:08 +00:00
|
|
|
pub spec: ChainSpec,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_LIGHTHOUSE_DIR: &str = ".lighthouse-validators";
|
|
|
|
|
2019-03-22 06:27:07 +00:00
|
|
|
impl Config {
|
2019-02-14 01:09:18 +00:00
|
|
|
/// Build a new configuration from defaults.
|
|
|
|
pub fn default() -> Self {
|
|
|
|
let data_dir = {
|
|
|
|
let home = dirs::home_dir().expect("Unable to determine home dir.");
|
|
|
|
home.join(DEFAULT_LIGHTHOUSE_DIR)
|
|
|
|
};
|
|
|
|
fs::create_dir_all(&data_dir)
|
|
|
|
.unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir));
|
2019-03-22 05:46:52 +00:00
|
|
|
let server = "localhost:5051".to_string();
|
2019-03-01 17:19:08 +00:00
|
|
|
let spec = ChainSpec::foundation();
|
|
|
|
Self {
|
|
|
|
data_dir,
|
|
|
|
server,
|
|
|
|
spec,
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-22 06:04:55 +00:00
|
|
|
|
|
|
|
pub fn parse_args(matches: ArgMatches, log: &slog::Logger) -> Result<Self, &'static str> {
|
2019-03-22 06:27:07 +00:00
|
|
|
let mut config = Config::default();
|
2019-03-22 06:04:55 +00:00
|
|
|
// Custom datadir
|
|
|
|
if let Some(dir) = matches.value_of("datadir") {
|
|
|
|
config.data_dir = PathBuf::from(dir.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom server port
|
|
|
|
if let Some(server_str) = matches.value_of("server") {
|
|
|
|
if let Ok(addr) = server_str.parse::<u16>() {
|
|
|
|
config.server = addr.to_string();
|
|
|
|
} else {
|
|
|
|
error!(log, "Invalid address"; "server" => server_str);
|
|
|
|
return Err("Invalid address");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Permit loading a custom spec from file.
|
|
|
|
// Custom spec
|
|
|
|
if let Some(spec_str) = matches.value_of("spec") {
|
|
|
|
match spec_str {
|
|
|
|
"foundation" => config.spec = ChainSpec::foundation(),
|
|
|
|
"few_validators" => config.spec = ChainSpec::few_validators(),
|
|
|
|
// Should be impossible due to clap's `possible_values(..)` function.
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log configuration
|
|
|
|
info!(log, "";
|
|
|
|
"data_dir" => &config.data_dir.to_str(),
|
|
|
|
"server" => &config.server);
|
|
|
|
Ok(config)
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|