mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Merged Age's changes and ripped out heaps of now obsolete stuff in the validator client.
- Replaced most instances of PublicKey with KeyPair, since they need to be passed into each validator thread now. - Pulled out a bunch of FreeAttestations, and replaced with regular Attestations (as per Paul's suggestion) - Started generalising pubkeys to 'signers' (though they are still just Keypairs) - Added validator_index into a few structs where relevant - Removed the SlotClock and DutiesReader from the BlockProducer and Attester services, since this logic is now abstracted to the higher level process. - Added a Hash trait to the Keypair (rather than just pubkey) which assumes the Pubkey uniquely defines it.
This commit is contained in:
@@ -32,3 +32,4 @@ tokio = "0.1.18"
|
||||
tokio-timer = "0.2.10"
|
||||
error-chain = "0.12.0"
|
||||
bincode = "^1.1.2"
|
||||
futures = "0.1.25"
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::sync::Arc;
|
||||
|
||||
use attester::{BeaconNode, BeaconNodeError, PublishOutcome};
|
||||
use protos::services::ProduceAttestationDataRequest;
|
||||
use types::{AttestationData, FreeAttestation, Slot};
|
||||
use types::{AttestationData, Attestation, Slot};
|
||||
|
||||
pub struct AttestationGrpcClient {
|
||||
client: Arc<AttestationServiceClient>,
|
||||
@@ -36,7 +36,7 @@ impl BeaconNode for AttestationGrpcClient {
|
||||
|
||||
fn publish_attestation(
|
||||
&self,
|
||||
free_attestation: FreeAttestation,
|
||||
attestation: Attestation,
|
||||
) -> Result<PublishOutcome, BeaconNodeError> {
|
||||
// TODO: return correct PublishOutcome
|
||||
Err(BeaconNodeError::DecodeFailure)
|
||||
|
||||
@@ -6,18 +6,19 @@ use std::time::Duration;
|
||||
|
||||
pub use self::attestation_grpc_client::AttestationGrpcClient;
|
||||
|
||||
pub struct AttesterService<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> {
|
||||
pub attester: Attester<T, U, V, W>,
|
||||
pub struct AttesterService<U: BeaconNode, W: Signer> {
|
||||
pub attester: Attester<U, W>,
|
||||
pub poll_interval_millis: u64,
|
||||
pub log: Logger,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> AttesterService<T, U, V, W> {
|
||||
impl<U: BeaconNode, W: Signer> AttesterService<U, W> {
|
||||
/// Run a loop which polls the Attester each `poll_interval_millis` millseconds.
|
||||
///
|
||||
/// Logs the results of the polls.
|
||||
pub fn run(&mut self) {
|
||||
loop {
|
||||
/* We don't do the polling any more...
|
||||
match self.attester.poll() {
|
||||
Err(error) => {
|
||||
error!(self.log, "Attester poll error"; "error" => format!("{:?}", error))
|
||||
@@ -47,7 +48,8 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> AttesterService<T,
|
||||
error!(self.log, "The Beacon Node does not recognise the validator"; "slot" => slot)
|
||||
}
|
||||
};
|
||||
|
||||
*/
|
||||
println!("Legacy polling still happening...");
|
||||
std::thread::sleep(Duration::from_millis(self.poll_interval_millis));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,18 +10,19 @@ use std::time::Duration;
|
||||
|
||||
pub use self::beacon_block_grpc_client::BeaconBlockGrpcClient;
|
||||
|
||||
pub struct BlockProducerService<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> {
|
||||
pub block_producer: BlockProducer<T, U, V, W>,
|
||||
pub struct BlockProducerService<U: BeaconNode, W: Signer> {
|
||||
pub block_producer: BlockProducer<U, W>,
|
||||
pub poll_interval_millis: u64,
|
||||
pub log: Logger,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducerService<T, U, V, W> {
|
||||
impl<U: BeaconNode, W: Signer> BlockProducerService<U, W> {
|
||||
/// Run a loop which polls the block producer each `poll_interval_millis` millseconds.
|
||||
///
|
||||
/// Logs the results of the polls.
|
||||
pub fn run(&mut self) {
|
||||
loop {
|
||||
/* Don't do polling any more
|
||||
match self.block_producer.poll() {
|
||||
Err(error) => {
|
||||
error!(self.log, "Block producer poll error"; "error" => format!("{:?}", error))
|
||||
@@ -54,7 +55,9 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducerServi
|
||||
error!(self.log, "Unable to get a `Fork` struct to generate signature domains"; "slot" => slot)
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
println!("Legacy polling still happening...");
|
||||
std::thread::sleep(Duration::from_millis(self.poll_interval_millis));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,90 +1,125 @@
|
||||
use block_proposer::{DutiesReader, DutiesReaderError};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
use types::{Epoch, Fork, Slot};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use types::{AttestationDuty, Epoch, Keypair, Slot};
|
||||
|
||||
/// When work needs to be performed by a validator, this type is given back to the main service
|
||||
/// which indicates all the information that required to process the work.
|
||||
///
|
||||
/// Note: This is calculated per slot, so a validator knows which slot is related to this struct.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WorkInfo {
|
||||
/// Validator needs to produce a block.
|
||||
pub produce_block: bool,
|
||||
/// Validator needs to produce an attestation. This supplies the required attestation data.
|
||||
pub attestation_duty: Option<AttestationDuty>,
|
||||
}
|
||||
|
||||
/// The information required for a validator to propose and attest during some epoch.
|
||||
///
|
||||
/// Generally obtained from a Beacon Node, this information contains the validators canonical index
|
||||
/// (thier sequence in the global validator induction process) and the "shuffling" for that index
|
||||
/// (their sequence in the global validator induction process) and the "shuffling" for that index
|
||||
/// for some epoch.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub struct EpochDuties {
|
||||
pub validator_index: u64,
|
||||
pub struct EpochDuty {
|
||||
pub block_production_slot: Option<Slot>,
|
||||
// Future shard info
|
||||
pub attestation_slot: Slot,
|
||||
pub attestation_shard: u64,
|
||||
pub committee_index: u64,
|
||||
pub validator_index: u64,
|
||||
}
|
||||
|
||||
impl EpochDuties {
|
||||
/// Returns `true` if the supplied `slot` is a slot in which the validator should produce a
|
||||
/// block.
|
||||
pub fn is_block_production_slot(&self, slot: Slot) -> bool {
|
||||
match self.block_production_slot {
|
||||
impl EpochDuty {
|
||||
/// Returns `WorkInfo` if work needs to be done in the supplied `slot`
|
||||
pub fn is_work_slot(&self, slot: Slot) -> Option<WorkInfo> {
|
||||
// if validator is required to produce a slot return true
|
||||
let produce_block = match self.block_production_slot {
|
||||
Some(s) if s == slot => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
// if the validator is required to attest to a shard, create the data
|
||||
let mut attestation_duty = None;
|
||||
if self.attestation_slot == slot {
|
||||
attestation_duty = Some(AttestationDuty {
|
||||
slot,
|
||||
shard: self.attestation_shard,
|
||||
committee_index: self.committee_index as usize,
|
||||
validator_index: self.validator_index as usize
|
||||
});
|
||||
}
|
||||
|
||||
if produce_block | attestation_duty.is_some() {
|
||||
return Some(WorkInfo {
|
||||
produce_block,
|
||||
attestation_duty,
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
/// Maps a list of Keypairs (many validators) to an EpochDuty.
|
||||
pub type EpochDuties = HashMap<Keypair, Option<EpochDuty>>;
|
||||
|
||||
pub enum EpochDutiesMapError {
|
||||
Poisoned,
|
||||
UnknownEpoch,
|
||||
UnknownValidator,
|
||||
}
|
||||
|
||||
/// Maps an `epoch` to some `EpochDuties` for a single validator.
|
||||
pub struct EpochDutiesMap {
|
||||
pub slots_per_epoch: u64,
|
||||
pub map: RwLock<HashMap<Epoch, EpochDuties>>,
|
||||
pub map: HashMap<Epoch, EpochDuties>,
|
||||
}
|
||||
|
||||
impl EpochDutiesMap {
|
||||
pub fn new(slots_per_epoch: u64) -> Self {
|
||||
Self {
|
||||
slots_per_epoch,
|
||||
map: RwLock::new(HashMap::new()),
|
||||
map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, epoch: Epoch) -> Result<Option<EpochDuties>, EpochDutiesMapError> {
|
||||
let map = self.map.read().map_err(|_| EpochDutiesMapError::Poisoned)?;
|
||||
match map.get(&epoch) {
|
||||
Some(duties) => Ok(Some(*duties)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
epoch_duties: EpochDuties,
|
||||
) -> Result<Option<EpochDuties>, EpochDutiesMapError> {
|
||||
let mut map = self
|
||||
.map
|
||||
.write()
|
||||
.map_err(|_| EpochDutiesMapError::Poisoned)?;
|
||||
Ok(map.insert(epoch, epoch_duties))
|
||||
}
|
||||
}
|
||||
|
||||
impl DutiesReader for EpochDutiesMap {
|
||||
fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError> {
|
||||
// Expose the hashmap methods
|
||||
impl Deref for EpochDutiesMap {
|
||||
type Target = HashMap<Epoch, EpochDuties>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.map
|
||||
}
|
||||
}
|
||||
impl DerefMut for EpochDutiesMap {
|
||||
fn deref_mut(&mut self) -> &mut HashMap<Epoch, EpochDuties> {
|
||||
&mut self.map
|
||||
}
|
||||
}
|
||||
|
||||
impl EpochDutiesMap {
|
||||
/// Checks if the validator has work to do.
|
||||
pub fn is_work_slot(
|
||||
&self,
|
||||
slot: Slot,
|
||||
signer: &Keypair,
|
||||
) -> Result<Option<WorkInfo>, EpochDutiesMapError> {
|
||||
let epoch = slot.epoch(self.slots_per_epoch);
|
||||
|
||||
let map = self.map.read().map_err(|_| DutiesReaderError::Poisoned)?;
|
||||
let duties = map
|
||||
let epoch_duties = self
|
||||
.map
|
||||
.get(&epoch)
|
||||
.ok_or_else(|| DutiesReaderError::UnknownEpoch)?;
|
||||
Ok(duties.is_block_production_slot(slot))
|
||||
}
|
||||
|
||||
fn fork(&self) -> Result<Fork, DutiesReaderError> {
|
||||
// TODO: this is garbage data.
|
||||
//
|
||||
// It will almost certainly cause signatures to fail verification.
|
||||
Ok(Fork {
|
||||
previous_version: [0; 4],
|
||||
current_version: [0; 4],
|
||||
epoch: Epoch::new(0),
|
||||
})
|
||||
.ok_or_else(|| EpochDutiesMapError::UnknownEpoch)?;
|
||||
if let Some(epoch_duty) = epoch_duties.get(signer) {
|
||||
if let Some(duty) = epoch_duty {
|
||||
// Retrieves the duty for a validator at a given slot
|
||||
return Ok(duty.is_work_slot(slot));
|
||||
} else {
|
||||
// the validator isn't active
|
||||
return Ok(None);
|
||||
}
|
||||
} else {
|
||||
// validator isn't known
|
||||
return Err(EpochDutiesMapError::UnknownValidator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,54 +1,56 @@
|
||||
use super::epoch_duties::{EpochDuties, EpochDuty};
|
||||
use super::traits::{BeaconNode, BeaconNodeError};
|
||||
use super::EpochDuties;
|
||||
use protos::services::{ProposeBlockSlotRequest, PublicKey as IndexRequest};
|
||||
use protos::services::{GetDutiesRequest, Validators};
|
||||
use protos::services_grpc::ValidatorServiceClient;
|
||||
use ssz::ssz_encode;
|
||||
use types::{Epoch, PublicKey, Slot};
|
||||
use std::collections::HashMap;
|
||||
use types::{Epoch, Keypair, Slot};
|
||||
|
||||
impl BeaconNode for ValidatorServiceClient {
|
||||
/// Request the shuffling from the Beacon Node (BN).
|
||||
///
|
||||
/// As this function takes a `PublicKey`, it will first attempt to resolve the public key into
|
||||
/// a validator index, then call the BN for production/attestation duties.
|
||||
///
|
||||
/// Note: presently only block production information is returned.
|
||||
fn request_shuffling(
|
||||
/// Requests all duties (block signing and committee attesting) from the Beacon Node (BN).
|
||||
fn request_duties(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
public_key: &PublicKey,
|
||||
) -> Result<Option<EpochDuties>, BeaconNodeError> {
|
||||
// Lookup the validator index for the supplied public key.
|
||||
let validator_index = {
|
||||
let mut req = IndexRequest::new();
|
||||
req.set_public_key(ssz_encode(public_key).to_vec());
|
||||
let resp = self
|
||||
.validator_index(&req)
|
||||
.map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?;
|
||||
resp.get_index()
|
||||
};
|
||||
|
||||
let mut req = ProposeBlockSlotRequest::new();
|
||||
req.set_validator_index(validator_index);
|
||||
signers: &[Keypair],
|
||||
) -> Result<EpochDuties, BeaconNodeError> {
|
||||
// Get the required duties from all validators
|
||||
// build the request
|
||||
let mut req = GetDutiesRequest::new();
|
||||
req.set_epoch(epoch.as_u64());
|
||||
let mut validators = Validators::new();
|
||||
validators.set_public_keys(signers.iter().map(|v| ssz_encode(&v.pk)).collect());
|
||||
req.set_validators(validators);
|
||||
|
||||
// send the request, get the duties reply
|
||||
let reply = self
|
||||
.propose_block_slot(&req)
|
||||
.get_validator_duties(&req)
|
||||
.map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?;
|
||||
|
||||
let block_production_slot = if reply.has_slot() {
|
||||
Some(reply.get_slot())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let block_production_slot = match block_production_slot {
|
||||
Some(slot) => Some(Slot::new(slot)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(Some(EpochDuties {
|
||||
validator_index,
|
||||
block_production_slot,
|
||||
}))
|
||||
let mut epoch_duties: HashMap<Keypair, Option<EpochDuty>> = HashMap::new();
|
||||
for (index, validator_duty) in reply.get_active_validators().iter().enumerate() {
|
||||
if !validator_duty.has_duty() {
|
||||
// validator is inactive
|
||||
epoch_duties.insert(signers[index].clone(), None);
|
||||
break;
|
||||
}
|
||||
// active validator
|
||||
let active_duty = validator_duty.get_duty();
|
||||
let block_production_slot = {
|
||||
if active_duty.has_block_production_slot() {
|
||||
Some(Slot::from(active_duty.get_block_production_slot()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
let epoch_duty = EpochDuty {
|
||||
block_production_slot,
|
||||
attestation_slot: Slot::from(active_duty.get_attestation_slot()),
|
||||
attestation_shard: active_duty.get_attestation_shard(),
|
||||
committee_index: active_duty.get_committee_index(),
|
||||
validator_index: active_duty.get_validator_index(),
|
||||
};
|
||||
epoch_duties.insert(signers[index].clone(), Some(epoch_duty));
|
||||
}
|
||||
Ok(epoch_duties)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
mod epoch_duties;
|
||||
mod grpc;
|
||||
#[cfg(test)]
|
||||
mod test_node;
|
||||
// TODO: reintroduce tests
|
||||
//#[cfg(test)]
|
||||
//mod test_node;
|
||||
mod traits;
|
||||
|
||||
pub use self::epoch_duties::EpochDutiesMap;
|
||||
use self::epoch_duties::{EpochDuties, EpochDutiesMapError};
|
||||
pub use self::epoch_duties::{EpochDutiesMap, WorkInfo};
|
||||
use self::traits::{BeaconNode, BeaconNodeError};
|
||||
use bls::PublicKey;
|
||||
use slot_clock::SlotClock;
|
||||
use futures::Async;
|
||||
use slog::{debug, error, info};
|
||||
use std::sync::Arc;
|
||||
use types::{ChainSpec, Epoch, Slot};
|
||||
use std::sync::RwLock;
|
||||
use types::{Epoch, Keypair, Slot};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum UpdateOutcome {
|
||||
/// The `EpochDuties` were not updated during this poll.
|
||||
NoChange(Epoch),
|
||||
@@ -21,76 +23,116 @@ pub enum UpdateOutcome {
|
||||
/// New `EpochDuties` were obtained, different to those which were previously known. This is
|
||||
/// likely to be the result of chain re-organisation.
|
||||
DutiesChanged(Epoch, EpochDuties),
|
||||
/// The Beacon Node was unable to return the duties as the validator is unknown, or the
|
||||
/// shuffling for the epoch is unknown.
|
||||
UnknownValidatorOrEpoch(Epoch),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
SlotClockError,
|
||||
SlotUnknowable,
|
||||
DutiesMapPoisoned,
|
||||
EpochMapPoisoned,
|
||||
BeaconNodeError(BeaconNodeError),
|
||||
UnknownEpoch,
|
||||
UnknownValidator,
|
||||
}
|
||||
|
||||
/// A polling state machine which ensures the latest `EpochDuties` are obtained from the Beacon
|
||||
/// Node.
|
||||
///
|
||||
/// This keeps track of all validator keys and required voting slots.
|
||||
pub struct DutiesManager<T: SlotClock, U: BeaconNode> {
|
||||
pub duties_map: Arc<EpochDutiesMap>,
|
||||
/// A list of all public keys known to the validator service.
|
||||
pub pubkeys: Vec<PublicKey>,
|
||||
pub spec: Arc<ChainSpec>,
|
||||
pub slot_clock: Arc<T>,
|
||||
pub struct DutiesManager<U: BeaconNode> {
|
||||
pub duties_map: RwLock<EpochDutiesMap>,
|
||||
/// A list of all signer objects known to the validator service.
|
||||
// TODO: Generalise the signers, so that they're not just keypairs
|
||||
pub signers: Vec<Keypair>,
|
||||
pub beacon_node: Arc<U>,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode> DutiesManager<T, U> {
|
||||
impl<U: BeaconNode> DutiesManager<U> {
|
||||
/// Check the Beacon Node for `EpochDuties`.
|
||||
///
|
||||
/// The present `epoch` will be learned from the supplied `SlotClock`. In production this will
|
||||
/// be a wall-clock (e.g., system time, remote server time, etc.).
|
||||
pub fn update(&self, slot: Slot) -> Result<UpdateOutcome, Error> {
|
||||
let epoch = slot.epoch(self.spec.slots_per_epoch);
|
||||
|
||||
if let Some(duties) = self
|
||||
.beacon_node
|
||||
.request_shuffling(epoch, &self.pubkeys[0])?
|
||||
{
|
||||
// If these duties were known, check to see if they're updates or identical.
|
||||
let result = if let Some(known_duties) = self.duties_map.get(epoch)? {
|
||||
if known_duties == duties {
|
||||
Ok(UpdateOutcome::NoChange(epoch))
|
||||
} else {
|
||||
Ok(UpdateOutcome::DutiesChanged(epoch, duties))
|
||||
}
|
||||
fn update(&self, epoch: Epoch) -> Result<UpdateOutcome, Error> {
|
||||
let duties = self.beacon_node.request_duties(epoch, &self.signers)?;
|
||||
// If these duties were known, check to see if they're updates or identical.
|
||||
if let Some(known_duties) = self.duties_map.read()?.get(&epoch) {
|
||||
if *known_duties == duties {
|
||||
return Ok(UpdateOutcome::NoChange(epoch));
|
||||
} else {
|
||||
Ok(UpdateOutcome::NewDuties(epoch, duties))
|
||||
};
|
||||
self.duties_map.insert(epoch, duties)?;
|
||||
result
|
||||
//TODO: Duties could be large here. Remove from display and avoid the clone.
|
||||
self.duties_map.write()?.insert(epoch, duties.clone());
|
||||
return Ok(UpdateOutcome::DutiesChanged(epoch, duties));
|
||||
}
|
||||
} else {
|
||||
Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch))
|
||||
//TODO: Remove clone by removing duties from outcome
|
||||
self.duties_map.write()?.insert(epoch, duties.clone());
|
||||
return Ok(UpdateOutcome::NewDuties(epoch, duties));
|
||||
};
|
||||
}
|
||||
|
||||
/// A future wrapping around `update()`. This will perform logic based upon the update
|
||||
/// process and complete once the update has completed.
|
||||
pub fn run_update(&self, epoch: Epoch, log: slog::Logger) -> Result<Async<()>, ()> {
|
||||
match self.update(epoch) {
|
||||
Err(error) => error!(log, "Epoch duties poll error"; "error" => format!("{:?}", error)),
|
||||
Ok(UpdateOutcome::NoChange(epoch)) => {
|
||||
debug!(log, "No change in duties"; "epoch" => epoch)
|
||||
}
|
||||
Ok(UpdateOutcome::DutiesChanged(epoch, duties)) => {
|
||||
info!(log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties))
|
||||
}
|
||||
Ok(UpdateOutcome::NewDuties(epoch, duties)) => {
|
||||
info!(log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties))
|
||||
}
|
||||
};
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
/// Returns a list of (Public, WorkInfo) indicating all the validators that have work to perform
|
||||
/// this slot.
|
||||
pub fn get_current_work(&self, slot: Slot) -> Option<Vec<(Keypair, WorkInfo)>> {
|
||||
let mut current_work: Vec<(Keypair, WorkInfo)> = Vec::new();
|
||||
|
||||
// if the map is poisoned, return None
|
||||
let duties = self.duties_map.read().ok()?;
|
||||
|
||||
for validator_signer in &self.signers {
|
||||
match duties.is_work_slot(slot, &validator_signer) {
|
||||
Ok(Some(work_type)) => current_work.push((validator_signer.clone(), work_type)),
|
||||
Ok(None) => {} // No work for this validator
|
||||
//TODO: This should really log an error, as we shouldn't end up with an err here.
|
||||
Err(_) => {} // Unknown epoch or validator, no work
|
||||
}
|
||||
}
|
||||
if current_work.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(current_work)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Use error_chain to handle errors
|
||||
impl From<BeaconNodeError> for Error {
|
||||
fn from(e: BeaconNodeError) -> Error {
|
||||
Error::BeaconNodeError(e)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Use error_chain to handle errors
|
||||
impl<T> From<std::sync::PoisonError<T>> for Error {
|
||||
fn from(_e: std::sync::PoisonError<T>) -> Error {
|
||||
Error::DutiesMapPoisoned
|
||||
}
|
||||
}
|
||||
impl From<EpochDutiesMapError> for Error {
|
||||
fn from(e: EpochDutiesMapError) -> Error {
|
||||
match e {
|
||||
EpochDutiesMapError::Poisoned => Error::EpochMapPoisoned,
|
||||
EpochDutiesMapError::UnknownEpoch => Error::UnknownEpoch,
|
||||
EpochDutiesMapError::UnknownValidator => Error::UnknownValidator,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: Modify tests for new Duties Manager form
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::test_node::TestBeaconNode;
|
||||
@@ -104,6 +146,7 @@ mod tests {
|
||||
//
|
||||
// These tests should serve as a good example for future tests.
|
||||
|
||||
|
||||
#[test]
|
||||
pub fn polling() {
|
||||
let spec = Arc::new(ChainSpec::foundation());
|
||||
@@ -154,3 +197,4 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use super::EpochDuties;
|
||||
use bls::PublicKey;
|
||||
use types::Epoch;
|
||||
use types::{Epoch, Keypair};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum BeaconNodeError {
|
||||
@@ -9,12 +8,13 @@ pub enum BeaconNodeError {
|
||||
|
||||
/// Defines the methods required to obtain a validators shuffling from a Beacon Node.
|
||||
pub trait BeaconNode: Send + Sync {
|
||||
/// Get the shuffling for the given epoch and public key.
|
||||
/// Gets the duties for all validators.
|
||||
///
|
||||
/// Returns Ok(None) if the public key is unknown, or the shuffling for that epoch is unknown.
|
||||
fn request_shuffling(
|
||||
/// Returns a vector of EpochDuties for each validator public key. The entry will be None for
|
||||
/// validators that are not activated.
|
||||
fn request_duties(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
public_key: &PublicKey,
|
||||
) -> Result<Option<EpochDuties>, BeaconNodeError>;
|
||||
signers: &[Keypair],
|
||||
) -> Result<EpochDuties, BeaconNodeError>;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ use protos::services_grpc::{
|
||||
use slog::{debug, error, info, warn};
|
||||
use slot_clock::{SlotClock, SystemTimeSlotClock};
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
use std::time::{Duration, Instant, SystemTime};
|
||||
use tokio::prelude::*;
|
||||
use tokio::runtime::Builder;
|
||||
@@ -40,12 +41,14 @@ pub struct Service {
|
||||
chain_id: u16,
|
||||
/// The fork state we processing on.
|
||||
fork: Fork,
|
||||
/// The slot clock keeping track of time.
|
||||
slot_clock: Arc<SystemTimeSlotClock>,
|
||||
/// The slot clock for this service.
|
||||
slot_clock: SystemTimeSlotClock,
|
||||
/// The current slot we are processing.
|
||||
current_slot: Slot,
|
||||
/// Duration until the next slot. This is used for initializing the tokio timer interval.
|
||||
duration_to_next_slot: Duration,
|
||||
/// The number of slots per epoch to allow for converting slots to epochs.
|
||||
slots_per_epoch: u64,
|
||||
// GRPC Clients
|
||||
/// The beacon block GRPC client.
|
||||
beacon_block_client: Arc<BeaconBlockServiceClient>,
|
||||
@@ -77,7 +80,7 @@ impl Service {
|
||||
|
||||
// retrieve node information
|
||||
let node_info = loop {
|
||||
let info = match beacon_node_client.info(&Empty::new()) {
|
||||
match beacon_node_client.info(&Empty::new()) {
|
||||
Err(e) => {
|
||||
warn!(log, "Could not connect to node. Error: {}", e);
|
||||
info!(log, "Retrying in 5 seconds...");
|
||||
@@ -118,13 +121,6 @@ impl Service {
|
||||
epoch: Epoch::from(proto_fork.get_epoch()),
|
||||
};
|
||||
|
||||
// build the validator slot clock
|
||||
let slot_clock = {
|
||||
let clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot)
|
||||
.expect("Unable to instantiate SystemTimeSlotClock.");
|
||||
Arc::new(clock)
|
||||
};
|
||||
|
||||
// initialize the RPC clients
|
||||
|
||||
// Beacon node gRPC beacon block endpoints.
|
||||
@@ -145,6 +141,10 @@ impl Service {
|
||||
Arc::new(AttestationServiceClient::new(ch))
|
||||
};
|
||||
|
||||
// build the validator slot clock
|
||||
let slot_clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot)
|
||||
.expect("Unable to instantiate SystemTimeSlotClock.");
|
||||
|
||||
let current_slot = slot_clock
|
||||
.present_slot()
|
||||
.map_err(|e| ErrorKind::SlotClockError(e))?
|
||||
@@ -182,6 +182,7 @@ impl Service {
|
||||
slot_clock,
|
||||
current_slot,
|
||||
duration_to_next_slot,
|
||||
slots_per_epoch: config.spec.slots_per_epoch,
|
||||
beacon_block_client,
|
||||
validator_client,
|
||||
attester_client,
|
||||
@@ -192,7 +193,7 @@ impl Service {
|
||||
/// Initialise the service then run the core thread.
|
||||
pub fn start(config: ValidatorConfig, log: slog::Logger) -> error_chain::Result<()> {
|
||||
// connect to the node and retrieve its properties and initialize the gRPC clients
|
||||
let service = Service::initialize_service(&config, log)?;
|
||||
let service = Service::initialize_service(&config, log.clone())?;
|
||||
|
||||
// we have connected to a node and established its parameters. Spin up the core service
|
||||
|
||||
@@ -214,77 +215,140 @@ impl Service {
|
||||
)
|
||||
};
|
||||
|
||||
// kick off core service
|
||||
/* kick off core service */
|
||||
|
||||
// generate keypairs
|
||||
|
||||
// TODO: keypairs are randomly generated; they should be loaded from a file or generated.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
let keypairs = match config.fetch_keys(&log) {
|
||||
let keypairs = match config.fetch_keys(&log.clone()) {
|
||||
Some(kps) => kps,
|
||||
None => panic!("No key pairs found, cannot start validator client without. Try running ./account_manager generate first.")
|
||||
};
|
||||
|
||||
// build requisite objects to pass to core thread.
|
||||
let duties_map = Arc::new(EpochDutiesMap::new(config.spec.slots_per_epoch));
|
||||
let epoch_map_for_attester = Arc::new(EpochMap::new(config.spec.slots_per_epoch));
|
||||
let manager = DutiesManager {
|
||||
|
||||
/* build requisite objects to pass to core thread */
|
||||
|
||||
// Builds a mapping of Epoch -> Map(Keypair, EpochDuty)
|
||||
// where EpochDuty contains slot numbers and attestation data that each validator needs to
|
||||
// produce work on.
|
||||
let duties_map = RwLock::new(EpochDutiesMap::new(config.spec.slots_per_epoch));
|
||||
|
||||
// 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.
|
||||
let manager = Arc::new(DutiesManager {
|
||||
duties_map,
|
||||
pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(),
|
||||
spec: Arc::new(config.spec),
|
||||
slot_clock: service.slot_clock.clone(),
|
||||
signers: keypairs,
|
||||
beacon_node: service.validator_client.clone(),
|
||||
};
|
||||
});
|
||||
|
||||
// run the core thread
|
||||
runtime
|
||||
.block_on(interval.for_each(move |_| {
|
||||
// get the current slot
|
||||
let log = service.log.clone();
|
||||
|
||||
/* get the current slot and epoch */
|
||||
let current_slot = match service.slot_clock.present_slot() {
|
||||
Err(e) => {
|
||||
error!(service.log, "SystemTimeError {:?}", e);
|
||||
error!(log, "SystemTimeError {:?}", e);
|
||||
return Ok(());
|
||||
}
|
||||
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!(service.log, "Processing slot: {}", current_slot.as_u64());
|
||||
info!(log, "Processing slot: {}", current_slot.as_u64());
|
||||
|
||||
// check for new duties
|
||||
// TODO: Convert to its own thread
|
||||
match manager.update(current_slot) {
|
||||
Err(error) => {
|
||||
error!(service.log, "Epoch duties poll error"; "error" => format!("{:?}", error))
|
||||
/* check for new duties */
|
||||
|
||||
let cloned_log = log.clone();
|
||||
let cloned_manager = manager.clone();
|
||||
tokio::spawn(futures::future::poll_fn(move || {
|
||||
cloned_manager.run_update(current_epoch.clone(), cloned_log.clone())
|
||||
}));
|
||||
|
||||
/* execute any specified duties */
|
||||
|
||||
if let Some(work) = manager.get_current_work(current_slot) {
|
||||
for (keypair, 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");
|
||||
let attester_grpc_client =
|
||||
Arc::new(
|
||||
AttestationGrpcClient::new(
|
||||
service.attester_client.clone()
|
||||
)
|
||||
);
|
||||
let signer = Arc::new(AttesterLocalSigner::new(keypair.clone()));
|
||||
let attester =
|
||||
Attester::new(
|
||||
attester_grpc_client,
|
||||
signer);
|
||||
let mut attester_service = AttesterService {
|
||||
attester,
|
||||
poll_interval_millis: POLL_INTERVAL_MILLIS,
|
||||
log: log.clone(),
|
||||
};
|
||||
attester_service.run();
|
||||
}
|
||||
}
|
||||
Ok(UpdateOutcome::NoChange(epoch)) => {
|
||||
debug!(service.log, "No change in duties"; "epoch" => epoch)
|
||||
}
|
||||
Ok(UpdateOutcome::DutiesChanged(epoch, duties)) => {
|
||||
info!(service.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties))
|
||||
}
|
||||
Ok(UpdateOutcome::NewDuties(epoch, duties)) => {
|
||||
info!(service.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties))
|
||||
}
|
||||
Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) => {
|
||||
error!(service.log, "Epoch or validator unknown"; "epoch" => epoch)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}))
|
||||
.map_err(|e| format!("Service thread failed: {:?}", e))?;
|
||||
|
||||
// completed a slot process
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
for keypair in keypairs {
|
||||
info!(self.log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id());
|
||||
|
||||
// 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 = self.slot_clock.clone();
|
||||
let log = self.log.clone();
|
||||
let beacon_node = self.validator_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();
|
||||
})
|
||||
};
|
||||
|
||||
let mut threads = vec![];
|
||||
|
||||
for keypair in keypairs {
|
||||
info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id());
|
||||
|
||||
/*
|
||||
// Spawn a new thread to maintain the validator's `EpochDuties`.
|
||||
let duties_manager_thread = {
|
||||
let spec = spec.clone();
|
||||
@@ -331,7 +395,6 @@ impl Service {
|
||||
block_producer_service.run();
|
||||
})
|
||||
};
|
||||
*/
|
||||
|
||||
// Spawn a new thread for attestation for the validator.
|
||||
let attester_thread = {
|
||||
@@ -358,7 +421,6 @@ impl Service {
|
||||
Ok(())
|
||||
|
||||
}
|
||||
/*
|
||||
// Naively wait for all the threads to complete.
|
||||
for tuple in threads {
|
||||
let (manager, producer, attester) = tuple;
|
||||
|
||||
Reference in New Issue
Block a user