Skip to content

Commit

Permalink
[zcash#1411] Refactor AccountBalance to use Balance for transparent f…
Browse files Browse the repository at this point in the history
…unds

will eventually close zcash#1411
  • Loading branch information
pacu committed Oct 24, 2024
1 parent 01552bd commit 1f19f2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
39 changes: 31 additions & 8 deletions zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,19 @@ pub struct AccountBalance {
/// possible operation on a transparent balance is to shield it, it is possible to create a
/// zero-conf transaction to perform that shielding, and the resulting shielded notes will be
/// subject to normal confirmation rules.
unshielded: NonNegativeAmount,
unshielded_balance: Balance,
}

impl AccountBalance {
/// The [`Balance`] value having zero values for all its fields.
pub const ZERO: Self = Self {
sapling_balance: Balance::ZERO,
orchard_balance: Balance::ZERO,
unshielded: NonNegativeAmount::ZERO,
unshielded_balance: Balance::ZERO,
};

fn check_total(&self) -> Result<NonNegativeAmount, BalanceError> {
(self.sapling_balance.total() + self.orchard_balance.total() + self.unshielded)
(self.sapling_balance.total() + self.orchard_balance.total() + self.unshielded_balance.total())
.ok_or(BalanceError::Overflow)
}

Expand Down Expand Up @@ -288,22 +288,45 @@ impl AccountBalance {
Ok(result)
}


/// Returns the total value of unspent transparent transaction outputs belonging to the wallet.
#[deprecated(
note = "this function is deprecated. Please use AccountBalance::unshielded_balance instead."
)]
pub fn unshielded(&self) -> NonNegativeAmount {
self.unshielded
self.unshielded_balance.total()
}

/// Returns the [`Balance`] of unshielded funds in the account.
pub fn unshielded_balance(&self) -> &Balance {
&self.unshielded_balance
}

/// Provides a `mutable reference to the [`Balance`] of transparent funds in the account
/// to the specified callback, checking invariants after the callback's action has been
/// evaluated.
pub fn with_unshielded_balance_mut<A, E: From<BalanceError>>(
&mut self,
f: impl FnOnce(&mut Balance) -> Result<A, E>,
) -> Result<A, E> {
let result = f(&mut self.unshielded_balance)?;
self.check_total()?;
Ok(result)
}

/// Adds the specified value to the unshielded total, checking for overflow of
/// the total account balance.
pub fn add_unshielded_value(&mut self, value: NonNegativeAmount) -> Result<(), BalanceError> {
self.unshielded = (self.unshielded + value).ok_or(BalanceError::Overflow)?;
self.check_total()?;
#[deprecated(
note = "this function is deprecated. Please use the `Balance::add_spendable_value` on the unshielded field instead instead."
)]
pub fn add_unshielded_value(&mut self, value: NonNegativeAmount) -> Result<(), BalanceError> {
self.unshielded_balance.add_pending_spendable_value(value)?;
Ok(())
}

/// Returns the total value of funds belonging to the account.
pub fn total(&self) -> NonNegativeAmount {
(self.sapling_balance.total() + self.orchard_balance.total() + self.unshielded)
(self.sapling_balance.total() + self.orchard_balance.total() + self.unshielded_balance.total())
.expect("Account balance cannot overflow MAX_MONEY")
}

Expand Down
5 changes: 4 additions & 1 deletion zcash_client_sqlite/src/wallet/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ pub(crate) fn add_transparent_account_balances(
account_balances
.entry(account)
.or_insert(AccountBalance::ZERO)
.add_unshielded_value(value)?;
.with_unshielded_balance_mut::<_, SqliteClientError>(|bal| {
bal.add_pending_spendable_value(value)?;
Ok(())
})?;
}
Ok(())
}
Expand Down

0 comments on commit 1f19f2c

Please sign in to comment.