@@ -28,7 +28,7 @@ use crate::types::{Broadcaster, ChainMonitor, ChannelManager, DynStore, Sweeper,
28
28
use crate :: { Error , NodeMetrics } ;
29
29
30
30
use lightning:: chain:: chaininterface:: ConfirmationTarget as LdkConfirmationTarget ;
31
- use lightning:: chain:: { Confirm , Filter , Listen } ;
31
+ use lightning:: chain:: { Confirm , Filter , Listen , WatchedOutput } ;
32
32
use lightning:: util:: ser:: Writeable ;
33
33
34
34
use lightning_transaction_sync:: EsploraSyncClient ;
@@ -42,7 +42,7 @@ use bdk_esplora::EsploraAsyncExt;
42
42
43
43
use esplora_client:: AsyncClient as EsploraAsyncClient ;
44
44
45
- use bitcoin:: { FeeRate , Network } ;
45
+ use bitcoin:: { FeeRate , Network , Script , ScriptBuf , Txid } ;
46
46
47
47
use std:: collections:: HashMap ;
48
48
use std:: sync:: { Arc , Mutex , RwLock } ;
@@ -111,22 +111,36 @@ impl WalletSyncStatus {
111
111
112
112
pub ( crate ) enum ElectrumRuntimeStatus {
113
113
Started ( Arc < ElectrumRuntimeClient > ) ,
114
- Stopped ,
114
+ Stopped {
115
+ pending_registered_txs : Vec < ( Txid , ScriptBuf ) > ,
116
+ pending_registered_outputs : Vec < WatchedOutput > ,
117
+ } ,
115
118
}
116
119
117
120
impl ElectrumRuntimeStatus {
118
121
pub ( crate ) fn new ( ) -> Self {
119
- Self :: Stopped
122
+ let pending_registered_txs = Vec :: new ( ) ;
123
+ let pending_registered_outputs = Vec :: new ( ) ;
124
+ Self :: Stopped { pending_registered_txs, pending_registered_outputs }
120
125
}
121
126
122
127
pub ( crate ) fn start (
123
128
& mut self , server_url : String , runtime : Arc < tokio:: runtime:: Runtime > , logger : Arc < Logger > ,
124
129
) -> Result < ( ) , Error > {
125
130
match self {
126
- Self :: Stopped => {
131
+ Self :: Stopped { pending_registered_txs , pending_registered_outputs } => {
127
132
let client =
128
133
Arc :: new ( ElectrumRuntimeClient :: new ( server_url. clone ( ) , runtime, logger) ?) ;
129
134
135
+ // Apply any pending `Filter` entries
136
+ for ( txid, script_pubkey) in pending_registered_txs. drain ( ..) {
137
+ client. register_tx ( & txid, & script_pubkey) ;
138
+ }
139
+
140
+ for output in pending_registered_outputs. drain ( ..) {
141
+ client. register_output ( output)
142
+ }
143
+
130
144
* self = Self :: Started ( client) ;
131
145
} ,
132
146
Self :: Started ( _) => {
@@ -140,12 +154,30 @@ impl ElectrumRuntimeStatus {
140
154
* self = Self :: new ( )
141
155
}
142
156
143
- pub ( crate ) fn client ( & self ) -> Option < & Arc < ElectrumRuntimeClient > > {
157
+ pub ( crate ) fn client ( & self ) -> Option < & ElectrumRuntimeClient > {
144
158
match self {
145
- Self :: Started ( client) => Some ( & client) ,
159
+ Self :: Started ( client) => Some ( & * client) ,
146
160
Self :: Stopped { .. } => None ,
147
161
}
148
162
}
163
+
164
+ fn register_tx ( & mut self , txid : & Txid , script_pubkey : & Script ) {
165
+ match self {
166
+ Self :: Started ( client) => client. register_tx ( txid, script_pubkey) ,
167
+ Self :: Stopped { pending_registered_txs, .. } => {
168
+ pending_registered_txs. push ( ( * txid, script_pubkey. to_owned ( ) ) )
169
+ } ,
170
+ }
171
+ }
172
+
173
+ fn register_output ( & mut self , output : lightning:: chain:: WatchedOutput ) {
174
+ match self {
175
+ Self :: Started ( client) => client. register_output ( output) ,
176
+ Self :: Stopped { pending_registered_outputs, .. } => {
177
+ pending_registered_outputs. push ( output)
178
+ } ,
179
+ }
180
+ }
149
181
}
150
182
151
183
pub ( crate ) enum ChainSource {
@@ -278,7 +310,7 @@ impl ChainSource {
278
310
Self :: Electrum { server_url, electrum_runtime_status, logger, .. } => {
279
311
electrum_runtime_status. write ( ) . unwrap ( ) . start (
280
312
server_url. clone ( ) ,
281
- runtime,
313
+ Arc :: clone ( & runtime) ,
282
314
Arc :: clone ( & logger) ,
283
315
) ?;
284
316
} ,
@@ -1261,17 +1293,21 @@ impl ChainSource {
1261
1293
}
1262
1294
1263
1295
impl Filter for ChainSource {
1264
- fn register_tx ( & self , txid : & bitcoin :: Txid , script_pubkey : & bitcoin :: Script ) {
1296
+ fn register_tx ( & self , txid : & Txid , script_pubkey : & Script ) {
1265
1297
match self {
1266
1298
Self :: Esplora { tx_sync, .. } => tx_sync. register_tx ( txid, script_pubkey) ,
1267
- Self :: Electrum { .. } => todo ! ( ) ,
1299
+ Self :: Electrum { electrum_runtime_status, .. } => {
1300
+ electrum_runtime_status. write ( ) . unwrap ( ) . register_tx ( txid, script_pubkey)
1301
+ } ,
1268
1302
Self :: BitcoindRpc { .. } => ( ) ,
1269
1303
}
1270
1304
}
1271
1305
fn register_output ( & self , output : lightning:: chain:: WatchedOutput ) {
1272
1306
match self {
1273
1307
Self :: Esplora { tx_sync, .. } => tx_sync. register_output ( output) ,
1274
- Self :: Electrum { .. } => todo ! ( ) ,
1308
+ Self :: Electrum { electrum_runtime_status, .. } => {
1309
+ electrum_runtime_status. write ( ) . unwrap ( ) . register_output ( output)
1310
+ } ,
1275
1311
Self :: BitcoindRpc { .. } => ( ) ,
1276
1312
}
1277
1313
}
0 commit comments