Skip to content

Commit 04137e9

Browse files
authored
Merge pull request #253 from tnull/2024-02-move-config
Move `Config` and associated `const`s to dedicated`config.rs`
2 parents d6aa27c + 2734dd8 commit 04137e9

File tree

5 files changed

+153
-137
lines changed

5 files changed

+153
-137
lines changed

src/builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::config::{
2+
Config, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, DEFAULT_ESPLORA_SERVER_URL,
3+
WALLET_KEYS_SEED_LEN,
4+
};
15
use crate::event::EventQueue;
26
use crate::fee_estimator::OnchainFeeEstimator;
37
use crate::gossip::GossipSource;
@@ -15,11 +19,7 @@ use crate::types::{
1519
OnionMessenger, PeerManager,
1620
};
1721
use crate::wallet::Wallet;
18-
use crate::LogLevel;
19-
use crate::{
20-
Config, Node, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, DEFAULT_ESPLORA_SERVER_URL,
21-
WALLET_KEYS_SEED_LEN,
22-
};
22+
use crate::{LogLevel, Node};
2323

2424
use lightning::chain::{chainmonitor, BestBlock, Watch};
2525
use lightning::ln::channelmanager::{self, ChainParameters, ChannelManagerReadArgs};

src/config.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use std::time::Duration;
2+
3+
use lightning::ln::msgs::SocketAddress;
4+
use lightning::util::logger::Level as LogLevel;
5+
6+
use bitcoin::secp256k1::PublicKey;
7+
use bitcoin::Network;
8+
9+
// Config defaults
10+
const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node/";
11+
const DEFAULT_NETWORK: Network = Network::Bitcoin;
12+
const DEFAULT_CLTV_EXPIRY_DELTA: u32 = 144;
13+
const DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS: u64 = 80;
14+
const DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS: u64 = 30;
15+
const DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS: u64 = 60 * 10;
16+
const DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER: u64 = 3;
17+
const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
18+
19+
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
20+
// number of derivation indexes after which BDK stops looking for new scripts belonging to the wallet.
21+
pub(crate) const BDK_CLIENT_STOP_GAP: usize = 20;
22+
23+
// The number of concurrent requests made against the API provider.
24+
pub(crate) const BDK_CLIENT_CONCURRENCY: u8 = 4;
25+
26+
// The default Esplora server we're using.
27+
pub(crate) const DEFAULT_ESPLORA_SERVER_URL: &str = "https://blockstream.info/api";
28+
29+
// The timeout after which we abandon retrying failed payments.
30+
pub(crate) const LDK_PAYMENT_RETRY_TIMEOUT: Duration = Duration::from_secs(10);
31+
32+
// The time in-between peer reconnection attempts.
33+
pub(crate) const PEER_RECONNECTION_INTERVAL: Duration = Duration::from_secs(10);
34+
35+
// The time in-between RGS sync attempts.
36+
pub(crate) const RGS_SYNC_INTERVAL: Duration = Duration::from_secs(60 * 60);
37+
38+
// The time in-between node announcement broadcast attempts.
39+
pub(crate) const NODE_ANN_BCAST_INTERVAL: Duration = Duration::from_secs(60 * 60);
40+
41+
// The lower limit which we apply to any configured wallet sync intervals.
42+
pub(crate) const WALLET_SYNC_INTERVAL_MINIMUM_SECS: u64 = 10;
43+
44+
// The length in bytes of our wallets' keys seed.
45+
pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
46+
47+
#[derive(Debug, Clone)]
48+
/// Represents the configuration of an [`Node`] instance.
49+
///
50+
/// ### Defaults
51+
///
52+
/// | Parameter | Value |
53+
/// |----------------------------------------|--------------------|
54+
/// | `storage_dir_path` | /tmp/ldk_node/ |
55+
/// | `log_dir_path` | None |
56+
/// | `network` | Bitcoin |
57+
/// | `listening_addresses` | None |
58+
/// | `default_cltv_expiry_delta` | 144 |
59+
/// | `onchain_wallet_sync_interval_secs` | 80 |
60+
/// | `wallet_sync_interval_secs` | 30 |
61+
/// | `fee_rate_cache_update_interval_secs` | 600 |
62+
/// | `trusted_peers_0conf` | [] |
63+
/// | `probing_liquidity_limit_multiplier` | 3 |
64+
/// | `log_level` | Debug |
65+
///
66+
/// [`Node`]: crate::Node
67+
pub struct Config {
68+
/// The path where the underlying LDK and BDK persist their data.
69+
pub storage_dir_path: String,
70+
/// The path where logs are stored.
71+
///
72+
/// If set to `None`, logs can be found in the `logs` subdirectory in [`Config::storage_dir_path`].
73+
pub log_dir_path: Option<String>,
74+
/// The used Bitcoin network.
75+
pub network: Network,
76+
/// The addresses on which the node will listen for incoming connections.
77+
pub listening_addresses: Option<Vec<SocketAddress>>,
78+
/// The default CLTV expiry delta to be used for payments.
79+
pub default_cltv_expiry_delta: u32,
80+
/// The time in-between background sync attempts of the onchain wallet, in seconds.
81+
///
82+
/// **Note:** A minimum of 10 seconds is always enforced.
83+
pub onchain_wallet_sync_interval_secs: u64,
84+
/// The time in-between background sync attempts of the LDK wallet, in seconds.
85+
///
86+
/// **Note:** A minimum of 10 seconds is always enforced.
87+
pub wallet_sync_interval_secs: u64,
88+
/// The time in-between background update attempts to our fee rate cache, in seconds.
89+
///
90+
/// **Note:** A minimum of 10 seconds is always enforced.
91+
pub fee_rate_cache_update_interval_secs: u64,
92+
/// A list of peers that we allow to establish zero confirmation channels to us.
93+
///
94+
/// **Note:** Allowing payments via zero-confirmation channels is potentially insecure if the
95+
/// funding transaction ends up never being confirmed on-chain. Zero-confirmation channels
96+
/// should therefore only be accepted from trusted peers.
97+
pub trusted_peers_0conf: Vec<PublicKey>,
98+
/// The liquidity factor by which we filter the outgoing channels used for sending probes.
99+
///
100+
/// Channels with available liquidity less than the required amount times this value won't be
101+
/// used to send pre-flight probes.
102+
pub probing_liquidity_limit_multiplier: u64,
103+
/// The level at which we log messages.
104+
///
105+
/// Any messages below this level will be excluded from the logs.
106+
pub log_level: LogLevel,
107+
}
108+
109+
impl Default for Config {
110+
fn default() -> Self {
111+
Self {
112+
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
113+
log_dir_path: None,
114+
network: DEFAULT_NETWORK,
115+
listening_addresses: None,
116+
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,
117+
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
118+
wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
119+
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
120+
trusted_peers_0conf: Vec::new(),
121+
probing_liquidity_limit_multiplier: DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER,
122+
log_level: DEFAULT_LOG_LEVEL,
123+
}
124+
}
125+
}
126+
127+
/// Returns a [`Config`] object populated with default values.
128+
///
129+
/// See the documentation of [`Config`] for more information on the used defaults.
130+
///
131+
/// This is mostly meant for use in bindings, in Rust this is synonymous with
132+
/// [`Config::default()`].
133+
pub fn default_config() -> Config {
134+
Config::default()
135+
}

src/io/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use crate::WALLET_KEYS_SEED_LEN;
2+
use crate::config::WALLET_KEYS_SEED_LEN;
33

44
use crate::logger::log_error;
55
use crate::peer_store::PeerStore;

src/lib.rs

Lines changed: 11 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
mod balance;
7979
mod builder;
80+
mod config;
8081
mod error;
8182
mod event;
8283
mod fee_estimator;
@@ -101,6 +102,7 @@ pub use lightning;
101102
pub use lightning_invoice;
102103

103104
pub use balance::{BalanceDetails, LightningBalance, PendingSweepBalance};
105+
pub use config::{default_config, Config};
104106
pub use error::Error as NodeError;
105107
use error::Error;
106108

@@ -118,6 +120,10 @@ pub use builder::BuildError;
118120
#[cfg(not(feature = "uniffi"))]
119121
pub use builder::NodeBuilder as Builder;
120122

123+
use config::{
124+
LDK_PAYMENT_RETRY_TIMEOUT, NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
125+
RGS_SYNC_INTERVAL, WALLET_SYNC_INTERVAL_MINIMUM_SECS,
126+
};
121127
use event::{EventHandler, EventQueue};
122128
use gossip::GossipSource;
123129
use liquidity::LiquiditySource;
@@ -155,7 +161,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
155161
use bitcoin::hashes::Hash;
156162
use bitcoin::secp256k1::PublicKey;
157163

158-
use bitcoin::{Address, Network, Txid};
164+
use bitcoin::{Address, Txid};
159165

160166
use rand::Rng;
161167

@@ -167,133 +173,6 @@ use std::time::{Duration, Instant, SystemTime};
167173
#[cfg(feature = "uniffi")]
168174
uniffi::include_scaffolding!("ldk_node");
169175

170-
// Config defaults
171-
const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node/";
172-
const DEFAULT_NETWORK: Network = Network::Bitcoin;
173-
const DEFAULT_CLTV_EXPIRY_DELTA: u32 = 144;
174-
const DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS: u64 = 80;
175-
const DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS: u64 = 30;
176-
const DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS: u64 = 60 * 10;
177-
const DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER: u64 = 3;
178-
const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
179-
180-
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
181-
// number of derivation indexes after which BDK stops looking for new scripts belonging to the wallet.
182-
const BDK_CLIENT_STOP_GAP: usize = 20;
183-
184-
// The number of concurrent requests made against the API provider.
185-
const BDK_CLIENT_CONCURRENCY: u8 = 4;
186-
187-
// The default Esplora server we're using.
188-
const DEFAULT_ESPLORA_SERVER_URL: &str = "https://blockstream.info/api";
189-
190-
// The timeout after which we abandon retrying failed payments.
191-
const LDK_PAYMENT_RETRY_TIMEOUT: Duration = Duration::from_secs(10);
192-
193-
// The time in-between peer reconnection attempts.
194-
const PEER_RECONNECTION_INTERVAL: Duration = Duration::from_secs(10);
195-
196-
// The time in-between RGS sync attempts.
197-
const RGS_SYNC_INTERVAL: Duration = Duration::from_secs(60 * 60);
198-
199-
// The time in-between node announcement broadcast attempts.
200-
const NODE_ANN_BCAST_INTERVAL: Duration = Duration::from_secs(60 * 60);
201-
202-
// The lower limit which we apply to any configured wallet sync intervals.
203-
const WALLET_SYNC_INTERVAL_MINIMUM_SECS: u64 = 10;
204-
205-
// The length in bytes of our wallets' keys seed.
206-
const WALLET_KEYS_SEED_LEN: usize = 64;
207-
208-
#[derive(Debug, Clone)]
209-
/// Represents the configuration of an [`Node`] instance.
210-
///
211-
/// ### Defaults
212-
///
213-
/// | Parameter | Value |
214-
/// |----------------------------------------|--------------------|
215-
/// | `storage_dir_path` | /tmp/ldk_node/ |
216-
/// | `log_dir_path` | None |
217-
/// | `network` | Bitcoin |
218-
/// | `listening_addresses` | None |
219-
/// | `default_cltv_expiry_delta` | 144 |
220-
/// | `onchain_wallet_sync_interval_secs` | 80 |
221-
/// | `wallet_sync_interval_secs` | 30 |
222-
/// | `fee_rate_cache_update_interval_secs` | 600 |
223-
/// | `trusted_peers_0conf` | [] |
224-
/// | `probing_liquidity_limit_multiplier` | 3 |
225-
/// | `log_level` | Debug |
226-
///
227-
pub struct Config {
228-
/// The path where the underlying LDK and BDK persist their data.
229-
pub storage_dir_path: String,
230-
/// The path where logs are stored.
231-
///
232-
/// If set to `None`, logs can be found in the `logs` subdirectory in [`Config::storage_dir_path`].
233-
pub log_dir_path: Option<String>,
234-
/// The used Bitcoin network.
235-
pub network: Network,
236-
/// The addresses on which the node will listen for incoming connections.
237-
pub listening_addresses: Option<Vec<SocketAddress>>,
238-
/// The default CLTV expiry delta to be used for payments.
239-
pub default_cltv_expiry_delta: u32,
240-
/// The time in-between background sync attempts of the onchain wallet, in seconds.
241-
///
242-
/// **Note:** A minimum of 10 seconds is always enforced.
243-
pub onchain_wallet_sync_interval_secs: u64,
244-
/// The time in-between background sync attempts of the LDK wallet, in seconds.
245-
///
246-
/// **Note:** A minimum of 10 seconds is always enforced.
247-
pub wallet_sync_interval_secs: u64,
248-
/// The time in-between background update attempts to our fee rate cache, in seconds.
249-
///
250-
/// **Note:** A minimum of 10 seconds is always enforced.
251-
pub fee_rate_cache_update_interval_secs: u64,
252-
/// A list of peers that we allow to establish zero confirmation channels to us.
253-
///
254-
/// **Note:** Allowing payments via zero-confirmation channels is potentially insecure if the
255-
/// funding transaction ends up never being confirmed on-chain. Zero-confirmation channels
256-
/// should therefore only be accepted from trusted peers.
257-
pub trusted_peers_0conf: Vec<PublicKey>,
258-
/// The liquidity factor by which we filter the outgoing channels used for sending probes.
259-
///
260-
/// Channels with available liquidity less than the required amount times this value won't be
261-
/// used to send pre-flight probes.
262-
pub probing_liquidity_limit_multiplier: u64,
263-
/// The level at which we log messages.
264-
///
265-
/// Any messages below this level will be excluded from the logs.
266-
pub log_level: LogLevel,
267-
}
268-
269-
impl Default for Config {
270-
fn default() -> Self {
271-
Self {
272-
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
273-
log_dir_path: None,
274-
network: DEFAULT_NETWORK,
275-
listening_addresses: None,
276-
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,
277-
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
278-
wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
279-
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
280-
trusted_peers_0conf: Vec::new(),
281-
probing_liquidity_limit_multiplier: DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER,
282-
log_level: DEFAULT_LOG_LEVEL,
283-
}
284-
}
285-
}
286-
287-
/// Returns a [`Config`] object populated with default values.
288-
///
289-
/// See the documentation of [`Config`] for more information on the used defaults.
290-
///
291-
/// This is mostly meant for use in bindings, in Rust this is synonymous with
292-
/// [`Config::default()`].
293-
pub fn default_config() -> Config {
294-
Config::default()
295-
}
296-
297176
/// The main interface object of LDK Node, wrapping the necessary LDK and BDK functionalities.
298177
///
299178
/// Needs to be initialized and instantiated through [`Builder::build`].
@@ -369,8 +248,10 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
369248
let wallet = Arc::clone(&self.wallet);
370249
let sync_logger = Arc::clone(&self.logger);
371250
let mut stop_sync = self.stop_receiver.clone();
372-
let onchain_wallet_sync_interval_secs =
373-
self.config.onchain_wallet_sync_interval_secs.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
251+
let onchain_wallet_sync_interval_secs = self
252+
.config
253+
.onchain_wallet_sync_interval_secs
254+
.max(config::WALLET_SYNC_INTERVAL_MINIMUM_SECS);
374255
std::thread::spawn(move || {
375256
tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(
376257
async move {

src/uniffi_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub use lightning::ln::ChannelId;
33
pub use lightning::ln::PaymentSecret;
44
pub use lightning::util::string::UntrustedString;
55

6-
pub use bitcoin::{BlockHash, OutPoint};
6+
pub use bitcoin::{BlockHash, Network, OutPoint};
77

88
pub use bip39::Mnemonic;
99

0 commit comments

Comments
 (0)