2019-03-20 05:27:58 +00:00
|
|
|
use bincode;
|
2019-03-12 10:56:45 +00:00
|
|
|
use bls::Keypair;
|
2019-03-20 05:23:33 +00:00
|
|
|
use clap::ArgMatches;
|
2019-02-14 01:09:18 +00:00
|
|
|
use std::fs;
|
2019-03-12 10:56:45 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Error, ErrorKind};
|
2019-02-14 01:09:18 +00:00
|
|
|
use std::path::PathBuf;
|
2019-03-23 04:46:51 +00:00
|
|
|
use slog::{debug, info, error};
|
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-23 04:46:51 +00:00
|
|
|
pub struct Config {
|
2019-03-20 05:23:33 +00:00
|
|
|
/// The data directory, which stores all validator databases
|
2019-02-14 01:09:18 +00:00
|
|
|
pub data_dir: PathBuf,
|
2019-03-20 05:23:33 +00:00
|
|
|
/// The server at which the Beacon Node can be contacted
|
2019-02-14 01:09:18 +00:00
|
|
|
pub server: String,
|
2019-03-20 05:23:33 +00:00
|
|
|
/// The chain specification that we are connecting to
|
2019-03-01 17:19:08 +00:00
|
|
|
pub spec: ChainSpec,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
const DEFAULT_PRIVATE_KEY_FILENAME: &str = "private.key";
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-23 04:46:51 +00:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
let data_dir = {
|
|
|
|
let home = dirs::home_dir().expect("Unable to determine home directory.");
|
|
|
|
home.join(".lighthouse-validator")
|
|
|
|
};
|
|
|
|
fs::create_dir_all(&data_dir)
|
|
|
|
.unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir));
|
|
|
|
|
|
|
|
let server = "localhost:50051".to_string();
|
|
|
|
|
|
|
|
let spec = ChainSpec::foundation();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
data_dir,
|
|
|
|
server,
|
|
|
|
spec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2019-03-20 05:23:33 +00:00
|
|
|
/// Build a new configuration from defaults, which are overrided by arguments provided.
|
2019-03-23 04:46:51 +00:00
|
|
|
pub fn parse_args(args: &ArgMatches, log: &slog::Logger) -> Result<Self, Error> {
|
|
|
|
let mut config = Config::default();
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
// Use the specified datadir, or default in the home directory
|
2019-03-23 04:46:51 +00:00
|
|
|
if let Some(datadir) = args.value_of("datadir") {
|
|
|
|
config.data_dir = PathBuf::from(datadir);
|
|
|
|
fs::create_dir_all(&config.data_dir)
|
|
|
|
.unwrap_or_else(|_| panic!("Unable to create {:?}", &config.data_dir));
|
|
|
|
info!(log, "Using custom data dir: {:?}", &config.data_dir);
|
2019-02-14 01:09:18 +00:00
|
|
|
};
|
2019-03-23 04:46:51 +00:00
|
|
|
|
|
|
|
if let Some(srv) = args.value_of("server") {
|
|
|
|
//TODO: I don't think this parses correctly a server & port combo
|
|
|
|
config.server = srv.to_string();
|
|
|
|
info!(log, "Using custom server: {:?}", &config.server);
|
2019-03-20 05:23:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Permit loading a custom spec from file.
|
2019-03-23 04:46:51 +00:00
|
|
|
if let Some(spec_str) = args.value_of("spec") {
|
|
|
|
info!(log, "Using custom spec: {:?}", spec_str);
|
|
|
|
config.spec = match spec_str {
|
|
|
|
"foundation" => ChainSpec::foundation(),
|
|
|
|
"few_validators" => ChainSpec::few_validators(),
|
|
|
|
// Should be impossible due to clap's `possible_values(..)` function.
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2019-03-20 05:23:33 +00:00
|
|
|
};
|
2019-03-12 10:56:45 +00:00
|
|
|
|
2019-03-23 04:46:51 +00:00
|
|
|
Ok(config)
|
2019-03-12 10:56:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
/// Try to load keys from validator_dir, returning None if none are found or an error.
|
2019-03-23 04:46:51 +00:00
|
|
|
pub fn fetch_keys(&self, log: &slog::Logger) -> Option<Vec<Keypair>> {
|
2019-03-12 10:56:45 +00:00
|
|
|
|
2019-03-23 04:46:51 +00:00
|
|
|
let key_pairs: Vec<Keypair> = fs::read_dir(&self.data_dir)
|
|
|
|
.unwrap()
|
|
|
|
.filter_map( |validator_dir| {
|
|
|
|
|
|
|
|
let validator_dir = validator_dir.ok()?;
|
|
|
|
|
|
|
|
if !(validator_dir.file_type().ok()?.is_dir()) {
|
|
|
|
// Skip non-directories (i.e. no files/symlinks)
|
|
|
|
return None;
|
|
|
|
}
|
2019-03-12 10:56:45 +00:00
|
|
|
|
2019-03-23 04:46:51 +00:00
|
|
|
let key_filename = validator_dir.path().join(DEFAULT_PRIVATE_KEY_FILENAME);
|
|
|
|
|
|
|
|
if !(key_filename.is_file()) {
|
|
|
|
info!(log, "Private key is not a file: {:?}", key_filename.to_str());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!(log, "Deserializing private key from file: {:?}", key_filename.to_str());
|
|
|
|
|
|
|
|
let mut key_file = File::open(key_filename.clone()).ok()?;
|
|
|
|
|
|
|
|
let key: Keypair = if let Ok(key_ok) = bincode::deserialize_from(&mut key_file) {
|
|
|
|
key_ok
|
|
|
|
} else {
|
|
|
|
error!(log, "Unable to deserialize the private key file: {:?}", key_filename);
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
let ki = key.identifier();
|
|
|
|
if ki != validator_dir.file_name().into_string().ok()? {
|
|
|
|
error!(log, "The validator key ({:?}) did not match the directory filename {:?}.", ki, &validator_dir.path().to_string_lossy());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(key)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Check if it's an empty vector, and return none.
|
|
|
|
if key_pairs.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(key_pairs)
|
2019-03-12 10:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
/// Saves a keypair to a file inside the appropriate validator directory. Returns the saved path filename.
|
|
|
|
pub fn save_key(&self, key: &Keypair) -> Result<PathBuf, Error> {
|
2019-03-23 04:46:51 +00:00
|
|
|
let validator_config_path = self.data_dir.join(key.identifier());
|
2019-03-20 05:23:33 +00:00
|
|
|
let key_path = validator_config_path.join(DEFAULT_PRIVATE_KEY_FILENAME);
|
|
|
|
|
|
|
|
fs::create_dir_all(&validator_config_path)?;
|
|
|
|
|
2019-03-12 10:56:45 +00:00
|
|
|
let mut key_file = File::create(&key_path)?;
|
2019-03-20 05:23:33 +00:00
|
|
|
|
2019-03-12 10:56:45 +00:00
|
|
|
bincode::serialize_into(&mut key_file, &key)
|
|
|
|
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
|
2019-03-20 05:23:33 +00:00
|
|
|
Ok(key_path)
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|