@@ -11,9 +11,10 @@ use spaces_wallet::bdk_wallet::{KeychainKind, Update};
1111use spaces_wallet:: bitcoin:: bip158:: BlockFilter ;
1212use spaces_wallet:: bitcoin:: ScriptBuf ;
1313use spaces_wallet:: SpacesWallet ;
14+ use crate :: calc_progress;
1415use crate :: client:: { BlockSource , BlockchainInfo } ;
1516use crate :: source:: BitcoinBlockSource ;
16- use crate :: wallets:: WalletProgressUpdate ;
17+ use crate :: wallets:: { WalletStatus , WalletProgressUpdate } ;
1718
1819pub struct CompactFilterSync {
1920 graph : IndexedTxGraph < ConfirmationBlockTime , KeychainTxOutIndex < KeychainKind > > ,
@@ -29,7 +30,7 @@ pub struct CompactFilterSync {
2930 total_filters : u32 ,
3031 wait : Option < Instant > ,
3132 state : SyncState ,
32- filters_queued : bool
33+ filters_queued : bool ,
3334}
3435
3536enum SyncState {
@@ -136,7 +137,7 @@ impl CompactFilterSync {
136137 }
137138 if info. headers != info. blocks {
138139 info ! ( "Source still syncing, retrying..." ) ;
139- * progress = WalletProgressUpdate :: Syncing ;
140+ * progress = WalletProgressUpdate :: new ( WalletStatus :: Syncing , None ) ;
140141 self . wait = Some ( Instant :: now ( ) ) ;
141142 return Ok ( ( ) ) ;
142143 }
@@ -147,10 +148,16 @@ impl CompactFilterSync {
147148 }
148149
149150 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+ ) ) ;
154161 self . wait = Some ( Instant :: now ( ) ) ;
155162 return Ok ( ( ) ) ;
156163 }
@@ -205,10 +212,12 @@ impl CompactFilterSync {
205212 } else {
206213 info ! ( "wallet({}) processed block filter {} - no match" , wallet. name( ) , height) ;
207214 }
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+ ) ;
212221 }
213222 SyncState :: QueueBlocks => {
214223 if !self . queued_blocks . is_empty ( ) {
@@ -231,12 +240,12 @@ impl CompactFilterSync {
231240 // The client has a global state for pending blocks in the queue
232241 // so we cap it just in case other things are queuing blocks
233242 // 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+ ) ;
240249 self . wait = Some ( Instant :: now ( ) ) ;
241250 return Ok ( ( ) ) ;
242251 }
@@ -251,7 +260,7 @@ impl CompactFilterSync {
251260 SyncState :: ProcessBlocks => {
252261 let ( height, hash) = match self . queued_blocks . pop_first ( ) {
253262 None => {
254- * progress = WalletProgressUpdate :: CbfApplyUpdate ;
263+ * progress = WalletProgressUpdate :: new ( WalletStatus :: CbfApplyUpdate , None ) ;
255264 self . state = SyncState :: ApplyUpdate ;
256265 return Ok ( ( ) ) ;
257266 }
@@ -262,10 +271,11 @@ impl CompactFilterSync {
262271 . ok_or ( anyhow ! ( "block {} {} not found" , height, hash) ) ?;
263272 self . chain_changeset . insert ( height, Some ( hash) ) ;
264273 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+ ) ;
269279 }
270280 SyncState :: ApplyUpdate => {
271281 info ! ( "wallet({}): updating wallet tip to {}" , wallet. name( ) , self . filters_tip) ;
@@ -280,9 +290,9 @@ impl CompactFilterSync {
280290 info ! ( "wallet({}): compact filter sync portion complete at {}" , wallet. name( ) , self . filters_tip) ;
281291 self . state = SyncState :: Synced ;
282292 // Only CBF portion is done
283- * progress = WalletProgressUpdate :: Syncing
293+ * progress = WalletProgressUpdate :: new ( WalletStatus :: Syncing , None ) ;
284294 }
285- SyncState :: Synced => { } ,
295+ SyncState :: Synced => { }
286296 }
287297 Ok ( ( ) )
288298 }
0 commit comments