diff --git a/tests/tests/mempool.rs b/tests/tests/mempool.rs index 34c8544db..a93c9c12f 100644 --- a/tests/tests/mempool.rs +++ b/tests/tests/mempool.rs @@ -21,7 +21,7 @@ async fn test_mempool_add_transaction(#[future] katana: Katana, _setup: ()) { let eth_provider = katana.eth_provider(); // Create a sample transaction - let (transaction, _) = create_sample_transactions(&katana, 1) + let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) .await .expect("Failed to create sample transaction") .pop() @@ -39,10 +39,57 @@ async fn test_mempool_add_transaction(#[future] katana: Katana, _setup: ()) { // Get updated mempool size let mempool_size = eth_provider.mempool().unwrap().pool_size(); + // Get the EOA address + let address = katana.eoa().evm_address().expect("Failed to get eoa address"); + + // Get transactions by sender address and nonce + let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender_and_nonce(address, 0); + + // Get transactions by origin + let origin_transaction = eth_provider.mempool().unwrap().get_transactions_by_origin(TransactionOrigin::Local); + + // Get local transactions + let local_transaction = eth_provider.mempool().unwrap().get_local_transactions(); + + // Get all transactions in the mempool + let all_transactions = eth_provider.mempool().unwrap().all_transactions(); + // Check pending, queued and total transactions assert_eq!(mempool_size.pending, 1); assert_eq!(mempool_size.queued, 0); assert_eq!(mempool_size.total, 1); + + // get_transactions_by_sender_and_nonce test + // Check if the returned transaction hash matches + assert_eq!(*sender_transactions.unwrap().hash(), transaction_signed.hash()); + + // get_transactions_by_origin function test + // Check if the returned transaction hash matches + assert_eq!(*origin_transaction[0].hash(), transaction_signed.hash()); + + // get_local_transactions function test + // Check if the returned transaction hash matches + assert_eq!(*local_transaction[0].hash(), transaction_signed.hash()); + assert_eq!(*local_transaction[0].hash(), *origin_transaction[0].hash()); + + // Remove transaction by hash + let _ = eth_provider.mempool().unwrap().remove_transactions(vec![transaction_signed.hash()]); + + // remove_transactions function tests + // Get updated mempool size + let mempool_size = eth_provider.mempool().unwrap().pool_size(); + // Check pending, queued and total transactions after remove_transactions + assert_eq!(mempool_size.pending, 0); + assert_eq!(mempool_size.queued, 0); + assert_eq!(mempool_size.total, 0); + + // all_transactions function tests + // Check if the first pending transaction hash matches + assert_eq!(*all_transactions.pending[0].hash(), transaction_signed.hash()); + // Ensure only one pending transaction is present + assert_eq!(all_transactions.pending.len(), 1); + // Ensure no queued transactions are present + assert_eq!(all_transactions.queued.len(), 0); } #[rstest] @@ -52,7 +99,7 @@ async fn test_mempool_add_external_transaction(#[future] katana: Katana, _setup: let eth_provider = katana.eth_provider(); // Create a sample transaction - let (transaction, _) = create_sample_transactions(&katana, 1) + let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) .await .expect("Failed to create sample transaction") .pop() @@ -61,6 +108,9 @@ async fn test_mempool_add_external_transaction(#[future] katana: Katana, _setup: // Add external transaction let result = eth_provider.mempool().unwrap().add_external_transaction(transaction).await; + // Get pooled transaction by hash + let hashes = eth_provider.mempool().unwrap().get_pooled_transaction_element(transaction_signed.hash()); + // Ensure the transaction was added successfully assert!(result.is_ok()); @@ -71,6 +121,10 @@ async fn test_mempool_add_external_transaction(#[future] katana: Katana, _setup: assert_eq!(mempool_size.pending, 1); assert_eq!(mempool_size.queued, 0); assert_eq!(mempool_size.total, 1); + + // get_pooled_transaction_element function test + // Check if the retrieved hash matches the expected hash + assert_eq!(hashes.unwrap().hash(), &transaction_signed.hash()); } #[rstest] @@ -78,25 +132,70 @@ async fn test_mempool_add_external_transaction(#[future] katana: Katana, _setup: #[tokio::test(flavor = "multi_thread")] async fn test_mempool_add_transactions(#[future] katana: Katana, _setup: ()) { let eth_provider = katana.eth_provider(); + // Get the EOA address + let address = katana.eoa().evm_address().expect("Failed to get eoa address"); + + // Set the number of transactions to create + let transaction_number = 2; // Create multiple sample transactions - let transactions = create_sample_transactions(&katana, 2).await.expect("Failed to create sample transaction"); + let transactions = + create_sample_transactions(&katana, transaction_number).await.expect("Failed to create sample transaction"); // Collect pooled transactions - let transactions = + let pooled_transactions = transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); + // Collect signed transactions + let signed_transactions = + transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); + // Add transactions to mempool - let _ = eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, transactions).await; + let _ = eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions).await; + + // Get pending transactions + let hashes = eth_provider.mempool().unwrap().pending_transactions(); + + // Get transactions by sender address + let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender(address); + + // Get unique senders from the mempool + let unique_senders = eth_provider.mempool().unwrap().unique_senders(); + + // Check if the first signed transaction is contained + let contains = eth_provider.mempool().unwrap().contains(&signed_transactions[0].hash()); // Get updated mempool size let mempool_size = eth_provider.mempool().unwrap().pool_size(); + // mempool_size function tests // Check pending transactions - assert_eq!(mempool_size.pending, 2); + assert_eq!(mempool_size.pending, transaction_number); // Check queued transactions assert_eq!(mempool_size.queued, 0); // Check total transactions - assert_eq!(mempool_size.total, 2); + assert_eq!(mempool_size.total, transaction_number); + + // pending_transactions function tests + // Check if the first pending transaction hash matches + assert_eq!(hashes[0].hash(), &signed_transactions[0].hash()); + // Check if the second pending transaction hash matches + assert_eq!(hashes[1].hash(), &signed_transactions[1].hash()); + // Ensure the number of pending transactions matches the expected count + assert_eq!(hashes.len(), transaction_number); + + // get_transactions_by_sender function tests + // Ensure only one transaction is returned + assert_eq!(sender_transactions.len(), transaction_number); + // Check if the returned transaction hash matches + assert_eq!(*sender_transactions[0].hash(), signed_transactions[0].hash()); + assert_eq!(*sender_transactions[1].hash(), signed_transactions[1].hash()); + + // unique_senders function test + // Ensure the EOA address is in the unique senders + assert!(unique_senders.contains(&address)); + + // contains function test + assert!(contains); } #[rstest] @@ -109,11 +208,27 @@ async fn test_mempool_add_external_transactions(#[future] katana: Katana, _setup let transactions = create_sample_transactions(&katana, 2).await.expect("Failed to create sample transaction"); // Collect pooled transactions - let transactions = + let pooled_transactions = transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); + // Collect signed transactions + let signed_transactions = + transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); + // Add external transactions to mempool - let _ = eth_provider.mempool().unwrap().add_external_transactions(transactions).await; + let _ = eth_provider.mempool().unwrap().add_external_transactions(pooled_transactions).await; + + // Get pooled transaction hashes + let hashes = eth_provider.mempool().unwrap().pooled_transaction_hashes(); + + // Set maximum number of hashes to retrieve + let hashes_max_number = 1; + + // Get pooled transaction hashes with a limit + let hashes_max = eth_provider.mempool().unwrap().pooled_transaction_hashes_max(hashes_max_number); + + // Get external transactions + let external_transactions = eth_provider.mempool().unwrap().get_external_transactions(); // Get updated mempool size let mempool_size = eth_provider.mempool().unwrap().pool_size(); @@ -123,6 +238,27 @@ async fn test_mempool_add_external_transactions(#[future] katana: Katana, _setup assert_eq!(mempool_size.queued, 0); // Check total transactions assert_eq!(mempool_size.total, 2); + + // pooled_transaction_hashes function tests + // Check if the first signed transaction hash is present + assert!(hashes.contains(&signed_transactions[0].hash())); + // Check if the second signed transaction hash is present + assert!(hashes.contains(&signed_transactions[1].hash())); + // Ensure the hashes are not empty + assert!(!hashes.is_empty()); + + // pooled_transaction_hashes_max function test + // Check if at least one signed transaction hash is present + assert!(hashes_max.contains(&signed_transactions[0].hash()) || hashes_max.contains(&signed_transactions[1].hash())); + // Ensure the number of hashes matches the limit + assert_eq!(hashes_max.len(), hashes_max_number); + // Ensure the hashes are not empty + assert!(!hashes_max.is_empty()); + + // get_external_transactions function test + // Check if the returned transaction hash matches + assert_eq!(*external_transactions[0].hash(), signed_transactions[0].hash()); + assert_eq!(*external_transactions[1].hash(), signed_transactions[1].hash()); } #[rstest] @@ -184,246 +320,6 @@ async fn test_mempool_transaction_event_listener(#[future] katana: Katana, _setu assert_eq!(listener.unwrap().hash(), transaction_signed.hash()); } -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_pooled_transaction_hashes(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create multiple sample transactions - let transactions = create_sample_transactions(&katana, 2).await.expect("Failed to create sample transaction"); - - // Collect pooled transactions - let pooled_transactions = - transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); - - // Collect signed transactions - let signed_transactions = - transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); - - // Add transactions to mempool - let _ = eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions).await; - - // Get pooled transaction hashes - let hashes = eth_provider.mempool().unwrap().pooled_transaction_hashes(); - - // Check if the first signed transaction hash is present - assert!(hashes.contains(&signed_transactions[0].hash())); - - // Check if the second signed transaction hash is present - assert!(hashes.contains(&signed_transactions[1].hash())); - - // Ensure the hashes are not empty - assert!(!hashes.is_empty()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_pooled_transaction_hashes_max(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create multiple sample transactions - let transactions = create_sample_transactions(&katana, 2).await.expect("Failed to create sample transaction"); - - // Collect pooled transactions - let pooled_transactions = - transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); - - // Collect signed transactions - let signed_transactions = - transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); - - // Add transactions to mempool - let _ = eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions).await; - - // Set maximum number of hashes to retrieve - let hashes_max = 1; - - // Get pooled transaction hashes with a limit - let hashes = eth_provider.mempool().unwrap().pooled_transaction_hashes_max(hashes_max); - - // Check if at least one signed transaction hash is present - assert!(hashes.contains(&signed_transactions[0].hash()) || hashes.contains(&signed_transactions[1].hash())); - - // Ensure the number of hashes matches the limit - assert_eq!(hashes.len(), hashes_max); - - // Ensure the hashes are not empty - assert!(!hashes.is_empty()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_get_pooled_transaction(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create multiple sample transactions - let transactions = create_sample_transactions(&katana, 2).await.expect("Failed to create sample transaction"); - - // Collect pooled transactions - let pooled_transactions = - transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); - - // Collect signed transactions - let signed_transactions = - transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); - - // Add transactions to mempool - let _ = eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions).await; - - // Get pooled transaction by hash - let hashes = eth_provider.mempool().unwrap().get_pooled_transaction_element(signed_transactions[0].hash()); - - // Check if the retrieved hash matches the expected hash - assert_eq!(hashes.unwrap().hash(), &signed_transactions[0].hash()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_pending_transactions(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Set the number of transactions to create - let transaction_number = 2; - - // Create multiple sample transactions - let transactions = - create_sample_transactions(&katana, transaction_number).await.expect("Failed to create sample transaction"); - - // Collect pooled transactions - let pooled_transactions = - transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); - - // Collect signed transactions - let signed_transactions = - transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); - - // Add transactions to mempool - let _ = eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions).await; - - // Get pending transactions - let hashes = eth_provider.mempool().unwrap().pending_transactions(); - - // Check if the first pending transaction hash matches - assert_eq!(hashes[0].hash(), &signed_transactions[0].hash()); - - // Check if the second pending transaction hash matches - assert_eq!(hashes[1].hash(), &signed_transactions[1].hash()); - - // Ensure the number of pending transactions matches the expected count - assert_eq!(hashes.len(), transaction_number); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_get_transactions_by_sender(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Local, transaction.clone()).await.unwrap(); - - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Get transactions by sender address - let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender(address); - - // Ensure only one transaction is returned - assert_eq!(sender_transactions.len(), 1); - - // Check if the returned transaction hash matches - assert_eq!(*sender_transactions[0].hash(), transaction_signed.hash()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_get_transactions_by_sender_and_nonce(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Local, transaction.clone()).await.unwrap(); - - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Get transactions by sender address and nonce - let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender_and_nonce(address, 0); - - // Check if the returned transaction hash matches - assert_eq!(*sender_transactions.unwrap().hash(), transaction_signed.hash()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn get_transactions_by_origin(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Local, transaction.clone()).await.unwrap(); - - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Get transactions by sender address and nonce - let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender_and_nonce(address, 0); - - // Check if the returned transaction hash matches - assert_eq!(*sender_transactions.unwrap().hash(), transaction_signed.hash()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_get_local_transactions(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Local, transaction.clone()).await.unwrap(); - - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Get transactions by sender address and nonce - let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender_and_nonce(address, 0); - - // Check if the returned transaction hash matches - assert_eq!(*sender_transactions.unwrap().hash(), transaction_signed.hash()); -} - #[rstest] #[awt] #[tokio::test(flavor = "multi_thread")] @@ -440,167 +336,11 @@ async fn test_mempool_get_private_transactions(#[future] katana: Katana, _setup: // Add private transaction to mempool eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Private, transaction.clone()).await.unwrap(); - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Get transactions by sender address and nonce - let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender_and_nonce(address, 0); + // Get private transactions + let private_transaction = eth_provider.mempool().unwrap().get_private_transactions(); // Check if the returned transaction hash matches - assert_eq!(*sender_transactions.unwrap().hash(), transaction_signed.hash()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_get_external_transactions(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add external transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::External, transaction.clone()).await.unwrap(); - - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Get transactions by sender address and nonce - let sender_transactions = eth_provider.mempool().unwrap().get_transactions_by_sender_and_nonce(address, 0); - - // Check if the returned transaction hash matches - assert_eq!(*sender_transactions.unwrap().hash(), transaction_signed.hash()); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_remove_transactions(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Local, transaction.clone()).await.unwrap(); - - // Check initial pool size - assert_eq!(eth_provider.mempool().unwrap().pool_size().total, 1); - - // Remove transaction by hash - let removed_transactions = eth_provider.mempool().unwrap().remove_transactions(vec![transaction_signed.hash()]); - - // Ensure one transaction was removed - assert_eq!(removed_transactions.len(), 1); - - // Get updated mempool size - let mempool_size = eth_provider.mempool().unwrap().pool_size(); - // Check pending transactions - assert_eq!(mempool_size.pending, 0); - // Check queued transactions - assert_eq!(mempool_size.queued, 0); - // Check total transactions - assert_eq!(mempool_size.total, 0); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_all_transactions(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Create a sample transaction - let (transaction, transaction_signed) = create_sample_transactions(&katana, 1) - .await - .expect("Failed to create sample transaction") - .pop() - .expect("Expected at least one transaction"); - - // Add transaction to mempool - eth_provider.mempool().unwrap().add_transaction(TransactionOrigin::Local, transaction.clone()).await.unwrap(); - - // Get all transactions in the mempool - let all_transactions = eth_provider.mempool().unwrap().all_transactions(); - - // Check if the first pending transaction hash matches - assert_eq!(*all_transactions.pending[0].hash(), transaction_signed.hash()); - - // Ensure only one pending transaction is present - assert_eq!(all_transactions.pending.len(), 1); - - // Ensure no queued transactions are present - assert_eq!(all_transactions.queued.len(), 0); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_retain_unknown(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Set the number of transactions to create - let transaction_number = 2; - - // Create multiple sample transactions - let transactions = - create_sample_transactions(&katana, transaction_number).await.expect("Failed to create sample transaction"); - - // Collect pooled transactions - let pooled_transactions = - transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); - - // Collect signed transactions - let signed_transactions = - transactions.iter().map(|(_, signed_transactions)| signed_transactions.clone()).collect::>(); - - // Add transactions to mempool - let _ = - eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions.clone()).await; - - // Check if the first signed transaction is contained - let contains = eth_provider.mempool().unwrap().contains(&signed_transactions[0].hash()); - - // Ensure it is contained - assert!(contains); -} - -#[rstest] -#[awt] -#[tokio::test(flavor = "multi_thread")] -async fn test_mempool_unique_senders(#[future] katana: Katana, _setup: ()) { - let eth_provider = katana.eth_provider(); - - // Set the number of transactions to create - let transaction_number = 2; - - // Create multiple sample transactions - let transactions = - create_sample_transactions(&katana, transaction_number).await.expect("Failed to create sample transaction"); - - // Collect pooled transactions - let pooled_transactions = - transactions.iter().map(|(eth_pooled_transaction, _)| eth_pooled_transaction.clone()).collect::>(); - - // Get the EOA address - let address = katana.eoa().evm_address().expect("Failed to get eoa address"); - - // Add transactions to mempool - let _ = - eth_provider.mempool().unwrap().add_transactions(TransactionOrigin::Local, pooled_transactions.clone()).await; - - // Get unique senders from the mempool - let unique_senders = eth_provider.mempool().unwrap().unique_senders(); - - // Ensure the EOA address is in the unique senders - assert!(unique_senders.contains(&address)); + assert_eq!(*private_transaction[0].hash(), transaction_signed.hash()); } // Helper function to create a sample transaction