@@ -28,7 +28,7 @@ use crate::types::{Broadcaster, ChainMonitor, ChannelManager, DynStore, Sweeper,
2828use crate :: { Error , NodeMetrics } ;
2929
3030use lightning:: chain:: chaininterface:: ConfirmationTarget as LdkConfirmationTarget ;
31- use lightning:: chain:: { Confirm , Filter , Listen } ;
31+ use lightning:: chain:: { Confirm , Filter , Listen , WatchedOutput } ;
3232use lightning:: util:: ser:: Writeable ;
3333
3434use lightning_transaction_sync:: EsploraSyncClient ;
@@ -42,7 +42,7 @@ use bdk_esplora::EsploraAsyncExt;
4242
4343use esplora_client:: AsyncClient as EsploraAsyncClient ;
4444
45- use bitcoin:: { FeeRate , Network } ;
45+ use bitcoin:: { FeeRate , Network , Script , ScriptBuf , Txid } ;
4646
4747use std:: collections:: HashMap ;
4848use std:: sync:: { Arc , Mutex , RwLock } ;
@@ -111,22 +111,36 @@ impl WalletSyncStatus {
111111
112112pub ( crate ) enum ElectrumRuntimeStatus {
113113 Started ( Arc < ElectrumRuntimeClient > ) ,
114- Stopped ,
114+ Stopped {
115+ pending_registered_txs : Vec < ( Txid , ScriptBuf ) > ,
116+ pending_registered_outputs : Vec < WatchedOutput > ,
117+ } ,
115118}
116119
117120impl ElectrumRuntimeStatus {
118121 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 }
120125 }
121126
122127 pub ( crate ) fn start (
123128 & mut self , server_url : String , runtime : Arc < tokio:: runtime:: Runtime > , logger : Arc < Logger > ,
124129 ) -> Result < ( ) , Error > {
125130 match self {
126- Self :: Stopped => {
131+ Self :: Stopped { pending_registered_txs , pending_registered_outputs } => {
127132 let client =
128133 Arc :: new ( ElectrumRuntimeClient :: new ( server_url. clone ( ) , runtime, logger) ?) ;
129134
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+
130144 * self = Self :: Started ( client) ;
131145 } ,
132146 Self :: Started ( _) => {
@@ -140,12 +154,30 @@ impl ElectrumRuntimeStatus {
140154 * self = Self :: new ( )
141155 }
142156
143- pub ( crate ) fn client ( & self ) -> Option < & Arc < ElectrumRuntimeClient > > {
157+ pub ( crate ) fn client ( & self ) -> Option < & ElectrumRuntimeClient > {
144158 match self {
145- Self :: Started ( client) => Some ( & client) ,
159+ Self :: Started ( client) => Some ( & * client) ,
146160 Self :: Stopped { .. } => None ,
147161 }
148162 }
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+ }
149181}
150182
151183pub ( crate ) enum ChainSource {
@@ -278,7 +310,7 @@ impl ChainSource {
278310 Self :: Electrum { server_url, electrum_runtime_status, logger, .. } => {
279311 electrum_runtime_status. write ( ) . unwrap ( ) . start (
280312 server_url. clone ( ) ,
281- runtime,
313+ Arc :: clone ( & runtime) ,
282314 Arc :: clone ( & logger) ,
283315 ) ?;
284316 } ,
@@ -1261,17 +1293,21 @@ impl ChainSource {
12611293}
12621294
12631295impl 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 ) {
12651297 match self {
12661298 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+ } ,
12681302 Self :: BitcoindRpc { .. } => ( ) ,
12691303 }
12701304 }
12711305 fn register_output ( & self , output : lightning:: chain:: WatchedOutput ) {
12721306 match self {
12731307 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+ } ,
12751311 Self :: BitcoindRpc { .. } => ( ) ,
12761312 }
12771313 }
0 commit comments