2019-03-15 10:44:39 +00:00
|
|
|
use crate::attester_service::{AttestationGrpcClient, AttesterService};
|
2019-03-22 05:48:25 +00:00
|
|
|
use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService};
|
2019-02-14 01:09:18 +00:00
|
|
|
use crate::config::ClientConfig;
|
2019-03-22 05:48:25 +00:00
|
|
|
use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap};
|
2019-03-18 20:12:06 +00:00
|
|
|
use attester::test_utils::EpochMap;
|
2019-03-08 12:48:33 +00:00
|
|
|
use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester};
|
|
|
|
use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer};
|
2019-02-14 01:09:18 +00:00
|
|
|
use bls::Keypair;
|
|
|
|
use clap::{App, Arg};
|
|
|
|
use grpcio::{ChannelBuilder, EnvBuilder};
|
2019-03-22 05:48:25 +00:00
|
|
|
use protos::services::{Empty, NodeInfo};
|
2019-03-15 10:44:39 +00:00
|
|
|
use protos::services_grpc::{
|
2019-03-22 05:48:25 +00:00
|
|
|
AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient,
|
|
|
|
ValidatorServiceClient,
|
2019-03-15 10:44:39 +00:00
|
|
|
};
|
2019-02-14 01:09:18 +00:00
|
|
|
use slog::{error, info, o, Drain};
|
|
|
|
use slot_clock::SystemTimeSlotClock;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
|
|
|
use types::ChainSpec;
|
|
|
|
|
2019-03-08 12:48:33 +00:00
|
|
|
mod attester_service;
|
2019-02-14 01:09:18 +00:00
|
|
|
mod block_producer_service;
|
|
|
|
mod config;
|
|
|
|
mod duties;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Logging
|
|
|
|
let decorator = slog_term::TermDecorator::new().build();
|
|
|
|
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
|
|
|
|
let drain = slog_async::Async::new(drain).build().fuse();
|
|
|
|
let log = slog::Logger::root(drain, o!());
|
|
|
|
|
|
|
|
// CLI
|
|
|
|
let matches = App::new("Lighthouse Validator Client")
|
|
|
|
.version("0.0.1")
|
|
|
|
.author("Sigma Prime <contact@sigmaprime.io>")
|
|
|
|
.about("Eth 2.0 Validator Client")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("datadir")
|
|
|
|
.long("datadir")
|
|
|
|
.value_name("DIR")
|
|
|
|
.help("Data directory for keys and databases.")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("server")
|
|
|
|
.long("server")
|
|
|
|
.value_name("server")
|
|
|
|
.help("Address to connect to BeaconNode.")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2019-03-01 17:19:08 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("spec")
|
|
|
|
.long("spec")
|
|
|
|
.value_name("spec")
|
|
|
|
.short("s")
|
|
|
|
.help("Configuration of Beacon Chain")
|
|
|
|
.takes_value(true)
|
2019-03-03 13:15:00 +00:00
|
|
|
.possible_values(&["foundation", "few_validators"])
|
|
|
|
.default_value("foundation"),
|
2019-03-01 17:19:08 +00:00
|
|
|
)
|
2019-02-14 01:09:18 +00:00
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let mut config = ClientConfig::default();
|
|
|
|
|
|
|
|
// Custom datadir
|
|
|
|
if let Some(dir) = matches.value_of("datadir") {
|
|
|
|
config.data_dir = PathBuf::from(dir.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom server port
|
|
|
|
if let Some(server_str) = matches.value_of("server") {
|
|
|
|
if let Ok(addr) = server_str.parse::<u16>() {
|
|
|
|
config.server = addr.to_string();
|
|
|
|
} else {
|
|
|
|
error!(log, "Invalid address"; "server" => server_str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:19:08 +00:00
|
|
|
// TODO: Permit loading a custom spec from file.
|
|
|
|
// Custom spec
|
|
|
|
if let Some(spec_str) = matches.value_of("spec") {
|
|
|
|
match spec_str {
|
|
|
|
"foundation" => config.spec = ChainSpec::foundation(),
|
|
|
|
"few_validators" => config.spec = ChainSpec::few_validators(),
|
2019-03-03 13:15:00 +00:00
|
|
|
// Should be impossible due to clap's `possible_values(..)` function.
|
|
|
|
_ => unreachable!(),
|
2019-03-01 17:19:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
// Log configuration
|
|
|
|
info!(log, "";
|
|
|
|
"data_dir" => &config.data_dir.to_str(),
|
|
|
|
"server" => &config.server);
|
|
|
|
|
2019-03-22 05:48:25 +00:00
|
|
|
// Beacon node gRPC beacon node endpoints.
|
|
|
|
let beacon_node_grpc_client = {
|
|
|
|
let env = Arc::new(EnvBuilder::new().build());
|
|
|
|
let ch = ChannelBuilder::new(env).connect(&config.server);
|
|
|
|
Arc::new(BeaconNodeServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
// Beacon node gRPC beacon block endpoints.
|
|
|
|
let beacon_block_grpc_client = {
|
|
|
|
let env = Arc::new(EnvBuilder::new().build());
|
|
|
|
let ch = ChannelBuilder::new(env).connect(&config.server);
|
|
|
|
Arc::new(BeaconBlockServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
|
|
|
// Beacon node gRPC validator endpoints.
|
|
|
|
let validator_grpc_client = {
|
|
|
|
let env = Arc::new(EnvBuilder::new().build());
|
|
|
|
let ch = ChannelBuilder::new(env).connect(&config.server);
|
|
|
|
Arc::new(ValidatorServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
2019-03-15 10:44:39 +00:00
|
|
|
//Beacon node gRPC attester endpoints.
|
|
|
|
let attester_grpc_client = {
|
|
|
|
let env = Arc::new(EnvBuilder::new().build());
|
|
|
|
let ch = ChannelBuilder::new(env).connect(&config.server);
|
|
|
|
Arc::new(AttestationServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
2019-03-22 05:48:25 +00:00
|
|
|
// retrieve node information
|
|
|
|
let node_info = beacon_node_grpc_client.info(&Empty::new());
|
|
|
|
|
|
|
|
info!(log, "Beacon node info: {:?}", node_info);
|
|
|
|
|
2019-03-01 17:19:08 +00:00
|
|
|
// Spec
|
|
|
|
let spec = Arc::new(config.spec.clone());
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
let genesis_time = 1_549_935_547;
|
|
|
|
let slot_clock = {
|
|
|
|
info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time);
|
2019-03-07 01:53:15 +00:00
|
|
|
let clock = SystemTimeSlotClock::new(genesis_time, spec.seconds_per_slot)
|
2019-02-14 01:09:18 +00:00
|
|
|
.expect("Unable to instantiate SystemTimeSlotClock.");
|
|
|
|
Arc::new(clock)
|
|
|
|
};
|
|
|
|
|
2019-03-07 01:53:15 +00:00
|
|
|
let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision.
|
|
|
|
info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start threads.
|
|
|
|
*/
|
|
|
|
let mut threads = vec![];
|
|
|
|
// TODO: keypairs are randomly generated; they should be loaded from a file or generated.
|
|
|
|
// https://github.com/sigp/lighthouse/issues/160
|
|
|
|
let keypairs = vec![Keypair::random()];
|
|
|
|
|
|
|
|
for keypair in keypairs {
|
|
|
|
info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id());
|
2019-03-04 06:51:54 +00:00
|
|
|
let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch));
|
2019-03-18 20:12:06 +00:00
|
|
|
let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
// Spawn a new thread to maintain the validator's `EpochDuties`.
|
|
|
|
let duties_manager_thread = {
|
|
|
|
let spec = spec.clone();
|
|
|
|
let duties_map = duties_map.clone();
|
|
|
|
let slot_clock = slot_clock.clone();
|
|
|
|
let log = log.clone();
|
|
|
|
let beacon_node = validator_grpc_client.clone();
|
|
|
|
let pubkey = keypair.pk.clone();
|
|
|
|
thread::spawn(move || {
|
|
|
|
let manager = DutiesManager {
|
|
|
|
duties_map,
|
|
|
|
pubkey,
|
|
|
|
spec,
|
|
|
|
slot_clock,
|
|
|
|
beacon_node,
|
|
|
|
};
|
|
|
|
let mut duties_manager_service = DutiesManagerService {
|
|
|
|
manager,
|
|
|
|
poll_interval_millis,
|
|
|
|
log,
|
|
|
|
};
|
|
|
|
|
|
|
|
duties_manager_service.run();
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Spawn a new thread to perform block production for the validator.
|
|
|
|
let producer_thread = {
|
|
|
|
let spec = spec.clone();
|
2019-03-08 12:48:33 +00:00
|
|
|
let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone()));
|
2019-02-14 01:09:18 +00:00
|
|
|
let duties_map = duties_map.clone();
|
|
|
|
let slot_clock = slot_clock.clone();
|
|
|
|
let log = log.clone();
|
|
|
|
let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone()));
|
|
|
|
thread::spawn(move || {
|
|
|
|
let block_producer =
|
|
|
|
BlockProducer::new(spec, duties_map, slot_clock, client, signer);
|
|
|
|
let mut block_producer_service = BlockProducerService {
|
|
|
|
block_producer,
|
|
|
|
poll_interval_millis,
|
|
|
|
log,
|
|
|
|
};
|
|
|
|
|
|
|
|
block_producer_service.run();
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-03-18 20:12:06 +00:00
|
|
|
// Spawn a new thread for attestation for the validator.
|
2019-03-08 12:48:33 +00:00
|
|
|
let attester_thread = {
|
|
|
|
let signer = Arc::new(AttesterLocalSigner::new(keypair.clone()));
|
2019-03-18 20:12:06 +00:00
|
|
|
let epoch_map = epoch_map_for_attester.clone();
|
2019-03-08 12:48:33 +00:00
|
|
|
let slot_clock = slot_clock.clone();
|
|
|
|
let log = log.clone();
|
2019-03-15 10:44:39 +00:00
|
|
|
let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone()));
|
2019-03-08 12:48:33 +00:00
|
|
|
thread::spawn(move || {
|
2019-03-18 20:12:06 +00:00
|
|
|
let attester = Attester::new(epoch_map, slot_clock, client, signer);
|
2019-03-08 12:48:33 +00:00
|
|
|
let mut attester_service = AttesterService {
|
|
|
|
attester,
|
|
|
|
poll_interval_millis,
|
|
|
|
log,
|
|
|
|
};
|
|
|
|
|
2019-03-15 10:44:39 +00:00
|
|
|
attester_service.run();
|
2019-03-08 12:48:33 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-03-18 20:12:06 +00:00
|
|
|
threads.push((duties_manager_thread, producer_thread, attester_thread));
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Naively wait for all the threads to complete.
|
|
|
|
for tuple in threads {
|
2019-03-18 20:12:06 +00:00
|
|
|
let (manager, producer, attester) = tuple;
|
2019-02-14 01:09:18 +00:00
|
|
|
let _ = producer.join();
|
|
|
|
let _ = manager.join();
|
2019-03-18 20:12:06 +00:00
|
|
|
let _ = attester.join();
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|