Update beacon_chain as per test bugs

This commit is contained in:
Paul Hauner
2019-01-25 11:30:06 +11:00
parent 643fc20063
commit f4f5b3a13c
9 changed files with 188 additions and 86 deletions

View File

@@ -1,22 +1,27 @@
use beacon_chain::block_processing::{Error as ProcessingError, Outcome as ProcessingOutcome};
use beacon_chain::{block_production::Error as BlockProductionError, BeaconChain};
use block_producer::{BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError};
use block_producer::{
BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, PublishOutcome,
};
use db::ClientDB;
use slot_clock::SlotClock;
use std::sync::Arc;
use types::{BeaconBlock, PublicKey, Signature};
pub struct DirectBeaconNode<'a, T: ClientDB, U: SlotClock> {
beacon_chain: &'a BeaconChain<T, U>,
pub struct DirectBeaconNode<T: ClientDB, U: SlotClock> {
beacon_chain: Arc<BeaconChain<T, U>>,
}
impl<'a, T: ClientDB, U: SlotClock> DirectBeaconNode<'a, T, U> {
pub fn new(beacon_chain: &'a BeaconChain<T, U>) -> Self {
impl<T: ClientDB, U: SlotClock> DirectBeaconNode<T, U> {
pub fn new(beacon_chain: Arc<BeaconChain<T, U>>) -> Self {
Self { beacon_chain }
}
}
impl<'a, T: ClientDB, U: SlotClock> BeaconBlockNode for DirectBeaconNode<'a, T, U>
impl<T: ClientDB, U: SlotClock> BeaconBlockNode for DirectBeaconNode<T, U>
where
BlockProductionError: From<<U>::Error>,
ProcessingError: From<<U as SlotClock>::Error>,
{
fn proposer_nonce(&self, pubkey: &PublicKey) -> Result<u64, BeaconBlockNodeError> {
let validator_index = self
@@ -51,8 +56,16 @@ where {
}
}
/// Returns the value specified by the `set_next_publish_result`.
fn publish_beacon_block(&self, block: BeaconBlock) -> Result<bool, BeaconBlockNodeError> {
Err(BeaconBlockNodeError::DecodeFailure)
fn publish_beacon_block(
&self,
block: BeaconBlock,
) -> Result<PublishOutcome, BeaconBlockNodeError> {
match self.beacon_chain.process_block(block) {
Ok(ProcessingOutcome::ValidBlock(_)) => Ok(PublishOutcome::ValidBlock),
Ok(ProcessingOutcome::InvalidBlock(reason)) => {
Ok(PublishOutcome::InvalidBlock(format!("{:?}", reason)))
}
Err(error) => Err(BeaconBlockNodeError::RemoteFailure(format!("{:?}", error))),
}
}
}

View File

@@ -2,15 +2,16 @@ use beacon_chain::{block_production::Error as BlockProductionError, BeaconChain}
use block_producer::{DutiesReader, DutiesReaderError};
use db::ClientDB;
use slot_clock::SlotClock;
use std::sync::Arc;
use types::PublicKey;
pub struct DirectDuties<'a, T: ClientDB, U: SlotClock> {
beacon_chain: &'a BeaconChain<T, U>,
pub struct DirectDuties<T: ClientDB, U: SlotClock> {
beacon_chain: Arc<BeaconChain<T, U>>,
pubkey: PublicKey,
}
impl<'a, T: ClientDB, U: SlotClock> DirectDuties<'a, T, U> {
pub fn new(pubkey: PublicKey, beacon_chain: &'a BeaconChain<T, U>) -> Self {
impl<T: ClientDB, U: SlotClock> DirectDuties<T, U> {
pub fn new(pubkey: PublicKey, beacon_chain: Arc<BeaconChain<T, U>>) -> Self {
Self {
beacon_chain,
pubkey,
@@ -18,11 +19,11 @@ impl<'a, T: ClientDB, U: SlotClock> DirectDuties<'a, T, U> {
}
}
impl<'a, T: ClientDB, U: SlotClock> DutiesReader for DirectDuties<'a, T, U>
impl<T: ClientDB, U: SlotClock> DutiesReader for DirectDuties<T, U>
where
BlockProductionError: From<<U>::Error>,
{
fn is_block_production_slot(&self, _epoch: u64, slot: u64) -> Result<bool, DutiesReaderError> {
fn is_block_production_slot(&self, slot: u64) -> Result<bool, DutiesReaderError> {
let validator_index = self
.beacon_chain
.validator_index(&self.pubkey)

View File

@@ -11,75 +11,77 @@ use spec::ChainSpec;
use std::sync::{Arc, RwLock};
use types::{Keypair, Validator};
pub struct TestRig<'a> {
pub struct TestRig {
db: Arc<MemoryDB>,
beacon_chain: BeaconChain<MemoryDB, TestingSlotClock>,
beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>,
block_store: Arc<BeaconBlockStore<MemoryDB>>,
state_store: Arc<BeaconStateStore<MemoryDB>>,
validators: Vec<TestValidator<'a>>,
validators: Vec<TestValidator>,
}
impl<'a> TestRig<'a> {
pub fn new(spec: ChainSpec) -> Self {
impl TestRig {
pub fn new(mut spec: ChainSpec, validator_count: usize) -> Self {
let db = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
let slot_clock = TestingSlotClock::new(0);
let mut beacon_chain =
BeaconChain::genesis(state_store.clone(), block_store.clone(), slot_clock, spec)
.unwrap();
// Remove the validators present in the spec (if any).
spec.initial_validators = Vec::with_capacity(validator_count);
spec.initial_balances = Vec::with_capacity(validator_count);
/*
let validators = generate_validators(validator_count, &beacon_chain);
beacon_chain.spec = inject_validators_into_spec(beacon_chain.spec.clone(), &validators[..]);
*/
// Insert `validator_count` new `Validator` records into the spec, retaining the keypairs
// for later user.
let mut keypairs = Vec::with_capacity(validator_count);
for _ in 0..validator_count {
let keypair = Keypair::random();
spec.initial_validators.push(Validator {
pubkey: keypair.pk.clone(),
..std::default::Default::default()
});
spec.initial_balances.push(32_000_000_000); // 32 ETH
keypairs.push(keypair);
}
// Create the Beacon Chain
let beacon_chain = Arc::new(
BeaconChain::genesis(state_store.clone(), block_store.clone(), slot_clock, spec)
.unwrap(),
);
// Spawn the test validator instances.
let mut validators = Vec::with_capacity(validator_count);
for keypair in keypairs {
validators.push(TestValidator::new(keypair.clone(), beacon_chain.clone()));
}
Self {
db,
beacon_chain,
block_store,
state_store,
validators: vec![],
validators,
}
}
pub fn generate_validators(&'a mut self, validator_count: usize) {
self.validators = Vec::with_capacity(validator_count);
for _ in 0..validator_count {
self.validators.push(TestValidator::new(&self.beacon_chain));
}
self.beacon_chain.spec =
inject_validators_into_spec(self.beacon_chain.spec.clone(), &self.validators[..]);
}
pub fn process_next_slot(&mut self) {
pub fn produce_next_slot(&mut self) {
let slot = self
.beacon_chain
.present_slot()
.expect("Unable to determine slot.")
+ 1;
self.beacon_chain.slot_clock.set_slot(slot);
let block_proposer = self
let proposer = self
.beacon_chain
.block_proposer(slot)
.expect("Unable to determine proposer.");
let validator = self
.validators
.get(block_proposer)
.expect("Block proposer unknown");
self.validators[proposer].set_slot(slot);
self.validators[proposer].produce_block().unwrap();
}
}
fn inject_validators_into_spec(mut spec: ChainSpec, validators: &[TestValidator]) -> ChainSpec {
spec.initial_validators = Vec::with_capacity(validators.len());
spec.initial_balances = Vec::with_capacity(validators.len());
for validator in validators {
spec.initial_validators.push(validator.validator_record());
spec.initial_balances.push(32_000_000_000); // 32 ETH
}
spec
}

View File

@@ -1,36 +1,46 @@
use super::{DirectBeaconNode, DirectDuties};
use beacon_chain::BeaconChain;
#[cfg(test)]
use block_producer::{test_utils::TestSigner, BlockProducer};
use block_producer::{test_utils::TestSigner, BlockProducer, Error as PollError};
use db::MemoryDB;
use slot_clock::TestingSlotClock;
use spec::ChainSpec;
use std::sync::{Arc, RwLock};
use types::{Keypair, Validator};
pub struct TestValidator<'a> {
pub use block_producer::PollOutcome;
#[derive(Debug, PartialEq)]
pub enum ProduceError {
DidNotProduce(PollOutcome),
PollError(PollError),
}
pub struct TestValidator {
block_producer: BlockProducer<
TestingSlotClock,
DirectBeaconNode<'a, MemoryDB, TestingSlotClock>,
DirectDuties<'a, MemoryDB, TestingSlotClock>,
DirectBeaconNode<MemoryDB, TestingSlotClock>,
DirectDuties<MemoryDB, TestingSlotClock>,
TestSigner,
>,
spec: Arc<ChainSpec>,
epoch_map: Arc<DirectDuties<'a, MemoryDB, TestingSlotClock>>,
epoch_map: Arc<DirectDuties<MemoryDB, TestingSlotClock>>,
keypair: Keypair,
beacon_node: Arc<DirectBeaconNode<'a, MemoryDB, TestingSlotClock>>,
slot_clock: Arc<RwLock<TestingSlotClock>>,
beacon_node: Arc<DirectBeaconNode<MemoryDB, TestingSlotClock>>,
slot_clock: Arc<TestingSlotClock>,
signer: Arc<TestSigner>,
}
impl<'a> TestValidator<'a> {
pub fn new(beacon_chain: &'a BeaconChain<MemoryDB, TestingSlotClock>) -> Self {
impl TestValidator {
pub fn new(
keypair: Keypair,
beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>,
) -> Self {
let spec = Arc::new(ChainSpec::foundation());
let keypair = Keypair::random();
let slot_clock = Arc::new(RwLock::new(TestingSlotClock::new(0)));
let slot_clock = Arc::new(TestingSlotClock::new(0));
let signer = Arc::new(TestSigner::new(keypair.clone()));
let beacon_node = Arc::new(DirectBeaconNode::new(beacon_chain));
let epoch_map = Arc::new(DirectDuties::new(keypair.pk.clone(), beacon_chain));
let beacon_node = Arc::new(DirectBeaconNode::new(beacon_chain.clone()));
let epoch_map = Arc::new(DirectDuties::new(keypair.pk.clone(), beacon_chain.clone()));
let block_producer = BlockProducer::new(
spec.clone(),
@@ -52,10 +62,15 @@ impl<'a> TestValidator<'a> {
}
}
pub fn validator_record(&self) -> Validator {
Validator {
pubkey: self.keypair.pk.clone(),
..std::default::Default::default()
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)),
}
}
pub fn set_slot(&mut self, slot: u64) {
self.slot_clock.set_slot(slot)
}
}