6
6
// accordance with one or both of these licenses.
7
7
8
8
mod bitcoind_rpc;
9
+ mod electrum;
9
10
10
11
use crate :: chain:: bitcoind_rpc:: {
11
12
BitcoindRpcClient , BoundedHeaderCache , ChainListener , FeeRateEstimationMode ,
12
13
} ;
14
+ use crate :: chain:: electrum:: ElectrumRuntimeClient ;
13
15
use crate :: config:: {
14
- Config , EsploraSyncConfig , BDK_CLIENT_CONCURRENCY , BDK_CLIENT_STOP_GAP ,
16
+ Config , ElectrumSyncConfig , EsploraSyncConfig , BDK_CLIENT_CONCURRENCY , BDK_CLIENT_STOP_GAP ,
15
17
BDK_WALLET_SYNC_TIMEOUT_SECS , FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS , LDK_WALLET_SYNC_TIMEOUT_SECS ,
16
18
RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL , TX_BROADCAST_TIMEOUT_SECS ,
17
19
WALLET_SYNC_INTERVAL_MINIMUM_SECS ,
@@ -107,6 +109,45 @@ impl WalletSyncStatus {
107
109
}
108
110
}
109
111
112
+ pub ( crate ) enum ElectrumRuntimeStatus {
113
+ Started ( Arc < ElectrumRuntimeClient > ) ,
114
+ Stopped ,
115
+ }
116
+
117
+ impl ElectrumRuntimeStatus {
118
+ pub ( crate ) fn new ( ) -> Self {
119
+ Self :: Stopped
120
+ }
121
+
122
+ pub ( crate ) fn start (
123
+ & mut self , server_url : String , runtime : Arc < tokio:: runtime:: Runtime > , logger : Arc < Logger > ,
124
+ ) -> Result < ( ) , Error > {
125
+ match self {
126
+ Self :: Stopped => {
127
+ let client =
128
+ Arc :: new ( ElectrumRuntimeClient :: new ( server_url. clone ( ) , runtime, logger) ?) ;
129
+
130
+ * self = Self :: Started ( client) ;
131
+ } ,
132
+ Self :: Started ( _) => {
133
+ debug_assert ! ( false , "We shouldn't call start if we're already started" )
134
+ } ,
135
+ }
136
+ Ok ( ( ) )
137
+ }
138
+
139
+ pub ( crate ) fn stop ( & mut self ) {
140
+ * self = Self :: new ( )
141
+ }
142
+
143
+ pub ( crate ) fn client ( & self ) -> Option < & Arc < ElectrumRuntimeClient > > {
144
+ match self {
145
+ Self :: Started ( client) => Some ( & client) ,
146
+ Self :: Stopped { .. } => None ,
147
+ }
148
+ }
149
+ }
150
+
110
151
pub ( crate ) enum ChainSource {
111
152
Esplora {
112
153
sync_config : EsploraSyncConfig ,
@@ -122,6 +163,20 @@ pub(crate) enum ChainSource {
122
163
logger : Arc < Logger > ,
123
164
node_metrics : Arc < RwLock < NodeMetrics > > ,
124
165
} ,
166
+ Electrum {
167
+ server_url : String ,
168
+ sync_config : ElectrumSyncConfig ,
169
+ electrum_runtime_status : RwLock < ElectrumRuntimeStatus > ,
170
+ onchain_wallet : Arc < Wallet > ,
171
+ onchain_wallet_sync_status : Mutex < WalletSyncStatus > ,
172
+ lightning_wallet_sync_status : Mutex < WalletSyncStatus > ,
173
+ fee_estimator : Arc < OnchainFeeEstimator > ,
174
+ tx_broadcaster : Arc < Broadcaster > ,
175
+ kv_store : Arc < DynStore > ,
176
+ config : Arc < Config > ,
177
+ logger : Arc < Logger > ,
178
+ node_metrics : Arc < RwLock < NodeMetrics > > ,
179
+ } ,
125
180
BitcoindRpc {
126
181
bitcoind_rpc_client : Arc < BitcoindRpcClient > ,
127
182
header_cache : tokio:: sync:: Mutex < BoundedHeaderCache > ,
@@ -167,6 +222,31 @@ impl ChainSource {
167
222
}
168
223
}
169
224
225
+ pub ( crate ) fn new_electrum (
226
+ server_url : String , sync_config : ElectrumSyncConfig , onchain_wallet : Arc < Wallet > ,
227
+ fee_estimator : Arc < OnchainFeeEstimator > , tx_broadcaster : Arc < Broadcaster > ,
228
+ kv_store : Arc < DynStore > , config : Arc < Config > , logger : Arc < Logger > ,
229
+ node_metrics : Arc < RwLock < NodeMetrics > > ,
230
+ ) -> Self {
231
+ let electrum_runtime_status = RwLock :: new ( ElectrumRuntimeStatus :: new ( ) ) ;
232
+ let onchain_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
233
+ let lightning_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
234
+ Self :: Electrum {
235
+ server_url,
236
+ sync_config,
237
+ electrum_runtime_status,
238
+ onchain_wallet,
239
+ onchain_wallet_sync_status,
240
+ lightning_wallet_sync_status,
241
+ fee_estimator,
242
+ tx_broadcaster,
243
+ kv_store,
244
+ config,
245
+ logger,
246
+ node_metrics,
247
+ }
248
+ }
249
+
170
250
pub ( crate ) fn new_bitcoind_rpc (
171
251
host : String , port : u16 , rpc_user : String , rpc_password : String ,
172
252
onchain_wallet : Arc < Wallet > , fee_estimator : Arc < OnchainFeeEstimator > ,
@@ -193,6 +273,33 @@ impl ChainSource {
193
273
}
194
274
}
195
275
276
+ pub ( crate ) fn start ( & self , runtime : Arc < tokio:: runtime:: Runtime > ) -> Result < ( ) , Error > {
277
+ match self {
278
+ Self :: Electrum { server_url, electrum_runtime_status, logger, .. } => {
279
+ electrum_runtime_status. write ( ) . unwrap ( ) . start (
280
+ server_url. clone ( ) ,
281
+ runtime,
282
+ Arc :: clone ( & logger) ,
283
+ ) ?;
284
+ } ,
285
+ _ => {
286
+ // Nothing to do for other chain sources.
287
+ } ,
288
+ }
289
+ Ok ( ( ) )
290
+ }
291
+
292
+ pub ( crate ) fn stop ( & self ) {
293
+ match self {
294
+ Self :: Electrum { electrum_runtime_status, .. } => {
295
+ electrum_runtime_status. write ( ) . unwrap ( ) . stop ( ) ;
296
+ } ,
297
+ _ => {
298
+ // Nothing to do for other chain sources.
299
+ } ,
300
+ }
301
+ }
302
+
196
303
pub ( crate ) fn as_utxo_source ( & self ) -> Option < Arc < dyn UtxoSource > > {
197
304
match self {
198
305
Self :: BitcoindRpc { bitcoind_rpc_client, .. } => Some ( bitcoind_rpc_client. rpc_client ( ) ) ,
@@ -271,6 +378,7 @@ impl ChainSource {
271
378
return ;
272
379
}
273
380
} ,
381
+ Self :: Electrum { .. } => todo ! ( ) ,
274
382
Self :: BitcoindRpc {
275
383
bitcoind_rpc_client,
276
384
header_cache,
@@ -538,6 +646,7 @@ impl ChainSource {
538
646
539
647
res
540
648
} ,
649
+ Self :: Electrum { .. } => todo ! ( ) ,
541
650
Self :: BitcoindRpc { .. } => {
542
651
// In BitcoindRpc mode we sync lightning and onchain wallet in one go by via
543
652
// `ChainPoller`. So nothing to do here.
@@ -637,6 +746,7 @@ impl ChainSource {
637
746
638
747
res
639
748
} ,
749
+ Self :: Electrum { .. } => todo ! ( ) ,
640
750
Self :: BitcoindRpc { .. } => {
641
751
// In BitcoindRpc mode we sync lightning and onchain wallet in one go by via
642
752
// `ChainPoller`. So nothing to do here.
@@ -655,6 +765,11 @@ impl ChainSource {
655
765
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
656
766
unreachable ! ( "Listeners will be synced via transction-based syncing" )
657
767
} ,
768
+ Self :: Electrum { .. } => {
769
+ // In Electrum mode we sync lightning and onchain wallets via
770
+ // `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
771
+ unreachable ! ( "Listeners will be synced via transction-based syncing" )
772
+ } ,
658
773
Self :: BitcoindRpc {
659
774
bitcoind_rpc_client,
660
775
header_cache,
@@ -875,6 +990,7 @@ impl ChainSource {
875
990
876
991
Ok ( ( ) )
877
992
} ,
993
+ Self :: Electrum { .. } => todo ! ( ) ,
878
994
Self :: BitcoindRpc {
879
995
bitcoind_rpc_client,
880
996
fee_estimator,
@@ -1085,6 +1201,7 @@ impl ChainSource {
1085
1201
}
1086
1202
}
1087
1203
} ,
1204
+ Self :: Electrum { .. } => todo ! ( ) ,
1088
1205
Self :: BitcoindRpc { bitcoind_rpc_client, tx_broadcaster, logger, .. } => {
1089
1206
// While it's a bit unclear when we'd be able to lean on Bitcoin Core >v28
1090
1207
// features, we should eventually switch to use `submitpackage` via the
@@ -1147,12 +1264,14 @@ impl Filter for ChainSource {
1147
1264
fn register_tx ( & self , txid : & bitcoin:: Txid , script_pubkey : & bitcoin:: Script ) {
1148
1265
match self {
1149
1266
Self :: Esplora { tx_sync, .. } => tx_sync. register_tx ( txid, script_pubkey) ,
1267
+ Self :: Electrum { .. } => todo ! ( ) ,
1150
1268
Self :: BitcoindRpc { .. } => ( ) ,
1151
1269
}
1152
1270
}
1153
1271
fn register_output ( & self , output : lightning:: chain:: WatchedOutput ) {
1154
1272
match self {
1155
1273
Self :: Esplora { tx_sync, .. } => tx_sync. register_output ( output) ,
1274
+ Self :: Electrum { .. } => todo ! ( ) ,
1156
1275
Self :: BitcoindRpc { .. } => ( ) ,
1157
1276
}
1158
1277
}
0 commit comments