Skip to content

Commit

Permalink
Add TestState::{get_balance, get_balance_at} convenience methods.
Browse files Browse the repository at this point in the history
Signed-off-by: Daira Emma Hopwood <[email protected]>
  • Loading branch information
daira committed Aug 30, 2023
1 parent 31e5a51 commit 3cf9f62
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
22 changes: 21 additions & 1 deletion zcash_client_sqlite/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ use zcash_primitives::{
use crate::{
chain::init::init_cache_database,
error::SqliteClientError,
wallet::{commitment_tree, init::init_wallet_db, sapling::tests::test_prover},
wallet::{
commitment_tree, get_balance, get_balance_at, init::init_wallet_db,
sapling::tests::test_prover,
},
AccountId, ReceivedNoteId, WalletDb,
};

Expand Down Expand Up @@ -323,6 +326,23 @@ impl<Cache> TestState<Cache> {
.and_then(|(_, usk, _)| usk.to_unified_full_viewing_key().sapling().cloned())
}

/// Returns the balance for the account, including all mined unspent notes
/// that we know about.
pub(crate) fn get_balance(&self, account: AccountId) -> Result<Amount, SqliteClientError> {
get_balance(&self.wallet().conn, account)
}

/// Returns the verified balance for the account at the specified height,
/// This may be used to obtain a balance that ignores notes that have been
/// received so recently that they are not yet deemed spendable.
pub(crate) fn get_balance_at(
&self,
account: AccountId,
height: BlockHeight,
) -> Result<Amount, SqliteClientError> {
get_balance_at(&self.wallet().conn, account, height)
}

/// Invokes [`create_spend_to_address`] with the given arguments.
#[allow(deprecated)]
#[allow(clippy::type_complexity)]
Expand Down
62 changes: 22 additions & 40 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ pub(crate) mod tests {
use crate::{
error::SqliteClientError,
testing::{AddressType, BlockCache, TestBuilder, TestState},
wallet::{commitment_tree, get_balance, get_balance_at},
wallet::commitment_tree,
AccountId, NoteId, ReceivedNoteId,
};

Expand Down Expand Up @@ -479,12 +479,10 @@ pub(crate) mod tests {
.get_target_and_anchor_heights(NonZeroU32::new(1).unwrap())
.unwrap()
.unwrap();
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
value
);
assert_eq!(
get_balance_at(&st.wallet().conn, AccountId::from(0), anchor_height).unwrap(),
st.get_balance_at(AccountId::from(0), anchor_height)
.unwrap(),
value
);

Expand Down Expand Up @@ -646,10 +644,7 @@ pub(crate) mod tests {
let to = dfvk.default_address().1.into();

// Account balance should be zero
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
Amount::zero()
);
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), Amount::zero());

// We cannot do anything if we aren't synchronised
assert_matches!(
Expand Down Expand Up @@ -685,12 +680,10 @@ pub(crate) mod tests {
.get_target_and_anchor_heights(NonZeroU32::new(10).unwrap())
.unwrap()
.unwrap();
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
value
);
assert_eq!(
get_balance_at(&st.wallet().conn, AccountId::from(0), anchor_height).unwrap(),
st.get_balance_at(AccountId::from(0), anchor_height)
.unwrap(),
value
);

Expand All @@ -705,11 +698,12 @@ pub(crate) mod tests {
.unwrap()
.unwrap();
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
st.get_balance(AccountId::from(0)).unwrap(),
(value + value).unwrap()
);
assert_eq!(
get_balance_at(&st.wallet().conn, AccountId::from(0), anchor_height2).unwrap(),
st.get_balance_at(AccountId::from(0), anchor_height2)
.unwrap(),
value
);

Expand Down Expand Up @@ -789,10 +783,7 @@ pub(crate) mod tests {
let value = Amount::from_u64(50000).unwrap();
let (h1, _, _) = st.generate_next_block(&dfvk, AddressType::DefaultExternal, value);
st.scan_cached_blocks(h1, 1);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
value
);
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), value);

// Send some of the funds to another address
let extsk2 = ExtendedSpendingKey::master(&[]);
Expand Down Expand Up @@ -887,10 +878,7 @@ pub(crate) mod tests {
let value = Amount::from_u64(50000).unwrap();
let (h1, _, _) = st.generate_next_block(&dfvk, AddressType::DefaultExternal, value);
st.scan_cached_blocks(h1, 1);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
value
);
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), value);

let extsk2 = ExtendedSpendingKey::master(&[]);
let addr2 = extsk2.default_address().1;
Expand Down Expand Up @@ -994,12 +982,10 @@ pub(crate) mod tests {
.get_target_and_anchor_heights(NonZeroU32::new(1).unwrap())
.unwrap()
.unwrap();
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
value
);
assert_eq!(
get_balance_at(&st.wallet().conn, AccountId::from(0), anchor_height).unwrap(),
st.get_balance_at(AccountId::from(0), anchor_height)
.unwrap(),
value
);

Expand Down Expand Up @@ -1037,12 +1023,10 @@ pub(crate) mod tests {
.get_target_and_anchor_heights(NonZeroU32::new(10).unwrap())
.unwrap()
.unwrap();
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
value
);
assert_eq!(
get_balance_at(&st.wallet().conn, AccountId::from(0), anchor_height).unwrap(),
st.get_balance_at(AccountId::from(0), anchor_height)
.unwrap(),
value
);

Expand Down Expand Up @@ -1094,12 +1078,10 @@ pub(crate) mod tests {
.get_target_and_anchor_heights(NonZeroU32::new(1).unwrap())
.unwrap()
.unwrap();
assert_eq!(st.get_balance(AccountId::from(0)).unwrap(), total);
assert_eq!(
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
total
);
assert_eq!(
get_balance_at(&st.wallet().conn, AccountId::from(0), anchor_height).unwrap(),
st.get_balance_at(AccountId::from(0), anchor_height)
.unwrap(),
total
);

Expand Down

0 comments on commit 3cf9f62

Please sign in to comment.