lighthouse/beacon_node/beacon_chain/tests/utils/validator.rs

76 lines
2.2 KiB
Rust
Raw Normal View History

2019-01-24 06:05:48 +00:00
use super::{DirectBeaconNode, DirectDuties};
use beacon_chain::BeaconChain;
#[cfg(test)]
2019-01-25 00:30:06 +00:00
use block_producer::{test_utils::TestSigner, BlockProducer, Error as PollError};
2019-01-24 06:05:48 +00:00
use db::MemoryDB;
use slot_clock::TestingSlotClock;
2019-01-25 02:52:21 +00:00
use std::sync::Arc;
use types::{ChainSpec, Keypair};
2019-01-24 06:05:48 +00:00
2019-01-25 00:30:06 +00:00
pub use block_producer::PollOutcome;
#[derive(Debug, PartialEq)]
pub enum ProduceError {
DidNotProduce(PollOutcome),
PollError(PollError),
}
pub struct TestValidator {
2019-01-24 06:05:48 +00:00
block_producer: BlockProducer<
TestingSlotClock,
2019-01-25 00:30:06 +00:00
DirectBeaconNode<MemoryDB, TestingSlotClock>,
DirectDuties<MemoryDB, TestingSlotClock>,
2019-01-24 06:05:48 +00:00
TestSigner,
>,
spec: Arc<ChainSpec>,
2019-01-25 00:30:06 +00:00
epoch_map: Arc<DirectDuties<MemoryDB, TestingSlotClock>>,
2019-01-24 06:05:48 +00:00
keypair: Keypair,
2019-01-25 00:30:06 +00:00
beacon_node: Arc<DirectBeaconNode<MemoryDB, TestingSlotClock>>,
slot_clock: Arc<TestingSlotClock>,
2019-01-24 06:05:48 +00:00
signer: Arc<TestSigner>,
}
2019-01-25 00:30:06 +00:00
impl TestValidator {
pub fn new(
keypair: Keypair,
beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>,
) -> Self {
2019-01-24 06:05:48 +00:00
let spec = Arc::new(ChainSpec::foundation());
2019-01-25 00:30:06 +00:00
let slot_clock = Arc::new(TestingSlotClock::new(0));
2019-01-24 06:05:48 +00:00
let signer = Arc::new(TestSigner::new(keypair.clone()));
2019-01-25 00:30:06 +00:00
let beacon_node = Arc::new(DirectBeaconNode::new(beacon_chain.clone()));
let epoch_map = Arc::new(DirectDuties::new(keypair.pk.clone(), beacon_chain.clone()));
2019-01-24 06:05:48 +00:00
let block_producer = BlockProducer::new(
spec.clone(),
keypair.pk.clone(),
epoch_map.clone(),
slot_clock.clone(),
beacon_node.clone(),
signer.clone(),
);
Self {
block_producer,
spec,
epoch_map,
keypair,
beacon_node,
slot_clock,
signer,
}
}
2019-01-25 00:30:06 +00:00
pub fn produce_block(&mut self) -> Result<PollOutcome, ProduceError> {
match self.block_producer.poll() {
Ok(PollOutcome::BlockProduced(slot)) => Ok(PollOutcome::BlockProduced(slot)),
Ok(outcome) => Err(ProduceError::DidNotProduce(outcome)),
Err(error) => Err(ProduceError::PollError(error)),
2019-01-24 06:05:48 +00:00
}
}
2019-01-25 00:30:06 +00:00
pub fn set_slot(&mut self, slot: u64) {
self.slot_clock.set_slot(slot)
}
2019-01-24 06:05:48 +00:00
}