From 0f49254007f7709ab6ab2a6223c9da824bdba08d Mon Sep 17 00:00:00 2001 From: Vihiga Tyonum Date: Sat, 14 Sep 2024 22:02:27 +0100 Subject: [PATCH] Refactor: use tx utils from testenv in chain crate - import testenv crate in chain crate - replace all macros and fns in the following tests with import from testenv crate - tests/test_indexed_tx_graph - tests/test_keychain_txout_index - tests/test_local_chain - tests/test_tx_graph - tests/test_tx_graph_conflicts [issue: #1602] --- crates/chain/Cargo.toml | 2 + crates/chain/tests/test_indexed_tx_graph.rs | 29 ++++++----- .../chain/tests/test_keychain_txout_index.rs | 6 +-- crates/chain/tests/test_local_chain.rs | 1 + crates/chain/tests/test_tx_graph.rs | 51 ++++++++++--------- crates/chain/tests/test_tx_graph_conflicts.rs | 4 +- 6 files changed, 49 insertions(+), 44 deletions(-) diff --git a/crates/chain/Cargo.toml b/crates/chain/Cargo.toml index 95bcaf9f6..6782a7d35 100644 --- a/crates/chain/Cargo.toml +++ b/crates/chain/Cargo.toml @@ -28,6 +28,8 @@ serde_json = { version = "1", optional = true } [dev-dependencies] rand = "0.8" proptest = "1.2.0" +bdk_testenv = { path = "../testenv", default-features = false } + [features] default = ["std", "miniscript"] diff --git a/crates/chain/tests/test_indexed_tx_graph.rs b/crates/chain/tests/test_indexed_tx_graph.rs index 1fbbcd0df..3a98bc8d8 100644 --- a/crates/chain/tests/test_indexed_tx_graph.rs +++ b/crates/chain/tests/test_indexed_tx_graph.rs @@ -12,6 +12,7 @@ use bdk_chain::{ local_chain::LocalChain, tx_graph, Balance, ChainPosition, ConfirmationBlockTime, DescriptorExt, }; +use bdk_testenv::{block_id, h, tx_utils::new_tx}; use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut}; use miniscript::Descriptor; @@ -49,7 +50,7 @@ fn insert_relevant_txs() { script_pubkey: spk_1, }, ], - ..common::new_tx(0) + ..new_tx(0) }; let tx_b = Transaction { @@ -57,7 +58,7 @@ fn insert_relevant_txs() { previous_output: OutPoint::new(tx_a.compute_txid(), 0), ..Default::default() }], - ..common::new_tx(1) + ..new_tx(1) }; let tx_c = Transaction { @@ -65,7 +66,7 @@ fn insert_relevant_txs() { previous_output: OutPoint::new(tx_a.compute_txid(), 1), ..Default::default() }], - ..common::new_tx(2) + ..new_tx(2) }; let txs = [tx_c, tx_b, tx_a]; @@ -187,7 +188,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(70000), script_pubkey: trusted_spks[0].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx2 is an incoming transaction received at untrusted keychain at block 1. @@ -196,7 +197,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(30000), script_pubkey: untrusted_spks[0].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx3 spends tx2 and gives a change back in trusted keychain. Confirmed at Block 2. @@ -209,7 +210,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(10000), script_pubkey: trusted_spks[1].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx4 is an external transaction receiving at untrusted keychain, unconfirmed. @@ -218,7 +219,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(20000), script_pubkey: untrusted_spks[1].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx5 is an external transaction receiving at trusted keychain, unconfirmed. @@ -227,11 +228,11 @@ fn test_list_owned_txouts() { value: Amount::from_sat(15000), script_pubkey: trusted_spks[2].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx6 is an unrelated transaction confirmed at 3. - let tx6 = common::new_tx(0); + let tx6 = new_tx(0); // Insert transactions into graph with respective anchors // Insert unconfirmed txs with a last_seen timestamp @@ -597,7 +598,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(0) + ..new_tx(0) }, anchor: None, last_seen: None, @@ -610,7 +611,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(1) + ..new_tx(1) }, anchor: None, last_seen: Some(2), @@ -623,7 +624,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(2) + ..new_tx(2) }, anchor: Some(blocks[1]), last_seen: None, @@ -636,7 +637,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(3) + ..new_tx(3) }, anchor: Some(block_id!(2, "B'")), last_seen: Some(2), @@ -649,7 +650,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(4) + ..new_tx(4) }, anchor: Some(block_id!(2, "B'")), last_seen: None, diff --git a/crates/chain/tests/test_keychain_txout_index.rs b/crates/chain/tests/test_keychain_txout_index.rs index 267ee2e99..cb9b40c69 100644 --- a/crates/chain/tests/test_keychain_txout_index.rs +++ b/crates/chain/tests/test_keychain_txout_index.rs @@ -7,7 +7,7 @@ use bdk_chain::{ indexer::keychain_txout::{ChangeSet, KeychainTxOutIndex}, DescriptorExt, DescriptorId, Indexer, Merge, }; - +use bdk_testenv::{h, tx_utils::new_tx}; use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxOut}; use miniscript::{Descriptor, DescriptorPublicKey}; @@ -253,7 +253,7 @@ fn test_lookahead() { value: Amount::from_sat(10_000), }, ], - ..common::new_tx(external_index) + ..new_tx(external_index) }; assert_eq!(txout_index.index_tx(&tx), ChangeSet::default()); assert_eq!( @@ -656,7 +656,7 @@ fn reassigning_keychain_to_a_new_descriptor_should_error() { #[test] fn when_querying_over_a_range_of_keychains_the_utxos_should_show_up() { let mut indexer = KeychainTxOutIndex::::new(0); - let mut tx = common::new_tx(0); + let mut tx = new_tx(0); for (i, descriptor) in DESCRIPTORS.iter().enumerate() { let descriptor = parse_descriptor(descriptor); diff --git a/crates/chain/tests/test_local_chain.rs b/crates/chain/tests/test_local_chain.rs index 29686564d..6ffc5d6e2 100644 --- a/crates/chain/tests/test_local_chain.rs +++ b/crates/chain/tests/test_local_chain.rs @@ -9,6 +9,7 @@ use bdk_chain::{ }, BlockId, }; +use bdk_testenv::{chain_update, h, local_chain}; use bitcoin::{block::Header, hashes::Hash, BlockHash}; use proptest::prelude::*; diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index a49c9e5f5..df0cf600b 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -9,6 +9,7 @@ use bdk_chain::{ tx_graph::{ChangeSet, TxGraph}, Anchor, ChainOracle, ChainPosition, Merge, }; +use bdk_testenv::{block_id, h, tx_utils::new_tx}; use bitcoin::{ absolute, hashes::Hash, transaction, Amount, BlockHash, OutPoint, ScriptBuf, SignedAmount, Transaction, TxIn, TxOut, Txid, @@ -474,7 +475,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL, TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_b0 spends tx_a0 @@ -484,7 +485,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL, TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_b1 spends tx_a0 @@ -494,7 +495,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let tx_b2 = Transaction { @@ -503,7 +504,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_c0 spends tx_b0 @@ -513,7 +514,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_c1 spends tx_b0 @@ -523,7 +524,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_c2 spends tx_b1 and tx_b2 @@ -539,7 +540,7 @@ fn test_walk_ancestors() { }, ], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let tx_c3 = Transaction { @@ -548,7 +549,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_d0 spends tx_c1 @@ -558,7 +559,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_d1 spends tx_c2 and tx_c3 @@ -574,7 +575,7 @@ fn test_walk_ancestors() { }, ], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_e0 spends tx_d1 @@ -584,7 +585,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let mut graph = TxGraph::::new([ @@ -667,7 +668,7 @@ fn test_conflicting_descendants() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_a2 spends previous_output and conflicts with tx_a @@ -677,7 +678,7 @@ fn test_conflicting_descendants() { ..TxIn::default() }], output: vec![TxOut::NULL, TxOut::NULL], - ..common::new_tx(1) + ..new_tx(1) }; // tx_b spends tx_a @@ -687,7 +688,7 @@ fn test_conflicting_descendants() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(2) + ..new_tx(2) }; let txid_a = tx_a.compute_txid(); @@ -709,7 +710,7 @@ fn test_conflicting_descendants() { fn test_descendants_no_repeat() { let tx_a = Transaction { output: vec![TxOut::NULL, TxOut::NULL, TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let txs_b = (0..3) @@ -719,7 +720,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(1) + ..new_tx(1) }) .collect::>(); @@ -730,7 +731,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(2) + ..new_tx(2) }) .collect::>(); @@ -746,7 +747,7 @@ fn test_descendants_no_repeat() { }, ], output: vec![TxOut::NULL], - ..common::new_tx(3) + ..new_tx(3) }; let tx_e = Transaction { @@ -755,7 +756,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(4) + ..new_tx(4) }; let txs_not_connected = (10..20) @@ -765,7 +766,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(v) + ..new_tx(v) }) .collect::>(); @@ -819,7 +820,7 @@ fn test_chain_spends() { script_pubkey: ScriptBuf::new(), }, ], - ..common::new_tx(0) + ..new_tx(0) }; // The first confirmed transaction spends vout: 0. And is confirmed at block 98. @@ -838,7 +839,7 @@ fn test_chain_spends() { script_pubkey: ScriptBuf::new(), }, ], - ..common::new_tx(0) + ..new_tx(0) }; // The second transactions spends vout:1, and is unconfirmed. @@ -857,7 +858,7 @@ fn test_chain_spends() { script_pubkey: ScriptBuf::new(), }, ], - ..common::new_tx(0) + ..new_tx(0) }; let mut graph = TxGraph::::default(); @@ -929,7 +930,7 @@ fn test_chain_spends() { previous_output: OutPoint::new(tx_0.compute_txid(), 0), ..Default::default() }], - ..common::new_tx(0) + ..new_tx(0) }; let _ = graph.insert_tx(tx_1_conflict.clone()); @@ -944,7 +945,7 @@ fn test_chain_spends() { previous_output: OutPoint::new(tx_0.compute_txid(), 1), ..Default::default() }], - ..common::new_tx(0) + ..new_tx(0) }; // Insert in graph and mark it as seen. diff --git a/crates/chain/tests/test_tx_graph_conflicts.rs b/crates/chain/tests/test_tx_graph_conflicts.rs index 45370b676..3f4845582 100644 --- a/crates/chain/tests/test_tx_graph_conflicts.rs +++ b/crates/chain/tests/test_tx_graph_conflicts.rs @@ -3,11 +3,11 @@ #[macro_use] mod common; -use std::collections::{BTreeSet, HashSet}; - use bdk_chain::{Balance, BlockId}; +use bdk_testenv::{block_id, h, local_chain}; use bitcoin::{Amount, OutPoint, ScriptBuf}; use common::*; +use std::collections::{BTreeSet, HashSet}; #[allow(dead_code)] struct Scenario<'a> {