Skip to content

Commit

Permalink
Merge pull request #1341 from Oscar-Pepper/zingo_sync_link_nullifiers
Browse files Browse the repository at this point in the history
Zingo sync link nullifiers
  • Loading branch information
zancas authored Nov 15, 2024
2 parents c8f0b16 + 7c8abdc commit 5fcc974
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 51 deletions.
38 changes: 27 additions & 11 deletions zingo-sync/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::BTreeMap;

use getset::{CopyGetters, Getters, MutGetters};
use getset::{CopyGetters, Getters, MutGetters, Setters};

use incrementalmerkletree::Position;
use zcash_client_backend::data_api::scanning::ScanRange;
Expand All @@ -20,14 +20,19 @@ use crate::{keys::KeyId, utils};
#[derive(Debug, Getters, MutGetters)]
#[getset(get = "pub", get_mut = "pub")]
pub struct SyncState {
/// A vec of block ranges with scan priorities from wallet birthday to chain tip.
/// In block height order with no overlaps or gaps.
scan_ranges: Vec<ScanRange>,
/// Block height and txid of known spends which are awaiting the scanning of the range it belongs to for transaction decryption.
spend_locations: Vec<(BlockHeight, TxId)>,
}

impl SyncState {
/// Create new SyncState
pub fn new() -> Self {
SyncState {
scan_ranges: Vec::new(),
spend_locations: Vec::new(),
}
}
}
Expand Down Expand Up @@ -56,7 +61,7 @@ impl OutputId {
}

/// Binary tree map of nullifiers from transaction spends or actions
#[derive(Debug, MutGetters)]
#[derive(Debug, Getters, MutGetters)]
#[getset(get = "pub", get_mut = "pub")]
pub struct NullifierMap {
sapling: BTreeMap<sapling_crypto::Nullifier, (BlockHeight, TxId)>,
Expand Down Expand Up @@ -119,11 +124,10 @@ impl WalletBlock {
}

/// Wallet transaction
#[derive(Debug, CopyGetters)]
#[getset(get_copy = "pub")]
#[derive(Debug, Getters, CopyGetters)]
pub struct WalletTransaction {
#[getset(get_copy = "pub")]
txid: TxId,
#[getset(get = "pub")]
transaction: zcash_primitives::transaction::Transaction,
#[getset(get_copy = "pub")]
block_height: BlockHeight,
#[getset(skip)]
Expand All @@ -138,15 +142,15 @@ pub struct WalletTransaction {

impl WalletTransaction {
pub fn from_parts(
txid: TxId,
transaction: zcash_primitives::transaction::Transaction,
block_height: BlockHeight,
sapling_notes: Vec<SaplingNote>,
orchard_notes: Vec<OrchardNote>,
outgoing_sapling_notes: Vec<OutgoingSaplingNote>,
outgoing_orchard_notes: Vec<OutgoingOrchardNote>,
) -> Self {
Self {
txid,
transaction,
block_height,
sapling_notes,
orchard_notes,
Expand All @@ -159,10 +163,18 @@ impl WalletTransaction {
&self.sapling_notes
}

pub fn sapling_notes_mut(&mut self) -> Vec<&mut SaplingNote> {
self.sapling_notes.iter_mut().collect()
}

pub fn orchard_notes(&self) -> &[OrchardNote] {
&self.orchard_notes
}

pub fn orchard_notes_mut(&mut self) -> Vec<&mut OrchardNote> {
self.orchard_notes.iter_mut().collect()
}

pub fn outgoing_sapling_notes(&self) -> &[OutgoingSaplingNote] {
&self.outgoing_sapling_notes
}
Expand All @@ -176,7 +188,7 @@ pub type SaplingNote = WalletNote<sapling_crypto::Note, sapling_crypto::Nullifie
pub type OrchardNote = WalletNote<orchard::Note, orchard::note::Nullifier>;

/// Wallet note, shielded output with metadata relevant to the wallet
#[derive(Debug, Getters, CopyGetters)]
#[derive(Debug, Getters, CopyGetters, Setters)]
pub struct WalletNote<N, Nf: Copy> {
/// Output ID
#[getset(get_copy = "pub")]
Expand All @@ -196,6 +208,8 @@ pub struct WalletNote<N, Nf: Copy> {
/// Memo
#[getset(get = "pub")]
memo: Memo,
#[getset(get = "pub", set = "pub")]
spending_transaction: Option<TxId>,
}

impl<N, Nf: Copy> WalletNote<N, Nf> {
Expand All @@ -206,6 +220,7 @@ impl<N, Nf: Copy> WalletNote<N, Nf> {
nullifier: Option<Nf>,
position: Position,
memo: Memo,
spending_transaction: Option<TxId>,
) -> Self {
Self {
output_id,
Expand All @@ -214,6 +229,7 @@ impl<N, Nf: Copy> WalletNote<N, Nf> {
nullifier,
position,
memo,
spending_transaction,
}
}
}
Expand All @@ -222,7 +238,7 @@ pub type OutgoingSaplingNote = OutgoingNote<sapling_crypto::Note>;
pub type OutgoingOrchardNote = OutgoingNote<orchard::Note>;

/// Note sent from this capability to a recipient
#[derive(Debug, Clone, Getters, CopyGetters, MutGetters)]
#[derive(Debug, Clone, Getters, CopyGetters, Setters)]
pub struct OutgoingNote<N> {
/// Output ID
#[getset(get_copy = "pub")]
Expand All @@ -237,7 +253,7 @@ pub struct OutgoingNote<N> {
#[getset(get = "pub")]
memo: Memo,
/// Recipient's full unified address from encoded memo
#[getset(get = "pub", get_mut = "pub")]
#[getset(get = "pub", set = "pub")]
recipient_ua: Option<UnifiedAddress>,
}

Expand Down
6 changes: 3 additions & 3 deletions zingo-sync/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use self::{compact_blocks::scan_compact_blocks, transactions::scan_transactions}

mod compact_blocks;
pub(crate) mod task;
mod transactions;
pub(crate) mod transactions;

struct InitialScanData {
previous_block: Option<WalletBlock>,
Expand Down Expand Up @@ -121,13 +121,13 @@ pub(crate) struct ScanResults {
pub(crate) shard_tree_data: ShardTreeData,
}

struct DecryptedNoteData {
pub(crate) struct DecryptedNoteData {
sapling_nullifiers_and_positions: HashMap<OutputId, (sapling_crypto::Nullifier, Position)>,
orchard_nullifiers_and_positions: HashMap<OutputId, (orchard::note::Nullifier, Position)>,
}

impl DecryptedNoteData {
fn new() -> Self {
pub(crate) fn new() -> Self {
DecryptedNoteData {
sapling_nullifiers_and_positions: HashMap::new(),
orchard_nullifiers_and_positions: HashMap::new(),
Expand Down
7 changes: 5 additions & 2 deletions zingo-sync/src/scan/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn scan_transaction<P: Parameters>(
}
}
Ok(WalletTransaction::from_parts(
transaction.txid(),
transaction,
block_height,
sapling_notes,
orchard_notes,
Expand Down Expand Up @@ -257,6 +257,7 @@ where
Some(*nullifier),
*position,
Memo::from_bytes(memo_bytes.as_ref()).unwrap(),
None,
));
}
}
Expand Down Expand Up @@ -359,6 +360,8 @@ fn add_recipient_unified_address<P, Nz>(
outgoing_notes
.iter_mut()
.filter(|note| ua_receivers.contains(&note.encoded_recipient(parameters)))
.for_each(|note| *note.recipient_ua_mut() = Some(ua.clone()))
.for_each(|note| {
note.set_recipient_ua(Some(ua.clone()));
});
}
}
Loading

0 comments on commit 5fcc974

Please sign in to comment.