2019-03-22 06:27:07 +00:00
|
|
|
/// The validator service. Connects to a beacon node and signs blocks when required.
|
|
|
|
use crate::attester_service::{AttestationGrpcClient, AttesterService};
|
|
|
|
use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService};
|
|
|
|
use crate::config::Config as ValidatorConfig;
|
2019-03-25 07:03:23 +00:00
|
|
|
use crate::duties::UpdateOutcome;
|
|
|
|
use crate::duties::{DutiesManager, EpochDutiesMap};
|
2019-03-25 05:50:15 +00:00
|
|
|
use crate::error as error_chain;
|
|
|
|
use crate::error::ErrorKind;
|
2019-03-22 06:27:07 +00:00
|
|
|
use attester::test_utils::EpochMap;
|
|
|
|
use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester};
|
|
|
|
use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer};
|
|
|
|
use bls::Keypair;
|
|
|
|
use grpcio::{ChannelBuilder, EnvBuilder};
|
2019-03-23 00:48:36 +00:00
|
|
|
use protos::services::Empty;
|
2019-03-22 06:27:07 +00:00
|
|
|
use protos::services_grpc::{
|
|
|
|
AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient,
|
|
|
|
ValidatorServiceClient,
|
|
|
|
};
|
2019-03-25 05:50:15 +00:00
|
|
|
use slog::{debug, error, info, warn};
|
2019-03-23 00:48:36 +00:00
|
|
|
use slot_clock::{SlotClock, SystemTimeSlotClock};
|
2019-03-22 06:27:07 +00:00
|
|
|
use std::sync::Arc;
|
2019-03-27 08:52:05 +00:00
|
|
|
use std::sync::RwLock;
|
2019-03-23 00:48:36 +00:00
|
|
|
use std::time::{Duration, Instant, SystemTime};
|
|
|
|
use tokio::prelude::*;
|
|
|
|
use tokio::runtime::Builder;
|
|
|
|
use tokio::timer::Interval;
|
|
|
|
use tokio_timer::clock::Clock;
|
|
|
|
use types::{Epoch, Fork, Slot};
|
2019-03-28 07:58:00 +00:00
|
|
|
use types::test_utils::generate_deterministic_keypairs;
|
2019-03-23 00:48:36 +00:00
|
|
|
|
|
|
|
//TODO: This service should be simplified in the future. Can be made more steamlined.
|
2019-03-22 06:27:07 +00:00
|
|
|
|
|
|
|
/// The validator service. This is the main thread that executes and maintains validator
|
|
|
|
/// duties.
|
2019-03-22 11:50:16 +00:00
|
|
|
pub struct Service {
|
|
|
|
/// The node we currently connected to.
|
|
|
|
connected_node_version: String,
|
|
|
|
/// The chain id we are processing on.
|
|
|
|
chain_id: u16,
|
|
|
|
/// The fork state we processing on.
|
|
|
|
fork: Fork,
|
2019-03-27 08:47:08 +00:00
|
|
|
/// The slot clock for this service.
|
|
|
|
slot_clock: SystemTimeSlotClock,
|
2019-03-22 13:36:48 +00:00
|
|
|
/// The current slot we are processing.
|
|
|
|
current_slot: Slot,
|
2019-03-27 08:47:08 +00:00
|
|
|
/// The number of slots per epoch to allow for converting slots to epochs.
|
|
|
|
slots_per_epoch: u64,
|
2019-03-22 12:21:26 +00:00
|
|
|
// GRPC Clients
|
|
|
|
/// The beacon block GRPC client.
|
|
|
|
beacon_block_client: Arc<BeaconBlockServiceClient>,
|
|
|
|
/// The validator GRPC client.
|
|
|
|
validator_client: Arc<ValidatorServiceClient>,
|
|
|
|
/// The attester GRPC client.
|
|
|
|
attester_client: Arc<AttestationServiceClient>,
|
|
|
|
/// The validator client logger.
|
|
|
|
log: slog::Logger,
|
2019-03-22 11:50:16 +00:00
|
|
|
}
|
2019-03-22 06:27:07 +00:00
|
|
|
|
|
|
|
impl Service {
|
2019-03-22 11:50:16 +00:00
|
|
|
/// Initial connection to the beacon node to determine its properties.
|
2019-03-22 12:21:26 +00:00
|
|
|
///
|
|
|
|
/// This tries to connect to a beacon node. Once connected, it initialised the gRPC clients
|
|
|
|
/// and returns an instance of the service.
|
2019-03-25 05:50:15 +00:00
|
|
|
fn initialize_service(
|
|
|
|
config: &ValidatorConfig,
|
|
|
|
log: slog::Logger,
|
|
|
|
) -> error_chain::Result<Self> {
|
2019-03-22 12:21:26 +00:00
|
|
|
// initialise the beacon node client to check for a connection
|
|
|
|
|
|
|
|
let env = Arc::new(EnvBuilder::new().build());
|
|
|
|
// Beacon node gRPC beacon node endpoints.
|
|
|
|
let beacon_node_client = {
|
|
|
|
let ch = ChannelBuilder::new(env.clone()).connect(&config.server);
|
|
|
|
Arc::new(BeaconNodeServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
2019-03-22 11:50:16 +00:00
|
|
|
// retrieve node information
|
|
|
|
let node_info = loop {
|
2019-03-27 08:47:08 +00:00
|
|
|
match beacon_node_client.info(&Empty::new()) {
|
2019-03-22 11:50:16 +00:00
|
|
|
Err(e) => {
|
|
|
|
warn!(log, "Could not connect to node. Error: {}", e);
|
|
|
|
info!(log, "Retrying in 5 seconds...");
|
|
|
|
std::thread::sleep(Duration::from_secs(5));
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-25 04:10:26 +00:00
|
|
|
Ok(info) => {
|
|
|
|
if SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap()
|
2019-03-25 06:47:23 +00:00
|
|
|
.as_secs()
|
|
|
|
< info.genesis_time
|
2019-03-25 04:10:26 +00:00
|
|
|
{
|
|
|
|
warn!(
|
|
|
|
log,
|
|
|
|
"Beacon Node's genesis time is in the future. No work to do.\n Exiting"
|
|
|
|
);
|
2019-03-25 05:50:15 +00:00
|
|
|
return Err("Genesis time in the future".into());
|
2019-03-25 04:10:26 +00:00
|
|
|
}
|
|
|
|
break info;
|
|
|
|
}
|
2019-03-22 11:50:16 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-03-22 12:01:10 +00:00
|
|
|
// build requisite objects to form Self
|
|
|
|
let genesis_time = node_info.get_genesis_time();
|
2019-03-26 04:44:28 +00:00
|
|
|
let genesis_slot = Slot::from(node_info.get_genesis_slot());
|
2019-03-22 12:01:10 +00:00
|
|
|
|
|
|
|
info!(log,"Beacon node connected"; "Node Version" => node_info.version.clone(), "Chain ID" => node_info.chain_id, "Genesis time" => genesis_time);
|
2019-03-22 11:50:16 +00:00
|
|
|
|
|
|
|
let proto_fork = node_info.get_fork();
|
|
|
|
let mut previous_version: [u8; 4] = [0; 4];
|
|
|
|
let mut current_version: [u8; 4] = [0; 4];
|
|
|
|
previous_version.copy_from_slice(&proto_fork.get_previous_version()[..4]);
|
|
|
|
current_version.copy_from_slice(&proto_fork.get_current_version()[..4]);
|
|
|
|
let fork = Fork {
|
|
|
|
previous_version,
|
|
|
|
current_version,
|
|
|
|
epoch: Epoch::from(proto_fork.get_epoch()),
|
|
|
|
};
|
|
|
|
|
2019-03-22 06:27:07 +00:00
|
|
|
// initialize the RPC clients
|
|
|
|
|
|
|
|
// Beacon node gRPC beacon block endpoints.
|
2019-03-22 12:21:26 +00:00
|
|
|
let beacon_block_client = {
|
2019-03-22 06:27:07 +00:00
|
|
|
let ch = ChannelBuilder::new(env.clone()).connect(&config.server);
|
|
|
|
Arc::new(BeaconBlockServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
|
|
|
// Beacon node gRPC validator endpoints.
|
2019-03-22 12:21:26 +00:00
|
|
|
let validator_client = {
|
2019-03-22 06:27:07 +00:00
|
|
|
let ch = ChannelBuilder::new(env.clone()).connect(&config.server);
|
|
|
|
Arc::new(ValidatorServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
|
|
|
//Beacon node gRPC attester endpoints.
|
2019-03-22 12:21:26 +00:00
|
|
|
let attester_client = {
|
2019-03-22 06:27:07 +00:00
|
|
|
let ch = ChannelBuilder::new(env.clone()).connect(&config.server);
|
|
|
|
Arc::new(AttestationServiceClient::new(ch))
|
|
|
|
};
|
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
// build the validator slot clock
|
2019-03-28 03:31:35 +00:00
|
|
|
let slot_clock =
|
|
|
|
SystemTimeSlotClock::new(genesis_slot, genesis_time, config.spec.seconds_per_slot)
|
|
|
|
.expect("Unable to instantiate SystemTimeSlotClock.");
|
2019-03-27 08:47:08 +00:00
|
|
|
|
2019-03-25 05:50:15 +00:00
|
|
|
let current_slot = slot_clock
|
|
|
|
.present_slot()
|
|
|
|
.map_err(|e| ErrorKind::SlotClockError(e))?
|
|
|
|
.expect("Genesis must be in the future");
|
2019-03-23 00:48:36 +00:00
|
|
|
|
2019-03-25 05:50:15 +00:00
|
|
|
Ok(Self {
|
2019-03-22 12:21:26 +00:00
|
|
|
connected_node_version: node_info.version,
|
|
|
|
chain_id: node_info.chain_id as u16,
|
|
|
|
fork,
|
|
|
|
slot_clock,
|
2019-03-22 13:36:48 +00:00
|
|
|
current_slot,
|
2019-03-27 08:47:08 +00:00
|
|
|
slots_per_epoch: config.spec.slots_per_epoch,
|
2019-03-22 12:21:26 +00:00
|
|
|
beacon_block_client,
|
|
|
|
validator_client,
|
|
|
|
attester_client,
|
|
|
|
log,
|
2019-03-25 05:50:15 +00:00
|
|
|
})
|
2019-03-22 12:21:26 +00:00
|
|
|
}
|
2019-03-22 06:27:07 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
/// Initialise the service then run the core thread.
|
2019-03-25 05:50:15 +00:00
|
|
|
pub fn start(config: ValidatorConfig, log: slog::Logger) -> error_chain::Result<()> {
|
2019-03-23 00:48:36 +00:00
|
|
|
// connect to the node and retrieve its properties and initialize the gRPC clients
|
2019-03-25 05:50:15 +00:00
|
|
|
let service = Service::initialize_service(&config, log)?;
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
// we have connected to a node and established its parameters. Spin up the core service
|
2019-03-22 06:27:07 +00:00
|
|
|
|
2019-03-22 13:36:48 +00:00
|
|
|
// set up the validator service runtime
|
2019-03-23 00:48:36 +00:00
|
|
|
let mut runtime = Builder::new()
|
|
|
|
.clock(Clock::system())
|
|
|
|
.name_prefix("validator-client-")
|
|
|
|
.build()
|
2019-03-25 05:50:15 +00:00
|
|
|
.map_err(|e| format!("Tokio runtime failed: {}", e))?;
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-28 03:31:35 +00:00
|
|
|
let duration_to_next_slot = service
|
|
|
|
.slot_clock
|
|
|
|
.duration_to_next_slot()
|
|
|
|
.map_err(|e| format!("System clock error: {:?}", e))?
|
|
|
|
.expect("Cannot start before genesis");
|
|
|
|
|
2019-03-22 13:36:48 +00:00
|
|
|
// set up the validator work interval - start at next slot and proceed every slot
|
|
|
|
let interval = {
|
|
|
|
// Set the interval to start at the next slot, and every slot after
|
|
|
|
let slot_duration = Duration::from_secs(config.spec.seconds_per_slot);
|
|
|
|
//TODO: Handle checked add correctly
|
2019-03-28 03:31:35 +00:00
|
|
|
Interval::new(Instant::now() + duration_to_next_slot, slot_duration)
|
2019-03-23 00:48:36 +00:00
|
|
|
};
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
/* kick off core service */
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
// generate keypairs
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
// TODO: keypairs are randomly generated; they should be loaded from a file or generated.
|
|
|
|
// https://github.com/sigp/lighthouse/issues/160
|
2019-03-28 08:08:33 +00:00
|
|
|
let keypairs = Arc::new(generate_deterministic_keypairs(8));
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
/* build requisite objects to pass to core thread */
|
|
|
|
|
|
|
|
// Builds a mapping of Epoch -> Map(PublicKey, EpochDuty)
|
|
|
|
// where EpochDuty contains slot numbers and attestation data that each validator needs to
|
|
|
|
// produce work on.
|
2019-03-27 08:52:05 +00:00
|
|
|
let duties_map = RwLock::new(EpochDutiesMap::new(config.spec.slots_per_epoch));
|
2019-03-26 04:59:00 +00:00
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
// builds a manager which maintains the list of current duties for all known validators
|
|
|
|
// and can check when a validator needs to perform a task.
|
2019-03-26 00:59:48 +00:00
|
|
|
let manager = Arc::new(DutiesManager {
|
2019-03-22 13:36:48 +00:00
|
|
|
duties_map,
|
2019-03-23 00:48:36 +00:00
|
|
|
pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(),
|
|
|
|
beacon_node: service.validator_client.clone(),
|
2019-03-26 00:59:48 +00:00
|
|
|
});
|
2019-03-22 13:36:48 +00:00
|
|
|
|
2019-03-25 07:03:23 +00:00
|
|
|
// run the core thread
|
2019-03-28 06:16:43 +00:00
|
|
|
runtime.block_on(
|
|
|
|
interval
|
|
|
|
.for_each(move |_| {
|
|
|
|
let log = service.log.clone();
|
|
|
|
|
|
|
|
/* get the current slot and epoch */
|
|
|
|
let current_slot = match service.slot_clock.present_slot() {
|
|
|
|
Err(e) => {
|
|
|
|
error!(log, "SystemTimeError {:?}", e);
|
|
|
|
return Ok(());
|
2019-03-27 11:22:51 +00:00
|
|
|
}
|
2019-03-28 06:16:43 +00:00
|
|
|
Ok(slot) => slot.expect("Genesis is in the future"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let current_epoch = current_slot.epoch(service.slots_per_epoch);
|
|
|
|
|
|
|
|
debug_assert!(
|
|
|
|
current_slot > service.current_slot,
|
|
|
|
"The Timer should poll a new slot"
|
|
|
|
);
|
|
|
|
|
|
|
|
info!(log, "Processing slot: {}", current_slot.as_u64());
|
|
|
|
|
|
|
|
/* check for new duties */
|
|
|
|
|
|
|
|
let cloned_manager = manager.clone();
|
|
|
|
let cloned_log = log.clone();
|
|
|
|
// spawn a new thread separate to the runtime
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
cloned_manager.run_update(current_epoch.clone(), cloned_log.clone());
|
|
|
|
dbg!("Finished thread");
|
|
|
|
});
|
|
|
|
|
|
|
|
/* execute any specified duties */
|
|
|
|
|
|
|
|
if let Some(work) = manager.get_current_work(current_slot) {
|
|
|
|
for (_public_key, work_type) in work {
|
|
|
|
if work_type.produce_block {
|
|
|
|
// TODO: Produce a beacon block in a new thread
|
|
|
|
}
|
|
|
|
if work_type.attestation_duty.is_some() {
|
|
|
|
// available AttestationDuty info
|
|
|
|
let attestation_duty =
|
|
|
|
work_type.attestation_duty.expect("Cannot be None");
|
|
|
|
//TODO: Produce an attestation in a new thread
|
|
|
|
}
|
2019-03-27 11:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 04:59:00 +00:00
|
|
|
|
2019-03-28 06:16:43 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.map_err(|e| format!("Service thread failed: {:?}", e)),
|
|
|
|
);
|
2019-03-26 04:59:00 +00:00
|
|
|
|
|
|
|
// completed a slot process
|
2019-03-25 05:50:15 +00:00
|
|
|
Ok(())
|
2019-03-23 00:48:36 +00:00
|
|
|
}
|
2019-03-22 12:21:26 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
// Spawn a new thread to perform block production for the validator.
|
|
|
|
let producer_thread = {
|
|
|
|
let spec = spec.clone();
|
|
|
|
let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone()));
|
|
|
|
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-22 06:27:07 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
// Spawn a new thread for attestation for the validator.
|
|
|
|
let attester_thread = {
|
|
|
|
let signer = Arc::new(AttesterLocalSigner::new(keypair.clone()));
|
|
|
|
let epoch_map = epoch_map_for_attester.clone();
|
|
|
|
let slot_clock = slot_clock.clone();
|
|
|
|
let log = log.clone();
|
|
|
|
let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone()));
|
|
|
|
thread::spawn(move || {
|
|
|
|
let attester = Attester::new(epoch_map, slot_clock, client, signer);
|
|
|
|
let mut attester_service = AttesterService {
|
|
|
|
attester,
|
|
|
|
poll_interval_millis,
|
|
|
|
log,
|
|
|
|
};
|
|
|
|
|
|
|
|
attester_service.run();
|
|
|
|
})
|
|
|
|
};
|
2019-03-22 06:27:07 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
threads.push((duties_manager_thread, producer_thread, attester_thread));
|
|
|
|
}
|
2019-03-22 06:27:07 +00:00
|
|
|
|
2019-03-23 00:48:36 +00:00
|
|
|
// Naively wait for all the threads to complete.
|
|
|
|
for tuple in threads {
|
|
|
|
let (manager, producer, attester) = tuple;
|
|
|
|
let _ = producer.join();
|
|
|
|
let _ = manager.join();
|
|
|
|
let _ = attester.join();
|
2019-03-22 06:27:07 +00:00
|
|
|
}
|
2019-03-23 00:48:36 +00:00
|
|
|
*/
|
2019-03-22 06:27:07 +00:00
|
|
|
}
|