Skip to content

Commit 030a1fd

Browse files
authored
some refactoring in transaction-pool mock utils (paradigmxyz#6043)
1 parent b08371b commit 030a1fd

File tree

2 files changed

+76
-92
lines changed

2 files changed

+76
-92
lines changed

crates/transaction-pool/src/test_utils/mock.rs

Lines changed: 74 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,12 @@ impl MockTransaction {
385385

386386
/// Gets the priority fee for dynamic fee transactions (EIP-1559 and EIP-4844)
387387
pub fn get_priority_fee(&self) -> Option<u128> {
388-
if let MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } |
389-
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } = self
390-
{
391-
Some(*max_priority_fee_per_gas)
392-
} else {
393-
None
388+
match self {
389+
MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } |
390+
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } => {
391+
Some(*max_priority_fee_per_gas)
392+
}
393+
_ => None,
394394
}
395395
}
396396

@@ -412,25 +412,19 @@ impl MockTransaction {
412412

413413
/// Gets the max fee for dynamic fee transactions (EIP-1559 and EIP-4844)
414414
pub fn get_max_fee(&self) -> Option<u128> {
415-
if let MockTransaction::Eip1559 { max_fee_per_gas, .. } |
416-
MockTransaction::Eip4844 { max_fee_per_gas, .. } = self
417-
{
418-
Some(*max_fee_per_gas)
419-
} else {
420-
None
415+
match self {
416+
MockTransaction::Eip1559 { max_fee_per_gas, .. } |
417+
MockTransaction::Eip4844 { max_fee_per_gas, .. } => Some(*max_fee_per_gas),
418+
_ => None,
421419
}
422420
}
423421

424422
/// Sets the access list for transactions supporting EIP-1559, EIP-4844, and EIP-2930.
425423
pub fn set_accesslist(&mut self, list: AccessList) -> &mut Self {
426424
match self {
427425
MockTransaction::Legacy { .. } => {}
428-
MockTransaction::Eip1559 { accesslist, .. } => {
429-
*accesslist = list;
430-
}
431-
MockTransaction::Eip4844 { accesslist, .. } => {
432-
*accesslist = list;
433-
}
426+
MockTransaction::Eip1559 { accesslist, .. } |
427+
MockTransaction::Eip4844 { accesslist, .. } |
434428
MockTransaction::Eip2930 { accesslist, .. } => {
435429
*accesslist = list;
436430
}
@@ -443,18 +437,15 @@ impl MockTransaction {
443437
/// Sets the gas price for the transaction.
444438
pub fn set_gas_price(&mut self, val: u128) -> &mut Self {
445439
match self {
446-
MockTransaction::Legacy { gas_price, .. } => {
440+
MockTransaction::Legacy { gas_price, .. } |
441+
MockTransaction::Eip2930 { gas_price, .. } => {
447442
*gas_price = val;
448443
}
449-
MockTransaction::Eip1559 { max_fee_per_gas, max_priority_fee_per_gas, .. } => {
450-
*max_fee_per_gas = val;
451-
*max_priority_fee_per_gas = val;
452-
}
444+
MockTransaction::Eip1559 { max_fee_per_gas, max_priority_fee_per_gas, .. } |
453445
MockTransaction::Eip4844 { max_fee_per_gas, max_priority_fee_per_gas, .. } => {
454446
*max_fee_per_gas = val;
455447
*max_priority_fee_per_gas = val;
456448
}
457-
MockTransaction::Eip2930 { gas_price, .. } => *gas_price = val,
458449
#[cfg(feature = "optimism")]
459450
MockTransaction::Deposit(_) => {}
460451
}
@@ -464,17 +455,15 @@ impl MockTransaction {
464455
/// Sets the gas price for the transaction.
465456
pub fn with_gas_price(mut self, val: u128) -> Self {
466457
match self {
467-
MockTransaction::Legacy { ref mut gas_price, .. } => {
458+
MockTransaction::Legacy { ref mut gas_price, .. } |
459+
MockTransaction::Eip2930 { ref mut gas_price, .. } => {
468460
*gas_price = val;
469461
}
470462
MockTransaction::Eip1559 {
471463
ref mut max_fee_per_gas,
472464
ref mut max_priority_fee_per_gas,
473465
..
474-
} => {
475-
*max_fee_per_gas = val;
476-
*max_priority_fee_per_gas = val;
477-
}
466+
} |
478467
MockTransaction::Eip4844 {
479468
ref mut max_fee_per_gas,
480469
ref mut max_priority_fee_per_gas,
@@ -483,9 +472,6 @@ impl MockTransaction {
483472
*max_fee_per_gas = val;
484473
*max_priority_fee_per_gas = val;
485474
}
486-
MockTransaction::Eip2930 { ref mut gas_price, .. } => {
487-
*gas_price = val;
488-
}
489475
#[cfg(feature = "optimism")]
490476
MockTransaction::Deposit(_) => {}
491477
}
@@ -506,20 +492,17 @@ impl MockTransaction {
506492

507493
/// Returns a clone with a decreased nonce
508494
pub fn prev(&self) -> Self {
509-
let next = self.clone().with_hash(B256::random());
510-
next.with_nonce(self.get_nonce() - 1)
495+
self.clone().with_hash(B256::random()).with_nonce(self.get_nonce() - 1)
511496
}
512497

513498
/// Returns a clone with an increased nonce
514499
pub fn next(&self) -> Self {
515-
let next = self.clone().with_hash(B256::random());
516-
next.with_nonce(self.get_nonce() + 1)
500+
self.clone().with_hash(B256::random()).with_nonce(self.get_nonce() + 1)
517501
}
518502

519503
/// Returns a clone with an increased nonce
520504
pub fn skip(&self, skip: u64) -> Self {
521-
let next = self.clone().with_hash(B256::random());
522-
next.with_nonce(self.get_nonce() + skip + 1)
505+
self.clone().with_hash(B256::random()).with_nonce(self.get_nonce() + skip + 1)
523506
}
524507

525508
/// Returns a clone with incremented nonce
@@ -540,9 +523,7 @@ impl MockTransaction {
540523

541524
/// Returns a new transaction with a higher gas price
542525
pub fn inc_price_by(&self, value: u128) -> Self {
543-
let next = self.clone();
544-
let gas = self.get_gas_price().checked_add(value).unwrap();
545-
next.with_gas_price(gas)
526+
self.clone().with_gas_price(self.get_gas_price().checked_add(value).unwrap())
546527
}
547528

548529
/// Returns a new transaction with a lower gas price -1
@@ -552,23 +533,17 @@ impl MockTransaction {
552533

553534
/// Returns a new transaction with a lower gas price
554535
pub fn decr_price_by(&self, value: u128) -> Self {
555-
let next = self.clone();
556-
let gas = self.get_gas_price().checked_sub(value).unwrap();
557-
next.with_gas_price(gas)
536+
self.clone().with_gas_price(self.get_gas_price().checked_sub(value).unwrap())
558537
}
559538

560539
/// Returns a new transaction with a higher value
561540
pub fn inc_value(&self) -> Self {
562-
let next = self.clone();
563-
let val = self.get_value().checked_add(U256::from(1)).unwrap();
564-
next.with_value(val)
541+
self.clone().with_value(self.get_value().checked_add(U256::from(1)).unwrap())
565542
}
566543

567544
/// Returns a new transaction with a higher gas limit
568545
pub fn inc_limit(&self) -> Self {
569-
let next = self.clone();
570-
let gas = self.get_gas_limit() + 1;
571-
next.with_gas_limit(gas)
546+
self.clone().with_gas_limit(self.get_gas_limit() + 1)
572547
}
573548

574549
/// Returns the transaction type identifier associated with the current [MockTransaction].
@@ -607,9 +582,9 @@ impl MockTransaction {
607582
impl PoolTransaction for MockTransaction {
608583
fn hash(&self) -> &TxHash {
609584
match self {
610-
MockTransaction::Legacy { hash, .. } => hash,
611-
MockTransaction::Eip1559 { hash, .. } => hash,
612-
MockTransaction::Eip4844 { hash, .. } => hash,
585+
MockTransaction::Legacy { hash, .. } |
586+
MockTransaction::Eip1559 { hash, .. } |
587+
MockTransaction::Eip4844 { hash, .. } |
613588
MockTransaction::Eip2930 { hash, .. } => hash,
614589
#[cfg(feature = "optimism")]
615590
MockTransaction::Deposit(TxDeposit { source_hash, .. }) => source_hash,
@@ -618,9 +593,9 @@ impl PoolTransaction for MockTransaction {
618593

619594
fn sender(&self) -> Address {
620595
match self {
621-
MockTransaction::Legacy { sender, .. } => *sender,
622-
MockTransaction::Eip1559 { sender, .. } => *sender,
623-
MockTransaction::Eip4844 { sender, .. } => *sender,
596+
MockTransaction::Legacy { sender, .. } |
597+
MockTransaction::Eip1559 { sender, .. } |
598+
MockTransaction::Eip4844 { sender, .. } |
624599
MockTransaction::Eip2930 { sender, .. } => *sender,
625600
#[cfg(feature = "optimism")]
626601
MockTransaction::Deposit(TxDeposit { from, .. }) => *from,
@@ -629,9 +604,9 @@ impl PoolTransaction for MockTransaction {
629604

630605
fn nonce(&self) -> u64 {
631606
match self {
632-
MockTransaction::Legacy { nonce, .. } => *nonce,
633-
MockTransaction::Eip1559 { nonce, .. } => *nonce,
634-
MockTransaction::Eip4844 { nonce, .. } => *nonce,
607+
MockTransaction::Legacy { nonce, .. } |
608+
MockTransaction::Eip1559 { nonce, .. } |
609+
MockTransaction::Eip4844 { nonce, .. } |
635610
MockTransaction::Eip2930 { nonce, .. } => *nonce,
636611
#[cfg(feature = "optimism")]
637612
MockTransaction::Deposit(_) => 0u64,
@@ -640,18 +615,14 @@ impl PoolTransaction for MockTransaction {
640615

641616
fn cost(&self) -> U256 {
642617
match self {
643-
MockTransaction::Legacy { gas_price, value, gas_limit, .. } => {
618+
MockTransaction::Legacy { gas_price, value, gas_limit, .. } |
619+
MockTransaction::Eip2930 { gas_limit, gas_price, value, .. } => {
644620
U256::from(*gas_limit) * U256::from(*gas_price) + *value
645621
}
646-
MockTransaction::Eip1559 { max_fee_per_gas, value, gas_limit, .. } => {
647-
U256::from(*gas_limit) * U256::from(*max_fee_per_gas) + *value
648-
}
622+
MockTransaction::Eip1559 { max_fee_per_gas, value, gas_limit, .. } |
649623
MockTransaction::Eip4844 { max_fee_per_gas, value, gas_limit, .. } => {
650624
U256::from(*gas_limit) * U256::from(*max_fee_per_gas) + *value
651625
}
652-
MockTransaction::Eip2930 { gas_limit, gas_price, value, .. } => {
653-
U256::from(*gas_limit) * U256::from(*gas_price) + *value
654-
}
655626
#[cfg(feature = "optimism")]
656627
MockTransaction::Deposit(_) => U256::ZERO,
657628
}
@@ -663,10 +634,10 @@ impl PoolTransaction for MockTransaction {
663634

664635
fn max_fee_per_gas(&self) -> u128 {
665636
match self {
666-
MockTransaction::Legacy { gas_price, .. } => *gas_price,
667-
MockTransaction::Eip1559 { max_fee_per_gas, .. } => *max_fee_per_gas,
668-
MockTransaction::Eip4844 { max_fee_per_gas, .. } => *max_fee_per_gas,
637+
MockTransaction::Legacy { gas_price, .. } |
669638
MockTransaction::Eip2930 { gas_price, .. } => *gas_price,
639+
MockTransaction::Eip1559 { max_fee_per_gas, .. } |
640+
MockTransaction::Eip4844 { max_fee_per_gas, .. } => *max_fee_per_gas,
670641
#[cfg(feature = "optimism")]
671642
MockTransaction::Deposit(_) => 0u128,
672643
}
@@ -675,8 +646,8 @@ impl PoolTransaction for MockTransaction {
675646
fn access_list(&self) -> Option<&AccessList> {
676647
match self {
677648
MockTransaction::Legacy { .. } => None,
678-
MockTransaction::Eip1559 { accesslist: accessslist, .. } => Some(accessslist),
679-
MockTransaction::Eip4844 { accesslist: accessslist, .. } => Some(accessslist),
649+
MockTransaction::Eip1559 { accesslist, .. } |
650+
MockTransaction::Eip4844 { accesslist, .. } |
680651
MockTransaction::Eip2930 { accesslist, .. } => Some(accesslist),
681652
#[cfg(feature = "optimism")]
682653
MockTransaction::Deposit(_) => None,
@@ -685,14 +656,11 @@ impl PoolTransaction for MockTransaction {
685656

686657
fn max_priority_fee_per_gas(&self) -> Option<u128> {
687658
match self {
688-
MockTransaction::Legacy { .. } => None,
689-
MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } => {
690-
Some(*max_priority_fee_per_gas)
691-
}
659+
MockTransaction::Legacy { .. } | MockTransaction::Eip2930 { .. } => None,
660+
MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } |
692661
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } => {
693662
Some(*max_priority_fee_per_gas)
694663
}
695-
MockTransaction::Eip2930 { .. } => None,
696664
#[cfg(feature = "optimism")]
697665
MockTransaction::Deposit(_) => None,
698666
}
@@ -705,58 +673,74 @@ impl PoolTransaction for MockTransaction {
705673
}
706674
}
707675

676+
/// Calculates the effective tip per gas given a base fee.
708677
fn effective_tip_per_gas(&self, base_fee: u64) -> Option<u128> {
678+
// Convert base_fee to u128 for precision in calculations
709679
let base_fee = base_fee as u128;
680+
681+
// Retrieve the maximum fee per gas
710682
let max_fee_per_gas = self.max_fee_per_gas();
683+
684+
// If the maximum fee per gas is less than the base fee, return None
711685
if max_fee_per_gas < base_fee {
712-
return None
686+
return None;
713687
}
714688

689+
// Calculate the fee by subtracting the base fee from the maximum fee per gas
715690
let fee = max_fee_per_gas - base_fee;
691+
692+
// If the maximum priority fee per gas is available, return the minimum of fee and priority
693+
// fee
716694
if let Some(priority_fee) = self.max_priority_fee_per_gas() {
717-
return Some(fee.min(priority_fee))
695+
return Some(fee.min(priority_fee));
718696
}
719697

698+
// Otherwise, return the calculated fee
720699
Some(fee)
721700
}
722701

702+
/// Returns the priority fee or gas price based on the transaction type.
723703
fn priority_fee_or_price(&self) -> u128 {
724704
match self {
725-
MockTransaction::Legacy { gas_price, .. } => *gas_price,
726-
MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } => *max_priority_fee_per_gas,
727-
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } => *max_priority_fee_per_gas,
705+
MockTransaction::Legacy { gas_price, .. } |
728706
MockTransaction::Eip2930 { gas_price, .. } => *gas_price,
707+
MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } |
708+
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } => *max_priority_fee_per_gas,
729709
#[cfg(feature = "optimism")]
730710
MockTransaction::Deposit(_) => 0u128,
731711
}
732712
}
733713

714+
/// Returns the transaction kind associated with the transaction.
734715
fn kind(&self) -> &TransactionKind {
735716
match self {
736-
MockTransaction::Legacy { to, .. } => to,
737-
MockTransaction::Eip1559 { to, .. } => to,
738-
MockTransaction::Eip4844 { to, .. } => to,
717+
MockTransaction::Legacy { to, .. } |
718+
MockTransaction::Eip1559 { to, .. } |
719+
MockTransaction::Eip4844 { to, .. } |
739720
MockTransaction::Eip2930 { to, .. } => to,
740721
#[cfg(feature = "optimism")]
741722
MockTransaction::Deposit(TxDeposit { to, .. }) => to,
742723
}
743724
}
744725

726+
/// Returns the input data associated with the transaction.
745727
fn input(&self) -> &[u8] {
746728
match self {
747729
MockTransaction::Legacy { .. } => &[],
748-
MockTransaction::Eip1559 { input, .. } => input,
749-
MockTransaction::Eip4844 { input, .. } => input,
730+
MockTransaction::Eip1559 { input, .. } |
731+
MockTransaction::Eip4844 { input, .. } |
750732
MockTransaction::Eip2930 { input, .. } => input,
751733
#[cfg(feature = "optimism")]
752734
MockTransaction::Deposit { .. } => &[],
753735
}
754736
}
755737

738+
/// Returns the size of the transaction.
756739
fn size(&self) -> usize {
757740
0
758741
}
759742

743+
/// Returns the transaction type as a byte identifier.
760744
fn tx_type(&self) -> u8 {
761745
match self {
762746
MockTransaction::Legacy { .. } => TxType::Legacy.into(),
@@ -768,14 +752,17 @@ impl PoolTransaction for MockTransaction {
768752
}
769753
}
770754

755+
/// Returns the encoded length of the transaction.
771756
fn encoded_length(&self) -> usize {
772757
0
773758
}
774759

760+
/// Returns the chain ID associated with the transaction.
775761
fn chain_id(&self) -> Option<u64> {
776762
Some(1)
777763
}
778764

765+
/// Returns true if the transaction is a deposit transaction.
779766
#[cfg(feature = "optimism")]
780767
fn is_deposit(&self) -> bool {
781768
matches!(self, MockTransaction::Deposit(_))
@@ -1137,10 +1124,9 @@ impl MockTransactionFactory {
11371124
origin: TransactionOrigin,
11381125
transaction: MockTransaction,
11391126
) -> MockValidTx {
1140-
let transaction_id = self.tx_id(&transaction);
11411127
MockValidTx {
11421128
propagate: false,
1143-
transaction_id,
1129+
transaction_id: self.tx_id(&transaction),
11441130
transaction,
11451131
timestamp: Instant::now(),
11461132
origin,

crates/transaction-pool/src/test_utils/pool.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ impl<R: Rng> MockTransactionSimulator<R> {
8383
/// Returns a new mock instance
8484
pub(crate) fn new(mut rng: R, config: MockSimulatorConfig) -> Self {
8585
let senders = config.addresses(&mut rng);
86-
let nonces = senders.iter().copied().map(|a| (a, 0)).collect();
87-
let balances = senders.iter().copied().map(|a| (a, config.balance)).collect();
8886
Self {
8987
base_fee: config.base_fee,
90-
balances,
91-
nonces,
88+
balances: senders.iter().copied().map(|a| (a, config.balance)).collect(),
89+
nonces: senders.iter().copied().map(|a| (a, 0)).collect(),
9290
senders,
9391
scenarios: config.scenarios,
9492
tx_generator: config.tx_generator,

0 commit comments

Comments
 (0)