Skip to content

Commit

Permalink
text(tx-pool): add unit tests for tx pool state (paradigmxyz#12690)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Nov 20, 2024
1 parent 2c885ee commit 11847b4
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions crates/transaction-pool/src/pool/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ bitflags::bitflags! {
}
}

// === impl TxState ===

impl TxState {
/// The state of a transaction is considered `pending`, if the transaction has:
/// - _No_ parked ancestors
Expand Down Expand Up @@ -89,8 +87,6 @@ pub enum SubPool {
Pending,
}

// === impl SubPool ===

impl SubPool {
/// Whether this transaction is to be moved to the pending sub-pool.
#[inline]
Expand Down Expand Up @@ -126,16 +122,15 @@ impl SubPool {
impl From<TxState> for SubPool {
fn from(value: TxState) -> Self {
if value.is_pending() {
return Self::Pending
}
if value.is_blob() {
Self::Pending
} else if value.is_blob() {
// all _non-pending_ blob transactions are in the blob sub-pool
return Self::Blob
Self::Blob
} else if value.bits() < TxState::BASE_FEE_POOL_BITS.bits() {
Self::Queued
} else {
Self::BaseFee
}
if value.bits() < TxState::BASE_FEE_POOL_BITS.bits() {
return Self::Queued
}
Self::BaseFee
}
}

Expand Down Expand Up @@ -204,4 +199,61 @@ mod tests {
assert!(state.is_blob());
assert!(!state.is_pending());
}

#[test]
fn test_tx_state_no_nonce_gap() {
let mut state = TxState::default();
state |= TxState::NO_NONCE_GAPS;
assert!(!state.has_nonce_gap());
}

#[test]
fn test_tx_state_with_nonce_gap() {
let state = TxState::default();
assert!(state.has_nonce_gap());
}

#[test]
fn test_tx_state_enough_balance() {
let mut state = TxState::default();
state.insert(TxState::ENOUGH_BALANCE);
assert!(state.contains(TxState::ENOUGH_BALANCE));
}

#[test]
fn test_tx_state_not_too_much_gas() {
let mut state = TxState::default();
state.insert(TxState::NOT_TOO_MUCH_GAS);
assert!(state.contains(TxState::NOT_TOO_MUCH_GAS));
}

#[test]
fn test_tx_state_enough_fee_cap_block() {
let mut state = TxState::default();
state.insert(TxState::ENOUGH_FEE_CAP_BLOCK);
assert!(state.contains(TxState::ENOUGH_FEE_CAP_BLOCK));
}

#[test]
fn test_tx_base_fee() {
let state = TxState::BASE_FEE_POOL_BITS;
assert_eq!(SubPool::BaseFee, state.into());
}

#[test]
fn test_blob_transaction_only() {
let state = TxState::BLOB_TRANSACTION;
assert_eq!(SubPool::Blob, state.into());
assert!(state.is_blob());
assert!(!state.is_pending());
}

#[test]
fn test_blob_transaction_with_base_fee_bits() {
let mut state = TxState::BASE_FEE_POOL_BITS;
state.insert(TxState::BLOB_TRANSACTION);
assert_eq!(SubPool::Blob, state.into());
assert!(state.is_blob());
assert!(!state.is_pending());
}
}

0 comments on commit 11847b4

Please sign in to comment.