From a238007e14627caf2bb04c6e517f59df7680bfec Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Wed, 16 Aug 2023 11:15:10 -0600 Subject: [PATCH] zcash_client_sqlite: Remove `testing::network` global accessor function. --- zcash_client_sqlite/src/testing.rs | 112 ++++++++++-------- .../src/wallet/commitment_tree.rs | 8 +- zcash_client_sqlite/src/wallet/init.rs | 85 +++++++------ .../init/migrations/add_transaction_views.rs | 21 ++-- .../migrations/received_notes_nullable_nf.rs | 9 +- .../init/migrations/v_transactions_net.rs | 13 +- zcash_client_sqlite/src/wallet/sapling.rs | 14 +-- zcash_client_sqlite/src/wallet/scanning.rs | 17 +-- 8 files changed, 145 insertions(+), 134 deletions(-) diff --git a/zcash_client_sqlite/src/testing.rs b/zcash_client_sqlite/src/testing.rs index 84eb383ed..c1ee2ec2f 100644 --- a/zcash_client_sqlite/src/testing.rs +++ b/zcash_client_sqlite/src/testing.rs @@ -1,6 +1,6 @@ use std::convert::Infallible; use std::fmt; -use std::{collections::HashMap, num::NonZeroU32}; +use std::num::NonZeroU32; #[cfg(feature = "unstable")] use std::fs::File; @@ -15,14 +15,13 @@ use tempfile::NamedTempFile; use tempfile::TempDir; #[allow(deprecated)] -use zcash_client_backend::data_api::wallet::create_spend_to_address; use zcash_client_backend::{ address::RecipientAddress, data_api::{ self, chain::{scan_cached_blocks, BlockSource}, wallet::{ - create_proposed_transaction, + create_proposed_transaction, create_spend_to_address, input_selection::{GreedyInputSelectorError, InputSelector, Proposal}, propose_transfer, spend, }, @@ -37,7 +36,7 @@ use zcash_client_backend::{ use zcash_note_encryption::Domain; use zcash_primitives::{ block::BlockHash, - consensus::{BlockHeight, Network, NetworkUpgrade, Parameters}, + consensus::{self, BlockHeight, Network, NetworkUpgrade, Parameters}, legacy::TransparentAddress, memo::MemoBytes, sapling::{ @@ -54,13 +53,6 @@ use zcash_primitives::{ zip32::{sapling::DiversifiableFullViewingKey, DiversifierIndex}, }; -#[cfg(feature = "transparent-inputs")] -use zcash_client_backend::data_api::wallet::{propose_shielding, shield_transparent_funds}; -#[cfg(feature = "transparent-inputs")] -use zcash_primitives::{ - legacy, legacy::keys::IncomingViewingKey, transaction::components::amount::NonNegativeAmount, -}; - use crate::{ chain::init::init_cache_database, error::SqliteClientError, @@ -74,13 +66,24 @@ use crate::{ use super::BlockDb; +#[cfg(feature = "transparent-inputs")] +use { + zcash_client_backend::data_api::wallet::{propose_shielding, shield_transparent_funds}, + zcash_primitives::{ + legacy::{self, keys::IncomingViewingKey}, + transaction::components::amount::NonNegativeAmount, + }, +}; + #[cfg(feature = "unstable")] use crate::{ chain::{init::init_blockmeta_db, BlockMeta}, FsBlockDb, }; + /// A builder for a `zcash_client_sqlite` test. pub(crate) struct TestBuilder { + network: Network, cache: Cache, seed: Option>, with_test_account: bool, @@ -90,6 +93,7 @@ impl TestBuilder<()> { /// Constructs a new test. pub(crate) fn new() -> Self { TestBuilder { + network: Network::TestNetwork, cache: (), seed: None, with_test_account: false, @@ -99,6 +103,7 @@ impl TestBuilder<()> { /// Adds a [`BlockDb`] cache to the test. pub(crate) fn with_block_cache(self) -> TestBuilder { TestBuilder { + network: self.network, cache: BlockCache::new(), seed: self.seed, with_test_account: self.with_test_account, @@ -109,6 +114,7 @@ impl TestBuilder<()> { #[cfg(feature = "unstable")] pub(crate) fn with_fs_block_cache(self) -> TestBuilder { TestBuilder { + network: self.network, cache: FsBlockCache::new(), seed: self.seed, with_test_account: self.with_test_account, @@ -131,10 +137,8 @@ impl TestBuilder { /// Builds the state for this test. pub(crate) fn build(self) -> TestState { - let params = network(); - let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), params).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), self.network).unwrap(); init_wallet_db(&mut db_data, self.seed).unwrap(); let test_account = if self.with_test_account { @@ -145,7 +149,7 @@ impl TestBuilder { }; TestState { - params, + params: self.network, cache: self.cache, latest_cached_block: None, _data_file: data_file, @@ -186,7 +190,15 @@ where let (height, prev_hash, initial_sapling_tree_size) = self .latest_cached_block .map(|(prev_height, prev_hash, end_size)| (prev_height + 1, prev_hash, end_size)) - .unwrap_or_else(|| (sapling_activation_height(), BlockHash([0; 32]), 0)); + .unwrap_or_else(|| { + ( + self.params + .activation_height(NetworkUpgrade::Sapling) + .unwrap(), + BlockHash([0; 32]), + 0, + ) + }); let (res, nf) = self.generate_block_at( height, @@ -215,6 +227,7 @@ where initial_sapling_tree_size: u32, ) -> (Cache::InsertResult, Nullifier) { let (cb, nf) = fake_compact_block( + &self.params, height, prev_hash, dfvk, @@ -246,9 +259,18 @@ where let (height, prev_hash, initial_sapling_tree_size) = self .latest_cached_block .map(|(prev_height, prev_hash, end_size)| (prev_height + 1, prev_hash, end_size)) - .unwrap_or_else(|| (sapling_activation_height(), BlockHash([0; 32]), 0)); + .unwrap_or_else(|| { + ( + self.params + .activation_height(NetworkUpgrade::Sapling) + .unwrap(), + BlockHash([0; 32]), + 0, + ) + }); let cb = fake_compact_block_spending( + &self.params, height, prev_hash, note, @@ -308,6 +330,19 @@ impl TestState { &mut self.db_data } + /// Exposes an immutable reference to the network in use. + pub(crate) fn network(&self) -> &Network { + &self.db_data.params + } + + /// Convenience method for obtaining the Sapling activation height for the network under test. + pub(crate) fn sapling_activation_height(&self) -> BlockHeight { + self.db_data + .params + .activation_height(NetworkUpgrade::Sapling) + .expect("Sapling activation height must be known.") + } + /// Exposes the test account, if enabled via [`TestBuilder::with_test_account`]. #[cfg(feature = "unstable")] pub(crate) fn test_account( @@ -525,42 +560,20 @@ impl TestState { } } -#[cfg(feature = "mainnet")] -pub(crate) fn network() -> Network { - Network::MainNetwork -} - -#[cfg(not(feature = "mainnet"))] -pub(crate) fn network() -> Network { - Network::TestNetwork -} - -#[cfg(feature = "mainnet")] -pub(crate) fn sapling_activation_height() -> BlockHeight { - Network::MainNetwork - .activation_height(NetworkUpgrade::Sapling) - .unwrap() -} - -#[cfg(not(feature = "mainnet"))] -pub(crate) fn sapling_activation_height() -> BlockHeight { - Network::TestNetwork - .activation_height(NetworkUpgrade::Sapling) - .unwrap() -} - #[cfg(test)] pub(crate) fn init_test_accounts_table_ufvk( db_data: &mut WalletDb, ) -> (UnifiedFullViewingKey, Option) { + use std::collections::HashMap; + let seed = [0u8; 32]; let account = AccountId::from(0); - let extsk = sapling::spending_key(&seed, network().coin_type(), account); + let extsk = sapling::spending_key(&seed, db_data.params.coin_type(), account); let dfvk = extsk.to_diversifiable_full_viewing_key(); #[cfg(feature = "transparent-inputs")] let (tkey, taddr) = { - let tkey = legacy::keys::AccountPrivKey::from_seed(&network(), &seed, account) + let tkey = legacy::keys::AccountPrivKey::from_seed(&db_data.params, &seed, account) .unwrap() .to_account_pubkey(); let taddr = tkey.derive_external_ivk().unwrap().default_address().0; @@ -593,7 +606,8 @@ pub(crate) enum AddressType { /// 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) fn fake_compact_block( +pub(crate) fn fake_compact_block( + params: &P, height: BlockHeight, prev_hash: BlockHash, dfvk: &DiversifiableFullViewingKey, @@ -609,7 +623,7 @@ pub(crate) fn fake_compact_block( // Create a fake Note for the account let mut rng = OsRng; - let rseed = generate_random_rseed(&network(), height, &mut rng); + let rseed = generate_random_rseed(params, height, &mut rng); let note = Note::from_parts(to, NoteValue::from_raw(value.into()), rseed); let encryptor = sapling_note_encryption::<_, Network>( Some(dfvk.fvk().ovk), @@ -655,7 +669,9 @@ pub(crate) fn fake_compact_block( /// Create a fake CompactBlock at the given height, spending a single note from the /// given address. -pub(crate) fn fake_compact_block_spending( +#[allow(clippy::too_many_arguments)] +pub(crate) fn fake_compact_block_spending( + params: &P, height: BlockHeight, prev_hash: BlockHash, (nf, in_value): (Nullifier, Amount), @@ -665,7 +681,7 @@ pub(crate) fn fake_compact_block_spending( initial_sapling_tree_size: u32, ) -> CompactBlock { let mut rng = OsRng; - let rseed = generate_random_rseed(&network(), height, &mut rng); + let rseed = generate_random_rseed(params, height, &mut rng); // Create a fake CompactBlock containing the note let cspend = CompactSaplingSpend { nf: nf.to_vec() }; @@ -700,7 +716,7 @@ pub(crate) fn fake_compact_block_spending( // Create a fake Note for the change ctx.outputs.push({ let change_addr = dfvk.default_address().1; - let rseed = generate_random_rseed(&network(), height, &mut rng); + let rseed = generate_random_rseed(params, height, &mut rng); let note = Note::from_parts( change_addr, NoteValue::from_raw((in_value - value).unwrap().into()), diff --git a/zcash_client_sqlite/src/wallet/commitment_tree.rs b/zcash_client_sqlite/src/wallet/commitment_tree.rs index 324208071..01302dca0 100644 --- a/zcash_client_sqlite/src/wallet/commitment_tree.rs +++ b/zcash_client_sqlite/src/wallet/commitment_tree.rs @@ -985,14 +985,14 @@ mod tests { }; use shardtree::ShardTree; use zcash_client_backend::data_api::chain::CommitmentTreeRoot; - use zcash_primitives::consensus::BlockHeight; + use zcash_primitives::consensus::{BlockHeight, Network}; use super::SqliteShardStore; - use crate::{testing, wallet::init::init_wallet_db, WalletDb, SAPLING_TABLES_PREFIX}; + use crate::{wallet::init::init_wallet_db, WalletDb, SAPLING_TABLES_PREFIX}; fn new_tree(m: usize) -> ShardTree, 4, 3> { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); data_file.keep().unwrap(); init_wallet_db(&mut db_data, None).unwrap(); @@ -1040,7 +1040,7 @@ mod tests { #[test] fn put_shard_roots() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); data_file.keep().unwrap(); init_wallet_db(&mut db_data, None).unwrap(); diff --git a/zcash_client_sqlite/src/wallet/init.rs b/zcash_client_sqlite/src/wallet/init.rs index 40913504c..354016fe4 100644 --- a/zcash_client_sqlite/src/wallet/init.rs +++ b/zcash_client_sqlite/src/wallet/init.rs @@ -386,36 +386,27 @@ mod tests { use zcash_primitives::{ block::BlockHash, - consensus::{BlockHeight, BranchId, NetworkUpgrade, Parameters}, + consensus::{self, BlockHeight, BranchId, Network, NetworkUpgrade, Parameters}, transaction::{TransactionData, TxVersion}, zip32::sapling::ExtendedFullViewingKey, }; - use crate::{ - error::SqliteClientError, - testing::{self, network}, - wallet::scanning::priority_code, - AccountId, WalletDb, - }; + use crate::{error::SqliteClientError, wallet::scanning::priority_code, AccountId, WalletDb}; use super::{init_accounts_table, init_blocks_table, init_wallet_db}; #[cfg(feature = "transparent-inputs")] use { - crate::{ - wallet::{self, pool_code, PoolType}, - WalletWrite, - }, + crate::wallet::{self, pool_code, PoolType}, zcash_address::test_vectors, - zcash_primitives::{ - consensus::Network, legacy::keys as transparent, zip32::DiversifierIndex, - }, + zcash_client_backend::data_api::WalletWrite, + zcash_primitives::{legacy::keys as transparent, zip32::DiversifierIndex}, }; #[test] fn verify_schema() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); init_wallet_db(&mut db_data, None).unwrap(); use regex::Regex; @@ -609,7 +600,7 @@ mod tests { AND (scan_queue.block_range_end - 1) >= shard.subtree_end_height ) WHERE scan_queue.priority > {}", - u32::from(testing::network().activation_height(NetworkUpgrade::Sapling).unwrap()), + u32::from(db_data.params.activation_height(NetworkUpgrade::Sapling).unwrap()), priority_code(&ScanPriority::Scanned), ), // v_transactions @@ -768,7 +759,7 @@ mod tests { #[test] fn init_migrate_from_0_3_0() { - fn init_0_3_0

( + fn init_0_3_0( wdb: &mut WalletDb, extfvk: &ExtendedFullViewingKey, account: AccountId, @@ -852,11 +843,11 @@ mod tests { )?; let address = encode_payment_address( - testing::network().hrp_sapling_payment_address(), + wdb.params.hrp_sapling_payment_address(), &extfvk.default_address().1, ); let extfvk = encode_extended_full_viewing_key( - testing::network().hrp_sapling_extended_full_viewing_key(), + wdb.params.hrp_sapling_extended_full_viewing_key(), extfvk, ); wdb.conn.execute( @@ -872,19 +863,21 @@ mod tests { Ok(()) } + let data_file = NamedTempFile::new().unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); + let seed = [0xab; 32]; let account = AccountId::from(0); - let secret_key = sapling::spending_key(&seed, testing::network().coin_type(), account); + let secret_key = sapling::spending_key(&seed, db_data.params.coin_type(), account); let extfvk = secret_key.to_extended_full_viewing_key(); - let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + init_0_3_0(&mut db_data, &extfvk, account).unwrap(); init_wallet_db(&mut db_data, Some(Secret::new(seed.to_vec()))).unwrap(); } #[test] fn init_migrate_from_autoshielding_poc() { - fn init_autoshielding

( + fn init_autoshielding( wdb: &mut WalletDb, extfvk: &ExtendedFullViewingKey, account: AccountId, @@ -984,11 +977,11 @@ mod tests { )?; let address = encode_payment_address( - testing::network().hrp_sapling_payment_address(), + wdb.params.hrp_sapling_payment_address(), &extfvk.default_address().1, ); let extfvk = encode_extended_full_viewing_key( - testing::network().hrp_sapling_extended_full_viewing_key(), + wdb.params.hrp_sapling_extended_full_viewing_key(), extfvk, ); wdb.conn.execute( @@ -1038,19 +1031,21 @@ mod tests { Ok(()) } + let data_file = NamedTempFile::new().unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); + let seed = [0xab; 32]; let account = AccountId::from(0); - let secret_key = sapling::spending_key(&seed, testing::network().coin_type(), account); + let secret_key = sapling::spending_key(&seed, db_data.params.coin_type(), account); let extfvk = secret_key.to_extended_full_viewing_key(); - let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + init_autoshielding(&mut db_data, &extfvk, account).unwrap(); init_wallet_db(&mut db_data, Some(Secret::new(seed.to_vec()))).unwrap(); } #[test] fn init_migrate_from_main_pre_migrations() { - fn init_main

( + fn init_main( wdb: &mut WalletDb, ufvk: &UnifiedFullViewingKey, account: AccountId, @@ -1150,9 +1145,9 @@ mod tests { [], )?; - let ufvk_str = ufvk.encode(&testing::network()); + let ufvk_str = ufvk.encode(&wdb.params); let address_str = - RecipientAddress::Unified(ufvk.default_address().0).encode(&testing::network()); + RecipientAddress::Unified(ufvk.default_address().0).encode(&wdb.params); wdb.conn.execute( "INSERT INTO accounts (account, ufvk, address, transparent_address) VALUES (?, ?, ?, '')", @@ -1168,7 +1163,7 @@ mod tests { { let taddr = RecipientAddress::Transparent(*ufvk.default_address().0.transparent().unwrap()) - .encode(&testing::network()); + .encode(&wdb.params); wdb.conn.execute( "INSERT INTO blocks (height, hash, time, sapling_tree) VALUES (0, 0, 0, x'000000')", [], @@ -1186,12 +1181,13 @@ mod tests { Ok(()) } + let data_file = NamedTempFile::new().unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); + let seed = [0xab; 32]; let account = AccountId::from(0); - let secret_key = - UnifiedSpendingKey::from_seed(&testing::network(), &seed, account).unwrap(); - let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let secret_key = UnifiedSpendingKey::from_seed(&db_data.params, &seed, account).unwrap(); + init_main( &mut db_data, &secret_key.to_unified_full_viewing_key(), @@ -1204,7 +1200,7 @@ mod tests { #[test] fn init_accounts_table_only_works_once() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap(); // We can call the function as many times as we want with no data @@ -1215,13 +1211,13 @@ mod tests { let account = AccountId::from(0); // First call with data should initialise the accounts table - let extsk = sapling::spending_key(&seed, network().coin_type(), account); + let extsk = sapling::spending_key(&seed, db_data.params.coin_type(), account); let dfvk = extsk.to_diversifiable_full_viewing_key(); #[cfg(feature = "transparent-inputs")] let ufvk = UnifiedFullViewingKey::new( Some( - transparent::AccountPrivKey::from_seed(&network(), &seed, account) + transparent::AccountPrivKey::from_seed(&db_data.params, &seed, account) .unwrap() .to_account_pubkey(), ), @@ -1243,8 +1239,9 @@ mod tests { #[test] fn init_accounts_table_allows_no_gaps() { + let params = Network::TestNetwork; let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), params).unwrap(); init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap(); // allow sequential initialization @@ -1253,7 +1250,7 @@ mod tests { ids.iter() .map(|a| { let account = AccountId::from(*a); - UnifiedSpendingKey::from_seed(&network(), &seed, account) + UnifiedSpendingKey::from_seed(¶ms, &seed, account) .map(|k| (account, k.to_unified_full_viewing_key())) .unwrap() }) @@ -1273,7 +1270,7 @@ mod tests { #[test] fn init_blocks_table_only_works_once() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap(); // First call with data should initialise the blocks table @@ -1300,14 +1297,14 @@ mod tests { #[test] fn init_accounts_table_stores_correct_address() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); init_wallet_db(&mut db_data, None).unwrap(); let seed = [0u8; 32]; // Add an account to the wallet let account_id = AccountId::from(0); - let usk = UnifiedSpendingKey::from_seed(&testing::network(), &seed, account_id).unwrap(); + let usk = UnifiedSpendingKey::from_seed(&db_data.params, &seed, account_id).unwrap(); let ufvk = usk.to_unified_full_viewing_key(); let expected_address = ufvk.sapling().unwrap().default_address().1; let ufvks = HashMap::from([(account_id, ufvk)]); diff --git a/zcash_client_sqlite/src/wallet/init/migrations/add_transaction_views.rs b/zcash_client_sqlite/src/wallet/init/migrations/add_transaction_views.rs index 0d217c545..05ab54aed 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/add_transaction_views.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/add_transaction_views.rs @@ -283,10 +283,9 @@ mod tests { use tempfile::NamedTempFile; use zcash_client_backend::keys::UnifiedSpendingKey; - use zcash_primitives::zip32::AccountId; + use zcash_primitives::{consensus::Network, zip32::AccountId}; use crate::{ - testing, wallet::init::{init_wallet_db_internal, migrations::addresses_table}, WalletDb, }; @@ -310,19 +309,19 @@ mod tests { #[test] fn transaction_views() { + let network = Network::TestNetwork; let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), network).unwrap(); init_wallet_db_internal(&mut db_data, None, &[addresses_table::MIGRATION_ID]).unwrap(); let usk = - UnifiedSpendingKey::from_seed(&testing::network(), &[0u8; 32][..], AccountId::from(0)) - .unwrap(); + UnifiedSpendingKey::from_seed(&network, &[0u8; 32][..], AccountId::from(0)).unwrap(); let ufvk = usk.to_unified_full_viewing_key(); db_data .conn .execute( "INSERT INTO accounts (account, ufvk) VALUES (0, ?)", - params![ufvk.encode(&testing::network())], + params![ufvk.encode(&network)], ) .unwrap(); @@ -402,8 +401,9 @@ mod tests { #[test] #[cfg(feature = "transparent-inputs")] fn migrate_from_wm2() { + let network = Network::TestNetwork; let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), network).unwrap(); init_wallet_db_internal( &mut db_data, None, @@ -440,8 +440,7 @@ mod tests { tx.write(&mut tx_bytes).unwrap(); let usk = - UnifiedSpendingKey::from_seed(&testing::network(), &[0u8; 32][..], AccountId::from(0)) - .unwrap(); + UnifiedSpendingKey::from_seed(&network, &[0u8; 32][..], AccountId::from(0)).unwrap(); let ufvk = usk.to_unified_full_viewing_key(); let (ua, _) = ufvk.default_address(); let taddr = ufvk @@ -451,11 +450,11 @@ mod tests { .ok() .map(|k| k.derive_address(0).unwrap()) }) - .map(|a| a.encode(&testing::network())); + .map(|a| a.encode(&network)); db_data.conn.execute( "INSERT INTO accounts (account, ufvk, address, transparent_address) VALUES (0, ?, ?, ?)", - params![ufvk.encode(&testing::network()), ua.encode(&testing::network()), &taddr] + params![ufvk.encode(&network), ua.encode(&network), &taddr] ).unwrap(); db_data .conn diff --git a/zcash_client_sqlite/src/wallet/init/migrations/received_notes_nullable_nf.rs b/zcash_client_sqlite/src/wallet/init/migrations/received_notes_nullable_nf.rs index 2c946f3aa..5a856dc3d 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/received_notes_nullable_nf.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/received_notes_nullable_nf.rs @@ -233,10 +233,9 @@ mod tests { use tempfile::NamedTempFile; use zcash_client_backend::keys::UnifiedSpendingKey; - use zcash_primitives::zip32::AccountId; + use zcash_primitives::{consensus::Network, zip32::AccountId}; use crate::{ - testing, wallet::init::{init_wallet_db_internal, migrations::v_transactions_net}, WalletDb, }; @@ -244,19 +243,19 @@ mod tests { #[test] fn received_notes_nullable_migration() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); init_wallet_db_internal(&mut db_data, None, &[v_transactions_net::MIGRATION_ID]).unwrap(); // Create an account in the wallet let usk0 = - UnifiedSpendingKey::from_seed(&testing::network(), &[0u8; 32][..], AccountId::from(0)) + UnifiedSpendingKey::from_seed(&db_data.params, &[0u8; 32][..], AccountId::from(0)) .unwrap(); let ufvk0 = usk0.to_unified_full_viewing_key(); db_data .conn .execute( "INSERT INTO accounts (account, ufvk) VALUES (0, ?)", - params![ufvk0.encode(&testing::network())], + params![ufvk0.encode(&db_data.params)], ) .unwrap(); diff --git a/zcash_client_sqlite/src/wallet/init/migrations/v_transactions_net.rs b/zcash_client_sqlite/src/wallet/init/migrations/v_transactions_net.rs index b6bfe4a19..14cd830b1 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/v_transactions_net.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/v_transactions_net.rs @@ -212,10 +212,9 @@ mod tests { use tempfile::NamedTempFile; use zcash_client_backend::keys::UnifiedSpendingKey; - use zcash_primitives::zip32::AccountId; + use zcash_primitives::{consensus::Network, zip32::AccountId}; use crate::{ - testing, wallet::init::{init_wallet_db_internal, migrations::add_transaction_views}, WalletDb, }; @@ -223,32 +222,32 @@ mod tests { #[test] fn v_transactions_net() { let data_file = NamedTempFile::new().unwrap(); - let mut db_data = WalletDb::for_path(data_file.path(), testing::network()).unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); init_wallet_db_internal(&mut db_data, None, &[add_transaction_views::MIGRATION_ID]) .unwrap(); // Create two accounts in the wallet. let usk0 = - UnifiedSpendingKey::from_seed(&testing::network(), &[0u8; 32][..], AccountId::from(0)) + UnifiedSpendingKey::from_seed(&db_data.params, &[0u8; 32][..], AccountId::from(0)) .unwrap(); let ufvk0 = usk0.to_unified_full_viewing_key(); db_data .conn .execute( "INSERT INTO accounts (account, ufvk) VALUES (0, ?)", - params![ufvk0.encode(&testing::network())], + params![ufvk0.encode(&db_data.params)], ) .unwrap(); let usk1 = - UnifiedSpendingKey::from_seed(&testing::network(), &[1u8; 32][..], AccountId::from(1)) + UnifiedSpendingKey::from_seed(&db_data.params, &[1u8; 32][..], AccountId::from(1)) .unwrap(); let ufvk1 = usk1.to_unified_full_viewing_key(); db_data .conn .execute( "INSERT INTO accounts (account, ufvk) VALUES (1, ?)", - params![ufvk1.encode(&testing::network())], + params![ufvk1.encode(&db_data.params)], ) .unwrap(); diff --git a/zcash_client_sqlite/src/wallet/sapling.rs b/zcash_client_sqlite/src/wallet/sapling.rs index edf4ac205..8dfd29645 100644 --- a/zcash_client_sqlite/src/wallet/sapling.rs +++ b/zcash_client_sqlite/src/wallet/sapling.rs @@ -439,7 +439,7 @@ pub(crate) mod tests { use crate::{ error::SqliteClientError, - testing::{self, network, AddressType, BlockCache, TestBuilder, TestState}, + testing::{AddressType, BlockCache, TestBuilder, TestState}, wallet::{commitment_tree, get_balance, get_balance_at}, AccountId, NoteId, ReceivedNoteId, }; @@ -532,7 +532,7 @@ pub(crate) mod tests { let ufvks = [(account, usk.to_unified_full_viewing_key())] .into_iter() .collect(); - let decrypted_outputs = decrypt_transaction(&testing::network(), h + 1, &tx, &ufvks); + let decrypted_outputs = decrypt_transaction(st.network(), h + 1, &tx, &ufvks); assert_eq!(decrypted_outputs.len(), 2); let mut found_tx_change_memo = false; @@ -619,7 +619,7 @@ pub(crate) mod tests { // Create a USK that doesn't exist in the wallet let acct1 = AccountId::from(1); - let usk1 = UnifiedSpendingKey::from_seed(&network(), &[1u8; 32], acct1).unwrap(); + let usk1 = UnifiedSpendingKey::from_seed(st.network(), &[1u8; 32], acct1).unwrap(); // Attempting to spend with a USK that is not in the wallet results in an error assert_matches!( @@ -900,7 +900,7 @@ pub(crate) mod tests { let to = addr2.into(); #[allow(clippy::type_complexity)] - let send_and_recover_with_policy = |test: &mut TestState, + let send_and_recover_with_policy = |st: &mut TestState, ovk_policy| -> Result< Option<(Note, PaymentAddress, MemoBytes)>, @@ -912,7 +912,7 @@ pub(crate) mod tests { ReceivedNoteId, >, > { - let txid = test.create_spend_to_address( + let txid = st.create_spend_to_address( &usk, &to, Amount::from_u64(15000).unwrap(), @@ -922,7 +922,7 @@ pub(crate) mod tests { )?; // Fetch the transaction from the database - let raw_tx: Vec<_> = test + let raw_tx: Vec<_> = st .wallet() .conn .query_row( @@ -937,7 +937,7 @@ pub(crate) mod tests { for output in tx.sapling_bundle().unwrap().shielded_outputs() { // Find the output that decrypts with the external OVK let result = try_sapling_output_recovery( - &network(), + st.network(), h1, &dfvk.to_ovk(Scope::External), output, diff --git a/zcash_client_sqlite/src/wallet/scanning.rs b/zcash_client_sqlite/src/wallet/scanning.rs index 25a1be909..48ad6d4ae 100644 --- a/zcash_client_sqlite/src/wallet/scanning.rs +++ b/zcash_client_sqlite/src/wallet/scanning.rs @@ -752,7 +752,7 @@ mod tests { }; use crate::{ - testing::{sapling_activation_height, AddressType, TestBuilder}, + testing::{AddressType, TestBuilder}, wallet::{init::init_blocks_table, scanning::suggest_scan_ranges}, }; @@ -1090,6 +1090,7 @@ mod tests { .build(); let dfvk = st.test_account_sapling().unwrap(); + let sapling_activation_height = st.sapling_activation_height(); assert_matches!( // In the following, we don't care what the root hashes are, they just need to be @@ -1098,15 +1099,15 @@ mod tests { 0, &[ CommitmentTreeRoot::from_parts( - sapling_activation_height() + 100, + sapling_activation_height + 100, Node::empty_root(Level::from(0)) ), CommitmentTreeRoot::from_parts( - sapling_activation_height() + 200, + sapling_activation_height + 200, Node::empty_root(Level::from(1)) ), CommitmentTreeRoot::from_parts( - sapling_activation_height() + 300, + sapling_activation_height + 300, Node::empty_root(Level::from(2)) ), ] @@ -1118,7 +1119,7 @@ mod tests { // of 10 blocks. After `scan_cached_blocks`, the scan queue should have a requested scan // range of 300..310 with `FoundNote` priority, 310..320 with `Scanned` priority. let initial_sapling_tree_size = (0x1 << 16) * 3 + 5; - let initial_height = sapling_activation_height() + 310; + let initial_height = sapling_activation_height + 310; let value = Amount::from_u64(50000).unwrap(); st.generate_block_at( @@ -1141,7 +1142,7 @@ mod tests { st.scan_cached_blocks(initial_height, 10); // Verify the that adjacent range needed to make the note spendable has been prioritized. - let sap_active = u32::from(sapling_activation_height()); + let sap_active = u32::from(sapling_activation_height); assert_matches!( st.wallet().suggest_scan_ranges(), Ok(scan_ranges) if scan_ranges == vec![ @@ -1162,7 +1163,7 @@ mod tests { // future. assert_matches!( st.wallet_mut() - .update_chain_tip(sapling_activation_height() + 340), + .update_chain_tip(sapling_activation_height + 340), Ok(()) ); @@ -1179,7 +1180,7 @@ mod tests { // Now simulate a jump ahead more than 100 blocks. assert_matches!( st.wallet_mut() - .update_chain_tip(sapling_activation_height() + 450), + .update_chain_tip(sapling_activation_height + 450), Ok(()) );