Skip to content

Commit

Permalink
Import & use tx utils from testenv in chain crate
Browse files Browse the repository at this point in the history
- 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]
  • Loading branch information
tvpeter committed Sep 14, 2024
1 parent c97c3ea commit b65f74b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 44 deletions.
2 changes: 2 additions & 0 deletions crates/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
29 changes: 15 additions & 14 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,23 +50,23 @@ fn insert_relevant_txs() {
script_pubkey: spk_1,
},
],
..common::new_tx(0)
..new_tx(0)
};

let tx_b = Transaction {
input: vec![TxIn {
previous_output: OutPoint::new(tx_a.compute_txid(), 0),
..Default::default()
}],
..common::new_tx(1)
..new_tx(1)
};

let tx_c = Transaction {
input: vec![TxIn {
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];
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions crates/chain/tests/test_keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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::<usize>::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);
Expand Down
1 change: 1 addition & 0 deletions crates/chain/tests/test_local_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
Loading

0 comments on commit b65f74b

Please sign in to comment.