Skip to content

Commit

Permalink
perf(validate-tx-pool): fast non-allocating is_local (paradigmxyz#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise authored Dec 3, 2024
1 parent 6baf519 commit 7008ac2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,11 @@ impl<T> RecoveredTx<T> {
self.signer
}

/// Reference to the signer of transaction recovered from signature
pub const fn signer_ref(&self) -> &Address {
&self.signer
}

/// Returns a reference to [`TransactionSigned`]
pub const fn as_signed(&self) -> &T {
&self.signed_transaction
Expand Down
20 changes: 10 additions & 10 deletions crates/transaction-pool/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ impl LocalTransactionConfig {

/// Returns whether the local addresses vector contains the given address.
#[inline]
pub fn contains_local_address(&self, address: Address) -> bool {
self.local_addresses.contains(&address)
pub fn contains_local_address(&self, address: &Address) -> bool {
self.local_addresses.contains(address)
}

/// Returns whether the particular transaction should be considered local.
///
/// This always returns false if the local exemptions are disabled.
#[inline]
pub fn is_local(&self, origin: TransactionOrigin, sender: Address) -> bool {
pub fn is_local(&self, origin: TransactionOrigin, sender: &Address) -> bool {
if self.no_local_exemptions() {
return false
}
Expand Down Expand Up @@ -286,10 +286,10 @@ mod tests {
let config = LocalTransactionConfig { local_addresses, ..Default::default() };

// Should contain the inserted address
assert!(config.contains_local_address(address));
assert!(config.contains_local_address(&address));

// Should not contain another random address
assert!(!config.contains_local_address(Address::new([2; 20])));
assert!(!config.contains_local_address(&Address::new([2; 20])));
}

#[test]
Expand All @@ -302,7 +302,7 @@ mod tests {
};

// Should return false as no exemptions is set to true
assert!(!config.is_local(TransactionOrigin::Local, address));
assert!(!config.is_local(TransactionOrigin::Local, &address));
}

#[test]
Expand All @@ -315,13 +315,13 @@ mod tests {
LocalTransactionConfig { no_exemptions: false, local_addresses, ..Default::default() };

// Should return true as the transaction origin is local
assert!(config.is_local(TransactionOrigin::Local, Address::new([2; 20])));
assert!(config.is_local(TransactionOrigin::Local, address));
assert!(config.is_local(TransactionOrigin::Local, &Address::new([2; 20])));
assert!(config.is_local(TransactionOrigin::Local, &address));

// Should return true as the address is in the local_addresses set
assert!(config.is_local(TransactionOrigin::External, address));
assert!(config.is_local(TransactionOrigin::External, &address));
// Should return false as the address is not in the local_addresses set
assert!(!config.is_local(TransactionOrigin::External, Address::new([2; 20])));
assert!(!config.is_local(TransactionOrigin::External, &Address::new([2; 20])));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ impl<T: PoolTransaction> AllTransactions<T> {
transaction: ValidPoolTransaction<T>,
on_chain_nonce: u64,
) -> Result<ValidPoolTransaction<T>, InsertErr<T>> {
if !self.local_transactions_config.is_local(transaction.origin, transaction.sender()) {
if !self.local_transactions_config.is_local(transaction.origin, transaction.sender_ref()) {
let current_txs =
self.tx_counter.get(&transaction.sender_id()).copied().unwrap_or_default();

Expand Down
4 changes: 4 additions & 0 deletions crates/transaction-pool/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ impl PoolTransaction for MockTransaction {
*self.get_sender()
}

fn sender_ref(&self) -> &Address {
self.get_sender()
}

fn nonce(&self) -> u64 {
*self.get_nonce()
}
Expand Down
8 changes: 8 additions & 0 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,9 @@ pub trait PoolTransaction:
/// The Sender of the transaction.
fn sender(&self) -> Address;

/// Reference to the Sender of the transaction.
fn sender_ref(&self) -> &Address;

/// Returns the nonce for this transaction.
fn nonce(&self) -> u64;

Expand Down Expand Up @@ -1277,6 +1280,11 @@ impl PoolTransaction for EthPooledTransaction {
self.transaction.signer()
}

/// Returns a reference to the Sender of the transaction.
fn sender_ref(&self) -> &Address {
self.transaction.signer_ref()
}

/// Returns the nonce for this transaction.
fn nonce(&self) -> u64 {
self.transaction.nonce()
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 @@ -263,7 +263,7 @@ where

// Drop non-local transactions with a fee lower than the configured fee for acceptance into
// the pool.
if !self.local_transactions_config.is_local(origin, transaction.sender()) &&
if !self.local_transactions_config.is_local(origin, transaction.sender_ref()) &&
transaction.is_eip1559() &&
transaction.max_priority_fee_per_gas() < self.minimum_priority_fee
{
Expand Down
5 changes: 5 additions & 0 deletions crates/transaction-pool/src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ impl<T: PoolTransaction> ValidPoolTransaction<T> {
self.transaction.sender()
}

/// Returns a reference to the address of the sender
pub fn sender_ref(&self) -> &Address {
self.transaction.sender_ref()
}

/// Returns the recipient of the transaction if it is not a CREATE transaction.
pub fn to(&self) -> Option<Address> {
self.transaction.to()
Expand Down

0 comments on commit 7008ac2

Please sign in to comment.