Skip to content

Commit

Permalink
zcash_client_sqlite: Add test showing tx_retrieval_queue migration bug
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Aug 21, 2024
1 parent 7c416d5 commit 6903fa6
Showing 1 changed file with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> {
pub(super) params: P,
}
Expand All @@ -24,7 +26,7 @@ impl<P> schemer::Migration for Migration<P> {
}

fn dependencies(&self) -> HashSet<Uuid> {
[utxos_to_txos::MIGRATION_ID].into_iter().collect()
DEPENDENCIES.into_iter().collect()
}

fn description(&self) -> &'static str {
Expand Down Expand Up @@ -133,10 +135,89 @@ impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {

#[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<Authorized>| {
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();
}
}

0 comments on commit 6903fa6

Please sign in to comment.