2019-03-20 05:27:58 +00:00
|
|
|
use bincode;
|
2019-03-12 10:56:45 +00:00
|
|
|
use bls::Keypair;
|
2019-03-28 03:32:02 +00:00
|
|
|
use clap::ArgMatches;
|
2019-06-09 00:21:50 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-07-10 00:27:44 +00:00
|
|
|
use slog::{debug, error, info, o, Drain};
|
|
|
|
use std::fs::{self, File, OpenOptions};
|
2019-03-12 10:56:45 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2019-09-01 09:33:43 +00:00
|
|
|
use std::ops::Range;
|
2019-02-14 01:09:18 +00:00
|
|
|
use std::path::PathBuf;
|
2019-07-10 00:27:44 +00:00
|
|
|
use std::sync::Mutex;
|
2019-06-09 00:21:50 +00:00
|
|
|
use types::{EthSpec, MainnetEthSpec};
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
pub const DEFAULT_SERVER: &str = "localhost";
|
|
|
|
pub const DEFAULT_SERVER_GRPC_PORT: &str = "5051";
|
|
|
|
pub const DEFAULT_SERVER_HTTP_PORT: &str = "5052";
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum KeySource {
|
|
|
|
/// Load the keypairs from disk.
|
|
|
|
Disk,
|
|
|
|
/// Generate the keypairs (insecure, generates predictable keys).
|
|
|
|
TestingKeypairRange(Range<usize>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for KeySource {
|
|
|
|
fn default() -> Self {
|
|
|
|
KeySource::Disk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
/// Stores the core configuration for this validator instance.
|
2019-06-09 00:21:50 +00:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
2019-03-22 06:27:07 +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-09-01 09:33:43 +00:00
|
|
|
/// The source for loading keypairs
|
|
|
|
#[serde(skip)]
|
|
|
|
pub key_source: KeySource,
|
2019-07-10 00:27:44 +00:00
|
|
|
/// The path where the logs will be outputted
|
|
|
|
pub log_file: 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-09-01 09:33:43 +00:00
|
|
|
/// The gRPC port on the server
|
|
|
|
pub server_grpc_port: u16,
|
|
|
|
/// The HTTP port on the server, for the REST API.
|
|
|
|
pub server_http_port: u16,
|
2019-06-09 00:21:50 +00:00
|
|
|
/// The number of slots per epoch.
|
2019-06-08 11:57:25 +00:00
|
|
|
pub slots_per_epoch: u64,
|
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 {
|
2019-02-14 01:09:18 +00:00
|
|
|
/// Build a new configuration from defaults.
|
2019-03-23 04:46:51 +00:00
|
|
|
fn default() -> Self {
|
2019-03-01 17:19:08 +00:00
|
|
|
Self {
|
2019-06-09 00:21:50 +00:00
|
|
|
data_dir: PathBuf::from(".lighthouse-validator"),
|
2019-09-01 09:33:43 +00:00
|
|
|
key_source: <_>::default(),
|
2019-07-10 00:27:44 +00:00
|
|
|
log_file: PathBuf::from(""),
|
2019-09-01 09:33:43 +00:00
|
|
|
server: DEFAULT_SERVER.into(),
|
|
|
|
server_grpc_port: DEFAULT_SERVER_GRPC_PORT
|
|
|
|
.parse::<u16>()
|
|
|
|
.expect("gRPC port constant should be valid"),
|
|
|
|
server_http_port: DEFAULT_SERVER_GRPC_PORT
|
|
|
|
.parse::<u16>()
|
|
|
|
.expect("HTTP port constant should be valid"),
|
2019-06-08 12:49:04 +00:00
|
|
|
slots_per_epoch: MainnetEthSpec::slots_per_epoch(),
|
2019-03-01 17:19:08 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-23 04:46:51 +00:00
|
|
|
}
|
2019-03-22 06:04:55 +00:00
|
|
|
|
2019-03-23 04:46:51 +00:00
|
|
|
impl Config {
|
2019-09-01 09:33:43 +00:00
|
|
|
/// Returns the full path for the client data directory (not just the name of the directory).
|
|
|
|
pub fn full_data_dir(&self) -> Option<PathBuf> {
|
|
|
|
dirs::home_dir().map(|path| path.join(&self.data_dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the data directory (and any non-existing parent directories).
|
|
|
|
pub fn create_data_dir(&self) -> Option<PathBuf> {
|
|
|
|
let path = dirs::home_dir()?.join(&self.data_dir);
|
|
|
|
fs::create_dir_all(&path).ok()?;
|
|
|
|
Some(path)
|
|
|
|
}
|
|
|
|
|
2019-06-09 00:21:50 +00:00
|
|
|
/// Apply the following arguments to `self`, replacing values if they are specified in `args`.
|
|
|
|
///
|
|
|
|
/// Returns an error if arguments are obviously invalid. May succeed even if some values are
|
|
|
|
/// invalid.
|
2019-07-10 00:27:44 +00:00
|
|
|
pub fn apply_cli_args(
|
|
|
|
&mut self,
|
|
|
|
args: &ArgMatches,
|
|
|
|
log: &mut slog::Logger,
|
|
|
|
) -> Result<(), &'static str> {
|
2019-06-09 00:21:50 +00:00
|
|
|
if let Some(datadir) = args.value_of("datadir") {
|
|
|
|
self.data_dir = PathBuf::from(datadir);
|
|
|
|
};
|
|
|
|
|
2019-07-10 00:27:44 +00:00
|
|
|
if let Some(log_file) = args.value_of("logfile") {
|
|
|
|
self.log_file = PathBuf::from(log_file);
|
|
|
|
self.update_logger(log)?;
|
|
|
|
};
|
|
|
|
|
2019-06-09 00:21:50 +00:00
|
|
|
if let Some(srv) = args.value_of("server") {
|
|
|
|
self.server = srv.to_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-07-10 00:27:44 +00:00
|
|
|
// Update the logger to output in JSON to specified file
|
|
|
|
fn update_logger(&mut self, log: &mut slog::Logger) -> Result<(), &'static str> {
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(&self.log_file);
|
|
|
|
|
|
|
|
if file.is_err() {
|
|
|
|
return Err("Cannot open log file");
|
|
|
|
}
|
|
|
|
let file = file.unwrap();
|
|
|
|
|
|
|
|
if let Some(file) = self.log_file.to_str() {
|
|
|
|
info!(
|
|
|
|
*log,
|
|
|
|
"Log file specified, output will now be written to {} in json.", file
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
info!(
|
|
|
|
*log,
|
|
|
|
"Log file specified output will now be written in json"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let drain = Mutex::new(slog_json::Json::default(file)).fuse();
|
|
|
|
let drain = slog_async::Async::new(drain).build().fuse();
|
|
|
|
*log = slog::Logger::root(drain, o!());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
/// Reads a single keypair from the given `path`.
|
|
|
|
///
|
|
|
|
/// `path` should be the path to a directory containing a private key. The file name of `path`
|
|
|
|
/// must align with the public key loaded from it, otherwise an error is returned.
|
|
|
|
///
|
|
|
|
/// An error will be returned if `path` is a file (not a directory).
|
|
|
|
fn read_keypair_file(&self, path: PathBuf) -> Result<Keypair, String> {
|
|
|
|
if !path.is_dir() {
|
|
|
|
return Err("Is not a directory".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let key_filename: PathBuf = path.join(DEFAULT_PRIVATE_KEY_FILENAME);
|
|
|
|
|
|
|
|
if !key_filename.is_file() {
|
|
|
|
return Err(format!(
|
|
|
|
"Private key is not a file: {:?}",
|
|
|
|
key_filename.to_str()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut key_file = File::open(key_filename.clone())
|
|
|
|
.map_err(|e| format!("Unable to open private key file: {}", e))?;
|
|
|
|
|
|
|
|
let key: Keypair = bincode::deserialize_from(&mut key_file)
|
|
|
|
.map_err(|e| format!("Unable to deserialize private key: {:?}", e))?;
|
|
|
|
|
|
|
|
let ki = key.identifier();
|
|
|
|
if &ki
|
|
|
|
!= &path
|
|
|
|
.file_name()
|
|
|
|
.ok_or_else(|| "Invalid path".to_string())?
|
|
|
|
.to_string_lossy()
|
|
|
|
{
|
|
|
|
return Err(format!(
|
|
|
|
"The validator key ({:?}) did not match the directory filename {:?}.",
|
|
|
|
ki,
|
|
|
|
path.to_str()
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
Ok(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-03 05:23:09 +00:00
|
|
|
#[allow(dead_code)]
|
2019-03-23 04:46:51 +00:00
|
|
|
pub fn fetch_keys(&self, log: &slog::Logger) -> Option<Vec<Keypair>> {
|
2019-09-01 09:33:43 +00:00
|
|
|
let key_pairs: Vec<Keypair> =
|
|
|
|
fs::read_dir(&self.full_data_dir().expect("Data dir must exist"))
|
|
|
|
.ok()?
|
|
|
|
.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-23 04:46:51 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
let key_filename = validator_dir.path().join(DEFAULT_PRIVATE_KEY_FILENAME);
|
2019-03-12 10:56:45 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
if !(key_filename.is_file()) {
|
|
|
|
info!(
|
|
|
|
log,
|
|
|
|
"Private key is not a file: {:?}",
|
|
|
|
key_filename.to_str()
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
2019-03-23 04:46:51 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
debug!(
|
2019-03-23 04:52:17 +00:00
|
|
|
log,
|
2019-09-01 09:33:43 +00:00
|
|
|
"Deserializing private key from file: {:?}",
|
2019-03-23 04:52:17 +00:00
|
|
|
key_filename.to_str()
|
|
|
|
);
|
2019-03-23 04:46:51 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
let mut key_file = File::open(key_filename.clone()).ok()?;
|
2019-03-23 04:46:51 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
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;
|
|
|
|
};
|
2019-03-23 04:46:51 +00:00
|
|
|
|
2019-09-01 09:33:43 +00:00
|
|
|
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();
|
2019-03-23 04:46:51 +00:00
|
|
|
|
|
|
|
// 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.
|
2019-04-03 05:23:09 +00:00
|
|
|
#[allow(dead_code)]
|
2019-03-20 05:23:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|