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-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-20 05:23:33 +00:00
|
|
|
pub struct ValidatorClientConfig {
|
|
|
|
/// 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 directory where the individual validator configuration directories are stored.
|
|
|
|
pub validator_dir: PathBuf,
|
|
|
|
/// 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_VALIDATOR_DATADIR: &str = ".lighthouse-validator";
|
|
|
|
const DEFAULT_VALIDATORS_SUBDIR: &str = "validators";
|
|
|
|
const DEFAULT_PRIVATE_KEY_FILENAME: &str = "private.key";
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
impl ValidatorClientConfig {
|
|
|
|
/// Build a new configuration from defaults, which are overrided by arguments provided.
|
|
|
|
pub fn build_config(arguments: &ArgMatches) -> Result<Self, Error> {
|
|
|
|
// Use the specified datadir, or default in the home directory
|
|
|
|
let data_dir: PathBuf = match arguments.value_of("datadir") {
|
|
|
|
Some(path) => PathBuf::from(path.to_string()),
|
|
|
|
None => {
|
2019-03-20 05:27:58 +00:00
|
|
|
let home = dirs::home_dir().ok_or_else(|| {
|
|
|
|
Error::new(ErrorKind::NotFound, "Unable to determine home directory.")
|
|
|
|
})?;
|
2019-03-20 05:23:33 +00:00
|
|
|
home.join(DEFAULT_VALIDATOR_DATADIR)
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
};
|
2019-03-12 10:56:45 +00:00
|
|
|
fs::create_dir_all(&data_dir)?;
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
let validator_dir = data_dir.join(DEFAULT_VALIDATORS_SUBDIR);
|
|
|
|
fs::create_dir_all(&validator_dir)?;
|
|
|
|
|
|
|
|
let server: String = match arguments.value_of("server") {
|
|
|
|
Some(srv) => {
|
|
|
|
//TODO: I don't think this parses correctly a server & port combo
|
|
|
|
srv.parse::<u16>()
|
|
|
|
.map_err(|e| Error::new(ErrorKind::InvalidInput, e))?
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
None => "localhost:50051".to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Permit loading a custom spec from file.
|
|
|
|
let spec: ChainSpec = match arguments.value_of("spec") {
|
|
|
|
Some(spec_str) => {
|
|
|
|
match spec_str {
|
|
|
|
"foundation" => ChainSpec::foundation(),
|
|
|
|
"few_validators" => ChainSpec::few_validators(),
|
|
|
|
// Should be impossible due to clap's `possible_values(..)` function.
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => ChainSpec::foundation(),
|
|
|
|
};
|
2019-03-12 10:56:45 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
2019-03-01 17:19:08 +00:00
|
|
|
data_dir,
|
2019-03-20 05:23:33 +00:00
|
|
|
validator_dir,
|
2019-03-01 17:19:08 +00:00
|
|
|
server,
|
|
|
|
spec,
|
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-12 10:56:45 +00:00
|
|
|
pub fn fetch_keys(&self) -> Result<Option<Vec<Keypair>>, Error> {
|
2019-03-20 05:23:33 +00:00
|
|
|
let mut validator_dirs = fs::read_dir(&self.validator_dir)?.peekable();
|
2019-03-12 10:56:45 +00:00
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
// There are no validator directories.
|
|
|
|
if validator_dirs.peek().is_none() {
|
2019-03-12 10:56:45 +00:00
|
|
|
return Ok(None);
|
2019-03-01 17:19:08 +00:00
|
|
|
}
|
2019-03-12 10:56:45 +00:00
|
|
|
|
|
|
|
let mut key_pairs: Vec<Keypair> = Vec::new();
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
for validator_dir_result in validator_dirs {
|
|
|
|
let validator_dir = validator_dir_result?;
|
|
|
|
|
|
|
|
// Try to open the key file directly
|
|
|
|
// TODO skip keyfiles that are not found, and log the error instead of returning it.
|
|
|
|
let mut key_file = File::open(validator_dir.path().join(DEFAULT_PRIVATE_KEY_FILENAME))?;
|
2019-03-12 10:56:45 +00:00
|
|
|
|
|
|
|
let key: Keypair = bincode::deserialize_from(&mut key_file)
|
|
|
|
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
|
|
|
|
|
2019-03-20 05:23:33 +00:00
|
|
|
// TODO skip keyfile if it's not matched, and log the error instead of returning it.
|
2019-03-20 05:27:58 +00:00
|
|
|
let validator_directory_name =
|
|
|
|
validator_dir.file_name().into_string().map_err(|_| {
|
2019-03-20 05:23:33 +00:00
|
|
|
Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
|
|
|
"The filename cannot be parsed to a string.",
|
|
|
|
)
|
|
|
|
})?;
|
2019-03-20 05:27:58 +00:00
|
|
|
if key.identifier() != validator_directory_name {
|
2019-03-20 05:23:33 +00:00
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
|
|
|
"The validator directory ID did not match the key found inside.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-03-12 10:56:45 +00:00
|
|
|
key_pairs.push(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(key_pairs))
|
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
let validator_config_path = self.validator_dir.join(key.identifier());
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|