2019-01-24 00:50:34 +00:00
|
|
|
pub mod test_utils;
|
2019-01-23 10:21:18 +00:00
|
|
|
mod traits;
|
|
|
|
|
|
|
|
use slot_clock::SlotClock;
|
2019-01-24 06:05:48 +00:00
|
|
|
use ssz::ssz_encode;
|
2019-01-25 02:02:59 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-25 02:52:21 +00:00
|
|
|
use types::{BeaconBlock, ChainSpec, Hash256, ProposalSignedData, PublicKey};
|
2019-01-23 10:21:18 +00:00
|
|
|
|
2019-01-25 00:29:41 +00:00
|
|
|
pub use self::traits::{
|
|
|
|
BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, PublishOutcome, Signer,
|
|
|
|
};
|
2019-01-23 10:21:18 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum PollOutcome {
|
|
|
|
/// A new block was produced.
|
|
|
|
BlockProduced(u64),
|
|
|
|
/// A block was not produced as it would have been slashable.
|
|
|
|
SlashableBlockNotProduced(u64),
|
|
|
|
/// The validator duties did not require a block to be produced.
|
|
|
|
BlockProductionNotRequired(u64),
|
|
|
|
/// The duties for the present epoch were not found.
|
|
|
|
ProducerDutiesUnknown(u64),
|
|
|
|
/// The slot has already been processed, execution was skipped.
|
|
|
|
SlotAlreadyProcessed(u64),
|
|
|
|
/// The Beacon Node was unable to produce a block at that slot.
|
|
|
|
BeaconNodeUnableToProduceBlock(u64),
|
2019-01-24 00:50:34 +00:00
|
|
|
/// The signer failed to sign the message.
|
|
|
|
SignerRejection(u64),
|
2019-01-24 06:05:48 +00:00
|
|
|
/// The public key for this validator is not an active validator.
|
|
|
|
ValidatorIsUnknown(u64),
|
2019-01-23 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
SlotClockError,
|
|
|
|
SlotUnknowable,
|
|
|
|
EpochMapPoisoned,
|
|
|
|
SlotClockPoisoned,
|
|
|
|
EpochLengthIsZero,
|
|
|
|
BeaconNodeError(BeaconNodeError),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A polling state machine which performs block production duties, based upon some epoch duties
|
|
|
|
/// (`EpochDutiesMap`) and a concept of time (`SlotClock`).
|
|
|
|
///
|
|
|
|
/// Ensures that messages are not slashable.
|
|
|
|
///
|
|
|
|
/// Relies upon an external service to keep the `EpochDutiesMap` updated.
|
2019-01-23 22:20:25 +00:00
|
|
|
pub struct BlockProducer<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> {
|
2019-01-25 00:29:41 +00:00
|
|
|
pub last_processed_slot: Option<u64>,
|
2019-01-24 06:05:48 +00:00
|
|
|
pubkey: PublicKey,
|
2019-01-23 10:21:18 +00:00
|
|
|
spec: Arc<ChainSpec>,
|
|
|
|
epoch_map: Arc<V>,
|
2019-01-25 00:29:41 +00:00
|
|
|
slot_clock: Arc<T>,
|
2019-01-23 10:21:18 +00:00
|
|
|
beacon_node: Arc<U>,
|
2019-01-23 22:20:25 +00:00
|
|
|
signer: Arc<W>,
|
2019-01-23 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 22:20:25 +00:00
|
|
|
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U, V, W> {
|
2019-01-23 10:21:18 +00:00
|
|
|
/// Returns a new instance where `last_processed_slot == 0`.
|
|
|
|
pub fn new(
|
|
|
|
spec: Arc<ChainSpec>,
|
2019-01-24 06:05:48 +00:00
|
|
|
pubkey: PublicKey,
|
2019-01-23 10:21:18 +00:00
|
|
|
epoch_map: Arc<V>,
|
2019-01-25 00:29:41 +00:00
|
|
|
slot_clock: Arc<T>,
|
2019-01-23 10:21:18 +00:00
|
|
|
beacon_node: Arc<U>,
|
2019-01-23 22:20:25 +00:00
|
|
|
signer: Arc<W>,
|
2019-01-23 10:21:18 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2019-01-25 00:29:41 +00:00
|
|
|
last_processed_slot: None,
|
2019-01-24 06:05:48 +00:00
|
|
|
pubkey,
|
2019-01-23 10:21:18 +00:00
|
|
|
spec,
|
|
|
|
epoch_map,
|
|
|
|
slot_clock,
|
|
|
|
beacon_node,
|
2019-01-23 22:20:25 +00:00
|
|
|
signer,
|
2019-01-23 10:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:20:25 +00:00
|
|
|
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U, V, W> {
|
2019-01-23 10:21:18 +00:00
|
|
|
/// "Poll" to see if the validator is required to take any action.
|
|
|
|
///
|
|
|
|
/// The slot clock will be read and any new actions undertaken.
|
|
|
|
pub fn poll(&mut self) -> Result<PollOutcome, Error> {
|
|
|
|
let slot = self
|
|
|
|
.slot_clock
|
|
|
|
.present_slot()
|
|
|
|
.map_err(|_| Error::SlotClockError)?
|
|
|
|
.ok_or(Error::SlotUnknowable)?;
|
|
|
|
|
|
|
|
// If this is a new slot.
|
2019-01-25 00:29:41 +00:00
|
|
|
if !self.is_processed_slot(slot) {
|
|
|
|
let is_block_production_slot = match self.epoch_map.is_block_production_slot(slot) {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(DutiesReaderError::UnknownEpoch) => {
|
2019-01-25 06:23:54 +00:00
|
|
|
return Ok(PollOutcome::ProducerDutiesUnknown(slot));
|
2019-01-25 00:29:41 +00:00
|
|
|
}
|
|
|
|
Err(DutiesReaderError::UnknownValidator) => {
|
2019-01-25 06:23:54 +00:00
|
|
|
return Ok(PollOutcome::ValidatorIsUnknown(slot));
|
2019-01-25 00:29:41 +00:00
|
|
|
}
|
2019-01-25 02:02:59 +00:00
|
|
|
Err(DutiesReaderError::EpochLengthIsZero) => return Err(Error::EpochLengthIsZero),
|
2019-01-25 00:29:41 +00:00
|
|
|
Err(DutiesReaderError::Poisoned) => return Err(Error::EpochMapPoisoned),
|
|
|
|
};
|
2019-01-23 10:21:18 +00:00
|
|
|
|
|
|
|
if is_block_production_slot {
|
2019-01-25 00:29:41 +00:00
|
|
|
self.last_processed_slot = Some(slot);
|
2019-01-23 10:21:18 +00:00
|
|
|
|
|
|
|
self.produce_block(slot)
|
|
|
|
} else {
|
|
|
|
Ok(PollOutcome::BlockProductionNotRequired(slot))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(PollOutcome::SlotAlreadyProcessed(slot))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 00:29:41 +00:00
|
|
|
fn is_processed_slot(&self, slot: u64) -> bool {
|
|
|
|
match self.last_processed_slot {
|
2019-01-25 02:02:59 +00:00
|
|
|
Some(processed_slot) if processed_slot >= slot => true,
|
2019-01-25 00:29:41 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:21:18 +00:00
|
|
|
/// Produce a block at some slot.
|
|
|
|
///
|
|
|
|
/// Assumes that a block is required at this slot (does not check the duties).
|
|
|
|
///
|
|
|
|
/// Ensures the message is not slashable.
|
|
|
|
///
|
|
|
|
/// !!! UNSAFE !!!
|
|
|
|
///
|
|
|
|
/// The slash-protection code is not yet implemented. There is zero protection against
|
|
|
|
/// slashing.
|
|
|
|
fn produce_block(&mut self, slot: u64) -> Result<PollOutcome, Error> {
|
2019-01-24 06:05:48 +00:00
|
|
|
let randao_reveal = {
|
|
|
|
let producer_nonce = self.beacon_node.proposer_nonce(&self.pubkey)?;
|
2019-01-25 05:54:19 +00:00
|
|
|
|
2019-01-24 06:05:48 +00:00
|
|
|
// TODO: add domain, etc to this message.
|
|
|
|
let message = ssz_encode(&producer_nonce);
|
2019-01-25 05:54:19 +00:00
|
|
|
|
2019-01-24 06:05:48 +00:00
|
|
|
match self.signer.bls_sign(&message) {
|
|
|
|
None => return Ok(PollOutcome::SignerRejection(slot)),
|
|
|
|
Some(signature) => signature,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(block) = self
|
|
|
|
.beacon_node
|
|
|
|
.produce_beacon_block(slot, &randao_reveal)?
|
|
|
|
{
|
2019-01-23 10:21:18 +00:00
|
|
|
if self.safe_to_produce(&block) {
|
2019-01-24 00:50:34 +00:00
|
|
|
if let Some(block) = self.sign_block(block) {
|
|
|
|
self.beacon_node.publish_beacon_block(block)?;
|
|
|
|
Ok(PollOutcome::BlockProduced(slot))
|
|
|
|
} else {
|
|
|
|
Ok(PollOutcome::SignerRejection(slot))
|
|
|
|
}
|
2019-01-23 10:21:18 +00:00
|
|
|
} else {
|
|
|
|
Ok(PollOutcome::SlashableBlockNotProduced(slot))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(PollOutcome::BeaconNodeUnableToProduceBlock(slot))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes a block, returning that block signed by the validators private key.
|
|
|
|
///
|
|
|
|
/// Important: this function will not check to ensure the block is not slashable. This must be
|
|
|
|
/// done upstream.
|
2019-01-24 00:50:34 +00:00
|
|
|
fn sign_block(&mut self, mut block: BeaconBlock) -> Option<BeaconBlock> {
|
2019-01-23 10:21:18 +00:00
|
|
|
self.store_produce(&block);
|
2019-01-24 00:50:34 +00:00
|
|
|
|
2019-01-25 05:47:24 +00:00
|
|
|
match self.signer.bls_sign(&block.proposal_root(&self.spec)[..]) {
|
2019-01-24 00:50:34 +00:00
|
|
|
None => None,
|
|
|
|
Some(signature) => {
|
|
|
|
block.signature = signature;
|
|
|
|
Some(block)
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if signing a block is safe (non-slashable).
|
|
|
|
///
|
|
|
|
/// !!! UNSAFE !!!
|
|
|
|
///
|
|
|
|
/// Important: this function is presently stubbed-out. It provides ZERO SAFETY.
|
|
|
|
fn safe_to_produce(&self, _block: &BeaconBlock) -> bool {
|
|
|
|
// TODO: ensure the producer doesn't produce slashable blocks.
|
|
|
|
// https://github.com/sigp/lighthouse/issues/160
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Record that a block was produced so that slashable votes may not be made in the future.
|
|
|
|
///
|
|
|
|
/// !!! UNSAFE !!!
|
|
|
|
///
|
|
|
|
/// Important: this function is presently stubbed-out. It provides ZERO SAFETY.
|
|
|
|
fn store_produce(&mut self, _block: &BeaconBlock) {
|
|
|
|
// TODO: record this block production to prevent future slashings.
|
|
|
|
// https://github.com/sigp/lighthouse/issues/160
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BeaconNodeError> for Error {
|
|
|
|
fn from(e: BeaconNodeError) -> Error {
|
|
|
|
Error::BeaconNodeError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-01-24 00:50:34 +00:00
|
|
|
use super::test_utils::{TestBeaconNode, TestEpochMap, TestSigner};
|
2019-01-23 10:21:18 +00:00
|
|
|
use super::*;
|
|
|
|
use slot_clock::TestingSlotClock;
|
2019-01-23 22:20:25 +00:00
|
|
|
use types::{
|
|
|
|
test_utils::{SeedableRng, TestRandom, XorShiftRng},
|
2019-01-24 00:50:34 +00:00
|
|
|
Keypair,
|
2019-01-23 22:20:25 +00:00
|
|
|
};
|
2019-01-23 10:21:18 +00:00
|
|
|
|
|
|
|
// TODO: implement more thorough testing.
|
|
|
|
// https://github.com/sigp/lighthouse/issues/160
|
|
|
|
//
|
|
|
|
// These tests should serve as a good example for future tests.
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn polling() {
|
|
|
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
|
|
|
|
|
|
|
let spec = Arc::new(ChainSpec::foundation());
|
2019-01-25 02:02:59 +00:00
|
|
|
let slot_clock = Arc::new(TestingSlotClock::new(0));
|
2019-01-23 10:21:18 +00:00
|
|
|
let beacon_node = Arc::new(TestBeaconNode::default());
|
2019-01-24 00:50:34 +00:00
|
|
|
let signer = Arc::new(TestSigner::new(Keypair::random()));
|
2019-01-23 10:21:18 +00:00
|
|
|
|
2019-01-25 02:02:59 +00:00
|
|
|
let mut epoch_map = TestEpochMap::new(spec.epoch_length);
|
2019-01-23 10:21:18 +00:00
|
|
|
let produce_slot = 100;
|
|
|
|
let produce_epoch = produce_slot / spec.epoch_length;
|
2019-01-25 02:02:59 +00:00
|
|
|
epoch_map.map.insert(produce_epoch, produce_slot);
|
2019-01-23 10:21:18 +00:00
|
|
|
let epoch_map = Arc::new(epoch_map);
|
2019-01-25 02:02:59 +00:00
|
|
|
let keypair = Keypair::random();
|
2019-01-23 10:21:18 +00:00
|
|
|
|
|
|
|
let mut block_producer = BlockProducer::new(
|
|
|
|
spec.clone(),
|
2019-01-25 02:02:59 +00:00
|
|
|
keypair.pk.clone(),
|
2019-01-23 10:21:18 +00:00
|
|
|
epoch_map.clone(),
|
|
|
|
slot_clock.clone(),
|
|
|
|
beacon_node.clone(),
|
2019-01-23 22:20:25 +00:00
|
|
|
signer.clone(),
|
2019-01-23 10:21:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Configure responses from the BeaconNode.
|
|
|
|
beacon_node.set_next_produce_result(Ok(Some(BeaconBlock::random_for_test(&mut rng))));
|
2019-01-25 02:02:59 +00:00
|
|
|
beacon_node.set_next_publish_result(Ok(PublishOutcome::ValidBlock));
|
|
|
|
beacon_node.set_next_nonce_result(Ok(0));
|
2019-01-23 10:21:18 +00:00
|
|
|
|
|
|
|
// One slot before production slot...
|
2019-01-25 02:02:59 +00:00
|
|
|
slot_clock.set_slot(produce_slot - 1);
|
2019-01-23 10:21:18 +00:00
|
|
|
assert_eq!(
|
|
|
|
block_producer.poll(),
|
|
|
|
Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1))
|
|
|
|
);
|
|
|
|
|
|
|
|
// On the produce slot...
|
2019-01-25 02:02:59 +00:00
|
|
|
slot_clock.set_slot(produce_slot);
|
2019-01-23 10:21:18 +00:00
|
|
|
assert_eq!(
|
|
|
|
block_producer.poll(),
|
|
|
|
Ok(PollOutcome::BlockProduced(produce_slot))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Trying the same produce slot again...
|
2019-01-25 02:02:59 +00:00
|
|
|
slot_clock.set_slot(produce_slot);
|
2019-01-23 10:21:18 +00:00
|
|
|
assert_eq!(
|
|
|
|
block_producer.poll(),
|
|
|
|
Ok(PollOutcome::SlotAlreadyProcessed(produce_slot))
|
|
|
|
);
|
|
|
|
|
|
|
|
// One slot after the produce slot...
|
2019-01-25 02:02:59 +00:00
|
|
|
slot_clock.set_slot(produce_slot + 1);
|
2019-01-23 10:21:18 +00:00
|
|
|
assert_eq!(
|
|
|
|
block_producer.poll(),
|
|
|
|
Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1))
|
|
|
|
);
|
|
|
|
|
|
|
|
// In an epoch without known duties...
|
|
|
|
let slot = (produce_epoch + 1) * spec.epoch_length;
|
2019-01-25 02:02:59 +00:00
|
|
|
slot_clock.set_slot(slot);
|
2019-01-23 10:21:18 +00:00
|
|
|
assert_eq!(
|
|
|
|
block_producer.poll(),
|
|
|
|
Ok(PollOutcome::ProducerDutiesUnknown(slot))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|