Skip to content

Commit ad59970

Browse files
committed
Merge #1612: Refactor: Move transaction testing utilities from crates/chain/tests/common to testenv crate
88a8403 refactor(test): move tx test utils to testenv (Vihiga Tyonum) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description This change moves all transaction testing utilities (macros and functions) from `crates/chain/tests/common` to `crates/testenv/tx_utils` to minimize duplication. Closes #1602 <!-- Describe the purpose of this PR, what's being adding and/or fixed --> ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ### Changelog notice <!-- Notice the release manager should include in the release tag message changelog --> <!-- See https://keepachangelog.com/en/1.0.0/ for examples --> ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [ ] I've added tests for the new feature * [ ] I've added docs for the new feature #### Bugfixes: * [ ] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [x] I'm linking the issue being fixed by this PR ACKs for top commit: ValuedMammal: ACK 88a8403 notmandatory: ACK 88a8403 Tree-SHA512: aa85c6683d44463e8858f18840fd628d98f54757edff85fe0489c7954fc56998456ce8069e328eff46e907235d092059975c10e420fe7dfe83e5cdd9b63fc8ed
2 parents ec7342d + 88a8403 commit ad59970

File tree

13 files changed

+368
-350
lines changed

13 files changed

+368
-350
lines changed

crates/chain/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ serde_json = { version = "1", optional = true }
2828
[dev-dependencies]
2929
rand = "0.8"
3030
proptest = "1.2.0"
31+
bdk_testenv = { path = "../testenv", default-features = false }
32+
3133

3234
[features]
3335
default = ["std", "miniscript"]

crates/chain/tests/common/mod.rs

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,3 @@
33
mod tx_template;
44
#[allow(unused_imports)]
55
pub use tx_template::*;
6-
7-
#[allow(unused_macros)]
8-
macro_rules! block_id {
9-
($height:expr, $hash:literal) => {{
10-
bdk_chain::BlockId {
11-
height: $height,
12-
hash: bitcoin::hashes::Hash::hash($hash.as_bytes()),
13-
}
14-
}};
15-
}
16-
17-
#[allow(unused_macros)]
18-
macro_rules! h {
19-
($index:literal) => {{
20-
bitcoin::hashes::Hash::hash($index.as_bytes())
21-
}};
22-
}
23-
24-
#[allow(unused_macros)]
25-
macro_rules! local_chain {
26-
[ $(($height:expr, $block_hash:expr)), * ] => {{
27-
#[allow(unused_mut)]
28-
bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $block_hash).into()),*].into_iter().collect())
29-
.expect("chain must have genesis block")
30-
}};
31-
}
32-
33-
#[allow(unused_macros)]
34-
macro_rules! chain_update {
35-
[ $(($height:expr, $hash:expr)), * ] => {{
36-
#[allow(unused_mut)]
37-
bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $hash).into()),*].into_iter().collect())
38-
.expect("chain must have genesis block")
39-
.tip()
40-
}};
41-
}
42-
43-
#[allow(unused_macros)]
44-
macro_rules! changeset {
45-
(checkpoints: $($tail:tt)*) => { changeset!(index: TxHeight, checkpoints: $($tail)*) };
46-
(
47-
index: $ind:ty,
48-
checkpoints: [ $(( $height:expr, $cp_to:expr )),* ]
49-
$(,txids: [ $(( $txid:expr, $tx_to:expr )),* ])?
50-
) => {{
51-
use bdk_chain::collections::BTreeMap;
52-
53-
#[allow(unused_mut)]
54-
bdk_chain::sparse_chain::ChangeSet::<$ind> {
55-
checkpoints: {
56-
let mut changes = BTreeMap::default();
57-
$(changes.insert($height, $cp_to);)*
58-
changes
59-
},
60-
txids: {
61-
let mut changes = BTreeMap::default();
62-
$($(changes.insert($txid, $tx_to.map(|h: TxHeight| h.into()));)*)?
63-
changes
64-
}
65-
}
66-
}};
67-
}
68-
69-
#[allow(unused)]
70-
pub fn new_tx(lt: u32) -> bitcoin::Transaction {
71-
bitcoin::Transaction {
72-
version: bitcoin::transaction::Version::non_standard(0x00),
73-
lock_time: bitcoin::absolute::LockTime::from_consensus(lt),
74-
input: vec![],
75-
output: vec![],
76-
}
77-
}
78-
79-
#[allow(unused)]
80-
pub const DESCRIPTORS: [&str; 7] = [
81-
"tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)",
82-
"tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)",
83-
"wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0/*)",
84-
"tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/0/*)",
85-
"tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/1/*)",
86-
"wpkh(xprv9s21ZrQH143K4EXURwMHuLS469fFzZyXk7UUpdKfQwhoHcAiYTakpe8pMU2RiEdvrU9McyuE7YDoKcXkoAwEGoK53WBDnKKv2zZbb9BzttX/1/0/*)",
87-
// non-wildcard
88-
"wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)",
89-
];

crates/chain/tests/common/tx_template.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg(feature = "miniscript")]
22

3+
use bdk_testenv::utils::DESCRIPTORS;
34
use rand::distributions::{Alphanumeric, DistString};
45
use std::collections::HashMap;
56

@@ -55,7 +56,7 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
5556
tx_templates: impl IntoIterator<Item = &'a TxTemplate<'a, A>>,
5657
) -> (TxGraph<A>, SpkTxOutIndex<u32>, HashMap<&'a str, Txid>) {
5758
let (descriptor, _) =
58-
Descriptor::parse_descriptor(&Secp256k1::signing_only(), super::DESCRIPTORS[2]).unwrap();
59+
Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap();
5960
let mut graph = TxGraph::<A>::default();
6061
let mut spk_index = SpkTxOutIndex::default();
6162
(0..10).for_each(|index| {

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ mod common;
55

66
use std::{collections::BTreeSet, sync::Arc};
77

8-
use crate::common::DESCRIPTORS;
98
use bdk_chain::{
109
indexed_tx_graph::{self, IndexedTxGraph},
1110
indexer::keychain_txout::KeychainTxOutIndex,
1211
local_chain::LocalChain,
1312
tx_graph, Balance, ChainPosition, ConfirmationBlockTime, DescriptorExt,
1413
};
14+
use bdk_testenv::{
15+
block_id, hash,
16+
utils::{new_tx, DESCRIPTORS},
17+
};
1518
use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
1619
use miniscript::Descriptor;
1720

@@ -49,23 +52,23 @@ fn insert_relevant_txs() {
4952
script_pubkey: spk_1,
5053
},
5154
],
52-
..common::new_tx(0)
55+
..new_tx(0)
5356
};
5457

5558
let tx_b = Transaction {
5659
input: vec![TxIn {
5760
previous_output: OutPoint::new(tx_a.compute_txid(), 0),
5861
..Default::default()
5962
}],
60-
..common::new_tx(1)
63+
..new_tx(1)
6164
};
6265

6366
let tx_c = Transaction {
6467
input: vec![TxIn {
6568
previous_output: OutPoint::new(tx_a.compute_txid(), 1),
6669
..Default::default()
6770
}],
68-
..common::new_tx(2)
71+
..new_tx(2)
6972
};
7073

7174
let txs = [tx_c, tx_b, tx_a];
@@ -126,15 +129,16 @@ fn insert_relevant_txs() {
126129
#[test]
127130
fn test_list_owned_txouts() {
128131
// Create Local chains
129-
let local_chain = LocalChain::from_blocks((0..150).map(|i| (i as u32, h!("random"))).collect())
130-
.expect("must have genesis hash");
132+
let local_chain =
133+
LocalChain::from_blocks((0..150).map(|i| (i as u32, hash!("random"))).collect())
134+
.expect("must have genesis hash");
131135

132136
// Initiate IndexedTxGraph
133137

134138
let (desc_1, _) =
135-
Descriptor::parse_descriptor(&Secp256k1::signing_only(), common::DESCRIPTORS[2]).unwrap();
139+
Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap();
136140
let (desc_2, _) =
137-
Descriptor::parse_descriptor(&Secp256k1::signing_only(), common::DESCRIPTORS[3]).unwrap();
141+
Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[3]).unwrap();
138142

139143
let mut graph = IndexedTxGraph::<ConfirmationBlockTime, KeychainTxOutIndex<String>>::new(
140144
KeychainTxOutIndex::new(10),
@@ -187,7 +191,7 @@ fn test_list_owned_txouts() {
187191
value: Amount::from_sat(70000),
188192
script_pubkey: trusted_spks[0].to_owned(),
189193
}],
190-
..common::new_tx(0)
194+
..new_tx(0)
191195
};
192196

193197
// tx2 is an incoming transaction received at untrusted keychain at block 1.
@@ -196,7 +200,7 @@ fn test_list_owned_txouts() {
196200
value: Amount::from_sat(30000),
197201
script_pubkey: untrusted_spks[0].to_owned(),
198202
}],
199-
..common::new_tx(0)
203+
..new_tx(0)
200204
};
201205

202206
// tx3 spends tx2 and gives a change back in trusted keychain. Confirmed at Block 2.
@@ -209,7 +213,7 @@ fn test_list_owned_txouts() {
209213
value: Amount::from_sat(10000),
210214
script_pubkey: trusted_spks[1].to_owned(),
211215
}],
212-
..common::new_tx(0)
216+
..new_tx(0)
213217
};
214218

215219
// tx4 is an external transaction receiving at untrusted keychain, unconfirmed.
@@ -218,7 +222,7 @@ fn test_list_owned_txouts() {
218222
value: Amount::from_sat(20000),
219223
script_pubkey: untrusted_spks[1].to_owned(),
220224
}],
221-
..common::new_tx(0)
225+
..new_tx(0)
222226
};
223227

224228
// tx5 is an external transaction receiving at trusted keychain, unconfirmed.
@@ -227,11 +231,11 @@ fn test_list_owned_txouts() {
227231
value: Amount::from_sat(15000),
228232
script_pubkey: trusted_spks[2].to_owned(),
229233
}],
230-
..common::new_tx(0)
234+
..new_tx(0)
231235
};
232236

233237
// tx6 is an unrelated transaction confirmed at 3.
234-
let tx6 = common::new_tx(0);
238+
let tx6 = new_tx(0);
235239

236240
// Insert transactions into graph with respective anchors
237241
// Insert unconfirmed txs with a last_seen timestamp
@@ -597,7 +601,7 @@ fn test_get_chain_position() {
597601
value: Amount::ONE_BTC,
598602
script_pubkey: spk.clone(),
599603
}],
600-
..common::new_tx(0)
604+
..new_tx(0)
601605
},
602606
anchor: None,
603607
last_seen: None,
@@ -610,7 +614,7 @@ fn test_get_chain_position() {
610614
value: Amount::ONE_BTC,
611615
script_pubkey: spk.clone(),
612616
}],
613-
..common::new_tx(1)
617+
..new_tx(1)
614618
},
615619
anchor: None,
616620
last_seen: Some(2),
@@ -623,7 +627,7 @@ fn test_get_chain_position() {
623627
value: Amount::ONE_BTC,
624628
script_pubkey: spk.clone(),
625629
}],
626-
..common::new_tx(2)
630+
..new_tx(2)
627631
},
628632
anchor: Some(blocks[1]),
629633
last_seen: None,
@@ -636,7 +640,7 @@ fn test_get_chain_position() {
636640
value: Amount::ONE_BTC,
637641
script_pubkey: spk.clone(),
638642
}],
639-
..common::new_tx(3)
643+
..new_tx(3)
640644
},
641645
anchor: Some(block_id!(2, "B'")),
642646
last_seen: Some(2),
@@ -649,7 +653,7 @@ fn test_get_chain_position() {
649653
value: Amount::ONE_BTC,
650654
script_pubkey: spk.clone(),
651655
}],
652-
..common::new_tx(4)
656+
..new_tx(4)
653657
},
654658
anchor: Some(block_id!(2, "B'")),
655659
last_seen: None,

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#![cfg(feature = "miniscript")]
22

3-
#[macro_use]
4-
mod common;
53
use bdk_chain::{
64
collections::BTreeMap,
75
indexer::keychain_txout::{ChangeSet, KeychainTxOutIndex},
86
DescriptorExt, DescriptorId, Indexer, Merge,
97
};
10-
8+
use bdk_testenv::{
9+
hash,
10+
utils::{new_tx, DESCRIPTORS},
11+
};
1112
use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxOut};
1213
use miniscript::{Descriptor, DescriptorPublicKey};
1314

14-
use crate::common::DESCRIPTORS;
15-
1615
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
1716
enum TestKeychain {
1817
External,
@@ -253,7 +252,7 @@ fn test_lookahead() {
253252
value: Amount::from_sat(10_000),
254253
},
255254
],
256-
..common::new_tx(external_index)
255+
..new_tx(external_index)
257256
};
258257
assert_eq!(txout_index.index_tx(&tx), ChangeSet::default());
259258
assert_eq!(
@@ -305,7 +304,7 @@ fn test_scan_with_lookahead() {
305304
.collect();
306305

307306
for (&spk_i, spk) in &spks {
308-
let op = OutPoint::new(h!("fake tx"), spk_i);
307+
let op = OutPoint::new(hash!("fake tx"), spk_i);
309308
let txout = TxOut {
310309
script_pubkey: spk.clone(),
311310
value: Amount::ZERO,
@@ -331,7 +330,7 @@ fn test_scan_with_lookahead() {
331330
.at_derivation_index(41)
332331
.unwrap()
333332
.script_pubkey();
334-
let op = OutPoint::new(h!("fake tx"), 41);
333+
let op = OutPoint::new(hash!("fake tx"), 41);
335334
let txout = TxOut {
336335
script_pubkey: spk_41,
337336
value: Amount::ZERO,
@@ -656,7 +655,7 @@ fn reassigning_keychain_to_a_new_descriptor_should_error() {
656655
#[test]
657656
fn when_querying_over_a_range_of_keychains_the_utxos_should_show_up() {
658657
let mut indexer = KeychainTxOutIndex::<usize>::new(0);
659-
let mut tx = common::new_tx(0);
658+
let mut tx = new_tx(0);
660659

661660
for (i, descriptor) in DESCRIPTORS.iter().enumerate() {
662661
let descriptor = parse_descriptor(descriptor);

0 commit comments

Comments
 (0)