-
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
Open
varun-doshi
wants to merge
4
commits into
0xPolygonMiden:next
Choose a base branch
from
varun-doshi:varun/blocknumber-struct
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||||||||||
use alloc::vec::Vec; | ||||||||||||||||
use core::fmt; | ||||||||||||||||
|
||||||||||||||||
use super::{Digest, Felt, Hasher, ZERO}; | ||||||||||||||||
use crate::utils::serde::{ | ||||||||||||||||
|
@@ -29,7 +30,7 @@ use crate::utils::serde::{ | |||||||||||||||
pub struct BlockHeader { | ||||||||||||||||
version: u32, | ||||||||||||||||
prev_hash: Digest, | ||||||||||||||||
block_num: u32, | ||||||||||||||||
block_num: BlockNumber, | ||||||||||||||||
chain_root: Digest, | ||||||||||||||||
account_root: Digest, | ||||||||||||||||
nullifier_root: Digest, | ||||||||||||||||
|
@@ -43,13 +44,6 @@ pub struct BlockHeader { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
impl BlockHeader { | ||||||||||||||||
/// 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; | ||||||||||||||||
|
||||||||||||||||
/// Creates a new block header. | ||||||||||||||||
#[allow(clippy::too_many_arguments)] | ||||||||||||||||
pub fn new( | ||||||||||||||||
|
@@ -88,7 +82,7 @@ impl BlockHeader { | |||||||||||||||
Self { | ||||||||||||||||
version, | ||||||||||||||||
prev_hash, | ||||||||||||||||
block_num, | ||||||||||||||||
block_num: block_num.into(), | ||||||||||||||||
chain_root, | ||||||||||||||||
account_root, | ||||||||||||||||
nullifier_root, | ||||||||||||||||
|
@@ -129,15 +123,15 @@ impl BlockHeader { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Returns the block number. | ||||||||||||||||
pub fn block_num(&self) -> u32 { | ||||||||||||||||
pub fn block_num(&self) -> BlockNumber { | ||||||||||||||||
self.block_num | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Returns the epoch to which this block belongs. | ||||||||||||||||
/// | ||||||||||||||||
/// This is the block number shifted right by [`Self::EPOCH_LENGTH_EXPONENT`]. | ||||||||||||||||
/// This is the block number shifted right by [`BlockNumber::EPOCH_LENGTH_EXPONENT`]. | ||||||||||||||||
pub fn block_epoch(&self) -> u16 { | ||||||||||||||||
block_epoch_from_number(self.block_num) | ||||||||||||||||
self.block_num.block_epoch() | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Returns the chain root. | ||||||||||||||||
|
@@ -187,8 +181,8 @@ impl BlockHeader { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Returns the block number of the epoch block to which this block belongs. | ||||||||||||||||
pub fn epoch_block_num(&self) -> u32 { | ||||||||||||||||
block_num_from_epoch(self.block_epoch()) | ||||||||||||||||
pub fn epoch_block_num(&self) -> BlockNumber { | ||||||||||||||||
BlockNumber::from_epoch(self.block_epoch()) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// HELPERS | ||||||||||||||||
|
@@ -275,17 +269,58 @@ impl Deserializable for BlockHeader { | |||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// UTILITIES | ||||||||||||||||
// ================================================================================================ | ||||||||||||||||
/// BLOCK NUMBER | ||||||||||||||||
|
||||||||||||||||
/// 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 | ||||||||||||||||
/// 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); | ||||||||||||||||
|
||||||||||||||||
impl Serializable for BlockNumber { | ||||||||||||||||
fn write_into<W: ByteWriter>(&self, target: &mut W) { | ||||||||||||||||
target.write_u32(self.0); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
fn get_size_hint(&self) -> usize { | ||||||||||||||||
core::mem::size_of::<u32>() | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// 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 | ||||||||||||||||
impl From<u32> for BlockNumber { | ||||||||||||||||
fn from(value: u32) -> Self { | ||||||||||||||||
BlockNumber(value) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
impl fmt::Display for BlockNumber { | ||||||||||||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||||||||||||||
write!(f, "{}", self.0) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
impl BlockNumber { | ||||||||||||||||
/// 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; | ||||||||||||||||
|
||||||||||||||||
/// 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) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Returns the epoch to which this block number belongs. | ||||||||||||||||
pub const fn block_epoch(&self) -> u16 { | ||||||||||||||||
(self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16 | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+316
to
+318
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Public methods should be documented. |
||||||||||||||||
|
||||||||||||||||
/// Returns the block number as a `u32`. | ||||||||||||||||
pub fn as_u32(&self) -> u32 { | ||||||||||||||||
self.0 | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+321
to
+323
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Public methods should be documented. |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
#[cfg(test)] | ||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 docs were moved incompletely. Can you remove them from the
impl BlockHeader
block and move them here?