tx-pool: migrate ensure_max_init_code_size to PoolTransaction trait (#11976)

This commit is contained in:
Thomas Coratger
2024-10-23 15:11:59 +02:00
committed by GitHub
parent 386379efd5
commit 5e0ba4104d
2 changed files with 26 additions and 22 deletions

View File

@ -2,7 +2,7 @@
use crate::{
blobstore::BlobStoreError,
error::PoolResult,
error::{InvalidPoolTransactionError, PoolResult},
pool::{state::SubPool, BestTransactionFilter, TransactionEvents},
validate::ValidPoolTransaction,
AllTransactionsEvents,
@ -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

View File

@ -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::{
@ -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)
}
}
@ -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.
@ -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.
@ -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};