-
Notifications
You must be signed in to change notification settings - Fork 54
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
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!
0358d5d
to
e43148c
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.
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?
@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. |
note_block_num.as_u32(), | ||
self.blocks.get(note_block_num.as_usize()).unwrap().header(), |
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.
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); |
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'd recommend adding a BlockNumber::next
or similar method to increment by one.
objects/src/block/header.rs
Outdated
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)] | ||
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.
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.
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.
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
objects/src/transaction/inputs.rs
Outdated
@@ -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() { |
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.
Should chain length not be a block number?
@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? |
3fcd0fc
to
5c1c71d
Compare
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 @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 |
5c1c71d
to
3fcd0fc
Compare
@varun-doshi No worries at all, we appreciate the contribution 😃! |
@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. |
e17b085
to
7bda320
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.
Looks good to me!
Just BlockNumber::parent
should be changed (see inline comment).
objects/src/block/block_number.rs
Outdated
pub fn parent(&self) -> u32 { | ||
self.checked_sub(1).expect("Cannot go below Genesis block!").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.
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
.
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.
Looks great! Thank you for working on this. I left a couple of optional comments inline.
objects/src/block/block_number.rs
Outdated
/// 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> { |
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.
These should probably return Option<BlockNumber>
, same with fn child
.
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.
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.
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.
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.
5b15a79
to
4c67afe
Compare
4c67afe
to
6cc6b76
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.
All looks good! Thank you again for working on this!
Ref #1040