mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 13:54:44 +00:00
Add progress on test rig
This commit is contained in:
@@ -7,4 +7,5 @@ edition = "2018"
|
||||
[dependencies]
|
||||
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
||||
spec = { path = "../../eth2/spec" }
|
||||
ssz = { path = "../../eth2/utils/ssz" }
|
||||
types = { path = "../../eth2/types" }
|
||||
|
||||
@@ -3,8 +3,9 @@ mod traits;
|
||||
|
||||
use slot_clock::SlotClock;
|
||||
use spec::ChainSpec;
|
||||
use ssz::ssz_encode;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use types::{BeaconBlock, Hash256, ProposalSignedData};
|
||||
use types::{BeaconBlock, Hash256, ProposalSignedData, PublicKey};
|
||||
|
||||
pub use self::traits::{BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, Signer};
|
||||
|
||||
@@ -24,6 +25,8 @@ pub enum PollOutcome {
|
||||
BeaconNodeUnableToProduceBlock(u64),
|
||||
/// The signer failed to sign the message.
|
||||
SignerRejection(u64),
|
||||
/// The public key for this validator is not an active validator.
|
||||
ValidatorIsUnknown(u64),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -44,6 +47,7 @@ pub enum Error {
|
||||
/// Relies upon an external service to keep the `EpochDutiesMap` updated.
|
||||
pub struct BlockProducer<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> {
|
||||
pub last_processed_slot: u64,
|
||||
pubkey: PublicKey,
|
||||
spec: Arc<ChainSpec>,
|
||||
epoch_map: Arc<V>,
|
||||
slot_clock: Arc<RwLock<T>>,
|
||||
@@ -55,6 +59,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
/// Returns a new instance where `last_processed_slot == 0`.
|
||||
pub fn new(
|
||||
spec: Arc<ChainSpec>,
|
||||
pubkey: PublicKey,
|
||||
epoch_map: Arc<V>,
|
||||
slot_clock: Arc<RwLock<T>>,
|
||||
beacon_node: Arc<U>,
|
||||
@@ -62,6 +67,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
) -> Self {
|
||||
Self {
|
||||
last_processed_slot: 0,
|
||||
pubkey,
|
||||
spec,
|
||||
epoch_map,
|
||||
slot_clock,
|
||||
@@ -96,6 +102,9 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
Err(DutiesReaderError::UnknownEpoch) => {
|
||||
return Ok(PollOutcome::ProducerDutiesUnknown(slot))
|
||||
}
|
||||
Err(DutiesReaderError::UnknownValidator) => {
|
||||
return Ok(PollOutcome::ValidatorIsUnknown(slot))
|
||||
}
|
||||
Err(DutiesReaderError::Poisoned) => return Err(Error::EpochMapPoisoned),
|
||||
};
|
||||
|
||||
@@ -122,7 +131,20 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
/// The slash-protection code is not yet implemented. There is zero protection against
|
||||
/// slashing.
|
||||
fn produce_block(&mut self, slot: u64) -> Result<PollOutcome, Error> {
|
||||
if let Some(block) = self.beacon_node.produce_beacon_block(slot)? {
|
||||
let randao_reveal = {
|
||||
let producer_nonce = self.beacon_node.proposer_nonce(&self.pubkey)?;
|
||||
// TODO: add domain, etc to this message.
|
||||
let message = ssz_encode(&producer_nonce);
|
||||
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)?
|
||||
{
|
||||
if self.safe_to_produce(&block) {
|
||||
if let Some(block) = self.sign_block(block) {
|
||||
self.beacon_node.publish_beacon_block(block)?;
|
||||
|
||||
@@ -1,20 +1,30 @@
|
||||
use crate::traits::{BeaconNode, BeaconNodeError};
|
||||
use std::sync::RwLock;
|
||||
use types::BeaconBlock;
|
||||
use types::{BeaconBlock, PublicKey, Signature};
|
||||
|
||||
type NonceResult = Result<u64, BeaconNodeError>;
|
||||
type ProduceResult = Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
type PublishResult = Result<bool, BeaconNodeError>;
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
#[derive(Default)]
|
||||
pub struct TestBeaconNode {
|
||||
pub produce_input: RwLock<Option<u64>>,
|
||||
pub nonce_input: RwLock<Option<PublicKey>>,
|
||||
pub nonce_result: RwLock<Option<NonceResult>>,
|
||||
|
||||
pub produce_input: RwLock<Option<(u64, Signature)>>,
|
||||
pub produce_result: RwLock<Option<ProduceResult>>,
|
||||
|
||||
pub publish_input: RwLock<Option<BeaconBlock>>,
|
||||
pub publish_result: RwLock<Option<PublishResult>>,
|
||||
}
|
||||
|
||||
impl TestBeaconNode {
|
||||
/// Set the result to be returned when `produce_beacon_block` is called.
|
||||
pub fn set_next_nonce_result(&self, result: NonceResult) {
|
||||
*self.nonce_result.write().unwrap() = Some(result);
|
||||
}
|
||||
|
||||
/// Set the result to be returned when `produce_beacon_block` is called.
|
||||
pub fn set_next_produce_result(&self, result: ProduceResult) {
|
||||
*self.produce_result.write().unwrap() = Some(result);
|
||||
@@ -27,9 +37,17 @@ impl TestBeaconNode {
|
||||
}
|
||||
|
||||
impl BeaconNode for TestBeaconNode {
|
||||
fn proposer_nonce(&self, pubkey: &PublicKey) -> NonceResult {
|
||||
*self.nonce_input.write().unwrap() = Some(pubkey.clone());
|
||||
match *self.nonce_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("TestBeaconNode: nonce_result == None"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value specified by the `set_next_produce_result`.
|
||||
fn produce_beacon_block(&self, slot: u64) -> ProduceResult {
|
||||
*self.produce_input.write().unwrap() = Some(slot);
|
||||
fn produce_beacon_block(&self, slot: u64, randao_reveal: &Signature) -> ProduceResult {
|
||||
*self.produce_input.write().unwrap() = Some((slot, randao_reveal.clone()));
|
||||
match *self.produce_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("TestBeaconNode: produce_result == None"),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use types::{BeaconBlock, Signature};
|
||||
use types::{BeaconBlock, PublicKey, Signature};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum BeaconNodeError {
|
||||
@@ -8,10 +8,18 @@ pub enum BeaconNodeError {
|
||||
|
||||
/// Defines the methods required to produce and publish blocks on a Beacon Node.
|
||||
pub trait BeaconNode: Send + Sync {
|
||||
/// Requests the proposer nonce (presently named `proposer_slots`).
|
||||
fn proposer_nonce(&self, pubkey: &PublicKey) -> Result<u64, BeaconNodeError>;
|
||||
|
||||
/// Request that the node produces a block.
|
||||
///
|
||||
/// Returns Ok(None) if the Beacon Node is unable to produce at the given slot.
|
||||
fn produce_beacon_block(&self, slot: u64) -> Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
fn produce_beacon_block(
|
||||
&self,
|
||||
slot: u64,
|
||||
randao_reveal: &Signature,
|
||||
) -> Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
|
||||
/// Request that the node publishes a block.
|
||||
///
|
||||
/// Returns `true` if the publish was sucessful.
|
||||
@@ -20,6 +28,7 @@ pub trait BeaconNode: Send + Sync {
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum DutiesReaderError {
|
||||
UnknownValidator,
|
||||
UnknownEpoch,
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ mod foundation;
|
||||
use bls::Signature;
|
||||
use types::{Address, Eth1Data, Hash256, Validator};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub struct ChainSpec {
|
||||
/*
|
||||
* Misc
|
||||
|
||||
Reference in New Issue
Block a user