//! Block execution for Morph L2. //! //! This module provides block execution functionality for Morph, including: //! - [`MorphBlockExecutor`]: The main block executor //! - [`MorphBlockExecutorFactory`]: Factory for creating block executors //! - [`MorphReceiptBuilder`]: Receipt construction for transactions mod factory; mod receipt; pub(crate) use factory::MorphBlockExecutorFactory; pub(crate) use receipt::{ DefaultMorphReceiptBuilder, MorphReceiptBuilder, MorphReceiptBuilderCtx, MorphReceiptTxFields, }; use crate::evm::MorphEvm; use alloy_consensus::Transaction; use alloy_consensus::transaction::TxHashRef; use alloy_evm::{ Database, Evm, RecoveredTx, block::{ BlockExecutionError, BlockExecutionResult, BlockExecutor, ExecutableTx, GasOutput, TxResult, }, }; use alloy_primitives::{Address, U256}; use morph_primitives::{MorphReceipt, MorphTxEnvelope}; use morph_revm::{L1_GAS_PRICE_ORACLE_ADDRESS, MorphHaltReason, TokenFeeInfo, evm::MorphContext}; use reth_primitives_traits::Recovered; use reth_revm::{DatabaseCommit, Inspector, context::result::ResultAndState}; use revm::context::Block; /// The result of executing a Morph transaction. /// /// Carries the EVM result together with the recovered transaction and cached fee /// information that are needed during [`MorphBlockExecutor::commit_transaction`]. pub struct MorphTxResult { /// The raw EVM execution result and state diff. pub result: ResultAndState, /// Recovered transaction (consensus tx + signer). pub recovered: Recovered, /// L1 data fee read from the handler cache immediately after execution. pub l1_fee: U256, } impl TxResult for MorphTxResult { type HaltReason = MorphHaltReason; fn result(&self) -> &ResultAndState { &self.result } fn into_result(self) -> ResultAndState { self.result } } /// Block executor for Morph L2 blocks. /// /// This executor handles Morph-specific block execution logic, differing from /// standard Ethereum execution in several key ways: /// /// ## L1 Fee Calculation /// All L2 transactions (except L1 messages) must pay an L1 data fee for posting /// transaction data to L1. This fee is calculated based on: /// - The RLP-encoded transaction size /// - Current L1 gas price from the L1 Gas Price Oracle contract /// - Hardfork-specific fee calculation logic (pre-Curie vs post-Curie) /// /// ## Token Fee Support (MorphTx 0x7F) /// MorphTx transactions allow users to pay gas fees using ERC20 tokens. /// The executor extracts token fee information from the L2TokenRegistry contract, /// including exchange rate and scale factor. /// /// ## Execution Flow /// 1. `apply_pre_execution_changes`: Set up state and load contracts /// 2. `execute_transaction_without_commit`: Execute transaction in EVM /// 3. `commit_transaction`: Build receipt from the cached fee info, commit state /// 4. `finish`: Return final execution result with all receipts pub struct MorphBlockExecutor { /// The EVM used by executor (owned, not a reference) evm: MorphEvm, /// Receipt builder receipt_builder: DefaultMorphReceiptBuilder, /// Receipts of executed transactions receipts: Vec, /// Total gas used by executed transactions gas_used: u64, /// Gas unavailable to later transactions in this block. /// /// Unlike receipt gas, L1 messages reserve their full gas limit because Morph geth does not /// return their unused gas to the block gas pool. gas_pool_used: u64, } impl MorphBlockExecutor where DB: Database, I: Inspector>, { /// Creates a new [`MorphBlockExecutor`]. /// /// # Arguments /// * `evm` - The EVM instance configured for Morph execution /// * `receipt_builder` - Builder for constructing transaction receipts pub(crate) fn new(evm: MorphEvm, receipt_builder: DefaultMorphReceiptBuilder) -> Self { Self { evm, receipt_builder, receipts: Vec::new(), gas_used: 0, gas_pool_used: 0, } } /// Extract MorphTx-specific fields for MorphTx (0x7F) transactions. /// /// MorphTx transactions include: /// - Token fee information (when using ERC20 for gas payment) /// - Transaction metadata (version, reference, memo) fn get_morph_tx_fields( &mut self, tx: &MorphTxEnvelope, sender: Address, ) -> Result, BlockExecutionError> { if !tx.is_morph_tx() { return Ok(None); } let fee_token_id = tx .fee_token_id() .ok_or_else(|| BlockExecutionError::msg("MorphTx missing fee_token_id"))?; let fee_limit = tx .fee_limit() .ok_or_else(|| BlockExecutionError::msg("MorphTx missing fee_limit"))?; let version = tx.version().unwrap_or(0); let reference = tx.reference(); let memo = tx.memo().cloned(); if fee_token_id == 0 { return Ok(Some(MorphReceiptTxFields { version, fee_token_id: 0, fee_rate: None, token_scale: None, fee_limit, reference, memo, })); } let token_info = match self.evm.cached_token_fee_info() { Some(info) => Some(info), // Only `price_ratio` and `scale` are read below, and both come straight from // registry storage. `load_storage_only` reads exactly that and never builds a // temporary EVM to resolve a balance this receipt has no use for. None => TokenFeeInfo::load_storage_only(self.evm.db_mut(), fee_token_id, sender) .map_err(|e| { BlockExecutionError::msg(format!("Failed to fetch token fee info: {e:?}")) })?, }; Ok(token_info.map(|info| MorphReceiptTxFields { version, fee_token_id, fee_rate: Some(info.price_ratio), token_scale: Some(info.scale), fee_limit, reference, memo, })) } } impl BlockExecutor for MorphBlockExecutor where DB: Database + DatabaseCommit, I: Inspector>, { type Transaction = MorphTxEnvelope; type Receipt = MorphReceipt; type Evm = MorphEvm; type Result = MorphTxResult; /// Applies pre-execution state changes before processing transactions. /// /// This method performs initialization required before executing any transactions: /// /// 1. **L1 Gas Oracle Cache**: Loads the L1 Gas Price Oracle contract into the /// account cache to optimize L1 fee calculations for all transactions /// /// # Errors /// Returns error if: /// - L1 Gas Price Oracle account cannot be loaded fn apply_pre_execution_changes(&mut self) -> Result<(), BlockExecutionError> { // Pre-warm the L1 gas oracle contract in the underlying DB cache so that // subsequent per-tx L1BlockInfo reads in the handler are fast. let _ = self .evm .db_mut() .basic(L1_GAS_PRICE_ORACLE_ADDRESS) .map_err(BlockExecutionError::other)?; Ok(()) } fn execute_transaction_without_commit( &mut self, tx: impl ExecutableTx, ) -> Result { let (tx_env, recovered) = tx.into_parts(); // Validate gas limit against the geth-compatible block gas pool. Receipt gas cannot be // used here: L1 messages report their actual execution cost in receipts but reserve their // full gas limit for block packing. let block_available_gas = self .evm .block() .gas_limit() .saturating_sub(self.gas_pool_used); if recovered.tx().gas_limit() > block_available_gas { return Err(BlockExecutionError::msg(format!( "transaction gas limit {} exceeds block available gas {}", recovered.tx().gas_limit(), block_available_gas ))); } // Clone the consensus tx and signer BEFORE transact (since we can't move out later). let consensus_tx = recovered.tx().clone(); let signer = *recovered.signer(); // Execute the transaction let result = self .evm .transact(tx_env) .map_err(|err| BlockExecutionError::evm(err, *consensus_tx.tx_hash()))?; // Read caches from the EVM immediately after execution, before the next tx resets them. let l1_fee = self.evm.cached_l1_data_fee(); Ok(MorphTxResult { result, recovered: Recovered::new_unchecked(consensus_tx, signer), l1_fee, }) } fn commit_transaction(&mut self, output: Self::Result) -> GasOutput { let MorphTxResult { result: ResultAndState { result, state }, recovered, l1_fee, } = output; // EIP-8037 separates regular and state gas; pre-Amsterdam morph treats // them as a single number, so use the unified `tx_gas_used` getter. let gas_used = result.gas().tx_gas_used(); self.gas_used += gas_used; // Morph geth's L1-message path deducts the full transaction gas limit from GasPool and // deliberately skips the refund path. Regular transactions return unused gas, so their // net gas-pool charge is the actual gas used. self.gas_pool_used += if recovered.tx().is_l1_msg() { recovered.tx().gas_limit() } else { gas_used }; // Get MorphTx-specific fields using the recovered transaction. Errors here // are tracing-only — the trait API no longer permits us to surface errors // from `commit_transaction`. let (tx, signer) = recovered.into_parts(); let morph_tx_fields = match self.get_morph_tx_fields(&tx, signer) { Ok(fields) => fields, Err(err) => { tracing::error!( target: "morph::evm", %err, "failed to load MorphTx receipt fields; emitting receipt without them" ); None } }; // Build receipt. let ctx: MorphReceiptBuilderCtx<'_, Self::Evm> = MorphReceiptBuilderCtx { tx: &tx, result, cumulative_gas_used: self.gas_used, l1_fee, morph_tx_fields, }; self.receipts.push(self.receipt_builder.build_receipt(ctx)); // Commit state changes self.evm.db_mut().commit(state); // Morph is pre-EIP-8037, so all gas is regular gas (no state-gas tracking). GasOutput::new(gas_used) } fn finish( self, ) -> Result<(Self::Evm, BlockExecutionResult), BlockExecutionError> { Ok(( self.evm, BlockExecutionResult { receipts: self.receipts, requests: Default::default(), gas_used: self.gas_used, blob_gas_used: 0, }, )) } fn evm_mut(&mut self) -> &mut Self::Evm { &mut self.evm } fn evm(&self) -> &Self::Evm { &self.evm } fn receipts(&self) -> &[Self::Receipt] { &self.receipts } }