superchain_primitives/
block.rs1use alloy_primitives::B256;
4use core::fmt::Display;
5
6#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
10pub struct BlockID {
11 pub hash: B256,
13 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
15 pub number: u64,
16}
17
18impl Display for BlockID {
19 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20 write!(
21 f,
22 "BlockID {{ hash: {}, number: {} }}",
23 self.hash, self.number
24 )
25 }
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31
32 #[test]
33 fn test_block_id_display() {
34 let block_id = BlockID {
35 hash: B256::from([0; 32]),
36 number: 0,
37 };
38 let expected = "BlockID { hash: 0x0000000000000000000000000000000000000000000000000000000000000000, number: 0 }";
39 assert_eq!(block_id.to_string(), expected);
40 }
41
42 #[test]
43 #[cfg(feature = "serde")]
44 fn block_id_serde() {
45 let block_id = BlockID {
46 hash: B256::from([1; 32]),
47 number: 1,
48 };
49
50 let block_id2: BlockID = serde_json::from_str(r#"{"hash":"0x0101010101010101010101010101010101010101010101010101010101010101","number":1}"#).unwrap();
51 assert_eq!(block_id, block_id2);
52 }
53
54 #[test]
55 #[cfg(feature = "serde")]
56 fn test_block_id_serde_with_hex() {
57 let block_id = BlockID {
58 hash: B256::from([1; 32]),
59 number: 1,
60 };
61
62 let json = serde_json::to_string(&block_id).unwrap();
63 assert_eq!(
64 json,
65 r#"{"hash":"0x0101010101010101010101010101010101010101010101010101010101010101","number":"0x1"}"#
66 );
67
68 let block_id2: BlockID = serde_json::from_str(&json).unwrap();
69 assert_eq!(block_id, block_id2);
70 }
71}