From 8c97f561d233a2c5b5e3da4b6f29bd05be052166 Mon Sep 17 00:00:00 2001 From: borngraced Date: Tue, 7 Nov 2023 12:28:04 +0100 Subject: [PATCH] fix review notes --- zcash_client_sqlite/src/chain.rs | 6 +- zcash_client_sqlite/src/lib.rs | 155 +-------------------- zcash_client_sqlite/src/wallet/transact.rs | 7 +- zcash_extras/src/wallet.rs | 6 +- 4 files changed, 11 insertions(+), 163 deletions(-) diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index b416ebe889..f3f88c33a1 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -79,14 +79,12 @@ mod tests { chain::{scan_cached_blocks, validate_chain}, error::{ChainInvalid, Error}, }; + use zcash_extras::{fake_compact_block, fake_compact_block_spending}; use crate::{ chain::init::init_cache_database, error::SqliteClientError, - tests::{ - self, fake_compact_block, fake_compact_block_spending, insert_into_cache, - sapling_activation_height, - }, + tests::{self, insert_into_cache, sapling_activation_height}, wallet::{ get_balance, init::{init_accounts_table, init_wallet_db}, diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 91561417eb..97064a64cc 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -544,27 +544,12 @@ fn address_from_extfvk( #[cfg(test)] mod tests { - use ff::PrimeField; - use group::GroupEncoding; use protobuf::Message; - use rand_core::{OsRng, RngCore}; use rusqlite::params; - use zcash_client_backend::proto::compact_formats::{ - CompactBlock, CompactOutput, CompactSpend, CompactTx, - }; - - use zcash_primitives::{ - block::BlockHash, - consensus::{BlockHeight, Network, NetworkUpgrade, Parameters}, - memo::MemoBytes, - sapling::{ - note_encryption::sapling_note_encryption, util::generate_random_rseed, Note, Nullifier, - PaymentAddress, - }, - transaction::components::Amount, - zip32::ExtendedFullViewingKey, - }; + use zcash_client_backend::proto::compact_formats::CompactBlock; + + use zcash_primitives::consensus::{BlockHeight, Network, NetworkUpgrade, Parameters}; use super::BlockDb; @@ -592,140 +577,6 @@ mod tests { .unwrap() } - /// Create a fake CompactBlock at the given height, containing a single output paying - /// the given address. Returns the CompactBlock and the nullifier for the new note. - pub fn fake_compact_block( - height: BlockHeight, - prev_hash: BlockHash, - extfvk: ExtendedFullViewingKey, - value: Amount, - ) -> (CompactBlock, Nullifier) { - let to = extfvk.default_address().unwrap().1; - - // Create a fake Note for the account - let mut rng = OsRng; - let rseed = generate_random_rseed(&network(), height, &mut rng); - let note = Note { - g_d: to.diversifier().g_d().unwrap(), - pk_d: *to.pk_d(), - value: value.into(), - rseed, - }; - let encryptor = sapling_note_encryption::<_, Network>( - Some(extfvk.fvk.ovk), - note.clone(), - to, - MemoBytes::empty(), - &mut rng, - ); - let cmu = note.cmu().to_repr().as_ref().to_vec(); - let epk = encryptor.epk().to_bytes().to_vec(); - let enc_ciphertext = encryptor.encrypt_note_plaintext(); - - // Create a fake CompactBlock containing the note - let mut cout = CompactOutput::new(); - cout.set_cmu(cmu); - cout.set_epk(epk); - cout.set_ciphertext(enc_ciphertext.as_ref()[..52].to_vec()); - let mut ctx = CompactTx::new(); - let mut txid = vec![0; 32]; - rng.fill_bytes(&mut txid); - ctx.set_hash(txid); - ctx.outputs.push(cout); - let mut cb = CompactBlock::new(); - cb.set_height(u64::from(height)); - cb.hash.resize(32, 0); - rng.fill_bytes(&mut cb.hash); - cb.prevHash.extend_from_slice(&prev_hash.0); - cb.vtx.push(ctx); - (cb, note.nf(&extfvk.fvk.vk, 0)) - } - - /// Create a fake CompactBlock at the given height, spending a single note from the - /// given address. - pub fn fake_compact_block_spending( - height: BlockHeight, - prev_hash: BlockHash, - (nf, in_value): (Nullifier, Amount), - extfvk: ExtendedFullViewingKey, - to: PaymentAddress, - value: Amount, - ) -> CompactBlock { - let mut rng = OsRng; - let rseed = generate_random_rseed(&network(), height, &mut rng); - - // Create a fake CompactBlock containing the note - let mut cspend = CompactSpend::new(); - cspend.set_nf(nf.to_vec()); - let mut ctx = CompactTx::new(); - let mut txid = vec![0; 32]; - rng.fill_bytes(&mut txid); - ctx.set_hash(txid); - ctx.spends.push(cspend); - - // Create a fake Note for the payment - ctx.outputs.push({ - let note = Note { - g_d: to.diversifier().g_d().unwrap(), - pk_d: *to.pk_d(), - value: value.into(), - rseed, - }; - let encryptor = sapling_note_encryption::<_, Network>( - Some(extfvk.fvk.ovk), - note.clone(), - to, - MemoBytes::empty(), - &mut rng, - ); - let cmu = note.cmu().to_repr().as_ref().to_vec(); - let epk = encryptor.epk().to_bytes().to_vec(); - let enc_ciphertext = encryptor.encrypt_note_plaintext(); - - let mut cout = CompactOutput::new(); - cout.set_cmu(cmu); - cout.set_epk(epk); - cout.set_ciphertext(enc_ciphertext.as_ref()[..52].to_vec()); - cout - }); - - // Create a fake Note for the change - ctx.outputs.push({ - let change_addr = extfvk.default_address().unwrap().1; - let rseed = generate_random_rseed(&network(), height, &mut rng); - let note = Note { - g_d: change_addr.diversifier().g_d().unwrap(), - pk_d: *change_addr.pk_d(), - value: (in_value - value).into(), - rseed, - }; - let encryptor = sapling_note_encryption::<_, Network>( - Some(extfvk.fvk.ovk), - note.clone(), - change_addr, - MemoBytes::empty(), - &mut rng, - ); - let cmu = note.cmu().to_repr().as_ref().to_vec(); - let epk = encryptor.epk().to_bytes().to_vec(); - let enc_ciphertext = encryptor.encrypt_note_plaintext(); - - let mut cout = CompactOutput::new(); - cout.set_cmu(cmu); - cout.set_epk(epk); - cout.set_ciphertext(enc_ciphertext.as_ref()[..52].to_vec()); - cout - }); - - let mut cb = CompactBlock::new(); - cb.set_height(u64::from(height)); - cb.hash.resize(32, 0); - rng.fill_bytes(&mut cb.hash); - cb.prevHash.extend_from_slice(&prev_hash.0); - cb.vtx.push(ctx); - cb - } - /// Insert a fake CompactBlock into the cache DB. pub(crate) fn insert_into_cache(db_cache: &BlockDb, cb: &CompactBlock) { let cb_bytes = cb.write_to_bytes().unwrap(); diff --git a/zcash_client_sqlite/src/wallet/transact.rs b/zcash_client_sqlite/src/wallet/transact.rs index e76f5eec97..32a7cf3946 100644 --- a/zcash_client_sqlite/src/wallet/transact.rs +++ b/zcash_client_sqlite/src/wallet/transact.rs @@ -166,10 +166,11 @@ mod tests { data_api::{chain::scan_cached_blocks, wallet::create_spend_to_address, WalletRead}, wallet::OvkPolicy, }; + use zcash_extras::fake_compact_block; use crate::{ chain::init::init_cache_database, - tests::{self, fake_compact_block, insert_into_cache, sapling_activation_height}, + tests::{self, insert_into_cache, sapling_activation_height}, wallet::{ get_balance, get_balance_at, init::{init_accounts_table, init_blocks_table, init_wallet_db}, @@ -610,7 +611,7 @@ mod tests { .query_row( "SELECT raw FROM transactions WHERE id_tx = ?", - [tx_row], + &[tx_row], |row| row.get(0), ) .unwrap(); @@ -623,7 +624,7 @@ mod tests { .query_row( "SELECT output_index FROM sent_notes WHERE tx = ?", - [tx_row], + &[tx_row], |row| row.get(0), ) .unwrap(); diff --git a/zcash_extras/src/wallet.rs b/zcash_extras/src/wallet.rs index 63fbd72a7f..c097bd99ba 100644 --- a/zcash_extras/src/wallet.rs +++ b/zcash_extras/src/wallet.rs @@ -2,13 +2,12 @@ use std::fmt::{Debug, Display}; use zcash_primitives::{ - consensus::{self, BranchId, NetworkUpgrade}, + consensus::{self, BranchId}, memo::MemoBytes, sapling::prover::TxProver, transaction::{ builder::Builder, components::{amount::DEFAULT_FEE, Amount}, - Transaction, }, zip32::{ExtendedFullViewingKey, ExtendedSpendingKey}, }; @@ -16,8 +15,7 @@ use zcash_primitives::{ use crate::WalletWrite; use zcash_client_backend::{ address::RecipientAddress, - data_api::{error::Error, ReceivedTransaction, SentTransaction}, - decrypt_transaction, + data_api::{error::Error, SentTransaction}, wallet::{AccountId, OvkPolicy}, };