Skip to content

Commit

Permalink
zcash_primitives: Generalise TransactionData protocol-specific bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Oct 8, 2024
1 parent 6481a71 commit 56ee59e
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 139 deletions.
6 changes: 4 additions & 2 deletions devtools/src/bin/inspect/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use zcash_primitives::{
legacy::{keys::pubkey_to_address, Script, TransparentAddress},
memo::{Memo, MemoBytes},
transaction::{
components::{amount::NonNegativeAmount, sapling as sapling_serialization, transparent},
components::{
amount::NonNegativeAmount, sapling as sapling_serialization, transparent, AllBundles,
},
sighash::{signature_hash, SignableInput, TransparentAuthorizingContext},
txid::TxIdDigester,
Authorization, Transaction, TransactionData, TxId, TxVersion,
Expand Down Expand Up @@ -207,7 +209,7 @@ pub(crate) fn inspect(
tx.write(&mut buf).unwrap();
let tx = Transaction::read(&buf[..], tx.consensus_branch_id()).unwrap();

let tx: TransactionData<PrecomputedAuth> = tx.into_data().map_authorization(
let tx: TransactionData<AllBundles<PrecomputedAuth>> = tx.into_data().map_authorization(
f_transparent,
(),
(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod tests {
use tempfile::NamedTempFile;
use zcash_primitives::{
legacy::{Script, TransparentAddress},
transaction::{components::transparent, Authorized, TransactionData, TxVersion},
transaction::{components::{transparent, AllBundles}, Authorized, TransactionData, TxVersion},
};
use zcash_protocol::{
consensus::{BranchId, Network},
Expand Down Expand Up @@ -185,7 +185,7 @@ mod tests {
.unwrap();

// Add transactions to the wallet that exercise the data migration.
let add_tx_to_wallet = |tx: TransactionData<Authorized>| {
let add_tx_to_wallet = |tx: TransactionData<AllBundles<Authorized>>| {
let tx = tx.freeze().unwrap();
let txid = tx.txid();
let mut raw_tx = vec![];
Expand Down
9 changes: 5 additions & 4 deletions zcash_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
components::{
amount::{Amount, BalanceError},
transparent::{self, builder::TransparentBuilder, TxOut},
AllBundles,
},
fees::{
transparent::{InputView, OutputView},
Expand Down Expand Up @@ -288,7 +289,7 @@ pub struct Builder<'a, P, U: sapling::builder::ProverProgress> {
sapling_asks: Vec<sapling::keys::SpendAuthorizingKey>,
orchard_saks: Vec<orchard::keys::SpendAuthorizingKey>,
#[cfg(zcash_unstable = "zfuture")]
tze_builder: TzeBuilder<'a, TransactionData<Unauthorized>>,
tze_builder: TzeBuilder<'a, TransactionData<AllBundles<Unauthorized>>>,
#[cfg(not(zcash_unstable = "zfuture"))]
tze_builder: std::marker::PhantomData<&'a ()>,
progress_notifier: U,
Expand Down Expand Up @@ -746,7 +747,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
#[cfg(zcash_unstable = "zfuture")]
let (tze_bundle, tze_signers) = self.tze_builder.build();

let unauthed_tx: TransactionData<Unauthorized> = TransactionData {
let unauthed_tx: TransactionData<AllBundles<Unauthorized>> = TransactionData {
version,
consensus_branch_id: BranchId::for_height(&self.params, self.target_height),
lock_time: 0,
Expand All @@ -765,7 +766,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
let txid_parts = unauthed_tx.digest(TxIdDigester);

let transparent_bundle = unauthed_tx.transparent_bundle.clone().map(|b| {
b.apply_signatures(
b.apply_signatures::<_, _, AllBundles<Unauthorized>>(
#[cfg(feature = "transparent-inputs")]
&unauthed_tx,
#[cfg(feature = "transparent-inputs")]
Expand Down Expand Up @@ -841,7 +842,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> ExtensionTxBuilder<'a>
for Builder<'a, P, U>
{
type BuildCtx = TransactionData<Unauthorized>;
type BuildCtx = TransactionData<AllBundles<Unauthorized>>;
type BuildError = tze::builder::Error;

fn add_tze_input<WBuilder, W: ToPayload>(
Expand Down
29 changes: 29 additions & 0 deletions zcash_primitives/src/transaction/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,38 @@ pub use crate::sapling::bundle::{OutputDescription, SpendDescription};
#[cfg(zcash_unstable = "zfuture")]
pub use self::tze::{TzeIn, TzeOut};

use super::Authorization;

// π_A + π_B + π_C
pub const GROTH_PROOF_SIZE: usize = 48 + 96 + 48;

/// The protocol-specific bundles of data within a transaction.
pub trait Bundles {
type Transparent: TransparentPart;
type Sprout: SproutPart;
type Sapling: SaplingPart;
type Orchard: OrchardPart;

#[cfg(zcash_unstable = "zfuture")]
type Tze: TzePart;
}

/// Marker type for a transaction that may contain payments within any Zcash protocol.
#[derive(Debug)]
pub struct AllBundles<A: Authorization> {
_auth: PhantomData<A>,
}

impl<A: Authorization> Bundles for AllBundles<A> {
type Transparent = Transparent<A::TransparentAuth>;
type Sprout = Sprout;
type Sapling = Sapling<A::SaplingAuth>;
type Orchard = Orchard<A::OrchardAuth>;

#[cfg(zcash_unstable = "zfuture")]
type Tze = Tze<A::TzeAuth>;
}

/// The protocol-agnostic parts of a shielded bundle.
///
/// The trait methods can be implemented without any knowledge of protocol-specific
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt;
use crate::{
legacy::{Script, TransparentAddress},
transaction::{
self as tx,
components::{
amount::{Amount, BalanceError, NonNegativeAmount},
transparent::{self, Authorization, Authorized, Bundle, TxIn, TxOut},
Expand All @@ -16,7 +17,6 @@ use crate::{
#[cfg(feature = "transparent-inputs")]
use {
crate::transaction::{
self as tx,
components::transparent::OutPoint,
sighash::{signature_hash, SignableInput, SIGHASH_ALL},
TransactionData, TxDigests,
Expand Down Expand Up @@ -239,9 +239,14 @@ impl TransparentAuthorizingContext for Unauthorized {
}

impl Bundle<Unauthorized> {
pub fn apply_signatures(
pub fn apply_signatures<
Sp: tx::sighash_v4::SproutSigDigester,
Sa: tx::sighash_v4::SaplingSigDigester,
B: tx::Bundles<Transparent = tx::Transparent<Unauthorized>, Sprout = Sp, Sapling = Sa>
+ tx::sighash_v5::FutureBundles,
>(
self,
#[cfg(feature = "transparent-inputs")] mtx: &TransactionData<tx::Unauthorized>,
#[cfg(feature = "transparent-inputs")] mtx: &TransactionData<B>,
#[cfg(feature = "transparent-inputs")] txid_parts_cache: &TxDigests<Blake2bHash>,
) -> Bundle<Authorized> {
#[cfg(feature = "transparent-inputs")]
Expand Down
4 changes: 2 additions & 2 deletions zcash_primitives/src/transaction/components/tze/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> {
impl Bundle<Unauthorized> {
pub fn into_authorized(
self,
unauthed_tx: &tx::TransactionData<tx::Unauthorized>,
signers: Vec<TzeSigner<'_, tx::TransactionData<tx::Unauthorized>>>,
unauthed_tx: &tx::TransactionData<tx::AllBundles<tx::Unauthorized>>,
signers: Vec<TzeSigner<'_, tx::TransactionData<tx::AllBundles<tx::Unauthorized>>>>,
) -> Result<Bundle<Authorized>, Error> {
// Create TZE input witnesses
let payloads = signers
Expand Down
Loading

0 comments on commit 56ee59e

Please sign in to comment.