Skip to content

Commit

Permalink
zcash_extensions: Fix build errors following value changes to `zcash_…
Browse files Browse the repository at this point in the history
…protocol`

This fixes a few errors that have been introduced over time since
`zcash_extensions` is not regularly built, and migrates
`zcash_extensions` to eliminate the use of deprecated `zcash_primitives`
APIs.
  • Loading branch information
nuttycom committed Jan 29, 2025
1 parent 2ec38ba commit b236665
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 53 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions zcash_extensions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
blake2b_simd.workspace = true
zcash_primitives.workspace = true
zcash_primitives = { workspace = true, features = ["non-standard-fees"] }
zcash_protocol.workspace = true

[dev-dependencies]
ff.workspace = true
jubjub.workspace = true
rand_core.workspace = true
sapling.workspace = true
orchard.workspace = true
transparent.workspace = true
zcash_address.workspace = true
zcash_proofs.workspace = true
zcash_proofs = { workspace = true, features = ["local-prover", "bundled-prover"] }

[features]
transparent-inputs = []
Expand Down
30 changes: 14 additions & 16 deletions zcash_extensions/src/transparent/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ use blake2b_simd::Params;

use zcash_primitives::{
extensions::transparent::{Extension, ExtensionTxBuilder, FromPayload, ToPayload},
transaction::components::{
amount::Amount,
tze::{OutPoint, TzeOut},
},
transaction::components::tze::{OutPoint, TzeOut},
};
use zcash_protocol::value::Zatoshis;

/// Types and constants used for Mode 0 (open a channel)
mod open {
Expand Down Expand Up @@ -377,7 +375,7 @@ impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder<B> {
/// construction.
pub fn demo_open(
&mut self,
value: Amount,
value: Zatoshis,
hash_1: [u8; 32],
) -> Result<(), DemoBuildError<B::BuildError>> {
// Call through to the generic builder.
Expand All @@ -391,7 +389,7 @@ impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder<B> {
pub fn demo_transfer_to_close(
&mut self,
prevout: (OutPoint, TzeOut),
transfer_amount: Amount,
transfer_amount: Zatoshis,
preimage_1: [u8; 32],
hash_2: [u8; 32],
) -> Result<(), DemoBuildError<B::BuildError>> {
Expand Down Expand Up @@ -483,21 +481,21 @@ mod tests {
use rand_core::OsRng;

use sapling::{zip32::ExtendedSpendingKey, Node, Rseed};
use transparent::{address::TransparentAddress, builder::TransparentSigningSet};
use zcash_primitives::{
consensus::{BlockHeight, BranchId, NetworkType, NetworkUpgrade, Parameters},
extensions::transparent::{self as tze, Extension, FromPayload, ToPayload},
legacy::TransparentAddress,
transaction::{
builder::{BuildConfig, Builder},
components::{
amount::{Amount, NonNegativeAmount},
transparent::builder::TransparentSigningSet,
tze::{Authorized, Bundle, OutPoint, TzeIn, TzeOut},
},
components::tze::{Authorized, Bundle, OutPoint, TzeIn, TzeOut},
fees::{fixed, zip317::MINIMUM_FEE},
Transaction, TransactionData, TxVersion,
},
};
use zcash_protocol::{
consensus::{BlockHeight, BranchId, NetworkType, NetworkUpgrade, Parameters},
value::Zatoshis,
};

use zcash_proofs::prover::LocalTxProver;

use super::{close, hash_1, open, Context, DemoBuilder, Precondition, Program, Witness};
Expand Down Expand Up @@ -669,7 +667,7 @@ mod tests {
//

let out_a = TzeOut {
value: Amount::from_u64(1).unwrap(),
value: Zatoshis::from_u64(1).unwrap(),
precondition: tze::Precondition::from(0, &Precondition::open(hash_1)),
};

Expand Down Expand Up @@ -700,7 +698,7 @@ mod tests {
witness: tze::Witness::from(0, &Witness::open(preimage_1)),
};
let out_b = TzeOut {
value: Amount::from_u64(1).unwrap(),
value: Zatoshis::const_from_u64(1),
precondition: tze::Precondition::from(0, &Precondition::close(hash_2)),
};

Expand Down Expand Up @@ -818,7 +816,7 @@ mod tests {
.add_sapling_spend::<Infallible>(dfvk.fvk().clone(), note1, witness1.path().unwrap())
.unwrap();

let value = NonNegativeAmount::const_from_u64(100000);
let value = Zatoshis::const_from_u64(100000);
let (h1, h2) = demo_hashes(&preimage_1, &preimage_2);
builder_a
.demo_open(value.into(), h1)
Expand Down
10 changes: 4 additions & 6 deletions zcash_primitives/src/extensions/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
use std::fmt;

use crate::transaction::components::{
tze::{self, TzeOut},
Amount,
};
use crate::transaction::components::tze::{self, TzeOut};
use zcash_protocol::value::Zatoshis;

/// A typesafe wrapper for witness payloads
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -203,11 +201,11 @@ pub trait ExtensionTxBuilder<'a> {
WBuilder: 'a + (FnOnce(&Self::BuildCtx) -> Result<W, Self::BuildError>);

/// Adds a TZE precondition to the transaction which must be satisfied by a future transaction's
/// witness in order to spend the specified `amount`.
/// witness in order to spend the specified value.
fn add_tze_output<Precondition: ToPayload>(
&mut self,
extension_id: u32,
value: Amount,
value: Zatoshis,
guarded_by: &Precondition,
) -> Result<(), Self::BuildError>;
}
5 changes: 3 additions & 2 deletions zcash_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,11 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Extensio
fn add_tze_output<G: ToPayload>(
&mut self,
extension_id: u32,
value: ZatBalance,
value: Zatoshis,
guarded_by: &G,
) -> Result<(), Self::BuildError> {
self.tze_builder.add_output(extension_id, value, guarded_by)
self.tze_builder.add_output(extension_id, value, guarded_by);
Ok(())

Check warning on line 988 in zcash_primitives/src/transaction/builder.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/transaction/builder.rs#L987-L988

Added lines #L987 - L988 were not covered by tests
}
}

Expand Down
9 changes: 4 additions & 5 deletions zcash_primitives/src/transaction/components/tze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::convert::TryFrom;
use std::fmt::Debug;
use std::io::{self, Read, Write};

use zcash_encoding::{CompactSize, Vector};

use super::amount::Amount;
use crate::{extensions::transparent as tze, transaction::TxId};
use zcash_encoding::{CompactSize, Vector};
use zcash_protocol::value::Zatoshis;

pub mod builder;

Expand Down Expand Up @@ -190,7 +189,7 @@ impl TzeIn<<Authorized as Authorization>::Witness> {

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TzeOut {
pub value: Amount,
pub value: Zatoshis,
pub precondition: tze::Precondition,
}

Expand All @@ -199,7 +198,7 @@ impl TzeOut {
let value = {
let mut tmp = [0; 8];
reader.read_exact(&mut tmp)?;
Amount::from_nonnegative_i64_le_bytes(tmp)
Zatoshis::from_nonnegative_i64_le_bytes(tmp)

Check warning on line 201 in zcash_primitives/src/transaction/components/tze.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/transaction/components/tze.rs#L201

Added line #L201 was not covered by tests
}
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "value out of range"))?;

Expand Down
24 changes: 8 additions & 16 deletions zcash_primitives/src/transaction/components/tze/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use crate::{
extensions::transparent::{self as tze, ToPayload},
transaction::{
self as tx,
components::{
amount::{Amount, BalanceError},
tze::{Authorization, Authorized, Bundle, OutPoint, TzeIn, TzeOut},
},
components::tze::{Authorization, Authorized, Bundle, OutPoint, TzeIn, TzeOut},
},
};
use zcash_protocol::value::{BalanceError, ZatBalance, Zatoshis};

#[derive(Debug, PartialEq, Eq)]
pub enum Error {
Expand Down Expand Up @@ -100,13 +98,9 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> {
pub fn add_output<G: ToPayload>(
&mut self,
extension_id: u32,
value: Amount,
value: Zatoshis,
guarded_by: &G,
) -> Result<(), Error> {
if value.is_negative() {
return Err(Error::InvalidAmount);
}

) -> () {
let (mode, payload) = guarded_by.to_payload();
self.vout.push(TzeOut {
value,
Expand All @@ -116,26 +110,24 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> {
payload,
},
});

Ok(())
}

pub fn value_balance(&self) -> Result<Amount, BalanceError> {
pub fn value_balance(&self) -> Result<ZatBalance, BalanceError> {

Check warning on line 115 in zcash_primitives/src/transaction/components/tze/builder.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/transaction/components/tze/builder.rs#L115

Added line #L115 was not covered by tests
let total_in = self
.vin
.iter()
.map(|tzi| tzi.coin.value)
.sum::<Option<Amount>>()
.sum::<Option<Zatoshis>>()
.ok_or(BalanceError::Overflow)?;

let total_out = self
.vout
.iter()
.map(|tzo| tzo.value)
.sum::<Option<Amount>>()
.sum::<Option<Zatoshis>>()
.ok_or(BalanceError::Overflow)?;

(total_in - total_out).ok_or(BalanceError::Underflow)
(ZatBalance::from(total_in) - ZatBalance::from(total_out)).ok_or(BalanceError::Underflow)

Check warning on line 130 in zcash_primitives/src/transaction/components/tze/builder.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/transaction/components/tze/builder.rs#L130

Added line #L130 was not covered by tests
}

pub fn build(self) -> (Option<Bundle<Unauthorized>>, Vec<TzeSigner<'a, BuildCtx>>) {
Expand Down
10 changes: 4 additions & 6 deletions zcash_primitives/src/transaction/fees/tze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
use crate::{
extensions::transparent as tze,
transaction::components::{
amount::Amount,
tze::{builder::TzeBuildInput, OutPoint, TzeOut},
},
transaction::components::tze::{builder::TzeBuildInput, OutPoint, TzeOut},
};
use zcash_protocol::value::Zatoshis;

/// This trait provides a minimized view of a TZE input suitable for use in
/// fee computation.
Expand All @@ -30,13 +28,13 @@ impl InputView for TzeBuildInput {
/// fee computation.
pub trait OutputView {
/// The value of the newly created output
fn value(&self) -> Amount;
fn value(&self) -> Zatoshis;
/// The precondition that must be satisfied in order to spend this output.
fn precondition(&self) -> &tze::Precondition;
}

impl OutputView for TzeOut {
fn value(&self) -> Amount {
fn value(&self) -> Zatoshis {
self.value
}

Expand Down

0 comments on commit b236665

Please sign in to comment.