mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-18 21:38:31 +00:00
Replace EpochDutiesMap with trait in BlockProducer
This commit is contained in:
@@ -63,7 +63,7 @@ impl BeaconNode for BeaconBlockServiceClient {
|
||||
let mut grpc_block = GrpcBeaconBlock::new();
|
||||
grpc_block.set_slot(block.slot);
|
||||
grpc_block.set_block_root(vec![0]);
|
||||
grpc_block.set_randao_reveal(block.randao_reveal.to_vec());
|
||||
grpc_block.set_randao_reveal(ssz_encode(&block.randao_reveal));
|
||||
grpc_block.set_signature(ssz_encode(&block.signature));
|
||||
|
||||
req.set_block(grpc_block);
|
||||
|
||||
@@ -2,10 +2,9 @@ mod grpc;
|
||||
mod service;
|
||||
#[cfg(test)]
|
||||
mod test_node;
|
||||
mod traits;
|
||||
pub mod traits;
|
||||
|
||||
use self::traits::{BeaconNode, BeaconNodeError};
|
||||
use super::EpochDutiesMap;
|
||||
use self::traits::{BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError};
|
||||
use slot_clock::SlotClock;
|
||||
use spec::ChainSpec;
|
||||
use std::sync::{Arc, RwLock};
|
||||
@@ -45,19 +44,19 @@ pub enum Error {
|
||||
/// Ensures that messages are not slashable.
|
||||
///
|
||||
/// Relies upon an external service to keep the `EpochDutiesMap` updated.
|
||||
pub struct BlockProducer<T: SlotClock, U: BeaconNode> {
|
||||
pub struct BlockProducer<T: SlotClock, U: BeaconNode, V: DutiesReader> {
|
||||
pub last_processed_slot: u64,
|
||||
spec: Arc<ChainSpec>,
|
||||
epoch_map: Arc<RwLock<EpochDutiesMap>>,
|
||||
epoch_map: Arc<V>,
|
||||
slot_clock: Arc<RwLock<T>>,
|
||||
beacon_node: Arc<U>,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode> BlockProducer<T, U> {
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader> BlockProducer<T, U, V> {
|
||||
/// Returns a new instance where `last_processed_slot == 0`.
|
||||
pub fn new(
|
||||
spec: Arc<ChainSpec>,
|
||||
epoch_map: Arc<RwLock<EpochDutiesMap>>,
|
||||
epoch_map: Arc<V>,
|
||||
slot_clock: Arc<RwLock<T>>,
|
||||
beacon_node: Arc<U>,
|
||||
) -> Self {
|
||||
@@ -71,7 +70,7 @@ impl<T: SlotClock, U: BeaconNode> BlockProducer<T, U> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode> BlockProducer<T, U> {
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader> BlockProducer<T, U, V> {
|
||||
/// "Poll" to see if the validator is required to take any action.
|
||||
///
|
||||
/// The slot clock will be read and any new actions undertaken.
|
||||
@@ -90,13 +89,14 @@ impl<T: SlotClock, U: BeaconNode> BlockProducer<T, U> {
|
||||
|
||||
// If this is a new slot.
|
||||
if slot > self.last_processed_slot {
|
||||
let is_block_production_slot = {
|
||||
let epoch_map = self.epoch_map.read().map_err(|_| Error::EpochMapPoisoned)?;
|
||||
match epoch_map.get(&epoch) {
|
||||
None => return Ok(PollOutcome::ProducerDutiesUnknown(slot)),
|
||||
Some(duties) => duties.is_block_production_slot(slot),
|
||||
}
|
||||
};
|
||||
let is_block_production_slot =
|
||||
match self.epoch_map.is_block_production_slot(epoch, slot) {
|
||||
Ok(result) => result,
|
||||
Err(DutiesReaderError::UnknownEpoch) => {
|
||||
return Ok(PollOutcome::ProducerDutiesUnknown(slot))
|
||||
}
|
||||
Err(DutiesReaderError::Poisoned) => return Err(Error::EpochMapPoisoned),
|
||||
};
|
||||
|
||||
if is_block_production_slot {
|
||||
self.last_processed_slot = slot;
|
||||
@@ -178,6 +178,7 @@ mod tests {
|
||||
use super::test_node::TestBeaconNode;
|
||||
use super::*;
|
||||
use crate::duties::EpochDuties;
|
||||
use crate::duties::EpochDutiesMap;
|
||||
use slot_clock::TestingSlotClock;
|
||||
use types::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
|
||||
@@ -191,7 +192,7 @@ mod tests {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
|
||||
let spec = Arc::new(ChainSpec::foundation());
|
||||
let epoch_map = Arc::new(RwLock::new(EpochDutiesMap::new()));
|
||||
let epoch_map = Arc::new(EpochDutiesMap::new());
|
||||
let slot_clock = Arc::new(RwLock::new(TestingSlotClock::new(0)));
|
||||
let beacon_node = Arc::new(TestBeaconNode::default());
|
||||
|
||||
@@ -213,7 +214,7 @@ mod tests {
|
||||
..std::default::Default::default()
|
||||
};
|
||||
let produce_epoch = produce_slot / spec.epoch_length;
|
||||
epoch_map.write().unwrap().insert(produce_epoch, duties);
|
||||
epoch_map.insert(produce_epoch, duties);
|
||||
|
||||
// One slot before production slot...
|
||||
slot_clock.write().unwrap().set_slot(produce_slot - 1);
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
use super::traits::BeaconNode;
|
||||
use super::traits::{BeaconNode, DutiesReader};
|
||||
use super::{BlockProducer, PollOutcome as BlockProducerPollOutcome, SlotClock};
|
||||
use slog::{error, info, warn, Logger};
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct BlockProducerService<T: SlotClock, U: BeaconNode> {
|
||||
pub block_producer: BlockProducer<T, U>,
|
||||
pub struct BlockProducerService<T: SlotClock, U: BeaconNode, V: DutiesReader> {
|
||||
pub block_producer: BlockProducer<T, U, V>,
|
||||
pub poll_interval_millis: u64,
|
||||
pub log: Logger,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode> BlockProducerService<T, U> {
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader> BlockProducerService<T, U, V> {
|
||||
/// Run a loop which polls the block producer each `poll_interval_millis` millseconds.
|
||||
///
|
||||
/// Logs the results of the polls.
|
||||
|
||||
@@ -17,3 +17,12 @@ pub trait BeaconNode: Send + Sync {
|
||||
/// Returns `true` if the publish was sucessful.
|
||||
fn publish_beacon_block(&self, block: BeaconBlock) -> Result<bool, BeaconNodeError>;
|
||||
}
|
||||
|
||||
pub enum DutiesReaderError {
|
||||
UnknownEpoch,
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
pub trait DutiesReader: Send + Sync {
|
||||
fn is_block_production_slot(&self, epoch: u64, slot: u64) -> Result<bool, DutiesReaderError>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user