mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 12:11:59 +00:00
Add state helpers from #148
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -113,10 +113,7 @@ impl BeaconChainHarness {
|
||||
pub fn produce_block(&mut self) -> BeaconBlock {
|
||||
let present_slot = self.beacon_chain.present_slot().unwrap();
|
||||
|
||||
let proposer = self
|
||||
.beacon_chain
|
||||
.block_proposer(present_slot)
|
||||
.expect("Unable to determine proposer.");
|
||||
let proposer = self.beacon_chain.block_proposer(present_slot).unwrap();
|
||||
|
||||
self.validators[proposer].produce_block().unwrap()
|
||||
}
|
||||
|
||||
@@ -35,9 +35,9 @@ where
|
||||
.ok_or_else(|| ProducerDutiesReaderError::UnknownValidator)?;
|
||||
|
||||
match self.beacon_chain.block_proposer(slot) {
|
||||
Some(proposer) if proposer == validator_index => Ok(true),
|
||||
Some(_) => Ok(false),
|
||||
None => Err(ProducerDutiesReaderError::UnknownEpoch),
|
||||
Ok(proposer) if proposer == validator_index => Ok(true),
|
||||
Ok(_) => Ok(false),
|
||||
Err(_) => Err(ProducerDutiesReaderError::UnknownEpoch),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@ use types::ChainSpec;
|
||||
|
||||
#[test]
|
||||
fn it_can_build_on_genesis_block() {
|
||||
let validator_count = 2;
|
||||
let mut harness = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
|
||||
let validator_count = 10;
|
||||
let spec = ChainSpec::foundation();
|
||||
let mut harness = BeaconChainHarness::new(spec, validator_count);
|
||||
|
||||
harness.advance_chain_with_block();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user