Skip to content

Commit

Permalink
Account for state.get_block_root_at_epoch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent eb64294 commit ab76e2c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn process_justification_and_finalization<T: EthSpec>(
state: &BeaconState<T>,
participation_cache: &ParticipationCache,
) -> Result<JustificationAndFinalizationState<T>, Error> {
let justification_and_finalization_state = JustificationAndFinalizationState::new(state)?;
let justification_and_finalization_state = JustificationAndFinalizationState::new(state);

if state.current_epoch() <= T::genesis_epoch().safe_add(1)? {
return Ok(justification_and_finalization_state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn process_justification_and_finalization<T: EthSpec>(
total_balances: &TotalBalances,
_spec: &ChainSpec,
) -> Result<JustificationAndFinalizationState<T>, Error> {
let justification_and_finalization_state = JustificationAndFinalizationState::new(state)?;
let justification_and_finalization_state = JustificationAndFinalizationState::new(state);

if state.current_epoch() <= T::genesis_epoch().safe_add(1)? {
return Ok(justification_and_finalization_state);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::per_epoch_processing::Error;
use types::{BeaconState, BeaconStateError, BitVector, Checkpoint, Epoch, EthSpec, Hash256};

#[must_use = "this value must be applied to a state or explicitly dropped"]
Expand All @@ -7,9 +6,9 @@ pub struct JustificationAndFinalizationState<T: EthSpec> {
* Immutable fields.
*/
previous_epoch: Epoch,
previous_epoch_target_root: Hash256,
previous_epoch_target_root: Result<Hash256, BeaconStateError>,
current_epoch: Epoch,
current_epoch_target_root: Hash256,
current_epoch_target_root: Result<Hash256, BeaconStateError>,
/*
* Mutable fields.
*/
Expand All @@ -20,19 +19,19 @@ pub struct JustificationAndFinalizationState<T: EthSpec> {
}

impl<T: EthSpec> JustificationAndFinalizationState<T> {
pub fn new(state: &BeaconState<T>) -> Result<Self, Error> {
pub fn new(state: &BeaconState<T>) -> Self {
let previous_epoch = state.previous_epoch();
let current_epoch = state.current_epoch();
Ok(Self {
Self {
previous_epoch,
previous_epoch_target_root: *state.get_block_root_at_epoch(previous_epoch)?,
previous_epoch_target_root: state.get_block_root_at_epoch(previous_epoch).copied(),
current_epoch,
current_epoch_target_root: *state.get_block_root_at_epoch(current_epoch)?,
current_epoch_target_root: state.get_block_root_at_epoch(current_epoch).copied(),
previous_justified_checkpoint: state.previous_justified_checkpoint(),
current_justified_checkpoint: state.current_justified_checkpoint(),
finalized_checkpoint: state.finalized_checkpoint(),
justification_bits: state.justification_bits().clone(),
})
}
}

pub fn apply_changes_to_state(self, state: &mut BeaconState<T>) {
Expand Down Expand Up @@ -67,11 +66,11 @@ impl<T: EthSpec> JustificationAndFinalizationState<T> {
self.current_epoch
}

pub fn get_block_root_at_epoch(&self, epoch: Epoch) -> Result<&Hash256, BeaconStateError> {
pub fn get_block_root_at_epoch(&self, epoch: Epoch) -> Result<Hash256, BeaconStateError> {
if epoch == self.previous_epoch {
Ok(&self.previous_epoch_target_root)
self.previous_epoch_target_root.clone()
} else if epoch == self.current_epoch {
Ok(&self.current_epoch_target_root)
self.current_epoch_target_root.clone()
} else {
Err(BeaconStateError::SlotOutOfBounds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub fn weigh_justification_and_finalization<T: EthSpec>(
if previous_target_balance.safe_mul(3)? >= total_active_balance.safe_mul(2)? {
*state.current_justified_checkpoint_mut() = Checkpoint {
epoch: previous_epoch,
root: *state.get_block_root_at_epoch(previous_epoch)?,
root: state.get_block_root_at_epoch(previous_epoch)?,
};
state.justification_bits_mut().set(1, true)?;
}
// If the current epoch gets justified, fill the last bit.
if current_target_balance.safe_mul(3)? >= total_active_balance.safe_mul(2)? {
*state.current_justified_checkpoint_mut() = Checkpoint {
epoch: current_epoch,
root: *state.get_block_root_at_epoch(current_epoch)?,
root: state.get_block_root_at_epoch(current_epoch)?,
};
state.justification_bits_mut().set(0, true)?;
}
Expand Down

0 comments on commit ab76e2c

Please sign in to comment.