@@ -87,12 +87,21 @@ pub trait RpcApiExt: RpcApi {
8787 fn wait_wallet_scan (
8888 & self ,
8989 progress_tx : Option < mpsc:: Sender < Progress > > ,
90+ shutdown_rx : Option < mpsc:: Receiver < ( ) > > ,
9091 interval : time:: Duration ,
91- mut wait_for_scanning : bool ,
9292 ) -> RpcResult < json:: GetWalletInfoResult > {
93- let start = time:: Instant :: now ( ) ;
93+ // Stop if the shutdown signal was received or if the channel was disconnected
94+ let should_shutdown = || {
95+ shutdown_rx
96+ . as_ref ( )
97+ . map_or ( false , |rx| rx. try_recv ( ) != Err ( mpsc:: TryRecvError :: Empty ) )
98+ } ;
99+
94100 Ok ( loop {
95101 let info = self . get_wallet_info ( ) ?;
102+ if should_shutdown ( ) {
103+ break info;
104+ }
96105 match info. scanning {
97106 None => {
98107 warn ! ( "Your bitcoin node does not report the `scanning` status in `getwalletinfo`. It is recommended to upgrade to Bitcoin Core v0.19+ to enable this." ) ;
@@ -109,36 +118,39 @@ pub trait RpcApiExt: RpcApi {
109118 break info;
110119 }
111120 }
112- // wait_wallet_scan() could be called before scanning actually started ,
113- // give it a few seconds to start up before giving up
114- if !wait_for_scanning || start . elapsed ( ) . as_secs ( ) > 3 {
121+ // Stop as soon as scanning is completed if no explicit shutdown_rx was given ,
122+ // or continue until the shutdown signal is received if it was.
123+ if shutdown_rx . is_none ( ) {
115124 break info;
116125 }
117126 }
118- Some ( ScanningDetails :: Scanning { progress , duration } ) => {
119- wait_for_scanning = false ;
120- let duration = duration as u64 ;
121- let progress_n = progress as f32 ;
127+ Some ( ScanningDetails :: Scanning {
128+ progress : progress_n ,
129+ duration,
130+ } ) => {
122131 let eta = if progress_n > 0.0 {
123- ( duration as f32 / progress_n) as u64 - duration
132+ ( duration as f32 / progress_n) as u64 - duration as u64
124133 } else {
125134 0
126135 } ;
127136
128- info ! ( target: "bwt" ,
129- "waiting for bitcoind to finish scanning [done {:.1}%, running for {}m, eta {}m]" ,
130- progress_n * 100.0 , duration / 60 , eta / 60
131- ) ;
132-
133137 if let Some ( ref progress_tx) = progress_tx {
134138 let progress = Progress :: Scan { progress_n, eta } ;
135139 if progress_tx. send ( progress) . is_err ( ) {
136140 break info;
137141 }
142+ } else {
143+ info ! ( target: "bwt" ,
144+ "waiting for bitcoind to finish scanning [done {:.1}%, running for {}m, eta {}m]" ,
145+ progress_n * 100.0 , duration / 60 , eta / 60
146+ ) ;
138147 }
139148 }
140- } ;
149+ }
141150 thread:: sleep ( interval) ;
151+ if should_shutdown ( ) {
152+ break info;
153+ }
142154 } )
143155 }
144156}
0 commit comments