reth_optimism_payload_builder/
builder.rs

1//! Optimism payload builder implementation.
2use crate::{
3    config::OpBuilderConfig, error::OpPayloadBuilderError, payload::OpBuiltPayload, OpAttributes,
4    OpPayloadBuilderAttributes, OpPayloadPrimitives,
5};
6use alloy_consensus::{BlockHeader, Transaction, Typed2718};
7use alloy_evm::Evm as AlloyEvm;
8use alloy_primitives::{B256, U256};
9use alloy_rpc_types_debug::ExecutionWitness;
10use alloy_rpc_types_engine::PayloadId;
11use reth_basic_payload_builder::*;
12use reth_chain_state::ExecutedBlock;
13use reth_chainspec::{ChainSpecProvider, EthChainSpec};
14use reth_evm::{
15    block::BlockExecutorFor,
16    execute::{
17        BlockBuilder, BlockBuilderOutcome, BlockExecutionError, BlockExecutor, BlockValidationError,
18    },
19    op_revm::{constants::L1_BLOCK_CONTRACT, L1BlockInfo},
20    ConfigureEvm, Database,
21};
22use reth_execution_types::ExecutionOutcome;
23use reth_optimism_forks::OpHardforks;
24use reth_optimism_primitives::{transaction::OpTransaction, ADDRESS_L2_TO_L1_MESSAGE_PASSER};
25use reth_optimism_txpool::{
26    estimated_da_size::DataAvailabilitySized,
27    interop::{is_valid_interop, MaybeInteropTransaction},
28    OpPooledTx,
29};
30use reth_payload_builder_primitives::PayloadBuilderError;
31use reth_payload_primitives::{BuildNextEnv, PayloadBuilderAttributes};
32use reth_payload_util::{BestPayloadTransactions, NoopPayloadTransactions, PayloadTransactions};
33use reth_primitives_traits::{
34    HeaderTy, NodePrimitives, SealedHeader, SealedHeaderFor, SignedTransaction, TxTy,
35};
36use reth_revm::{
37    cancelled::CancelOnDrop, database::StateProviderDatabase, db::State,
38    witness::ExecutionWitnessRecord,
39};
40use reth_storage_api::{errors::ProviderError, StateProvider, StateProviderFactory};
41use reth_transaction_pool::{BestTransactionsAttributes, PoolTransaction, TransactionPool};
42use revm::context::{Block, BlockEnv};
43use std::{marker::PhantomData, sync::Arc};
44use tracing::{debug, trace, warn};
45
46/// Optimism's payload builder
47#[derive(Debug)]
48pub struct OpPayloadBuilder<
49    Pool,
50    Client,
51    Evm,
52    Txs = (),
53    Attrs = OpPayloadBuilderAttributes<TxTy<<Evm as ConfigureEvm>::Primitives>>,
54> {
55    /// The rollup's compute pending block configuration option.
56    // TODO(clabby): Implement this feature.
57    pub compute_pending_block: bool,
58    /// The type responsible for creating the evm.
59    pub evm_config: Evm,
60    /// Transaction pool.
61    pub pool: Pool,
62    /// Node client.
63    pub client: Client,
64    /// Settings for the builder, e.g. DA settings.
65    pub config: OpBuilderConfig,
66    /// The type responsible for yielding the best transactions for the payload if mempool
67    /// transactions are allowed.
68    pub best_transactions: Txs,
69    /// Marker for the payload attributes type.
70    _pd: PhantomData<Attrs>,
71}
72
73impl<Pool, Client, Evm, Txs, Attrs> Clone for OpPayloadBuilder<Pool, Client, Evm, Txs, Attrs>
74where
75    Pool: Clone,
76    Client: Clone,
77    Evm: ConfigureEvm,
78    Txs: Clone,
79{
80    fn clone(&self) -> Self {
81        Self {
82            evm_config: self.evm_config.clone(),
83            pool: self.pool.clone(),
84            client: self.client.clone(),
85            config: self.config.clone(),
86            best_transactions: self.best_transactions.clone(),
87            compute_pending_block: self.compute_pending_block,
88            _pd: PhantomData,
89        }
90    }
91}
92
93impl<Pool, Client, Evm, Attrs> OpPayloadBuilder<Pool, Client, Evm, (), Attrs> {
94    /// `OpPayloadBuilder` constructor.
95    ///
96    /// Configures the builder with the default settings.
97    pub fn new(pool: Pool, client: Client, evm_config: Evm) -> Self {
98        Self::with_builder_config(pool, client, evm_config, Default::default())
99    }
100
101    /// Configures the builder with the given [`OpBuilderConfig`].
102    pub const fn with_builder_config(
103        pool: Pool,
104        client: Client,
105        evm_config: Evm,
106        config: OpBuilderConfig,
107    ) -> Self {
108        Self {
109            pool,
110            client,
111            compute_pending_block: true,
112            evm_config,
113            config,
114            best_transactions: (),
115            _pd: PhantomData,
116        }
117    }
118}
119
120impl<Pool, Client, Evm, Txs, Attrs> OpPayloadBuilder<Pool, Client, Evm, Txs, Attrs> {
121    /// Sets the rollup's compute pending block configuration option.
122    pub const fn set_compute_pending_block(mut self, compute_pending_block: bool) -> Self {
123        self.compute_pending_block = compute_pending_block;
124        self
125    }
126
127    /// Configures the type responsible for yielding the transactions that should be included in the
128    /// payload.
129    pub fn with_transactions<T>(
130        self,
131        best_transactions: T,
132    ) -> OpPayloadBuilder<Pool, Client, Evm, T, Attrs> {
133        let Self { pool, client, compute_pending_block, evm_config, config, .. } = self;
134        OpPayloadBuilder {
135            pool,
136            client,
137            compute_pending_block,
138            evm_config,
139            best_transactions,
140            config,
141            _pd: PhantomData,
142        }
143    }
144
145    /// Enables the rollup's compute pending block configuration option.
146    pub const fn compute_pending_block(self) -> Self {
147        self.set_compute_pending_block(true)
148    }
149
150    /// Returns the rollup's compute pending block configuration option.
151    pub const fn is_compute_pending_block(&self) -> bool {
152        self.compute_pending_block
153    }
154}
155
156impl<Pool, Client, Evm, N, T, Attrs> OpPayloadBuilder<Pool, Client, Evm, T, Attrs>
157where
158    Pool: TransactionPool<Transaction: OpPooledTx<Consensus = N::SignedTx>>,
159    Client: StateProviderFactory + ChainSpecProvider<ChainSpec: OpHardforks>,
160    N: OpPayloadPrimitives,
161    Evm: ConfigureEvm<
162        Primitives = N,
163        NextBlockEnvCtx: BuildNextEnv<Attrs, N::BlockHeader, Client::ChainSpec>,
164    >,
165    Attrs: OpAttributes<Transaction = TxTy<Evm::Primitives>>,
166{
167    /// Constructs an Optimism payload from the transactions sent via the
168    /// Payload attributes by the sequencer. If the `no_tx_pool` argument is passed in
169    /// the payload attributes, the transaction pool will be ignored and the only transactions
170    /// included in the payload will be those sent through the attributes.
171    ///
172    /// Given build arguments including an Optimism client, transaction pool,
173    /// and configuration, this function creates a transaction payload. Returns
174    /// a result indicating success with the payload or an error in case of failure.
175    fn build_payload<'a, Txs>(
176        &self,
177        args: BuildArguments<Attrs, OpBuiltPayload<N>>,
178        best: impl FnOnce(BestTransactionsAttributes) -> Txs + Send + Sync + 'a,
179    ) -> Result<BuildOutcome<OpBuiltPayload<N>>, PayloadBuilderError>
180    where
181        Txs:
182            PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx> + OpPooledTx>,
183    {
184        let BuildArguments { mut cached_reads, config, cancel, best_payload } = args;
185
186        let ctx = OpPayloadBuilderCtx {
187            evm_config: self.evm_config.clone(),
188            builder_config: self.config.clone(),
189            chain_spec: self.client.chain_spec(),
190            config,
191            cancel,
192            best_payload,
193        };
194
195        let builder = OpBuilder::new(best);
196
197        let state_provider = self.client.state_by_block_hash(ctx.parent().hash())?;
198        let state = StateProviderDatabase::new(&state_provider);
199
200        if ctx.attributes().no_tx_pool() {
201            builder.build(state, &state_provider, ctx)
202        } else {
203            // sequencer mode we can reuse cachedreads from previous runs
204            builder.build(cached_reads.as_db_mut(state), &state_provider, ctx)
205        }
206        .map(|out| out.with_cached_reads(cached_reads))
207    }
208
209    /// Computes the witness for the payload.
210    pub fn payload_witness(
211        &self,
212        parent: SealedHeader<N::BlockHeader>,
213        attributes: Attrs::RpcPayloadAttributes,
214    ) -> Result<ExecutionWitness, PayloadBuilderError>
215    where
216        Attrs: PayloadBuilderAttributes,
217    {
218        let attributes =
219            Attrs::try_new(parent.hash(), attributes, 3).map_err(PayloadBuilderError::other)?;
220
221        let config = PayloadConfig { parent_header: Arc::new(parent), attributes };
222        let ctx = OpPayloadBuilderCtx {
223            evm_config: self.evm_config.clone(),
224            builder_config: self.config.clone(),
225            chain_spec: self.client.chain_spec(),
226            config,
227            cancel: Default::default(),
228            best_payload: Default::default(),
229        };
230
231        let state_provider = self.client.state_by_block_hash(ctx.parent().hash())?;
232
233        let builder = OpBuilder::new(|_| NoopPayloadTransactions::<Pool::Transaction>::default());
234        builder.witness(state_provider, &ctx)
235    }
236}
237
238/// Implementation of the [`PayloadBuilder`] trait for [`OpPayloadBuilder`].
239impl<Pool, Client, Evm, N, Txs, Attrs> PayloadBuilder
240    for OpPayloadBuilder<Pool, Client, Evm, Txs, Attrs>
241where
242    N: OpPayloadPrimitives,
243    Client: StateProviderFactory + ChainSpecProvider<ChainSpec: OpHardforks> + Clone,
244    Pool: TransactionPool<Transaction: OpPooledTx<Consensus = N::SignedTx>>,
245    Evm: ConfigureEvm<
246        Primitives = N,
247        NextBlockEnvCtx: BuildNextEnv<Attrs, N::BlockHeader, Client::ChainSpec>,
248    >,
249    Txs: OpPayloadTransactions<Pool::Transaction>,
250    Attrs: OpAttributes<Transaction = N::SignedTx>,
251{
252    type Attributes = Attrs;
253    type BuiltPayload = OpBuiltPayload<N>;
254
255    fn try_build(
256        &self,
257        args: BuildArguments<Self::Attributes, Self::BuiltPayload>,
258    ) -> Result<BuildOutcome<Self::BuiltPayload>, PayloadBuilderError> {
259        let pool = self.pool.clone();
260        self.build_payload(args, |attrs| self.best_transactions.best_transactions(pool, attrs))
261    }
262
263    fn on_missing_payload(
264        &self,
265        _args: BuildArguments<Self::Attributes, Self::BuiltPayload>,
266    ) -> MissingPayloadBehaviour<Self::BuiltPayload> {
267        // we want to await the job that's already in progress because that should be returned as
268        // is, there's no benefit in racing another job
269        MissingPayloadBehaviour::AwaitInProgress
270    }
271
272    // NOTE: this should only be used for testing purposes because this doesn't have access to L1
273    // system txs, hence on_missing_payload we return [MissingPayloadBehaviour::AwaitInProgress].
274    fn build_empty_payload(
275        &self,
276        config: PayloadConfig<Self::Attributes, N::BlockHeader>,
277    ) -> Result<Self::BuiltPayload, PayloadBuilderError> {
278        let args = BuildArguments {
279            config,
280            cached_reads: Default::default(),
281            cancel: Default::default(),
282            best_payload: None,
283        };
284        self.build_payload(args, |_| NoopPayloadTransactions::<Pool::Transaction>::default())?
285            .into_payload()
286            .ok_or_else(|| PayloadBuilderError::MissingPayload)
287    }
288}
289
290/// The type that builds the payload.
291///
292/// Payload building for optimism is composed of several steps.
293/// The first steps are mandatory and defined by the protocol.
294///
295/// 1. first all System calls are applied.
296/// 2. After canyon the forced deployed `create2deployer` must be loaded
297/// 3. all sequencer transactions are executed (part of the payload attributes)
298///
299/// Depending on whether the node acts as a sequencer and is allowed to include additional
300/// transactions (`no_tx_pool == false`):
301/// 4. include additional transactions
302///
303/// And finally
304/// 5. build the block: compute all roots (txs, state)
305#[derive(derive_more::Debug)]
306pub struct OpBuilder<'a, Txs> {
307    /// Yields the best transaction to include if transactions from the mempool are allowed.
308    #[debug(skip)]
309    best: Box<dyn FnOnce(BestTransactionsAttributes) -> Txs + 'a>,
310}
311
312impl<'a, Txs> OpBuilder<'a, Txs> {
313    /// Creates a new [`OpBuilder`].
314    pub fn new(best: impl FnOnce(BestTransactionsAttributes) -> Txs + Send + Sync + 'a) -> Self {
315        Self { best: Box::new(best) }
316    }
317}
318
319impl<Txs> OpBuilder<'_, Txs> {
320    /// Builds the payload on top of the state.
321    pub fn build<Evm, ChainSpec, N, Attrs>(
322        self,
323        db: impl Database<Error = ProviderError>,
324        state_provider: impl StateProvider,
325        ctx: OpPayloadBuilderCtx<Evm, ChainSpec, Attrs>,
326    ) -> Result<BuildOutcomeKind<OpBuiltPayload<N>>, PayloadBuilderError>
327    where
328        Evm: ConfigureEvm<
329            Primitives = N,
330            NextBlockEnvCtx: BuildNextEnv<Attrs, N::BlockHeader, ChainSpec>,
331        >,
332        ChainSpec: EthChainSpec + OpHardforks,
333        N: OpPayloadPrimitives,
334        Txs:
335            PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx> + OpPooledTx>,
336        Attrs: OpAttributes<Transaction = N::SignedTx>,
337    {
338        let Self { best } = self;
339        debug!(target: "payload_builder", id=%ctx.payload_id(), parent_header = ?ctx.parent().hash(), parent_number = ctx.parent().number(), "building new payload");
340
341        let mut db = State::builder().with_database(db).with_bundle_update().build();
342
343        // Load the L1 block contract into the database cache. If the L1 block contract is not
344        // pre-loaded the database will panic when trying to fetch the DA footprint gas
345        // scalar.
346        db.load_cache_account(L1_BLOCK_CONTRACT).map_err(BlockExecutionError::other)?;
347
348        let mut builder = ctx.block_builder(&mut db)?;
349
350        // 1. apply pre-execution changes
351        builder.apply_pre_execution_changes().map_err(|err| {
352            warn!(target: "payload_builder", %err, "failed to apply pre-execution changes");
353            PayloadBuilderError::Internal(err.into())
354        })?;
355
356        // 2. execute sequencer transactions
357        let mut info = ctx.execute_sequencer_transactions(&mut builder)?;
358
359        // 3. if mem pool transactions are requested we execute them
360        if !ctx.attributes().no_tx_pool() {
361            let best_txs = best(ctx.best_transaction_attributes(builder.evm_mut().block()));
362            if ctx.execute_best_transactions(&mut info, &mut builder, best_txs)?.is_some() {
363                return Ok(BuildOutcomeKind::Cancelled)
364            }
365
366            // check if the new payload is even more valuable
367            if !ctx.is_better_payload(info.total_fees) {
368                // can skip building the block
369                return Ok(BuildOutcomeKind::Aborted { fees: info.total_fees })
370            }
371        }
372
373        let BlockBuilderOutcome { execution_result, hashed_state, trie_updates, block } =
374            builder.finish(state_provider)?;
375
376        let sealed_block = Arc::new(block.sealed_block().clone());
377        debug!(target: "payload_builder", id=%ctx.attributes().payload_id(), sealed_block_header = ?sealed_block.header(), "sealed built block");
378
379        let execution_outcome = ExecutionOutcome::new(
380            db.take_bundle(),
381            vec![execution_result.receipts],
382            block.number(),
383            Vec::new(),
384        );
385
386        // create the executed block data
387        let executed: ExecutedBlock<N> = ExecutedBlock {
388            recovered_block: Arc::new(block),
389            execution_output: Arc::new(execution_outcome),
390            hashed_state: Arc::new(hashed_state),
391            trie_updates: Arc::new(trie_updates),
392        };
393
394        let no_tx_pool = ctx.attributes().no_tx_pool();
395
396        let payload =
397            OpBuiltPayload::new(ctx.payload_id(), sealed_block, info.total_fees, Some(executed));
398
399        if no_tx_pool {
400            // if `no_tx_pool` is set only transactions from the payload attributes will be included
401            // in the payload. In other words, the payload is deterministic and we can
402            // freeze it once we've successfully built it.
403            Ok(BuildOutcomeKind::Freeze(payload))
404        } else {
405            Ok(BuildOutcomeKind::Better { payload })
406        }
407    }
408
409    /// Builds the payload and returns its [`ExecutionWitness`] based on the state after execution.
410    pub fn witness<Evm, ChainSpec, N, Attrs>(
411        self,
412        state_provider: impl StateProvider,
413        ctx: &OpPayloadBuilderCtx<Evm, ChainSpec, Attrs>,
414    ) -> Result<ExecutionWitness, PayloadBuilderError>
415    where
416        Evm: ConfigureEvm<
417            Primitives = N,
418            NextBlockEnvCtx: BuildNextEnv<Attrs, N::BlockHeader, ChainSpec>,
419        >,
420        ChainSpec: EthChainSpec + OpHardforks,
421        N: OpPayloadPrimitives,
422        Txs: PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
423        Attrs: OpAttributes<Transaction = N::SignedTx>,
424    {
425        let mut db = State::builder()
426            .with_database(StateProviderDatabase::new(&state_provider))
427            .with_bundle_update()
428            .build();
429        let mut builder = ctx.block_builder(&mut db)?;
430
431        builder.apply_pre_execution_changes()?;
432        ctx.execute_sequencer_transactions(&mut builder)?;
433        builder.into_executor().apply_post_execution_changes()?;
434
435        if ctx.chain_spec.is_isthmus_active_at_timestamp(ctx.attributes().timestamp()) {
436            // force load `L2ToL1MessagePasser.sol` so l2 withdrawals root can be computed even if
437            // no l2 withdrawals in block
438            _ = db.load_cache_account(ADDRESS_L2_TO_L1_MESSAGE_PASSER)?;
439        }
440
441        let ExecutionWitnessRecord { hashed_state, codes, keys, lowest_block_number: _ } =
442            ExecutionWitnessRecord::from_executed_state(&db);
443        let state = state_provider.witness(Default::default(), hashed_state)?;
444        Ok(ExecutionWitness {
445            state: state.into_iter().collect(),
446            codes,
447            keys,
448            ..Default::default()
449        })
450    }
451}
452
453/// A type that returns a the [`PayloadTransactions`] that should be included in the pool.
454pub trait OpPayloadTransactions<Transaction>: Clone + Send + Sync + Unpin + 'static {
455    /// Returns an iterator that yields the transaction in the order they should get included in the
456    /// new payload.
457    fn best_transactions<Pool: TransactionPool<Transaction = Transaction>>(
458        &self,
459        pool: Pool,
460        attr: BestTransactionsAttributes,
461    ) -> impl PayloadTransactions<Transaction = Transaction>;
462}
463
464impl<T: PoolTransaction + MaybeInteropTransaction> OpPayloadTransactions<T> for () {
465    fn best_transactions<Pool: TransactionPool<Transaction = T>>(
466        &self,
467        pool: Pool,
468        attr: BestTransactionsAttributes,
469    ) -> impl PayloadTransactions<Transaction = T> {
470        BestPayloadTransactions::new(pool.best_transactions_with_attributes(attr))
471    }
472}
473
474/// Holds the state after execution
475#[derive(Debug)]
476pub struct ExecutedPayload<N: NodePrimitives> {
477    /// Tracked execution info
478    pub info: ExecutionInfo,
479    /// Withdrawal hash.
480    pub withdrawals_root: Option<B256>,
481    /// The transaction receipts.
482    pub receipts: Vec<N::Receipt>,
483    /// The block env used during execution.
484    pub block_env: BlockEnv,
485}
486
487/// This acts as the container for executed transactions and its byproducts (receipts, gas used)
488#[derive(Default, Debug)]
489pub struct ExecutionInfo {
490    /// All gas used so far
491    pub cumulative_gas_used: u64,
492    /// Estimated DA size
493    pub cumulative_da_bytes_used: u64,
494    /// Tracks fees from executed mempool transactions
495    pub total_fees: U256,
496}
497
498impl ExecutionInfo {
499    /// Create a new instance with allocated slots.
500    pub const fn new() -> Self {
501        Self { cumulative_gas_used: 0, cumulative_da_bytes_used: 0, total_fees: U256::ZERO }
502    }
503
504    /// Returns true if the transaction would exceed the block limits:
505    /// - block gas limit: ensures the transaction still fits into the block.
506    /// - tx DA limit: if configured, ensures the tx does not exceed the maximum allowed DA limit
507    ///   per tx.
508    /// - block DA limit: if configured, ensures the transaction's DA size does not exceed the
509    ///   maximum allowed DA limit per block.
510    pub fn is_tx_over_limits(
511        &self,
512        tx_da_size: u64,
513        block_gas_limit: u64,
514        tx_data_limit: Option<u64>,
515        block_data_limit: Option<u64>,
516        tx_gas_limit: u64,
517        da_footprint_gas_scalar: Option<u16>,
518    ) -> bool {
519        if tx_data_limit.is_some_and(|da_limit| tx_da_size > da_limit) {
520            return true;
521        }
522
523        let total_da_bytes_used = self.cumulative_da_bytes_used.saturating_add(tx_da_size);
524
525        if block_data_limit.is_some_and(|da_limit| total_da_bytes_used > da_limit) {
526            return true;
527        }
528
529        // Post Jovian: the tx DA footprint must be less than the block gas limit
530        if let Some(da_footprint_gas_scalar) = da_footprint_gas_scalar {
531            let tx_da_footprint =
532                total_da_bytes_used.saturating_mul(da_footprint_gas_scalar as u64);
533            if tx_da_footprint > block_gas_limit {
534                return true;
535            }
536        }
537
538        self.cumulative_gas_used + tx_gas_limit > block_gas_limit
539    }
540}
541
542/// Container type that holds all necessities to build a new payload.
543#[derive(derive_more::Debug)]
544pub struct OpPayloadBuilderCtx<
545    Evm: ConfigureEvm,
546    ChainSpec,
547    Attrs = OpPayloadBuilderAttributes<TxTy<<Evm as ConfigureEvm>::Primitives>>,
548> {
549    /// The type that knows how to perform system calls and configure the evm.
550    pub evm_config: Evm,
551    /// Additional config for the builder/sequencer, e.g. DA and gas limit
552    pub builder_config: OpBuilderConfig,
553    /// The chainspec
554    pub chain_spec: Arc<ChainSpec>,
555    /// How to build the payload.
556    pub config: PayloadConfig<Attrs, HeaderTy<Evm::Primitives>>,
557    /// Marker to check whether the job has been cancelled.
558    pub cancel: CancelOnDrop,
559    /// The currently best payload.
560    pub best_payload: Option<OpBuiltPayload<Evm::Primitives>>,
561}
562
563impl<Evm, ChainSpec, Attrs> OpPayloadBuilderCtx<Evm, ChainSpec, Attrs>
564where
565    Evm: ConfigureEvm<
566        Primitives: OpPayloadPrimitives,
567        NextBlockEnvCtx: BuildNextEnv<Attrs, HeaderTy<Evm::Primitives>, ChainSpec>,
568    >,
569    ChainSpec: EthChainSpec + OpHardforks,
570    Attrs: OpAttributes<Transaction = TxTy<Evm::Primitives>>,
571{
572    /// Returns the parent block the payload will be build on.
573    pub fn parent(&self) -> &SealedHeaderFor<Evm::Primitives> {
574        self.config.parent_header.as_ref()
575    }
576
577    /// Returns the builder attributes.
578    pub const fn attributes(&self) -> &Attrs {
579        &self.config.attributes
580    }
581
582    /// Returns the current fee settings for transactions from the mempool
583    pub fn best_transaction_attributes(&self, block_env: impl Block) -> BestTransactionsAttributes {
584        BestTransactionsAttributes::new(
585            block_env.basefee(),
586            block_env.blob_gasprice().map(|p| p as u64),
587        )
588    }
589
590    /// Returns the unique id for this payload job.
591    pub fn payload_id(&self) -> PayloadId {
592        self.attributes().payload_id()
593    }
594
595    /// Returns true if the fees are higher than the previous payload.
596    pub fn is_better_payload(&self, total_fees: U256) -> bool {
597        is_better_payload(self.best_payload.as_ref(), total_fees)
598    }
599
600    /// Prepares a [`BlockBuilder`] for the next block.
601    pub fn block_builder<'a, DB: Database>(
602        &'a self,
603        db: &'a mut State<DB>,
604    ) -> Result<
605        impl BlockBuilder<
606                Primitives = Evm::Primitives,
607                Executor: BlockExecutorFor<'a, Evm::BlockExecutorFactory, DB>,
608            > + 'a,
609        PayloadBuilderError,
610    > {
611        self.evm_config
612            .builder_for_next_block(
613                db,
614                self.parent(),
615                Evm::NextBlockEnvCtx::build_next_env(
616                    self.attributes(),
617                    self.parent(),
618                    self.chain_spec.as_ref(),
619                )
620                .map_err(PayloadBuilderError::other)?,
621            )
622            .map_err(PayloadBuilderError::other)
623    }
624
625    /// Executes all sequencer transactions that are included in the payload attributes.
626    pub fn execute_sequencer_transactions(
627        &self,
628        builder: &mut impl BlockBuilder<Primitives = Evm::Primitives>,
629    ) -> Result<ExecutionInfo, PayloadBuilderError> {
630        let mut info = ExecutionInfo::new();
631
632        for sequencer_tx in self.attributes().sequencer_transactions() {
633            // A sequencer's block should never contain blob transactions.
634            if sequencer_tx.value().is_eip4844() {
635                return Err(PayloadBuilderError::other(
636                    OpPayloadBuilderError::BlobTransactionRejected,
637                ))
638            }
639
640            // Convert the transaction to a [RecoveredTx]. This is
641            // purely for the purposes of utilizing the `evm_config.tx_env`` function.
642            // Deposit transactions do not have signatures, so if the tx is a deposit, this
643            // will just pull in its `from` address.
644            let sequencer_tx = sequencer_tx.value().try_clone_into_recovered().map_err(|_| {
645                PayloadBuilderError::other(OpPayloadBuilderError::TransactionEcRecoverFailed)
646            })?;
647
648            let gas_used = match builder.execute_transaction(sequencer_tx.clone()) {
649                Ok(gas_used) => gas_used,
650                Err(BlockExecutionError::Validation(BlockValidationError::InvalidTx {
651                    error,
652                    ..
653                })) => {
654                    trace!(target: "payload_builder", %error, ?sequencer_tx, "Error in sequencer transaction, skipping.");
655                    continue
656                }
657                Err(err) => {
658                    // this is an error that we should treat as fatal for this attempt
659                    return Err(PayloadBuilderError::EvmExecutionError(Box::new(err)))
660                }
661            };
662
663            // add gas used by the transaction to cumulative gas used, before creating the receipt
664            info.cumulative_gas_used += gas_used;
665        }
666
667        Ok(info)
668    }
669
670    /// Executes the given best transactions and updates the execution info.
671    ///
672    /// Returns `Ok(Some(())` if the job was cancelled.
673    pub fn execute_best_transactions<Builder>(
674        &self,
675        info: &mut ExecutionInfo,
676        builder: &mut Builder,
677        mut best_txs: impl PayloadTransactions<
678            Transaction: PoolTransaction<Consensus = TxTy<Evm::Primitives>> + OpPooledTx,
679        >,
680    ) -> Result<Option<()>, PayloadBuilderError>
681    where
682        Builder: BlockBuilder<Primitives = Evm::Primitives>,
683        <<Builder::Executor as BlockExecutor>::Evm as AlloyEvm>::DB: Database,
684    {
685        let mut block_gas_limit = builder.evm_mut().block().gas_limit();
686        if let Some(gas_limit_config) = self.builder_config.gas_limit_config.gas_limit() {
687            // If a gas limit is configured, use that limit as target if it's smaller, otherwise use
688            // the block's actual gas limit.
689            block_gas_limit = gas_limit_config.min(block_gas_limit);
690        };
691        let block_da_limit = self.builder_config.da_config.max_da_block_size();
692        let tx_da_limit = self.builder_config.da_config.max_da_tx_size();
693        let base_fee = builder.evm_mut().block().basefee();
694
695        while let Some(tx) = best_txs.next(()) {
696            let interop = tx.interop_deadline();
697            let tx_da_size = tx.estimated_da_size();
698            let tx = tx.into_consensus();
699
700            let da_footprint_gas_scalar = self
701                .chain_spec
702                .is_jovian_active_at_timestamp(self.attributes().timestamp())
703                .then_some(
704                    L1BlockInfo::fetch_da_footprint_gas_scalar(builder.evm_mut().db_mut()).expect(
705                        "DA footprint should always be available from the database post jovian",
706                    ),
707                );
708
709            if info.is_tx_over_limits(
710                tx_da_size,
711                block_gas_limit,
712                tx_da_limit,
713                block_da_limit,
714                tx.gas_limit(),
715                da_footprint_gas_scalar,
716            ) {
717                // we can't fit this transaction into the block, so we need to mark it as
718                // invalid which also removes all dependent transaction from
719                // the iterator before we can continue
720                best_txs.mark_invalid(tx.signer(), tx.nonce());
721                continue
722            }
723
724            // A sequencer's block should never contain blob or deposit transactions from the pool.
725            if tx.is_eip4844() || tx.is_deposit() {
726                best_txs.mark_invalid(tx.signer(), tx.nonce());
727                continue
728            }
729
730            // We skip invalid cross chain txs, they would be removed on the next block update in
731            // the maintenance job
732            if let Some(interop) = interop &&
733                !is_valid_interop(interop, self.config.attributes.timestamp())
734            {
735                best_txs.mark_invalid(tx.signer(), tx.nonce());
736                continue
737            }
738            // check if the job was cancelled, if so we can exit early
739            if self.cancel.is_cancelled() {
740                return Ok(Some(()))
741            }
742
743            let gas_used = match builder.execute_transaction(tx.clone()) {
744                Ok(gas_used) => gas_used,
745                Err(BlockExecutionError::Validation(BlockValidationError::InvalidTx {
746                    error,
747                    ..
748                })) => {
749                    if error.is_nonce_too_low() {
750                        // if the nonce is too low, we can skip this transaction
751                        trace!(target: "payload_builder", %error, ?tx, "skipping nonce too low transaction");
752                    } else {
753                        // if the transaction is invalid, we can skip it and all of its
754                        // descendants
755                        trace!(target: "payload_builder", %error, ?tx, "skipping invalid transaction and its descendants");
756                        best_txs.mark_invalid(tx.signer(), tx.nonce());
757                    }
758                    continue
759                }
760                Err(err) => {
761                    // this is an error that we should treat as fatal for this attempt
762                    return Err(PayloadBuilderError::EvmExecutionError(Box::new(err)))
763                }
764            };
765
766            // add gas used by the transaction to cumulative gas used, before creating the
767            // receipt
768            info.cumulative_gas_used += gas_used;
769            info.cumulative_da_bytes_used += tx_da_size;
770
771            // update and add to total fees
772            let miner_fee = tx
773                .effective_tip_per_gas(base_fee)
774                .expect("fee is always valid; execution succeeded");
775            info.total_fees += U256::from(miner_fee) * U256::from(gas_used);
776        }
777
778        Ok(None)
779    }
780}