From b823aa31b3a7b907106f834830f9aa51952e74a4 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Tue, 21 Jan 2025 16:25:19 -0700 Subject: [PATCH] zcash_extensions: Fix build errors following value changes to `zcash_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. --- Cargo.lock | 2 ++ zcash_extensions/Cargo.toml | 6 ++-- zcash_extensions/src/transparent/demo.rs | 30 +++++++++---------- .../src/extensions/transparent.rs | 10 +++---- zcash_primitives/src/transaction/builder.rs | 5 ++-- .../src/transaction/components/tze.rs | 9 +++--- .../src/transaction/components/tze/builder.rs | 24 +++++---------- zcash_primitives/src/transaction/fees/tze.rs | 10 +++---- 8 files changed, 43 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17b61d5463..eed9787a71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6392,6 +6392,8 @@ dependencies = [ "zcash_address", "zcash_primitives", "zcash_proofs", + "zcash_protocol", + "zcash_transparent", ] [[package]] diff --git a/zcash_extensions/Cargo.toml b/zcash_extensions/Cargo.toml index 6cf04207fc..b61e136497 100644 --- a/zcash_extensions/Cargo.toml +++ b/zcash_extensions/Cargo.toml @@ -16,7 +16,8 @@ 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 @@ -24,8 +25,9 @@ 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 = [] diff --git a/zcash_extensions/src/transparent/demo.rs b/zcash_extensions/src/transparent/demo.rs index 84054ccc8f..0c3378026c 100644 --- a/zcash_extensions/src/transparent/demo.rs +++ b/zcash_extensions/src/transparent/demo.rs @@ -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 { @@ -377,7 +375,7 @@ impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder { /// construction. pub fn demo_open( &mut self, - value: Amount, + value: Zatoshis, hash_1: [u8; 32], ) -> Result<(), DemoBuildError> { // Call through to the generic builder. @@ -391,7 +389,7 @@ impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder { 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> { @@ -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}; @@ -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)), }; @@ -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)), }; @@ -818,7 +816,7 @@ mod tests { .add_sapling_spend::(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) diff --git a/zcash_primitives/src/extensions/transparent.rs b/zcash_primitives/src/extensions/transparent.rs index 65ed7f78f7..7d2dd8340c 100644 --- a/zcash_primitives/src/extensions/transparent.rs +++ b/zcash_primitives/src/extensions/transparent.rs @@ -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)] @@ -203,11 +201,11 @@ pub trait ExtensionTxBuilder<'a> { WBuilder: 'a + (FnOnce(&Self::BuildCtx) -> Result); /// 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( &mut self, extension_id: u32, - value: Amount, + value: Zatoshis, guarded_by: &Precondition, ) -> Result<(), Self::BuildError>; } diff --git a/zcash_primitives/src/transaction/builder.rs b/zcash_primitives/src/transaction/builder.rs index bf3d4cb677..3003b3e83c 100644 --- a/zcash_primitives/src/transaction/builder.rs +++ b/zcash_primitives/src/transaction/builder.rs @@ -981,10 +981,11 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Extensio fn add_tze_output( &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(()) } } diff --git a/zcash_primitives/src/transaction/components/tze.rs b/zcash_primitives/src/transaction/components/tze.rs index 72dfe26731..f316ea352d 100644 --- a/zcash_primitives/src/transaction/components/tze.rs +++ b/zcash_primitives/src/transaction/components/tze.rs @@ -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; @@ -190,7 +189,7 @@ impl TzeIn<::Witness> { #[derive(Clone, Debug, PartialEq, Eq)] pub struct TzeOut { - pub value: Amount, + pub value: Zatoshis, pub precondition: tze::Precondition, } @@ -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) } .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "value out of range"))?; diff --git a/zcash_primitives/src/transaction/components/tze/builder.rs b/zcash_primitives/src/transaction/components/tze/builder.rs index 974dac9ce8..38339cad1c 100644 --- a/zcash_primitives/src/transaction/components/tze/builder.rs +++ b/zcash_primitives/src/transaction/components/tze/builder.rs @@ -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 { @@ -100,13 +98,9 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> { pub fn add_output( &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, @@ -116,26 +110,24 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> { payload, }, }); - - Ok(()) } - pub fn value_balance(&self) -> Result { + pub fn value_balance(&self) -> Result { let total_in = self .vin .iter() .map(|tzi| tzi.coin.value) - .sum::>() + .sum::>() .ok_or(BalanceError::Overflow)?; let total_out = self .vout .iter() .map(|tzo| tzo.value) - .sum::>() + .sum::>() .ok_or(BalanceError::Overflow)?; - (total_in - total_out).ok_or(BalanceError::Underflow) + (ZatBalance::from(total_in) - ZatBalance::from(total_out)).ok_or(BalanceError::Underflow) } pub fn build(self) -> (Option>, Vec>) { diff --git a/zcash_primitives/src/transaction/fees/tze.rs b/zcash_primitives/src/transaction/fees/tze.rs index 1a603f2065..457f1ae680 100644 --- a/zcash_primitives/src/transaction/fees/tze.rs +++ b/zcash_primitives/src/transaction/fees/tze.rs @@ -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. @@ -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 }