Returns attestation duty for validator client processing

This commit is contained in:
Age Manning 2019-03-27 22:41:55 +11:00
parent 75195bbbf4
commit d3af95d1eb
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
3 changed files with 30 additions and 17 deletions

View File

@ -1,12 +1,17 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use types::{Epoch, PublicKey, Slot}; use types::{AttestationDuty, Epoch, PublicKey, Slot};
/// The type of work a validator is required to do in a given 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)] #[derive(Debug, Clone)]
pub struct WorkType { pub struct WorkInfo {
/// Validator needs to produce a block.
pub produce_block: bool, pub produce_block: bool,
pub produce_attestation: 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. /// The information required for a validator to propose and attest during some epoch.
@ -23,23 +28,28 @@ pub struct EpochDuty {
} }
impl EpochDuty { impl EpochDuty {
/// Returns `WorkType` if work needs to be done in the supplied `slot` /// Returns `WorkInfo` if work needs to be done in the supplied `slot`
pub fn is_work_slot(&self, slot: Slot) -> Option<WorkType> { pub fn is_work_slot(&self, slot: Slot) -> Option<WorkInfo> {
// if validator is required to produce a slot return true // if validator is required to produce a slot return true
let produce_block = match self.block_production_slot { let produce_block = match self.block_production_slot {
Some(s) if s == slot => true, Some(s) if s == slot => true,
_ => false, _ => false,
}; };
let mut produce_attestation = false; // if the validator is required to attest to a shard, create the data
let mut attestation_duty = None;
if self.attestation_slot == slot { if self.attestation_slot == slot {
produce_attestation = true; attestation_duty = Some(AttestationDuty {
slot,
shard: self.attestation_shard,
committee_index: self.committee_index as usize,
});
} }
if produce_block | produce_attestation { if produce_block | attestation_duty.is_some() {
return Some(WorkType { return Some(WorkInfo {
produce_block, produce_block,
produce_attestation, attestation_duty,
}); });
} }
None None
@ -89,7 +99,7 @@ impl EpochDutiesMap {
&self, &self,
slot: Slot, slot: Slot,
pubkey: &PublicKey, pubkey: &PublicKey,
) -> Result<Option<WorkType>, EpochDutiesMapError> { ) -> Result<Option<WorkInfo>, EpochDutiesMapError> {
let epoch = slot.epoch(self.slots_per_epoch); let epoch = slot.epoch(self.slots_per_epoch);
let epoch_duties = self let epoch_duties = self

View File

@ -6,7 +6,7 @@ mod grpc;
mod traits; mod traits;
use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; use self::epoch_duties::{EpochDuties, EpochDutiesMapError};
pub use self::epoch_duties::{EpochDutiesMap, WorkType}; pub use self::epoch_duties::{EpochDutiesMap, WorkInfo};
use self::traits::{BeaconNode, BeaconNodeError}; use self::traits::{BeaconNode, BeaconNodeError};
use futures::Async; use futures::Async;
use slog::{debug, error, info}; use slog::{debug, error, info};
@ -85,10 +85,10 @@ impl<U: BeaconNode> DutiesManager<U> {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
/// Returns a list of (Public, WorkType) indicating all the validators that have work to perform /// Returns a list of (Public, WorkInfo) indicating all the validators that have work to perform
/// this slot. /// this slot.
pub fn get_current_work(&self, slot: Slot) -> Option<Vec<(PublicKey, WorkType)>> { pub fn get_current_work(&self, slot: Slot) -> Option<Vec<(PublicKey, WorkInfo)>> {
let mut current_work: Vec<(PublicKey, WorkType)> = Vec::new(); let mut current_work: Vec<(PublicKey, WorkInfo)> = Vec::new();
// if the map is poisoned, return None // if the map is poisoned, return None
let duties = self.duties_map.read().ok()?; let duties = self.duties_map.read().ok()?;

View File

@ -272,7 +272,10 @@ impl Service {
if work_type.produce_block { if work_type.produce_block {
// TODO: Produce a beacon block in a new thread // TODO: Produce a beacon block in a new thread
} }
if work_type.produce_attestation { 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 //TODO: Produce an attestation in a new thread
} }
} }