From 6903fa6543cafdf1b777a5ff37c8d741b7c07618 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 21 Aug 2024 14:58:11 -0500 Subject: [PATCH] zcash_client_sqlite: Add test showing tx_retrieval_queue migration bug --- .../init/migrations/tx_retrieval_queue.rs | 87 ++++++++++++++++++- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs b/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs index 4c17103e0..3e7acce0f 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs @@ -14,6 +14,8 @@ use super::utxos_to_txos; pub(super) const MIGRATION_ID: Uuid = Uuid::from_u128(0xfec02b61_3988_4b4f_9699_98977fac9e7f); +const DEPENDENCIES: [Uuid; 1] = [utxos_to_txos::MIGRATION_ID]; + pub(super) struct Migration

{ pub(super) params: P, } @@ -24,7 +26,7 @@ impl

schemer::Migration for Migration

{ } fn dependencies(&self) -> HashSet { - [utxos_to_txos::MIGRATION_ID].into_iter().collect() + DEPENDENCIES.into_iter().collect() } fn description(&self) -> &'static str { @@ -133,10 +135,89 @@ impl RusqliteMigration for Migration

{ #[cfg(test)] mod tests { - use crate::wallet::init::migrations::tests::test_migrate; + use rusqlite::named_params; + use secrecy::Secret; + use tempfile::NamedTempFile; + use zcash_primitives::{ + legacy::{Script, TransparentAddress}, + transaction::{components::transparent, Authorized, TransactionData, TxVersion}, + }; + use zcash_protocol::{ + consensus::{BranchId, Network}, + value::Zatoshis, + }; + + use crate::{ + wallet::init::{init_wallet_db_internal, migrations::tests::test_migrate}, + WalletDb, + }; + + use super::{DEPENDENCIES, MIGRATION_ID}; #[test] fn migrate() { - test_migrate(&[super::MIGRATION_ID]); + test_migrate(&[MIGRATION_ID]); + } + + #[test] + fn migrate_with_data() { + let data_file = NamedTempFile::new().unwrap(); + let mut db_data = WalletDb::for_path(data_file.path(), Network::TestNetwork).unwrap(); + + let seed_bytes = vec![0xab; 32]; + + // Migrate to database state just prior to this migration. + init_wallet_db_internal( + &mut db_data, + Some(Secret::new(seed_bytes.clone())), + &DEPENDENCIES, + false, + ) + .unwrap(); + + // Add transactions to the wallet that exercise the data migration. + let add_tx_to_wallet = |tx: TransactionData| { + let tx = tx.freeze().unwrap(); + let txid = tx.txid(); + let mut raw_tx = vec![]; + tx.write(&mut raw_tx).unwrap(); + db_data + .conn + .execute( + r#"INSERT INTO transactions (txid, raw) VALUES (:txid, :raw);"#, + named_params! {":txid": txid.as_ref(), ":raw": raw_tx}, + ) + .unwrap(); + }; + add_tx_to_wallet(TransactionData::from_parts( + TxVersion::Zip225, + BranchId::Nu5, + 0, + 12345678.into(), + Some(transparent::Bundle { + vin: vec![transparent::TxIn { + prevout: transparent::OutPoint::fake(), + script_sig: Script(vec![]), + sequence: 0, + }], + vout: vec![transparent::TxOut { + value: Zatoshis::const_from_u64(10_000), + script_pubkey: TransparentAddress::PublicKeyHash([7; 20]).script(), + }], + authorization: transparent::Authorized, + }), + None, + None, + None, + )); + + // Check that we can apply this migration. + init_wallet_db_internal( + &mut db_data, + Some(Secret::new(seed_bytes)), + &[MIGRATION_ID], + false, + ) + .unwrap(); } }