Skip to content

Commit

Permalink
zcash_client_sqlite: Make scan_cached_blocks_detects_spends_out_of_or…
Browse files Browse the repository at this point in the history
…der a common single-pool test
  • Loading branch information
nuttycom committed Mar 11, 2024
1 parent 2a10c11 commit d260064
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
37 changes: 0 additions & 37 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
39 changes: 39 additions & 0 deletions zcash_client_sqlite/src/testing/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,3 +1396,42 @@ pub(crate) fn checkpoint_gaps<T: ShieldedPoolTester>() {
Ok(_)
);
}

pub(crate) fn scan_cached_blocks_detects_spends_out_of_order<T: ShieldedPoolTester>() {
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(&not_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());
}
5 changes: 5 additions & 0 deletions zcash_client_sqlite/src/wallet/orchard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,9 @@ pub(crate) mod tests {
fn checkpoint_gaps() {
testing::pool::checkpoint_gaps::<OrchardPoolTester>()
}

#[test]
fn scan_cached_blocks_detects_spends_out_of_order() {
testing::pool::scan_cached_blocks_detects_spends_out_of_order::<OrchardPoolTester>()
}
}
5 changes: 5 additions & 0 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,9 @@ pub(crate) mod tests {
fn checkpoint_gaps() {
testing::pool::checkpoint_gaps::<SaplingPoolTester>()
}

#[test]
fn scan_cached_blocks_detects_spends_out_of_order() {
testing::pool::scan_cached_blocks_detects_spends_out_of_order::<SaplingPoolTester>()
}
}

0 comments on commit d260064

Please sign in to comment.