Reduce duplication of keypair generation

This commit is contained in:
Paul Hauner
2019-11-24 07:42:13 +11:00
parent e94c265036
commit 07681612d0
4 changed files with 34 additions and 32 deletions

View File

@@ -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<T> {
pub client: T,
pub datadir: TempDir,
}
impl<E: EthSpec> LocalBeaconNode<ProductionClient<E>> {
/// 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<E>, 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<T: EthSpec> {
pub client: ProductionValidatorClient<T>,
pub datadir: TempDir,
}
impl<E: EthSpec> LocalValidatorClient<E> {
/// 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<E>,
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<E>, config: ValidatorConfig) -> Self {
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
let datadir = TempDir::new("lighthouse-validator")

View File

@@ -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<usize>),
InsecureKeypairs(Vec<usize>),
}
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,
};

View File

@@ -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<T: EthSpec> ProductionValidatorClient<T> {
// 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(),

View File

@@ -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<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
}
pub fn insecure_ephemeral_validators(
range: Range<usize>,
validator_indices: &[usize],
spec: ChainSpec,
fork_service: ForkService<T, E>,
log: Logger,
@@ -83,8 +82,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
.map_err(|e| format!("Unable to create temp dir: {:?}", e))?;
let data_dir = PathBuf::from(temp_dir.path());
let validators = range
.collect::<Vec<_>>()
let validators = validator_indices
.par_iter()
.map(|index| {
ValidatorDirectoryBuilder::default()