From 08d131bbd40c7c91b62ae408e28377e8bc400eb0 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Sun, 10 Mar 2024 18:47:59 -0600 Subject: [PATCH] zcash_client_sqlite: Make scan_cached_blocks_detects_spends_out_of_order a common single-pool test --- zcash_client_sqlite/src/chain.rs | 37 --------------------- zcash_client_sqlite/src/testing/pool.rs | 39 +++++++++++++++++++++++ zcash_client_sqlite/src/wallet/orchard.rs | 5 +++ zcash_client_sqlite/src/wallet/sapling.rs | 5 +++ 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index 38b32d3c1b..284bc11de3 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -613,41 +613,4 @@ mod tests { // Account balance should equal the change assert_eq!(st.get_total_balance(account.0), (value - value2).unwrap()); } - - #[test] - fn scan_cached_blocks_detects_spends_out_of_order() { - let mut st = TestBuilder::new() - .with_block_cache() - .with_test_account(AccountBirthday::from_sapling_activation) - .build(); - let account = st.test_account().unwrap(); - - let dfvk = st.test_account_sapling().unwrap(); - - // Wallet summary is not yet available - assert_eq!(st.get_wallet_summary(0), None); - - // Create a fake CompactBlock sending value to the address - let value = NonNegativeAmount::const_from_u64(5); - let (received_height, _, nf) = - st.generate_next_block(&dfvk, AddressType::DefaultExternal, value); - - // Create a second fake CompactBlock spending value from the address - let extsk2 = ExtendedSpendingKey::master(&[0]); - let to2 = extsk2.default_address().1; - let value2 = NonNegativeAmount::const_from_u64(2); - let (spent_height, _) = st.generate_next_block_spending(&dfvk, (nf, value), to2, value2); - - // Scan the spending block first. - st.scan_cached_blocks(spent_height, 1); - - // Account balance should equal the change - assert_eq!(st.get_total_balance(account.0), (value - value2).unwrap()); - - // Now scan the block in which we received the note that was spent. - st.scan_cached_blocks(received_height, 1); - - // Account balance should be the same. - assert_eq!(st.get_total_balance(account.0), (value - value2).unwrap()); - } } diff --git a/zcash_client_sqlite/src/testing/pool.rs b/zcash_client_sqlite/src/testing/pool.rs index 16150165fa..309840e08b 100644 --- a/zcash_client_sqlite/src/testing/pool.rs +++ b/zcash_client_sqlite/src/testing/pool.rs @@ -1396,3 +1396,42 @@ pub(crate) fn checkpoint_gaps() { Ok(_) ); } + +pub(crate) fn scan_cached_blocks_detects_spends_out_of_order() { + let mut st = TestBuilder::new() + .with_block_cache() + .with_test_account(AccountBirthday::from_sapling_activation) + .build(); + + let account = st.test_account().unwrap(); + let dfvk = T::test_account_fvk(&st); + + // Wallet summary is not yet available + assert_eq!(st.get_wallet_summary(0), None); + + // Create a fake CompactBlock sending value to the address + let value = NonNegativeAmount::const_from_u64(5); + let (received_height, _, nf) = st.generate_next_block( + &dfvk, + AddressType::DefaultExternal, + value + ); + + // Create a second fake CompactBlock spending value from the address + let not_our_key = T::sk_to_fvk(&T::sk(&[0xf5; 32])); + let to2 = T::fvk_default_address(¬_our_key); + let value2 = NonNegativeAmount::const_from_u64(2); + let (spent_height, _) = st.generate_next_block_spending(&dfvk, (nf, value), to2, value2); + + // Scan the spending block first. + st.scan_cached_blocks(spent_height, 1); + + // Account balance should equal the change + assert_eq!(st.get_total_balance(account.0), (value - value2).unwrap()); + + // Now scan the block in which we received the note that was spent. + st.scan_cached_blocks(received_height, 1); + + // Account balance should be the same. + assert_eq!(st.get_total_balance(account.0), (value - value2).unwrap()); +} diff --git a/zcash_client_sqlite/src/wallet/orchard.rs b/zcash_client_sqlite/src/wallet/orchard.rs index 8f9c797307..1b9f3f7635 100644 --- a/zcash_client_sqlite/src/wallet/orchard.rs +++ b/zcash_client_sqlite/src/wallet/orchard.rs @@ -588,4 +588,9 @@ pub(crate) mod tests { fn checkpoint_gaps() { testing::pool::checkpoint_gaps::() } + + #[test] + fn scan_cached_blocks_detects_spends_out_of_order() { + testing::pool::scan_cached_blocks_detects_spends_out_of_order::() + } } diff --git a/zcash_client_sqlite/src/wallet/sapling.rs b/zcash_client_sqlite/src/wallet/sapling.rs index 889e2f07fe..b30aa17041 100644 --- a/zcash_client_sqlite/src/wallet/sapling.rs +++ b/zcash_client_sqlite/src/wallet/sapling.rs @@ -591,4 +591,9 @@ pub(crate) mod tests { fn checkpoint_gaps() { testing::pool::checkpoint_gaps::() } + + #[test] + fn scan_cached_blocks_detects_spends_out_of_order() { + testing::pool::scan_cached_blocks_detects_spends_out_of_order::() + } }