Skip to content

Commit

Permalink
Move Amount and NonNegativeAmount types to the zcash_protocol c…
Browse files Browse the repository at this point in the history
…rate.
  • Loading branch information
nuttycom committed Jan 27, 2024
1 parent 2dc8986 commit 9cf0d55
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 47 deletions.
1 change: 1 addition & 0 deletions components/zcash_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

pub mod consensus;
pub mod constants;
pub mod value;
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use std::iter::Sum;
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};

use memuse::DynamicUsage;
use orchard::value as orchard;

use crate::sapling;

pub const COIN: i64 = 1_0000_0000;
pub const MAX_MONEY: i64 = 21_000_000 * COIN;
Expand Down Expand Up @@ -235,14 +232,6 @@ impl Mul<usize> for Amount {
}
}

impl TryFrom<orchard::ValueSum> for Amount {
type Error = ();

fn try_from(v: orchard::ValueSum) -> Result<Amount, Self::Error> {
i64::try_from(v).map_err(|_| ()).and_then(Amount::try_from)
}
}

/// 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
Expand All @@ -254,6 +243,11 @@ impl NonNegativeAmount {
/// Returns the identity `NonNegativeAmount`
pub const ZERO: Self = NonNegativeAmount(Amount(0));

/// Returns this NonNegativeAmount as a u64.
pub fn into_u64(self) -> u64 {
self.0.try_into().unwrap()
}

/// Creates a NonNegativeAmount from a u64.
///
/// Returns an error if the amount is outside the range `{0..MAX_MONEY}`.
Expand Down Expand Up @@ -323,35 +317,15 @@ impl From<&NonNegativeAmount> for Amount {

impl From<NonNegativeAmount> for u64 {
fn from(n: NonNegativeAmount) -> Self {
n.0.try_into().unwrap()
}
}

impl From<NonNegativeAmount> for sapling::value::NoteValue {
fn from(n: NonNegativeAmount) -> Self {
sapling::value::NoteValue::from_raw(n.into())
}
}

impl TryFrom<sapling::value::NoteValue> for NonNegativeAmount {
type Error = ();

fn try_from(value: sapling::value::NoteValue) -> Result<Self, Self::Error> {
Self::from_u64(value.inner())
}
}

impl From<NonNegativeAmount> for orchard::NoteValue {
fn from(n: NonNegativeAmount) -> Self {
orchard::NoteValue::from_raw(n.into())
n.into_u64()
}
}

impl TryFrom<orchard::NoteValue> for NonNegativeAmount {
impl TryFrom<u64> for NonNegativeAmount {
type Error = ();

fn try_from(value: orchard::NoteValue) -> Result<Self, Self::Error> {
Self::from_u64(value.inner())
fn try_from(value: u64) -> Result<Self, Self::Error> {
NonNegativeAmount::from_u64(value)
}
}

Expand Down
4 changes: 2 additions & 2 deletions zcash_client_backend/src/fees/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl InputView<()> for SpendInfo {
}

fn value(&self) -> NonNegativeAmount {
NonNegativeAmount::try_from(self.value())
NonNegativeAmount::try_from(self.value().inner())
.expect("An existing note to be spent must have a valid amount value.")
}
}
Expand All @@ -81,7 +81,7 @@ pub trait OutputView {

impl OutputView for OutputInfo {
fn value(&self) -> NonNegativeAmount {
NonNegativeAmount::try_from(self.value())
NonNegativeAmount::try_from(self.value().inner())
.expect("Output values should be checked at construction.")
}
}
Expand Down
2 changes: 1 addition & 1 deletion zcash_client_backend/src/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ mod tests {
// Create a fake Note for the account
let mut rng = OsRng;
let rseed = generate_random_rseed(zip212_enforcement, &mut rng);
let note = sapling::Note::from_parts(to, NoteValue::from(value), rseed);
let note = sapling::Note::from_parts(to, NoteValue::from_raw(value.into()), rseed);
let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk),
note.clone(),
Expand Down
4 changes: 3 additions & 1 deletion zcash_client_backend/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub enum Note {
impl Note {
pub fn value(&self) -> NonNegativeAmount {
match self {
Note::Sapling(n) => n.value().try_into().expect(
Note::Sapling(n) => n.value().inner().try_into().expect(
"Sapling notes must have values in the range of valid non-negative ZEC values.",
),
#[cfg(feature = "orchard")]
Expand Down Expand Up @@ -349,6 +349,7 @@ impl<NoteRef> sapling_fees::InputView<NoteRef> for ReceivedNote<NoteRef, sapling
fn value(&self) -> NonNegativeAmount {
self.note
.value()
.inner()
.try_into()
.expect("Sapling note values are indirectly checked by consensus.")
}
Expand All @@ -363,6 +364,7 @@ impl<NoteRef> orchard_fees::InputView<NoteRef> for ReceivedNote<NoteRef, orchard
fn value(&self) -> NonNegativeAmount {
self.note
.value()
.inner()
.try_into()
.expect("Orchard note values are indirectly checked by consensus.")
}
Expand Down
10 changes: 7 additions & 3 deletions zcash_client_sqlite/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,9 @@ pub(crate) fn fake_compact_block<P: consensus::Parameters>(
zip212_enforcement(params, height),
&mut rng,
);
let note = Note::from_parts(to, NoteValue::from(value), 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(),
Expand Down Expand Up @@ -874,7 +876,9 @@ pub(crate) fn fake_compact_block_spending<P: consensus::Parameters>(

// Create a fake Note for the payment
ctx.outputs.push({
let note = Note::from_parts(to, NoteValue::from(value), 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(),
Expand All @@ -898,7 +902,7 @@ pub(crate) fn fake_compact_block_spending<P: consensus::Parameters>(
let rseed = generate_random_rseed(zip212_enforcement, &mut rng);
let note = Note::from_parts(
change_addr,
NoteValue::from((in_value - value).unwrap()),
NoteValue::from_raw((in_value - value).unwrap().into_u64()),
rseed,
);
let encryptor = sapling_note_encryption(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
&mut commitment_tree,
dfvk,
&diversifier,
&note_value.try_into().unwrap(),
&sapling::value::NoteValue::from_raw(note_value.into_u64()),
&rseed,
note_commitment_tree_position,
)?;
Expand Down
6 changes: 3 additions & 3 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zcash_primitives::{
consensus::{self, BlockHeight},
memo::MemoBytes,
transaction::{
components::{amount::NonNegativeAmount, Amount},
components::Amount,
TxId,
},
zip32::{AccountId, Scope},
Expand Down Expand Up @@ -116,7 +116,7 @@ fn to_spendable_note<P: consensus::Parameters>(
Diversifier(tmp)
};

let note_value = NonNegativeAmount::from_nonnegative_i64(row.get(4)?).map_err(|_e| {
let note_value: u64 = row.get::<_, i64>(4)?.try_into().map_err(|_e| {
SqliteClientError::CorruptedData("Note values must be nonnegative".to_string())
})?;

Expand Down Expand Up @@ -165,7 +165,7 @@ fn to_spendable_note<P: consensus::Parameters>(
output_index,
Note::Sapling(sapling::Note::from_parts(
recipient,
note_value.into(),
sapling::value::NoteValue::from_raw(note_value),
rseed,
)),
spending_key_scope,
Expand Down
1 change: 1 addition & 0 deletions zcash_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ temporary-zcashd = []
## Exposes APIs that are useful for testing, such as `proptest` strategies.
test-dependencies = [
"dep:proptest",
"zcash_protocol/test-dependencies",
"orchard/test-dependencies",
"sapling/test-dependencies",
]
Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/src/transaction/components.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Structs representing the components within Zcash transactions.
pub mod amount;
pub use zcash_protocol::value as amount;
pub mod orchard;
pub mod sapling;
pub mod sprout;
Expand Down

0 comments on commit 9cf0d55

Please sign in to comment.