diff --git a/crates/mempool/src/mempool_test.rs b/crates/mempool/src/mempool_test.rs index 5d612343f5..8671aa5f18 100644 --- a/crates/mempool/src/mempool_test.rs +++ b/crates/mempool/src/mempool_test.rs @@ -40,7 +40,7 @@ struct FullContent; struct PartialContent; impl MempoolContent { - fn new(pool_txs: P, queue_txs: Q) -> Self + fn with_pool_and_queue(pool_txs: P, queue_txs: Q) -> Self where P: IntoIterator, // TODO(Ayelet): Consider using `&ThinTransaction` instead of `TransactionReference`. @@ -212,7 +212,8 @@ fn test_get_txs_returns_by_priority_order(#[case] requested_txs: usize) { let tx_references_iterator = txs.iter().map(TransactionReference::new); let txs_iterator = txs.iter().cloned(); - let mut mempool: Mempool = MempoolContent::new(txs_iterator, tx_references_iterator).into(); + let mut mempool: Mempool = + MempoolContent::with_pool_and_queue(txs_iterator, tx_references_iterator).into(); // Test. let fetched_txs = mempool.get_txs(requested_txs).unwrap(); @@ -228,7 +229,8 @@ fn test_get_txs_returns_by_priority_order(#[case] requested_txs: usize) { // Assert: non-returned transactions are still in the mempool. let remaining_tx_references = remaining_txs.iter().map(TransactionReference::new); - let mempool_content = MempoolContent::new(remaining_txs.to_vec(), remaining_tx_references); + let mempool_content = + MempoolContent::with_pool_and_queue(remaining_txs.to_vec(), remaining_tx_references); mempool_content.assert_eq_mempool_content(&mempool); } @@ -244,7 +246,8 @@ fn test_get_txs_multi_nonce() { let queue_txs = [&tx_nonce_0].map(TransactionReference::new); let pool_txs = [tx_nonce_0, tx_nonce_1, tx_nonce_2]; - let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs).into(); + let mut mempool: Mempool = + MempoolContent::with_pool_and_queue(pool_txs.clone(), queue_txs).into(); // Test. let fetched_txs = mempool.get_txs(3).unwrap(); @@ -268,7 +271,7 @@ fn test_get_txs_replenishes_queue_only_between_chunks() { let queue_txs = [&tx_address_0_nonce_0, &tx_address_1_nonce_0].map(TransactionReference::new); let pool_txs = [&tx_address_0_nonce_0, &tx_address_0_nonce_1, &tx_address_1_nonce_0].map(|tx| tx.clone()); - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. let txs = mempool.get_txs(3).unwrap(); @@ -301,7 +304,7 @@ fn test_get_txs_replenishes_queue_multi_account_between_chunks() { &tx_address_1_nonce_1, ] .map(|tx| tx.clone()); - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. let txs = mempool.get_txs(2).unwrap(); @@ -313,7 +316,8 @@ fn test_get_txs_replenishes_queue_multi_account_between_chunks() { let expected_queue_txs = [&tx_address_0_nonce_1, &tx_address_1_nonce_1].map(TransactionReference::new); let expected_pool_txs = [tx_address_0_nonce_1, tx_address_1_nonce_1]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -327,7 +331,7 @@ fn test_get_txs_with_holes_multiple_accounts() { let queue_txs = [TransactionReference::new(&tx_address_1_nonce_0)]; let pool_txs = [tx_address_0_nonce_1.clone(), tx_address_1_nonce_0.clone()]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. let txs = mempool.get_txs(2).unwrap(); @@ -337,7 +341,8 @@ fn test_get_txs_with_holes_multiple_accounts() { let expected_pool_txs = [tx_address_0_nonce_1]; let expected_queue_txs = []; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -348,7 +353,8 @@ fn test_get_txs_with_holes_single_account() { let pool_txs = [input_nonce_1.tx]; let queue_txs = []; - let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs.clone()).into(); + let mut mempool: Mempool = + MempoolContent::with_pool_and_queue(pool_txs.clone(), queue_txs.clone()).into(); // Test. let txs = mempool.get_txs(1).unwrap(); @@ -356,7 +362,7 @@ fn test_get_txs_with_holes_single_account() { // Assert. assert_eq!(txs, &[]); - let expected_mempool_content = MempoolContent::new(pool_txs, queue_txs); + let expected_mempool_content = MempoolContent::with_pool_and_queue(pool_txs, queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -384,7 +390,8 @@ fn test_add_tx(mut mempool: Mempool) { let expected_queue_txs: Vec = add_tx_inputs.iter().map(|input| TransactionReference::new(&input.tx)).collect(); let expected_pool_txs = add_tx_inputs.into_iter().map(|input| input.tx); - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -408,7 +415,8 @@ fn test_add_tx_multi_nonce_success(mut mempool: Mempool) { [&input_address_1_nonce_0.tx, &input_address_0_nonce_0.tx].map(TransactionReference::new); let expected_pool_txs = [input_address_0_nonce_0.tx, input_address_1_nonce_0.tx, input_address_0_nonce_1.tx]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -441,7 +449,7 @@ fn test_add_tx_lower_than_queued_nonce() { let queue_txs = [TransactionReference::new(&valid_input.tx)]; let expected_mempool_content = MempoolContent::with_queue(queue_txs.clone()); let pool_txs = [valid_input.tx]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test and assert the original transaction remains. assert_matches!(mempool.add_tx(lower_nonce_input), Err(MempoolError::DuplicateNonce { .. })); @@ -485,7 +493,8 @@ fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) { let expected_queue_txs = [TransactionReference::new(&input1.tx), TransactionReference::new(&input2.tx)]; let expected_pool_txs = [input1.tx, input2.tx]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); // TODO: currently hash comparison tie-breaks the two. Once more robust tie-breaks are added // replace this assertion with a dedicated test. @@ -502,7 +511,7 @@ fn test_add_tx_delete_tx_with_lower_nonce_than_account_nonce() { let queue_txs = [TransactionReference::new(&tx_nonce_0_account_nonce_0.tx)]; let pool_txs = [tx_nonce_0_account_nonce_0.tx]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. add_tx(&mut mempool, &tx_nonce_1_account_nonce_1); @@ -510,7 +519,8 @@ fn test_add_tx_delete_tx_with_lower_nonce_than_account_nonce() { // Assert the transaction with the lower nonce is removed. let expected_queue_txs = [TransactionReference::new(&tx_nonce_1_account_nonce_1.tx)]; let expected_pool_txs = [tx_nonce_1_account_nonce_1.tx]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -568,7 +578,8 @@ fn test_add_tx_sequential_nonces(mut mempool: Mempool) { // Assert: only eligible transaction appears in the queue. let expected_queue_txs = [TransactionReference::new(&input_nonce_0.tx)]; let expected_pool_txs = [input_nonce_0.tx, input_nonce_1.tx]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -585,7 +596,8 @@ fn test_add_tx_filling_hole(mut mempool: Mempool) { // Assert: the second transaction is in the pool and not in the queue. let expected_queue_txs = []; let expected_pool_txs = [input_nonce_1.tx.clone()]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); // Test: add the first transaction, which fills the hole. @@ -594,7 +606,8 @@ fn test_add_tx_filling_hole(mut mempool: Mempool) { // Assert: only the eligible transaction appears in the queue. let expected_queue_txs = [TransactionReference::new(&input_nonce_0.tx)]; let expected_pool_txs = [input_nonce_1.tx, input_nonce_0.tx]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs); + let expected_mempool_content = + MempoolContent::with_pool_and_queue(expected_pool_txs, expected_queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -607,7 +620,7 @@ fn test_add_tx_after_get_txs_fails_on_duplicate_nonce() { let pool_txs = [input_tx.tx.clone()]; let queue_txs = [TransactionReference::new(&input_tx.tx)]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. mempool.get_txs(1).unwrap(); @@ -632,7 +645,8 @@ fn test_commit_block_includes_all_txs() { let queue_txs = [&tx_address0_nonce4, &tx_address1_nonce3, &tx_address2_nonce1] .map(TransactionReference::new); let pool_txs = [tx_address0_nonce4, tx_address0_nonce5, tx_address1_nonce3, tx_address2_nonce1]; - let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs.clone()).into(); + let mut mempool: Mempool = + MempoolContent::with_pool_and_queue(pool_txs.clone(), queue_txs.clone()).into(); // Test. let state_changes = HashMap::from([ @@ -642,7 +656,7 @@ fn test_commit_block_includes_all_txs() { assert!(mempool.commit_block(state_changes).is_ok()); // Assert. - let expected_mempool_content = MempoolContent::new(pool_txs, queue_txs); + let expected_mempool_content = MempoolContent::with_pool_and_queue(pool_txs, queue_txs); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -653,7 +667,7 @@ fn test_commit_block_rewinds_nonce() { let queued_txs = [TransactionReference::new(&tx_address0_nonce5)]; let pool_txs = [tx_address0_nonce5]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queued_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queued_txs).into(); // Test. let state_changes = HashMap::from([ @@ -678,7 +692,7 @@ fn test_commit_block_from_different_leader() { let queued_txs = [TransactionReference::new(&tx_address1_nonce2)]; let pool_txs = [tx_address0_nonce3, tx_address0_nonce5, tx_address0_nonce6.clone(), tx_address1_nonce2]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queued_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queued_txs).into(); // Test. let state_changes = HashMap::from([ @@ -753,7 +767,7 @@ fn test_flow_partial_commit_block() { &tx_address2_nonce2, ] .map(|tx| tx.clone()); - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. @@ -772,7 +786,7 @@ fn test_flow_partial_commit_block() { // Assert. let expected_pool_txs = [tx_address0_nonce5, tx_address0_nonce6, tx_address1_nonce2]; - let expected_mempool_content = MempoolContent::new(expected_pool_txs, []); + let expected_mempool_content = MempoolContent::with_pool_and_queue(expected_pool_txs, []); expected_mempool_content.assert_eq_mempool_content(&mempool); } @@ -787,7 +801,7 @@ fn test_flow_commit_block_closes_hole() { let queued_txs = [TransactionReference::new(&tx_nonce3)]; let pool_txs = [tx_nonce3, tx_nonce5.clone()]; - let mut mempool: Mempool = MempoolContent::new(pool_txs, queued_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queued_txs).into(); // Test. let state_changes = @@ -820,7 +834,7 @@ fn test_flow_send_same_nonce_tx_after_previous_not_included() { let queue_txs = [TransactionReference::new(&tx_nonce3)]; let pool_txs = [&tx_nonce3, &tx_input_nonce4.tx, &tx_nonce5].map(|tx| tx.clone()); - let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into(); + let mut mempool: Mempool = MempoolContent::with_pool_and_queue(pool_txs, queue_txs).into(); // Test. let txs = mempool.get_txs(2).unwrap();