Skip to content

Commit

Permalink
Remove conditionals from epoch_from_block_number
Browse files Browse the repository at this point in the history
Upon analyzing the conditional logic of the `epoch_from_block_height`
it seems that we really want the epoch to increase on the successor of
the multiples of the `epoch_height`.  The conditional branches make this less obvious with 3 separate computed results for the calculation.  It seems that this can be achieved with a single statement.

In addition, extra clarity as to the parameter's intents can be added by
renaming `epoch_height` to something that indicates what the
parameter is symbolizing.
  • Loading branch information
Ayiga committed Jan 17, 2025
1 parent 46e1c0a commit 27c53a0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/types/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ pub fn bincode_opts() -> WithOtherTrailing<
}

/// Returns an epoch number given a block number and an epoch height
///
/// The epoch has the following properties:
/// - `block_number` 0 is always epoch 0
/// - `block_number` 1 is the first block in epoch 1
/// - The epoch increases every `blocks_per_epoch`
/// - Every epoch, other than 0, has `blocks_per_epoch` blocks within it
#[must_use]
pub fn epoch_from_block_number(block_number: u64, epoch_height: u64) -> u64 {
if epoch_height == 0 {
0
} else if block_number % epoch_height == 0 {
block_number / epoch_height
} else {
block_number / epoch_height + 1
}
pub fn epoch_from_block_number(block_number: u64, blocks_per_epoch: u64) -> u64 {
(block_number + blocks_per_epoch - 1) / blocks_per_epoch
}

/// A function for generating a cute little user mnemonic from a hash
Expand Down

0 comments on commit 27c53a0

Please sign in to comment.