Skip to main content

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(
97            storage_commitment,
98            class_commitment,
99            self.0.starknet_version,
100        );
101        self
102    }
103
104    pub fn timestamp(mut self, timestamp: BlockTimestamp) -> Self {
105        self.0.timestamp = timestamp;
106        self
107    }
108
109    pub fn eth_l1_gas_price(mut self, eth_l1_gas_price: GasPrice) -> Self {
110        self.0.eth_l1_gas_price = eth_l1_gas_price;
111        self
112    }
113
114    pub fn strk_l1_gas_price(mut self, strk_l1_gas_price: GasPrice) -> Self {
115        self.0.strk_l1_gas_price = strk_l1_gas_price;
116        self
117    }
118
119    pub fn eth_l2_gas_price(mut self, eth_l2_gas_price: GasPrice) -> Self {
120        self.0.eth_l2_gas_price = eth_l2_gas_price;
121        self
122    }
123
124    pub fn strk_l2_gas_price(mut self, strk_l2_gas_price: GasPrice) -> Self {
125        self.0.strk_l2_gas_price = strk_l2_gas_price;
126        self
127    }
128
129    pub fn eth_l1_data_gas_price(mut self, eth_l1_data_gas_price: GasPrice) -> Self {
130        self.0.eth_l1_data_gas_price = eth_l1_data_gas_price;
131        self
132    }
133
134    pub fn strk_l1_data_gas_price(mut self, strk_l1_data_gas_price: GasPrice) -> Self {
135        self.0.strk_l1_data_gas_price = strk_l1_data_gas_price;
136        self
137    }
138
139    pub fn sequencer_address(mut self, sequencer_address: SequencerAddress) -> Self {
140        self.0.sequencer_address = sequencer_address;
141        self
142    }
143
144    pub fn transaction_commitment(mut self, transaction_commitment: TransactionCommitment) -> Self {
145        self.0.transaction_commitment = transaction_commitment;
146        self
147    }
148
149    pub fn event_commitment(mut self, event_commitment: EventCommitment) -> Self {
150        self.0.event_commitment = event_commitment;
151        self
152    }
153
154    pub fn starknet_version(mut self, starknet_version: StarknetVersion) -> Self {
155        self.0.starknet_version = starknet_version;
156        self
157    }
158
159    pub fn transaction_count(mut self, transaction_count: usize) -> Self {
160        self.0.transaction_count = transaction_count;
161        self
162    }
163
164    pub fn event_count(mut self, event_count: usize) -> Self {
165        self.0.event_count = event_count;
166        self
167    }
168
169    pub fn l1_da_mode(mut self, l1_da_mode: L1DataAvailabilityMode) -> Self {
170        self.0.l1_da_mode = l1_da_mode;
171        self
172    }
173
174    pub fn receipt_commitment(mut self, receipt_commitment: ReceiptCommitment) -> Self {
175        self.0.receipt_commitment = receipt_commitment;
176        self
177    }
178
179    pub fn state_diff_commitment(mut self, state_diff_commitment: StateDiffCommitment) -> Self {
180        self.0.state_diff_commitment = state_diff_commitment;
181        self
182    }
183
184    pub fn state_diff_length(mut self, state_diff_length: u64) -> Self {
185        self.0.state_diff_length = state_diff_length;
186        self
187    }
188
189    pub fn finalize_with_hash(mut self, hash: BlockHash) -> BlockHeader {
190        self.0.hash = hash;
191        self.0
192    }
193}