Add attesatation aggregation to test harness

This commit is contained in:
Paul Hauner
2019-01-28 17:07:13 +11:00
parent be7e326c33
commit 2882110525
4 changed files with 61 additions and 21 deletions

View File

@@ -9,7 +9,7 @@ use slot_clock::TestingSlotClock;
use std::fs::File;
use std::io::prelude::*;
use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Keypair, Validator};
use types::{BeaconBlock, ChainSpec, FreeAttestation, Keypair, Validator};
pub struct BeaconChainHarness {
pub db: Arc<MemoryDB>,
@@ -74,33 +74,69 @@ impl BeaconChainHarness {
}
}
pub fn advance_chain_without_block(&mut self) -> BeaconBlock {
self.produce_next_slot()
}
pub fn advance_chain_with_block(&mut self) {
let block = self.produce_next_slot();
self.beacon_chain.process_block(block).unwrap();
}
fn produce_next_slot(&mut self) -> BeaconBlock {
/// Move the `slot_clock` for the `BeaconChain` forward one slot.
///
/// This is the equivalent of advancing a system clock forward one `SLOT_DURATION`.
pub fn increment_beacon_chain_slot(&mut self) {
let slot = self
.beacon_chain
.present_slot()
.expect("Unable to determine slot.")
+ 1;
self.beacon_chain.slot_clock.set_slot(slot);
}
/// Gather the `FreeAttestation`s from the valiators.
///
/// Note: validators will only produce attestations _once per slot_. So, if you call this twice
/// you'll only get attestations on the first run.
pub fn gather_free_attesations(&mut self) -> Vec<FreeAttestation> {
let present_slot = self.beacon_chain.present_slot().unwrap();
let mut free_attestations = vec![];
for validator in &mut self.validators {
// Advance the validator slot.
validator.set_slot(present_slot);
// Prompt the validator to produce an attestation (if required).
if let Ok(free_attestation) = validator.produce_free_attestation() {
free_attestations.push(free_attestation);
}
}
free_attestations
}
/// Get the block from the proposer for the slot.
///
/// Note: the validator will only produce it _once per slot_. So, if you call this twice you'll
/// only get a block once.
pub fn produce_block(&mut self) -> BeaconBlock {
let present_slot = self.beacon_chain.present_slot().unwrap();
let proposer = self
.beacon_chain
.block_proposer(slot)
.block_proposer(present_slot)
.expect("Unable to determine proposer.");
self.validators[proposer].set_slot(slot);
self.validators[proposer].produce_block().unwrap()
}
/// Advances the chain with a BeaconBlock and attestations from all validators.
///
/// This is the ideal scenario for the Beacon Chain, 100% honest participation from
/// validators.
pub fn advance_chain_with_block(&mut self) {
self.increment_beacon_chain_slot();
let free_attestations = self.gather_free_attesations();
for free_attestation in free_attestations {
self.beacon_chain
.process_free_attestation(free_attestation.clone())
.unwrap();
}
let block = self.produce_block();
self.beacon_chain.process_block(block).unwrap();
}
pub fn chain_dump(&self) -> Result<Vec<SlotDump>, DumpError> {
self.beacon_chain.chain_dump()
}

View File

@@ -26,9 +26,7 @@ where
&self,
free_attestation: FreeAttestation,
) -> Result<PublishOutcome, NodeError> {
match self.beacon_chain.process_free_attestation(free_attestation) {
Ok(_) => Ok(PublishOutcome::ValidAttestation),
Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))),
}
self.published_attestations.write().push(free_attestation);
Ok(PublishOutcome::ValidAttestation)
}
}