From 9ed4f94f07b5a2b24b820701f9c261ea37dffdc7 Mon Sep 17 00:00:00 2001 From: Solar Mithril Date: Mon, 24 Feb 2025 15:52:24 +0700 Subject: [PATCH] ExecutionInfo and ExecutedPayload Refactoring --- .../src/payload_builder_vanilla.rs | 70 +------------------ .../op-rbuilder/src/primitives/execution.rs | 70 +++++++++++++++++++ crates/op-rbuilder/src/primitives/mod.rs | 3 + 3 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 crates/op-rbuilder/src/primitives/execution.rs diff --git a/crates/op-rbuilder/src/payload_builder_vanilla.rs b/crates/op-rbuilder/src/payload_builder_vanilla.rs index f9efed8d..1921bf9a 100644 --- a/crates/op-rbuilder/src/payload_builder_vanilla.rs +++ b/crates/op-rbuilder/src/payload_builder_vanilla.rs @@ -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; @@ -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}; @@ -692,72 +692,6 @@ impl OpPayloadTransactions for () { } } -/// Holds the state after execution -#[derive(Debug)] -pub struct ExecutedPayload { - /// Tracked execution info - pub info: ExecutionInfo, - /// Withdrawal hash. - pub withdrawals_root: Option, -} - -/// This acts as the container for executed transactions and its byproducts (receipts, gas used) -#[derive(Default, Debug)] -pub struct ExecutionInfo { - /// All executed transactions (unrecovered). - pub executed_transactions: Vec, - /// The recovered senders for the executed transactions. - pub executed_senders: Vec
, - /// The transaction receipts - pub receipts: Vec, - /// 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 ExecutionInfo { - /// 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, - block_data_limit: Option, - ) -> 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 { diff --git a/crates/op-rbuilder/src/primitives/execution.rs b/crates/op-rbuilder/src/primitives/execution.rs new file mode 100644 index 00000000..795afb9c --- /dev/null +++ b/crates/op-rbuilder/src/primitives/execution.rs @@ -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 { + /// Tracked execution info + pub info: ExecutionInfo, + /// Withdrawal hash. + pub withdrawals_root: Option, +} + +/// This acts as the container for executed transactions and its byproducts (receipts, gas used) +#[derive(Default, Debug)] +pub struct ExecutionInfo { + /// All executed transactions (unrecovered). + pub executed_transactions: Vec, + /// The recovered senders for the executed transactions. + pub executed_senders: Vec
, + /// The transaction receipts + pub receipts: Vec, + /// 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 ExecutionInfo { + /// 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, + block_data_limit: Option, + ) -> 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 + } +} diff --git a/crates/op-rbuilder/src/primitives/mod.rs b/crates/op-rbuilder/src/primitives/mod.rs index bc35af8c..839640f2 100644 --- a/crates/op-rbuilder/src/primitives/mod.rs +++ b/crates/op-rbuilder/src/primitives/mod.rs @@ -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};