Skip to main content

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    pub fn starknet_version(&self) -> StarknetVersion {
113        match self {
114            L2BlockToCommit::FromConsensus(block) => block.header.starknet_version,
115            L2BlockToCommit::FromFgw(block) => block.header.starknet_version,
116        }
117    }
118}
119
120impl ConsensusFinalizedBlockHeader {
121    pub fn compute_hash(
122        self,
123        parent_hash: BlockHash,
124        state_commitment: StateCommitment,
125        block_hash_fn: impl Fn(&BlockHeader) -> BlockHash,
126    ) -> BlockHeader {
127        let mut header = BlockHeader {
128            // Intentionally set to zero, will be computed later.
129            hash: BlockHash::ZERO,
130            parent_hash,
131            number: self.number,
132            timestamp: self.timestamp,
133            eth_l1_gas_price: self.eth_l1_gas_price,
134            strk_l1_gas_price: self.strk_l1_gas_price,
135            eth_l1_data_gas_price: self.eth_l1_data_gas_price,
136            strk_l1_data_gas_price: self.strk_l1_data_gas_price,
137            eth_l2_gas_price: self.eth_l2_gas_price,
138            strk_l2_gas_price: self.strk_l2_gas_price,
139            sequencer_address: self.sequencer_address,
140            starknet_version: self.starknet_version,
141            event_commitment: self.event_commitment,
142            state_commitment,
143            transaction_commitment: self.transaction_commitment,
144            transaction_count: self.transaction_count,
145            event_count: self.event_count,
146            l1_da_mode: self.l1_da_mode,
147            receipt_commitment: self.receipt_commitment,
148            state_diff_commitment: self.state_diff_commitment,
149            state_diff_length: self.state_diff_length,
150        };
151        header.hash = block_hash_fn(&header);
152        header
153    }
154}