@@ -11,9 +11,10 @@ use spaces_wallet::bdk_wallet::{KeychainKind, Update};
11
11
use spaces_wallet:: bitcoin:: bip158:: BlockFilter ;
12
12
use spaces_wallet:: bitcoin:: ScriptBuf ;
13
13
use spaces_wallet:: SpacesWallet ;
14
+ use crate :: calc_progress;
14
15
use crate :: client:: { BlockSource , BlockchainInfo } ;
15
16
use crate :: source:: BitcoinBlockSource ;
16
- use crate :: wallets:: WalletProgressUpdate ;
17
+ use crate :: wallets:: { WalletStatus , WalletProgressUpdate } ;
17
18
18
19
pub struct CompactFilterSync {
19
20
graph : IndexedTxGraph < ConfirmationBlockTime , KeychainTxOutIndex < KeychainKind > > ,
@@ -29,7 +30,7 @@ pub struct CompactFilterSync {
29
30
total_filters : u32 ,
30
31
wait : Option < Instant > ,
31
32
state : SyncState ,
32
- filters_queued : bool
33
+ filters_queued : bool ,
33
34
}
34
35
35
36
enum SyncState {
@@ -136,7 +137,7 @@ impl CompactFilterSync {
136
137
}
137
138
if info. headers != info. blocks {
138
139
info ! ( "Source still syncing, retrying..." ) ;
139
- * progress = WalletProgressUpdate :: Syncing ;
140
+ * progress = WalletProgressUpdate :: new ( WalletStatus :: Syncing , None ) ;
140
141
self . wait = Some ( Instant :: now ( ) ) ;
141
142
return Ok ( ( ) ) ;
142
143
}
@@ -147,10 +148,16 @@ impl CompactFilterSync {
147
148
}
148
149
149
150
info ! ( "Filters syncing, retrying..." ) ;
150
- * progress = WalletProgressUpdate :: CbfFilterSync {
151
- total : info. filter_headers . unwrap_or ( 0 ) ,
152
- completed : info. filters . unwrap_or ( 0 ) ,
153
- } ;
151
+ * progress = WalletProgressUpdate :: new ( WalletStatus :: CbfFilterSync , Some (
152
+ calc_progress (
153
+ info. checkpoint . map ( |c| c. height ) . unwrap_or ( 0 ) ,
154
+ info. filters . unwrap_or ( 0 ) ,
155
+ std:: cmp:: max (
156
+ info. prune_height . unwrap_or ( 0 ) ,
157
+ info. filter_headers . unwrap_or ( 0 )
158
+ ) ,
159
+ )
160
+ ) ) ;
154
161
self . wait = Some ( Instant :: now ( ) ) ;
155
162
return Ok ( ( ) ) ;
156
163
}
@@ -205,10 +212,12 @@ impl CompactFilterSync {
205
212
} else {
206
213
info ! ( "wallet({}) processed block filter {} - no match" , wallet. name( ) , height) ;
207
214
}
208
- * progress = WalletProgressUpdate :: CbfProcessFilters {
209
- total : self . total_filters ,
210
- completed : self . total_filters - self . queued_filters . len ( ) as u32 ,
211
- } ;
215
+
216
+ let completed = self . total_filters as f32 - self . queued_filters . len ( ) as f32 ;
217
+ * progress = WalletProgressUpdate :: new (
218
+ WalletStatus :: CbfProcessFilters ,
219
+ Some ( completed / self . total_filters as f32 )
220
+ ) ;
212
221
}
213
222
SyncState :: QueueBlocks => {
214
223
if !self . queued_blocks . is_empty ( ) {
@@ -231,12 +240,12 @@ impl CompactFilterSync {
231
240
// The client has a global state for pending blocks in the queue
232
241
// so we cap it just in case other things are queuing blocks
233
242
// at the same time
234
- let pending = std:: cmp:: min ( status. pending , self . block_matches ) ;
235
- * progress = WalletProgressUpdate :: CbfDownloadMatchingBlocks {
236
- total : self . block_matches ,
237
- completed : self . block_matches - pending ,
238
- } ;
239
-
243
+ let pending = std:: cmp:: min ( status. pending , self . block_matches ) as f32 ;
244
+ let completed = self . block_matches as f32 - pending ;
245
+ * progress = WalletProgressUpdate :: new (
246
+ WalletStatus :: CbfDownloadMatchingBlocks ,
247
+ Some ( completed / self . block_matches as f32 )
248
+ ) ;
240
249
self . wait = Some ( Instant :: now ( ) ) ;
241
250
return Ok ( ( ) ) ;
242
251
}
@@ -251,7 +260,7 @@ impl CompactFilterSync {
251
260
SyncState :: ProcessBlocks => {
252
261
let ( height, hash) = match self . queued_blocks . pop_first ( ) {
253
262
None => {
254
- * progress = WalletProgressUpdate :: CbfApplyUpdate ;
263
+ * progress = WalletProgressUpdate :: new ( WalletStatus :: CbfApplyUpdate , None ) ;
255
264
self . state = SyncState :: ApplyUpdate ;
256
265
return Ok ( ( ) ) ;
257
266
}
@@ -262,10 +271,11 @@ impl CompactFilterSync {
262
271
. ok_or ( anyhow ! ( "block {} {} not found" , height, hash) ) ?;
263
272
self . chain_changeset . insert ( height, Some ( hash) ) ;
264
273
let _ = self . graph . apply_block_relevant ( & block, height) ;
265
- * progress = WalletProgressUpdate :: CbfProcessMatchingBlocks {
266
- total : self . block_matches ,
267
- completed : self . block_matches - self . queued_blocks . len ( ) as u32 ,
268
- } ;
274
+ let completed = self . block_matches - self . queued_blocks . len ( ) as u32 ;
275
+ * progress = WalletProgressUpdate :: new (
276
+ WalletStatus :: CbfProcessMatchingBlocks ,
277
+ Some ( completed as f32 / self . block_matches as f32 )
278
+ ) ;
269
279
}
270
280
SyncState :: ApplyUpdate => {
271
281
info ! ( "wallet({}): updating wallet tip to {}" , wallet. name( ) , self . filters_tip) ;
@@ -280,9 +290,9 @@ impl CompactFilterSync {
280
290
info ! ( "wallet({}): compact filter sync portion complete at {}" , wallet. name( ) , self . filters_tip) ;
281
291
self . state = SyncState :: Synced ;
282
292
// Only CBF portion is done
283
- * progress = WalletProgressUpdate :: Syncing
293
+ * progress = WalletProgressUpdate :: new ( WalletStatus :: Syncing , None ) ;
284
294
}
285
- SyncState :: Synced => { } ,
295
+ SyncState :: Synced => { }
286
296
}
287
297
Ok ( ( ) )
288
298
}
0 commit comments