@@ -5,7 +5,7 @@ use alloc::boxed::Box;
5
5
use alloc:: collections:: BTreeSet ;
6
6
use alloc:: sync:: Arc ;
7
7
use alloc:: vec:: Vec ;
8
- use bdk_core:: BlockId ;
8
+ use bdk_core:: { BlockId , ChainQuery } ;
9
9
use bitcoin:: { Transaction , Txid } ;
10
10
11
11
type CanonicalMap < A > = HashMap < Txid , ( Arc < Transaction > , CanonicalReason < A > ) > ;
@@ -53,53 +53,13 @@ pub struct CanonicalizationTask<'g, A> {
53
53
confirmed_anchors : HashMap < Txid , A > ,
54
54
}
55
55
56
- impl < ' g , A : Anchor > CanonicalizationTask < ' g , A > {
57
- /// Creates a new canonicalization task.
58
- pub fn new ( tx_graph : & ' g TxGraph < A > , params : CanonicalizationParams ) -> Self {
59
- let anchors = tx_graph. all_anchors ( ) ;
60
- let unprocessed_assumed_txs = Box :: new (
61
- params
62
- . assume_canonical
63
- . into_iter ( )
64
- . rev ( )
65
- . filter_map ( |txid| Some ( ( txid, tx_graph. get_tx ( txid) ?) ) ) ,
66
- ) ;
67
- let unprocessed_anchored_txs = Box :: new (
68
- tx_graph
69
- . txids_by_descending_anchor_height ( )
70
- . filter_map ( |( _, txid) | Some ( ( txid, tx_graph. get_tx ( txid) ?, anchors. get ( & txid) ?) ) ) ,
71
- ) ;
72
- let unprocessed_seen_txs = Box :: new (
73
- tx_graph
74
- . txids_by_descending_last_seen ( )
75
- . filter_map ( |( last_seen, txid) | Some ( ( txid, tx_graph. get_tx ( txid) ?, last_seen) ) ) ,
76
- ) ;
56
+ impl < ' g , A : Anchor > ChainQuery for CanonicalizationTask < ' g , A > {
57
+ type Request = CanonicalizationRequest < A > ;
58
+ type Response = CanonicalizationResponse < A > ;
59
+ type Context = BlockId ;
60
+ type Result = CanonicalView < A > ;
77
61
78
- let mut task = Self {
79
- tx_graph,
80
-
81
- unprocessed_assumed_txs,
82
- unprocessed_anchored_txs,
83
- unprocessed_seen_txs,
84
- unprocessed_leftover_txs : VecDeque :: new ( ) ,
85
-
86
- canonical : HashMap :: new ( ) ,
87
- not_canonical : HashSet :: new ( ) ,
88
-
89
- pending_anchor_checks : VecDeque :: new ( ) ,
90
-
91
- canonical_order : Vec :: new ( ) ,
92
- confirmed_anchors : HashMap :: new ( ) ,
93
- } ;
94
-
95
- // process assumed transactions first (they don't need queries)
96
- task. process_assumed_txs ( ) ;
97
-
98
- task
99
- }
100
-
101
- /// Returns the next query needed, if any.
102
- pub fn next_query ( & mut self ) -> Option < CanonicalizationRequest < A > > {
62
+ fn next_query ( & mut self ) -> Option < Self :: Request > {
103
63
// Check if we have pending anchor checks
104
64
if let Some ( ( _, _, anchors) ) = self . pending_anchor_checks . front ( ) {
105
65
return Some ( CanonicalizationRequest {
@@ -111,8 +71,7 @@ impl<'g, A: Anchor> CanonicalizationTask<'g, A> {
111
71
self . process_anchored_txs ( )
112
72
}
113
73
114
- /// Resolves a query with the given response.
115
- pub fn resolve_query ( & mut self , response : CanonicalizationResponse < A > ) {
74
+ fn resolve_query ( & mut self , response : Self :: Response ) {
116
75
if let Some ( ( txid, tx, anchors) ) = self . pending_anchor_checks . pop_front ( ) {
117
76
match response {
118
77
Some ( best_anchor) => {
@@ -138,13 +97,11 @@ impl<'g, A: Anchor> CanonicalizationTask<'g, A> {
138
97
}
139
98
}
140
99
141
- /// Returns true if the canonicalization process is complete.
142
- pub fn is_finished ( & self ) -> bool {
100
+ fn is_finished ( & mut self ) -> bool {
143
101
self . pending_anchor_checks . is_empty ( ) && self . unprocessed_anchored_txs . size_hint ( ) . 0 == 0
144
102
}
145
103
146
- /// Completes the canonicalization and returns a CanonicalView.
147
- pub fn finish ( mut self , chain_tip : BlockId ) -> CanonicalView < A > {
104
+ fn finish ( mut self , context : Self :: Context ) -> Self :: Result {
148
105
// Process remaining transactions (seen and leftover)
149
106
self . process_seen_txs ( ) ;
150
107
self . process_leftover_txs ( ) ;
@@ -224,7 +181,53 @@ impl<'g, A: Anchor> CanonicalizationTask<'g, A> {
224
181
}
225
182
}
226
183
227
- CanonicalView :: new ( chain_tip, view_order, view_txs, view_spends)
184
+ CanonicalView :: new ( context, view_order, view_txs, view_spends)
185
+ }
186
+ }
187
+
188
+ impl < ' g , A : Anchor > CanonicalizationTask < ' g , A > {
189
+ /// Creates a new canonicalization task.
190
+ pub fn new ( tx_graph : & ' g TxGraph < A > , params : CanonicalizationParams ) -> Self {
191
+ let anchors = tx_graph. all_anchors ( ) ;
192
+ let unprocessed_assumed_txs = Box :: new (
193
+ params
194
+ . assume_canonical
195
+ . into_iter ( )
196
+ . rev ( )
197
+ . filter_map ( |txid| Some ( ( txid, tx_graph. get_tx ( txid) ?) ) ) ,
198
+ ) ;
199
+ let unprocessed_anchored_txs = Box :: new (
200
+ tx_graph
201
+ . txids_by_descending_anchor_height ( )
202
+ . filter_map ( |( _, txid) | Some ( ( txid, tx_graph. get_tx ( txid) ?, anchors. get ( & txid) ?) ) ) ,
203
+ ) ;
204
+ let unprocessed_seen_txs = Box :: new (
205
+ tx_graph
206
+ . txids_by_descending_last_seen ( )
207
+ . filter_map ( |( last_seen, txid) | Some ( ( txid, tx_graph. get_tx ( txid) ?, last_seen) ) ) ,
208
+ ) ;
209
+
210
+ let mut task = Self {
211
+ tx_graph,
212
+
213
+ unprocessed_assumed_txs,
214
+ unprocessed_anchored_txs,
215
+ unprocessed_seen_txs,
216
+ unprocessed_leftover_txs : VecDeque :: new ( ) ,
217
+
218
+ canonical : HashMap :: new ( ) ,
219
+ not_canonical : HashSet :: new ( ) ,
220
+
221
+ pending_anchor_checks : VecDeque :: new ( ) ,
222
+
223
+ canonical_order : Vec :: new ( ) ,
224
+ confirmed_anchors : HashMap :: new ( ) ,
225
+ } ;
226
+
227
+ // process assumed transactions first (they don't need queries)
228
+ task. process_assumed_txs ( ) ;
229
+
230
+ task
228
231
}
229
232
230
233
fn is_canonicalized ( & self , txid : Txid ) -> bool {
0 commit comments