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

@@ -2,7 +2,7 @@ use clap::{App, Arg, SubCommand};
pub fn cli_app<'a, 'b>() -> App<'a, 'b> { pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new("account_manager") App::new("account_manager")
.visible_aliases(&["am", "account", "account_manager"]) .visible_aliases(&["a", "am", "account", "account_manager"])
.about("Utilities for generating and managing Ethereum 2.0 accounts.") .about("Utilities for generating and managing Ethereum 2.0 accounts.")
.subcommand( .subcommand(
SubCommand::with_name("validator") SubCommand::with_name("validator")

View File

@@ -1,8 +1,8 @@
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, SubCommand};
pub fn cli_app<'a, 'b>() -> App<'a, 'b> { pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new("Beacon Node") App::new("beacon_node")
.visible_aliases(&["b", "bn", "beacon", "beacon_node"]) .visible_aliases(&["b", "bn", "beacon"])
.version(crate_version!()) .version(crate_version!())
.author("Sigma Prime <contact@sigmaprime.io>") .author("Sigma Prime <contact@sigmaprime.io>")
.about("Eth 2.0 Client") .about("Eth 2.0 Client")

View File

@@ -158,7 +158,7 @@ fn run<E: EthSpec>(
None None
}; };
let validator_client = if let Some(sub_matches) = matches.subcommand_matches("Validator Client") let validator_client = if let Some(sub_matches) = matches.subcommand_matches("validator_client")
{ {
let runtime_context = environment.core_context(); let runtime_context = environment.core_context();

View File

@@ -34,7 +34,6 @@ 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"
lazy_static = "1.4.0"
eth2_ssz_derive = { path = "../eth2/utils/ssz_derive" } eth2_ssz_derive = { path = "../eth2/utils/ssz_derive" }
hex = "0.4" hex = "0.4"
deposit_contract = { path = "../eth2/utils/deposit_contract" } deposit_contract = { path = "../eth2/utils/deposit_contract" }

View File

@@ -1,18 +1,5 @@
use crate::config::Config; use crate::config::DEFAULT_HTTP_SERVER;
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_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")
@@ -23,16 +10,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(&DEFAULTS.server) .default_value(&DEFAULT_HTTP_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)
.takes_value(true), .takes_value(true),
) )
/* /*

View File

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

View File

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