Skip to content

Commit

Permalink
The return type of ChangeValue::new is now optional; it returns None
Browse files Browse the repository at this point in the history
if a memo is given for the transparent pool. Use `ChangeValue::shielded`
to avoid this error case when creating a `ChangeValue` known to be for a
shielded pool.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
Signed-off-by: Daira-Emma Hopwood <daira@jacaranda.org>
  • Loading branch information
daira and str4d committed Jun 18, 2024
1 parent 3582f84 commit c3e532f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
6 changes: 5 additions & 1 deletion zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this library adheres to Rust's notion of
### Added
- `zcash_client_backend::data_api`:
- `chain::BlockCache` trait, behind the `sync` feature flag.
- `zcash_client_backend::fees::ChangeValue::transparent`
- `zcash_client_backend::fees::ChangeValue::{transparent, shielded}`
- `zcash_client_backend::scanning`:
- `testing` module
- `zcash_client_backend::sync` module, behind the `sync` feature flag.
Expand All @@ -31,6 +31,10 @@ and this library adheres to Rust's notion of
- The return type of `ChangeValue::output_pool`, and the type of the
`output_pool` argument to `ChangeValue::new`, have changed from
`ShieldedProtocol` to `zcash_protocol::PoolType`.
- The return type of `ChangeValue::new` is now optional; it returns `None`
if a memo is given for the transparent pool. Use `ChangeValue::shielded`
to avoid this error case when creating a `ChangeValue` known to be for a
shielded pool.
- `zcash_client_backend::wallet::Recipient` variants have changed. Instead of
wrapping protocol-address types, the `Recipient` type now wraps a
`zcash_address::ZcashAddress`. This simplifies the process of tracking the
Expand Down
31 changes: 20 additions & 11 deletions zcash_client_backend/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ pub struct ChangeValue {

impl ChangeValue {
/// Constructs a new change value from its constituent parts.
pub fn new(output_pool: PoolType, value: NonNegativeAmount, memo: Option<MemoBytes>) -> Self {
Self {
pub fn new(
output_pool: PoolType,
value: NonNegativeAmount,
memo: Option<MemoBytes>,
) -> Option<Self> {
(matches!(output_pool, PoolType::Shielded(_)) || memo.is_none()).then_some(Self {
output_pool,
value,
memo,
}
})
}

/// Constructs a new change value that will be created as a transparent output.
Expand All @@ -48,23 +52,28 @@ impl ChangeValue {
}
}

/// Constructs a new change value that will be created as a Sapling output.
pub fn sapling(value: NonNegativeAmount, memo: Option<MemoBytes>) -> Self {
/// Constructs a new change value that will be created as a shielded output.
pub fn shielded(
protocol: ShieldedProtocol,
value: NonNegativeAmount,
memo: Option<MemoBytes>,
) -> Self {
Self {
output_pool: PoolType::Shielded(ShieldedProtocol::Sapling),
output_pool: PoolType::Shielded(protocol),
value,
memo,
}
}

/// Constructs a new change value that will be created as a Sapling output.
pub fn sapling(value: NonNegativeAmount, memo: Option<MemoBytes>) -> Self {
Self::shielded(ShieldedProtocol::Sapling, value, memo)
}

/// Constructs a new change value that will be created as an Orchard output.
#[cfg(feature = "orchard")]
pub fn orchard(value: NonNegativeAmount, memo: Option<MemoBytes>) -> Self {
Self {
output_pool: PoolType::Shielded(ShieldedProtocol::Orchard),
value,
memo,
}
Self::shielded(ShieldedProtocol::Orchard, value, memo)
}

/// Returns the pool to which the change output should be sent.
Expand Down
10 changes: 5 additions & 5 deletions zcash_client_backend/src/fees/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use zcash_primitives::{
fees::{transparent, FeeRule},
},
};
use zcash_protocol::{PoolType, ShieldedProtocol};
use zcash_protocol::ShieldedProtocol;

use super::{
sapling as sapling_fees, ChangeError, ChangeValue, DustAction, DustOutputPolicy,
Expand Down Expand Up @@ -219,8 +219,8 @@ where
})
}
DustAction::AllowDustChange => TransactionBalance::new(
vec![ChangeValue::new(
PoolType::Shielded(change_pool),
vec![ChangeValue::shielded(
change_pool,
proposed_change,
change_memo,
)],
Expand All @@ -235,8 +235,8 @@ where
}
} else {
TransactionBalance::new(
vec![ChangeValue::new(
PoolType::Shielded(change_pool),
vec![ChangeValue::shielded(
change_pool,
proposed_change,
change_memo,
)],
Expand Down

0 comments on commit c3e532f

Please sign in to comment.