Skip to content

Commit

Permalink
WIP: add Orchard output generation to fake_compact_block
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Mar 7, 2024
1 parent 63b7c6b commit 380ad19
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 44 deletions.
112 changes: 71 additions & 41 deletions zcash_client_sqlite/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::num::NonZeroU32;
#[cfg(feature = "unstable")]
use std::fs::File;

use nonempty::NonEmpty;
use nonempty::{NonEmpty, nonempty};

Check failure on line 8 in zcash_client_sqlite/src/testing.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `nonempty`

error: unused import: `nonempty` --> zcash_client_sqlite/src/testing.rs:8:26 | 8 | use nonempty::{NonEmpty, nonempty}; | ^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings`
use prost::Message;
use rand_core::{OsRng, RngCore};
use rusqlite::{params, Connection};
Expand All @@ -22,6 +22,7 @@ use sapling::{
zip32::DiversifiableFullViewingKey,
Note, Nullifier, PaymentAddress,
};
use zcash_client_backend::keys::UnifiedAddressRequest;

Check failure on line 25 in zcash_client_sqlite/src/testing.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `zcash_client_backend::keys::UnifiedAddressRequest`

error: unused import: `zcash_client_backend::keys::UnifiedAddressRequest` --> zcash_client_sqlite/src/testing.rs:25:5 | 25 | use zcash_client_backend::keys::UnifiedAddressRequest; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#[allow(deprecated)]
use zcash_client_backend::{
address::Address,
Expand All @@ -35,7 +36,8 @@ use zcash_client_backend::{
},
AccountBalance, AccountBirthday, WalletRead, WalletSummary, WalletWrite,
},
keys::UnifiedSpendingKey,
fees::{standard, DustOutputPolicy},
ShieldedProtocol,
proposal::Proposal,
proto::compact_formats::{
self as compact, CompactBlock, CompactSaplingOutput, CompactSaplingSpend, CompactTx,
Expand All @@ -44,10 +46,7 @@ use zcash_client_backend::{
wallet::OvkPolicy,
zip321,
};
use zcash_client_backend::{
fees::{standard, DustOutputPolicy},
ShieldedProtocol,
};
use zcash_keys::keys::{UnifiedSpendingKey, UnifiedFullViewingKey};
use zcash_note_encryption::Domain;
use zcash_primitives::{
block::BlockHash,
Expand Down Expand Up @@ -219,8 +218,8 @@ where
&mut self,
height: BlockHeight,
prev_hash: BlockHash,
dfvk: &DiversifiableFullViewingKey,
req: AddressType,
dfvk: &UnifiedFullViewingKey,
req: AddressRequest,
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
) -> (Cache::InsertResult, Nullifier) {
Expand Down Expand Up @@ -765,48 +764,79 @@ pub(crate) enum AddressType {
Internal,
}

/// Create a fake CompactBlock at the given height, containing a single output paying
/// an address. Returns the CompactBlock and the nullifier for the new note.
pub(crate) struct AddressRequest {
protocol: ShieldedProtocol,
address_type: AddressType,
}

impl AddressRequest {
fn default_external(protocol: ShieldedProtocol) -> Self {
Self { protocol, address_type: AddressType::DefaultExternal }
}

fn diversified_external(protocol: ShieldedProtocol, index: DiversifierIndex) -> Self {
Self { protocol, address_type: AddressType::DiversifiedExternal(index) }
}

fn internal(protocol: ShieldedProtocol) -> Self {
Self { protocol, address_type: AddressType::Internal }
}
}

/// Create a fake CompactBlock at the given height, containing a single output paying to an address
/// derived from the provided UFVK. Returns the CompactBlock and the nullifier for the new note.
pub(crate) fn fake_compact_block<P: consensus::Parameters>(
params: &P,
height: BlockHeight,
prev_hash: BlockHash,
dfvk: &DiversifiableFullViewingKey,
req: AddressType,
value: NonNegativeAmount,
ufvk: &UnifiedFullViewingKey,
note_recipient: AddressRequest,
note_value: NonNegativeAmount,
initial_sapling_tree_size: u32,
) -> (CompactBlock, Nullifier) {
let to = match req {
AddressType::DefaultExternal => dfvk.default_address().1,
AddressType::DiversifiedExternal(idx) => dfvk.find_address(idx).unwrap().1,
AddressType::Internal => dfvk.change_address().1,
};

// Create a fake Note for the account
initial_orchard_tree_size: u32,
) -> (CompactBlock, Either<sapling::Nullifier, orchard::note::Nullifier>) {

Check failure on line 797 in zcash_client_sqlite/src/testing.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

cannot find type `Either` in this scope

error[E0412]: cannot find type `Either` in this scope --> zcash_client_sqlite/src/testing.rs:797:21 | 797 | ) -> (CompactBlock, Either<sapling::Nullifier, orchard::note::Nullifier>) { | ^^^^^^ not found in this scope | help: consider importing this enum | 1 | use maybe_rayon::iter::Either; |

Check failure on line 797 in zcash_client_sqlite/src/testing.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

failed to resolve: use of undeclared crate or module `orchard`

error[E0433]: failed to resolve: use of undeclared crate or module `orchard` --> zcash_client_sqlite/src/testing.rs:797:48 | 797 | ) -> (CompactBlock, Either<sapling::Nullifier, orchard::note::Nullifier>) { | ^^^^^^^ use of undeclared crate or module `orchard`
let mut rng = OsRng;
let rseed = generate_random_rseed(zip212_enforcement(params, height), &mut rng);
let note = Note::from_parts(to, NoteValue::from_raw(value.into_u64()), rseed);
let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk),
note.clone(),
*MemoBytes::empty().as_array(),
&mut rng,
);
let cmu = note.cmu().to_bytes().to_vec();
let ephemeral_key = SaplingDomain::epk_bytes(encryptor.epk()).0.to_vec();
let enc_ciphertext = encryptor.encrypt_note_plaintext();

// Create a fake CompactBlock containing the note
let cout = CompactSaplingOutput {
cmu,
ephemeral_key,
ciphertext: enc_ciphertext.as_ref()[..52].to_vec(),
};
let mut ctx = CompactTx::default();
let mut txid = vec![0; 32];
rng.fill_bytes(&mut txid);
ctx.hash = txid;
ctx.outputs.push(cout);
match note_recipient.protocol {
ShieldedProtocol::Sapling => {
let dfvk = ufvk.sapling().unwrap();
let to = match note_recipient.address_type {
AddressType::DefaultExternal => dfvk.default_address().1,
AddressType::DiversifiedExternal(idx) => dfvk.find_address(idx).unwrap().1,
AddressType::Internal => dfvk.change_address().1,
};
let rseed = generate_random_rseed(zip212_enforcement(params, height), &mut rng);

// Create a fake Note
let note = Note::from_parts(to, NoteValue::from_raw(value.into_u64()), rseed);

Check failure on line 815 in zcash_client_sqlite/src/testing.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

cannot find value `value` in this scope

error[E0425]: cannot find value `value` in this scope --> zcash_client_sqlite/src/testing.rs:815:65 | 815 | let note = Note::from_parts(to, NoteValue::from_raw(value.into_u64()), rseed); | ^^^^^ not found in this scope
let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk),
note.clone(),
*MemoBytes::empty().as_array(),
&mut rng,
);
let cmu = note.cmu().to_bytes().to_vec();
let ephemeral_key = SaplingDomain::epk_bytes(encryptor.epk()).0.to_vec();
let enc_ciphertext = encryptor.encrypt_note_plaintext();

// Create a fake CompactBlock containing the note
let cout = CompactSaplingOutput {
cmu,
ephemeral_key,
ciphertext: enc_ciphertext.as_ref()[..52].to_vec(),
};

ctx.outputs.push(cout);
}
ShieldedProtocol::Orchard => {

}
}

let mut cb = CompactBlock {
hash: {
let mut hash = vec![0; 32];
Expand Down Expand Up @@ -875,8 +905,8 @@ pub(crate) fn fake_compact_block_spending<P: consensus::Parameters>(
height: BlockHeight,
prev_hash: BlockHash,
(nf, in_value): (Nullifier, NonNegativeAmount),
dfvk: &DiversifiableFullViewingKey,
to: PaymentAddress,
ufvk: &UnifiedFullViewingKey,
to: Address,
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
) -> CompactBlock {
Expand Down
3 changes: 1 addition & 2 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,8 +1691,7 @@ pub(crate) mod tests {
);
}

#[test]
fn birthday_in_anchor_shard() {
fn birthday_in_anchor_shard(protocol: ShieldedProtocol) {
let (mut st, dfvk, birthday, _) = test_with_canopy_birthday();

// Set up the following situation:
Expand Down
18 changes: 17 additions & 1 deletion zcash_keys/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Helper functions for managing light client key material.
use std::{error, fmt};
use nonempty::NonEmpty;

use zcash_address::unified::{self, Container, Encoding, Typecode};
use zcash_protocol::consensus::{self, NetworkConstants};
use zcash_protocol::{consensus::{self, NetworkConstants}, ShieldedProtocol};
use zip32::{AccountId, DiversifierIndex};

use crate::address::UnifiedAddress;
Expand Down Expand Up @@ -518,6 +519,21 @@ impl UnifiedAddressRequest {
}
}

/// Constructs a new request for a Unified address having the specified set of shielded receivers.
pub fn shielded(protocols: NonEmpty<ShieldedProtocol>) -> Self {
let mut has_orchard = false;
let mut has_sapling = false;
for protocol in protocols {
match protocol {
ShieldedProtocol::Sapling => { has_sapling = true; }
ShieldedProtocol::Orchard => { has_orchard = true; }
}
}
Self {
has_orchard, has_sapling, has_p2pkh: false
}
}

/// Constructs a new unified address request that includes a request for a receiver of each
/// type that is supported given the active feature flags.
pub fn all() -> Option<Self> {
Expand Down

0 comments on commit 380ad19

Please sign in to comment.