Skip to content

Commit 81a8c27

Browse files
mattssetiendnemhane
authored
test(crates): add comprehensive tests for mock transaction factory and modifications (#15842)
Co-authored-by: tiendn <[email protected]> Co-authored-by: Emilia Hane <[email protected]>
1 parent cf3240f commit 81a8c27

File tree

1 file changed

+82
-0
lines changed
  • crates/transaction-pool/src/test_utils

1 file changed

+82
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,3 +1754,85 @@ fn test_mock_priority() {
17541754
let hi = lo.next().inc_price();
17551755
assert!(o.priority(&hi, 0) > o.priority(&lo, 0));
17561756
}
1757+
1758+
#[cfg(test)]
1759+
mod tests {
1760+
use super::*;
1761+
use alloy_consensus::Transaction;
1762+
use alloy_primitives::U256;
1763+
1764+
#[test]
1765+
fn test_mock_transaction_factory() {
1766+
let mut factory = MockTransactionFactory::default();
1767+
1768+
// Test legacy transaction creation
1769+
let legacy = factory.create_legacy();
1770+
assert_eq!(legacy.transaction.tx_type(), TxType::Legacy);
1771+
1772+
// Test EIP1559 transaction creation
1773+
let eip1559 = factory.create_eip1559();
1774+
assert_eq!(eip1559.transaction.tx_type(), TxType::Eip1559);
1775+
1776+
// Test EIP4844 transaction creation
1777+
let eip4844 = factory.create_eip4844();
1778+
assert_eq!(eip4844.transaction.tx_type(), TxType::Eip4844);
1779+
}
1780+
1781+
#[test]
1782+
fn test_mock_transaction_set() {
1783+
let sender = Address::random();
1784+
let nonce_start = 0u64;
1785+
let count = 3;
1786+
1787+
// Test legacy transaction set
1788+
let legacy_set = MockTransactionSet::dependent(sender, nonce_start, count, TxType::Legacy);
1789+
assert_eq!(legacy_set.transactions.len(), count);
1790+
for (idx, tx) in legacy_set.transactions.iter().enumerate() {
1791+
assert_eq!(tx.tx_type(), TxType::Legacy);
1792+
assert_eq!(tx.nonce(), nonce_start + idx as u64);
1793+
assert_eq!(tx.sender(), sender);
1794+
}
1795+
1796+
// Test EIP1559 transaction set
1797+
let eip1559_set =
1798+
MockTransactionSet::dependent(sender, nonce_start, count, TxType::Eip1559);
1799+
assert_eq!(eip1559_set.transactions.len(), count);
1800+
for (idx, tx) in eip1559_set.transactions.iter().enumerate() {
1801+
assert_eq!(tx.tx_type(), TxType::Eip1559);
1802+
assert_eq!(tx.nonce(), nonce_start + idx as u64);
1803+
assert_eq!(tx.sender(), sender);
1804+
}
1805+
}
1806+
1807+
#[test]
1808+
fn test_mock_transaction_modifications() {
1809+
let tx = MockTransaction::eip1559();
1810+
1811+
// Test price increment
1812+
let original_price = tx.get_gas_price();
1813+
let tx_inc = tx.inc_price();
1814+
assert!(tx_inc.get_gas_price() > original_price);
1815+
1816+
// Test gas limit increment
1817+
let original_limit = tx.gas_limit();
1818+
let tx_inc = tx.inc_limit();
1819+
assert!(tx_inc.gas_limit() > original_limit);
1820+
1821+
// Test nonce increment
1822+
let original_nonce = tx.nonce();
1823+
let tx_inc = tx.inc_nonce();
1824+
assert_eq!(tx_inc.nonce(), original_nonce + 1);
1825+
}
1826+
1827+
#[test]
1828+
fn test_mock_transaction_cost() {
1829+
let tx = MockTransaction::eip1559()
1830+
.with_gas_limit(7_000)
1831+
.with_max_fee(100)
1832+
.with_value(U256::ZERO);
1833+
1834+
// Cost is calculated as (gas_limit * max_fee_per_gas) + value
1835+
let expected_cost = U256::from(7_000u64) * U256::from(100u128) + U256::ZERO;
1836+
assert_eq!(*tx.cost(), expected_cost);
1837+
}
1838+
}

0 commit comments

Comments
 (0)