text(tx-pool): add unit tests for tx pool state (#12690)

This commit is contained in:
Thomas Coratger
2024-11-20 10:53:53 +01:00
committed by GitHub
parent 2c885eee21
commit 11847b4f1e

View File

@ -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
@ -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]
@ -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
}
}
@ -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());
}
}