Skip to content

Commit

Permalink
zcash_client_backend: Use NonNegativeAmount for `select_spendable_n…
Browse files Browse the repository at this point in the history
…otes`
  • Loading branch information
nuttycom committed Mar 8, 2024
1 parent 2c6055a commit 0f0df44
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
4 changes: 4 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ and this library adheres to Rust's notion of
- Arguments to `ScannedBlock::from_parts` have changed.
- Changes to the `WalletRead` trait:
- Added `get_orchard_nullifiers`
- Changes to the `InputSource` trait:
- `select_spendable_notes` now takes its `target_value` argument as a
`NonNegativeAmount`. Also, the values of the returned map are also
`NonNegativeAmount`s instead of `Amount`s.
- `ShieldedProtocol` has a new `Orchard` variant.
- `WalletCommitmentTrees`
- `type OrchardShardStore`
Expand Down
14 changes: 7 additions & 7 deletions zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use zcash_primitives::{
consensus::BlockHeight,
memo::{Memo, MemoBytes},
transaction::{
components::amount::{Amount, BalanceError, NonNegativeAmount},
components::amount::{BalanceError, NonNegativeAmount},
Transaction, TxId,
},
};
Expand Down Expand Up @@ -445,7 +445,7 @@ pub trait InputSource {
fn select_spendable_notes(
&self,
account: Self::AccountId,
target_value: Amount,
target_value: NonNegativeAmount,
sources: &[ShieldedProtocol],
anchor_height: BlockHeight,
exclude: &[Self::NoteRef],
Expand Down Expand Up @@ -662,7 +662,7 @@ pub trait WalletRead {
&self,
_account: Self::AccountId,
_max_height: BlockHeight,
) -> Result<HashMap<TransparentAddress, Amount>, Self::Error> {
) -> Result<HashMap<TransparentAddress, NonNegativeAmount>, Self::Error> {
Ok(HashMap::new())
}

Expand Down Expand Up @@ -891,7 +891,7 @@ pub struct SentTransaction<'a, AccountId> {
pub created: time::OffsetDateTime,
pub account: AccountId,
pub outputs: Vec<SentTransactionOutput<AccountId>>,
pub fee_amount: Amount,
pub fee_amount: NonNegativeAmount,
#[cfg(feature = "transparent-inputs")]
pub utxos_spent: Vec<OutPoint>,
}
Expand Down Expand Up @@ -1277,7 +1277,7 @@ pub mod testing {
block::BlockHash,
consensus::{BlockHeight, Network},
memo::Memo,
transaction::{components::Amount, Transaction, TxId},
transaction::{components::amount::NonNegativeAmount, Transaction, TxId},
};

use crate::{
Expand Down Expand Up @@ -1342,7 +1342,7 @@ pub mod testing {
fn select_spendable_notes(
&self,
_account: Self::AccountId,
_target_value: Amount,
_target_value: NonNegativeAmount,
_sources: &[ShieldedProtocol],
_anchor_height: BlockHeight,
_exclude: &[Self::NoteRef],
Expand Down Expand Up @@ -1487,7 +1487,7 @@ pub mod testing {
&self,
_account: Self::AccountId,
_max_height: BlockHeight,
) -> Result<HashMap<TransparentAddress, Amount>, Self::Error> {
) -> Result<HashMap<TransparentAddress, NonNegativeAmount>, Self::Error> {
Ok(HashMap::new())
}

Expand Down
7 changes: 2 additions & 5 deletions zcash_client_backend/src/data_api/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ use zcash_primitives::{
memo::MemoBytes,
transaction::{
builder::{BuildConfig, BuildResult, Builder},
components::{
amount::{Amount, NonNegativeAmount},
sapling::zip212_enforcement,
},
components::{amount::NonNegativeAmount, sapling::zip212_enforcement},
fees::{zip317::FeeError as Zip317FeeError, FeeRule, StandardFeeRule},
Transaction, TxId,
},
Expand Down Expand Up @@ -1181,7 +1178,7 @@ where
created: time::OffsetDateTime::now_utc(),
account,
outputs,
fee_amount: Amount::from(proposal_step.balance().fee_required()),
fee_amount: proposal_step.balance().fee_required(),
#[cfg(feature = "transparent-inputs")]
utxos_spent,
})
Expand Down
9 changes: 3 additions & 6 deletions zcash_client_sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ use zcash_primitives::{
block::BlockHash,
consensus::{self, BlockHeight},
memo::{Memo, MemoBytes},
transaction::{
components::amount::{Amount, NonNegativeAmount},
Transaction, TxId,
},
transaction::{components::amount::NonNegativeAmount, Transaction, TxId},
zip32::{AccountId, DiversifierIndex, Scope},
};

Expand Down Expand Up @@ -203,7 +200,7 @@ impl<C: Borrow<rusqlite::Connection>, P: consensus::Parameters> InputSource for
fn select_spendable_notes(
&self,
account: AccountId,
target_value: Amount,
target_value: NonNegativeAmount,
_sources: &[ShieldedProtocol],
anchor_height: BlockHeight,
exclude: &[Self::NoteRef],
Expand Down Expand Up @@ -408,7 +405,7 @@ impl<C: Borrow<rusqlite::Connection>, P: consensus::Parameters> WalletRead for W
&self,
account: AccountId,
max_height: BlockHeight,
) -> Result<HashMap<TransparentAddress, Amount>, Self::Error> {
) -> Result<HashMap<TransparentAddress, NonNegativeAmount>, Self::Error> {
wallet::get_transparent_balances(self.conn.borrow(), &self.params, account, max_height)
}

Expand Down
14 changes: 7 additions & 7 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ pub(crate) fn get_transparent_balances<P: consensus::Parameters>(
params: &P,
account: AccountId,
max_height: BlockHeight,
) -> Result<HashMap<TransparentAddress, Amount>, SqliteClientError> {
) -> Result<HashMap<TransparentAddress, NonNegativeAmount>, SqliteClientError> {
let chain_tip_height = scan_queue_extrema(conn)?.map(|range| *range.end());
let stable_height = chain_tip_height
.unwrap_or(max_height)
Expand All @@ -1515,7 +1515,7 @@ pub(crate) fn get_transparent_balances<P: consensus::Parameters>(
while let Some(row) = rows.next()? {
let taddr_str: String = row.get(0)?;
let taddr = TransparentAddress::decode(params, &taddr_str)?;
let value = Amount::from_i64(row.get(1)?).unwrap();
let value = NonNegativeAmount::from_nonnegative_i64(row.get(1)?)?;

res.insert(taddr, value);
}
Expand Down Expand Up @@ -1638,7 +1638,7 @@ pub(crate) fn put_tx_meta(
pub(crate) fn put_tx_data(
conn: &rusqlite::Connection,
tx: &Transaction,
fee: Option<Amount>,
fee: Option<NonNegativeAmount>,
created_at: Option<time::OffsetDateTime>,
) -> Result<i64, SqliteClientError> {
let mut stmt_upsert_tx_data = conn.prepare_cached(
Expand All @@ -1660,7 +1660,7 @@ pub(crate) fn put_tx_data(
":created_at": created_at,
":expiry_height": u32::from(tx.expiry_height()),
":raw": raw_tx,
":fee": fee.map(i64::from),
":fee": fee.map(u64::from),
];

stmt_upsert_tx_data
Expand Down Expand Up @@ -2092,7 +2092,7 @@ mod tests {
zcash_primitives::{
consensus::BlockHeight,
transaction::{
components::{Amount, OutPoint, TxOut},
components::{OutPoint, TxOut},
fees::fixed::FeeRule as FixedFeeRule,
},
},
Expand Down Expand Up @@ -2279,8 +2279,8 @@ mod tests {
.unwrap()
.get(taddr)
.cloned()
.unwrap_or(Amount::zero()),
Amount::from(expected),
.unwrap_or(NonNegativeAmount::ZERO),
expected,
);
assert_eq!(
st.wallet()
Expand Down
14 changes: 7 additions & 7 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sapling::{self, Diversifier, Nullifier, Rseed};
use zcash_primitives::{
consensus::{self, BlockHeight},
memo::MemoBytes,
transaction::{components::Amount, TxId},
transaction::{components::amount::NonNegativeAmount, TxId},
zip32::{AccountId, Scope},
};

Expand Down Expand Up @@ -232,7 +232,7 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
conn: &Connection,
params: &P,
account: AccountId,
target_value: Amount,
target_value: NonNegativeAmount,
anchor_height: BlockHeight,
exclude: &[ReceivedNoteId],
) -> Result<Vec<ReceivedNote<ReceivedNoteId, Note>>, SqliteClientError> {
Expand Down Expand Up @@ -306,7 +306,7 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
named_params![
":account": &u32::from(account),
":anchor_height": &u32::from(anchor_height),
":target_value": &i64::from(target_value),
":target_value": &u64::from(target_value),
":exclude": &excluded_ptr,
":wallet_birthday": u32::from(birthday_height)
],
Expand Down Expand Up @@ -485,7 +485,7 @@ pub(crate) mod tests {
legacy::TransparentAddress,
memo::{Memo, MemoBytes},
transaction::{
components::{amount::NonNegativeAmount, sapling::zip212_enforcement, Amount},
components::{amount::NonNegativeAmount, sapling::zip212_enforcement},
fees::{
fixed::FeeRule as FixedFeeRule, zip317::FeeError as Zip317FeeError, StandardFeeRule,
},
Expand Down Expand Up @@ -1756,7 +1756,7 @@ pub(crate) mod tests {
&st.wallet().conn,
&st.wallet().params,
AccountId::ZERO,
Amount::const_from_i64(300000),
NonNegativeAmount::const_from_u64(300000),
received_tx_height + 10,
&[],
)
Expand All @@ -1772,7 +1772,7 @@ pub(crate) mod tests {
&st.wallet().conn,
&st.wallet().params,
AccountId::ZERO,
Amount::const_from_i64(300000),
NonNegativeAmount::const_from_u64(300000),
received_tx_height + 10,
&[],
)
Expand Down Expand Up @@ -1826,7 +1826,7 @@ pub(crate) mod tests {
&st.wallet().conn,
&st.wallet().params,
account,
Amount::const_from_i64(300000),
NonNegativeAmount::const_from_u64(300000),
birthday.height() + 5,
&[],
)
Expand Down

0 comments on commit 0f0df44

Please sign in to comment.