Skip to content

Commit

Permalink
fix review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Nov 7, 2023
1 parent faed2b8 commit 8c97f56
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 163 deletions.
6 changes: 2 additions & 4 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
155 changes: 3 additions & 152 deletions zcash_client_sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,27 +544,12 @@ fn address_from_extfvk<P: consensus::Parameters>(

#[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;

Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions zcash_client_sqlite/src/wallet/transact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -610,7 +611,7 @@ mod tests {
.query_row(
"SELECT raw FROM transactions
WHERE id_tx = ?",
[tx_row],
&[tx_row],

Check failure on line 614 in zcash_client_sqlite/src/wallet/transact.rs

View workflow job for this annotation

GitHub Actions / Clippy (nightly)

the trait bound `&[i64; 1]: rusqlite::Params` is not satisfied

error[E0277]: the trait bound `&[i64; 1]: rusqlite::Params` is not satisfied --> zcash_client_sqlite/src/wallet/transact.rs:614:21 | 611 | .query_row( | --------- required by a bound introduced by this call ... 614 | &[tx_row], | ^^^^^^^^^ the trait `rusqlite::Params` is not implemented for `&[i64; 1]` | note: required by a bound in `rusqlite::Connection::query_row` --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rusqlite-0.28.0/src/lib.rs:666:12 | 664 | pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> Result<T> | --------- required by a bound in this associated function 665 | where 666 | P: Params, | ^^^^^^ required by this bound in `Connection::query_row` help: consider removing the leading `&`-reference | 614 - &[tx_row], 614 + [tx_row], |
|row| row.get(0),
)
.unwrap();
Expand All @@ -623,7 +624,7 @@ mod tests {
.query_row(
"SELECT output_index FROM sent_notes
WHERE tx = ?",
[tx_row],
&[tx_row],

Check failure on line 627 in zcash_client_sqlite/src/wallet/transact.rs

View workflow job for this annotation

GitHub Actions / Clippy (nightly)

the trait bound `&[i64; 1]: rusqlite::Params` is not satisfied

error[E0277]: the trait bound `&[i64; 1]: rusqlite::Params` is not satisfied --> zcash_client_sqlite/src/wallet/transact.rs:627:21 | 624 | .query_row( | --------- required by a bound introduced by this call ... 627 | &[tx_row], | ^^^^^^^^^ the trait `rusqlite::Params` is not implemented for `&[i64; 1]` | note: required by a bound in `rusqlite::Connection::query_row` --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rusqlite-0.28.0/src/lib.rs:666:12 | 664 | pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> Result<T> | --------- required by a bound in this associated function 665 | where 666 | P: Params, | ^^^^^^ required by this bound in `Connection::query_row` help: consider removing the leading `&`-reference | 627 - &[tx_row], 627 + [tx_row], |
|row| row.get(0),
)
.unwrap();
Expand Down
6 changes: 2 additions & 4 deletions zcash_extras/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
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},
};

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},
};

Expand Down

0 comments on commit 8c97f56

Please sign in to comment.