Skip to content

Commit

Permalink
tx-pool: migrate ensure_max_init_code_size to PoolTransaction tra…
Browse files Browse the repository at this point in the history
…it (#11976)
  • Loading branch information
tcoratger authored Oct 23, 2024
1 parent 386379e commit 5e0ba41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
22 changes: 21 additions & 1 deletion crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
blobstore::BlobStoreError,
error::PoolResult,
error::{InvalidPoolTransactionError, PoolResult},
pool::{state::SubPool, BestTransactionFilter, TransactionEvents},
validate::ValidPoolTransaction,
AllTransactionsEvents,
Expand Down Expand Up @@ -961,6 +961,26 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {

/// Returns `chain_id`
fn chain_id(&self) -> Option<u64>;

/// Ensures that the transaction's code size does not exceed the provided `max_init_code_size`.
///
/// This is specifically relevant for contract creation transactions ([`TxKind::Create`]),
/// where the input data contains the initialization code. If the input code size exceeds
/// the configured limit, an [`InvalidPoolTransactionError::ExceedsMaxInitCodeSize`] error is
/// returned.
fn ensure_max_init_code_size(
&self,
max_init_code_size: usize,
) -> Result<(), InvalidPoolTransactionError> {
if self.kind().is_create() && self.input().len() > max_init_code_size {
Err(InvalidPoolTransactionError::ExceedsMaxInitCodeSize(
self.size(),
max_init_code_size,
))
} else {
Ok(())
}
}
}

/// Super trait for transactions that can be converted to and from Eth transactions
Expand Down
26 changes: 5 additions & 21 deletions crates/transaction-pool/src/validate/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
traits::TransactionOrigin,
validate::{ValidTransaction, ValidationTask, MAX_INIT_CODE_BYTE_SIZE},
EthBlobTransactionSidecar, EthPoolTransaction, LocalTransactionConfig, PoolTransaction,
EthBlobTransactionSidecar, EthPoolTransaction, LocalTransactionConfig,
TransactionValidationOutcome, TransactionValidationTaskExecutor, TransactionValidator,
};
use alloy_consensus::constants::{
Expand Down Expand Up @@ -223,7 +223,7 @@ where

// Check whether the init code size has been exceeded.
if self.fork_tracker.is_shanghai_activated() {
if let Err(err) = ensure_max_init_code_size(&transaction, MAX_INIT_CODE_BYTE_SIZE) {
if let Err(err) = transaction.ensure_max_init_code_size(MAX_INIT_CODE_BYTE_SIZE) {
return TransactionValidationOutcome::Invalid(transaction, err)
}
}
Expand Down Expand Up @@ -711,7 +711,7 @@ impl EthTransactionValidatorBuilder {
EthTransactionValidator { inner: Arc::new(inner) }
}

/// Builds a the [`EthTransactionValidator`] and spawns validation tasks via the
/// Builds a [`EthTransactionValidator`] and spawns validation tasks via the
/// [`TransactionValidationTaskExecutor`]
///
/// The validator will spawn `additional_tasks` additional tasks for validation.
Expand Down Expand Up @@ -783,22 +783,6 @@ impl ForkTracker {
}
}

/// Ensure that the code size is not greater than `max_init_code_size`.
/// `max_init_code_size` should be configurable so this will take it as an argument.
pub fn ensure_max_init_code_size<T: PoolTransaction>(
transaction: &T,
max_init_code_size: usize,
) -> Result<(), InvalidPoolTransactionError> {
if transaction.kind().is_create() && transaction.input().len() > max_init_code_size {
Err(InvalidPoolTransactionError::ExceedsMaxInitCodeSize(
transaction.size(),
max_init_code_size,
))
} else {
Ok(())
}
}

/// Ensures that gas limit of the transaction exceeds the intrinsic gas of the transaction.
///
/// Caution: This only checks past the Merge hardfork.
Expand Down Expand Up @@ -833,8 +817,8 @@ pub fn ensure_intrinsic_gas<T: EthPoolTransaction>(
mod tests {
use super::*;
use crate::{
blobstore::InMemoryBlobStore, error::PoolErrorKind, CoinbaseTipOrdering,
EthPooledTransaction, Pool, TransactionPool,
blobstore::InMemoryBlobStore, error::PoolErrorKind, traits::PoolTransaction,
CoinbaseTipOrdering, EthPooledTransaction, Pool, TransactionPool,
};
use alloy_eips::eip2718::Decodable2718;
use alloy_primitives::{hex, U256};
Expand Down

0 comments on commit 5e0ba41

Please sign in to comment.