Skip to content

Commit 4169060

Browse files
committed
text(chain,wallet): Test assume_canonical mod
Also change `TxTemplate` API to allow for testing with `assume_canonical`.
1 parent 3567fb0 commit 4169060

File tree

5 files changed

+228
-39
lines changed

5 files changed

+228
-39
lines changed

crates/chain/benches/canonicalization.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, Lo
9191
}
9292

9393
fn run_list_canonical_txs(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txs: usize) {
94-
let txs = tx_graph
95-
.graph()
96-
.list_canonical_txs(chain, chain.tip().block_id(), CanonicalizationMods::NONE);
94+
let txs = tx_graph.graph().list_canonical_txs(
95+
chain,
96+
chain.tip().block_id(),
97+
CanonicalizationMods::NONE,
98+
);
9799
assert_eq!(txs.count(), exp_txs);
98100
}
99101

crates/chain/tests/common/tx_template.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bdk_testenv::utils::DESCRIPTORS;
44
use rand::distributions::{Alphanumeric, DistString};
55
use std::collections::HashMap;
66

7-
use bdk_chain::{spk_txout::SpkTxOutIndex, tx_graph::TxGraph, Anchor};
7+
use bdk_chain::{spk_txout::SpkTxOutIndex, tx_graph::TxGraph, Anchor, CanonicalizationMods};
88
use bitcoin::{
99
locktime::absolute::LockTime, secp256k1::Secp256k1, transaction, Amount, OutPoint, ScriptBuf,
1010
Sequence, Transaction, TxIn, TxOut, Txid, Witness,
@@ -24,6 +24,7 @@ pub struct TxTemplate<'a, A> {
2424
pub outputs: &'a [TxOutTemplate],
2525
pub anchors: &'a [A],
2626
pub last_seen: Option<u64>,
27+
pub assume_canonical: bool,
2728
}
2829

2930
#[allow(dead_code)]
@@ -51,25 +52,34 @@ impl TxOutTemplate {
5152
}
5253
}
5354

55+
#[allow(dead_code)]
56+
pub struct TxTemplateEnv<'a, A> {
57+
pub tx_graph: TxGraph<A>,
58+
pub indexer: SpkTxOutIndex<u32>,
59+
pub txid_to_name: HashMap<&'a str, Txid>,
60+
pub canonicalization_mods: CanonicalizationMods,
61+
}
62+
5463
#[allow(dead_code)]
5564
pub fn init_graph<'a, A: Anchor + Clone + 'a>(
5665
tx_templates: impl IntoIterator<Item = &'a TxTemplate<'a, A>>,
57-
) -> (TxGraph<A>, SpkTxOutIndex<u32>, HashMap<&'a str, Txid>) {
66+
) -> TxTemplateEnv<'a, A> {
5867
let (descriptor, _) =
5968
Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap();
60-
let mut graph = TxGraph::<A>::default();
61-
let mut spk_index = SpkTxOutIndex::default();
69+
let mut tx_graph = TxGraph::<A>::default();
70+
let mut indexer = SpkTxOutIndex::default();
6271
(0..10).for_each(|index| {
63-
spk_index.insert_spk(
72+
indexer.insert_spk(
6473
index,
6574
descriptor
6675
.at_derivation_index(index)
6776
.unwrap()
6877
.script_pubkey(),
6978
);
7079
});
71-
let mut tx_ids = HashMap::<&'a str, Txid>::new();
80+
let mut txid_to_name = HashMap::<&'a str, Txid>::new();
7281

82+
let mut canonicalization_mods = CanonicalizationMods::default();
7383
for (bogus_txin_vout, tx_tmp) in tx_templates.into_iter().enumerate() {
7484
let tx = Transaction {
7585
version: transaction::Version::non_standard(0),
@@ -98,7 +108,7 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
98108
witness: Witness::new(),
99109
},
100110
TxInTemplate::PrevTx(prev_name, prev_vout) => {
101-
let prev_txid = tx_ids.get(prev_name).expect(
111+
let prev_txid = txid_to_name.get(prev_name).expect(
102112
"txin template must spend from tx of template that comes before",
103113
);
104114
TxIn {
@@ -120,21 +130,30 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
120130
},
121131
Some(index) => TxOut {
122132
value: Amount::from_sat(output.value),
123-
script_pubkey: spk_index.spk_at_index(index).unwrap(),
133+
script_pubkey: indexer.spk_at_index(index).unwrap(),
124134
},
125135
})
126136
.collect(),
127137
};
128138

129-
tx_ids.insert(tx_tmp.tx_name, tx.compute_txid());
130-
spk_index.scan(&tx);
131-
let _ = graph.insert_tx(tx.clone());
139+
let txid = tx.compute_txid();
140+
if tx_tmp.assume_canonical {
141+
canonicalization_mods.assume_canonical.push(txid);
142+
}
143+
txid_to_name.insert(tx_tmp.tx_name, txid);
144+
indexer.scan(&tx);
145+
let _ = tx_graph.insert_tx(tx.clone());
132146
for anchor in tx_tmp.anchors.iter() {
133-
let _ = graph.insert_anchor(tx.compute_txid(), anchor.clone());
147+
let _ = tx_graph.insert_anchor(txid, anchor.clone());
134148
}
135149
if let Some(last_seen) = tx_tmp.last_seen {
136-
let _ = graph.insert_seen_at(tx.compute_txid(), last_seen);
150+
let _ = tx_graph.insert_seen_at(txid, last_seen);
137151
}
138152
}
139-
(graph, spk_index, tx_ids)
153+
TxTemplateEnv {
154+
tx_graph,
155+
indexer,
156+
txid_to_name,
157+
canonicalization_mods,
158+
}
140159
}

crates/chain/tests/test_tx_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ fn call_map_anchors_with_non_deterministic_anchor() {
12111211
outputs: &[TxOutTemplate::new(10000, Some(1))],
12121212
anchors: &[block_id!(1, "A")],
12131213
last_seen: None,
1214+
..Default::default()
12141215
},
12151216
TxTemplate {
12161217
tx_name: "tx2",
@@ -1227,7 +1228,7 @@ fn call_map_anchors_with_non_deterministic_anchor() {
12271228
..Default::default()
12281229
},
12291230
];
1230-
let (graph, _, _) = init_graph(&template);
1231+
let graph = init_graph(&template).tx_graph;
12311232
let new_graph = graph.clone().map_anchors(|a| NonDeterministicAnchor {
12321233
anchor_block: a,
12331234
// A non-deterministic value

0 commit comments

Comments
 (0)