Integrate ValidatorDirectory with validator_client

This commit is contained in:
Paul Hauner
2019-11-19 10:52:29 +11:00
parent ad5fed74cb
commit 93afdac904
5 changed files with 61 additions and 85 deletions

View File

@@ -13,7 +13,6 @@ clap = "2.33.0"
slog = "2.5.2" slog = "2.5.2"
slog-term = "2.4.2" slog-term = "2.4.2"
slog-async = "2.3.0" slog-async = "2.3.0"
validator_client = { path = "../validator_client" }
types = { path = "../eth2/types" } types = { path = "../eth2/types" }
dirs = "2.0.2" dirs = "2.0.2"
environment = { path = "../lighthouse/environment" } environment = { path = "../lighthouse/environment" }

View File

@@ -9,7 +9,6 @@ name = "validator_client"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
bls = { path = "../eth2/utils/bls" }
eth2_ssz = "0.1.2" eth2_ssz = "0.1.2"
eth2_config = { path = "../eth2/utils/eth2_config" } eth2_config = { path = "../eth2/utils/eth2_config" }
tree_hash = "0.1.0" tree_hash = "0.1.0"
@@ -37,3 +36,5 @@ environment = { path = "../lighthouse/environment" }
parking_lot = "0.7" parking_lot = "0.7"
exit-future = "0.1.4" exit-future = "0.1.4"
libc = "0.2.65" libc = "0.2.65"
account_manager = { path = "../account_manager" }
lazy_static = "1.4.0"

View File

@@ -1,5 +1,21 @@
use crate::config::{DEFAULT_SERVER, DEFAULT_SERVER_GRPC_PORT, DEFAULT_SERVER_HTTP_PORT}; use crate::config::Config;
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, SubCommand};
use lazy_static::lazy_static;
lazy_static! {
/// The default configuration. Is in lazy_static because clap requires references, therefore we
/// can't initialize the defaults in the `cli_app` function
static ref DEFAULTS: Config = {
Config::default()
};
static ref DEFAULT_SERVER_GRPC_PORT: String = {
format!("{}", DEFAULTS.server_grpc_port)
};
static ref DEFAULT_SERVER_HTTP_PORT: String = {
format!("{}", DEFAULTS.server_http_port)
};
}
pub fn cli_app<'a, 'b>() -> App<'a, 'b> { pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new("Validator Client") App::new("Validator Client")
@@ -35,7 +51,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("server") .long("server")
.value_name("NETWORK_ADDRESS") .value_name("NETWORK_ADDRESS")
.help("Address to connect to BeaconNode.") .help("Address to connect to BeaconNode.")
.default_value(DEFAULT_SERVER) .default_value(&DEFAULTS.server)
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
@@ -44,7 +60,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.short("g") .short("g")
.value_name("PORT") .value_name("PORT")
.help("Port to use for gRPC API connection to the server.") .help("Port to use for gRPC API connection to the server.")
.default_value(DEFAULT_SERVER_GRPC_PORT) .default_value(&DEFAULT_SERVER_GRPC_PORT)
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
@@ -53,7 +69,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.short("h") .short("h")
.value_name("PORT") .value_name("PORT")
.help("Port to use for HTTP API connection to the server.") .help("Port to use for HTTP API connection to the server.")
.default_value(DEFAULT_SERVER_HTTP_PORT) .default_value(&DEFAULT_SERVER_HTTP_PORT)
.takes_value(true), .takes_value(true),
) )
/* /*

View File

@@ -1,5 +1,5 @@
use account_manager::validator::ValidatorDirectory;
use bincode; use bincode;
use bls::Keypair;
use clap::ArgMatches; use clap::ArgMatches;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use slog::{error, warn}; use slog::{error, warn};
@@ -9,13 +9,9 @@ use std::ops::Range;
use std::path::PathBuf; use std::path::PathBuf;
use types::{ use types::{
test_utils::{generate_deterministic_keypair, load_keypairs_from_yaml}, test_utils::{generate_deterministic_keypair, load_keypairs_from_yaml},
EthSpec, MainnetEthSpec, EthSpec, Keypair, 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)] #[derive(Clone)]
pub enum KeySource { pub enum KeySource {
/// Load the keypairs from disk. /// Load the keypairs from disk.
@@ -58,16 +54,12 @@ impl Default for Config {
/// Build a new configuration from defaults. /// Build a new configuration from defaults.
fn default() -> Self { fn default() -> Self {
Self { Self {
data_dir: PathBuf::from(".lighthouse-validator"), data_dir: PathBuf::from(".lighthouse/validators"),
key_source: <_>::default(), key_source: <_>::default(),
log_file: PathBuf::from(""), log_file: PathBuf::from(""),
server: DEFAULT_SERVER.into(), server: "localhost".into(),
server_grpc_port: DEFAULT_SERVER_GRPC_PORT server_grpc_port: 5051,
.parse::<u16>() server_http_port: 5052,
.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(), slots_per_epoch: MainnetEthSpec::slots_per_epoch(),
} }
} }
@@ -106,75 +98,44 @@ impl Config {
Ok(()) Ok(())
} }
/// Reads a single keypair from the given `path`. /// Loads the validator keys from disk.
/// ///
/// `path` should be the path to a directory containing a private key. The file name of `path` /// ## Errors
/// 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). /// Returns an error if the base directory does not exist, however it does not return for any
fn read_keypair_file(&self, path: PathBuf) -> Result<Keypair, String> { /// invalid directories/files. Instead, it just filters out failures and logs errors. This
if !path.is_dir() { /// behaviour is intended to avoid the scenario where a single invalid file can stop all
return Err("Is not a directory".into()); /// validators.
}
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()
{
Err(format!(
"The validator key ({:?}) did not match the directory filename {:?}.",
ki,
path.to_str()
))
} else {
Ok(key)
}
}
pub fn fetch_keys_from_disk(&self, log: &slog::Logger) -> Result<Vec<Keypair>, String> { pub fn fetch_keys_from_disk(&self, log: &slog::Logger) -> Result<Vec<Keypair>, String> {
Ok( let base_dir = self
fs::read_dir(&self.full_data_dir().expect("Data dir must exist")) .full_data_dir()
.map_err(|e| format!("Failed to read datadir: {:?}", e))? .ok_or_else(|| format!("Base directory does not exist: {:?}", self.full_data_dir()))?;
.filter_map(|validator_dir| {
let path = validator_dir.ok()?.path();
if path.is_dir() { let keypairs = fs::read_dir(&base_dir)
match self.read_keypair_file(path.clone()) { .map_err(|e| format!("Failed to read base directory: {:?}", e))?
Ok(keypair) => Some(keypair), .filter_map(|validator_dir| {
Err(e) => { let path = validator_dir.ok()?.path();
error!(
log, if path.is_dir() {
"Failed to parse a validator keypair"; match ValidatorDirectory::load_for_signing(path.clone()) {
"error" => e, Ok(validator_directory) => validator_directory.voting_keypair,
"path" => path.to_str(), Err(e) => {
); error!(
None log,
} "Failed to load a validator directory";
"error" => e,
"path" => path.to_str(),
);
None
} }
} else {
None
} }
}) } else {
.collect(), None
) }
})
.collect();
Ok(keypairs)
} }
pub fn fetch_testing_keypairs( pub fn fetch_testing_keypairs(

View File

@@ -14,7 +14,6 @@ use crate::config::Config as ValidatorConfig;
use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap}; use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap};
use crate::error as error_chain; use crate::error as error_chain;
use crate::signer::Signer; use crate::signer::Signer;
use bls::Keypair;
use eth2_config::Eth2Config; use eth2_config::Eth2Config;
use grpcio::{ChannelBuilder, EnvBuilder}; use grpcio::{ChannelBuilder, EnvBuilder};
use parking_lot::RwLock; use parking_lot::RwLock;
@@ -28,7 +27,7 @@ use slot_clock::{SlotClock, SystemTimeSlotClock};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use types::{ChainSpec, Epoch, EthSpec, Fork, Slot}; use types::{ChainSpec, Epoch, EthSpec, Fork, Keypair, Slot};
/// The validator service. This is the main thread that executes and maintains validator /// The validator service. This is the main thread that executes and maintains validator
/// duties. /// duties.