Skip to content

Commit

Permalink
feat: add qol stuff for block ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 30, 2024
1 parent 8c82294 commit 3c3e613
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/models/block/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,44 @@ pub struct BlockIdShort {
pub seqno: u32,
}

impl BlockIdShort {
/// Returns `true` if this block id is for a masterchain block.
///
/// See [`ShardIdent::MASTERCHAIN`]
#[inline]
pub const fn is_masterchain(&self) -> bool {
self.shard.is_masterchain()
}

/// Returns a previous block id.
pub fn saturating_prev(&self) -> Self {
self.saturating_sub(1)
}

/// Returns a next block id.
pub fn saturating_next(&self) -> Self {
self.saturating_add(1)
}

/// Returns a new block id with the seqno
/// increased at most by the specified value.
pub fn saturating_add(&self, seqno: u32) -> Self {
Self {
shard: self.shard,
seqno: self.seqno.saturating_add(seqno),
}
}

/// Returns a new block id with the seqno
/// decreased at most by the specified value.
pub fn saturating_sub(&self, seqno: u32) -> Self {
Self {
shard: self.shard,
seqno: self.seqno.saturating_sub(seqno),
}
}
}

impl std::fmt::Display for BlockIdShort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}:{}", self.shard, self.seqno))
Expand Down
10 changes: 10 additions & 0 deletions src/models/block/shard_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ impl ShardDescription {
const TAG_V4: u8 = 0xd;
#[cfg(feature = "venom")]
const TAG_V5: u8 = 0xe;

/// Converts a `ShardDescription` to a `BlockId` given a shard identifier.
pub fn as_block_id(&self, shard: ShardIdent) -> BlockId {
BlockId {
shard,
seqno: self.seqno,
root_hash: self.root_hash,
file_hash: self.file_hash,
}
}
}

impl Store for ShardDescription {
Expand Down

0 comments on commit 3c3e613

Please sign in to comment.