Skip to content

Commit

Permalink
ExecutionInfo and ExecutedPayload Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SozinM committed Feb 24, 2025
1 parent 84aad95 commit 9ed4f94
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 68 deletions.
70 changes: 2 additions & 68 deletions crates/op-rbuilder/src/payload_builder_vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::generator::BuildArguments;
use crate::{
generator::{BlockCell, PayloadBuilder},
metrics::OpRBuilderMetrics,
primitives::PayloadBuilderService,
primitives::{ExecutedPayload, ExecutionInfo, PayloadBuilderService},
tx_signer::Signer,
};
use alloy_consensus::constants::EMPTY_WITHDRAWALS;
Expand All @@ -13,7 +13,7 @@ use alloy_consensus::{
};
use alloy_eips::merge::BEACON_NONCE;
use alloy_primitives::private::alloy_rlp::Encodable;
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use alloy_primitives::{Address, Bytes, TxKind, U256};
use alloy_rpc_types_engine::PayloadId;
use alloy_rpc_types_eth::Withdrawals;
use op_alloy_consensus::{OpDepositReceipt, OpTypedTransaction};
Expand Down Expand Up @@ -692,72 +692,6 @@ impl<T: PoolTransaction> OpPayloadTransactions<T> for () {
}
}

/// Holds the state after execution
#[derive(Debug)]
pub struct ExecutedPayload<N: NodePrimitives> {
/// Tracked execution info
pub info: ExecutionInfo<N>,
/// Withdrawal hash.
pub withdrawals_root: Option<B256>,
}

/// This acts as the container for executed transactions and its byproducts (receipts, gas used)
#[derive(Default, Debug)]
pub struct ExecutionInfo<N: NodePrimitives> {
/// All executed transactions (unrecovered).
pub executed_transactions: Vec<N::SignedTx>,
/// The recovered senders for the executed transactions.
pub executed_senders: Vec<Address>,
/// The transaction receipts
pub receipts: Vec<N::Receipt>,
/// All gas used so far
pub cumulative_gas_used: u64,
/// Estimated DA size
pub cumulative_da_bytes_used: u64,
/// Tracks fees from executed mempool transactions
pub total_fees: U256,
}

impl<N: NodePrimitives> ExecutionInfo<N> {
/// Create a new instance with allocated slots.
pub fn with_capacity(capacity: usize) -> Self {
Self {
executed_transactions: Vec::with_capacity(capacity),
executed_senders: Vec::with_capacity(capacity),
receipts: Vec::with_capacity(capacity),
cumulative_gas_used: 0,
cumulative_da_bytes_used: 0,
total_fees: U256::ZERO,
}
}

/// Returns true if the transaction would exceed the block limits:
/// - block gas limit: ensures the transaction still fits into the block.
/// - tx DA limit: if configured, ensures the tx does not exceed the maximum allowed DA limit
/// per tx.
/// - block DA limit: if configured, ensures the transaction's DA size does not exceed the
/// maximum allowed DA limit per block.
pub fn is_tx_over_limits(
&self,
tx: &N::SignedTx,
block_gas_limit: u64,
tx_data_limit: Option<u64>,
block_data_limit: Option<u64>,
) -> bool {
if tx_data_limit.is_some_and(|da_limit| tx.length() as u64 > da_limit) {
return true;
}

if block_data_limit
.is_some_and(|da_limit| self.cumulative_da_bytes_used + (tx.length() as u64) > da_limit)
{
return true;
}

self.cumulative_gas_used + tx.gas_limit() > block_gas_limit
}
}

/// Container type that holds all necessities to build a new payload.
#[derive(Debug)]
pub struct OpPayloadBuilderCtx<EvmConfig: ConfigureEvmEnv, ChainSpec, N: NodePrimitives> {
Expand Down
70 changes: 70 additions & 0 deletions crates/op-rbuilder/src/primitives/execution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use alloy_consensus::Transaction;
use alloy_primitives::private::alloy_rlp::Encodable;
use alloy_primitives::{Address, B256, U256};
use reth_primitives_traits::NodePrimitives;

/// Holds the state after execution
#[derive(Debug)]
pub struct ExecutedPayload<N: NodePrimitives> {
/// Tracked execution info
pub info: ExecutionInfo<N>,
/// Withdrawal hash.
pub withdrawals_root: Option<B256>,
}

/// This acts as the container for executed transactions and its byproducts (receipts, gas used)
#[derive(Default, Debug)]
pub struct ExecutionInfo<N: NodePrimitives> {
/// All executed transactions (unrecovered).
pub executed_transactions: Vec<N::SignedTx>,
/// The recovered senders for the executed transactions.
pub executed_senders: Vec<Address>,
/// The transaction receipts
pub receipts: Vec<N::Receipt>,
/// All gas used so far
pub cumulative_gas_used: u64,
/// Estimated DA size
pub cumulative_da_bytes_used: u64,
/// Tracks fees from executed mempool transactions
pub total_fees: U256,
}

impl<N: NodePrimitives> ExecutionInfo<N> {
/// Create a new instance with allocated slots.
pub fn with_capacity(capacity: usize) -> Self {
Self {
executed_transactions: Vec::with_capacity(capacity),
executed_senders: Vec::with_capacity(capacity),
receipts: Vec::with_capacity(capacity),
cumulative_gas_used: 0,
cumulative_da_bytes_used: 0,
total_fees: U256::ZERO,
}
}

/// Returns true if the transaction would exceed the block limits:
/// - block gas limit: ensures the transaction still fits into the block.
/// - tx DA limit: if configured, ensures the tx does not exceed the maximum allowed DA limit
/// per tx.
/// - block DA limit: if configured, ensures the transaction's DA size does not exceed the
/// maximum allowed DA limit per block.
pub fn is_tx_over_limits(
&self,
tx: &N::SignedTx,
block_gas_limit: u64,
tx_data_limit: Option<u64>,
block_data_limit: Option<u64>,
) -> bool {
if tx_data_limit.is_some_and(|da_limit| tx.length() as u64 > da_limit) {
return true;
}

if block_data_limit
.is_some_and(|da_limit| self.cumulative_da_bytes_used + (tx.length() as u64) > da_limit)
{
return true;
}

self.cumulative_gas_used + tx.gas_limit() > block_gas_limit
}
}
3 changes: 3 additions & 0 deletions crates/op-rbuilder/src/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! This module contains types from the reth that weren't heavily modified
mod execution;
mod payload_builder_service;

pub use payload_builder_service::PayloadBuilderService;

pub use execution::{ExecutedPayload, ExecutionInfo};

0 comments on commit 9ed4f94

Please sign in to comment.