Expand beacon_chain_sim

This commit is contained in:
Paul Hauner
2019-11-20 14:40:28 +11:00
parent 90d63a46c7
commit 40a0bd0544
9 changed files with 181 additions and 24 deletions

View File

@@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
node_test_rig = { path = "../node_test_rig" }
types = { path = "../../eth2/types" }
validator_client = { path = "../../validator_client" }

View File

@@ -1,19 +1,23 @@
use node_test_rig::{
environment::{EnvironmentBuilder, RuntimeContext},
testing_client_config, ClientConfig, LocalBeaconNode, ProductionClient,
testing_client_config, ClientConfig, LocalBeaconNode, LocalValidatorClient, ProductionClient,
ValidatorConfig,
};
use types::EthSpec;
pub type BeaconNode<E> = LocalBeaconNode<ProductionClient<E>>;
fn main() {
match simulation(4) {
let nodes = 4;
let validators_per_node = 64 / nodes;
match simulation(nodes, validators_per_node) {
Ok(()) => println!("Simulation exited successfully"),
Err(e) => println!("Simulation exited with error: {}", e),
}
}
fn simulation(num_nodes: usize) -> Result<(), String> {
fn simulation(num_nodes: usize, validators_per_node: usize) -> Result<(), String> {
if num_nodes < 1 {
return Err("Must have at least one node".into());
}
@@ -28,19 +32,48 @@ fn simulation(num_nodes: usize) -> Result<(), String> {
let boot_node =
BeaconNode::production(env.service_context("boot_node".into()), base_config.clone());
let nodes = (1..num_nodes)
let mut nodes = (1..num_nodes)
.map(|i| {
let context = env.service_context(format!("node_{}", i));
new_with_bootnode_via_enr(context, &boot_node, base_config.clone())
})
.collect::<Vec<_>>();
let validators = nodes
.iter()
.enumerate()
.map(|(i, node)| {
let mut context = env.service_context(format!("validator_{}", i));
// Pull the spec from the beacon node's beacon chain, in case there were some changes
// to the spec after the node booted.
context.eth2_config.spec = node
.client
.beacon_chain()
.expect("should have beacon chain")
.spec
.clone();
let indices =
(i * validators_per_node..(i + 1) * validators_per_node).collect::<Vec<_>>();
new_validator_client(
env.service_context(format!("validator_{}", i)),
node,
ValidatorConfig::default(),
&indices,
)
})
.collect::<Vec<_>>();
nodes.insert(0, boot_node);
env.block_until_ctrl_c()?;
Ok(())
}
// TODO: this function does not result in nodes connecting to each other. Age to investigate?
// TODO: this function does not result in nodes connecting to each other. This is a bug due to
// using a 0 port for discovery. Age is fixing it.
fn new_with_bootnode_via_enr<E: EthSpec>(
context: RuntimeContext<E>,
boot_node: &BeaconNode<E>,
@@ -75,3 +108,22 @@ fn new_with_bootnode_via_multiaddr<E: EthSpec>(
BeaconNode::production(context, config)
}
fn new_validator_client<E: EthSpec>(
context: RuntimeContext<E>,
beacon_node: &BeaconNode<E>,
base_config: ValidatorConfig,
keypair_indices: &[usize],
) -> LocalValidatorClient<E> {
let mut config = base_config;
let (grpc_endpoint, grpc_port) = beacon_node
.client
.grpc_listen_addr()
.expect("Must have gRPC started");
config.server = grpc_endpoint;
config.server_grpc_port = grpc_port;
LocalValidatorClient::production_with_insecure_keypairs(context, config, keypair_indices)
}

View File

@@ -16,3 +16,4 @@ serde = "1.0"
futures = "0.1.25"
genesis = { path = "../../beacon_node/genesis" }
remote_beacon_node = { path = "../../eth2/utils/remote_beacon_node" }
validator_client = { path = "../../validator_client" }

View File

@@ -3,13 +3,17 @@ use environment::RuntimeContext;
use futures::Future;
use remote_beacon_node::RemoteBeaconNode;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
use tempdir::TempDir;
use types::EthSpec;
use validator_client::{validator_directory::ValidatorDirectoryBuilder, ProductionValidatorClient};
pub use beacon_node::{ClientConfig, ProductionClient};
pub use environment;
pub use validator_client::Config as ValidatorConfig;
/// Provides a beacon node that is running in the current process. Useful for testing purposes.
/// Provides a beacon node that is running in the current process (i.e., local). Useful for testing
/// purposes.
pub struct LocalBeaconNode<T> {
pub client: T,
pub datadir: TempDir,
@@ -56,10 +60,67 @@ pub fn testing_client_config() -> ClientConfig {
client_config.rest_api.port = 0;
client_config.websocket_server.port = 0;
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("should get system time")
.as_secs();
client_config.genesis = ClientGenesis::Interop {
validator_count: 8,
genesis_time: 13_371_337,
genesis_time: now,
};
client_config
}
pub struct LocalValidatorClient<T: EthSpec> {
pub client: ProductionValidatorClient<T>,
pub datadir: TempDir,
}
impl<E: EthSpec> LocalValidatorClient<E> {
pub fn production_with_insecure_keypairs(
context: RuntimeContext<E>,
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");
});
Self::new(context, config, datadir)
}
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")
.expect("should create temp directory for client datadir");
Self::new(context, config, datadir)
}
fn new(context: RuntimeContext<E>, mut config: ValidatorConfig, datadir: TempDir) -> Self {
config.data_dir = datadir.path().into();
let client =
ProductionValidatorClient::new(context, config).expect("should start validator client");
Self { client, datadir }
}
}