-
Notifications
You must be signed in to change notification settings - Fork 53
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
base: next
Are you sure you want to change the base?
feat: added BlockNumber struct #1043
Conversation
This is currently in init state. I will implement the functions as advised next |
There was a problem hiding this 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.
-
I think we can make more use of
BlockNumber
in many places, see inline comments. -
It would be great if we can get rid of the standalone functions so they are only accessed through
BlockNumber::*
functions:
miden-base/objects/src/block/header.rs
Lines 281 to 289 in c98b1b7
/// 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.
0b1e3ab
to
0358d5d
Compare
There was a problem hiding this 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 u32
s 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
andmake 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!
objects/src/block/header.rs
Outdated
/// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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?
objects/src/block/header.rs
Outdated
|
||
pub const fn from_epoch(epoch: u16) -> BlockNumber { | ||
BlockNumber((epoch as u32) << BlockNumber::EPOCH_LENGTH_EXPONENT) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
pub const fn block_epoch(&self) -> u16 { | ||
(self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
pub fn as_u32(&self) -> u32 { | ||
self.0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
objects/src/transaction/chain_mmr.rs
Outdated
return Err(ChainMmrError::block_num_too_big( | ||
chain_length, | ||
block.block_num().as_u32(), |
There was a problem hiding this comment.
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?
objects/src/transaction/chain_mmr.rs
Outdated
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()) | ||
} |
There was a problem hiding this comment.
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
.
pub fn new( | ||
anchor_block_number: BlockNumber, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
objects/src/notes/location.rs
Outdated
pub fn block_num(&self) -> BlockNumber { | ||
self.block_num.into() |
There was a problem hiding this comment.
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.
0358d5d
to
e43148c
Compare
Ref #1040