pathfinder_common/
header.rs

1use fake::Dummy;
2
3use crate::prelude::*;
4use crate::{BlockCommitmentSignature, ReceiptCommitment, StateDiffCommitment};
5
6#[derive(Debug, Clone, PartialEq, Eq, Default, Dummy)]
7pub struct BlockHeader {
8    pub hash: BlockHash,
9    pub parent_hash: BlockHash,
10    pub number: BlockNumber,
11    pub timestamp: BlockTimestamp,
12    pub eth_l1_gas_price: GasPrice,
13    pub strk_l1_gas_price: GasPrice,
14    pub eth_l1_data_gas_price: GasPrice,
15    pub strk_l1_data_gas_price: GasPrice,
16    pub eth_l2_gas_price: GasPrice,
17    pub strk_l2_gas_price: GasPrice,
18    pub sequencer_address: SequencerAddress,
19    pub starknet_version: StarknetVersion,
20    pub event_commitment: EventCommitment,
21    pub state_commitment: StateCommitment,
22    pub transaction_commitment: TransactionCommitment,
23    pub transaction_count: usize,
24    pub event_count: usize,
25    pub l1_da_mode: L1DataAvailabilityMode,
26    pub receipt_commitment: ReceiptCommitment,
27    pub state_diff_commitment: StateDiffCommitment,
28    pub state_diff_length: u64,
29}
30
31#[derive(
32    Debug, Copy, Clone, PartialEq, Eq, Default, Dummy, serde::Serialize, serde::Deserialize,
33)]
34#[serde(rename_all = "UPPERCASE")]
35pub enum L1DataAvailabilityMode {
36    #[default]
37    Calldata,
38    Blob,
39}
40
41#[derive(Debug, Clone, PartialEq, Default, Dummy)]
42pub struct SignedBlockHeader {
43    pub header: BlockHeader,
44    pub signature: BlockCommitmentSignature,
45}
46
47pub struct BlockHeaderBuilder(BlockHeader);
48
49impl BlockHeader {
50    /// Creates a [builder](BlockHeaderBuilder) with all fields initialized to
51    /// default values.
52    pub fn builder() -> BlockHeaderBuilder {
53        BlockHeaderBuilder(BlockHeader::default())
54    }
55
56    /// Creates a [builder](BlockHeaderBuilder) with an incremented block number
57    /// and parent hash set to this block's hash.
58    pub fn child_builder(&self) -> BlockHeaderBuilder {
59        BlockHeaderBuilder(BlockHeader::default())
60            .number(self.number + 1)
61            .parent_hash(self.hash)
62    }
63
64    /// Creates a [StateUpdate] with the block hash and state commitment fields
65    /// initialized to match this header.
66    pub fn init_state_update(&self) -> StateUpdate {
67        StateUpdate::default()
68            .with_block_hash(self.hash)
69            .with_state_commitment(self.state_commitment)
70    }
71}
72
73impl BlockHeaderBuilder {
74    pub fn number(mut self, number: BlockNumber) -> Self {
75        self.0.number = number;
76        self
77    }
78
79    pub fn parent_hash(mut self, parent_hash: BlockHash) -> Self {
80        self.0.parent_hash = parent_hash;
81        self
82    }
83
84    pub fn state_commitment(mut self, state_commitment: StateCommitment) -> Self {
85        self.0.state_commitment = state_commitment;
86        self
87    }
88
89    /// Sets the [StateCommitment] by calculating its value from the passed
90    /// [StorageCommitment] and [ClassCommitment].
91    pub fn calculated_state_commitment(
92        mut self,
93        storage_commitment: StorageCommitment,
94        class_commitment: ClassCommitment,
95    ) -> Self {
96        self.0.state_commitment = StateCommitment::calculate(storage_commitment, class_commitment);
97        self
98    }
99
100    pub fn timestamp(mut self, timestamp: BlockTimestamp) -> Self {
101        self.0.timestamp = timestamp;
102        self
103    }
104
105    pub fn eth_l1_gas_price(mut self, eth_l1_gas_price: GasPrice) -> Self {
106        self.0.eth_l1_gas_price = eth_l1_gas_price;
107        self
108    }
109
110    pub fn strk_l1_gas_price(mut self, strk_l1_gas_price: GasPrice) -> Self {
111        self.0.strk_l1_gas_price = strk_l1_gas_price;
112        self
113    }
114
115    pub fn eth_l2_gas_price(mut self, eth_l2_gas_price: GasPrice) -> Self {
116        self.0.eth_l2_gas_price = eth_l2_gas_price;
117        self
118    }
119
120    pub fn strk_l2_gas_price(mut self, strk_l2_gas_price: GasPrice) -> Self {
121        self.0.strk_l2_gas_price = strk_l2_gas_price;
122        self
123    }
124
125    pub fn eth_l1_data_gas_price(mut self, eth_l1_data_gas_price: GasPrice) -> Self {
126        self.0.eth_l1_data_gas_price = eth_l1_data_gas_price;
127        self
128    }
129
130    pub fn strk_l1_data_gas_price(mut self, strk_l1_data_gas_price: GasPrice) -> Self {
131        self.0.strk_l1_data_gas_price = strk_l1_data_gas_price;
132        self
133    }
134
135    pub fn sequencer_address(mut self, sequencer_address: SequencerAddress) -> Self {
136        self.0.sequencer_address = sequencer_address;
137        self
138    }
139
140    pub fn transaction_commitment(mut self, transaction_commitment: TransactionCommitment) -> Self {
141        self.0.transaction_commitment = transaction_commitment;
142        self
143    }
144
145    pub fn event_commitment(mut self, event_commitment: EventCommitment) -> Self {
146        self.0.event_commitment = event_commitment;
147        self
148    }
149
150    pub fn starknet_version(mut self, starknet_version: StarknetVersion) -> Self {
151        self.0.starknet_version = starknet_version;
152        self
153    }
154
155    pub fn transaction_count(mut self, transaction_count: usize) -> Self {
156        self.0.transaction_count = transaction_count;
157        self
158    }
159
160    pub fn event_count(mut self, event_count: usize) -> Self {
161        self.0.event_count = event_count;
162        self
163    }
164
165    pub fn l1_da_mode(mut self, l1_da_mode: L1DataAvailabilityMode) -> Self {
166        self.0.l1_da_mode = l1_da_mode;
167        self
168    }
169
170    pub fn receipt_commitment(mut self, receipt_commitment: ReceiptCommitment) -> Self {
171        self.0.receipt_commitment = receipt_commitment;
172        self
173    }
174
175    pub fn finalize_with_hash(mut self, hash: BlockHash) -> BlockHeader {
176        self.0.hash = hash;
177        self.0
178    }
179}