Skip to content

Commit 81562e9

Browse files
committed
Make electrum dependencies optional
1 parent 7977b04 commit 81562e9

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ codegen-units = 1 # Reduce number of codegen units to increase optimizations.
2525
panic = 'abort' # Abort on panic
2626

2727
[features]
28-
default = []
28+
default = ["electrum"]
29+
electrum = ["dep:bdk_electrum", "dep:electrum-client", "lightning-transaction-sync/electrum"]
2930

3031
[dependencies]
3132
lightning = { version = "0.1.0", features = ["std"] }
@@ -36,7 +37,7 @@ lightning-persister = { version = "0.1.0" }
3637
lightning-background-processor = { version = "0.1.0", features = ["futures"] }
3738
lightning-rapid-gossip-sync = { version = "0.1.0" }
3839
lightning-block-sync = { version = "0.1.0", features = ["rpc-client", "tokio"] }
39-
lightning-transaction-sync = { version = "0.1.0", features = ["esplora-async-https", "time", "electrum"] }
40+
lightning-transaction-sync = { version = "0.1.0", features = ["esplora-async-https", "time"] }
4041
lightning-liquidity = { version = "0.1.0", features = ["std"] }
4142

4243
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "main", features = ["std"] }
@@ -63,7 +64,7 @@ lightning-liquidity = { version = "0.1.0", features = ["std"] }
6364

6465
bdk_chain = { version = "0.21.1", default-features = false, features = ["std"] }
6566
bdk_esplora = { version = "0.20.1", default-features = false, features = ["async-https-rustls", "tokio"]}
66-
bdk_electrum = { version = "0.20.1", default-features = false, features = ["use-rustls"]}
67+
bdk_electrum = { version = "0.20.1", default-features = false, features = ["use-rustls"], optional = true }
6768
bdk_wallet = { version = "1.0.0", default-features = false, features = ["std", "keys-bip39"]}
6869

6970
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
@@ -77,7 +78,7 @@ rand = "0.8.5"
7778
chrono = { version = "0.4", default-features = false, features = ["clock"] }
7879
tokio = { version = "1.37", default-features = false, features = [ "rt-multi-thread", "time", "sync", "macros" ] }
7980
esplora-client = { version = "0.11", default-features = false, features = ["tokio", "async-https-rustls"] }
80-
electrum-client = { version = "0.22.0", default-features = true }
81+
electrum-client = { version = "0.22.0", default-features = true, optional = true }
8182
libc = "0.2"
8283
uniffi = { version = "0.27.3", features = ["build"], optional = true }
8384
serde = { version = "1.0.210", default-features = false, features = ["std", "derive"] }

src/builder.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, may_announce_channel, AnnounceError, Config, ElectrumSyncConfig,
11-
EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL, WALLET_KEYS_SEED_LEN,
10+
default_user_config, may_announce_channel, AnnounceError, Config, EsploraSyncConfig,
11+
DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL, WALLET_KEYS_SEED_LEN,
1212
};
1313

14+
#[cfg(feature = "electrum")]
15+
use crate::config::ElectrumSyncConfig;
16+
1417
use crate::connection::ConnectionManager;
1518
use crate::event::EventQueue;
1619
use crate::fee_estimator::OnchainFeeEstimator;
@@ -85,6 +88,7 @@ const LSPS_HARDENED_CHILD_INDEX: u32 = 577;
8588
#[derive(Debug, Clone)]
8689
enum ChainDataSourceConfig {
8790
Esplora { server_url: String, sync_config: Option<EsploraSyncConfig> },
91+
#[cfg(feature = "electrum")]
8892
Electrum { server_url: String, sync_config: Option<ElectrumSyncConfig> },
8993
BitcoindRpc { rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String },
9094
}
@@ -291,6 +295,7 @@ impl NodeBuilder {
291295
///
292296
/// If no `sync_config` is given, default values are used. See [`ElectrumSyncConfig`] for more
293297
/// information.
298+
#[cfg(feature = "electrum")]
294299
pub fn set_chain_source_electrum(
295300
&mut self, server_url: String, sync_config: Option<ElectrumSyncConfig>,
296301
) -> &mut Self {
@@ -710,6 +715,7 @@ impl ArcedNodeBuilder {
710715
///
711716
/// If no `sync_config` is given, default values are used. See [`ElectrumSyncConfig`] for more
712717
/// information.
718+
#[cfg(feature = "electrum")]
713719
pub fn set_chain_source_electrum(
714720
&self, server_url: String, sync_config: Option<ElectrumSyncConfig>,
715721
) {
@@ -1054,6 +1060,7 @@ fn build_with_store_internal(
10541060
Arc::clone(&node_metrics),
10551061
))
10561062
},
1063+
#[cfg(feature = "electrum")]
10571064
Some(ChainDataSourceConfig::Electrum { server_url, sync_config }) => {
10581065
let sync_config = sync_config.unwrap_or(ElectrumSyncConfig::default());
10591066
Arc::new(ChainSource::new_electrum(

src/chain/electrum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "electrum")]
12
// This file is Copyright its original authors, visible in version control history.
23
//
34
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or

src/chain/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
// accordance with one or both of these licenses.
77

88
mod bitcoind_rpc;
9+
#[cfg(feature = "electrum")]
910
mod electrum;
1011

1112
use crate::chain::bitcoind_rpc::{
1213
BitcoindRpcClient, BoundedHeaderCache, ChainListener, FeeRateEstimationMode,
1314
};
15+
#[cfg(feature = "electrum")]
1416
use crate::chain::electrum::ElectrumRuntimeClient;
1517
use crate::config::{
16-
BackgroundSyncConfig, Config, ElectrumSyncConfig, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY,
18+
BackgroundSyncConfig, Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY,
1719
BDK_CLIENT_STOP_GAP, BDK_WALLET_SYNC_TIMEOUT_SECS, FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS,
1820
LDK_WALLET_SYNC_TIMEOUT_SECS, RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL,
1921
TX_BROADCAST_TIMEOUT_SECS, WALLET_SYNC_INTERVAL_MINIMUM_SECS,
2022
};
23+
24+
#[cfg(feature = "electrum")]
25+
use crate::config::ElectrumSyncConfig;
26+
2127
use crate::fee_estimator::{
2228
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
2329
ConfirmationTarget, OnchainFeeEstimator,
@@ -110,6 +116,7 @@ impl WalletSyncStatus {
110116
}
111117
}
112118

119+
#[cfg(feature = "electrum")]
113120
pub(crate) enum ElectrumRuntimeStatus {
114121
Started(Arc<ElectrumRuntimeClient>),
115122
Stopped {
@@ -118,6 +125,7 @@ pub(crate) enum ElectrumRuntimeStatus {
118125
},
119126
}
120127

128+
#[cfg(feature = "electrum")]
121129
impl ElectrumRuntimeStatus {
122130
pub(crate) fn new() -> Self {
123131
let pending_registered_txs = Vec::new();
@@ -201,6 +209,7 @@ pub(crate) enum ChainSource {
201209
logger: Arc<Logger>,
202210
node_metrics: Arc<RwLock<NodeMetrics>>,
203211
},
212+
#[cfg(feature = "electrum")]
204213
Electrum {
205214
server_url: String,
206215
sync_config: ElectrumSyncConfig,
@@ -260,6 +269,7 @@ impl ChainSource {
260269
}
261270
}
262271

272+
#[cfg(feature = "electrum")]
263273
pub(crate) fn new_electrum(
264274
server_url: String, sync_config: ElectrumSyncConfig, onchain_wallet: Arc<Wallet>,
265275
fee_estimator: Arc<OnchainFeeEstimator>, tx_broadcaster: Arc<Broadcaster>,
@@ -313,6 +323,7 @@ impl ChainSource {
313323

314324
pub(crate) fn start(&self, runtime: Arc<tokio::runtime::Runtime>) -> Result<(), Error> {
315325
match self {
326+
#[cfg(feature = "electrum")]
316327
Self::Electrum { server_url, electrum_runtime_status, config, logger, .. } => {
317328
electrum_runtime_status.write().unwrap().start(
318329
server_url.clone(),
@@ -330,6 +341,7 @@ impl ChainSource {
330341

331342
pub(crate) fn stop(&self) {
332343
match self {
344+
#[cfg(feature = "electrum")]
333345
Self::Electrum { electrum_runtime_status, .. } => {
334346
electrum_runtime_status.write().unwrap().stop();
335347
},
@@ -372,6 +384,7 @@ impl ChainSource {
372384
return;
373385
}
374386
},
387+
#[cfg(feature = "electrum")]
375388
Self::Electrum { sync_config, logger, .. } => {
376389
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
377390
self.start_tx_based_sync_loop(
@@ -718,6 +731,7 @@ impl ChainSource {
718731

719732
res
720733
},
734+
#[cfg(feature = "electrum")]
721735
Self::Electrum {
722736
electrum_runtime_status,
723737
onchain_wallet,
@@ -909,6 +923,7 @@ impl ChainSource {
909923

910924
res
911925
},
926+
#[cfg(feature = "electrum")]
912927
Self::Electrum {
913928
electrum_runtime_status,
914929
lightning_wallet_sync_status,
@@ -998,6 +1013,7 @@ impl ChainSource {
9981013
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
9991014
unreachable!("Listeners will be synced via transction-based syncing")
10001015
},
1016+
#[cfg(feature = "electrum")]
10011017
Self::Electrum { .. } => {
10021018
// In Electrum mode we sync lightning and onchain wallets via
10031019
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
@@ -1223,6 +1239,7 @@ impl ChainSource {
12231239

12241240
Ok(())
12251241
},
1242+
#[cfg(feature = "electrum")]
12261243
Self::Electrum {
12271244
electrum_runtime_status,
12281245
fee_estimator,
@@ -1478,6 +1495,7 @@ impl ChainSource {
14781495
}
14791496
}
14801497
},
1498+
#[cfg(feature = "electrum")]
14811499
Self::Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
14821500
let electrum_client: Arc<ElectrumRuntimeClient> = if let Some(client) =
14831501
electrum_runtime_status.read().unwrap().client().as_ref()
@@ -1560,6 +1578,7 @@ impl Filter for ChainSource {
15601578
fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
15611579
match self {
15621580
Self::Esplora { tx_sync, .. } => tx_sync.register_tx(txid, script_pubkey),
1581+
#[cfg(feature = "electrum")]
15631582
Self::Electrum { electrum_runtime_status, .. } => {
15641583
electrum_runtime_status.write().unwrap().register_tx(txid, script_pubkey)
15651584
},
@@ -1569,6 +1588,7 @@ impl Filter for ChainSource {
15691588
fn register_output(&self, output: lightning::chain::WatchedOutput) {
15701589
match self {
15711590
Self::Esplora { tx_sync, .. } => tx_sync.register_output(output),
1591+
#[cfg(feature = "electrum")]
15721592
Self::Electrum { electrum_runtime_status, .. } => {
15731593
electrum_runtime_status.write().unwrap().register_output(output)
15741594
},

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ impl Default for EsploraSyncConfig {
380380
///
381381
/// Background syncing is enabled by default, using the default values specified in
382382
/// [`BackgroundSyncConfig`].
383+
#[cfg(feature = "electrum")]
383384
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
384385
pub struct ElectrumSyncConfig {
385386
/// Background sync configuration.
@@ -391,6 +392,7 @@ pub struct ElectrumSyncConfig {
391392
pub background_sync_config: Option<BackgroundSyncConfig>,
392393
}
393394

395+
#[cfg(feature = "electrum")]
394396
impl Default for ElectrumSyncConfig {
395397
fn default() -> Self {
396398
Self { background_sync_config: Some(BackgroundSyncConfig::default()) }

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ impl Node {
12691269
.await?;
12701270
chain_source.sync_onchain_wallet().await?;
12711271
},
1272+
#[cfg(feature = "electrum")]
12721273
ChainSource::Electrum { .. } => {
12731274
chain_source.update_fee_rate_estimates().await?;
12741275
chain_source
@@ -1546,7 +1547,7 @@ impl Node {
15461547

15471548
/// Creates a digital ECDSA signature of a message with the node's secret key.
15481549
///
1549-
/// A receiver knowing the corresponding `PublicKey` (e.g. the nodes id) and the message
1550+
/// A receiver knowing the corresponding `PublicKey` (e.g. the node's id) and the message
15501551
/// can be sure that the signature was generated by the caller.
15511552
/// Signatures are EC recoverable, meaning that given the message and the
15521553
/// signature the `PublicKey` of the signer can be extracted.

tests/common/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ pub(crate) mod logging;
1212

1313
use logging::TestLogWriter;
1414

15-
use ldk_node::config::{Config, ElectrumSyncConfig, EsploraSyncConfig};
15+
use ldk_node::config::{Config, EsploraSyncConfig};
16+
#[cfg(feature = "electrum")]
17+
use ldk_node::config::ElectrumSyncConfig;
1618
use ldk_node::io::sqlite_store::SqliteStore;
1719
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
1820
use ldk_node::{
@@ -36,6 +38,7 @@ use bitcoin::{Address, Amount, Network, OutPoint, Txid};
3638
use electrsd::corepc_node::Client as BitcoindClient;
3739
use electrsd::corepc_node::Node as BitcoinD;
3840
use electrsd::{corepc_node, ElectrsD};
41+
#[cfg(feature = "electrum")]
3942
use electrum_client::ElectrumApi;
4043

4144
use rand::distributions::Alphanumeric;
@@ -255,6 +258,7 @@ type TestNode = Node;
255258
#[derive(Clone)]
256259
pub(crate) enum TestChainSource<'a> {
257260
Esplora(&'a ElectrsD),
261+
#[cfg(feature = "electrum")]
258262
Electrum(&'a ElectrsD),
259263
BitcoindRpc(&'a BitcoinD),
260264
}
@@ -312,6 +316,7 @@ pub(crate) fn setup_node(
312316
let sync_config = EsploraSyncConfig { background_sync_config: None };
313317
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
314318
},
319+
#[cfg(feature = "electrum")]
315320
TestChainSource::Electrum(electrsd) => {
316321
let electrum_url = format!("tcp://{}", electrsd.electrum_url);
317322
let sync_config = ElectrumSyncConfig { background_sync_config: None };
@@ -360,6 +365,7 @@ pub(crate) fn setup_node(
360365
node
361366
}
362367

368+
#[cfg(feature = "electrum")]
363369
pub(crate) fn generate_blocks_and_wait<E: ElectrumApi>(
364370
bitcoind: &BitcoindClient, electrs: &E, num: usize,
365371
) {
@@ -376,6 +382,7 @@ pub(crate) fn generate_blocks_and_wait<E: ElectrumApi>(
376382
println!("\n");
377383
}
378384

385+
#[cfg(feature = "electrum")]
379386
pub(crate) fn wait_for_block<E: ElectrumApi>(electrs: &E, min_height: usize) {
380387
let mut header = match electrs.block_headers_subscribe() {
381388
Ok(header) => header,
@@ -398,6 +405,7 @@ pub(crate) fn wait_for_block<E: ElectrumApi>(electrs: &E, min_height: usize) {
398405
}
399406
}
400407

408+
#[cfg(feature = "electrum")]
401409
pub(crate) fn wait_for_tx<E: ElectrumApi>(electrs: &E, txid: Txid) {
402410
let mut tx_res = electrs.transaction_get(&txid);
403411
loop {
@@ -411,6 +419,7 @@ pub(crate) fn wait_for_tx<E: ElectrumApi>(electrs: &E, txid: Txid) {
411419
}
412420
}
413421

422+
#[cfg(feature = "electrum")]
414423
pub(crate) fn wait_for_outpoint_spend<E: ElectrumApi>(electrs: &E, outpoint: OutPoint) {
415424
let tx = electrs.transaction_get(&outpoint.txid).unwrap();
416425
let txout_script = tx.output.get(outpoint.vout as usize).unwrap().clone().script_pubkey;
@@ -448,6 +457,7 @@ where
448457
}
449458
}
450459

460+
#[cfg(feature = "electrum")]
451461
pub(crate) fn premine_and_distribute_funds<E: ElectrumApi>(
452462
bitcoind: &BitcoindClient, electrs: &E, addrs: Vec<Address>, amount: Amount,
453463
) {
@@ -496,6 +506,7 @@ pub fn open_channel(
496506
wait_for_tx(&electrsd.client, funding_txo_a.txid);
497507
}
498508

509+
#[cfg(feature = "electrum")]
499510
pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
500511
node_a: TestNode, node_b: TestNode, bitcoind: &BitcoindClient, electrsd: &E, allow_0conf: bool,
501512
expect_anchor_channel: bool, force_close: bool,

0 commit comments

Comments
 (0)