|
| 1 | +use edr_eth::{ |
| 2 | + transaction::{Eip1559TransactionRequest, Eip155TransactionRequest, TransactionKind}, |
| 3 | + AccountInfo, Address, Bytes, HashMap, SpecId, U256, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + state::{AccountTrie, StateError, TrieState}, |
| 8 | + ExecutableTransaction, MemPool, MemPoolAddTransactionError, TransactionCreationError, |
| 9 | +}; |
| 10 | + |
| 11 | +/// A test fixture for `MemPool`. |
| 12 | +pub struct MemPoolTestFixture { |
| 13 | + /// The mem pool. |
| 14 | + pub mem_pool: MemPool, |
| 15 | + /// The state. |
| 16 | + pub state: TrieState, |
| 17 | +} |
| 18 | + |
| 19 | +impl MemPoolTestFixture { |
| 20 | + /// Constructs an instance with the provided accounts. |
| 21 | + pub fn with_accounts(accounts: &[(Address, AccountInfo)]) -> Self { |
| 22 | + let accounts = accounts.iter().cloned().collect::<HashMap<_, _>>(); |
| 23 | + let trie = AccountTrie::with_accounts(&accounts); |
| 24 | + |
| 25 | + MemPoolTestFixture { |
| 26 | + mem_pool: MemPool::new(10_000_000u64), |
| 27 | + state: TrieState::with_accounts(trie), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Tries to add the provided transaction to the mem pool. |
| 32 | + pub fn add_transaction( |
| 33 | + &mut self, |
| 34 | + transaction: ExecutableTransaction, |
| 35 | + ) -> Result<(), MemPoolAddTransactionError<StateError>> { |
| 36 | + self.mem_pool.add_transaction(&self.state, transaction) |
| 37 | + } |
| 38 | + |
| 39 | + /// Sets the block gas limit. |
| 40 | + pub fn set_block_gas_limit(&mut self, block_gas_limit: u64) -> Result<(), StateError> { |
| 41 | + self.mem_pool |
| 42 | + .set_block_gas_limit(&self.state, block_gas_limit) |
| 43 | + } |
| 44 | + |
| 45 | + /// Updates the mem pool. |
| 46 | + pub fn update(&mut self) -> Result<(), StateError> { |
| 47 | + self.mem_pool.update(&self.state) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Creates a dummy EIP-155 transaction. |
| 52 | +pub fn dummy_eip155_transaction( |
| 53 | + caller: Address, |
| 54 | + nonce: u64, |
| 55 | +) -> Result<ExecutableTransaction, TransactionCreationError> { |
| 56 | + dummy_eip155_transaction_with_price(caller, nonce, U256::ZERO) |
| 57 | +} |
| 58 | + |
| 59 | +/// Creates a dummy EIP-155 transaction with the provided gas price. |
| 60 | +pub fn dummy_eip155_transaction_with_price( |
| 61 | + caller: Address, |
| 62 | + nonce: u64, |
| 63 | + gas_price: U256, |
| 64 | +) -> Result<ExecutableTransaction, TransactionCreationError> { |
| 65 | + dummy_eip155_transaction_with_price_and_limit(caller, nonce, gas_price, 30_000) |
| 66 | +} |
| 67 | + |
| 68 | +/// Creates a dummy EIP-155 transaction with the provided gas limit. |
| 69 | +pub fn dummy_eip155_transaction_with_limit( |
| 70 | + caller: Address, |
| 71 | + nonce: u64, |
| 72 | + gas_limit: u64, |
| 73 | +) -> Result<ExecutableTransaction, TransactionCreationError> { |
| 74 | + dummy_eip155_transaction_with_price_and_limit(caller, nonce, U256::ZERO, gas_limit) |
| 75 | +} |
| 76 | + |
| 77 | +fn dummy_eip155_transaction_with_price_and_limit( |
| 78 | + caller: Address, |
| 79 | + nonce: u64, |
| 80 | + gas_price: U256, |
| 81 | + gas_limit: u64, |
| 82 | +) -> Result<ExecutableTransaction, TransactionCreationError> { |
| 83 | + dummy_eip155_transaction_with_price_limit_and_value( |
| 84 | + caller, |
| 85 | + nonce, |
| 86 | + gas_price, |
| 87 | + gas_limit, |
| 88 | + U256::ZERO, |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +/// Creates a dummy EIP-155 transaction with the provided gas price, gas limit, |
| 93 | +/// and value. |
| 94 | +pub fn dummy_eip155_transaction_with_price_limit_and_value( |
| 95 | + caller: Address, |
| 96 | + nonce: u64, |
| 97 | + gas_price: U256, |
| 98 | + gas_limit: u64, |
| 99 | + value: U256, |
| 100 | +) -> Result<ExecutableTransaction, TransactionCreationError> { |
| 101 | + let from = Address::random(); |
| 102 | + let request = Eip155TransactionRequest { |
| 103 | + nonce, |
| 104 | + gas_price, |
| 105 | + gas_limit, |
| 106 | + kind: TransactionKind::Call(from), |
| 107 | + value, |
| 108 | + input: Bytes::new(), |
| 109 | + chain_id: 123, |
| 110 | + }; |
| 111 | + let transaction = request.fake_sign(&caller); |
| 112 | + |
| 113 | + ExecutableTransaction::with_caller(SpecId::LATEST, transaction.into(), caller) |
| 114 | +} |
| 115 | + |
| 116 | +/// Creates a dummy EIP-1559 transaction with the provided max fee and max |
| 117 | +/// priority fee per gas. |
| 118 | +pub fn dummy_eip1559_transaction( |
| 119 | + caller: Address, |
| 120 | + nonce: u64, |
| 121 | + max_fee_per_gas: U256, |
| 122 | + max_priority_fee_per_gas: U256, |
| 123 | +) -> Result<ExecutableTransaction, TransactionCreationError> { |
| 124 | + let from = Address::random(); |
| 125 | + let request = Eip1559TransactionRequest { |
| 126 | + chain_id: 123, |
| 127 | + nonce, |
| 128 | + max_priority_fee_per_gas, |
| 129 | + max_fee_per_gas, |
| 130 | + gas_limit: 30_000, |
| 131 | + kind: TransactionKind::Call(from), |
| 132 | + value: U256::ZERO, |
| 133 | + input: Bytes::new(), |
| 134 | + access_list: Vec::new(), |
| 135 | + }; |
| 136 | + let transaction = request.fake_sign(&caller); |
| 137 | + |
| 138 | + ExecutableTransaction::with_caller(SpecId::LATEST, transaction.into(), caller) |
| 139 | +} |
0 commit comments