Skip to content

Commit 10fffa9

Browse files
committed
Move Esplora-specific sync options to dedicated config
.. as other upcoming chain sources might not have the same config options such as syncing intervals, or at least not with the same semantics.
1 parent db2988a commit 10fffa9

File tree

13 files changed

+130
-71
lines changed

13 files changed

+130
-71
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::str::FromStr;
2424
fn main() {
2525
let mut builder = Builder::new();
2626
builder.set_network(Network::Testnet);
27-
builder.set_esplora_server("https://blockstream.info/testnet/api".to_string());
27+
builder.set_chain_source_esplora("https://blockstream.info/testnet/api".to_string(), None);
2828
builder.set_gossip_source_rgs("https://rapidsync.lightningdevkit.org/testnet/snapshot".to_string());
2929

3030
let node = builder.build().unwrap();

bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode/LibraryTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ class LibraryTest {
130130
println("Config 2: $config2")
131131

132132
val builder1 = Builder.fromConfig(config1)
133-
builder1.setEsploraServer(esploraEndpoint)
133+
builder1.setChainSourceEsplora(esploraEndpoint, null)
134134
val builder2 = Builder.fromConfig(config2)
135-
builder2.setEsploraServer(esploraEndpoint)
135+
builder2.setChainSourceEsplora(esploraEndpoint, null)
136136

137137
val node1 = builder1.build()
138138
val node2 = builder2.build()

bindings/ldk_node.udl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ dictionary Config {
99
Network network;
1010
sequence<SocketAddress>? listening_addresses;
1111
NodeAlias? node_alias;
12-
u64 onchain_wallet_sync_interval_secs;
13-
u64 wallet_sync_interval_secs;
14-
u64 fee_rate_cache_update_interval_secs;
1512
sequence<PublicKey> trusted_peers_0conf;
1613
u64 probing_liquidity_limit_multiplier;
1714
LogLevel log_level;
@@ -24,6 +21,12 @@ dictionary AnchorChannelsConfig {
2421
u64 per_channel_reserve_sats;
2522
};
2623

24+
dictionary EsploraSyncConfig {
25+
u64 onchain_wallet_sync_interval_secs;
26+
u64 lightning_wallet_sync_interval_secs;
27+
u64 fee_rate_cache_update_interval_secs;
28+
};
29+
2730
interface Builder {
2831
constructor();
2932
[Name=from_config]
@@ -32,7 +35,7 @@ interface Builder {
3235
[Throws=BuildError]
3336
void set_entropy_seed_bytes(sequence<u8> seed_bytes);
3437
void set_entropy_bip39_mnemonic(Mnemonic mnemonic, string? passphrase);
35-
void set_esplora_server(string esplora_server_url);
38+
void set_chain_source_esplora(string server_url, EsploraSyncConfig? config);
3639
void set_gossip_source_p2p();
3740
void set_gossip_source_rgs(string rgs_server_url);
3841
void set_liquidity_source_lsps2(SocketAddress address, PublicKey node_id, string? token);

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def setup_node(tmp_dir, esplora_endpoint, listening_addresses):
8484
config = default_config()
8585
builder = Builder.from_config(config)
8686
builder.set_storage_dir_path(tmp_dir)
87-
builder.set_esplora_server(esplora_endpoint)
87+
builder.set_chain_source_esplora(esplora_endpoint, None)
8888
builder.set_network(DEFAULT_TEST_NETWORK)
8989
builder.set_listening_addresses(listening_addresses)
9090
return builder.build()

src/builder.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// accordance with one or both of these licenses.
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
9-
use crate::config::{default_user_config, Config, WALLET_KEYS_SEED_LEN};
9+
use crate::config::{default_user_config, Config, EsploraSyncConfig, WALLET_KEYS_SEED_LEN};
1010

1111
use crate::connection::ConnectionManager;
1212
use crate::event::EventQueue;
@@ -77,7 +77,7 @@ use std::time::SystemTime;
7777

7878
#[derive(Debug, Clone)]
7979
enum ChainDataSourceConfig {
80-
Esplora(String),
80+
Esplora { server_url: String, sync_config: Option<EsploraSyncConfig> },
8181
}
8282

8383
#[derive(Debug, Clone)]
@@ -237,8 +237,14 @@ impl NodeBuilder {
237237
}
238238

239239
/// Configures the [`Node`] instance to source its chain data from the given Esplora server.
240-
pub fn set_esplora_server(&mut self, esplora_server_url: String) -> &mut Self {
241-
self.chain_data_source_config = Some(ChainDataSourceConfig::Esplora(esplora_server_url));
240+
///
241+
/// If no `sync_config` is given, default values are used. See [`EsploraSyncConfig`] for more
242+
/// information.
243+
pub fn set_chain_source_esplora(
244+
&mut self, server_url: String, sync_config: Option<EsploraSyncConfig>,
245+
) -> &mut Self {
246+
self.chain_data_source_config =
247+
Some(ChainDataSourceConfig::Esplora { server_url, sync_config });
242248
self
243249
}
244250

@@ -464,8 +470,13 @@ impl ArcedNodeBuilder {
464470
}
465471

466472
/// Configures the [`Node`] instance to source its chain data from the given Esplora server.
467-
pub fn set_esplora_server(&self, esplora_server_url: String) {
468-
self.inner.write().unwrap().set_esplora_server(esplora_server_url);
473+
///
474+
/// If no `sync_config` is given, default values are used. See [`EsploraSyncConfig`] for more
475+
/// information.
476+
pub fn set_chain_source_esplora(
477+
&self, server_url: String, sync_config: Option<EsploraSyncConfig>,
478+
) {
479+
self.inner.write().unwrap().set_chain_source_esplora(server_url, sync_config);
469480
}
470481

471482
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
@@ -608,21 +619,27 @@ fn build_with_store_internal(
608619
));
609620

610621
let chain_source = match chain_data_source_config {
611-
Some(ChainDataSourceConfig::Esplora(server_url)) => Arc::new(ChainSource::new_esplora(
612-
server_url.clone(),
613-
Arc::clone(&wallet),
614-
Arc::clone(&fee_estimator),
615-
Arc::clone(&tx_broadcaster),
616-
Arc::clone(&kv_store),
617-
Arc::clone(&config),
618-
Arc::clone(&logger),
619-
Arc::clone(&node_metrics),
620-
)),
622+
Some(ChainDataSourceConfig::Esplora { server_url, sync_config }) => {
623+
let sync_config = sync_config.unwrap_or(EsploraSyncConfig::default());
624+
Arc::new(ChainSource::new_esplora(
625+
server_url.clone(),
626+
sync_config,
627+
Arc::clone(&wallet),
628+
Arc::clone(&fee_estimator),
629+
Arc::clone(&tx_broadcaster),
630+
Arc::clone(&kv_store),
631+
Arc::clone(&config),
632+
Arc::clone(&logger),
633+
Arc::clone(&node_metrics),
634+
))
635+
},
621636
None => {
622637
// Default to Esplora client.
623638
let server_url = DEFAULT_ESPLORA_SERVER_URL.to_string();
639+
let sync_config = EsploraSyncConfig::default();
624640
Arc::new(ChainSource::new_esplora(
625641
server_url.clone(),
642+
sync_config,
626643
Arc::clone(&wallet),
627644
Arc::clone(&fee_estimator),
628645
Arc::clone(&tx_broadcaster),

src/chain/mod.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// accordance with one or both of these licenses.
77

88
use crate::config::{
9-
Config, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, BDK_WALLET_SYNC_TIMEOUT_SECS,
10-
FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS,
9+
Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP,
10+
BDK_WALLET_SYNC_TIMEOUT_SECS, FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS,
1111
RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL, TX_BROADCAST_TIMEOUT_SECS,
1212
WALLET_SYNC_INTERVAL_MINIMUM_SECS,
1313
};
@@ -96,6 +96,7 @@ impl WalletSyncStatus {
9696

9797
pub(crate) enum ChainSource {
9898
Esplora {
99+
sync_config: EsploraSyncConfig,
99100
esplora_client: EsploraAsyncClient,
100101
onchain_wallet: Arc<Wallet>,
101102
onchain_wallet_sync_status: Mutex<WalletSyncStatus>,
@@ -112,9 +113,10 @@ pub(crate) enum ChainSource {
112113

113114
impl ChainSource {
114115
pub(crate) fn new_esplora(
115-
server_url: String, onchain_wallet: Arc<Wallet>, fee_estimator: Arc<OnchainFeeEstimator>,
116-
tx_broadcaster: Arc<Broadcaster>, kv_store: Arc<DynStore>, config: Arc<Config>,
117-
logger: Arc<FilesystemLogger>, node_metrics: Arc<RwLock<NodeMetrics>>,
116+
server_url: String, sync_config: EsploraSyncConfig, onchain_wallet: Arc<Wallet>,
117+
fee_estimator: Arc<OnchainFeeEstimator>, tx_broadcaster: Arc<Broadcaster>,
118+
kv_store: Arc<DynStore>, config: Arc<Config>, logger: Arc<FilesystemLogger>,
119+
node_metrics: Arc<RwLock<NodeMetrics>>,
118120
) -> Self {
119121
let mut client_builder = esplora_client::Builder::new(&server_url);
120122
client_builder = client_builder.timeout(DEFAULT_ESPLORA_CLIENT_TIMEOUT_SECS);
@@ -124,6 +126,7 @@ impl ChainSource {
124126
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
125127
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
126128
Self::Esplora {
129+
sync_config,
127130
esplora_client,
128131
onchain_wallet,
129132
onchain_wallet_sync_status,
@@ -144,16 +147,17 @@ impl ChainSource {
144147
output_sweeper: Arc<Sweeper>,
145148
) {
146149
match self {
147-
Self::Esplora { config, logger, .. } => {
150+
Self::Esplora { sync_config, logger, .. } => {
148151
// Setup syncing intervals
149-
let onchain_wallet_sync_interval_secs =
150-
config.onchain_wallet_sync_interval_secs.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
152+
let onchain_wallet_sync_interval_secs = sync_config
153+
.onchain_wallet_sync_interval_secs
154+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
151155
let mut onchain_wallet_sync_interval =
152156
tokio::time::interval(Duration::from_secs(onchain_wallet_sync_interval_secs));
153157
onchain_wallet_sync_interval
154158
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
155159

156-
let fee_rate_cache_update_interval_secs = config
160+
let fee_rate_cache_update_interval_secs = sync_config
157161
.fee_rate_cache_update_interval_secs
158162
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
159163
let mut fee_rate_update_interval =
@@ -163,11 +167,12 @@ impl ChainSource {
163167
fee_rate_update_interval
164168
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
165169

166-
let wallet_sync_interval_secs =
167-
config.wallet_sync_interval_secs.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
168-
let mut wallet_sync_interval =
169-
tokio::time::interval(Duration::from_secs(wallet_sync_interval_secs));
170-
wallet_sync_interval
170+
let lightning_wallet_sync_interval_secs = sync_config
171+
.lightning_wallet_sync_interval_secs
172+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
173+
let mut lightning_wallet_sync_interval =
174+
tokio::time::interval(Duration::from_secs(lightning_wallet_sync_interval_secs));
175+
lightning_wallet_sync_interval
171176
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
172177

173178
// Start the syncing loop.
@@ -186,7 +191,7 @@ impl ChainSource {
186191
_ = fee_rate_update_interval.tick() => {
187192
let _ = self.update_fee_rate_estimates().await;
188193
}
189-
_ = wallet_sync_interval.tick() => {
194+
_ = lightning_wallet_sync_interval.tick() => {
190195
let _ = self.sync_lightning_wallet(
191196
Arc::clone(&channel_manager),
192197
Arc::clone(&chain_monitor),

src/config.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,6 @@ pub struct Config {
121121
/// **Note**: We will only allow opening and accepting public channels if the `node_alias` and the
122122
/// `listening_addresses` are set.
123123
pub node_alias: Option<NodeAlias>,
124-
/// The time in-between background sync attempts of the onchain wallet, in seconds.
125-
///
126-
/// **Note:** A minimum of 10 seconds is always enforced.
127-
pub onchain_wallet_sync_interval_secs: u64,
128-
/// The time in-between background sync attempts of the LDK wallet, in seconds.
129-
///
130-
/// **Note:** A minimum of 10 seconds is always enforced.
131-
pub wallet_sync_interval_secs: u64,
132-
/// The time in-between background update attempts to our fee rate cache, in seconds.
133-
///
134-
/// **Note:** A minimum of 10 seconds is always enforced.
135-
pub fee_rate_cache_update_interval_secs: u64,
136124
/// A list of peers that we allow to establish zero confirmation channels to us.
137125
///
138126
/// **Note:** Allowing payments via zero-confirmation channels is potentially insecure if the
@@ -182,9 +170,6 @@ impl Default for Config {
182170
log_dir_path: None,
183171
network: DEFAULT_NETWORK,
184172
listening_addresses: None,
185-
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
186-
wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
187-
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
188173
trusted_peers_0conf: Vec::new(),
189174
probing_liquidity_limit_multiplier: DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER,
190175
log_level: DEFAULT_LOG_LEVEL,
@@ -302,6 +287,41 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
302287
user_config
303288
}
304289

290+
/// Options related to syncing the Lightning and on-chain wallets via an Esplora backend.
291+
///
292+
/// ### Defaults
293+
///
294+
/// | Parameter | Value |
295+
/// |----------------------------------------|--------------------|
296+
/// | `onchain_wallet_sync_interval_secs` | 80 |
297+
/// | `lightning_wallet_sync_interval_secs` | 30 |
298+
/// | `fee_rate_cache_update_interval_secs` | 600 |
299+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
300+
pub struct EsploraSyncConfig {
301+
/// The time in-between background sync attempts of the onchain wallet, in seconds.
302+
///
303+
/// **Note:** A minimum of 10 seconds is always enforced.
304+
pub onchain_wallet_sync_interval_secs: u64,
305+
/// The time in-between background sync attempts of the LDK wallet, in seconds.
306+
///
307+
/// **Note:** A minimum of 10 seconds is always enforced.
308+
pub lightning_wallet_sync_interval_secs: u64,
309+
/// The time in-between background update attempts to our fee rate cache, in seconds.
310+
///
311+
/// **Note:** A minimum of 10 seconds is always enforced.
312+
pub fee_rate_cache_update_interval_secs: u64,
313+
}
314+
315+
impl Default for EsploraSyncConfig {
316+
fn default() -> Self {
317+
Self {
318+
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
319+
lightning_wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
320+
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
321+
}
322+
}
323+
}
324+
305325
/// Options which apply on a per-channel basis and may change at runtime or based on negotiation
306326
/// with our counterparty.
307327
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//! fn main() {
3434
//! let mut builder = Builder::new();
3535
//! builder.set_network(Network::Testnet);
36-
//! builder.set_esplora_server("https://blockstream.info/testnet/api".to_string());
36+
//! builder.set_chain_source_esplora("https://blockstream.info/testnet/api".to_string(), None);
3737
//! builder.set_gossip_source_rgs("https://rapidsync.lightningdevkit.org/testnet/snapshot".to_string());
3838
//!
3939
//! let node = builder.build().unwrap();
@@ -1186,10 +1186,14 @@ impl Node {
11861186
/// Manually sync the LDK and BDK wallets with the current chain state and update the fee rate
11871187
/// cache.
11881188
///
1189-
/// **Note:** The wallets are regularly synced in the background, which is configurable via
1190-
/// [`Config::onchain_wallet_sync_interval_secs`] and [`Config::wallet_sync_interval_secs`].
1191-
/// Therefore, using this blocking sync method is almost always redundant and should be avoided
1192-
/// where possible.
1189+
/// **Note:** The wallets are regularly synced in the background, which is configurable via the
1190+
/// respective config object, e.g., via
1191+
/// [`EsploraSyncConfig::onchain_wallet_sync_interval_secs`] and
1192+
/// [`EsploraSyncConfig::lightning_wallet_sync_interval_secs`]. Therefore, using this blocking
1193+
/// sync method is almost always redundant and should be avoided where possible.
1194+
///
1195+
/// [`EsploraSyncConfig::onchain_wallet_sync_interval_secs`]: crate::config::EsploraSyncConfig::onchain_wallet_sync_interval_secs
1196+
/// [`EsploraSyncConfig::lightning_wallet_sync_interval_secs`]: crate::config::EsploraSyncConfig::lightning_wallet_sync_interval_secs
11931197
pub fn sync_wallets(&self) -> Result<(), Error> {
11941198
let rt_lock = self.runtime.read().unwrap();
11951199
if rt_lock.is_none() {

src/uniffi_types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
// Make sure to add any re-exported items that need to be used in uniffi below.
1212

13-
pub use crate::config::{default_config, AnchorChannelsConfig, MaxDustHTLCExposure};
13+
pub use crate::config::{
14+
default_config, AnchorChannelsConfig, EsploraSyncConfig, MaxDustHTLCExposure,
15+
};
1416
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
1517
pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus};
1618
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};

tests/common/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![cfg(any(test, cln_test, vss_test))]
99
#![allow(dead_code)]
1010

11-
use ldk_node::config::Config;
11+
use ldk_node::config::{Config, EsploraSyncConfig};
1212
use ldk_node::io::sqlite_store::SqliteStore;
1313
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
1414
use ldk_node::{Builder, Event, LightningBalance, LogLevel, Node, NodeError, PendingSweepBalance};
@@ -217,8 +217,6 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
217217
}
218218

219219
config.network = Network::Regtest;
220-
config.onchain_wallet_sync_interval_secs = 100000;
221-
config.wallet_sync_interval_secs = 100000;
222220
println!("Setting network: {}", config.network);
223221

224222
let rand_dir = random_storage_path();
@@ -280,8 +278,11 @@ pub(crate) fn setup_two_nodes(
280278

281279
pub(crate) fn setup_node(electrsd: &ElectrsD, config: Config) -> TestNode {
282280
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
281+
let mut sync_config = EsploraSyncConfig::default();
282+
sync_config.onchain_wallet_sync_interval_secs = 100000;
283+
sync_config.lightning_wallet_sync_interval_secs = 100000;
283284
setup_builder!(builder, config);
284-
builder.set_esplora_server(esplora_url.clone());
285+
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
285286
let test_sync_store = Arc::new(TestSyncStore::new(config.storage_dir_path.into()));
286287
let node = builder.build_with_store(test_sync_store).unwrap();
287288
node.start().unwrap();

tests/integration_tests_cln.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_cln() {
4545
// Setup LDK Node
4646
let config = common::random_config(true);
4747
let mut builder = Builder::from_config(config);
48-
builder.set_esplora_server("http://127.0.0.1:3002".to_string());
48+
builder.set_chain_source_esplora("http://127.0.0.1:3002".to_string(), None);
4949

5050
let node = builder.build().unwrap();
5151
node.start().unwrap();

0 commit comments

Comments
 (0)