Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove getset from zingo-sync #1573

Closed
wants to merge 15 commits into from
Closed
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libtonode-tests/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn sync_mainnet_test() {
.unwrap();

dbg!(lightclient.wallet.wallet_blocks());
dbg!(lightclient.wallet.nullifier_map());
dbg!(lightclient.wallet.sync_state());
//dbg!(lightclient.wallet.nullifier_map);
//dbg!(lightclient.wallet.sync_state);
}

#[tokio::test]
Expand Down
3 changes: 0 additions & 3 deletions zingo-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ memuse.workspace = true
crossbeam-channel.workspace = true
rayon.workspace = true

# Minimise boilerplate
getset.workspace = true

# Error handling
thiserror.workspace = true

Expand Down
7 changes: 2 additions & 5 deletions zingo-sync/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::collections::HashMap;

use getset::Getters;
use incrementalmerkletree::Position;
use orchard::{
keys::{FullViewingKey, IncomingViewingKey, Scope},
Expand Down Expand Up @@ -126,11 +125,9 @@ impl ScanningKeyOps<OrchardDomain, orchard::note::Nullifier>
}

/// A set of keys to be used in scanning for decryptable transaction outputs.
#[derive(Getters)]
#[getset(get = "pub(crate)")]
pub(crate) struct ScanningKeys {
sapling: HashMap<KeyId, ScanningKey<SaplingIvk, NullifierDerivingKey>>,
orchard: HashMap<KeyId, ScanningKey<IncomingViewingKey, FullViewingKey>>,
pub(crate) sapling: HashMap<KeyId, ScanningKey<SaplingIvk, NullifierDerivingKey>>,
pub(crate) orchard: HashMap<KeyId, ScanningKey<IncomingViewingKey, FullViewingKey>>,
}

impl ScanningKeys {
Expand Down
89 changes: 36 additions & 53 deletions zingo-sync/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use std::collections::BTreeMap;

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

use incrementalmerkletree::Position;
use zcash_client_backend::data_api::scanning::ScanRange;
use zcash_keys::{address::UnifiedAddress, encoding::encode_payment_address};
Expand All @@ -17,14 +15,13 @@ use zcash_primitives::{
use crate::{keys::KeyId, utils};

/// Encapsulates the current state of sync
#[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>,
pub(crate) 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)>,
#[allow(dead_code)]
pub(crate) spend_locations: Vec<(BlockHeight, TxId)>,
}

impl SyncState {
Expand All @@ -37,7 +34,7 @@ impl SyncState {
}

pub fn fully_scanned(&self) -> bool {
self.scan_ranges().iter().all(|scan_range| {
self.scan_ranges.iter().all(|scan_range| {
matches!(
scan_range.priority(),
zcash_client_backend::data_api::scanning::ScanPriority::Scanned
Expand All @@ -53,13 +50,12 @@ impl Default for SyncState {
}

/// Output ID for a given pool type
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, CopyGetters)]
#[getset(get_copy = "pub")]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub struct OutputId {
/// ID of associated transaction
txid: TxId,
pub(crate) txid: TxId,
/// Index of output within the transactions bundle of the given pool type.
output_index: usize,
pub(crate) output_index: usize,
}

impl OutputId {
Expand All @@ -70,11 +66,9 @@ impl OutputId {
}

/// Binary tree map of nullifiers from transaction spends or actions
#[derive(Debug, Getters, MutGetters)]
#[getset(get = "pub", get_mut = "pub")]
pub struct NullifierMap {
sapling: BTreeMap<sapling_crypto::Nullifier, (BlockHeight, TxId)>,
orchard: BTreeMap<orchard::note::Nullifier, (BlockHeight, TxId)>,
pub(crate) sapling: BTreeMap<sapling_crypto::Nullifier, (BlockHeight, TxId)>,
pub(crate) orchard: BTreeMap<orchard::note::Nullifier, (BlockHeight, TxId)>,
}

impl NullifierMap {
Expand All @@ -93,17 +87,17 @@ impl Default for NullifierMap {
}

/// Wallet block data
#[derive(Debug, Clone, CopyGetters)]
#[getset(get_copy = "pub")]
#[derive(Clone, Debug)]
pub struct WalletBlock {
block_height: BlockHeight,
block_hash: BlockHash,
pub(crate) block_height: BlockHeight,
pub(crate) block_hash: BlockHash,
#[allow(dead_code)]
prev_hash: BlockHash,
#[allow(dead_code)]
time: u32,
#[getset(skip)]
txids: Vec<TxId>,
sapling_commitment_tree_size: u32,
orchard_commitment_tree_size: u32,
pub(crate) sapling_commitment_tree_size: u32,
pub(crate) orchard_commitment_tree_size: u32,
}

impl WalletBlock {
Expand Down Expand Up @@ -133,19 +127,13 @@ impl WalletBlock {
}

/// Wallet transaction
#[derive(Debug, Getters, CopyGetters)]
#[derive(Debug)]
pub struct WalletTransaction {
#[getset(get = "pub")]
transaction: zcash_primitives::transaction::Transaction,
#[getset(get_copy = "pub")]
block_height: BlockHeight,
#[getset(skip)]
sapling_notes: Vec<SaplingNote>,
#[getset(skip)]
orchard_notes: Vec<OrchardNote>,
#[getset(skip)]
pub(crate) transaction: zcash_primitives::transaction::Transaction,
pub(crate) block_height: BlockHeight,
pub(crate) sapling_notes: Vec<SaplingNote>,
pub(crate) orchard_notes: Vec<OrchardNote>,
outgoing_sapling_notes: Vec<OutgoingSaplingNote>,
#[getset(skip)]
outgoing_orchard_notes: Vec<OutgoingOrchardNote>,
}

Expand Down Expand Up @@ -197,28 +185,25 @@ 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, Setters)]
#[derive(Debug)]
pub struct WalletNote<N, Nf: Copy> {
/// Output ID
#[getset(get_copy = "pub")]
#[allow(dead_code)]
output_id: OutputId,
/// Identifier for key used to decrypt output
#[getset(get_copy = "pub")]
#[allow(dead_code)]
key_id: KeyId,
/// Decrypted note with recipient and value
#[getset(get = "pub")]
#[allow(dead_code)]
note: N,
/// Derived nullifier
#[getset(get_copy = "pub")]
nullifier: Option<Nf>, //TODO: syncing without nullfiier deriving key
pub(crate) nullifier: Option<Nf>, //TODO: syncing without nullfiier deriving key
/// Commitment tree leaf position
#[getset(get_copy = "pub")]
#[allow(dead_code)]
position: Position,
/// Memo
#[getset(get = "pub")]
memo: Memo,
#[getset(get = "pub", set = "pub")]
spending_transaction: Option<TxId>,
pub(crate) memo: Memo,
pub(crate) spending_transaction: Option<TxId>,
}

impl<N, Nf: Copy> WalletNote<N, Nf> {
Expand Down Expand Up @@ -247,23 +232,21 @@ 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, Setters)]
#[derive(Debug, Clone)]
pub struct OutgoingNote<N> {
/// Output ID
#[getset(get_copy = "pub")]
#[allow(dead_code)]
output_id: OutputId,
/// Identifier for key used to decrypt output
#[getset(get_copy = "pub")]
#[allow(dead_code)]
key_id: KeyId,
/// Decrypted note with recipient and value
#[getset(get = "pub")]
note: N,
/// Memo
#[getset(get = "pub")]
#[allow(dead_code)]
memo: Memo,
/// Recipient's full unified address from encoded memo
#[getset(get = "pub", set = "pub")]
recipient_ua: Option<UnifiedAddress>,
pub(crate) recipient_ua: Option<UnifiedAddress>,
}

impl<N> OutgoingNote<N> {
Expand Down Expand Up @@ -291,7 +274,7 @@ impl SyncOutgoingNotes for OutgoingNote<sapling_crypto::Note> {
{
encode_payment_address(
parameters.hrp_sapling_payment_address(),
&self.note().recipient(),
&self.note.recipient(),
)
}
}
Expand All @@ -301,7 +284,7 @@ impl SyncOutgoingNotes for OutgoingNote<orchard::Note> {
where
P: Parameters + NetworkConstants,
{
utils::encode_orchard_receiver(parameters, &self.note().recipient()).unwrap()
utils::encode_orchard_receiver(parameters, &self.note.recipient()).unwrap()
}
}

Expand Down
4 changes: 2 additions & 2 deletions zingo-sync/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl InitialScanData {
let (sapling_initial_tree_size, orchard_initial_tree_size) =
if let Some(prev) = &previous_wallet_block {
(
prev.sapling_commitment_tree_size(),
prev.orchard_commitment_tree_size(),
prev.sapling_commitment_tree_size,
prev.orchard_commitment_tree_size,
)
} else if let Some(chain_metadata) = &first_block.chain_metadata {
// calculate initial tree size by subtracting number of outputs in block from the blocks final tree size
Expand Down
30 changes: 14 additions & 16 deletions zingo-sync/src/scan/compact_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ where
// the edge case of transactions that this capability created but did not receive change
// or create outgoing data is handled when the nullifiers are added and linked
incoming_sapling_outputs.iter().for_each(|(output_id, _)| {
relevant_txids.insert(output_id.txid());
relevant_txids.insert(output_id.txid);
});
incoming_orchard_outputs.iter().for_each(|(output_id, _)| {
relevant_txids.insert(output_id.txid());
relevant_txids.insert(output_id.txid);
});
// TODO: add outgoing outputs to relevant txids

Expand All @@ -99,13 +99,13 @@ where

calculate_nullifiers_and_positions(
sapling_tree_size,
scanning_keys.sapling(),
&scanning_keys.sapling,
&incoming_sapling_outputs,
&mut decrypted_note_data.sapling_nullifiers_and_positions,
);
calculate_nullifiers_and_positions(
orchard_tree_size,
scanning_keys.orchard(),
&scanning_keys.orchard,
&incoming_orchard_outputs,
&mut decrypted_note_data.orchard_nullifiers_and_positions,
);
Expand All @@ -128,7 +128,7 @@ where

check_tree_size(block, &wallet_block).unwrap();

wallet_blocks.insert(wallet_block.block_height(), wallet_block);
wallet_blocks.insert(wallet_block.block_height, wallet_block);
}
// TODO: map nullifiers

Expand Down Expand Up @@ -170,8 +170,8 @@ fn check_continuity(
let mut prev_hash: Option<BlockHash> = None;

if let Some(prev) = previous_compact_block {
prev_height = Some(prev.block_height());
prev_hash = Some(prev.block_hash());
prev_height = Some(prev.block_height);
prev_hash = Some(prev.block_hash);
}

for block in compact_blocks {
Expand Down Expand Up @@ -203,13 +203,11 @@ fn check_continuity(

fn check_tree_size(compact_block: &CompactBlock, wallet_block: &WalletBlock) -> Result<(), ()> {
if let Some(chain_metadata) = &compact_block.chain_metadata {
if chain_metadata.sapling_commitment_tree_size
!= wallet_block.sapling_commitment_tree_size()
if chain_metadata.sapling_commitment_tree_size != wallet_block.sapling_commitment_tree_size
{
panic!("sapling tree size is incorrect!")
}
if chain_metadata.orchard_commitment_tree_size
!= wallet_block.orchard_commitment_tree_size()
if chain_metadata.orchard_commitment_tree_size != wallet_block.orchard_commitment_tree_size
{
panic!("orchard tree size is incorrect!")
}
Expand All @@ -234,7 +232,7 @@ fn calculate_nullifiers_and_positions<D, K, Nf>(
.iter()
.for_each(|(output_id, incoming_output)| {
let position = Position::from(u64::from(
tree_size + u32::try_from(output_id.output_index()).unwrap(),
tree_size + u32::try_from(output_id.output_index).unwrap(),
));
let key = keys
.get(&incoming_output.ivk_tag)
Expand All @@ -257,7 +255,7 @@ fn calculate_sapling_leaves_and_retentions<D: Domain>(
let incoming_output_indices: Vec<usize> = incoming_decrypted_outputs
.keys()
.copied()
.map(|output_id| output_id.output_index())
.map(|output_id| output_id.output_index)
.collect();

if outputs.is_empty() {
Expand Down Expand Up @@ -305,7 +303,7 @@ fn calculate_orchard_leaves_and_retentions<D: Domain>(
let incoming_output_indices: Vec<usize> = incoming_decrypted_outputs
.keys()
.copied()
.map(|output_id| output_id.output_index())
.map(|output_id| output_id.output_index)
.collect();

if actions.is_empty() {
Expand Down Expand Up @@ -356,7 +354,7 @@ fn collect_nullifiers(
.map(|spend| sapling_crypto::Nullifier::from_slice(spend.nf.as_slice()).unwrap())
.for_each(|nullifier| {
nullifier_map
.sapling_mut()
.sapling
.insert(nullifier, (block_height, transaction.txid()));
});
transaction
Expand All @@ -368,7 +366,7 @@ fn collect_nullifiers(
})
.for_each(|nullifier| {
nullifier_map
.orchard_mut()
.orchard
.insert(nullifier, (block_height, transaction.txid()));
});
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions zingo-sync/src/scan/compact_blocks/runners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ where
sapling: BatchRunner::new(
batch_size_threshold,
scanning_keys
.sapling()
.sapling
.iter()
.map(|(id, key)| (*id, key.prepare())),
),
orchard: BatchRunner::new(
batch_size_threshold,
scanning_keys
.orchard()
.orchard
.iter()
.map(|(id, key)| (*id, key.prepare())),
),
Expand Down
2 changes: 1 addition & 1 deletion zingo-sync/src/scan/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ where
if !wallet
.get_sync_state()
.unwrap()
.scan_ranges()
.scan_ranges
.iter()
.any(|scan_range| scan_range.priority() == ScanPriority::Verify)
{
Expand Down
Loading