pathfinder_common/
l1.rs

1use pathfinder_crypto::Felt;
2use primitive_types::H256;
3
4use crate::macros;
5
6/// An Ethereum block number.
7#[derive(Copy, Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct L1BlockNumber(u64);
9
10macros::fmt::thin_display!(L1BlockNumber);
11
12macros::i64_backed_u64::new_get_partialeq!(L1BlockNumber);
13macros::i64_backed_u64::serdes!(L1BlockNumber);
14
15impl From<L1BlockNumber> for Felt {
16    fn from(x: L1BlockNumber) -> Self {
17        Felt::from(x.0)
18    }
19}
20
21impl std::iter::Iterator for L1BlockNumber {
22    type Item = L1BlockNumber;
23
24    fn next(&mut self) -> Option<Self::Item> {
25        Some(*self + 1)
26    }
27}
28
29impl L1BlockNumber {
30    pub const GENESIS: L1BlockNumber = L1BlockNumber::new_or_panic(0);
31}
32
33impl std::ops::Add<u64> for L1BlockNumber {
34    type Output = L1BlockNumber;
35
36    fn add(self, rhs: u64) -> Self::Output {
37        Self(self.0 + rhs)
38    }
39}
40
41impl std::ops::AddAssign<u64> for L1BlockNumber {
42    fn add_assign(&mut self, rhs: u64) {
43        self.0 += rhs;
44    }
45}
46
47impl std::ops::Sub<u64> for L1BlockNumber {
48    type Output = L1BlockNumber;
49
50    fn sub(self, rhs: u64) -> Self::Output {
51        Self(self.0 - rhs)
52    }
53}
54
55impl std::ops::SubAssign<u64> for L1BlockNumber {
56    fn sub_assign(&mut self, rhs: u64) {
57        self.0 -= rhs;
58    }
59}
60
61/// An Ethereum transaction hash.
62#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub struct L1TransactionHash(H256);
64
65macros::fmt::thin_display!(L1TransactionHash);
66macros::fmt::thin_debug!(L1TransactionHash);
67
68impl L1TransactionHash {
69    /// Creates a new `L1TransactionHash` from a `H256`.
70    pub fn new(hash: H256) -> Self {
71        Self(hash)
72    }
73
74    /// Returns the raw bytes of the transaction hash.
75    pub fn as_bytes(&self) -> &[u8] {
76        self.0.as_bytes()
77    }
78
79    /// Creates a new `L1TransactionHash` from a slice of bytes.
80    ///
81    /// # Panics
82    ///
83    /// If the length of the byte slice is not 32.
84    pub fn from_slice(bytes: &[u8]) -> Self {
85        Self(H256::from_slice(bytes))
86    }
87}
88
89impl From<H256> for L1TransactionHash {
90    fn from(hash: H256) -> Self {
91        Self(hash)
92    }
93}
94
95impl From<L1TransactionHash> for H256 {
96    fn from(tx_hash: L1TransactionHash) -> Self {
97        tx_hash.0
98    }
99}
100
101impl From<[u8; 32]> for L1TransactionHash {
102    fn from(bytes: [u8; 32]) -> Self {
103        Self(H256::from(bytes))
104    }
105}
106
107impl AsRef<[u8]> for L1TransactionHash {
108    fn as_ref(&self) -> &[u8] {
109        self.0.as_ref()
110    }
111}