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-02-14 01:09:18 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
pub const DEFAULT_HTTP_SERVER: &str = "http://localhost:5052/";
|
2019-09-01 09:33:43 +00:00
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
/// Specifies a method for obtaining validator keypairs.
|
2019-09-01 09:33:43 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum KeySource {
|
|
|
|
/// Load the keypairs from disk.
|
|
|
|
Disk,
|
|
|
|
/// Generate the keypairs (insecure, generates predictable keys).
|
2019-11-25 04:48:24 +00:00
|
|
|
InsecureKeypairs(Vec<usize>),
|
2019-09-01 09:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-25 04:48:24 +00:00
|
|
|
/// Specifies how the validator client should load keypairs.
|
2019-09-01 09:33:43 +00:00
|
|
|
#[serde(skip)]
|
|
|
|
pub key_source: KeySource,
|
2019-11-25 04:48:24 +00:00
|
|
|
/// The http endpoint of the beacon node API.
|
|
|
|
///
|
|
|
|
/// Should be similar to `http://localhost:8080`
|
|
|
|
pub http_server: String,
|
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-11-25 04:48:24 +00:00
|
|
|
data_dir: PathBuf::from(".lighthouse/validators"),
|
2019-09-01 09:33:43 +00:00
|
|
|
key_source: <_>::default(),
|
2019-11-25 04:48:24 +00:00
|
|
|
http_server: DEFAULT_HTTP_SERVER.to_string(),
|
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-11-25 04:48:24 +00:00
|
|
|
/// Returns a `Default` implementation of `Self` with some parameters modified by the supplied
|
|
|
|
/// `cli_args`.
|
|
|
|
pub fn from_cli(cli_args: &ArgMatches) -> Result<Config, String> {
|
|
|
|
let mut config = Config::default();
|
2019-06-09 00:21:50 +00:00
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
if let Some(server) = cli_args.value_of("server") {
|
|
|
|
config.http_server = server.to_string();
|
2019-09-01 09:33:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
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(
|
|
|
|
"Cannot specify --eth2-config and --bootstrap as it may result \
|
|
|
|
in ambiguity."
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
process_testnet_subcommand(sub_cli_args, config)
|
|
|
|
}
|
|
|
|
_ => return Err("You must use the testnet command. See '--help'.".into()),
|
|
|
|
}?;
|
2019-09-01 10:09:46 +00:00
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
Ok(config)
|
2019-09-01 10:09:46 +00:00
|
|
|
}
|
2019-11-25 04:48:24 +00:00
|
|
|
}
|
2019-09-01 10:09:46 +00:00
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
/// Parses the `testnet` CLI subcommand, modifying the `config` based upon the parameters in
|
|
|
|
/// `cli_args`.
|
|
|
|
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")
|
|
|
|
.ok_or_else(|| "No first validator supplied")?
|
|
|
|
.parse::<usize>()
|
|
|
|
.map_err(|e| format!("Unable to parse first validator: {:?}", e))?;
|
|
|
|
let last = sub_cli_args
|
|
|
|
.value_of("last_validator")
|
|
|
|
.ok_or_else(|| "No last validator supplied")?
|
|
|
|
.parse::<usize>()
|
|
|
|
.map_err(|e| format!("Unable to parse last validator: {:?}", e))?;
|
|
|
|
|
|
|
|
if last < first {
|
|
|
|
return Err("Cannot supply a last validator less than the first".to_string());
|
2019-09-10 16:13:54 +00:00
|
|
|
}
|
2019-03-23 04:46:51 +00:00
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
KeySource::InsecureKeypairs((first..last).collect())
|
2019-03-12 10:56:45 +00:00
|
|
|
}
|
2019-11-25 04:48:24 +00:00
|
|
|
_ => KeySource::Disk,
|
|
|
|
};
|
2019-03-20 05:23:33 +00:00
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
Ok(config)
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|