Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update epoch_from_block_number for clarity #4037

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break if blocks_per_epoch is 0, which happens in cases when epochs aren't configured yet.

Also I just merged a change that adds a new fn option_epoch_from_block_number right after this one, I'd rebase and change that one too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you are right. I was reaching the zero check as being against block_number instead of epoch_height for some reason. Good catch on my misinterpretation.

}

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