@@ -134,64 +134,54 @@ pub struct ElectrumUpdate {
134134
135135/// Trait to extend [`Client`] functionality.
136136pub trait ElectrumExt {
137- /// Scan the blockchain (via electrum) for the data specified and returns updates for
138- /// [`bdk_chain`] data structures.
137+ /// Full scan the keychain scripts specified with the blockchain (via an Electrum client) and
138+ /// returns updates for [`bdk_chain`] data structures.
139139 ///
140140 /// - `prev_tip`: the most recent blockchain tip present locally
141141 /// - `keychain_spks`: keychains that we want to scan transactions for
142- /// - `txids`: transactions for which we want updated [`Anchor`]s
143- /// - `outpoints`: transactions associated with these outpoints (residing, spending) that we
144- /// want to included in the update
145142 ///
146- /// The scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
143+ /// The full scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
147144 /// transactions. `batch_size` specifies the max number of script pubkeys to request for in a
148145 /// single batch request.
149- fn scan < K : Ord + Clone > (
146+ fn full_scan < K : Ord + Clone > (
150147 & self ,
151148 prev_tip : CheckPoint ,
152149 keychain_spks : BTreeMap < K , impl IntoIterator < Item = ( u32 , ScriptBuf ) > > ,
153- txids : impl IntoIterator < Item = Txid > ,
154- outpoints : impl IntoIterator < Item = OutPoint > ,
155150 stop_gap : usize ,
156151 batch_size : usize ,
157152 ) -> Result < ( ElectrumUpdate , BTreeMap < K , u32 > ) , Error > ;
158153
159- /// Convenience method to call [`scan`] without requiring a keychain.
154+ /// Sync a set of scripts with the blockchain (via an Electrum client) for the data specified
155+ /// and returns updates for [`bdk_chain`] data structures.
160156 ///
161- /// [`scan`]: ElectrumExt::scan
162- fn scan_without_keychain (
157+ /// - `prev_tip`: the most recent blockchain tip present locally
158+ /// - `misc_spks`: an iterator of scripts we want to sync transactions for
159+ /// - `txids`: transactions for which we want updated [`Anchor`]s
160+ /// - `outpoints`: transactions associated with these outpoints (residing, spending) that we
161+ /// want to include in the update
162+ ///
163+ /// `batch_size` specifies the max number of script pubkeys to request for in a single batch
164+ /// request.
165+ ///
166+ /// If the scripts to sync are unknown, such as when restoring or importing a keychain that
167+ /// may include scripts that have been used, use [`full_scan`] with the keychain.
168+ ///
169+ /// [`full_scan`]: ElectrumExt::full_scan
170+ fn sync (
163171 & self ,
164172 prev_tip : CheckPoint ,
165173 misc_spks : impl IntoIterator < Item = ScriptBuf > ,
166174 txids : impl IntoIterator < Item = Txid > ,
167175 outpoints : impl IntoIterator < Item = OutPoint > ,
168176 batch_size : usize ,
169- ) -> Result < ElectrumUpdate , Error > {
170- let spk_iter = misc_spks
171- . into_iter ( )
172- . enumerate ( )
173- . map ( |( i, spk) | ( i as u32 , spk) ) ;
174-
175- let ( electrum_update, _) = self . scan (
176- prev_tip,
177- [ ( ( ) , spk_iter) ] . into ( ) ,
178- txids,
179- outpoints,
180- usize:: MAX ,
181- batch_size,
182- ) ?;
183-
184- Ok ( electrum_update)
185- }
177+ ) -> Result < ElectrumUpdate , Error > ;
186178}
187179
188180impl ElectrumExt for Client {
189- fn scan < K : Ord + Clone > (
181+ fn full_scan < K : Ord + Clone > (
190182 & self ,
191183 prev_tip : CheckPoint ,
192184 keychain_spks : BTreeMap < K , impl IntoIterator < Item = ( u32 , ScriptBuf ) > > ,
193- txids : impl IntoIterator < Item = Txid > ,
194- outpoints : impl IntoIterator < Item = OutPoint > ,
195185 stop_gap : usize ,
196186 batch_size : usize ,
197187 ) -> Result < ( ElectrumUpdate , BTreeMap < K , u32 > ) , Error > {
@@ -201,9 +191,6 @@ impl ElectrumExt for Client {
201191 . collect :: < BTreeMap < K , _ > > ( ) ;
202192 let mut scanned_spks = BTreeMap :: < ( K , u32 ) , ( ScriptBuf , bool ) > :: new ( ) ;
203193
204- let txids = txids. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
205- let outpoints = outpoints. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
206-
207194 let ( electrum_update, keychain_update) = loop {
208195 let ( tip, _) = construct_update_tip ( self , prev_tip. clone ( ) ) ?;
209196 let mut relevant_txids = RelevantTxids :: default ( ) ;
@@ -242,15 +229,6 @@ impl ElectrumExt for Client {
242229 }
243230 }
244231
245- populate_with_txids ( self , & cps, & mut relevant_txids, & mut txids. iter ( ) . cloned ( ) ) ?;
246-
247- let _txs = populate_with_outpoints (
248- self ,
249- & cps,
250- & mut relevant_txids,
251- & mut outpoints. iter ( ) . cloned ( ) ,
252- ) ?;
253-
254232 // check for reorgs during scan process
255233 let server_blockhash = self . block_header ( tip. height ( ) as usize ) ?. block_hash ( ) ;
256234 if tip. hash ( ) != server_blockhash {
@@ -284,6 +262,41 @@ impl ElectrumExt for Client {
284262
285263 Ok ( ( electrum_update, keychain_update) )
286264 }
265+
266+ fn sync (
267+ & self ,
268+ prev_tip : CheckPoint ,
269+ misc_spks : impl IntoIterator < Item = ScriptBuf > ,
270+ txids : impl IntoIterator < Item = Txid > ,
271+ outpoints : impl IntoIterator < Item = OutPoint > ,
272+ batch_size : usize ,
273+ ) -> Result < ElectrumUpdate , Error > {
274+ let spk_iter = misc_spks
275+ . into_iter ( )
276+ . enumerate ( )
277+ . map ( |( i, spk) | ( i as u32 , spk) ) ;
278+
279+ let ( mut electrum_update, _) = self . full_scan (
280+ prev_tip. clone ( ) ,
281+ [ ( ( ) , spk_iter) ] . into ( ) ,
282+ usize:: MAX ,
283+ batch_size,
284+ ) ?;
285+
286+ let ( tip, _) = construct_update_tip ( self , prev_tip) ?;
287+ let cps = tip
288+ . iter ( )
289+ . take ( 10 )
290+ . map ( |cp| ( cp. height ( ) , cp) )
291+ . collect :: < BTreeMap < u32 , CheckPoint > > ( ) ;
292+
293+ populate_with_txids ( self , & cps, & mut electrum_update. relevant_txids , txids) ?;
294+
295+ let _txs =
296+ populate_with_outpoints ( self , & cps, & mut electrum_update. relevant_txids , outpoints) ?;
297+
298+ Ok ( electrum_update)
299+ }
287300}
288301
289302/// Return a [`CheckPoint`] of the latest tip, that connects with `prev_tip`.
@@ -405,7 +418,7 @@ fn populate_with_outpoints(
405418 client : & Client ,
406419 cps : & BTreeMap < u32 , CheckPoint > ,
407420 relevant_txids : & mut RelevantTxids ,
408- outpoints : & mut impl Iterator < Item = OutPoint > ,
421+ outpoints : impl IntoIterator < Item = OutPoint > ,
409422) -> Result < HashMap < Txid , Transaction > , Error > {
410423 let mut full_txs = HashMap :: new ( ) ;
411424 for outpoint in outpoints {
@@ -466,7 +479,7 @@ fn populate_with_txids(
466479 client : & Client ,
467480 cps : & BTreeMap < u32 , CheckPoint > ,
468481 relevant_txids : & mut RelevantTxids ,
469- txids : & mut impl Iterator < Item = Txid > ,
482+ txids : impl IntoIterator < Item = Txid > ,
470483) -> Result < ( ) , Error > {
471484 for txid in txids {
472485 let tx = match client. transaction_get ( & txid) {
0 commit comments