pathfinder_common/
l2.rs

1use fake::Dummy;
2
3use crate::event::Event;
4use crate::receipt::Receipt;
5use crate::state_update::StateUpdateData;
6use crate::transaction::Transaction;
7use crate::{
8    BlockHash,
9    BlockHeader,
10    BlockNumber,
11    BlockTimestamp,
12    EventCommitment,
13    GasPrice,
14    L1DataAvailabilityMode,
15    ReceiptCommitment,
16    SequencerAddress,
17    StarknetVersion,
18    StateCommitment,
19    StateDiffCommitment,
20    TransactionCommitment,
21};
22
23pub enum L2BlockToCommit {
24    FromConsensus(ConsensusFinalizedL2Block),
25    FromFgw(L2Block),
26}
27
28#[derive(Clone, Debug, Default)]
29pub struct L2Block {
30    pub header: BlockHeader,
31    pub state_update: StateUpdateData,
32    pub transactions_and_receipts: Vec<(Transaction, Receipt)>,
33    pub events: Vec<Vec<Event>>,
34}
35
36/// An [L2Block] that is the result of executing a consensus proposal. The only
37/// differences from an [L2Block] are:
38/// - the state tries have not been updated yet,
39/// - in consequence the block hash could not have been computed yet.
40#[derive(Clone, Debug, Default)]
41pub struct ConsensusFinalizedL2Block {
42    pub header: ConsensusFinalizedBlockHeader,
43    pub state_update: StateUpdateData,
44    pub transactions_and_receipts: Vec<(Transaction, Receipt)>,
45    pub events: Vec<Vec<Event>>,
46}
47
48/// An L2 [BlockHeader] that is the result of executing a consensus proposal
49/// that was decided upon. The only differences from a [BlockHeader] are:
50/// - the state tries have not been updated yet, so the state commitment is
51///   missing,
52/// - in consequence the block hash could not have been computed yet,
53/// - parent hash is updated when the header is transformed into a full
54///   [BlockHeader] to avoid additional DB lookup in consensus.
55
56#[derive(Debug, Clone, Default, PartialEq, Eq, Dummy)]
57pub struct ConsensusFinalizedBlockHeader {
58    pub number: BlockNumber,
59    pub timestamp: BlockTimestamp,
60    pub eth_l1_gas_price: GasPrice,
61    pub strk_l1_gas_price: GasPrice,
62    pub eth_l1_data_gas_price: GasPrice,
63    pub strk_l1_data_gas_price: GasPrice,
64    pub eth_l2_gas_price: GasPrice,
65    pub strk_l2_gas_price: GasPrice,
66    pub sequencer_address: SequencerAddress,
67    pub starknet_version: StarknetVersion,
68    pub event_commitment: EventCommitment,
69    pub transaction_commitment: TransactionCommitment,
70    pub transaction_count: usize,
71    pub event_count: usize,
72    pub l1_da_mode: L1DataAvailabilityMode,
73    pub receipt_commitment: ReceiptCommitment,
74    pub state_diff_commitment: StateDiffCommitment,
75    pub state_diff_length: u64,
76}
77
78impl From<L2Block> for L2BlockToCommit {
79    fn from(block: L2Block) -> Self {
80        L2BlockToCommit::FromFgw(block)
81    }
82}
83
84impl From<ConsensusFinalizedL2Block> for L2BlockToCommit {
85    fn from(block: ConsensusFinalizedL2Block) -> Self {
86        L2BlockToCommit::FromConsensus(block)
87    }
88}
89
90impl L2BlockToCommit {
91    pub fn number(&self) -> BlockNumber {
92        match self {
93            L2BlockToCommit::FromConsensus(block) => block.header.number,
94            L2BlockToCommit::FromFgw(block) => block.header.number,
95        }
96    }
97
98    pub fn state_commitment(&self) -> Option<StateCommitment> {
99        match self {
100            L2BlockToCommit::FromConsensus(_) => None,
101            L2BlockToCommit::FromFgw(block) => Some(block.header.state_commitment),
102        }
103    }
104
105    pub fn state_update(&self) -> &StateUpdateData {
106        match self {
107            L2BlockToCommit::FromConsensus(block) => &block.state_update,
108            L2BlockToCommit::FromFgw(block) => &block.state_update,
109        }
110    }
111}
112
113impl ConsensusFinalizedBlockHeader {
114    pub fn compute_hash(
115        self,
116        parent_hash: BlockHash,
117        state_commitment: StateCommitment,
118        block_hash_fn: impl Fn(&BlockHeader) -> BlockHash,
119    ) -> BlockHeader {
120        let mut header = BlockHeader {
121            // Intentionally set to zero, will be computed later.
122            hash: BlockHash::ZERO,
123            parent_hash,
124            number: self.number,
125            timestamp: self.timestamp,
126            eth_l1_gas_price: self.eth_l1_gas_price,
127            strk_l1_gas_price: self.strk_l1_gas_price,
128            eth_l1_data_gas_price: self.eth_l1_data_gas_price,
129            strk_l1_data_gas_price: self.strk_l1_data_gas_price,
130            eth_l2_gas_price: self.eth_l2_gas_price,
131            strk_l2_gas_price: self.strk_l2_gas_price,
132            sequencer_address: self.sequencer_address,
133            starknet_version: self.starknet_version,
134            event_commitment: self.event_commitment,
135            state_commitment,
136            transaction_commitment: self.transaction_commitment,
137            transaction_count: self.transaction_count,
138            event_count: self.event_count,
139            l1_da_mode: self.l1_da_mode,
140            receipt_commitment: self.receipt_commitment,
141            state_diff_commitment: self.state_diff_commitment,
142            state_diff_length: self.state_diff_length,
143        };
144        header.hash = block_hash_fn(&header);
145        header
146    }
147}