Add FreeAttesation type

This commit is contained in:
Paul Hauner
2019-01-28 16:21:33 +11:00
parent 5bbffcb053
commit be7e326c33
11 changed files with 92 additions and 66 deletions

View File

@@ -4,7 +4,7 @@ use beacon_chain::block_processing::Error as ProcessingError;
use beacon_chain::block_production::Error as BlockProductionError;
use db::ClientDB;
use slot_clock::SlotClock;
use types::{AttestationData, Signature};
use types::{AttestationData, FreeAttestation};
impl<T: ClientDB, U: SlotClock> AttesterBeaconNode for BenchingBeaconNode<T, U>
where
@@ -24,15 +24,9 @@ where
fn publish_attestation_data(
&self,
attestation_data: AttestationData,
signature: Signature,
validator_index: u64,
free_attestation: FreeAttestation,
) -> Result<PublishOutcome, NodeError> {
match self.beacon_chain.process_free_attestation(
&attestation_data,
&signature,
validator_index,
) {
match self.beacon_chain.process_free_attestation(free_attestation) {
Ok(_) => Ok(PublishOutcome::ValidAttestation),
Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))),
}

View File

@@ -3,16 +3,11 @@ use db::ClientDB;
use parking_lot::RwLock;
use slot_clock::SlotClock;
use std::sync::Arc;
use types::{AttestationData, BeaconBlock, Signature};
use types::{BeaconBlock, FreeAttestation};
mod attester;
mod producer;
/// An attestation that hasn't been aggregated into an `Attestation`.
///
/// (attestation_data, signature, validator_index)
pub type FreeAttestation = (AttestationData, Signature, u64);
pub struct BenchingBeaconNode<T: ClientDB, U: SlotClock> {
beacon_chain: Arc<BeaconChain<T, U>>,
published_blocks: RwLock<Vec<BeaconBlock>>,
@@ -31,4 +26,8 @@ impl<T: ClientDB, U: SlotClock> BenchingBeaconNode<T, U> {
pub fn last_published_block(&self) -> Option<BeaconBlock> {
Some(self.published_blocks.read().last()?.clone())
}
pub fn last_published_free_attestation(&self) -> Option<FreeAttestation> {
Some(self.published_attestations.read().last()?.clone())
}
}

View File

@@ -1,24 +1,30 @@
use attester::Attester;
use attester::{Attester, Error as AttestationPollError};
use beacon_chain::BeaconChain;
use block_producer::{BlockProducer, Error as PollError};
use block_producer::{BlockProducer, Error as BlockPollError};
use db::MemoryDB;
use signer::TestSigner;
use slot_clock::TestingSlotClock;
use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Keypair};
use types::{BeaconBlock, ChainSpec, FreeAttestation, Keypair};
mod beacon_node;
mod direct_duties;
mod signer;
pub use self::beacon_node::BenchingBeaconNode;
pub use self::direct_duties::DirectDuties;
pub use block_producer::PollOutcome;
pub use attester::PollOutcome as AttestationPollOutcome;
pub use block_producer::PollOutcome as BlockPollOutcome;
#[derive(Debug, PartialEq)]
pub enum ProduceError {
DidNotProduce(PollOutcome),
PollError(PollError),
pub enum BlockProduceError {
DidNotProduce(BlockPollOutcome),
PollError(BlockPollError),
}
#[derive(Debug, PartialEq)]
pub enum AttestationProduceError {
DidNotProduce(AttestationPollOutcome),
PollError(AttestationPollError),
}
pub struct TestValidator {
@@ -81,13 +87,13 @@ impl TestValidator {
}
}
pub fn produce_block(&mut self) -> Result<BeaconBlock, ProduceError> {
pub fn produce_block(&mut self) -> Result<BeaconBlock, BlockProduceError> {
// Using `BenchingBeaconNode`, the validator will always return sucessufully if it tries to
// publish a block.
match self.block_producer.poll() {
Ok(PollOutcome::BlockProduced(_)) => {}
Ok(outcome) => return Err(ProduceError::DidNotProduce(outcome)),
Err(error) => return Err(ProduceError::PollError(error)),
Ok(BlockPollOutcome::BlockProduced(_)) => {}
Ok(outcome) => return Err(BlockProduceError::DidNotProduce(outcome)),
Err(error) => return Err(BlockProduceError::PollError(error)),
};
Ok(self
.beacon_node
@@ -95,6 +101,18 @@ impl TestValidator {
.expect("Unable to obtain produced block."))
}
pub fn produce_free_attestation(&mut self) -> Result<FreeAttestation, AttestationProduceError> {
match self.attester.poll() {
Ok(AttestationPollOutcome::AttestationProduced(_)) => {}
Ok(outcome) => return Err(AttestationProduceError::DidNotProduce(outcome)),
Err(error) => return Err(AttestationProduceError::PollError(error)),
};
Ok(self
.beacon_node
.last_published_free_attestation()
.expect("Unable to obtain produced attestation."))
}
pub fn set_slot(&mut self, slot: u64) {
self.slot_clock.set_slot(slot)
}