mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 21:08:32 +00:00
Add first changes to validator CLI
This commit is contained in:
@@ -5,19 +5,45 @@ use serde_derive::{Deserialize, Serialize};
|
||||
use slog::{debug, error, info, o, Drain};
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::ops::Range;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use types::{EthSpec, MainnetEthSpec};
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores the core configuration for this validator instance.
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// The data directory, which stores all validator databases
|
||||
pub data_dir: PathBuf,
|
||||
/// The source for loading keypairs
|
||||
#[serde(skip)]
|
||||
pub key_source: KeySource,
|
||||
/// The path where the logs will be outputted
|
||||
pub log_file: PathBuf,
|
||||
/// The server at which the Beacon Node can be contacted
|
||||
pub server: String,
|
||||
/// 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,
|
||||
/// The number of slots per epoch.
|
||||
pub slots_per_epoch: u64,
|
||||
}
|
||||
@@ -29,14 +55,33 @@ impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
data_dir: PathBuf::from(".lighthouse-validator"),
|
||||
key_source: <_>::default(),
|
||||
log_file: PathBuf::from(""),
|
||||
server: "localhost:5051".to_string(),
|
||||
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"),
|
||||
slots_per_epoch: MainnetEthSpec::slots_per_epoch(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// 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
|
||||
@@ -94,61 +139,106 @@ impl Config {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to load keys from validator_dir, returning None if none are found or an error.
|
||||
#[allow(dead_code)]
|
||||
pub fn fetch_keys(&self, log: &slog::Logger) -> Option<Vec<Keypair>> {
|
||||
let key_pairs: Vec<Keypair> = fs::read_dir(&self.data_dir)
|
||||
.ok()?
|
||||
.filter_map(|validator_dir| {
|
||||
let validator_dir = validator_dir.ok()?;
|
||||
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;
|
||||
}
|
||||
if !(validator_dir.file_type().ok()?.is_dir()) {
|
||||
// Skip non-directories (i.e. no files/symlinks)
|
||||
return None;
|
||||
}
|
||||
|
||||
let key_filename = validator_dir.path().join(DEFAULT_PRIVATE_KEY_FILENAME);
|
||||
let key_filename = validator_dir.path().join(DEFAULT_PRIVATE_KEY_FILENAME);
|
||||
|
||||
if !(key_filename.is_file()) {
|
||||
info!(
|
||||
if !(key_filename.is_file()) {
|
||||
info!(
|
||||
log,
|
||||
"Private key is not a file: {:?}",
|
||||
key_filename.to_str()
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
debug!(
|
||||
log,
|
||||
"Private key is not a file: {:?}",
|
||||
"Deserializing private key from 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 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 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();
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user