Add beacon state test builder, tidy errors

This commit is contained in:
Paul Hauner
2019-02-15 16:12:24 +11:00
parent 5031ee5505
commit ec4a658fe7
9 changed files with 173 additions and 104 deletions

View File

@@ -3,7 +3,7 @@ use hashing::hash;
use log::debug;
use ssz::{ssz_encode, TreeHash};
use types::{
beacon_state::{AttestationValidationError, CommitteesError},
beacon_state::{AttestationParticipantsError, BeaconStateError},
AggregatePublicKey, Attestation, BeaconBlock, BeaconState, ChainSpec, Crosslink, Epoch, Exit,
Fork, Hash256, PendingAttestation, PublicKey, Signature,
};
@@ -41,10 +41,23 @@ pub enum Error {
BadCustodyReseeds,
BadCustodyChallenges,
BadCustodyResponses,
CommitteesError(CommitteesError),
BeaconStateError(BeaconStateError),
SlotProcessingError(SlotProcessingError),
}
#[derive(Debug, PartialEq)]
pub enum AttestationValidationError {
IncludedTooEarly,
IncludedTooLate,
WrongJustifiedSlot,
WrongJustifiedRoot,
BadLatestCrosslinkRoot,
BadSignature,
ShardBlockRootNotZero,
NoBlockRoot,
AttestationParticipantsError(AttestationParticipantsError),
}
macro_rules! ensure {
($condition: expr, $result: expr) => {
if !$condition {
@@ -390,9 +403,9 @@ impl From<AttestationValidationError> for Error {
}
}
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Error {
Error::BeaconStateError(e)
}
}
@@ -401,3 +414,9 @@ impl From<SlotProcessingError> for Error {
Error::SlotProcessingError(e)
}
}
impl From<AttestationParticipantsError> for AttestationValidationError {
fn from(e: AttestationParticipantsError) -> AttestationValidationError {
AttestationValidationError::AttestationParticipantsError(e)
}
}

View File

@@ -5,7 +5,7 @@ use ssz::TreeHash;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use types::{
beacon_state::{AttestationParticipantsError, CommitteesError, InclusionError},
beacon_state::{AttestationParticipantsError, BeaconStateError, InclusionError},
validator_registry::get_active_validator_indices,
BeaconState, ChainSpec, Crosslink, Epoch, Hash256, PendingAttestation,
};
@@ -27,7 +27,7 @@ pub enum Error {
NoBlockRoots,
BaseRewardQuotientIsZero,
NoRandaoSeed,
CommitteesError(CommitteesError),
BeaconStateError(BeaconStateError),
AttestationParticipantsError(AttestationParticipantsError),
InclusionError(InclusionError),
WinningRootError(WinningRootError),
@@ -559,9 +559,7 @@ impl EpochProcessable for BeaconState {
self.current_epoch_start_shard = (self.current_epoch_start_shard
+ self.get_current_epoch_committee_count(spec) as u64)
% spec.shard_count;
self.current_epoch_seed = self
.generate_seed(self.current_calculation_epoch, spec)
.ok_or_else(|| Error::NoRandaoSeed)?;
self.current_epoch_seed = self.generate_seed(self.current_calculation_epoch, spec)?
} else {
let epochs_since_last_registry_update =
current_epoch - self.validator_registry_update_epoch;
@@ -569,9 +567,8 @@ impl EpochProcessable for BeaconState {
& epochs_since_last_registry_update.is_power_of_two()
{
self.current_calculation_epoch = next_epoch;
self.current_epoch_seed = self
.generate_seed(self.current_calculation_epoch, spec)
.ok_or_else(|| Error::NoRandaoSeed)?;
self.current_epoch_seed =
self.generate_seed(self.current_calculation_epoch, spec)?
}
}
@@ -689,9 +686,9 @@ impl From<InclusionError> for Error {
}
}
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Error {
Error::BeaconStateError(e)
}
}

View File

@@ -1,9 +1,9 @@
use crate::{EpochProcessable, EpochProcessingError};
use types::{beacon_state::CommitteesError, BeaconState, ChainSpec, Hash256};
use types::{beacon_state::BeaconStateError, BeaconState, ChainSpec, Hash256};
#[derive(Debug, PartialEq)]
pub enum Error {
CommitteesError(CommitteesError),
BeaconStateError(BeaconStateError),
EpochProcessingError(EpochProcessingError),
}
@@ -49,9 +49,9 @@ fn merkle_root(_input: &[Hash256]) -> Hash256 {
Hash256::zero()
}
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Error {
Error::BeaconStateError(e)
}
}