From 27c53a0664f5f09f307606994886600a2ce4ba15 Mon Sep 17 00:00:00 2001 From: Theodore Schnepper Date: Fri, 17 Jan 2025 07:49:37 -0700 Subject: [PATCH] Remove conditionals from `epoch_from_block_number` 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. --- crates/types/src/utils.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/types/src/utils.rs b/crates/types/src/utils.rs index 3183b4a508..a0355e5d3d 100644 --- a/crates/types/src/utils.rs +++ b/crates/types/src/utils.rs @@ -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