From 07681612d0e3f57314695cc6e9cf9e54b0cdf317 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Nov 2019 07:42:13 +1100 Subject: [PATCH] Reduce duplication of keypair generation --- tests/node_test_rig/src/lib.rs | 46 ++++++++++++++----------- validator_client/src/config.rs | 5 ++- validator_client/src/lib.rs | 9 +++-- validator_client/src/validator_store.rs | 6 ++-- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/tests/node_test_rig/src/lib.rs b/tests/node_test_rig/src/lib.rs index 2061031edd..c81034be51 100644 --- a/tests/node_test_rig/src/lib.rs +++ b/tests/node_test_rig/src/lib.rs @@ -1,3 +1,7 @@ +//! Provides easy ways to run a beacon node or validator client in-process. +//! +//! Intended to be used for testing and simulation purposes. Not for production. + use beacon_node::{beacon_chain::BeaconChainTypes, Client, ProductionBeaconNode}; use environment::RuntimeContext; use futures::Future; @@ -6,21 +10,25 @@ use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; use tempdir::TempDir; use types::EthSpec; -use validator_client::{validator_directory::ValidatorDirectoryBuilder, ProductionValidatorClient}; +use validator_client::{KeySource, ProductionValidatorClient}; pub use beacon_node::{ClientConfig, ClientGenesis, ProductionClient}; pub use environment; pub use validator_client::Config as ValidatorConfig; -/// Provides a beacon node that is running in the current process (i.e., local). Useful for testing -/// purposes. +/// Provids a beacon node that is running in the current process on a given tokio executor (it +/// is _local_ to this process). +/// +/// Intended for use in testing and simulation. Not for production. pub struct LocalBeaconNode { pub client: T, pub datadir: TempDir, } impl LocalBeaconNode> { - /// Starts a new, production beacon node. + /// Starts a new, production beacon node on the tokio runtime in the given `context`. + /// + /// The node created is using the same types as the node we use in production. pub fn production(context: RuntimeContext, mut client_config: ClientConfig) -> Self { // Creates a temporary directory that will be deleted once this `TempDir` is dropped. let datadir = TempDir::new("lighthouse_node_test_rig") @@ -80,40 +88,38 @@ pub fn testing_client_config() -> ClientConfig { client_config } +/// Provids a validator client that is running in the current process on a given tokio executor (it +/// is _local_ to this process). +/// +/// Intended for use in testing and simulation. Not for production. pub struct LocalValidatorClient { pub client: ProductionValidatorClient, pub datadir: TempDir, } impl LocalValidatorClient { + /// Creates a validator client with insecure deterministic keypairs. The validator directories + /// are created in a temp dir then removed when the process exits. + /// + /// The validator created is using the same types as the node we use in production. pub fn production_with_insecure_keypairs( context: RuntimeContext, - config: ValidatorConfig, + mut config: ValidatorConfig, keypair_indices: &[usize], ) -> Self { // Creates a temporary directory that will be deleted once this `TempDir` is dropped. let datadir = TempDir::new("lighthouse-beacon-node") .expect("should create temp directory for client datadir"); - keypair_indices.iter().for_each(|i| { - ValidatorDirectoryBuilder::default() - .spec(context.eth2_config.spec.clone()) - .full_deposit_amount() - .expect("should set full deposit amount") - .insecure_keypairs(*i) - .create_directory(PathBuf::from(datadir.path())) - .expect("should create directory") - .write_keypair_files() - .expect("should write keypair files") - .write_eth1_data_file() - .expect("should write eth1 data file") - .build() - .expect("should build dir"); - }); + config.key_source = KeySource::InsecureKeypairs(keypair_indices.to_vec()); Self::new(context, config, datadir) } + /// Creates a validator client that attempts to read keys from the default data dir. + /// + /// - The validator created is using the same types as the node we use in production. + /// - It is recommended to use `production_with_insecure_keypairs` for testing. pub fn production(context: RuntimeContext, config: ValidatorConfig) -> Self { // Creates a temporary directory that will be deleted once this `TempDir` is dropped. let datadir = TempDir::new("lighthouse-validator") diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 3f09560c91..001f453de8 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -1,6 +1,5 @@ use clap::ArgMatches; use serde_derive::{Deserialize, Serialize}; -use std::ops::Range; use std::path::PathBuf; pub const DEFAULT_HTTP_SERVER: &str = "http://localhost:5052/"; @@ -11,7 +10,7 @@ pub enum KeySource { /// Load the keypairs from disk. Disk, /// Generate the keypairs (insecure, generates predictable keys). - TestingKeypairRange(Range), + InsecureKeypairs(Vec), } impl Default for KeySource { @@ -93,7 +92,7 @@ fn process_testnet_subcommand(cli_args: &ArgMatches, mut config: Config) -> Resu return Err("Cannot supply a last validator less than the first".to_string()); } - KeySource::TestingKeypairRange(first..last) + KeySource::InsecureKeypairs((first..last).collect()) } _ => KeySource::Disk, }; diff --git a/validator_client/src/lib.rs b/validator_client/src/lib.rs index 52f6360ac9..bdd373c33d 100644 --- a/validator_client/src/lib.rs +++ b/validator_client/src/lib.rs @@ -9,12 +9,11 @@ mod validator_store; pub mod validator_directory; pub use cli::cli_app; -pub use config::Config; +pub use config::{Config, KeySource}; use attestation_service::{AttestationService, AttestationServiceBuilder}; use block_service::{BlockService, BlockServiceBuilder}; use clap::ArgMatches; -use config::KeySource; use duties_service::{DutiesService, DutiesServiceBuilder}; use environment::RuntimeContext; use exit_future::Signal; @@ -37,7 +36,7 @@ use validator_store::ValidatorStore; /// The interval between attempts to contact the beacon node during startup. const RETRY_DELAY: Duration = Duration::from_secs(2); -/// The global timeout for HTTP events. +/// The global timeout for HTTP requests to the beacon node. const HTTP_TIMEOUT: Duration = Duration::from_secs(12); #[derive(Clone)] @@ -144,9 +143,9 @@ impl ProductionValidatorClient { // Generate ephemeral insecure keypairs for testing purposes. // // Do not use in production. - KeySource::TestingKeypairRange(range) => { + KeySource::InsecureKeypairs(indices) => { ValidatorStore::insecure_ephemeral_validators( - range.clone(), + &indices, context.eth2_config.spec.clone(), fork_service.clone(), log_3.clone(), diff --git a/validator_client/src/validator_store.rs b/validator_client/src/validator_store.rs index 892b015ee7..4256ce4939 100644 --- a/validator_client/src/validator_store.rs +++ b/validator_client/src/validator_store.rs @@ -8,7 +8,6 @@ use std::collections::HashMap; use std::fs::read_dir; use std::iter::FromIterator; use std::marker::PhantomData; -use std::ops::Range; use std::path::PathBuf; use std::sync::Arc; use tempdir::TempDir; @@ -74,7 +73,7 @@ impl ValidatorStore { } pub fn insecure_ephemeral_validators( - range: Range, + validator_indices: &[usize], spec: ChainSpec, fork_service: ForkService, log: Logger, @@ -83,8 +82,7 @@ impl ValidatorStore { .map_err(|e| format!("Unable to create temp dir: {:?}", e))?; let data_dir = PathBuf::from(temp_dir.path()); - let validators = range - .collect::>() + let validators = validator_indices .par_iter() .map(|index| { ValidatorDirectoryBuilder::default()