From 26d95b4a0e0acdb81eb0020961bf48e1a7610868 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 26 Apr 2023 14:06:52 +0000 Subject: [PATCH 1/2] zcash_primitives: Temporarily expose v5 Sapling bundle parsing This is currently exposed via `Transaction` for usage in `zcashd`, but may be removed in future (if `zcashd` moves transaction parsing entirely into Rust), or renamed and stabilised. --- zcash_primitives/CHANGELOG.md | 5 +++++ zcash_primitives/src/transaction/mod.rs | 26 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/zcash_primitives/CHANGELOG.md b/zcash_primitives/CHANGELOG.md index b1cf2e126..dbc7d8fe1 100644 --- a/zcash_primitives/CHANGELOG.md +++ b/zcash_primitives/CHANGELOG.md @@ -6,6 +6,11 @@ and this library adheres to Rust's notion of [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `zcash_primitives::transaction`: + - `Transaction::temporary_zcashd_read_v5_sapling` + - `Transaction::temporary_zcashd_write_v5_sapling` + ### Changed - Bumped dependencies to `secp256k1 0.26`, `hdwallet 0.4`. - `zcash_primitives::transactions::component::amount::DEFAULT_FEE` increased zip317 diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 5bd4af154..a500b54bb 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -730,6 +730,13 @@ impl Transaction { Ok((consensus_branch_id, lock_time, expiry_height)) } + #[cfg(feature = "temporary-zcashd")] + pub fn temporary_zcashd_read_v5_sapling( + reader: R, + ) -> io::Result>> { + Self::read_v5_sapling(reader) + } + #[allow(clippy::redundant_closure)] fn read_v5_sapling( mut reader: R, @@ -917,8 +924,23 @@ impl Transaction { Ok(()) } - pub fn write_v5_sapling(&self, mut writer: W) -> io::Result<()> { - if let Some(bundle) = &self.sapling_bundle { + #[cfg(feature = "temporary-zcashd")] + pub fn temporary_zcashd_write_v5_sapling( + sapling_bundle: Option<&sapling::Bundle>, + writer: W, + ) -> io::Result<()> { + Self::write_v5_sapling_inner(sapling_bundle, writer) + } + + pub fn write_v5_sapling(&self, writer: W) -> io::Result<()> { + Self::write_v5_sapling_inner(self.sapling_bundle.as_ref(), writer) + } + + fn write_v5_sapling_inner( + sapling_bundle: Option<&sapling::Bundle>, + mut writer: W, + ) -> io::Result<()> { + if let Some(bundle) = sapling_bundle { Vector::write(&mut writer, bundle.shielded_spends(), |w, e| { e.write_v5_without_witness_data(w) })?; From 8db7a071a0b79687d79ffc6894fe573944b16008 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 26 Apr 2023 15:33:48 +0000 Subject: [PATCH 2/2] zcash_primitives: `impl DynamicUsage for sapling::Bundle` --- zcash_primitives/CHANGELOG.md | 4 +++ .../src/transaction/components/sapling.rs | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/zcash_primitives/CHANGELOG.md b/zcash_primitives/CHANGELOG.md index dbc7d8fe1..001a41fc5 100644 --- a/zcash_primitives/CHANGELOG.md +++ b/zcash_primitives/CHANGELOG.md @@ -10,6 +10,10 @@ and this library adheres to Rust's notion of - `zcash_primitives::transaction`: - `Transaction::temporary_zcashd_read_v5_sapling` - `Transaction::temporary_zcashd_write_v5_sapling` +- Implementations of `memuse::DynamicUsage` for the following types: + - `zcash_primitives::transaction::components::sapling`: + - `Bundle` + - `SpendDescription` ### Changed - Bumped dependencies to `secp256k1 0.26`, `hdwallet 0.4`. diff --git a/zcash_primitives/src/transaction/components/sapling.rs b/zcash_primitives/src/transaction/components/sapling.rs index dc1b66380..149bfdbd4 100644 --- a/zcash_primitives/src/transaction/components/sapling.rs +++ b/zcash_primitives/src/transaction/components/sapling.rs @@ -192,6 +192,24 @@ impl Bundle { } } +impl DynamicUsage for Bundle { + fn dynamic_usage(&self) -> usize { + self.shielded_spends.dynamic_usage() + self.shielded_outputs.dynamic_usage() + } + + fn dynamic_usage_bounds(&self) -> (usize, Option) { + let bounds = ( + self.shielded_spends.dynamic_usage_bounds(), + self.shielded_outputs.dynamic_usage_bounds(), + ); + + ( + bounds.0 .0 + bounds.1 .0, + bounds.0 .1.zip(bounds.1 .1).map(|(a, b)| a + b), + ) + } +} + #[derive(Clone)] pub struct SpendDescription { cv: ValueCommitment, @@ -263,6 +281,16 @@ impl SpendDescription { } } +impl DynamicUsage for SpendDescription { + fn dynamic_usage(&self) -> usize { + self.zkproof.dynamic_usage() + } + + fn dynamic_usage_bounds(&self) -> (usize, Option) { + self.zkproof.dynamic_usage_bounds() + } +} + /// Consensus rules (§4.4) & (§4.5): /// - Canonical encoding is enforced here. /// - "Not small order" is enforced here.