Expose less information about Sapling outputs to change calculation.

This commit is contained in:
Kris Nuttycombe 2022-11-03 09:23:33 -06:00
parent 1be97e9cef
commit 37e78e1fe3
5 changed files with 33 additions and 26 deletions

View File

@ -82,7 +82,7 @@ pub trait ChangeStrategy {
transparent_inputs: &[impl TransparentInput],
transparent_outputs: &[TxOut],
sapling_inputs: &[impl SaplingInput],
sapling_outputs: &[SaplingOutput],
sapling_outputs: &[impl SaplingOutput],
) -> Result<TransactionBalance, ChangeError<Self::Error>>;
}
@ -115,7 +115,7 @@ impl ChangeStrategy for BasicFixedFeeChangeStrategy {
transparent_inputs: &[impl TransparentInput],
transparent_outputs: &[TxOut],
sapling_inputs: &[impl SaplingInput],
sapling_outputs: &[SaplingOutput],
sapling_outputs: &[impl SaplingOutput],
) -> Result<TransactionBalance, ChangeError<Self::Error>> {
let overflow = || ChangeError::StrategyError(BalanceError::Overflow);
let underflow = || ChangeError::StrategyError(BalanceError::Underflow);

View File

@ -21,15 +21,15 @@ and this library adheres to Rust's notion of
- `Builder::sapling_inputs()`
- `Builder::sapling_outputs()`
- `zcash_primitives::transaction::fees` a new module containing abstractions
and types related to fee calculations.
and types related to fee calculations.
- `FeeRule` a trait that describes how to compute the fee required for a
transaction given inputs and outputs to the transaction.
- Added to `zcash_primitives::transaction::components::sapling::builder`
- `SaplingInput` a trait that provides a minimized view of a Sapling input suitable
for use in fee computation.
- The `SaplingOutput` type has been made public, so that it can now be used in fee
computation.
- `SaplingBuilder::inputs` and `SaplingBuilder::outputs`: accessors for Sapling
for use in change and fee computation.
- `SaplingOutput` a trait that provides a minimized view of a Sapling output suitable
for use in change and fee computation.
- `SaplingBuilder::inputs` and `SaplingBuilder::outputs`: accessors for Sapling
builder state.
- Added to `zcash_primitives::transaction::components::transparent::builder`
- `TransparentInput` a trait that provides a minimized view of a transparent input suitable
@ -74,9 +74,9 @@ and this library adheres to Rust's notion of
- `ChainCode::as_bytes`
- `DiversifierIndex::{as_bytes}`
- Implementations of `From<u32>` and `From<u64>` for `DiversifierIndex`
- `zcash_primitives::zip32::sapling` has been added and now contains
- `zcash_primitives::zip32::sapling` has been added and now contains
all of the Sapling zip32 key types that were previously located in
`zcash_primitives::zip32` directly. The base `zip32` module reexports
`zcash_primitives::zip32` directly. The base `zip32` module reexports
the moved types for backwards compatibility.
- `DiversifierKey::{from_bytes, as_bytes}`
- `ExtendedSpendingKey::{from_bytes, to_bytes}`

View File

@ -177,7 +177,7 @@ impl<'a, P, R> Builder<'a, P, R> {
/// Returns the set of Sapling outputs currently set to be produced by
/// the transaction.
pub fn sapling_outputs(&self) -> &[SaplingOutput] {
pub fn sapling_outputs(&self) -> &[impl SaplingOutput] {
self.sapling_builder.outputs()
}
}

View File

@ -67,6 +67,13 @@ pub trait SaplingInput {
fn value(&self) -> Amount;
}
/// A trait that provides a minimized view of a Sapling output suitable for use in
/// fee and change calculation.
pub trait SaplingOutput {
/// The value of the output being produced.
fn value(&self) -> Amount;
}
#[derive(Debug, Clone)]
pub struct SpendDescriptionInfo {
extsk: ExtendedSpendingKey,
@ -87,7 +94,7 @@ impl SaplingInput for SpendDescriptionInfo {
/// A struct containing the information required in order to construct a
/// Sapling output to a transaction.
#[derive(Clone)]
pub struct SaplingOutput {
struct SaplingOutputInfo {
/// `None` represents the `ovk = ⊥` case.
ovk: Option<OutgoingViewingKey>,
to: PaymentAddress,
@ -95,7 +102,7 @@ pub struct SaplingOutput {
memo: MemoBytes,
}
impl SaplingOutput {
impl SaplingOutputInfo {
fn new_internal<P: consensus::Parameters, R: RngCore>(
params: &P,
rng: &mut R,
@ -119,7 +126,7 @@ impl SaplingOutput {
rseed,
};
Ok(SaplingOutput {
Ok(SaplingOutputInfo {
ovk,
to,
note,
@ -127,12 +134,6 @@ impl SaplingOutput {
})
}
pub fn value(&self) -> Amount {
// this unwrap is safe because the note's value was initially
// constructed from an `Amount`.
Amount::from_u64(self.note.value).unwrap()
}
fn build<P: consensus::Parameters, Pr: TxProver, R: RngCore>(
self,
prover: &Pr,
@ -173,6 +174,12 @@ impl SaplingOutput {
}
}
impl SaplingOutput for SaplingOutputInfo {
fn value(&self) -> Amount {
Amount::from_u64(self.note.value).expect("Note values should be checked at construction.")
}
}
/// Metadata about a transaction created by a [`SaplingBuilder`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SaplingMetadata {
@ -217,7 +224,7 @@ pub struct SaplingBuilder<P> {
target_height: BlockHeight,
value_balance: Amount,
spends: Vec<SpendDescriptionInfo>,
outputs: Vec<SaplingOutput>,
outputs: Vec<SaplingOutputInfo>,
}
#[derive(Clone)]
@ -255,7 +262,7 @@ impl<P> SaplingBuilder<P> {
}
/// Returns the Sapling outputs that will be produced by the transaction being constructed
pub fn outputs(&self) -> &[SaplingOutput] {
pub fn outputs(&self) -> &[impl SaplingOutput] {
&self.outputs
}
@ -314,7 +321,7 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
value: Amount,
memo: MemoBytes,
) -> Result<(), Error> {
let output = SaplingOutput::new_internal(
let output = SaplingOutputInfo::new_internal(
&self.params,
&mut rng,
self.target_height,

View File

@ -29,7 +29,7 @@ pub trait FeeRule {
transparent_inputs: &[impl TransparentInput],
transparent_outputs: &[TxOut],
sapling_inputs: &[impl SaplingInput],
sapling_outputs: &[SaplingOutput],
sapling_outputs: &[impl SaplingOutput],
) -> Result<Amount, Self::Error>;
}
@ -50,7 +50,7 @@ pub trait FutureFeeRule: FeeRule {
transparent_inputs: &[impl TransparentInput],
transparent_outputs: &[TxOut],
sapling_inputs: &[impl SaplingInput],
sapling_outputs: &[SaplingOutput],
sapling_outputs: &[impl SaplingOutput],
tze_inputs: &[impl TzeInput],
tze_outputs: &[TzeOut],
) -> Result<Amount, Self::Error>;
@ -79,7 +79,7 @@ impl FeeRule for FixedFeeRule {
_transparent_inputs: &[impl TransparentInput],
_transparent_outputs: &[TxOut],
_sapling_inputs: &[impl SaplingInput],
_sapling_outputs: &[SaplingOutput],
_sapling_outputs: &[impl SaplingOutput],
) -> Result<Amount, Self::Error> {
Ok(self.fixed_fee)
}
@ -94,7 +94,7 @@ impl FutureFeeRule for FixedFeeRule {
_transparent_inputs: &[impl TransparentInput],
_transparent_outputs: &[TxOut],
_sapling_inputs: &[impl SaplingInput],
_sapling_outputs: &[SaplingOutput],
_sapling_outputs: &[impl SaplingOutput],
_tze_inputs: &[impl TzeInput],
_tze_outputs: &[TzeOut],
) -> Result<Amount, Self::Error> {