From dde24206f7825cf2628f3ce9789a0eb3b2193402 Mon Sep 17 00:00:00 2001 From: sword_smith Date: Tue, 7 May 2024 22:51:58 +0200 Subject: [PATCH] Fix tests after redefining behavior of `set_new_tip` After redefining the behavior of the `set_new_tip` to update global state with a new block, the function formerly known as `store_block`, we need to rearrange the order in which blocks are added in a few tests. With these changes, all tests pass, and #143 is closed. --- src/models/state/wallet/mod.rs | 13 +++++++------ src/peer_loop.rs | 15 +++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/models/state/wallet/mod.rs b/src/models/state/wallet/mod.rs index e5de44b3..9bcf4f74 100644 --- a/src/models/state/wallet/mod.rs +++ b/src/models/state/wallet/mod.rs @@ -804,10 +804,6 @@ mod wallet_tests { #[traced_test] #[tokio::test] async fn wallet_state_maintanence_multiple_inputs_outputs_test() -> Result<()> { - // An archival state is needed for how we currently add inputs to a transaction. - // So it's just used to generate test data, not in any of the functions that are - // actually tested. - let mut rng = thread_rng(); let network = Network::RegTest; let own_wallet_secret = WalletSecret::new_random(); @@ -820,10 +816,10 @@ mod wallet_tests { let premine_wallet = mock_genesis_wallet_state(WalletSecret::devnet_wallet(), network) .await .wallet_secret; - let premine_receiver_global_state_lock = + let premine_receiver_global_state = mock_genesis_global_state(network, 2, premine_wallet).await; let mut premine_receiver_global_state = - premine_receiver_global_state_lock.lock_guard_mut().await; + premine_receiver_global_state.lock_guard_mut().await; let launch = genesis_block.kernel.header.timestamp; let seven_months = Timestamp::months(7); let preminers_original_balance = premine_receiver_global_state @@ -921,6 +917,11 @@ mod wallet_tests { "Preminer must have spent 15: 12 + 1 for sent, 2 for fees" ); + own_wallet_state + .update_wallet_state_with_new_block(&previous_msa, &block_1) + .await + .unwrap(); + // Verify that update added 4 UTXOs to list of monitored transactions: // three as regular outputs, and one as coinbase UTXO monitored_utxos = get_monitored_utxos(&own_wallet_state).await; diff --git a/src/peer_loop.rs b/src/peer_loop.rs index e9b35b66..e142fa39 100644 --- a/src/peer_loop.rs +++ b/src/peer_loop.rs @@ -1601,8 +1601,9 @@ mod peer_loop_tests { #[traced_test] #[tokio::test] async fn block_request_batch_out_of_order_test() -> Result<()> { - // Scenario: Same as above, but the peer supplies their hashes in a wrong order. - // Ensure that the correct blocks are returned, in the right order. + // Scenario: A fork began at block 2, node knows two blocks of height 2 and two of height 3. + // A peer requests a batch of blocks starting from block 1, but the peer supplies their + // hashes in a wrong order. Ensure that the correct blocks are returned, in the right order. let mut rng = thread_rng(); let network = Network::Alpha; @@ -1626,9 +1627,9 @@ mod peer_loop_tests { global_state_mut.set_new_tip(block_1.clone()).await?; global_state_mut.set_new_tip(block_2_a.clone()).await?; - global_state_mut.set_new_tip(block_3_a.clone()).await?; global_state_mut.set_new_tip(block_2_b.clone()).await?; global_state_mut.set_new_tip(block_3_b.clone()).await?; + global_state_mut.set_new_tip(block_3_a.clone()).await?; drop(global_state_mut); @@ -1664,9 +1665,11 @@ mod peer_loop_tests { #[traced_test] #[tokio::test] async fn find_canonical_chain_when_multiple_blocks_at_same_height_test() -> Result<()> { - let mut rng = thread_rng(); // Scenario: A fork began at block 2, node knows two blocks of height 2 and two of height 3. - // A peer requests a block at height 2. Verify that the correct block at height 2 is returned. + // A peer requests a block at height 2. Verify that the correct block at height 2 is + // returned. + + let mut rng = thread_rng(); let network = Network::Alpha; let (_peer_broadcast_tx, from_main_rx_clone, to_main_tx, _to_main_rx1, state_lock, hsd) = get_test_genesis_setup(network, 0).await?; @@ -1688,9 +1691,9 @@ mod peer_loop_tests { global_state_mut.set_new_tip(block_1.clone()).await?; global_state_mut.set_new_tip(block_2_a.clone()).await?; - global_state_mut.set_new_tip(block_3_a.clone()).await?; global_state_mut.set_new_tip(block_2_b.clone()).await?; global_state_mut.set_new_tip(block_3_b.clone()).await?; + global_state_mut.set_new_tip(block_3_a.clone()).await?; drop(global_state_mut);