Skip to content

Commit

Permalink
zcash_client_backend: Generalize DecryptedOutput to support Orchard
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Mar 8, 2024
1 parent 637e024 commit 9de4f1f
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 156 deletions.
15 changes: 15 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ and this library adheres to Rust's notion of
- `AccountBalance::with_orchard_balance_mut`
- `AccountBirthday::orchard_frontier`
- `BlockMetadata::orchard_tree_size`
- `DecryptedTransaction::{new, tx(), outputs()}`
- `ScannedBlock::orchard`
- `ScannedBlockCommitments::orchard`
- `SentTransaction::new`
- `ORCHARD_SHARD_HEIGHT`
- `BlockMetadata::orchard_tree_size`
- `chain::ScanSummary::{spent_orchard_note_count, received_orchard_note_count}`
Expand Down Expand Up @@ -51,12 +53,23 @@ and this library adheres to Rust's notion of
- `select_spendable_notes` now takes its `target_value` argument as a
`NonNegativeAmount`. Also, the values of the returned map are also
`NonNegativeAmount`s instead of `Amount`s.
- Fields of `DecryptedTransaction` are now private. Use `DecryptedTransaction::new`
and the newly provided accessors instead.
- Fields of `SentTransaction` are now private. Use `SentTransaction::new`
and the newly provided accessors instead.
- `ShieldedProtocol` has a new `Orchard` variant.
- `WalletCommitmentTrees`
- `type OrchardShardStore`
- `fn with_orchard_tree_mut`
- `fn put_orchard_subtree_roots`
- Added method `WalletRead::validate_seed`
- `zcash_client_backend::decrypt`:
- Fields of `DecryptedOutput` are now private. Use `DecryptedOutput::new`
and the newly provided accessors instead.
- `decrypt_transaction` now returns a `DecryptedOutput<crate::wallet::Note>`
instead of a `DecryptedOutput<sapling::Note>` and will decrypt Orchard
outputs when the `orchard` feature is enabled. In addition, the type
constraint on its `<A>` parameter has been strengthened to `Copy`.
- `zcash_client_backend::fees`:
- Arguments to `ChangeStrategy::compute_balance` have changed.
- `zcash_client_backend::zip321::render::amount_str` now takes a
Expand All @@ -69,6 +82,8 @@ and this library adheres to Rust's notion of
### Removed
- `zcash_client_backend::PoolType::is_receiver`: use
`zcash_keys::Address::has_receiver` instead.
- `zcash_client_backend::DecryptedTransaction::sapling_outputs` use
the `DecryptedTransaction::outputs` method instead.

### Fixed
- This release fixes an error in amount parsing in `zip321` that previously
Expand Down
82 changes: 73 additions & 9 deletions zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,13 +872,29 @@ impl<A> ScannedBlock<A> {
}

/// A transaction that was detected during scanning of the blockchain,
/// including its decrypted Sapling outputs.
/// including its decrypted Sapling and/or Orchard outputs.
///
/// The purpose of this struct is to permit atomic updates of the
/// wallet database when transactions are successfully decrypted.
pub struct DecryptedTransaction<'a, AccountId> {
pub tx: &'a Transaction,
pub sapling_outputs: &'a Vec<DecryptedOutput<sapling::Note, AccountId>>,
tx: &'a Transaction,
outputs: &'a Vec<DecryptedOutput<Note, AccountId>>,
}

impl<'a, AccountId> DecryptedTransaction<'a, AccountId> {
/// Constructs a new [`DecryptedTransaction`] from its constituent parts.
pub fn new(tx: &'a Transaction, outputs: &'a Vec<DecryptedOutput<Note, AccountId>>) -> Self {
Self { tx, outputs }
}

/// Returns the raw transaction data.
pub fn tx(&self) -> &Transaction {
self.tx
}
/// Returns the Sapling outputs that were decrypted from the transaction.
pub fn outputs(&self) -> &Vec<DecryptedOutput<Note, AccountId>> {
self.outputs
}
}

/// A transaction that was constructed and sent by the wallet.
Expand All @@ -887,13 +903,61 @@ pub struct DecryptedTransaction<'a, AccountId> {
/// wallet database when transactions are created and submitted
/// to the network.
pub struct SentTransaction<'a, AccountId> {
pub tx: &'a Transaction,
pub created: time::OffsetDateTime,
pub account: AccountId,
pub outputs: Vec<SentTransactionOutput<AccountId>>,
pub fee_amount: NonNegativeAmount,
tx: &'a Transaction,
created: time::OffsetDateTime,
account: AccountId,
outputs: Vec<SentTransactionOutput<AccountId>>,
fee_amount: NonNegativeAmount,
#[cfg(feature = "transparent-inputs")]
pub utxos_spent: Vec<OutPoint>,
utxos_spent: Vec<OutPoint>,
}

impl<'a, AccountId> SentTransaction<'a, AccountId> {
/// Constructs a new [`SentTransaction`] from its constituent parts.
pub fn new(
tx: &'a Transaction,
created: time::OffsetDateTime,
account: AccountId,
outputs: Vec<SentTransactionOutput<AccountId>>,
fee_amount: NonNegativeAmount,
#[cfg(feature = "transparent-inputs")] utxos_spent: Vec<OutPoint>,
) -> Self {
Self {
tx,
created,
account,
outputs,
fee_amount,
#[cfg(feature = "transparent-inputs")]
utxos_spent,
}
}

/// Returns the transaction that was sent.
pub fn tx(&self) -> &Transaction {
self.tx
}
/// Returns the timestamp of the transaction's creation.
pub fn created(&self) -> time::OffsetDateTime {
self.created
}
/// Returns the id for the account that created the outputs.
pub fn account_id(&self) -> &AccountId {
&self.account
}
/// Returns the outputs of the transaction.
pub fn outputs(&self) -> &[SentTransactionOutput<AccountId>] {
self.outputs.as_ref()
}
/// Returns the fee paid by the transaction.
pub fn fee_amount(&self) -> NonNegativeAmount {
self.fee_amount
}
/// Returns the list of UTXOs spent in the created transaction.
#[cfg(feature = "transparent-inputs")]
pub fn utxos_spent(&self) -> &[OutPoint] {
self.utxos_spent.as_ref()
}
}

/// An output of a transaction generated by the wallet.
Expand Down
6 changes: 3 additions & 3 deletions zcash_client_backend/src/data_api/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ where
.or_else(|| params.activation_height(NetworkUpgrade::Sapling))
.expect("Sapling activation height must be known.");

data.store_decrypted_tx(DecryptedTransaction {
data.store_decrypted_tx(DecryptedTransaction::new(
tx,
sapling_outputs: &decrypt_transaction(params, height, tx, &ufvks),
})?;
&decrypt_transaction(params, height, tx, &ufvks),
))?;

Ok(())
}
Expand Down
Loading

0 comments on commit 9de4f1f

Please sign in to comment.