miden_objects/block/
block_number.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use core::{fmt, ops::Add};

use super::Felt;
use crate::utils::serde::{
    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};

// BLOCK NUMBER
// ================================================================================================

/// 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, Default, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)]
pub struct BlockNumber(u32);

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;

    /// The block height of the genesis block.
    pub const GENESIS: Self = Self(0);

    /// Returns the previous block number
    pub fn parent(self) -> Option<BlockNumber> {
        self.checked_sub(1)
    }

    /// Returns the next block number
    pub fn child(self) -> BlockNumber {
        self + 1
    }

    /// 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)
    }

    /// Creates a `BlockNumber` from a `usize`.
    pub fn from_usize(value: usize) -> Self {
        BlockNumber(value as u32)
    }

    /// Returns the epoch to which this block number belongs.
    pub const fn block_epoch(&self) -> u16 {
        (self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16
    }

    /// Returns the block number as a `u32`.
    pub fn as_u32(&self) -> u32 {
        self.0
    }

    /// Returns the block number as a `u64`.
    pub fn as_u64(&self) -> u64 {
        self.0 as u64
    }

    /// Returns the block number as a `usize`.
    pub fn as_usize(&self) -> usize {
        self.0 as usize
    }

    /// Checked integer subtraction. Computes `self - rhs`, returning `None` if underflow occurred.
    pub fn checked_sub(&self, rhs: u32) -> Option<Self> {
        self.0.checked_sub(rhs).map(Self)
    }
}

impl Add<u32> for BlockNumber {
    type Output = Self;

    fn add(self, other: u32) -> Self::Output {
        BlockNumber(self.0 + other)
    }
}

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>()
    }
}

impl Deserializable for BlockNumber {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        source.read::<u32>().map(BlockNumber::from)
    }
}

impl From<BlockNumber> for Felt {
    fn from(value: BlockNumber) -> Self {
        Felt::from(value.as_u32())
    }
}

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)
    }
}