diff --git a/zcash_client_backend/CHANGELOG.md b/zcash_client_backend/CHANGELOG.md index 89e91250c..ab9d2277c 100644 --- a/zcash_client_backend/CHANGELOG.md +++ b/zcash_client_backend/CHANGELOG.md @@ -28,7 +28,7 @@ and this library adheres to Rust's notion of - `wallet::input_selection::ShieldingSelector` has been factored out from the `InputSelector` trait to separate out transparent functionality and move it behind the `transparent-inputs` feature flag. -- `zcash_client_backend::fees::standard` +- `zcash_client_backend::fees::{standard, sapling}` - `zcash_client_backend::wallet`: - `WalletNote` - `ReceivedNote` diff --git a/zcash_client_backend/src/data_api/wallet/input_selection.rs b/zcash_client_backend/src/data_api/wallet/input_selection.rs index 1936ca222..564b12c75 100644 --- a/zcash_client_backend/src/data_api/wallet/input_selection.rs +++ b/zcash_client_backend/src/data_api/wallet/input_selection.rs @@ -10,7 +10,6 @@ use zcash_primitives::{ transaction::{ components::{ amount::{BalanceError, NonNegativeAmount}, - sapling::fees as sapling, TxOut, }, fees::FeeRule, @@ -21,7 +20,7 @@ use zcash_primitives::{ use crate::{ address::{RecipientAddress, UnifiedAddress}, data_api::SaplingInputSource, - fees::{ChangeError, ChangeStrategy, DustOutputPolicy, TransactionBalance}, + fees::{sapling, ChangeError, ChangeStrategy, DustOutputPolicy, TransactionBalance}, wallet::{ReceivedNote, WalletTransparentOutput}, zip321::TransactionRequest, }; diff --git a/zcash_client_backend/src/fees.rs b/zcash_client_backend/src/fees.rs index cf52956c7..e6d9d6b31 100644 --- a/zcash_client_backend/src/fees.rs +++ b/zcash_client_backend/src/fees.rs @@ -6,15 +6,14 @@ use zcash_primitives::{ transaction::{ components::{ amount::{BalanceError, NonNegativeAmount}, - sapling::fees as sapling, - transparent::fees as transparent, OutPoint, }, - fees::FeeRule, + fees::{transparent, FeeRule}, }, }; pub mod fixed; +pub mod sapling; pub mod standard; pub mod zip317; @@ -260,12 +259,16 @@ pub trait ChangeStrategy { #[cfg(test)] pub(crate) mod tests { - use zcash_primitives::transaction::components::{ - amount::NonNegativeAmount, - sapling::fees as sapling, - transparent::{fees as transparent, OutPoint, TxOut}, + use zcash_primitives::transaction::{ + components::{ + amount::NonNegativeAmount, + transparent::{OutPoint, TxOut}, + }, + fees::transparent, }; + use super::sapling; + #[derive(Debug)] pub(crate) struct TestTransparentInput { pub outpoint: OutPoint, diff --git a/zcash_client_backend/src/fees/fixed.rs b/zcash_client_backend/src/fees/fixed.rs index 773f1d8df..39314dcf7 100644 --- a/zcash_client_backend/src/fees/fixed.rs +++ b/zcash_client_backend/src/fees/fixed.rs @@ -4,17 +4,14 @@ use zcash_primitives::{ consensus::{self, BlockHeight}, memo::MemoBytes, transaction::{ - components::{ - amount::{BalanceError, NonNegativeAmount}, - sapling::fees as sapling, - transparent::fees as transparent, - }, - fees::{fixed::FeeRule as FixedFeeRule, FeeRule}, + components::amount::{BalanceError, NonNegativeAmount}, + fees::{fixed::FeeRule as FixedFeeRule, transparent, FeeRule}, }, }; use super::{ - ChangeError, ChangeStrategy, ChangeValue, DustAction, DustOutputPolicy, TransactionBalance, + sapling, ChangeError, ChangeStrategy, ChangeValue, DustAction, DustOutputPolicy, + TransactionBalance, }; /// A change strategy that and proposes change as a single output to the most current supported diff --git a/zcash_client_backend/src/fees/sapling.rs b/zcash_client_backend/src/fees/sapling.rs new file mode 100644 index 000000000..847ac7b10 --- /dev/null +++ b/zcash_client_backend/src/fees/sapling.rs @@ -0,0 +1,60 @@ +//! Types related to computation of fees and change related to the Sapling components +//! of a transaction. + +use std::convert::Infallible; + +use zcash_primitives::{ + sapling::builder::{SaplingOutputInfo, SpendDescriptionInfo}, + transaction::components::amount::NonNegativeAmount, +}; + +/// A trait that provides a minimized view of a Sapling input suitable for use in +/// fee and change calculation. +pub trait InputView { + /// An identifier for the input being spent. + fn note_id(&self) -> &NoteRef; + /// The value of the input being spent. + fn value(&self) -> NonNegativeAmount; +} + +impl InputView for Infallible { + fn note_id(&self) -> &N { + unreachable!() + } + fn value(&self) -> NonNegativeAmount { + unreachable!() + } +} + +// `SpendDescriptionInfo` does not contain a note identifier, so we can only implement +// `InputView<()>` +impl InputView<()> for SpendDescriptionInfo { + fn note_id(&self) -> &() { + &() + } + + fn value(&self) -> NonNegativeAmount { + NonNegativeAmount::try_from(self.value()) + .expect("An existing note to be spent must have a valid amount value.") + } +} + +/// A trait that provides a minimized view of a Sapling output suitable for use in +/// fee and change calculation. +pub trait OutputView { + /// The value of the output being produced. + fn value(&self) -> NonNegativeAmount; +} + +impl OutputView for SaplingOutputInfo { + fn value(&self) -> NonNegativeAmount { + NonNegativeAmount::try_from(self.value()) + .expect("Output values should be checked at construction.") + } +} + +impl OutputView for Infallible { + fn value(&self) -> NonNegativeAmount { + unreachable!() + } +} diff --git a/zcash_client_backend/src/fees/standard.rs b/zcash_client_backend/src/fees/standard.rs index 572bb814b..7dbd740ef 100644 --- a/zcash_client_backend/src/fees/standard.rs +++ b/zcash_client_backend/src/fees/standard.rs @@ -4,18 +4,19 @@ use zcash_primitives::{ consensus::{self, BlockHeight}, memo::MemoBytes, transaction::{ - components::{ - amount::NonNegativeAmount, sapling::fees as sapling, transparent::fees as transparent, - }, + components::amount::NonNegativeAmount, fees::{ fixed::FeeRule as FixedFeeRule, + transparent, zip317::{FeeError as Zip317FeeError, FeeRule as Zip317FeeRule}, StandardFeeRule, }, }, }; -use super::{fixed, zip317, ChangeError, ChangeStrategy, DustOutputPolicy, TransactionBalance}; +use super::{ + fixed, sapling, zip317, ChangeError, ChangeStrategy, DustOutputPolicy, TransactionBalance, +}; /// A change strategy that proposes change as a single output to the most current supported /// shielded pool and delegates fee calculation to the provided fee rule. diff --git a/zcash_client_backend/src/fees/zip317.rs b/zcash_client_backend/src/fees/zip317.rs index 4063041ce..0a9315759 100644 --- a/zcash_client_backend/src/fees/zip317.rs +++ b/zcash_client_backend/src/fees/zip317.rs @@ -8,12 +8,9 @@ use zcash_primitives::{ consensus::{self, BlockHeight}, memo::MemoBytes, transaction::{ - components::{ - amount::{BalanceError, NonNegativeAmount}, - sapling::fees as sapling, - transparent::fees as transparent, - }, + components::amount::{BalanceError, NonNegativeAmount}, fees::{ + transparent, zip317::{FeeError as Zip317FeeError, FeeRule as Zip317FeeRule}, FeeRule, }, @@ -21,7 +18,8 @@ use zcash_primitives::{ }; use super::{ - ChangeError, ChangeStrategy, ChangeValue, DustAction, DustOutputPolicy, TransactionBalance, + sapling, ChangeError, ChangeStrategy, ChangeValue, DustAction, DustOutputPolicy, + TransactionBalance, }; /// A change strategy that and proposes change as a single output to the most current supported diff --git a/zcash_client_backend/src/wallet.rs b/zcash_client_backend/src/wallet.rs index b95d7a730..1ee29fbd6 100644 --- a/zcash_client_backend/src/wallet.rs +++ b/zcash_client_backend/src/wallet.rs @@ -10,15 +10,15 @@ use zcash_primitives::{ transaction::{ components::{ amount::NonNegativeAmount, - sapling::fees as sapling_fees, - transparent::{self, OutPoint, TxOut}, + transparent::{OutPoint, TxOut}, }, + fees::transparent as transparent_fees, TxId, }, zip32::{AccountId, Scope}, }; -use crate::{address::UnifiedAddress, PoolType, ShieldedProtocol}; +use crate::{address::UnifiedAddress, fees::sapling as sapling_fees, PoolType, ShieldedProtocol}; /// A unique identifier for a shielded transaction output #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] @@ -122,7 +122,7 @@ impl WalletTransparentOutput { } } -impl transparent::fees::InputView for WalletTransparentOutput { +impl transparent_fees::InputView for WalletTransparentOutput { fn outpoint(&self) -> &OutPoint { &self.outpoint } diff --git a/zcash_primitives/CHANGELOG.md b/zcash_primitives/CHANGELOG.md index bc243b660..742bd4c9d 100644 --- a/zcash_primitives/CHANGELOG.md +++ b/zcash_primitives/CHANGELOG.md @@ -19,6 +19,8 @@ and this library adheres to Rust's notion of - `builder::{InProgressProofs, Unproven, Proven}` - `builder::{InProgressSignatures, Unsigned, PartiallyAuthorized}` - `builder::{MaybeSigned, SigningParts}` + - `builder::{SpendDescriptionInfo::value}` + - `builder::{SaplingOutputInfo}` - `bundle` module, containing the following types moved from `zcash_primitives::transaction::components::sapling`: - `Bundle` @@ -69,6 +71,8 @@ and this library adheres to Rust's notion of - `temporary_zcashd_write_output_v4` - `temporary_zcashd_read_v4_components` - `temporary_zcashd_write_v4_components` + - `components::transparent`: + - `builder::TransparentInputInfo` - `fees::StandardFeeRule` - Constants in `fees::zip317`: - `MARGINAL_FEE` @@ -182,7 +186,14 @@ and this library adheres to Rust's notion of argument as the diversifier may be obtained from the note. - `components::transparent::TxOut.value` now has type `NonNegativeAmount` instead of `Amount`. + - `components::transparent::fees` has been moved to + `zcash_primitives::transaction::fees::transparent` + - `components::transparent::builder::TransparentBuilder::{inputs, outputs}` + have changed to return `&[TransparentInputInfo]` and `&[TxOut]` respectively, + in order to avoid coupling to the fee traits. - `Unauthorized::SaplingAuth` now has type `InProgress`. + - `fees::FeeRule::fee_required` now takes an additional `orchard_action_count` + argument. - The following methods now take `NonNegativeAmount` instead of `Amount`: - `builder::Builder::{add_sapling_output, add_transparent_output}` - `components::transparent::builder::TransparentBuilder::add_output` @@ -190,12 +201,9 @@ and this library adheres to Rust's notion of - `fees::zip317::FeeRule::non_standard` - The following methods now return `NonNegativeAmount` instead of `Amount`: - `components::amount::testing::arb_nonnegative_amount` - - `components::sapling`: - - `fees::InputView::value` - - `fees::OutputView::value` - - `components::transparent`: - - `fees::InputView::value` - - `fees::OutputView::value` + - `fees::transparent`: + - `InputView::value` + - `OutputView::value` - `fees::FeeRule::{fee_required, fee_required_zfuture}` - `fees::fixed::FeeRule::fixed_fee` - `fees::zip317::FeeRule::marginal_fee` @@ -237,6 +245,8 @@ and this library adheres to Rust's notion of - `SpendDescription::::apply_signature` - `Bundle::::apply_signatures` (use `Bundle::>::apply_signatures` instead). + - The `fees` module was removed. Its contents were unused in this crate, + are now instead made available by `zcash_client_backend::fees::sapling`. - `impl From for u64` - `zcash_primitives::zip32`: - `sapling` module (moved from `zcash_primitives::sapling::zip32`). diff --git a/zcash_primitives/src/sapling/builder.rs b/zcash_primitives/src/sapling/builder.rs index 9a7cdab31..76266b11a 100644 --- a/zcash_primitives/src/sapling/builder.rs +++ b/zcash_primitives/src/sapling/builder.rs @@ -25,10 +25,7 @@ use crate::{ zip32::ExtendedSpendingKey, Diversifier, MerklePath, Node, Note, PaymentAddress, ProofGenerationKey, SaplingIvk, }, - transaction::{ - builder::Progress, - components::{amount::NonNegativeAmount, sapling::fees}, - }, + transaction::builder::Progress, }; /// If there are any shielded inputs, always have at least two shielded outputs, padding @@ -77,18 +74,6 @@ pub struct SpendDescriptionInfo { rcv: ValueCommitTrapdoor, } -impl fees::InputView<()> for SpendDescriptionInfo { - fn note_id(&self) -> &() { - // The builder does not make use of note identifiers, so we can just return the unit value. - &() - } - - fn value(&self) -> NonNegativeAmount { - // An existing note to be spent must have a valid amount value. - NonNegativeAmount::from_u64(self.note.value().inner()).unwrap() - } -} - impl SpendDescriptionInfo { fn new_internal( mut rng: &mut R, @@ -105,6 +90,10 @@ impl SpendDescriptionInfo { } } + pub fn value(&self) -> NoteValue { + self.note.value() + } + fn build( self, anchor: Option, @@ -154,7 +143,7 @@ impl SpendDescriptionInfo { /// A struct containing the information required in order to construct a /// Sapling output to a transaction. #[derive(Clone)] -struct SaplingOutputInfo { +pub struct SaplingOutputInfo { /// `None` represents the `ovk = ⊥` case. ovk: Option, note: Note, @@ -249,12 +238,13 @@ impl SaplingOutputInfo { zkproof, ) } -} -impl fees::OutputView for SaplingOutputInfo { - fn value(&self) -> NonNegativeAmount { - NonNegativeAmount::from_u64(self.note.value().inner()) - .expect("Note values should be checked at construction.") + pub fn recipient(&self) -> PaymentAddress { + self.note.recipient() + } + + pub fn value(&self) -> NoteValue { + self.note.value() } } @@ -317,12 +307,12 @@ impl SaplingBuilder { /// Returns the list of Sapling inputs that will be consumed by the transaction being /// constructed. - pub fn inputs(&self) -> &[impl fees::InputView<()>] { + pub fn inputs(&self) -> &[SpendDescriptionInfo] { &self.spends } /// Returns the Sapling outputs that will be produced by the transaction being constructed - pub fn outputs(&self) -> &[impl fees::OutputView] { + pub fn outputs(&self) -> &[SaplingOutputInfo] { &self.outputs } diff --git a/zcash_primitives/src/transaction/builder.rs b/zcash_primitives/src/transaction/builder.rs index ba5e23983..358a0b51e 100644 --- a/zcash_primitives/src/transaction/builder.rs +++ b/zcash_primitives/src/transaction/builder.rs @@ -20,8 +20,7 @@ use crate::{ transaction::{ components::{ amount::{Amount, BalanceError}, - sapling::fees as sapling_fees, - transparent::{self, builder::TransparentBuilder}, + transparent::{self, builder::TransparentBuilder, TxOut}, }, fees::FeeRule, sighash::{signature_hash, SignableInput}, @@ -31,7 +30,10 @@ use crate::{ }; #[cfg(feature = "transparent-inputs")] -use crate::transaction::components::transparent::TxOut; +use crate::transaction::components::transparent::builder::TransparentInputInfo; + +#[cfg(not(feature = "transparent-inputs"))] +use std::convert::Infallible; #[cfg(feature = "zfuture")] use crate::{ @@ -184,25 +186,26 @@ impl<'a, P, R> Builder<'a, P, R> { /// Returns the set of transparent inputs currently committed to be consumed /// by the transaction. - pub fn transparent_inputs(&self) -> &[impl transparent::fees::InputView] { + #[cfg(feature = "transparent-inputs")] + pub fn transparent_inputs(&self) -> &[TransparentInputInfo] { self.transparent_builder.inputs() } /// Returns the set of transparent outputs currently set to be produced by /// the transaction. - pub fn transparent_outputs(&self) -> &[impl transparent::fees::OutputView] { + pub fn transparent_outputs(&self) -> &[TxOut] { self.transparent_builder.outputs() } /// Returns the set of Sapling inputs currently committed to be consumed /// by the transaction. - pub fn sapling_inputs(&self) -> &[impl sapling_fees::InputView<()>] { + pub fn sapling_inputs(&self) -> &[sapling::builder::SpendDescriptionInfo] { self.sapling_builder.inputs() } /// Returns the set of Sapling outputs currently set to be produced by /// the transaction. - pub fn sapling_outputs(&self) -> &[impl sapling_fees::OutputView] { + pub fn sapling_outputs(&self) -> &[sapling::builder::SaplingOutputInfo] { self.sapling_builder.outputs() } } @@ -414,10 +417,16 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> { /// This fee is a function of the spends and outputs that have been added to the builder, /// pursuant to the specified [`FeeRule`]. pub fn get_fee(&self, fee_rule: &FR) -> Result { + #[cfg(feature = "transparent-inputs")] + let transparent_inputs = self.transparent_builder.inputs(); + + #[cfg(not(feature = "transparent-inputs"))] + let transparent_inputs: &[Infallible] = &[]; + fee_rule.fee_required( &self.params, self.target_height, - self.transparent_builder.inputs(), + transparent_inputs, self.transparent_builder.outputs(), self.sapling_builder.inputs().len(), self.sapling_builder.bundle_output_count(), @@ -460,14 +469,28 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> { output_prover: &OP, fee_rule: &FR, ) -> Result<(Transaction, SaplingMetadata), Error> { + #[cfg(feature = "transparent-inputs")] + let transparent_inputs = self.transparent_builder.inputs(); + + #[cfg(not(feature = "transparent-inputs"))] + let transparent_inputs: &[Infallible] = &[]; + let fee = fee_rule .fee_required_zfuture( &self.params, self.target_height, - self.transparent_builder.inputs(), + transparent_inputs, self.transparent_builder.outputs(), self.sapling_builder.inputs().len(), self.sapling_builder.bundle_output_count(), + std::cmp::max( + self.orchard_builder + .as_ref() + .map_or(0, |builder| builder.outputs().len()), + self.orchard_builder + .as_ref() + .map_or(0, |builder| builder.spends().len()), + ), self.tze_builder.inputs(), self.tze_builder.outputs(), ) diff --git a/zcash_primitives/src/transaction/components/sapling.rs b/zcash_primitives/src/transaction/components/sapling.rs index a0acd4147..0d37250d5 100644 --- a/zcash_primitives/src/transaction/components/sapling.rs +++ b/zcash_primitives/src/transaction/components/sapling.rs @@ -21,8 +21,6 @@ use crate::{ use super::{Amount, GROTH_PROOF_SIZE}; -pub mod fees; - /// Consensus rules (§4.4) & (§4.5): /// - Canonical encoding is enforced here. /// - "Not small order" is enforced here. diff --git a/zcash_primitives/src/transaction/components/sapling/fees.rs b/zcash_primitives/src/transaction/components/sapling/fees.rs deleted file mode 100644 index 61800b1e8..000000000 --- a/zcash_primitives/src/transaction/components/sapling/fees.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! Types related to computation of fees and change related to the Sapling components -//! of a transaction. - -use crate::transaction::components::amount::NonNegativeAmount; - -/// A trait that provides a minimized view of a Sapling input suitable for use in -/// fee and change calculation. -pub trait InputView { - /// An identifier for the input being spent. - fn note_id(&self) -> &NoteRef; - /// The value of the input being spent. - fn value(&self) -> NonNegativeAmount; -} - -/// A trait that provides a minimized view of a Sapling output suitable for use in -/// fee and change calculation. -pub trait OutputView { - /// The value of the output being produced. - fn value(&self) -> NonNegativeAmount; -} diff --git a/zcash_primitives/src/transaction/components/transparent.rs b/zcash_primitives/src/transaction/components/transparent.rs index e580f4050..b7049fb3e 100644 --- a/zcash_primitives/src/transaction/components/transparent.rs +++ b/zcash_primitives/src/transaction/components/transparent.rs @@ -10,7 +10,6 @@ use crate::legacy::{Script, TransparentAddress}; use super::amount::{Amount, BalanceError, NonNegativeAmount}; pub mod builder; -pub mod fees; pub trait Authorization: Debug { type ScriptSig: Debug + Clone + PartialEq; diff --git a/zcash_primitives/src/transaction/components/transparent/builder.rs b/zcash_primitives/src/transaction/components/transparent/builder.rs index 757f8c8c4..6ca1bd7ff 100644 --- a/zcash_primitives/src/transaction/components/transparent/builder.rs +++ b/zcash_primitives/src/transaction/components/transparent/builder.rs @@ -7,10 +7,9 @@ use crate::{ transaction::{ components::{ amount::{Amount, BalanceError, NonNegativeAmount}, - transparent::{self, fees, Authorization, Authorized, Bundle, TxIn, TxOut}, + transparent::{self, Authorization, Authorized, Bundle, TxIn, TxOut}, }, sighash::TransparentAuthorizingContext, - OutPoint, }, }; @@ -18,6 +17,7 @@ use crate::{ use { crate::transaction::{ self as tx, + components::transparent::OutPoint, sighash::{signature_hash, SignableInput, SIGHASH_ALL}, TransactionData, TxDigests, }, @@ -40,25 +40,9 @@ impl fmt::Display for Error { } } -/// An uninhabited type that allows the type of [`TransparentBuilder::inputs`] -/// to resolve when the transparent-inputs feature is not turned on. -#[cfg(not(feature = "transparent-inputs"))] -#[derive(Debug)] -enum InvalidTransparentInput {} - -#[cfg(not(feature = "transparent-inputs"))] -impl fees::InputView for InvalidTransparentInput { - fn outpoint(&self) -> &OutPoint { - panic!("transparent-inputs feature flag is not enabled."); - } - fn coin(&self) -> &TxOut { - panic!("transparent-inputs feature flag is not enabled."); - } -} - #[cfg(feature = "transparent-inputs")] #[derive(Debug, Clone)] -struct TransparentInputInfo { +pub struct TransparentInputInfo { sk: secp256k1::SecretKey, pubkey: [u8; secp256k1::constants::PUBLIC_KEY_SIZE], utxo: OutPoint, @@ -66,12 +50,12 @@ struct TransparentInputInfo { } #[cfg(feature = "transparent-inputs")] -impl fees::InputView for TransparentInputInfo { - fn outpoint(&self) -> &OutPoint { +impl TransparentInputInfo { + pub fn outpoint(&self) -> &OutPoint { &self.utxo } - fn coin(&self) -> &TxOut { + pub fn coin(&self) -> &TxOut { &self.coin } } @@ -110,19 +94,13 @@ impl TransparentBuilder { /// Returns the list of transparent inputs that will be consumed by the transaction being /// constructed. - pub fn inputs(&self) -> &[impl fees::InputView] { - #[cfg(feature = "transparent-inputs")] - return &self.inputs; - - #[cfg(not(feature = "transparent-inputs"))] - { - let invalid: &[InvalidTransparentInput] = &[]; - invalid - } + #[cfg(feature = "transparent-inputs")] + pub fn inputs(&self) -> &[TransparentInputInfo] { + &self.inputs } /// Returns the transparent outputs that will be produced by the transaction being constructed. - pub fn outputs(&self) -> &[impl fees::OutputView] { + pub fn outputs(&self) -> &[TxOut] { &self.vout } diff --git a/zcash_primitives/src/transaction/components/tze.rs b/zcash_primitives/src/transaction/components/tze.rs index e038d3eec..0e88d35d1 100644 --- a/zcash_primitives/src/transaction/components/tze.rs +++ b/zcash_primitives/src/transaction/components/tze.rs @@ -12,7 +12,6 @@ use super::amount::Amount; use crate::{extensions::transparent as tze, transaction::TxId}; pub mod builder; -pub mod fees; fn to_io_error(_: std::num::TryFromIntError) -> io::Error { 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 82bd1f2e0..ff8ca49bf 100644 --- a/zcash_primitives/src/transaction/components/tze/builder.rs +++ b/zcash_primitives/src/transaction/components/tze/builder.rs @@ -9,7 +9,7 @@ use crate::{ self as tx, components::{ amount::{Amount, BalanceError}, - tze::{fees, Authorization, Authorized, Bundle, OutPoint, TzeIn, TzeOut}, + tze::{Authorization, Authorized, Bundle, OutPoint, TzeIn, TzeOut}, }, }, }; @@ -36,16 +36,16 @@ pub struct TzeSigner<'a, BuildCtx> { } #[derive(Clone)] -struct TzeBuildInput { +pub struct TzeBuildInput { tzein: TzeIn<()>, coin: TzeOut, } -impl fees::InputView for TzeBuildInput { - fn outpoint(&self) -> &OutPoint { +impl TzeBuildInput { + pub fn outpoint(&self) -> &OutPoint { &self.tzein.prevout } - fn coin(&self) -> &TzeOut { + pub fn coin(&self) -> &TzeOut { &self.coin } } @@ -72,11 +72,11 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> { } } - pub fn inputs(&self) -> &[impl fees::InputView] { + pub fn inputs(&self) -> &[TzeBuildInput] { &self.vin } - pub fn outputs(&self) -> &[impl fees::OutputView] { + pub fn outputs(&self) -> &[TzeOut] { &self.vout } diff --git a/zcash_primitives/src/transaction/fees.rs b/zcash_primitives/src/transaction/fees.rs index fbdda8f7b..313391be6 100644 --- a/zcash_primitives/src/transaction/fees.rs +++ b/zcash_primitives/src/transaction/fees.rs @@ -2,15 +2,16 @@ use crate::{ consensus::{self, BlockHeight}, - transaction::components::{amount::NonNegativeAmount, transparent::fees as transparent}, + transaction::components::amount::NonNegativeAmount, }; -#[cfg(feature = "zfuture")] -use crate::transaction::components::tze::fees as tze; - pub mod fixed; +pub mod transparent; pub mod zip317; +#[cfg(feature = "zfuture")] +pub mod tze; + /// A trait that represents the ability to compute the fees that must be paid /// by a transaction having a specified set of inputs and outputs. pub trait FeeRule { @@ -52,6 +53,7 @@ pub trait FutureFeeRule: FeeRule { transparent_outputs: &[impl transparent::OutputView], sapling_input_count: usize, sapling_output_count: usize, + orchard_action_count: usize, tze_inputs: &[impl tze::InputView], tze_outputs: &[impl tze::OutputView], ) -> Result; diff --git a/zcash_primitives/src/transaction/fees/fixed.rs b/zcash_primitives/src/transaction/fees/fixed.rs index bb323a386..660eb5f78 100644 --- a/zcash_primitives/src/transaction/fees/fixed.rs +++ b/zcash_primitives/src/transaction/fees/fixed.rs @@ -1,11 +1,11 @@ use crate::{ consensus::{self, BlockHeight}, - transaction::components::{amount::NonNegativeAmount, transparent::fees as transparent}, - transaction::fees::zip317, + transaction::components::amount::NonNegativeAmount, + transaction::fees::{transparent, zip317}, }; #[cfg(feature = "zfuture")] -use crate::transaction::components::tze::fees as tze; +use crate::transaction::fees::tze; /// A fee rule that always returns a fixed fee, irrespective of the structure of /// the transaction being constructed. @@ -72,6 +72,7 @@ impl super::FutureFeeRule for FeeRule { _transparent_outputs: &[impl transparent::OutputView], _sapling_input_count: usize, _sapling_output_count: usize, + _orchard_action_count: usize, _tze_inputs: &[impl tze::InputView], _tze_outputs: &[impl tze::OutputView], ) -> Result { diff --git a/zcash_primitives/src/transaction/components/transparent/fees.rs b/zcash_primitives/src/transaction/fees/transparent.rs similarity index 62% rename from zcash_primitives/src/transaction/components/transparent/fees.rs rename to zcash_primitives/src/transaction/fees/transparent.rs index 12ac0d616..e5c591604 100644 --- a/zcash_primitives/src/transaction/components/transparent/fees.rs +++ b/zcash_primitives/src/transaction/fees/transparent.rs @@ -1,12 +1,16 @@ //! Types related to computation of fees and change related to the transparent components //! of a transaction. -use super::TxOut; +use std::convert::Infallible; + use crate::{ legacy::Script, - transaction::{components::amount::NonNegativeAmount, OutPoint}, + transaction::components::{amount::NonNegativeAmount, transparent::TxOut, OutPoint}, }; +#[cfg(feature = "transparent-inputs")] +use crate::transaction::components::transparent::builder::TransparentInputInfo; + /// This trait provides a minimized view of a transparent input suitable for use in /// fee and change computation. pub trait InputView: std::fmt::Debug { @@ -16,6 +20,26 @@ pub trait InputView: std::fmt::Debug { fn coin(&self) -> &TxOut; } +#[cfg(feature = "transparent-inputs")] +impl InputView for TransparentInputInfo { + fn outpoint(&self) -> &OutPoint { + self.outpoint() + } + + fn coin(&self) -> &TxOut { + self.coin() + } +} + +impl InputView for Infallible { + fn outpoint(&self) -> &OutPoint { + unreachable!() + } + fn coin(&self) -> &TxOut { + unreachable!() + } +} + /// This trait provides a minimized view of a transparent output suitable for use in /// fee and change computation. pub trait OutputView: std::fmt::Debug { diff --git a/zcash_primitives/src/transaction/components/tze/fees.rs b/zcash_primitives/src/transaction/fees/tze.rs similarity index 78% rename from zcash_primitives/src/transaction/components/tze/fees.rs rename to zcash_primitives/src/transaction/fees/tze.rs index 1a72d6cc3..1a603f206 100644 --- a/zcash_primitives/src/transaction/components/tze/fees.rs +++ b/zcash_primitives/src/transaction/fees/tze.rs @@ -1,10 +1,10 @@ //! Abstractions and types related to fee calculations for TZE components of a transaction. use crate::{ - extensions::transparent::{self as tze}, + extensions::transparent as tze, transaction::components::{ amount::Amount, - tze::{OutPoint, TzeOut}, + tze::{builder::TzeBuildInput, OutPoint, TzeOut}, }, }; @@ -17,6 +17,15 @@ pub trait InputView { fn coin(&self) -> &TzeOut; } +impl InputView for TzeBuildInput { + fn outpoint(&self) -> &OutPoint { + self.outpoint() + } + fn coin(&self) -> &TzeOut { + self.coin() + } +} + /// This trait provides a minimized view of a TZE output suitable for use in /// fee computation. pub trait OutputView { diff --git a/zcash_primitives/src/transaction/fees/zip317.rs b/zcash_primitives/src/transaction/fees/zip317.rs index b0b08d7c0..c12a5cc1c 100644 --- a/zcash_primitives/src/transaction/fees/zip317.rs +++ b/zcash_primitives/src/transaction/fees/zip317.rs @@ -7,9 +7,12 @@ use core::cmp::max; use crate::{ consensus::{self, BlockHeight}, legacy::TransparentAddress, - transaction::components::{ - amount::{BalanceError, NonNegativeAmount}, - transparent::{fees as transparent, OutPoint}, + transaction::{ + components::{ + amount::{BalanceError, NonNegativeAmount}, + transparent::OutPoint, + }, + fees::transparent, }, };