Tidy validator config, CLIs

This commit is contained in:
Paul Hauner
2019-11-23 15:48:41 +11:00
parent 68942318a7
commit 52e3389a3a
7 changed files with 32 additions and 78 deletions

View File

@@ -1,18 +1,5 @@
use crate::config::Config;
use crate::config::DEFAULT_HTTP_SERVER;
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_HTTP_PORT: String = {
format!("{}", DEFAULTS.server_http_port)
};
}
pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new("validator_client")
@@ -23,16 +10,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("server")
.value_name("NETWORK_ADDRESS")
.help("Address to connect to BeaconNode.")
.default_value(&DEFAULTS.server)
.takes_value(true),
)
.arg(
Arg::with_name("server-http-port")
.long("server-http-port")
.short("h")
.value_name("PORT")
.help("Port to use for HTTP API connection to the server.")
.default_value(&DEFAULT_SERVER_HTTP_PORT)
.default_value(&DEFAULT_HTTP_SERVER)
.takes_value(true),
)
/*

View File

@@ -2,8 +2,10 @@ use clap::ArgMatches;
use serde_derive::{Deserialize, Serialize};
use std::ops::Range;
use std::path::PathBuf;
use types::{EthSpec, MainnetEthSpec};
pub const DEFAULT_HTTP_SERVER: &str = "http://localhost:5052/";
/// Specifies a method for obtaining validator keypairs.
#[derive(Clone)]
pub enum KeySource {
/// Load the keypairs from disk.
@@ -23,17 +25,13 @@ impl Default for KeySource {
pub struct Config {
/// The data directory, which stores all validator databases
pub data_dir: PathBuf,
/// The source for loading keypairs
/// Specifies how the validator client should load 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 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,
/// The http endpoint of the beacon node API.
///
/// Should be similar to `http://localhost:8080`
pub http_server: String,
}
impl Default for Config {
@@ -42,10 +40,7 @@ impl Default for Config {
Self {
data_dir: PathBuf::from(".lighthouse/validators"),
key_source: <_>::default(),
log_file: PathBuf::from(""),
server: "localhost".into(),
server_http_port: 5052,
slots_per_epoch: MainnetEthSpec::slots_per_epoch(),
http_server: DEFAULT_HTTP_SERVER.to_string(),
}
}
}
@@ -53,23 +48,13 @@ impl Default for Config {
impl Config {
/// Parses the CLI arguments and attempts to load the client configuration.
pub fn from_cli(cli_args: &ArgMatches) -> Result<Config, String> {
let mut client_config = Config::default();
if let Some(datadir) = cli_args.value_of("datadir") {
client_config.data_dir = PathBuf::from(datadir);
};
let mut config = Config::default();
if let Some(server) = cli_args.value_of("server") {
client_config.server = server.to_string();
config.http_server = server.to_string();
}
if let Some(port) = cli_args.value_of("server-http-port") {
client_config.server_http_port = port
.parse::<u16>()
.map_err(|e| format!("Unable to parse HTTP port: {:?}", e))?;
}
let client_config = match cli_args.subcommand() {
let config = match cli_args.subcommand() {
("testnet", Some(sub_cli_args)) => {
if cli_args.is_present("eth2-config") && sub_cli_args.is_present("bootstrap") {
return Err(
@@ -78,21 +63,18 @@ impl Config {
.into(),
);
}
process_testnet_subcommand(sub_cli_args, client_config)
process_testnet_subcommand(sub_cli_args, config)
}
_ => return Err("You must use the testnet command. See '--help'.".into()),
}?;
Ok(client_config)
Ok(config)
}
}
/// Parses the `testnet` CLI subcommand.
fn process_testnet_subcommand(
cli_args: &ArgMatches,
mut client_config: Config,
) -> Result<Config, String> {
client_config.key_source = match cli_args.subcommand() {
fn process_testnet_subcommand(cli_args: &ArgMatches, mut config: Config) -> Result<Config, String> {
config.key_source = match cli_args.subcommand() {
("insecure", Some(sub_cli_args)) => {
let first = sub_cli_args
.value_of("first_validator")
@@ -114,5 +96,5 @@ fn process_testnet_subcommand(
_ => KeySource::Disk,
};
Ok(client_config)
Ok(config)
}

View File

@@ -14,7 +14,7 @@ pub use config::Config;
use attestation_service::{AttestationService, AttestationServiceBuilder};
use block_service::{BlockService, BlockServiceBuilder};
use clap::ArgMatches;
use config::{Config as ClientConfig, KeySource};
use config::KeySource;
use duties_service::{DutiesService, DutiesServiceBuilder};
use environment::RuntimeContext;
use exit_future::Signal;
@@ -53,35 +53,30 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
context: RuntimeContext<T>,
cli_args: &ArgMatches,
) -> impl Future<Item = Self, Error = String> {
ClientConfig::from_cli(&cli_args)
Config::from_cli(&cli_args)
.into_future()
.map_err(|e| format!("Unable to initialize config: {}", e))
.and_then(|client_config| Self::new(context, client_config))
.and_then(|config| Self::new(context, config))
}
/// Instantiates the validator client, _without_ starting the timers to trigger block
/// and attestation production.
pub fn new(
mut context: RuntimeContext<T>,
client_config: ClientConfig,
config: Config,
) -> impl Future<Item = Self, Error = String> {
let log_1 = context.log.clone();
let log_2 = context.log.clone();
let log_3 = context.log.clone();
let http_server_addr = format!(
"http://{}:{}",
client_config.server, client_config.server_http_port
);
info!(
log_1,
"Starting validator client";
"beacon_node" => &http_server_addr,
"datadir" => format!("{:?}", client_config.data_dir),
"beacon_node" => &config.http_server,
"datadir" => format!("{:?}", config.data_dir),
);
RemoteBeaconNode::new(http_server_addr)
RemoteBeaconNode::new(config.http_server.clone())
.map_err(|e| format!("Unable to init beacon node http client: {}", e))
.into_future()
.and_then(move |beacon_node| wait_for_node(beacon_node, log_2))
@@ -125,12 +120,12 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
Duration::from_millis(context.eth2_config.spec.milliseconds_per_slot),
);
let validator_store: ValidatorStore<T> = match &client_config.key_source {
let validator_store: ValidatorStore<T> = match &config.key_source {
// Load pre-existing validators from the data dir.
//
// Use the `account_manager` to generate these files.
KeySource::Disk => ValidatorStore::load_from_disk(
client_config.data_dir.clone(),
config.data_dir.clone(),
context.eth2_config.spec.clone(),
log_3.clone(),
)?,