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

Merged
merged 12 commits into from
Jan 16, 2025

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!

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/transaction/chain_mmr.rs Outdated Show resolved Hide resolved
objects/src/transaction/chain_mmr.rs Outdated Show resolved Hide resolved
objects/src/accounts/account_id_anchor.rs Outdated Show resolved Hide resolved
objects/src/notes/location.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.

Looks good to me! Thank you again for the contribution!

I pushed some minor commits, mostly to resolve the merge conflicts.

@bobbinth Do you want to take a look or should we merge it?

@PhilippGackstatter
Copy link
Contributor

@varun-doshi I only now notice that your commits are not signed. This repo requires that commits are signed, otherwise we cannot merge the PR. Can you sign them (this might be helpful: https://stackoverflow.com/questions/41882919/is-there-a-way-to-gpg-sign-all-previous-commits)? If you don't want to do that, I can also rebase the PR to include your commits in mine and sign them, though I think the PR would not count as your contribution then. Let me know how you want to proceed.

Comment on lines 590 to 591
note_block_num.as_u32(),
self.blocks.get(note_block_num.as_usize()).unwrap().header(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the maps not use BlockNumber as a key instead?

@@ -632,7 +632,7 @@ impl MockChain {
/// This will also make all the objects currently pending available for use.
/// If `block_num` is `Some(number)`, blocks will be generated up to `number`.
pub fn seal_block(&mut self, block_num: Option<u32>) -> Block {
let next_block_num = self.blocks.last().map_or(0, |b| b.header().block_num() + 1);
let next_block_num = self.blocks.last().map_or(0, |b| b.header().block_num().as_u32() + 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd recommend adding a BlockNumber::next or similar method to increment by one.

Comment on lines 278 to 279
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)]
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.

Should we not derive Default, and possible add some utility functions like next/previous or child/parent?

I'd also appreciate addition and checked subtraction implementations.

Copy link
Author

Choose a reason for hiding this comment

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

Using next or previous results in this warning from clippy:

method `next` can be confused for the standard trait method `std::iter::Iterator::next`

hence going with parent and child

@@ -44,9 +44,9 @@ impl TransactionInputs {

// check the block_chain and block_header are consistent
let block_num = block_header.block_num();
if block_chain.chain_length() != block_header.block_num() as usize {
if block_chain.chain_length() != block_header.block_num().as_usize() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should chain length not be a block number?

@varun-doshi
Copy link
Author

@varun-doshi I only now notice that your commits are not signed. This repo requires that commits are signed, otherwise we cannot merge the PR. Can you sign them (this might be helpful: https://stackoverflow.com/questions/41882919/is-there-a-way-to-gpg-sign-all-previous-commits)? If you don't want to do that, I can also rebase the PR to include your commits in mine and sign them, though I think the PR would not count as your contribution then. Let me know how you want to proceed.

@PhilippGackstatter I'm rebasing and signing my commits, but looks like for some reason after the rebase, your signatures for the last 3 commits are gone(git log --show-signature). Is that okay? You can perhaps sign them again?

@varun-doshi varun-doshi force-pushed the varun/blocknumber-struct branch from 3fcd0fc to 5c1c71d Compare January 14, 2025 12:25
@PhilippGackstatter
Copy link
Contributor

PhilippGackstatter commented Jan 14, 2025

@PhilippGackstatter I'm rebasing and signing my commits, but looks like for some reason after the rebase, your signatures for the last 3 commits are gone(git log --show-signature). Is that okay? You can perhaps sign them again?

Thanks. I think you may have rebased onto some older branch as there are now a lot of unrelated changes in your PR and conflicts that shouldn't be there. Did you rebase onto 0xPolygonMiden/miden-base/next, i.e. the next branch form this repo and not your own repo?

@bobbinth told me he can override the commit sign requirement, so I think it's easiest to just do that. I can force push the old state of the branch again if that's fine with you.

@varun-doshi
Copy link
Author

varun-doshi commented Jan 14, 2025

@PhilippGackstatter I'm rebasing and signing my commits, but looks like for some reason after the rebase, your signatures for the last 3 commits are gone(git log --show-signature). Is that okay? You can perhaps sign them again?

Thanks. I think you may have rebased onto some older branch as there are now a lot of unrelated changes in your PR and conflicts that shouldn't be there. Did you rebase onto 0xPolygonMiden/miden-base/next, i.e. the next branch form this repo and not your own repo?

@bobbinth told me he can override the commit sign requirement, so I think it's easiest to just do that. I can force push the old state of the branch again if that's fine with you.

I see
Yes thats fine with me
Apologies for the confusion

@PhilippGackstatter
Copy link
Contributor

@varun-doshi No worries at all, we appreciate the contribution 😃!

@varun-doshi
Copy link
Author

varun-doshi commented Jan 14, 2025

@PhilippGackstatter should i go ahead and make the changes suggested by @Mirko-von-Leipzig ?(in case you've not already started on those)

@PhilippGackstatter
Copy link
Contributor

@PhilippGackstatter should i go ahead and make the changes suggested by @Mirko-von-Leipzig ?(in case you've not already started on those)

Sure, feel free to address those, thanks.

@varun-doshi varun-doshi force-pushed the varun/blocknumber-struct branch from e17b085 to 7bda320 Compare January 15, 2025 10:10
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.

Looks good to me!
Just BlockNumber::parent should be changed (see inline comment).

Comment on lines 26 to 28
pub fn parent(&self) -> u32 {
self.checked_sub(1).expect("Cannot go below Genesis block!").0
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should return Option<BlockNumber>. If you call BlockNumber::from(0_u32).parent() this would panic which is not what we want. The caller of parent() needs to deal with the possibility of calling this on block number 0.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you for working on this. I left a couple of optional comments inline.

objects/src/transaction/inputs.rs Outdated Show resolved Hide resolved
objects/src/block/block_number.rs Show resolved Hide resolved
/// Returns the previous block number
pub fn parent(&self) -> u32 {
self.checked_sub(1).expect("Cannot go below Genesis block!").0
pub fn parent(&self) -> Option<u32> {
Copy link
Contributor

Choose a reason for hiding this comment

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

These should probably return Option<BlockNumber>, same with fn child.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should child actually return Option<BlockNumber>? It seems fine to return BlockNumber since a u32 overflow would in practice only occur decades from now.
For parent I think it makes sense because in practice one might actually call parent() on the genesis block.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right I realise what I said was ambiguous. I agree, I meant that they should both return BlockNumber and not u32 values.

fn parent(&self) -> Option<BlockNumber>
fn child(&self) -> BlockNumber

btw you should probably also take by value since these are trivially Copy types so no need for references.

@varun-doshi varun-doshi force-pushed the varun/blocknumber-struct branch from 4c67afe to 6cc6b76 Compare January 16, 2025 15:19
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

All looks good! Thank you again for working on this!

@bobbinth bobbinth merged commit 2e4a974 into 0xPolygonMiden:next Jan 16, 2025
11 checks passed
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.

4 participants