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

feat: added BlockNumber struct #1043

Open
wants to merge 4 commits into
base: next
Choose a base branch
from

Conversation

varun-doshi
Copy link

Ref #1040

@varun-doshi
Copy link
Author

This is currently in init state. I will implement the functions as advised next

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

Looks good generally. I have some nits and mainly two comments.

  1. I think we can make more use of BlockNumber in many places, see inline comments.

  2. It would be great if we can get rid of the standalone functions so they are only accessed through BlockNumber::* functions:

/// Returns the block number of the epoch block for the given `epoch`.
pub const fn block_num_from_epoch(epoch: u16) -> u32 {
(epoch as u32) << BlockHeader::EPOCH_LENGTH_EXPONENT
}
/// Returns the epoch of the given block number.
pub const fn block_epoch_from_number(block_number: u32) -> u16 {
(block_number >> BlockHeader::EPOCH_LENGTH_EXPONENT) as u16
}

Not sure whether we should keep BlockHeader::block_epoch. It can be easily replaced by block_header.block_num().epoch(), but it's also convenient. Both options are fine, I think.

objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
objects/src/block/header.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Thanks for the updates, it looks great!

I mentioned a couple more places where we can replace u32s with BlockNumber.

To get this PR in a mergable state, there are some other small tasks:

  • Add an entry in CHANGELOG.md pointing to this PR, for example.

    Add BlockNumber struct (feat: added BlockNumber struct #1043).

  • Run make format, make clippy and make doc locally to ensure everything is formatted correctly, that no lints complain and that docs can be built without errors.
  • You can also run cargo test -p miden-objects --features testing to run tests locally and make sure CI won't complain.
  • There is a small merge conflict that needs to be resolved.

Scroll to the bottom of this PR to see which checks in CI are failing.

Thanks again for your contributions!

Comment on lines 277 to 278
/// Holds `u32` type to signify Block Number

#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)]
/// A convenience wrapper around a `u32` representing the number of a block.
///
/// Each block has a unique number and block numbers increase monotonically by `1`.
pub struct BlockNumber(u32);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Holds `u32` type to signify Block Number
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)]
/// A convenience wrapper around a `u32` representing the number of a block.
///
/// Each block has a unique number and block numbers increase monotonically by `1`.
pub struct BlockNumber(u32);
/// A convenience wrapper around a `u32` representing the number of a block.
///
/// Each block has a unique number and block numbers increase monotonically by `1`.
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)]
pub struct BlockNumber(u32);
  • Leftover docs from before.
  • Nit: derives are usually placed directly above the struct definition.

/// The epoch of a block can be obtained by shifting the block number to the right by this
/// exponent.
pub const EPOCH_LENGTH_EXPONENT: u8 = 16;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The epoch of a block can be obtained by shifting the block number to the right by this
/// exponent.
pub const EPOCH_LENGTH_EXPONENT: u8 = 16;
/// The length of an epoch expressed as a power of two. `2^(EPOCH_LENGTH_EXPONENT)` is the
/// number of blocks in an epoch.
///
/// The epoch of a block can be obtained by shifting the block number to the right by this
/// exponent.
pub const EPOCH_LENGTH_EXPONENT: u8 = 16;

The docs were moved incompletely. Can you remove them from the impl BlockHeader block and move them here?

Comment on lines 311 to 313

pub const fn from_epoch(epoch: u16) -> BlockNumber {
BlockNumber((epoch as u32) << BlockNumber::EPOCH_LENGTH_EXPONENT)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub const fn from_epoch(epoch: u16) -> BlockNumber {
BlockNumber((epoch as u32) << BlockNumber::EPOCH_LENGTH_EXPONENT)
}
/// Creates the [`BlockNumber`] corresponding to the epoch block for the provided `epoch`.
pub const fn from_epoch(epoch: u16) -> BlockNumber {
BlockNumber((epoch as u32) << BlockNumber::EPOCH_LENGTH_EXPONENT)
}

Public methods should be documented.

Comment on lines +316 to +318
pub const fn block_epoch(&self) -> u16 {
(self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub const fn block_epoch(&self) -> u16 {
(self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16
}
/// Returns the epoch to which this block number belongs.
pub const fn block_epoch(&self) -> u16 {
(self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16
}

Public methods should be documented.

Comment on lines +320 to +323
pub fn as_u32(&self) -> u32 {
self.0
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn as_u32(&self) -> u32 {
self.0
}
/// Returns the block number as a `u32`.
pub fn as_u32(&self) -> u32 {
self.0
}

Public methods should be documented.

Comment on lines 51 to 53
return Err(ChainMmrError::block_num_too_big(
chain_length,
block.block_num().as_u32(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty much everywhere we used a u32 before to represent a block number we can now use BlockNumber, so also in ChainMmrError. Could you update those as well?

Comment on lines 83 to 88
pub fn contains_block(&self, block_num: u32) -> bool {
self.blocks.contains_key(&block_num)
self.blocks.contains_key(&block_num.into())
}

/// Returns the block header for the specified block, or None if the block is not present in
/// this chain MMR.
pub fn get_block(&self, block_num: u32) -> Option<&BlockHeader> {
self.blocks.get(&block_num)
self.blocks.get(&block_num.into())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these can also take BlockNumber now instead of u32.

Comment on lines 51 to 52
pub fn new(
anchor_block_number: BlockNumber,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn new(
anchor_block_number: BlockNumber,
pub fn new(
anchor_block_number: impl Into<BlockNumber>,

Just realized we could make this nicer by doing this.
That way we can pass both BlockNumber and u32 to this function.

You can just add a line let anchor_block_number = anchor_block_number.into() as the first line in this function to make the conversion.

Comment on lines 21 to 22
pub fn block_num(&self) -> BlockNumber {
self.block_num.into()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can change the block_num field in NoteLocation directly to a BlockNumber, then we don't have to convert here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants