Skip to content

Commit

Permalink
Add comments, safety to block shuffle id
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 16, 2020
1 parent 0d970a7 commit 09494e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
current: head_block.current_epoch_shuffling_id.clone(),
next: head_block.next_epoch_shuffling_id.clone(),
}
.id_for_epoch(shuffling_epoch);
.id_for_epoch(shuffling_epoch)
.ok_or_else(|| Error::InvalidShufflingId)?;

// Obtain the shuffling cache, timing how long we wait.
let cache_wait_timer =
Expand Down
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum BeaconChainError {
ObservedBlockProducersError(ObservedBlockProducersError),
PruningError(PruningError),
ArithError(ArithError),
InvalidShufflingId,
}

easy_from_to!(SlotProcessingError, BeaconChainError);
Expand Down
16 changes: 11 additions & 5 deletions beacon_node/beacon_chain/src/shuffling_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,27 @@ impl ShufflingCache {
}
}

/// Contains the shuffling IDs for a beacon block.
pub struct BlockShufflingIds {
pub current: ShufflingId,
pub next: ShufflingId,
}

impl BlockShufflingIds {
pub fn id_for_epoch(&self, epoch: Epoch) -> ShufflingId {
/// Returns the shuffling ID for the given epoch.
///
/// Returns `None` if `epoch` is prior to `self.current.shuffling_epoch`.
pub fn id_for_epoch(&self, epoch: Epoch) -> Option<ShufflingId> {
if epoch == self.current.shuffling_epoch {
self.current.clone()
Some(self.current.clone())
} else if epoch == self.next.shuffling_epoch {
self.next.clone()
} else {
Some(self.next.clone())
} else if epoch > self.next.shuffling_epoch {
let mut shuffling_id = self.next.clone();
shuffling_id.shuffling_epoch = epoch;
shuffling_id
Some(shuffling_id)
} else {
None
}
}
}

0 comments on commit 09494e0

Please sign in to comment.