mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 17:26:04 +00:00
Reduce duplication of keypair generation
This commit is contained in:
@@ -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 beacon_node::{beacon_chain::BeaconChainTypes, Client, ProductionBeaconNode};
|
||||||
use environment::RuntimeContext;
|
use environment::RuntimeContext;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
@@ -6,21 +10,25 @@ use std::path::PathBuf;
|
|||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
use types::EthSpec;
|
use types::EthSpec;
|
||||||
use validator_client::{validator_directory::ValidatorDirectoryBuilder, ProductionValidatorClient};
|
use validator_client::{KeySource, ProductionValidatorClient};
|
||||||
|
|
||||||
pub use beacon_node::{ClientConfig, ClientGenesis, ProductionClient};
|
pub use beacon_node::{ClientConfig, ClientGenesis, ProductionClient};
|
||||||
pub use environment;
|
pub use environment;
|
||||||
pub use validator_client::Config as ValidatorConfig;
|
pub use validator_client::Config as ValidatorConfig;
|
||||||
|
|
||||||
/// Provides a beacon node that is running in the current process (i.e., local). Useful for testing
|
/// Provids a beacon node that is running in the current process on a given tokio executor (it
|
||||||
/// purposes.
|
/// is _local_ to this process).
|
||||||
|
///
|
||||||
|
/// Intended for use in testing and simulation. Not for production.
|
||||||
pub struct LocalBeaconNode<T> {
|
pub struct LocalBeaconNode<T> {
|
||||||
pub client: T,
|
pub client: T,
|
||||||
pub datadir: TempDir,
|
pub datadir: TempDir,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: EthSpec> LocalBeaconNode<ProductionClient<E>> {
|
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 {
|
pub fn production(context: RuntimeContext<E>, mut client_config: ClientConfig) -> Self {
|
||||||
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
||||||
let datadir = TempDir::new("lighthouse_node_test_rig")
|
let datadir = TempDir::new("lighthouse_node_test_rig")
|
||||||
@@ -80,40 +88,38 @@ pub fn testing_client_config() -> ClientConfig {
|
|||||||
client_config
|
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 struct LocalValidatorClient<T: EthSpec> {
|
||||||
pub client: ProductionValidatorClient<T>,
|
pub client: ProductionValidatorClient<T>,
|
||||||
pub datadir: TempDir,
|
pub datadir: TempDir,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: EthSpec> LocalValidatorClient<E> {
|
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(
|
pub fn production_with_insecure_keypairs(
|
||||||
context: RuntimeContext<E>,
|
context: RuntimeContext<E>,
|
||||||
config: ValidatorConfig,
|
mut config: ValidatorConfig,
|
||||||
keypair_indices: &[usize],
|
keypair_indices: &[usize],
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
||||||
let datadir = TempDir::new("lighthouse-beacon-node")
|
let datadir = TempDir::new("lighthouse-beacon-node")
|
||||||
.expect("should create temp directory for client datadir");
|
.expect("should create temp directory for client datadir");
|
||||||
|
|
||||||
keypair_indices.iter().for_each(|i| {
|
config.key_source = KeySource::InsecureKeypairs(keypair_indices.to_vec());
|
||||||
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");
|
|
||||||
});
|
|
||||||
|
|
||||||
Self::new(context, config, datadir)
|
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 {
|
pub fn production(context: RuntimeContext<E>, config: ValidatorConfig) -> Self {
|
||||||
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
||||||
let datadir = TempDir::new("lighthouse-validator")
|
let datadir = TempDir::new("lighthouse-validator")
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use std::ops::Range;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub const DEFAULT_HTTP_SERVER: &str = "http://localhost:5052/";
|
pub const DEFAULT_HTTP_SERVER: &str = "http://localhost:5052/";
|
||||||
@@ -11,7 +10,7 @@ pub enum KeySource {
|
|||||||
/// Load the keypairs from disk.
|
/// Load the keypairs from disk.
|
||||||
Disk,
|
Disk,
|
||||||
/// Generate the keypairs (insecure, generates predictable keys).
|
/// Generate the keypairs (insecure, generates predictable keys).
|
||||||
TestingKeypairRange(Range<usize>),
|
InsecureKeypairs(Vec<usize>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for KeySource {
|
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());
|
return Err("Cannot supply a last validator less than the first".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
KeySource::TestingKeypairRange(first..last)
|
KeySource::InsecureKeypairs((first..last).collect())
|
||||||
}
|
}
|
||||||
_ => KeySource::Disk,
|
_ => KeySource::Disk,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ mod validator_store;
|
|||||||
pub mod validator_directory;
|
pub mod validator_directory;
|
||||||
|
|
||||||
pub use cli::cli_app;
|
pub use cli::cli_app;
|
||||||
pub use config::Config;
|
pub use config::{Config, KeySource};
|
||||||
|
|
||||||
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::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;
|
||||||
@@ -37,7 +36,7 @@ use validator_store::ValidatorStore;
|
|||||||
/// The interval between attempts to contact the beacon node during startup.
|
/// The interval between attempts to contact the beacon node during startup.
|
||||||
const RETRY_DELAY: Duration = Duration::from_secs(2);
|
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);
|
const HTTP_TIMEOUT: Duration = Duration::from_secs(12);
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -144,9 +143,9 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
|
|||||||
// Generate ephemeral insecure keypairs for testing purposes.
|
// Generate ephemeral insecure keypairs for testing purposes.
|
||||||
//
|
//
|
||||||
// Do not use in production.
|
// Do not use in production.
|
||||||
KeySource::TestingKeypairRange(range) => {
|
KeySource::InsecureKeypairs(indices) => {
|
||||||
ValidatorStore::insecure_ephemeral_validators(
|
ValidatorStore::insecure_ephemeral_validators(
|
||||||
range.clone(),
|
&indices,
|
||||||
context.eth2_config.spec.clone(),
|
context.eth2_config.spec.clone(),
|
||||||
fork_service.clone(),
|
fork_service.clone(),
|
||||||
log_3.clone(),
|
log_3.clone(),
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use std::collections::HashMap;
|
|||||||
use std::fs::read_dir;
|
use std::fs::read_dir;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::Range;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
@@ -74,7 +73,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn insecure_ephemeral_validators(
|
pub fn insecure_ephemeral_validators(
|
||||||
range: Range<usize>,
|
validator_indices: &[usize],
|
||||||
spec: ChainSpec,
|
spec: ChainSpec,
|
||||||
fork_service: ForkService<T, E>,
|
fork_service: ForkService<T, E>,
|
||||||
log: Logger,
|
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))?;
|
.map_err(|e| format!("Unable to create temp dir: {:?}", e))?;
|
||||||
let data_dir = PathBuf::from(temp_dir.path());
|
let data_dir = PathBuf::from(temp_dir.path());
|
||||||
|
|
||||||
let validators = range
|
let validators = validator_indices
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|index| {
|
.map(|index| {
|
||||||
ValidatorDirectoryBuilder::default()
|
ValidatorDirectoryBuilder::default()
|
||||||
|
|||||||
Reference in New Issue
Block a user