mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 22:04:44 +00:00
Merges in validator client branch
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use super::EpochDuties;
|
||||
use types::{Epoch, PublicKey};
|
||||
use types::{Epoch, Keypair};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum BeaconNodeDutiesError {
|
||||
@@ -15,6 +15,6 @@ pub trait BeaconNodeDuties: Send + Sync {
|
||||
fn request_duties(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
pubkeys: &[PublicKey],
|
||||
signers: &[Keypair],
|
||||
) -> Result<EpochDuties, BeaconNodeDutiesError>;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use types::{AttestationDuty, Epoch, PublicKey, Slot};
|
||||
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.
|
||||
@@ -71,8 +71,8 @@ impl fmt::Display for EpochDuty {
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a list of public keys (many validators) to an EpochDuty.
|
||||
pub type EpochDuties = HashMap<PublicKey, Option<EpochDuty>>;
|
||||
/// Maps a list of keypairs (many validators) to an EpochDuty.
|
||||
pub type EpochDuties = HashMap<Keypair, Option<EpochDuty>>;
|
||||
|
||||
pub enum EpochDutiesMapError {
|
||||
UnknownEpoch,
|
||||
@@ -113,7 +113,7 @@ impl EpochDutiesMap {
|
||||
pub fn is_work_slot(
|
||||
&self,
|
||||
slot: Slot,
|
||||
pubkey: &PublicKey,
|
||||
signer: &Keypair,
|
||||
) -> Result<Option<WorkInfo>, EpochDutiesMapError> {
|
||||
let epoch = slot.epoch(self.slots_per_epoch);
|
||||
|
||||
@@ -121,7 +121,7 @@ impl EpochDutiesMap {
|
||||
.map
|
||||
.get(&epoch)
|
||||
.ok_or_else(|| EpochDutiesMapError::UnknownEpoch)?;
|
||||
if let Some(epoch_duty) = epoch_duties.get(pubkey) {
|
||||
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));
|
||||
|
||||
@@ -6,21 +6,21 @@ use protos::services_grpc::ValidatorServiceClient;
|
||||
use ssz::ssz_encode;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use types::{Epoch, PublicKey, Slot};
|
||||
use types::{Epoch, Keypair, Slot};
|
||||
|
||||
impl BeaconNodeDuties for ValidatorServiceClient {
|
||||
/// Requests all duties (block signing and committee attesting) from the Beacon Node (BN).
|
||||
fn request_duties(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
pubkeys: &[PublicKey],
|
||||
signers: &[Keypair],
|
||||
) -> Result<EpochDuties, BeaconNodeDutiesError> {
|
||||
// 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(pubkeys.iter().map(|v| ssz_encode(v)).collect());
|
||||
validators.set_public_keys(signers.iter().map(|v| ssz_encode(&v.pk)).collect());
|
||||
req.set_validators(validators);
|
||||
|
||||
// set a timeout for requests
|
||||
@@ -31,11 +31,11 @@ impl BeaconNodeDuties for ValidatorServiceClient {
|
||||
.get_validator_duties(&req)
|
||||
.map_err(|err| BeaconNodeDutiesError::RemoteFailure(format!("{:?}", err)))?;
|
||||
|
||||
let mut epoch_duties: HashMap<PublicKey, Option<EpochDuty>> = HashMap::new();
|
||||
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(pubkeys[index].clone(), None);
|
||||
epoch_duties.insert(signers[index].clone(), None);
|
||||
continue;
|
||||
}
|
||||
// active validator
|
||||
@@ -53,7 +53,7 @@ impl BeaconNodeDuties for ValidatorServiceClient {
|
||||
attestation_shard: active_duty.get_attestation_shard(),
|
||||
committee_index: active_duty.get_committee_index(),
|
||||
};
|
||||
epoch_duties.insert(pubkeys[index].clone(), Some(epoch_duty));
|
||||
epoch_duties.insert(signers[index].clone(), Some(epoch_duty));
|
||||
}
|
||||
Ok(epoch_duties)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use futures::Async;
|
||||
use slog::{debug, error, info};
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
use types::{Epoch, PublicKey, Slot};
|
||||
use types::{Epoch, Keypair, Slot};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum UpdateOutcome {
|
||||
@@ -40,8 +40,9 @@ pub enum Error {
|
||||
/// This keeps track of all validator keys and required voting slots.
|
||||
pub struct DutiesManager<U: BeaconNodeDuties> {
|
||||
pub duties_map: RwLock<EpochDutiesMap>,
|
||||
/// A list of all public keys known to the validator service.
|
||||
pub pubkeys: Vec<PublicKey>,
|
||||
/// A list of all signer objects known to the validator service.
|
||||
// TODO: Generalise the signers, so that they're not just keypairs
|
||||
pub signers: Arc<Vec<Keypair>>,
|
||||
pub beacon_node: Arc<U>,
|
||||
}
|
||||
|
||||
@@ -50,7 +51,7 @@ impl<U: BeaconNodeDuties> DutiesManager<U> {
|
||||
///
|
||||
/// be a wall-clock (e.g., system time, remote server time, etc.).
|
||||
fn update(&self, epoch: Epoch) -> Result<UpdateOutcome, Error> {
|
||||
let duties = self.beacon_node.request_duties(epoch, &self.pubkeys)?;
|
||||
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) {
|
||||
@@ -91,17 +92,18 @@ impl<U: BeaconNodeDuties> DutiesManager<U> {
|
||||
|
||||
/// 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<(PublicKey, WorkInfo)>> {
|
||||
let mut current_work: Vec<(PublicKey, WorkInfo)> = Vec::new();
|
||||
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_pk in &self.pubkeys {
|
||||
match duties.is_work_slot(slot, &validator_pk) {
|
||||
Ok(Some(work_type)) => current_work.push((validator_pk.clone(), work_type)),
|
||||
for validator_signer in self.signers.iter() {
|
||||
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
|
||||
Err(_) => {} // Unknown epoch or validator, no work
|
||||
//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() {
|
||||
@@ -136,9 +138,9 @@ impl From<EpochDutiesMapError> for Error {
|
||||
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));
|
||||
info!(log, "Validator: {:?}",pk; "Duty" => format!("{}",display_duty));
|
||||
} else {
|
||||
info!(log, "Validator: {}",pk; "Duty" => "None");
|
||||
info!(log, "Validator: {:?}",pk; "Duty" => "None");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user