@@ -87,12 +87,21 @@ pub trait RpcApiExt: RpcApi {
87
87
fn wait_wallet_scan (
88
88
& self ,
89
89
progress_tx : Option < mpsc:: Sender < Progress > > ,
90
+ shutdown_rx : Option < mpsc:: Receiver < ( ) > > ,
90
91
interval : time:: Duration ,
91
- mut wait_for_scanning : bool ,
92
92
) -> 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
+
94
100
Ok ( loop {
95
101
let info = self . get_wallet_info ( ) ?;
102
+ if should_shutdown ( ) {
103
+ break info;
104
+ }
96
105
match info. scanning {
97
106
None => {
98
107
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 {
109
118
break info;
110
119
}
111
120
}
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 ( ) {
115
124
break info;
116
125
}
117
126
}
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
+ } ) => {
122
131
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
124
133
} else {
125
134
0
126
135
} ;
127
136
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
-
133
137
if let Some ( ref progress_tx) = progress_tx {
134
138
let progress = Progress :: Scan { progress_n, eta } ;
135
139
if progress_tx. send ( progress) . is_err ( ) {
136
140
break info;
137
141
}
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
+ ) ;
138
147
}
139
148
}
140
- } ;
149
+ }
141
150
thread:: sleep ( interval) ;
151
+ if should_shutdown ( ) {
152
+ break info;
153
+ }
142
154
} )
143
155
}
144
156
}
0 commit comments