Skip to content

Commit 776e383

Browse files
committed
refactor(test): move tx_template to testenv
- move tx_template from chain to testenv - update imports from chain/common/ to bdk_testenv /tx_template/
1 parent 75c9365 commit 776e383

7 files changed

Lines changed: 60 additions & 35 deletions

File tree

crates/chain/tests/common/mod.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

crates/chain/tests/test_indexed_tx_graph.rs

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

3-
#[macro_use]
4-
mod common;
5-
63
use std::{collections::BTreeSet, str::FromStr, sync::Arc};
74

85
use bdk_chain::{

crates/chain/tests/test_tx_graph.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![cfg(feature = "miniscript")]
22

3-
#[macro_use]
4-
mod common;
53
use bdk_chain::{collections::*, BlockId, CanonicalizationParams, ConfirmationBlockTime};
64
use bdk_chain::{
75
local_chain::LocalChain,
@@ -10,13 +8,13 @@ use bdk_chain::{
108
Anchor, ChainOracle, ChainPosition, Merge,
119
};
1210
use bdk_testenv::{block_id, hash, utils::new_tx};
11+
use bdk_testenv::{init_graph, TxInTemplate, TxOutTemplate, TxTemplate};
1312
use bitcoin::hex::FromHex;
1413
use bitcoin::Witness;
1514
use bitcoin::{
1615
absolute, hashes::Hash, transaction, Amount, BlockHash, OutPoint, ScriptBuf, SignedAmount,
1716
Transaction, TxIn, TxOut, Txid,
1817
};
19-
use common::*;
2018
use core::iter;
2119
use rand::RngCore;
2220
use std::sync::Arc;

crates/chain/tests/test_tx_graph_conflicts.rs

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

3-
#[macro_use]
4-
mod common;
5-
63
use bdk_chain::{local_chain::LocalChain, Balance, BlockId};
74
use bdk_testenv::{block_id, hash, local_chain};
5+
use bdk_testenv::{init_graph, TxInTemplate, TxOutTemplate, TxTemplate};
86
use bitcoin::{Amount, BlockHash, OutPoint};
9-
use common::*;
107
use std::collections::{BTreeSet, HashSet};
118

129
#[allow(dead_code)]

crates/testenv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ workspace = true
1919
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false }
2020
electrsd = { version = "0.38.0", features = [ "legacy" ], default-features = false }
2121
bitcoin = { version = "0.32.0", default-features = false }
22+
rand = { version = "0.8.0", default-features = false, features = ["std", "small_rng", "std_rng"] }
2223

2324
[dev-dependencies]
2425
bdk_testenv = { path = "." }

crates/testenv/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
22

3+
pub mod tx_template;
34
pub mod utils;
5+
pub use tx_template::*;
46

57
use anyhow::Context;
68
use bdk_chain::bitcoin::{
Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,79 @@
1-
#![cfg(feature = "miniscript")]
1+
//! Transaction templates for constructing complex transaction histories for testing purposes.
22
3-
use bdk_testenv::utils::DESCRIPTORS;
4-
use rand::distributions::{Alphanumeric, DistString};
5-
use std::collections::HashMap;
6-
7-
use bdk_chain::{spk_txout::SpkTxOutIndex, tx_graph::TxGraph, Anchor, CanonicalizationParams};
3+
use crate::utils::DESCRIPTORS;
4+
use bdk_chain::{
5+
miniscript::Descriptor, spk_txout::SpkTxOutIndex, tx_graph::TxGraph, Anchor,
6+
CanonicalizationParams,
7+
};
88
use bitcoin::{
99
locktime::absolute::LockTime, secp256k1::Secp256k1, transaction, Amount, OutPoint, ScriptBuf,
1010
Sequence, Transaction, TxIn, TxOut, Txid, Witness,
1111
};
12-
use miniscript::Descriptor;
12+
use rand::distributions::{Alphanumeric, DistString};
13+
use std::collections::HashMap;
1314

14-
/// Template for creating a transaction in `TxGraph`.
15+
/// Template for creating a transaction in a [`TxGraph`].
1516
///
16-
/// The incentive for transaction templates is to create a transaction history in a simple manner to
17-
/// avoid having to explicitly hash previous transactions to form previous outpoints of later
18-
/// transactions.
17+
/// This is the main building block for constructing complex transaction histories
18+
/// for tests. It allows you to refer to previous transactions by name instead of
19+
/// manually managing txids and outpoints.
1920
#[derive(Clone, Copy, Default)]
2021
pub struct TxTemplate<'a, A> {
21-
/// Uniquely identifies the transaction, before it can have a txid.
22+
/// A unique name used to refer to this transaction in other templates.
2223
pub tx_name: &'a str,
24+
25+
/// The inputs of this transaction.
2326
pub inputs: &'a [TxInTemplate<'a>],
27+
28+
/// The outputs of this transaction.
2429
pub outputs: &'a [TxOutTemplate],
30+
31+
/// Anchors (confirmations) for this transaction.
2532
pub anchors: &'a [A],
33+
34+
/// Unix timestamp when this transaction was last seen in the mempool.
2635
pub last_seen: Option<u64>,
36+
37+
/// If `true`, this transaction will be treated as canonical regardless of
38+
/// conflict resolution rules (used for testing forced canonicalization).
2739
pub assume_canonical: bool,
2840
}
2941

42+
/// Describes how an input is created in a [`TxTemplate`].
3043
#[allow(dead_code)]
3144
pub enum TxInTemplate<'a> {
32-
/// This will give a random txid and vout.
45+
/// A random (bogus) previous output. Useful when the actual prevout doesn't matter.
3346
Bogus,
3447

35-
/// This is used for coinbase transactions because they do not have previous outputs.
48+
/// A coinbase input (no previous output).
3649
Coinbase,
3750

38-
/// Contains the `tx_name` and `vout` that we are spending. The rule is that we must only spend
39-
/// from tx of a previous `TxTemplate`.
51+
/// Spends from a previous transaction defined in the template list.
52+
///
53+
/// The rule is that the referenced transaction (`prev_name`) must appear
54+
/// earlier in the list passed to [`init_graph`].
4055
PrevTx(&'a str, usize),
4156
}
4257

58+
/// Describes an output in a [`TxTemplate`].
4359
pub struct TxOutTemplate {
60+
/// Value in satoshis.
4461
pub value: u64,
45-
pub spk_index: Option<u32>, // some = get spk from SpkTxOutIndex, none = random spk
62+
/// If `Some(index)`, the output will use the script pubkey at that index
63+
/// from the test descriptor set. If `None`, a random (empty) script is used.
64+
pub spk_index: Option<u32>,
4665
}
4766

48-
#[allow(unused)]
4967
impl TxOutTemplate {
5068
pub fn new(value: u64, spk_index: Option<u32>) -> Self {
5169
TxOutTemplate { value, spk_index }
5270
}
5371
}
5472

73+
/// The result of calling [`init_graph`].
74+
///
75+
/// Contains the built [`TxGraph`], the associated indexer, and a mapping from
76+
/// template names to their final txids.
5577
#[allow(dead_code)]
5678
pub struct TxTemplateEnv<'a, A> {
5779
pub tx_graph: TxGraph<A>,
@@ -60,14 +82,21 @@ pub struct TxTemplateEnv<'a, A> {
6082
pub canonicalization_params: CanonicalizationParams,
6183
}
6284

63-
#[allow(dead_code)]
85+
/// Builds a [`TxGraph`] (and associated indexer) from a list of [`TxTemplate`]s.
86+
///
87+
/// This is the main entry point for using transaction templates in tests.
88+
/// It handles txid generation, outpoint wiring, anchor insertion, and last-seen
89+
/// timestamps automatically.
6490
pub fn init_graph<'a, A: Anchor + Clone + 'a>(
6591
tx_templates: impl IntoIterator<Item = &'a TxTemplate<'a, A>>,
6692
) -> TxTemplateEnv<'a, A> {
6793
let (descriptor, _) =
6894
Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap();
95+
6996
let mut tx_graph = TxGraph::<A>::default();
7097
let mut indexer = SpkTxOutIndex::default();
98+
99+
// Pre-populate the indexer with 10 script pubkeys from the test descriptor
71100
(0..10).for_each(|index| {
72101
indexer.insert_spk(
73102
index,
@@ -77,9 +106,10 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
77106
.script_pubkey(),
78107
);
79108
});
80-
let mut txid_to_name = HashMap::<&'a str, Txid>::new();
81109

110+
let mut txid_to_name = HashMap::<&'a str, Txid>::new();
82111
let mut canonicalization_params = CanonicalizationParams::default();
112+
83113
for (bogus_txin_vout, tx_tmp) in tx_templates.into_iter().enumerate() {
84114
let tx = Transaction {
85115
version: transaction::Version::non_standard(0),
@@ -137,19 +167,24 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
137167
};
138168

139169
let txid = tx.compute_txid();
170+
140171
if tx_tmp.assume_canonical {
141172
canonicalization_params.assume_canonical.push(txid);
142173
}
174+
143175
txid_to_name.insert(tx_tmp.tx_name, txid);
144176
indexer.scan(&tx);
145177
let _ = tx_graph.insert_tx(tx.clone());
178+
146179
for anchor in tx_tmp.anchors.iter() {
147180
let _ = tx_graph.insert_anchor(txid, anchor.clone());
148181
}
182+
149183
if let Some(last_seen) = tx_tmp.last_seen {
150184
let _ = tx_graph.insert_seen_at(txid, last_seen);
151185
}
152186
}
187+
153188
TxTemplateEnv {
154189
tx_graph,
155190
indexer,

0 commit comments

Comments
 (0)