@@ -4,7 +4,7 @@ use bdk_testenv::utils::DESCRIPTORS;
44use rand:: distributions:: { Alphanumeric , DistString } ;
55use 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 } ;
88use 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) ]
5564pub 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}
0 commit comments