From af15ecf55ee1e93d1fc668896ef1aa0d8d3b9730 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Sat, 27 Jan 2024 11:53:44 -0700 Subject: [PATCH] zcash_protocol: Rename value amounts to `Zatoshis` and `ZatBalance` --- components/zcash_protocol/src/value.rs | 319 +++++++++--------- zcash_client_backend/src/data_api/wallet.rs | 10 +- zcash_client_backend/src/decrypt.rs | 2 +- zcash_client_sqlite/src/testing.rs | 19 +- .../init/migrations/receiving_key_scopes.rs | 5 +- zcash_client_sqlite/src/wallet/sapling.rs | 7 +- zcash_primitives/benches/note_decryption.rs | 2 +- .../src/transaction/components.rs | 13 +- .../src/transaction/components/sapling.rs | 7 +- 9 files changed, 198 insertions(+), 186 deletions(-) diff --git a/components/zcash_protocol/src/value.rs b/components/zcash_protocol/src/value.rs index b902c81460..7767220c00 100644 --- a/components/zcash_protocol/src/value.rs +++ b/components/zcash_protocol/src/value.rs @@ -10,118 +10,118 @@ pub const MAX_MONEY: i64 = 21_000_000 * COIN; /// A type-safe representation of a Zcash value delta, in zatoshis. /// -/// An Amount can only be constructed from an integer that is within the valid monetary +/// An ZatBalance can only be constructed from an integer that is within the valid monetary /// range of `{-MAX_MONEY..MAX_MONEY}` (where `MAX_MONEY` = 21,000,000 × 10⁸ zatoshis). /// However, this range is not preserved as an invariant internally; it is possible to -/// add two valid Amounts together to obtain an invalid Amount. It is the user's -/// responsibility to handle the result of serializing potentially-invalid Amounts. In -/// particular, a [`Transaction`] containing serialized invalid Amounts will be rejected +/// add two valid ZatBalances together to obtain an invalid ZatBalance. It is the user's +/// responsibility to handle the result of serializing potentially-invalid ZatBalances. In +/// particular, a [`Transaction`] containing serialized invalid ZatBalances will be rejected /// by the network consensus rules. /// /// [`Transaction`]: crate::transaction::Transaction #[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)] -pub struct Amount(i64); +pub struct ZatBalance(i64); -memuse::impl_no_dynamic_usage!(Amount); +memuse::impl_no_dynamic_usage!(ZatBalance); -impl Amount { - /// Returns a zero-valued Amount. +impl ZatBalance { + /// Returns a zero-valued ZatBalance. pub const fn zero() -> Self { - Amount(0) + ZatBalance(0) } - /// Creates a constant Amount from an i64. + /// Creates a constant ZatBalance from an i64. /// /// Panics: if the amount is outside the range `{-MAX_MONEY..MAX_MONEY}`. pub const fn const_from_i64(amount: i64) -> Self { assert!(-MAX_MONEY <= amount && amount <= MAX_MONEY); // contains is not const - Amount(amount) + ZatBalance(amount) } - /// Creates a constant Amount from a u64. + /// Creates a constant ZatBalance from a u64. /// /// Panics: if the amount is outside the range `{0..MAX_MONEY}`. const fn const_from_u64(amount: u64) -> Self { assert!(amount <= (MAX_MONEY as u64)); // contains is not const - Amount(amount as i64) + ZatBalance(amount as i64) } - /// Creates an Amount from an i64. + /// Creates an ZatBalance from an i64. /// /// Returns an error if the amount is outside the range `{-MAX_MONEY..MAX_MONEY}`. pub fn from_i64(amount: i64) -> Result { if (-MAX_MONEY..=MAX_MONEY).contains(&amount) { - Ok(Amount(amount)) + Ok(ZatBalance(amount)) } else { Err(()) } } - /// Creates a non-negative Amount from an i64. + /// Creates a non-negative ZatBalance from an i64. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_nonnegative_i64(amount: i64) -> Result { if (0..=MAX_MONEY).contains(&amount) { - Ok(Amount(amount)) + Ok(ZatBalance(amount)) } else { Err(()) } } - /// Creates an Amount from a u64. + /// Creates an ZatBalance from a u64. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_u64(amount: u64) -> Result { if amount <= MAX_MONEY as u64 { - Ok(Amount(amount as i64)) + Ok(ZatBalance(amount as i64)) } else { Err(()) } } - /// Reads an Amount from a signed 64-bit little-endian integer. + /// Reads an ZatBalance from a signed 64-bit little-endian integer. /// /// Returns an error if the amount is outside the range `{-MAX_MONEY..MAX_MONEY}`. pub fn from_i64_le_bytes(bytes: [u8; 8]) -> Result { let amount = i64::from_le_bytes(bytes); - Amount::from_i64(amount) + ZatBalance::from_i64(amount) } - /// Reads a non-negative Amount from a signed 64-bit little-endian integer. + /// Reads a non-negative ZatBalance from a signed 64-bit little-endian integer. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_nonnegative_i64_le_bytes(bytes: [u8; 8]) -> Result { let amount = i64::from_le_bytes(bytes); - Amount::from_nonnegative_i64(amount) + ZatBalance::from_nonnegative_i64(amount) } - /// Reads an Amount from an unsigned 64-bit little-endian integer. + /// Reads an ZatBalance from an unsigned 64-bit little-endian integer. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_u64_le_bytes(bytes: [u8; 8]) -> Result { let amount = u64::from_le_bytes(bytes); - Amount::from_u64(amount) + ZatBalance::from_u64(amount) } - /// Returns the Amount encoded as a signed 64-bit little-endian integer. + /// Returns the ZatBalance encoded as a signed 64-bit little-endian integer. pub fn to_i64_le_bytes(self) -> [u8; 8] { self.0.to_le_bytes() } - /// Returns `true` if `self` is positive and `false` if the Amount is zero or + /// Returns `true` if `self` is positive and `false` if the ZatBalance is zero or /// negative. pub const fn is_positive(self) -> bool { self.0.is_positive() } - /// Returns `true` if `self` is negative and `false` if the Amount is zero or + /// Returns `true` if `self` is negative and `false` if the ZatBalance is zero or /// positive. pub const fn is_negative(self) -> bool { self.0.is_negative() } - pub fn sum>(values: I) -> Option { - let mut result = Amount::zero(); + pub fn sum>(values: I) -> Option { + let mut result = ZatBalance::zero(); for value in values { result = (result + value)?; } @@ -129,147 +129,147 @@ impl Amount { } } -impl TryFrom for Amount { +impl TryFrom for ZatBalance { type Error = (); fn try_from(value: i64) -> Result { - Amount::from_i64(value) + ZatBalance::from_i64(value) } } -impl From for i64 { - fn from(amount: Amount) -> i64 { +impl From for i64 { + fn from(amount: ZatBalance) -> i64 { amount.0 } } -impl From<&Amount> for i64 { - fn from(amount: &Amount) -> i64 { +impl From<&ZatBalance> for i64 { + fn from(amount: &ZatBalance) -> i64 { amount.0 } } -impl TryFrom for u64 { +impl TryFrom for u64 { type Error = (); - fn try_from(value: Amount) -> Result { + fn try_from(value: ZatBalance) -> Result { value.0.try_into().map_err(|_| ()) } } -impl Add for Amount { - type Output = Option; +impl Add for ZatBalance { + type Output = Option; - fn add(self, rhs: Amount) -> Option { - Amount::from_i64(self.0 + rhs.0).ok() + fn add(self, rhs: ZatBalance) -> Option { + ZatBalance::from_i64(self.0 + rhs.0).ok() } } -impl Add for Option { +impl Add for Option { type Output = Self; - fn add(self, rhs: Amount) -> Option { + fn add(self, rhs: ZatBalance) -> Option { self.and_then(|lhs| lhs + rhs) } } -impl AddAssign for Amount { - fn add_assign(&mut self, rhs: Amount) { +impl AddAssign for ZatBalance { + fn add_assign(&mut self, rhs: ZatBalance) { *self = (*self + rhs).expect("Addition must produce a valid amount value.") } } -impl Sub for Amount { - type Output = Option; +impl Sub for ZatBalance { + type Output = Option; - fn sub(self, rhs: Amount) -> Option { - Amount::from_i64(self.0 - rhs.0).ok() + fn sub(self, rhs: ZatBalance) -> Option { + ZatBalance::from_i64(self.0 - rhs.0).ok() } } -impl Sub for Option { +impl Sub for Option { type Output = Self; - fn sub(self, rhs: Amount) -> Option { + fn sub(self, rhs: ZatBalance) -> Option { self.and_then(|lhs| lhs - rhs) } } -impl SubAssign for Amount { - fn sub_assign(&mut self, rhs: Amount) { +impl SubAssign for ZatBalance { + fn sub_assign(&mut self, rhs: ZatBalance) { *self = (*self - rhs).expect("Subtraction must produce a valid amount value.") } } -impl Sum for Option { - fn sum>(iter: I) -> Self { - iter.fold(Some(Amount::zero()), |acc, a| acc? + a) +impl Sum for Option { + fn sum>(iter: I) -> Self { + iter.fold(Some(ZatBalance::zero()), |acc, a| acc? + a) } } -impl<'a> Sum<&'a Amount> for Option { - fn sum>(iter: I) -> Self { - iter.fold(Some(Amount::zero()), |acc, a| acc? + *a) +impl<'a> Sum<&'a ZatBalance> for Option { + fn sum>(iter: I) -> Self { + iter.fold(Some(ZatBalance::zero()), |acc, a| acc? + *a) } } -impl Neg for Amount { +impl Neg for ZatBalance { type Output = Self; fn neg(self) -> Self { - Amount(-self.0) + ZatBalance(-self.0) } } -impl Mul for Amount { - type Output = Option; +impl Mul for ZatBalance { + type Output = Option; - fn mul(self, rhs: usize) -> Option { + fn mul(self, rhs: usize) -> Option { let rhs: i64 = rhs.try_into().ok()?; self.0 .checked_mul(rhs) - .and_then(|i| Amount::try_from(i).ok()) + .and_then(|i| ZatBalance::try_from(i).ok()) } } /// A type-safe representation of some nonnegative amount of Zcash. /// -/// A NonNegativeAmount can only be constructed from an integer that is within the valid monetary +/// A Zatoshis can only be constructed from an integer that is within the valid monetary /// range of `{0..MAX_MONEY}` (where `MAX_MONEY` = 21,000,000 × 10⁸ zatoshis). #[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)] -pub struct NonNegativeAmount(Amount); +pub struct Zatoshis(ZatBalance); -impl NonNegativeAmount { - /// Returns the identity `NonNegativeAmount` - pub const ZERO: Self = NonNegativeAmount(Amount(0)); +impl Zatoshis { + /// Returns the identity `Zatoshis` + pub const ZERO: Self = Zatoshis(ZatBalance(0)); - /// Returns this NonNegativeAmount as a u64. + /// Returns this Zatoshis as a u64. pub fn into_u64(self) -> u64 { self.0.try_into().unwrap() } - /// Creates a NonNegativeAmount from a u64. + /// Creates a Zatoshis from a u64. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_u64(amount: u64) -> Result { - Amount::from_u64(amount).map(NonNegativeAmount) + ZatBalance::from_u64(amount).map(Zatoshis) } - /// Creates a constant NonNegativeAmount from a u64. + /// Creates a constant Zatoshis from a u64. /// /// Panics: if the amount is outside the range `{-MAX_MONEY..MAX_MONEY}`. pub const fn const_from_u64(amount: u64) -> Self { - NonNegativeAmount(Amount::const_from_u64(amount)) + Zatoshis(ZatBalance::const_from_u64(amount)) } - /// Creates a NonNegativeAmount from an i64. + /// Creates a Zatoshis from an i64. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_nonnegative_i64(amount: i64) -> Result { - Amount::from_nonnegative_i64(amount).map(NonNegativeAmount) + ZatBalance::from_nonnegative_i64(amount).map(Zatoshis) } - /// Reads an NonNegativeAmount from an unsigned 64-bit little-endian integer. + /// Reads an Zatoshis from an unsigned 64-bit little-endian integer. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. pub fn from_u64_le_bytes(bytes: [u8; 8]) -> Result { @@ -277,7 +277,7 @@ impl NonNegativeAmount { Self::from_u64(amount) } - /// Reads a NonNegativeAmount from a signed integer represented as a two's + /// Reads a Zatoshis from a signed integer represented as a two's /// complement 64-bit little-endian value. /// /// Returns an error if the amount is outside the range `{0..MAX_MONEY}`. @@ -286,110 +286,110 @@ impl NonNegativeAmount { Self::from_nonnegative_i64(amount) } - /// Returns this NonNegativeAmount encoded as a signed two's complement 64-bit + /// Returns this Zatoshis encoded as a signed two's complement 64-bit /// little-endian value. pub fn to_i64_le_bytes(self) -> [u8; 8] { self.0.to_i64_le_bytes() } - /// Returns whether or not this `NonNegativeAmount` is the zero value. + /// Returns whether or not this `Zatoshis` is the zero value. pub fn is_zero(&self) -> bool { - self == &NonNegativeAmount::ZERO + self == &Zatoshis::ZERO } - /// Returns whether or not this `NonNegativeAmount` is positive. + /// Returns whether or not this `Zatoshis` is positive. pub fn is_positive(&self) -> bool { - self > &NonNegativeAmount::ZERO + self > &Zatoshis::ZERO } } -impl From for Amount { - fn from(n: NonNegativeAmount) -> Self { +impl From for ZatBalance { + fn from(n: Zatoshis) -> Self { n.0 } } -impl From<&NonNegativeAmount> for Amount { - fn from(n: &NonNegativeAmount) -> Self { +impl From<&Zatoshis> for ZatBalance { + fn from(n: &Zatoshis) -> Self { n.0 } } -impl From for u64 { - fn from(n: NonNegativeAmount) -> Self { +impl From for u64 { + fn from(n: Zatoshis) -> Self { n.into_u64() } } -impl TryFrom for NonNegativeAmount { +impl TryFrom for Zatoshis { type Error = (); fn try_from(value: u64) -> Result { - NonNegativeAmount::from_u64(value) + Zatoshis::from_u64(value) } } -impl TryFrom for NonNegativeAmount { +impl TryFrom for Zatoshis { type Error = (); - fn try_from(value: Amount) -> Result { + fn try_from(value: ZatBalance) -> Result { if value.is_negative() { Err(()) } else { - Ok(NonNegativeAmount(value)) + Ok(Zatoshis(value)) } } } -impl Add for NonNegativeAmount { - type Output = Option; +impl Add for Zatoshis { + type Output = Option; - fn add(self, rhs: NonNegativeAmount) -> Option { - (self.0 + rhs.0).map(NonNegativeAmount) + fn add(self, rhs: Zatoshis) -> Option { + (self.0 + rhs.0).map(Zatoshis) } } -impl Add for Option { +impl Add for Option { type Output = Self; - fn add(self, rhs: NonNegativeAmount) -> Option { + fn add(self, rhs: Zatoshis) -> Option { self.and_then(|lhs| lhs + rhs) } } -impl Sub for NonNegativeAmount { - type Output = Option; +impl Sub for Zatoshis { + type Output = Option; - fn sub(self, rhs: NonNegativeAmount) -> Option { - (self.0 - rhs.0).and_then(|amt| NonNegativeAmount::try_from(amt).ok()) + fn sub(self, rhs: Zatoshis) -> Option { + (self.0 - rhs.0).and_then(|amt| Zatoshis::try_from(amt).ok()) } } -impl Sub for Option { +impl Sub for Option { type Output = Self; - fn sub(self, rhs: NonNegativeAmount) -> Option { + fn sub(self, rhs: Zatoshis) -> Option { self.and_then(|lhs| lhs - rhs) } } -impl Mul for NonNegativeAmount { +impl Mul for Zatoshis { type Output = Option; - fn mul(self, rhs: usize) -> Option { - (self.0 * rhs).and_then(|v| NonNegativeAmount::try_from(v).ok()) + fn mul(self, rhs: usize) -> Option { + (self.0 * rhs).and_then(|v| Zatoshis::try_from(v).ok()) } } -impl Sum for Option { - fn sum>(iter: I) -> Self { - iter.fold(Some(NonNegativeAmount::ZERO), |acc, a| acc? + a) +impl Sum for Option { + fn sum>(iter: I) -> Self { + iter.fold(Some(Zatoshis::ZERO), |acc, a| acc? + a) } } -impl<'a> Sum<&'a NonNegativeAmount> for Option { - fn sum>(iter: I) -> Self { - iter.fold(Some(NonNegativeAmount::ZERO), |acc, a| acc? + *a) +impl<'a> Sum<&'a Zatoshis> for Option { + fn sum>(iter: I) -> Self { + iter.fold(Some(Zatoshis::ZERO), |acc, a| acc? + *a) } } @@ -409,12 +409,12 @@ impl std::fmt::Display for BalanceError { BalanceError::Overflow => { write!( f, - "Amount addition resulted in a value outside the valid range." + "ZatBalance addition resulted in a value outside the valid range." ) } BalanceError::Underflow => write!( f, - "Amount subtraction resulted in a value outside the valid range." + "ZatBalance subtraction resulted in a value outside the valid range." ), } } @@ -430,102 +430,105 @@ impl From for BalanceError { pub mod testing { use proptest::prelude::prop_compose; - use super::{Amount, NonNegativeAmount, MAX_MONEY}; + use super::{ZatBalance, Zatoshis, MAX_MONEY}; prop_compose! { - pub fn arb_amount()(amt in -MAX_MONEY..MAX_MONEY) -> Amount { - Amount::from_i64(amt).unwrap() + pub fn arb_zat_balance()(amt in -MAX_MONEY..MAX_MONEY) -> ZatBalance { + ZatBalance::from_i64(amt).unwrap() } } prop_compose! { - pub fn arb_nonnegative_amount()(amt in 0i64..MAX_MONEY) -> NonNegativeAmount { - NonNegativeAmount::from_u64(amt as u64).unwrap() + pub fn arb_positive_zat_balance()(amt in 1i64..MAX_MONEY) -> ZatBalance { + ZatBalance::from_i64(amt).unwrap() } } prop_compose! { - pub fn arb_positive_amount()(amt in 1i64..MAX_MONEY) -> Amount { - Amount::from_i64(amt).unwrap() + pub fn arb_zatoshis()(amt in 0i64..MAX_MONEY) -> Zatoshis { + Zatoshis::from_u64(amt as u64).unwrap() } } } #[cfg(test)] mod tests { - use super::{Amount, MAX_MONEY}; + use super::{ZatBalance, MAX_MONEY}; #[test] fn amount_in_range() { let zero = b"\x00\x00\x00\x00\x00\x00\x00\x00"; - assert_eq!(Amount::from_u64_le_bytes(*zero).unwrap(), Amount(0)); + assert_eq!(ZatBalance::from_u64_le_bytes(*zero).unwrap(), ZatBalance(0)); assert_eq!( - Amount::from_nonnegative_i64_le_bytes(*zero).unwrap(), - Amount(0) + ZatBalance::from_nonnegative_i64_le_bytes(*zero).unwrap(), + ZatBalance(0) ); - assert_eq!(Amount::from_i64_le_bytes(*zero).unwrap(), Amount(0)); + assert_eq!(ZatBalance::from_i64_le_bytes(*zero).unwrap(), ZatBalance(0)); let neg_one = b"\xff\xff\xff\xff\xff\xff\xff\xff"; - assert!(Amount::from_u64_le_bytes(*neg_one).is_err()); - assert!(Amount::from_nonnegative_i64_le_bytes(*neg_one).is_err()); - assert_eq!(Amount::from_i64_le_bytes(*neg_one).unwrap(), Amount(-1)); + assert!(ZatBalance::from_u64_le_bytes(*neg_one).is_err()); + assert!(ZatBalance::from_nonnegative_i64_le_bytes(*neg_one).is_err()); + assert_eq!( + ZatBalance::from_i64_le_bytes(*neg_one).unwrap(), + ZatBalance(-1) + ); let max_money = b"\x00\x40\x07\x5a\xf0\x75\x07\x00"; assert_eq!( - Amount::from_u64_le_bytes(*max_money).unwrap(), - Amount(MAX_MONEY) + ZatBalance::from_u64_le_bytes(*max_money).unwrap(), + ZatBalance(MAX_MONEY) ); assert_eq!( - Amount::from_nonnegative_i64_le_bytes(*max_money).unwrap(), - Amount(MAX_MONEY) + ZatBalance::from_nonnegative_i64_le_bytes(*max_money).unwrap(), + ZatBalance(MAX_MONEY) ); assert_eq!( - Amount::from_i64_le_bytes(*max_money).unwrap(), - Amount(MAX_MONEY) + ZatBalance::from_i64_le_bytes(*max_money).unwrap(), + ZatBalance(MAX_MONEY) ); let max_money_p1 = b"\x01\x40\x07\x5a\xf0\x75\x07\x00"; - assert!(Amount::from_u64_le_bytes(*max_money_p1).is_err()); - assert!(Amount::from_nonnegative_i64_le_bytes(*max_money_p1).is_err()); - assert!(Amount::from_i64_le_bytes(*max_money_p1).is_err()); + assert!(ZatBalance::from_u64_le_bytes(*max_money_p1).is_err()); + assert!(ZatBalance::from_nonnegative_i64_le_bytes(*max_money_p1).is_err()); + assert!(ZatBalance::from_i64_le_bytes(*max_money_p1).is_err()); let neg_max_money = b"\x00\xc0\xf8\xa5\x0f\x8a\xf8\xff"; - assert!(Amount::from_u64_le_bytes(*neg_max_money).is_err()); - assert!(Amount::from_nonnegative_i64_le_bytes(*neg_max_money).is_err()); + assert!(ZatBalance::from_u64_le_bytes(*neg_max_money).is_err()); + assert!(ZatBalance::from_nonnegative_i64_le_bytes(*neg_max_money).is_err()); assert_eq!( - Amount::from_i64_le_bytes(*neg_max_money).unwrap(), - Amount(-MAX_MONEY) + ZatBalance::from_i64_le_bytes(*neg_max_money).unwrap(), + ZatBalance(-MAX_MONEY) ); let neg_max_money_m1 = b"\xff\xbf\xf8\xa5\x0f\x8a\xf8\xff"; - assert!(Amount::from_u64_le_bytes(*neg_max_money_m1).is_err()); - assert!(Amount::from_nonnegative_i64_le_bytes(*neg_max_money_m1).is_err()); - assert!(Amount::from_i64_le_bytes(*neg_max_money_m1).is_err()); + assert!(ZatBalance::from_u64_le_bytes(*neg_max_money_m1).is_err()); + assert!(ZatBalance::from_nonnegative_i64_le_bytes(*neg_max_money_m1).is_err()); + assert!(ZatBalance::from_i64_le_bytes(*neg_max_money_m1).is_err()); } #[test] fn add_overflow() { - let v = Amount(MAX_MONEY); - assert_eq!(v + Amount(1), None) + let v = ZatBalance(MAX_MONEY); + assert_eq!(v + ZatBalance(1), None) } #[test] #[should_panic] fn add_assign_panics_on_overflow() { - let mut a = Amount(MAX_MONEY); - a += Amount(1); + let mut a = ZatBalance(MAX_MONEY); + a += ZatBalance(1); } #[test] fn sub_underflow() { - let v = Amount(-MAX_MONEY); - assert_eq!(v - Amount(1), None) + let v = ZatBalance(-MAX_MONEY); + assert_eq!(v - ZatBalance(1), None) } #[test] #[should_panic] fn sub_assign_panics_on_underflow() { - let mut a = Amount(-MAX_MONEY); - a -= Amount(1); + let mut a = ZatBalance(-MAX_MONEY); + a -= ZatBalance(1); } } diff --git a/zcash_client_backend/src/data_api/wallet.rs b/zcash_client_backend/src/data_api/wallet.rs index 3cc4d9ab21..c37cfd1548 100644 --- a/zcash_client_backend/src/data_api/wallet.rs +++ b/zcash_client_backend/src/data_api/wallet.rs @@ -10,7 +10,10 @@ use zcash_primitives::{ memo::MemoBytes, transaction::{ builder::{BuildConfig, Builder}, - components::{amount::{Amount, NonNegativeAmount}, sapling::zip212_enforcement}, + components::{ + amount::{Amount, NonNegativeAmount}, + sapling::zip212_enforcement, + }, fees::{zip317::FeeError as Zip317FeeError, FeeRule, StandardFeeRule}, Transaction, TxId, }, @@ -777,10 +780,7 @@ where try_sapling_note_decryption( &internal_ivk, &bundle.shielded_outputs()[output_index], - zip212_enforcement( - params, - proposal.min_target_height(), - ), + zip212_enforcement(params, proposal.min_target_height()), ) .map(|(note, _, _)| (account, note)) }) diff --git a/zcash_client_backend/src/decrypt.rs b/zcash_client_backend/src/decrypt.rs index 4f4a16bd43..334ccf6008 100644 --- a/zcash_client_backend/src/decrypt.rs +++ b/zcash_client_backend/src/decrypt.rs @@ -6,7 +6,7 @@ use sapling::note_encryption::{ use zcash_primitives::{ consensus::{self, BlockHeight}, memo::MemoBytes, - transaction::{Transaction, components::sapling::zip212_enforcement}, + transaction::{components::sapling::zip212_enforcement, Transaction}, zip32::{AccountId, Scope}, }; diff --git a/zcash_client_sqlite/src/testing.rs b/zcash_client_sqlite/src/testing.rs index 2b6474664f..1e56097e69 100644 --- a/zcash_client_sqlite/src/testing.rs +++ b/zcash_client_sqlite/src/testing.rs @@ -761,13 +761,12 @@ pub(crate) fn fake_compact_block( // Create a fake Note for the account let mut rng = OsRng; - let rseed = generate_random_rseed( - zip212_enforcement(params, height), - &mut rng, - ); - let note = Note::from_parts(to, + let rseed = generate_random_rseed(zip212_enforcement(params, height), &mut rng); + let note = Note::from_parts( + to, sapling::value::NoteValue::from_raw(value.into_u64()), - rseed); + rseed, + ); let encryptor = sapling_note_encryption( Some(dfvk.fvk().ovk), note.clone(), @@ -876,9 +875,11 @@ pub(crate) fn fake_compact_block_spending( // Create a fake Note for the payment ctx.outputs.push({ - let note = Note::from_parts(to, - sapling::value::NoteValue::from_raw(value.into_u64()), - rseed); + let note = Note::from_parts( + to, + sapling::value::NoteValue::from_raw(value.into_u64()), + rseed, + ); let encryptor = sapling_note_encryption( Some(dfvk.fvk().ovk), note.clone(), diff --git a/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs b/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs index c9aca77afb..cbf55606e9 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs @@ -19,7 +19,10 @@ use sapling::{ use zcash_client_backend::{data_api::SAPLING_SHARD_HEIGHT, keys::UnifiedFullViewingKey}; use zcash_primitives::{ consensus::{self, BlockHeight, BranchId}, - transaction::{components::{amount::NonNegativeAmount, sapling::zip212_enforcement}, Transaction}, + transaction::{ + components::{amount::NonNegativeAmount, sapling::zip212_enforcement}, + Transaction, + }, zip32::Scope, }; diff --git a/zcash_client_sqlite/src/wallet/sapling.rs b/zcash_client_sqlite/src/wallet/sapling.rs index e7ad49be06..8c1c3c3867 100644 --- a/zcash_client_sqlite/src/wallet/sapling.rs +++ b/zcash_client_sqlite/src/wallet/sapling.rs @@ -9,10 +9,7 @@ use sapling::{self, Diversifier, Nullifier, Rseed}; use zcash_primitives::{ consensus::{self, BlockHeight}, memo::MemoBytes, - transaction::{ - components::Amount, - TxId, - }, + transaction::{components::Amount, TxId}, zip32::{AccountId, Scope}, }; @@ -483,7 +480,7 @@ pub(crate) mod tests { legacy::TransparentAddress, memo::{Memo, MemoBytes}, transaction::{ - components::{amount::NonNegativeAmount, Amount, sapling::zip212_enforcement}, + components::{amount::NonNegativeAmount, sapling::zip212_enforcement, Amount}, fees::{ fixed::FeeRule as FixedFeeRule, zip317::FeeError as Zip317FeeError, StandardFeeRule, }, diff --git a/zcash_primitives/benches/note_decryption.rs b/zcash_primitives/benches/note_decryption.rs index da6a4ac82e..99048cb843 100644 --- a/zcash_primitives/benches/note_decryption.rs +++ b/zcash_primitives/benches/note_decryption.rs @@ -16,7 +16,7 @@ use sapling::{ use zcash_note_encryption::batch; use zcash_primitives::{ consensus::{NetworkUpgrade::Canopy, Parameters, TEST_NETWORK}, - transaction::components::{Amount, sapling::zip212_enforcement}, + transaction::components::{sapling::zip212_enforcement, Amount}, }; #[cfg(unix)] diff --git a/zcash_primitives/src/transaction/components.rs b/zcash_primitives/src/transaction/components.rs index f5a530cfe3..bba5ddf951 100644 --- a/zcash_primitives/src/transaction/components.rs +++ b/zcash_primitives/src/transaction/components.rs @@ -1,6 +1,17 @@ //! Structs representing the components within Zcash transactions. +pub mod amount { + pub use zcash_protocol::value::{ + BalanceError, ZatBalance as Amount, Zatoshis as NonNegativeAmount, COIN, + }; -pub use zcash_protocol::value as amount; + #[cfg(feature = "test-dependencies")] + pub mod testing { + pub use zcash_protocol::value::testing::{ + arb_positive_zat_balance as arb_positive_amount, arb_zat_balance as arb_amount, + arb_zatoshis as arb_nonnegative_amount, + }; + } +} pub mod orchard; pub mod sapling; pub mod sprout; diff --git a/zcash_primitives/src/transaction/components/sapling.rs b/zcash_primitives/src/transaction/components/sapling.rs index 86d33cabe4..62e493865d 100644 --- a/zcash_primitives/src/transaction/components/sapling.rs +++ b/zcash_primitives/src/transaction/components/sapling.rs @@ -1,7 +1,7 @@ use ff::PrimeField; use redjubjub::SpendAuth; use sapling::note_encryption::Zip212Enforcement; -use zcash_protocol::consensus::{Parameters, NetworkUpgrade, BlockHeight, ZIP212_GRACE_PERIOD}; +use zcash_protocol::consensus::{BlockHeight, NetworkUpgrade, Parameters, ZIP212_GRACE_PERIOD}; use std::io::{self, Read, Write}; @@ -24,10 +24,7 @@ use crate::{ use super::{Amount, GROTH_PROOF_SIZE}; /// Returns the enforcement policy for ZIP 212 at the given height. -pub fn zip212_enforcement( - params: &impl Parameters, - height: BlockHeight, -) -> Zip212Enforcement { +pub fn zip212_enforcement(params: &impl Parameters, height: BlockHeight) -> Zip212Enforcement { if params.is_nu_active(NetworkUpgrade::Canopy, height) { let grace_period_end_height = params.activation_height(NetworkUpgrade::Canopy).unwrap() + ZIP212_GRACE_PERIOD;