Refine state transition to allow first transition

This commit is contained in:
Paul Hauner
2019-01-31 00:39:34 +11:00
parent 6a4252b8c6
commit 7d94cfb0e4
22 changed files with 680 additions and 436 deletions

View File

@@ -96,7 +96,9 @@ impl AttestationAggregator {
self.store
.values()
.filter_map(|attestation| {
if state.validate_attestation(attestation, spec).is_ok()
if state
.validate_attestation_without_signature(attestation, spec)
.is_ok()
&& !known_attestation_data.contains(&attestation.data)
{
Some(attestation.clone())

View File

@@ -1,5 +1,6 @@
use super::state_transition::Error as TransitionError;
use super::{BeaconChain, ClientDB, DBError, SlotClock};
use log::debug;
use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError};
use ssz::{ssz_encode, Encodable};
use types::{
@@ -65,6 +66,8 @@ where
where
V: BeaconBlockReader + Encodable + Sized,
{
debug!("Processing block with slot {}...", block.slot());
let block = block
.into_beacon_block()
.ok_or(Error::UnableToDecodeBlock)?;

View File

@@ -1,6 +1,7 @@
use super::state_transition::Error as TransitionError;
use super::{BeaconChain, ClientDB, DBError, SlotClock};
use bls::Signature;
use log::debug;
use slot_clock::TestingSlotClockError;
use types::{
readers::{BeaconBlockReader, BeaconStateReader},
@@ -33,6 +34,8 @@ where
.map_err(|e| e.into())?
.ok_or(Error::PresentSlotIsNone)?;
debug!("Producing block for slot {}...", present_slot);
let parent_root = self.head().beacon_block_root;
let parent_block_reader = self
.block_store
@@ -45,6 +48,8 @@ where
.into_beacon_state()
.ok_or_else(|| Error::DBError("State invalid.".to_string()))?;
debug!("Finding attesatations for block...");
let attestations = self
.attestation_aggregator
.read()
@@ -52,6 +57,8 @@ where
// TODO: advance the parent_state slot.
.get_attestations_for_state(&parent_state, &self.spec);
debug!("Found {} attestation(s).", attestations.len());
let mut block = BeaconBlock {
slot: present_slot,
parent_root: parent_root.clone(),
@@ -81,6 +88,8 @@ where
block.state_root = state_root;
debug!("Block produced.");
Ok((block, state))
}
}

View File

@@ -1,11 +1,11 @@
use crate::{BeaconChain, CheckPoint, ClientDB, SlotClock};
use std::sync::RwLockReadGuard;
use types::{beacon_state::SlotProcessingError, BeaconBlock, BeaconState, Hash256};
use types::{beacon_state::CommitteesError, BeaconBlock, BeaconState, Hash256};
#[derive(Debug, PartialEq)]
pub enum Error {
PastSlot,
UnableToDetermineProducer,
CommitteesError(CommitteesError),
}
impl<T, U> BeaconChain<T, U>
@@ -64,10 +64,8 @@ where
}
}
impl From<SlotProcessingError> for Error {
fn from(e: SlotProcessingError) -> Error {
match e {
SlotProcessingError::UnableToDetermineProducer => Error::UnableToDetermineProducer,
}
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
}
}

View File

@@ -1,9 +0,0 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock};
impl<T, U> BeaconChain<T, U>
where
T: ClientDB,
U: SlotClock,
{
pub fn per_epoch_processing(&self) {}
}

View File

@@ -1,10 +1,10 @@
use super::{BeaconChain, ClientDB, SlotClock};
use types::{beacon_state::Error as BeaconStateError, PublicKey};
use types::{beacon_state::CommitteesError, PublicKey};
#[derive(Debug, PartialEq)]
pub enum Error {
SlotClockError,
BeaconStateError(BeaconStateError),
CommitteesError(CommitteesError),
}
impl<T, U> BeaconChain<T, U>
@@ -45,7 +45,7 @@ where
}
}
pub fn block_proposer(&self, slot: u64) -> Result<usize, Error> {
pub fn block_proposer(&self, slot: u64) -> Result<usize, CommitteesError> {
// TODO: fix unwrap
let present_slot = self.present_slot().unwrap();
// TODO: fix unwrap
@@ -67,12 +67,14 @@ where
let present_slot = self.present_slot()?;
let state = self.state(present_slot).ok()?;
Some(state.attestation_slot_and_shard_for_validator(validator_index, &self.spec))
state
.attestation_slot_and_shard_for_validator(validator_index, &self.spec)
.ok()
}
}
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Error {
Error::BeaconStateError(e)
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
}
}

View File

@@ -7,7 +7,6 @@ pub mod block_processing;
pub mod block_production;
mod canonical_head;
pub mod dump;
pub mod epoch_processing;
mod finalized_head;
mod info;
mod lmd_ghost;

View File

@@ -1,13 +1,13 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock};
use bls::{PublicKey, Signature};
use boolean_bitfield::BooleanBitfield;
use hashing::hash;
use log::debug;
use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError};
use ssz::{ssz_encode, TreeHash};
use types::{
beacon_state::{AttestationValidationError, SlotProcessingError},
beacon_state::{AttestationValidationError, CommitteesError, EpochProcessingError},
readers::BeaconBlockReader,
AttestationData, BeaconBlock, BeaconState, Exit, Fork, Hash256, PendingAttestation,
BeaconBlock, BeaconState, Exit, Fork, Hash256, PendingAttestation,
};
// TODO: define elsehwere.
@@ -51,6 +51,8 @@ pub enum Error {
BadCustodyChallenges,
BadCustodyResponses,
SlotClockError(SystemTimeSlotClockError),
CommitteesError(CommitteesError),
EpochProcessingError(EpochProcessingError),
}
impl<T, U> BeaconChain<T, U>
@@ -82,6 +84,11 @@ where
) -> Result<BeaconState, Error> {
ensure!(state.slot < block.slot, Error::StateAlreadyTransitioned);
debug!(
"Starting state transition from slot {} to {}...",
state.slot, block.slot
);
for _ in state.slot..block.slot {
state.per_slot_processing(block.parent_root.clone(), &self.spec)?;
}
@@ -113,6 +120,8 @@ where
);
}
debug!("Block signature is valid.");
/*
* RANDAO
*/
@@ -127,6 +136,8 @@ where
Error::BadRandaoSignature
);
debug!("RANDAO signature is valid.");
// TODO: check this is correct.
let new_mix = {
let mut mix = state.latest_randao_mixes
@@ -228,6 +239,11 @@ where
state.latest_attestations.push(pending_attestation);
}
debug!(
"{} attestations verified & processed.",
block.body.attestations.len()
);
/*
* Deposits
*/
@@ -294,9 +310,11 @@ where
);
if state.slot % self.spec.epoch_length == 0 {
state.per_epoch_processing(&self.spec).unwrap();
state.per_epoch_processing(&self.spec)?;
}
debug!("State transition complete.");
Ok(state)
}
}
@@ -337,16 +355,20 @@ impl From<SystemTimeSlotClockError> for Error {
}
}
impl From<SlotProcessingError> for Error {
fn from(e: SlotProcessingError) -> Error {
match e {
SlotProcessingError::UnableToDetermineProducer => Error::NoBlockProducer,
}
}
}
impl From<AttestationValidationError> for Error {
fn from(e: AttestationValidationError) -> Error {
Error::InvalidAttestation(e)
}
}
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
}
}
impl From<EpochProcessingError> for Error {
fn from(e: EpochProcessingError) -> Error {
Error::EpochProcessingError(e)
}
}