Add state helpers from #148

This commit is contained in:
Paul Hauner
2019-01-28 19:12:20 +11:00
parent f92b9d618a
commit 6a4252b8c6
10 changed files with 167 additions and 65 deletions

View File

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

View File

@@ -98,7 +98,7 @@ where
let block_proposer_index = state
.get_beacon_proposer_index(block.slot, &self.spec)
.ok_or(Error::NoBlockProducer)?;
.map_err(|_| Error::NoBlockProducer)?;
let block_proposer = &state.validator_registry[block_proposer_index];
if verify_block_signature {
@@ -294,7 +294,7 @@ where
);
if state.slot % self.spec.epoch_length == 0 {
state.per_epoch_processing(&self.spec);
state.per_epoch_processing(&self.spec).unwrap();
}
Ok(state)