Skip to content

Commit

Permalink
perf(validate-tx-pool): fast non-allocating is_create (#13063)
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise authored Dec 2, 2024
1 parent 8a047ed commit 30800af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions crates/transaction-pool/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,16 @@ impl PoolTransaction for MockTransaction {
}
}

/// Returns true if the transaction is a contract creation.
fn is_create(&self) -> bool {
match self {
Self::Legacy { to, .. } | Self::Eip1559 { to, .. } | Self::Eip2930 { to, .. } => {
to.is_create()
}
Self::Eip4844 { .. } => false,
}
}

/// Returns the input data associated with the transaction.
fn input(&self) -> &[u8] {
self.get_input()
Expand Down
12 changes: 11 additions & 1 deletion crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,11 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {
/// [`TxKind::Create`] if the transaction is a contract creation.
fn kind(&self) -> TxKind;

/// Returns true if the transaction is a contract creation.
/// We don't provide a default implementation via `kind` as it copies the 21-byte
/// [`TxKind`] for this simple check. A proper implementation shouldn't allocate.
fn is_create(&self) -> bool;

/// Returns the recipient of the transaction if it is not a [`TxKind::Create`]
/// transaction.
fn to(&self) -> Option<Address> {
Expand Down Expand Up @@ -1109,7 +1114,7 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {
&self,
max_init_code_size: usize,
) -> Result<(), InvalidPoolTransactionError> {
if self.kind().is_create() && self.input().len() > max_init_code_size {
if self.is_create() && self.input().len() > max_init_code_size {
Err(InvalidPoolTransactionError::ExceedsMaxInitCodeSize(
self.size(),
max_init_code_size,
Expand Down Expand Up @@ -1328,6 +1333,11 @@ impl PoolTransaction for EthPooledTransaction {
self.transaction.kind()
}

/// Returns true if the transaction is a contract creation.
fn is_create(&self) -> bool {
self.transaction.is_create()
}

fn input(&self) -> &[u8] {
self.transaction.input()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/transaction-pool/src/validate/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ pub fn ensure_intrinsic_gas<T: EthPoolTransaction>(
let gas_after_merge = validate_initial_tx_gas(
spec_id,
transaction.input(),
transaction.kind().is_create(),
transaction.is_create(),
transaction.access_list().map(|list| list.0.as_slice()).unwrap_or(&[]),
transaction.authorization_count() as u64,
);
Expand Down

0 comments on commit 30800af

Please sign in to comment.