@@ -4,7 +4,7 @@ use bdk_testenv::utils::DESCRIPTORS;
4
4
use rand:: distributions:: { Alphanumeric , DistString } ;
5
5
use std:: collections:: HashMap ;
6
6
7
- use bdk_chain:: { spk_txout:: SpkTxOutIndex , tx_graph:: TxGraph , Anchor } ;
7
+ use bdk_chain:: { spk_txout:: SpkTxOutIndex , tx_graph:: TxGraph , Anchor , CanonicalizationMods } ;
8
8
use bitcoin:: {
9
9
locktime:: absolute:: LockTime , secp256k1:: Secp256k1 , transaction, Amount , OutPoint , ScriptBuf ,
10
10
Sequence , Transaction , TxIn , TxOut , Txid , Witness ,
@@ -24,6 +24,7 @@ pub struct TxTemplate<'a, A> {
24
24
pub outputs : & ' a [ TxOutTemplate ] ,
25
25
pub anchors : & ' a [ A ] ,
26
26
pub last_seen : Option < u64 > ,
27
+ pub assume_canonical : bool ,
27
28
}
28
29
29
30
#[ allow( dead_code) ]
@@ -51,25 +52,34 @@ impl TxOutTemplate {
51
52
}
52
53
}
53
54
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
+
54
63
#[ allow( dead_code) ]
55
64
pub fn init_graph < ' a , A : Anchor + Clone + ' a > (
56
65
tx_templates : impl IntoIterator < Item = & ' a TxTemplate < ' a , A > > ,
57
- ) -> ( TxGraph < A > , SpkTxOutIndex < u32 > , HashMap < & ' a str , Txid > ) {
66
+ ) -> TxTemplateEnv < ' a , A > {
58
67
let ( descriptor, _) =
59
68
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 ( ) ;
62
71
( 0 ..10 ) . for_each ( |index| {
63
- spk_index . insert_spk (
72
+ indexer . insert_spk (
64
73
index,
65
74
descriptor
66
75
. at_derivation_index ( index)
67
76
. unwrap ( )
68
77
. script_pubkey ( ) ,
69
78
) ;
70
79
} ) ;
71
- let mut tx_ids = HashMap :: < & ' a str , Txid > :: new ( ) ;
80
+ let mut txid_to_name = HashMap :: < & ' a str , Txid > :: new ( ) ;
72
81
82
+ let mut canonicalization_mods = CanonicalizationMods :: default ( ) ;
73
83
for ( bogus_txin_vout, tx_tmp) in tx_templates. into_iter ( ) . enumerate ( ) {
74
84
let tx = Transaction {
75
85
version : transaction:: Version :: non_standard ( 0 ) ,
@@ -98,7 +108,7 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
98
108
witness : Witness :: new ( ) ,
99
109
} ,
100
110
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 (
102
112
"txin template must spend from tx of template that comes before" ,
103
113
) ;
104
114
TxIn {
@@ -120,21 +130,30 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
120
130
} ,
121
131
Some ( index) => TxOut {
122
132
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 ( ) ,
124
134
} ,
125
135
} )
126
136
. collect ( ) ,
127
137
} ;
128
138
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 ( ) ) ;
132
146
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 ( ) ) ;
134
148
}
135
149
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) ;
137
151
}
138
152
}
139
- ( graph, spk_index, tx_ids)
153
+ TxTemplateEnv {
154
+ tx_graph,
155
+ indexer,
156
+ txid_to_name,
157
+ canonicalization_mods,
158
+ }
140
159
}
0 commit comments