2019-02-14 01:09:18 +00:00
|
|
|
mod epoch_duties;
|
|
|
|
mod grpc;
|
2019-03-27 08:47:08 +00:00
|
|
|
// TODO: reintroduce tests
|
|
|
|
//#[cfg(test)]
|
|
|
|
//mod test_node;
|
2019-02-14 01:09:18 +00:00
|
|
|
mod traits;
|
|
|
|
|
|
|
|
use self::epoch_duties::{EpochDuties, EpochDutiesMapError};
|
2019-03-27 11:41:55 +00:00
|
|
|
pub use self::epoch_duties::{EpochDutiesMap, WorkInfo};
|
2019-02-14 01:09:18 +00:00
|
|
|
use self::traits::{BeaconNode, BeaconNodeError};
|
2019-03-26 00:59:48 +00:00
|
|
|
use futures::Async;
|
|
|
|
use slog::{debug, error, info};
|
2019-02-14 01:09:18 +00:00
|
|
|
use std::sync::Arc;
|
2019-03-27 08:47:08 +00:00
|
|
|
use std::sync::RwLock;
|
2019-03-27 11:22:51 +00:00
|
|
|
use types::{Epoch, PublicKey, Slot};
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-03-25 07:03:23 +00:00
|
|
|
pub enum UpdateOutcome {
|
2019-02-14 01:09:18 +00:00
|
|
|
/// The `EpochDuties` were not updated during this poll.
|
|
|
|
NoChange(Epoch),
|
|
|
|
/// The `EpochDuties` for the `epoch` were previously unknown, but obtained in the poll.
|
|
|
|
NewDuties(Epoch, EpochDuties),
|
|
|
|
/// 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),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
2019-03-27 08:47:08 +00:00
|
|
|
DutiesMapPoisoned,
|
2019-02-14 01:09:18 +00:00
|
|
|
EpochMapPoisoned,
|
|
|
|
BeaconNodeError(BeaconNodeError),
|
2019-03-27 08:47:08 +00:00
|
|
|
UnknownEpoch,
|
|
|
|
UnknownValidator,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A polling state machine which ensures the latest `EpochDuties` are obtained from the Beacon
|
|
|
|
/// Node.
|
|
|
|
///
|
2019-03-23 00:48:36 +00:00
|
|
|
/// This keeps track of all validator keys and required voting slots.
|
2019-03-26 04:59:00 +00:00
|
|
|
pub struct DutiesManager<U: BeaconNode> {
|
2019-03-27 08:47:08 +00:00
|
|
|
pub duties_map: RwLock<EpochDutiesMap>,
|
2019-03-23 00:48:36 +00:00
|
|
|
/// A list of all public keys known to the validator service.
|
|
|
|
pub pubkeys: Vec<PublicKey>,
|
2019-02-14 01:09:18 +00:00
|
|
|
pub beacon_node: Arc<U>,
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:59:00 +00:00
|
|
|
impl<U: BeaconNode> DutiesManager<U> {
|
2019-03-25 07:03:23 +00:00
|
|
|
/// Check the Beacon Node for `EpochDuties`.
|
2019-02-14 01:09:18 +00:00
|
|
|
///
|
|
|
|
/// be a wall-clock (e.g., system time, remote server time, etc.).
|
2019-03-27 08:47:08 +00:00
|
|
|
fn update(&self, epoch: Epoch) -> Result<UpdateOutcome, Error> {
|
|
|
|
let duties = self.beacon_node.request_duties(epoch, &self.pubkeys)?;
|
2019-03-28 06:16:43 +00:00
|
|
|
{
|
|
|
|
// 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));
|
|
|
|
}
|
2019-03-27 08:47:08 +00:00
|
|
|
}
|
2019-03-28 06:16:43 +00:00
|
|
|
}
|
|
|
|
if !self.duties_map.read()?.contains_key(&epoch) {
|
2019-03-27 11:22:51 +00:00
|
|
|
//TODO: Remove clone by removing duties from outcome
|
|
|
|
self.duties_map.write()?.insert(epoch, duties.clone());
|
|
|
|
return Ok(UpdateOutcome::NewDuties(epoch, duties));
|
2019-03-28 06:16:43 +00:00
|
|
|
}
|
|
|
|
// duties have changed
|
|
|
|
//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));
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-26 00:59:48 +00:00
|
|
|
|
|
|
|
/// A future wrapping around `update()`. This will perform logic based upon the update
|
|
|
|
/// process and complete once the update has completed.
|
2019-03-27 08:47:08 +00:00
|
|
|
pub fn run_update(&self, epoch: Epoch, log: slog::Logger) -> Result<Async<()>, ()> {
|
|
|
|
match self.update(epoch) {
|
2019-03-26 00:59:48 +00:00
|
|
|
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)) => {
|
2019-03-28 15:23:03 +00:00
|
|
|
info!(log, "New duties obtained"; "epoch" => epoch);
|
|
|
|
print_duties(&log, duties);
|
2019-03-26 00:59:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
2019-03-27 11:22:51 +00:00
|
|
|
|
2019-03-27 11:41:55 +00:00
|
|
|
/// Returns a list of (Public, WorkInfo) indicating all the validators that have work to perform
|
2019-03-27 11:22:51 +00:00
|
|
|
/// this slot.
|
2019-03-27 11:41:55 +00:00
|
|
|
pub fn get_current_work(&self, slot: Slot) -> Option<Vec<(PublicKey, WorkInfo)>> {
|
|
|
|
let mut current_work: Vec<(PublicKey, WorkInfo)> = Vec::new();
|
2019-03-27 11:22:51 +00:00
|
|
|
|
|
|
|
// if the map is poisoned, return None
|
|
|
|
let duties = self.duties_map.read().ok()?;
|
|
|
|
|
|
|
|
for validator_pk in &self.pubkeys {
|
|
|
|
match duties.is_work_slot(slot, &validator_pk) {
|
|
|
|
Ok(Some(work_type)) => current_work.push((validator_pk.clone(), work_type)),
|
|
|
|
Ok(None) => {} // No work for this validator
|
|
|
|
Err(_) => {} // Unknown epoch or validator, no work
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if current_work.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(current_work)
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
//TODO: Use error_chain to handle errors
|
2019-02-14 01:09:18 +00:00
|
|
|
impl From<BeaconNodeError> for Error {
|
|
|
|
fn from(e: BeaconNodeError) -> Error {
|
|
|
|
Error::BeaconNodeError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:47:08 +00:00
|
|
|
//TODO: Use error_chain to handle errors
|
|
|
|
impl<T> From<std::sync::PoisonError<T>> for Error {
|
2019-03-27 11:22:51 +00:00
|
|
|
fn from(_e: std::sync::PoisonError<T>) -> Error {
|
2019-03-27 08:47:08 +00:00
|
|
|
Error::DutiesMapPoisoned
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
impl From<EpochDutiesMapError> for Error {
|
|
|
|
fn from(e: EpochDutiesMapError) -> Error {
|
|
|
|
match e {
|
2019-03-27 08:47:08 +00:00
|
|
|
EpochDutiesMapError::UnknownEpoch => Error::UnknownEpoch,
|
|
|
|
EpochDutiesMapError::UnknownValidator => Error::UnknownValidator,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 15:23:03 +00:00
|
|
|
fn print_duties(log: &slog::Logger, duties: EpochDuties) {
|
|
|
|
for (pk, duty) in duties.iter() {
|
|
|
|
if let Some(display_duty) = duty {
|
|
|
|
info!(log, "Validator: {}",pk; "Duty" => format!("{}",display_duty));
|
|
|
|
} else {
|
|
|
|
info!(log, "Validator: {}",pk; "Duty" => "None");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 00:33:24 +00:00
|
|
|
/* TODO: Modify tests for new Duties Manager form
|
2019-02-14 01:09:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::test_node::TestBeaconNode;
|
|
|
|
use super::*;
|
|
|
|
use bls::Keypair;
|
|
|
|
use slot_clock::TestingSlotClock;
|
|
|
|
use types::Slot;
|
|
|
|
|
|
|
|
// TODO: implement more thorough testing.
|
|
|
|
// https://github.com/sigp/lighthouse/issues/160
|
|
|
|
//
|
|
|
|
// These tests should serve as a good example for future tests.
|
|
|
|
|
2019-03-26 00:33:24 +00:00
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
#[test]
|
|
|
|
pub fn polling() {
|
|
|
|
let spec = Arc::new(ChainSpec::foundation());
|
2019-03-04 06:51:54 +00:00
|
|
|
let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch));
|
2019-02-14 01:09:18 +00:00
|
|
|
let keypair = Keypair::random();
|
|
|
|
let slot_clock = Arc::new(TestingSlotClock::new(0));
|
|
|
|
let beacon_node = Arc::new(TestBeaconNode::default());
|
|
|
|
|
|
|
|
let manager = DutiesManager {
|
|
|
|
spec: spec.clone(),
|
|
|
|
pubkey: keypair.pk.clone(),
|
|
|
|
duties_map: duties_map.clone(),
|
|
|
|
slot_clock: slot_clock.clone(),
|
|
|
|
beacon_node: beacon_node.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Configure response from the BeaconNode.
|
|
|
|
let duties = EpochDuties {
|
|
|
|
validator_index: 0,
|
|
|
|
block_production_slot: Some(Slot::new(10)),
|
|
|
|
};
|
|
|
|
beacon_node.set_next_shuffling_result(Ok(Some(duties)));
|
|
|
|
|
|
|
|
// Get the duties for the first time...
|
|
|
|
assert_eq!(
|
|
|
|
manager.poll(),
|
|
|
|
Ok(PollOutcome::NewDuties(Epoch::new(0), duties))
|
|
|
|
);
|
|
|
|
// Get the same duties again...
|
|
|
|
assert_eq!(manager.poll(), Ok(PollOutcome::NoChange(Epoch::new(0))));
|
|
|
|
|
|
|
|
// Return new duties.
|
|
|
|
let duties = EpochDuties {
|
|
|
|
validator_index: 0,
|
|
|
|
block_production_slot: Some(Slot::new(11)),
|
|
|
|
};
|
|
|
|
beacon_node.set_next_shuffling_result(Ok(Some(duties)));
|
|
|
|
assert_eq!(
|
|
|
|
manager.poll(),
|
|
|
|
Ok(PollOutcome::DutiesChanged(Epoch::new(0), duties))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Return no duties.
|
|
|
|
beacon_node.set_next_shuffling_result(Ok(None));
|
|
|
|
assert_eq!(
|
|
|
|
manager.poll(),
|
|
|
|
Ok(PollOutcome::UnknownValidatorOrEpoch(Epoch::new(0)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 00:33:24 +00:00
|
|
|
*/
|