//! Taiko block executor integrating anchor pre-execution and tx filtering. #[cfg(feature = "prover")] use alloy_consensus::transaction::Recovered; use alloy_consensus::{Transaction, TransactionEnvelope, TxReceipt}; use alloy_eips::{Encodable2718, eip7685::Requests}; use alloy_evm::{ FromRecoveredTx, FromTxWithEncoded, RecoveredTx, block::GasOutput, eth::{EthTxResult, receipt_builder::ReceiptBuilder}, }; use alloy_primitives::{Address, Bytes, Log, U256, Uint}; use reth_evm::{ Evm, block::{ BlockExecutionError, BlockExecutor, BlockValidationError, CommitChanges, ExecutableTx, InternalBlockExecutionError, StateDB, SystemCaller, }, eth::receipt_builder::ReceiptBuilderCtx, }; use reth_execution_types::BlockExecutionResult; use reth_revm::context::{Block as _, result::ResultAndState}; use revm_database_interface::{Database, DatabaseCommit}; use crate::factory::TaikoBlockExecutionCtx; use alethia_reth_chainspec::spec::TaikoExecutorSpec; use alethia_reth_evm::{ alloy::{TAIKO_GOLDEN_TOUCH_ADDRESS, TaikoAnchorEvm, TaikoZkGasEvm}, handler::get_treasury_address, zk_gas::{adapter::ZK_GAS_LIMIT_ERR, meter::ZkGasOutcome}, }; use alethia_reth_primitives::decode_shasta_basefee_sharing_pctg; /// Block execution artifacts for transactions that were accepted by prover filtering. #[cfg(feature = "prover")] #[derive(Debug)] pub struct CommittedBlockExecutionOutcome { /// Execution result after applying all committed transactions. pub execution_result: BlockExecutionResult, /// Transactions that were accepted by the executor and included in execution. pub committed_transactions: Vec>, } /// Dedicated block-execution error raised when a block hits the zk gas limit. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ZkGasLimitExceeded; impl std::fmt::Display for ZkGasLimitExceeded { /// Formats the dedicated zk gas limit error message. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(ZK_GAS_LIMIT_ERR) } } impl std::error::Error for ZkGasLimitExceeded {} /// Dedicated block-execution error raised when imported `header.difficulty` does not match /// the recomputed finalized block zk gas. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ZkGasDifficultyMismatch { /// Difficulty carried by the imported block header. pub expected: U256, /// Finalized block zk gas recomputed during execution. pub got: U256, } impl std::fmt::Display for ZkGasDifficultyMismatch { /// Formats the dedicated zk gas difficulty mismatch message. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "zk gas header difficulty mismatch: expected {}, got {}", self.expected, self.got) } } impl std::error::Error for ZkGasDifficultyMismatch {} /// Returns `true` when `error` represents the dedicated zk gas truncation condition. pub fn is_zk_gas_limit_exceeded(error: &BlockExecutionError) -> bool { match error { BlockExecutionError::Internal(err) => err.is_other::(), _ => false, } } /// Returns `true` when `error` represents a zk gas difficulty commitment mismatch. pub fn is_zk_gas_difficulty_mismatch(error: &BlockExecutionError) -> bool { match error { BlockExecutionError::Internal(err) => err.is_other::(), _ => false, } } /// Returns `true` when `error` is the kind of failure that prover-style execution tolerates for a /// non-anchor transaction by skipping it: zk gas truncation, an invalid transaction, or a gas limit /// exceeding the block's remaining gas. /// /// This is the single definition of the recoverable error set, shared by the prover executor's /// `try_execute_filtered` and the tx-list witness debug RPC, so the two cannot drift when a new /// recoverable variant is added. Callers remain responsible for never applying it to the anchor /// transaction, which must always be fatal. pub fn is_recoverable_non_anchor_tx_error(error: &BlockExecutionError) -> bool { is_zk_gas_limit_exceeded(error) || matches!( error, BlockExecutionError::Validation( BlockValidationError::InvalidTx { .. } | BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { .. } ) ) } /// Block executor for Taiko network. pub struct TaikoBlockExecutor<'a, Evm, Spec, R: ReceiptBuilder> { /// Reference to the specification object. spec: Spec, /// Context for block execution. pub ctx: TaikoBlockExecutionCtx<'a>, /// Inner EVM. evm: Evm, /// Utility to call system smart contracts. system_caller: SystemCaller, /// Receipt builder. receipt_builder: R, /// Receipts of executed transactions. receipts: Vec, /// Total gas used by transactions in this block. gas_used: u64, /// Flag indicating that zk gas exhausted the block and later transactions must not run. zk_gas_exhausted: bool, /// Flag indicating whether the executor has been initialized with the anchor transaction info /// in `apply_pre_execution_changes`. evm_extra_execution_ctx_initialized: bool, } impl<'a, Evm, Spec, R> TaikoBlockExecutor<'a, Evm, Spec, R> where Spec: Clone, R: ReceiptBuilder, { /// Creates a new [`TaikoBlockExecutor`] pub fn new( mut evm: Evm, ctx: TaikoBlockExecutionCtx<'a>, spec: Spec, receipt_builder: R, ) -> Self where Evm: TaikoAnchorEvm + TaikoZkGasEvm, { // The executor installs the authoritative anchor context through the anchor system // call in `apply_pre_execution_changes`; replay-only derivation must stay off so a // missing initialization keeps failing loudly. evm.set_anchor_ctx_derivation_enabled(false); // The executor owns the per-transaction zk gas bracket (reset, intrinsic charge, // commit) in `execute_transaction_without_commit`; the wrapper's per-transact entry // reset must stay off or it would wipe the intrinsic charged before `transact` runs. evm.set_per_transact_zk_gas_reset_enabled(false); Self { evm, ctx, receipts: Vec::new(), gas_used: 0, zk_gas_exhausted: false, system_caller: SystemCaller::new(spec.clone()), spec, receipt_builder, evm_extra_execution_ctx_initialized: false, } } /// Returns the dedicated truncation error used when zk gas exhausts the block. fn zk_gas_limit_error() -> BlockExecutionError { BlockExecutionError::other(ZkGasLimitExceeded) } /// Synchronizes the finalized zk gas total from the EVM meter into the execution /// context that the assembler later reads. fn sync_finalized_block_zk_gas(&self) where Evm: TaikoZkGasEvm, { if let Some(zk_gas) = self.evm.block_zk_gas_used() { self.ctx.set_finalized_block_zk_gas(zk_gas); } } /// Reserves finalized block zk gas without executing or committing a transaction. /// /// This supports simulations that need to preserve budget for a mandatory transaction they /// cannot execute. Pre-Unzen EVMs have no meter, so the reservation is a successful no-op. pub fn reserve_block_zk_gas(&mut self, amount: u64) -> Result<(), BlockExecutionError> where Evm: TaikoZkGasEvm, { match self.evm.reserve_block_zk_gas(amount) { Ok(Some(zk_gas)) => self.ctx.set_finalized_block_zk_gas(zk_gas), Ok(None) => {} Err(ZkGasOutcome::LimitExceeded) => return Err(Self::zk_gas_limit_error()), } Ok(()) } /// Discards any in-flight zk gas for the current transaction while preserving the committed /// block total. fn reset_current_transaction_zk_gas(&mut self) where Evm: TaikoZkGasEvm, { self.evm.reset_transaction_zk_gas(); self.sync_finalized_block_zk_gas(); } /// Commits the current transaction's zk gas and publishes the updated block total. /// /// # Panics /// /// If committing would exceed the block zk gas budget. [`BlockExecutor::commit_transaction`] /// is infallible in alloy-evm 0.39, so `execute_transaction_without_commit` pre-checks the /// budget while the failure can still be reported; reaching the exceeded branch here means /// the executor was driven with a result that skipped that check. fn commit_current_transaction_zk_gas(&mut self) where Evm: TaikoZkGasEvm, { match self.evm.commit_transaction_zk_gas() { Ok(Some(zk_gas)) => self.ctx.set_finalized_block_zk_gas(zk_gas), Ok(None) => {} Err(ZkGasOutcome::LimitExceeded) => unreachable!( "zk gas commit exceeded the block budget; \ execute_transaction_without_commit must pre-check the budget" ), } } /// Validates the imported header difficulty, when present, against the finalized block /// zk gas recomputed by execution. fn validate_expected_zk_gas_difficulty(&self) -> Result<(), BlockExecutionError> { let Some(expected) = self.ctx.expected_difficulty() else { return Ok(()) }; let got = U256::from(self.ctx.finalized_block_zk_gas()); if got == expected { return Ok(()); } Err(BlockExecutionError::other(ZkGasDifficultyMismatch { expected, got })) } } /// Executor hooks that let transaction selection keep filling a block after one candidate /// overflows the zk gas budget. /// /// Block import, sealing and prover execution never use these hooks, so they keep stopping at the /// first zk gas overflow. pub trait TxSelectionExecutor { /// Clears the zk gas exhaustion an overflowed transaction left, so the next candidate can run. /// Only transaction selection may call this; block execution must keep stopping at the first /// overflow. Call it only between transactions, after an overflow error: it discards the /// in-flight zk gas. fn clear_zk_gas_exhaustion(&mut self); /// Returns whether the block zk gas budget can still pay another transaction's intrinsic /// charge. Always true without a zk gas meter. fn can_fit_another_transaction_zk_gas(&self) -> bool; } impl TxSelectionExecutor for TaikoBlockExecutor<'_, Evm, Spec, R> { /// Drops the sticky exhaustion flag and discards the overflowed transaction's in-flight zk /// gas, keeping the committed block total. fn clear_zk_gas_exhaustion(&mut self) { self.zk_gas_exhausted = false; self.reset_current_transaction_zk_gas(); } /// Asks the EVM meter whether the finalized block budget covers one more intrinsic charge. fn can_fit_another_transaction_zk_gas(&self) -> bool { self.evm.can_fit_tx_intrinsic_zk_gas() } } #[cfg(feature = "prover")] impl TaikoBlockExecutor<'_, E, Spec, R> where E: Evm< DB: StateDB + DatabaseCommit, Tx: FromRecoveredTx + FromTxWithEncoded, > + TaikoZkGasEvm, Spec: TaikoExecutorSpec + Clone, R: ReceiptBuilder< Transaction: Transaction + Encodable2718 + Clone, Receipt: TxReceipt, >, ::TxType: Send + 'static, { /// Executes a prover candidate block and returns the transactions that were actually committed. pub fn execute_block_with_committed_transactions<'tx>( mut self, transactions: impl IntoIterator>, ) -> Result, BlockExecutionError> where R::Transaction: 'tx, { self.apply_pre_execution_changes()?; let mut committed_transactions = Vec::new(); for (idx, tx) in transactions.into_iter().enumerate() { let is_anchor_transaction = idx == 0; if !is_anchor_transaction && tx.signer() == Address::ZERO { continue; } let committed_tx = Recovered::new_unchecked((*tx.inner()).clone(), tx.signer()); if self.try_execute_filtered(tx, is_anchor_transaction)? { committed_transactions.push(committed_tx); } if self.zk_gas_exhausted { break; } } let execution_result = self.apply_post_execution_changes()?; Ok(CommittedBlockExecutionOutcome { execution_result, committed_transactions }) } } /// Shared prover-mode filter: executes a single transaction and classifies the failure. /// /// Lives on the inherent impl so both [`Self::execute_block_with_committed_transactions`] and the /// trait-level [`BlockExecutor::execute_block`] route through one place — the filtering rules /// (zk gas truncation, invalid tx, gas-exceeds-block) cannot drift between the two prover entry /// points. #[cfg(feature = "prover")] impl TaikoBlockExecutor<'_, E, Spec, R> where E: Evm< DB: StateDB + DatabaseCommit, Tx: FromRecoveredTx + FromTxWithEncoded, > + TaikoZkGasEvm, Spec: TaikoExecutorSpec + Clone, R: ReceiptBuilder>, ::TxType: Send + 'static, { /// Executes `tx` and returns `Ok(true)` if it committed, `Ok(false)` if it was filtered as a /// recoverable non-anchor failure, or `Err` for fatal / anchor errors. fn try_execute_filtered( &mut self, tx: impl ExecutableTx, is_anchor_transaction: bool, ) -> Result { match self.execute_transaction(tx) { Ok(_) => Ok(true), // We don't allow the anchor transaction to be discarded even if it would otherwise be a // recoverable failure; this should never happen in practice. Err(err) if !is_anchor_transaction && is_recoverable_non_anchor_tx_error(&err) => { Ok(false) } Err(err) => Err(err), } } } impl BlockExecutor for TaikoBlockExecutor<'_, E, Spec, R> where E: Evm< DB: StateDB + DatabaseCommit, Tx: FromRecoveredTx + FromTxWithEncoded, > + TaikoZkGasEvm, Spec: TaikoExecutorSpec + Clone, R: ReceiptBuilder>, ::TxType: Send + 'static, { /// Input transaction type. type Transaction = R::Transaction; /// Receipt type this executor produces. type Receipt = R::Receipt; /// EVM used by the executor. type Evm = E; /// Result of transaction execution. type Result = EthTxResult::TxType>; /// Applies any necessary changes before executing the block's transactions. /// NOTE: Here we use a system call to set the Anchor transact sender account information and /// decode the base fee share percentage from the block's extra data. fn apply_pre_execution_changes(&mut self) -> Result<(), BlockExecutionError> { self.system_caller.apply_blockhashes_contract_call(self.ctx.parent_hash, &mut self.evm)?; self.system_caller .apply_beacon_root_contract_call(self.ctx.parent_beacon_block_root, &mut self.evm)?; // Initialize the golden touch address nonce if it is not already set. if !self.evm_extra_execution_ctx_initialized { let account_info = self.evm.db_mut().basic(Address::from(TAIKO_GOLDEN_TOUCH_ADDRESS)).map_err( |e| BlockExecutionError::Internal(InternalBlockExecutionError::Other(e.into())), )?; // Decode the base fee share percentage from the block's extra data. let base_fee_share_pgtg = if self.spec.is_shasta_active(self.evm.block().timestamp().to()) { decode_shasta_basefee_sharing_pctg(self.ctx.extra_data.as_ref()) as u64 } else if self.spec.is_ontake_active_at_block(self.evm.block().number().to()) { decode_post_ontake_extra_data(self.ctx.extra_data.clone()) } else { 0 }; self.evm .transact_system_call( Address::from(TAIKO_GOLDEN_TOUCH_ADDRESS), get_treasury_address(self.evm().chain_id()), encode_anchor_system_call_data( base_fee_share_pgtg, account_info.map_or(0, |account| account.nonce), ), ) .map_err(|e| { BlockExecutionError::Internal(InternalBlockExecutionError::Other(e.into())) })?; self.evm_extra_execution_ctx_initialized = true; } Ok(()) } /// Executes a transaction and returns the resulting state diff without persisting it; the /// caller decides whether to commit. fn execute_transaction_with_commit_condition( &mut self, tx: impl ExecutableTx, f: impl FnOnce(&Self::Result) -> CommitChanges, ) -> Result, BlockExecutionError> { let output = self.execute_transaction_without_commit(tx)?; if !f(&output).should_commit() { self.reset_current_transaction_zk_gas(); return Ok(None); } Ok(Some(self.commit_transaction(output))) } /// Executes a transaction and returns the resulting state diff without persisting it; the /// caller decides whether to commit. fn execute_transaction_without_commit( &mut self, tx: impl ExecutableTx, ) -> Result { if self.zk_gas_exhausted { return Err(Self::zk_gas_limit_error()); } let (tx_env, tx) = tx.into_parts(); // The sum of the transaction's gas limit, Tg, and the gas utilized in this block prior, // must be no greater than the block's gasLimit. let block_available_gas = self.evm.block().gas_limit() - self.gas_used; if tx.tx().gas_limit() > block_available_gas { return Err(BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { transaction_gas_limit: tx.tx().gas_limit(), block_available_gas, } .into()); } self.reset_current_transaction_zk_gas(); // Charge the per-transaction intrinsic zk gas before EVM execution begins, as required // by the Unzen zk gas spec (taikoxyz/taiko-mono#21669). The intrinsic sits in the // in-flight tx total so it is committed alongside opcode/precompile usage on success // and discarded on revert/failure. A schedule with a zero intrinsic makes this a no-op. // If the intrinsic alone would exceed the remaining block budget, mirror the // mid-tx exhaustion path used by the inspector. if let Err(ZkGasOutcome::LimitExceeded) = self.evm.charge_tx_intrinsic_zk_gas() { self.zk_gas_exhausted = true; self.reset_current_transaction_zk_gas(); return Err(Self::zk_gas_limit_error()); } let result = match self.evm.transact(tx_env) { Ok(result) => result, Err(err) if err.to_string() == ZK_GAS_LIMIT_ERR => { self.zk_gas_exhausted = true; self.reset_current_transaction_zk_gas(); return Err(Self::zk_gas_limit_error()); } Err(err) => { self.reset_current_transaction_zk_gas(); return Err(BlockExecutionError::evm(err, tx.tx().trie_hash())); } }; // The trait's `commit_transaction` is infallible in alloy-evm 0.39, so the block zk gas // budget must be checked here, where truncation can still be reported. The in-flight // total is final at this point — nothing meters between execution and commit. if self.evm.transaction_zk_gas_commit_would_exceed() { self.zk_gas_exhausted = true; self.reset_current_transaction_zk_gas(); return Err(Self::zk_gas_limit_error()); } Ok(EthTxResult { result, blob_gas_used: tx.tx().blob_gas_used().unwrap_or_default(), tx_type: tx.tx().tx_type(), }) } /// Commits a previously executed transaction: updates receipts, gas accounting, and writes the /// buffered state changes to the database. /// /// `output` must be a result produced by `execute_transaction_without_commit` on this /// executor: that is the only place the block zk gas budget is pre-checked while truncation /// can still be reported (committing is infallible), and /// `commit_current_transaction_zk_gas` panics on a result that skipped the check. /// /// State hooks fire from the `State` database on commit (reth v2.4.0 moved hook delivery off /// the block executor), so no explicit hook call is needed here. fn commit_transaction(&mut self, output: Self::Result) -> GasOutput { let EthTxResult { result: ResultAndState { result, state }, tx_type, .. } = output; let gas_used = result.tx_gas_used(); self.commit_current_transaction_zk_gas(); // append gas used self.gas_used += gas_used; // Push transaction changeset and calculate header bloom filter for receipt. self.receipts.push(self.receipt_builder.build_receipt(ReceiptBuilderCtx { tx_type, evm: &self.evm, result, state: &state, cumulative_gas_used: self.gas_used, })); // Commit the state changes. self.evm.db_mut().commit(state); GasOutput::new(gas_used) } /// Applies any necessary changes after executing the block's transactions, completes execution /// and returns the underlying EVM along with execution result. fn finish(self) -> Result<(Self::Evm, BlockExecutionResult), BlockExecutionError> { self.sync_finalized_block_zk_gas(); self.validate_expected_zk_gas_difficulty()?; Ok(( self.evm, BlockExecutionResult { receipts: self.receipts, requests: Requests::default(), gas_used: self.gas_used, blob_gas_used: 0, }, )) } /// Exposes mutable reference to EVM. fn evm_mut(&mut self) -> &mut Self::Evm { &mut self.evm } /// Exposes immutable reference to EVM. fn evm(&self) -> &Self::Evm { &self.evm } /// Returns a reference to all recorded receipts. fn receipts(&self) -> &[Self::Receipt] { &self.receipts } /// Executes all transactions in a block, applying pre and post execution changes. /// NOTE: For proving system, we skip the invalid transactions directly inside this function. #[cfg(feature = "prover")] fn execute_block( mut self, transactions: impl IntoIterator>, ) -> Result, BlockExecutionError> where Self: Sized, { self.apply_pre_execution_changes()?; for (idx, tx) in transactions.into_iter().enumerate() { let is_anchor_transaction = idx == 0; let (tx_env, tx) = tx.into_parts(); // Check transaction signature at first, if invalid, skip it directly. if !is_anchor_transaction && *tx.signer() == Address::ZERO { continue; } self.try_execute_filtered((tx_env, tx), is_anchor_transaction)?; if self.zk_gas_exhausted { break; } } self.apply_post_execution_changes() } } /// Encode anchor pre-execution context for the treasury system call. /// /// The payload is a fixed 16-byte big-endian blob (`u64 base_fee_share_pctg || u64 caller_nonce`) /// that the EVM-side system-call hook decodes before transaction execution. fn encode_anchor_system_call_data(base_fee_share_pctg: u64, caller_nonce: u64) -> Bytes { let mut buf = [0u8; 16]; buf[..8].copy_from_slice(&base_fee_share_pctg.to_be_bytes()); buf[8..].copy_from_slice(&caller_nonce.to_be_bytes()); Bytes::copy_from_slice(&buf) } /// Decode post-Ontake `extra_data` into the configured base-fee sharing percentage. /// /// Ontake+ blocks store the percentage as a `uint256`; only the low 64 bits are consumed here. fn decode_post_ontake_extra_data(extradata: Bytes) -> u64 { let value = Uint::<256, 4>::from_be_slice(&extradata); value.as_limbs()[0] } #[cfg(test)] mod test { use std::sync::Arc; use alloy_consensus::{Signed, TxLegacy}; use alloy_evm::EvmFactory; use alloy_primitives::{Address, B256, Bytes, ChainId, Signature, TxKind, U64, U256}; use reth_ethereum_primitives::TransactionSigned; use reth_evm::{ConfigureEvm, block::BlockExecutor}; use reth_evm_ethereum::RethReceiptBuilder; use reth_primitives_traits::SignedTransaction; use reth_revm::{ State, db::{CacheDB, EmptyDB, states::bundle_state::BundleRetention}, state::{AccountInfo, Bytecode, bytecode::opcode}, }; use alethia_reth_evm::{ alloy::decode_anchor_system_call_data, factory::TaikoEvmFactory, spec::TaikoSpecId, zk_gas::unzen::{BLOCK_ZK_GAS_LIMIT, TX_INTRINSIC_ZK_GAS}, }; use super::*; use crate::{ config::{TaikoEvmConfig, TaikoNextBlockEnvAttributes}, testutil::{ BENCH_LIMIT_TARGET, BENCH_SUCCESS_TARGET, db_with_contracts, insert_contract, recovered_tx, unzen_chain_spec, unzen_evm_env, unzen_execution_ctx, }, }; use alethia_reth_chainspec::spec::TaikoChainSpec; const BENCH_CALLER: Address = Address::with_last_byte(0x30); #[test] fn test_encode_anchor_system_call_data() { let base_fee_share_pctg = U64::random().to::(); let caller_nonce = U64::random().to::(); let encoded_data = encode_anchor_system_call_data(base_fee_share_pctg, caller_nonce); assert_eq!(encoded_data.len(), 16); assert_eq!(&encoded_data[..8], &base_fee_share_pctg.to_be_bytes()); assert_eq!(&encoded_data[8..], &caller_nonce.to_be_bytes()); let (decoded_pctg, decoded_nonce) = decode_anchor_system_call_data(&encoded_data).expect("decoding should succeed"); assert_eq!(decoded_pctg, base_fee_share_pctg); assert_eq!(decoded_nonce, caller_nonce); } #[test] fn test_decode_post_ontake_extra_data() { let base_fee_share_pctg = U64::random().to::(); assert_eq!( decode_post_ontake_extra_data(Bytes::copy_from_slice( &U256::from_limbs([base_fee_share_pctg, 0, 0, 0]).to_be_bytes::<32>(), )), base_fee_share_pctg ); } #[test] fn executor_reserves_block_zk_gas_and_synchronizes_execution_context() { let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder().with_database(db_with_contracts(&[])).with_bundle_update().build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let ctx = unzen_execution_ctx(); let mut executor = TaikoBlockExecutor::new(evm, ctx.clone(), chain_spec, RethReceiptBuilder::default()); executor.reserve_block_zk_gas(2_000_000).expect("reserve should fit"); assert_eq!(ctx.finalized_block_zk_gas(), 2_000_000); } #[test] fn executor_reserves_block_zk_gas_as_noop_without_meter() { let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder().with_database(db_with_contracts(&[])).with_bundle_update().build(); let mut env = unzen_evm_env(); env.cfg_env.spec = TaikoSpecId::SHASTA; let evm = TaikoEvmFactory.create_evm(&mut state, env); let ctx = unzen_execution_ctx(); let mut executor = TaikoBlockExecutor::new(evm, ctx.clone(), chain_spec, RethReceiptBuilder::default()); executor.reserve_block_zk_gas(2_000_000).expect("pre-Unzen reserve should be a no-op"); assert_eq!(ctx.finalized_block_zk_gas(), 0); } #[test] fn clear_zk_gas_exhaustion_lets_the_next_transaction_run() { let caller_a = Address::with_last_byte(0x31); let caller_b = Address::with_last_byte(0x32); let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder() .with_database(db_with_contracts(&[(caller_a, 0), (caller_b, 0)])) .with_bundle_update() .build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let ctx = unzen_execution_ctx(); let mut executor = TaikoBlockExecutor::new(evm, ctx.clone(), chain_spec, RethReceiptBuilder::default()); let err = executor .execute_transaction(recovered_tx(caller_a, BENCH_LIMIT_TARGET, 0, 1)) .expect_err("the overflowing transaction should exceed the zk gas limit"); assert!(is_zk_gas_limit_exceeded(&err)); assert_eq!(ctx.finalized_block_zk_gas(), 0); let sticky_err = executor .execute_transaction(recovered_tx(caller_b, BENCH_SUCCESS_TARGET, 0, 1)) .expect_err("zk gas exhaustion should stay sticky until cleared"); assert!(is_zk_gas_limit_exceeded(&sticky_err)); executor.clear_zk_gas_exhaustion(); executor .execute_transaction(recovered_tx(caller_b, BENCH_SUCCESS_TARGET, 0, 1)) .expect("the next transaction should run once exhaustion is cleared"); assert_eq!(executor.receipts().len(), 1); assert!(ctx.finalized_block_zk_gas() >= TX_INTRINSIC_ZK_GAS); assert!(ctx.finalized_block_zk_gas() < BLOCK_ZK_GAS_LIMIT); } #[test] fn overflowed_transaction_leaves_no_state_after_clearing_exhaustion() { let caller_a = Address::with_last_byte(0x31); let caller_b = Address::with_last_byte(0x32); let writer = Address::with_last_byte(0x25); let mut db = db_with_contracts(&[(caller_a, 0), (caller_b, 0)]); // Sets storage slot 0 to 1, then runs the 64 KiB KECCAK256 loop, so the transaction // overflows the zk gas budget after it has written storage. insert_contract( &mut db, writer, Bytecode::new_raw(Bytes::from(vec![ opcode::PUSH1, 0x01, opcode::PUSH1, 0x00, opcode::SSTORE, // slot 0 = 1 opcode::JUMPDEST, // loop entry at offset 5 opcode::PUSH3, 0x01, 0x00, 0x00, // len 0x010000 (64 KiB) opcode::PUSH1, 0x00, // memory offset 0 opcode::KECCAK256, opcode::POP, // discard the digest opcode::PUSH1, 0x05, // jump destination 5 opcode::JUMP, // loop ])), ); let funded_balance = db.basic(caller_a).expect("db read").expect("caller a is funded").balance; let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder().with_database(db).with_bundle_update().build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let ctx = unzen_execution_ctx(); let mut executor = TaikoBlockExecutor::new(evm, ctx.clone(), chain_spec, RethReceiptBuilder::default()); let err = executor .execute_transaction(recovered_tx(caller_a, writer, 0, 1)) .expect_err("the writer transaction should exceed the zk gas limit"); assert!(is_zk_gas_limit_exceeded(&err)); assert_eq!(ctx.finalized_block_zk_gas(), 0); executor.clear_zk_gas_exhaustion(); executor .execute_transaction(recovered_tx(caller_b, BENCH_SUCCESS_TARGET, 0, 1)) .expect("the next transaction should run once exhaustion is cleared"); let (evm, _) = executor.finish().expect("executor should finish"); drop(evm); // The overflowed transaction left nothing behind: no nonce bump, no fee charge and no // storage write. Only the next transaction's changes are in the state. let a = state.basic(caller_a).expect("db read").expect("caller a exists"); assert_eq!(a.nonce, 0); assert_eq!(a.balance, funded_balance); assert_eq!(state.storage(writer, U256::ZERO).expect("db read"), U256::ZERO); assert_eq!(state.basic(caller_b).expect("db read").expect("caller b exists").nonce, 1); let finalized = ctx.finalized_block_zk_gas(); assert!(finalized >= TX_INTRINSIC_ZK_GAS); assert!(finalized < BLOCK_ZK_GAS_LIMIT); state.merge_transitions(BundleRetention::Reverts); let bundle = state.take_bundle(); assert!(bundle.account(&caller_a).is_none()); assert!(bundle.account(&writer).is_none()); assert!(bundle.account(&caller_b).is_some()); } #[test] fn can_fit_another_transaction_zk_gas_tracks_the_intrinsic() { let chain_spec = Arc::new(unzen_chain_spec()); { let mut state = State::builder().with_database(db_with_contracts(&[])).with_bundle_update().build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let mut executor = TaikoBlockExecutor::new( evm, unzen_execution_ctx(), chain_spec.clone(), RethReceiptBuilder::default(), ); executor .reserve_block_zk_gas(BLOCK_ZK_GAS_LIMIT - TX_INTRINSIC_ZK_GAS) .expect("reserve should fit"); assert!(executor.can_fit_another_transaction_zk_gas()); } { let mut state = State::builder().with_database(db_with_contracts(&[])).with_bundle_update().build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let mut executor = TaikoBlockExecutor::new( evm, unzen_execution_ctx(), chain_spec.clone(), RethReceiptBuilder::default(), ); executor .reserve_block_zk_gas(BLOCK_ZK_GAS_LIMIT - TX_INTRINSIC_ZK_GAS + 1) .expect("reserve should fit"); assert!(!executor.can_fit_another_transaction_zk_gas()); } { let mut state = State::builder().with_database(db_with_contracts(&[])).with_bundle_update().build(); let mut env = unzen_evm_env(); env.cfg_env.spec = TaikoSpecId::SHASTA; let evm = TaikoEvmFactory.create_evm(&mut state, env); let mut executor = TaikoBlockExecutor::new( evm, unzen_execution_ctx(), chain_spec, RethReceiptBuilder::default(), ); executor .reserve_block_zk_gas(BLOCK_ZK_GAS_LIMIT - TX_INTRINSIC_ZK_GAS + 1) .expect("pre-Unzen reserve should be a no-op"); assert!(executor.can_fit_another_transaction_zk_gas()); } } #[test] fn executor_discards_limit_exceeded_tx_and_stops_after_it() { let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder() .with_database(db_with_contracts(&[(BENCH_CALLER, 0)])) .with_bundle_update() .build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); assert_eq!(evm.block_zk_gas_used(), Some(0)); let ctx = unzen_execution_ctx(); let mut executor = TaikoBlockExecutor::new( evm, ctx.clone(), chain_spec.clone(), RethReceiptBuilder::default(), ); let gas_used = executor .execute_transaction(recovered_tx(BENCH_CALLER, BENCH_SUCCESS_TARGET, 0, 1)) .expect("first transaction should commit"); let finalized_after_first = ctx.finalized_block_zk_gas(); assert!(finalized_after_first > 0); let err = executor .execute_transaction(recovered_tx(BENCH_CALLER, BENCH_LIMIT_TARGET, 1, 1)) .expect_err("second transaction should be discarded"); assert!(is_zk_gas_limit_exceeded(&err)); assert_eq!(ctx.finalized_block_zk_gas(), finalized_after_first); let repeated_err = executor .execute_transaction(recovered_tx(BENCH_CALLER, BENCH_SUCCESS_TARGET, 1, 1)) .expect_err("later transactions should not execute after zk gas exhaustion"); assert!(is_zk_gas_limit_exceeded(&repeated_err)); let (_, result) = executor.finish().expect("executor should finish after truncation"); assert_eq!(result.receipts.len(), 1); assert_eq!(result.gas_used, gas_used.tx_gas_used()); } #[test] fn executor_includes_tx_intrinsic_in_committed_block_zk_gas() { let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder() .with_database(db_with_contracts(&[(BENCH_CALLER, 0)])) .with_bundle_update() .build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let ctx = unzen_execution_ctx(); let mut executor = TaikoBlockExecutor::new(evm, ctx.clone(), chain_spec, RethReceiptBuilder::default()); executor .execute_transaction(recovered_tx(BENCH_CALLER, BENCH_SUCCESS_TARGET, 0, 1)) .expect("successful tx should commit"); let finalized = ctx.finalized_block_zk_gas(); assert!( finalized >= TX_INTRINSIC_ZK_GAS, "finalized block zk gas ({finalized}) must include the per-tx intrinsic charge ({TX_INTRINSIC_ZK_GAS})" ); } #[cfg(feature = "prover")] #[test] fn execute_block_stops_after_non_anchor_zk_gas_exhaustion() { let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder() .with_database(db_with_contracts(&[(BENCH_CALLER, 0)])) .with_bundle_update() .build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let ctx = unzen_execution_ctx(); let executor = TaikoBlockExecutor::new(evm, ctx.clone(), chain_spec, RethReceiptBuilder::default()); let result = executor.execute_block(vec![ recovered_tx(BENCH_CALLER, BENCH_SUCCESS_TARGET, 0, 1), recovered_tx(BENCH_CALLER, BENCH_LIMIT_TARGET, 1, 1), recovered_tx(BENCH_CALLER, BENCH_SUCCESS_TARGET, 2, 1), ]); let result = result.expect("prover execute_block should truncate instead of erroring"); assert_eq!(result.receipts.len(), 1); assert!(result.gas_used > 0); assert!(ctx.finalized_block_zk_gas() > 0); } #[test] fn executor_rejects_imported_unzen_block_when_difficulty_mismatches_finalized_zk_gas() { let chain_spec = Arc::new(unzen_chain_spec()); let mut state = State::builder() .with_database(db_with_contracts(&[(BENCH_CALLER, 0)])) .with_bundle_update() .build(); let evm = TaikoEvmFactory.create_evm(&mut state, unzen_evm_env()); let mut ctx = unzen_execution_ctx(); ctx.expected_difficulty = Some(U256::ZERO); let mut executor = TaikoBlockExecutor::new(evm, ctx, chain_spec, RethReceiptBuilder::default()); executor .execute_transaction(recovered_tx(BENCH_CALLER, BENCH_SUCCESS_TARGET, 0, 1)) .expect("transaction should execute successfully"); let err: BlockExecutionError = match executor.finish() { Ok(_) => panic!("imported Unzen blocks must reject difficulty mismatches"), Err(err) => err, }; assert!(is_zk_gas_difficulty_mismatch(&err)); assert!(err.to_string().contains("difficulty")); } #[test] fn is_recoverable_non_anchor_tx_error_classifies_recoverable_set() { let gas_err = BlockExecutionError::Validation( BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { transaction_gas_limit: 2, block_available_gas: 1, }, ); assert!(is_recoverable_non_anchor_tx_error(&gas_err)); // A zk gas difficulty mismatch is fatal, not a recoverable per-transaction failure. let difficulty_err = BlockExecutionError::other(ZkGasDifficultyMismatch { expected: U256::from(1u64), got: U256::from(2u64), }); assert!(!is_recoverable_non_anchor_tx_error(&difficulty_err)); } #[test] fn test_apply_pre_execution_changes_initializes_anchor_context_from_account_nonce() { let chain_spec = Arc::new(TaikoChainSpec::default()); let config = TaikoEvmConfig::new(chain_spec.clone()); let golden_touch = Address::from(TAIKO_GOLDEN_TOUCH_ADDRESS); let treasury = get_treasury_address(chain_spec.inner.chain().id()); let nonce = 7; let make_executor = || { let mut db = CacheDB::::default(); db.insert_account_info( golden_touch, AccountInfo { nonce, balance: U256::ZERO, ..Default::default() }, ); let evm_env = config .next_evm_env( &alloy_consensus::Header::default(), &TaikoNextBlockEnvAttributes { timestamp: 1, suggested_fee_recipient: Address::ZERO, prev_randao: B256::ZERO, gas_limit: 30_000_000, extra_data: Bytes::new(), base_fee_per_gas: 1, parent_beacon_block_root: None, }, ) .expect("next block env should build"); let evm = config.evm_factory().create_evm(db, evm_env); TaikoBlockExecutor::new( evm, TaikoBlockExecutionCtx { parent_hash: B256::ZERO, parent_beacon_block_root: None, ommers: &[], withdrawals: None, basefee_per_gas: 1, extra_data: Bytes::new(), is_unzen_active: false, expected_difficulty: None, finalized_block_zk_gas: Default::default(), }, chain_spec.clone(), RethReceiptBuilder::default(), ) }; let tx = TxLegacy { chain_id: Some(ChainId::from(chain_spec.inner.chain().id())), nonce, gas_price: 1, gas_limit: 21_000, to: TxKind::Call(treasury), value: U256::ZERO, input: Bytes::new(), }; let signature = Signature::new(U256::from(1), U256::from(2), false); let signed: TransactionSigned = Signed::new_unchecked(tx, signature, B256::ZERO).into(); let anchor_tx = signed.with_signer(golden_touch); let mut executor_without_init = make_executor(); assert!( executor_without_init.execute_transaction(anchor_tx.clone()).is_err(), "without pre-execution initialization, the zero-balance golden-touch tx must not be treated as anchor" ); let mut executor_with_init = make_executor(); executor_with_init .apply_pre_execution_changes() .expect("pre-execution changes should succeed"); assert!( executor_with_init.execute_transaction(anchor_tx).is_ok(), "pre-execution initialization should seed anchor detection from the golden-touch account nonce" ); } }