35
35
//! fn main() {
36
36
//! let mut builder = Builder::new();
37
37
//! builder.set_network(Network::Testnet);
38
- //! builder.set_esplora_server_url ("https://blockstream.info/testnet/api".to_string());
38
+ //! builder.set_esplora_server ("https://blockstream.info/testnet/api".to_string());
39
39
//!
40
40
//! let node = builder.build();
41
41
//! node.start().unwrap();
@@ -161,6 +161,13 @@ use std::time::{Duration, Instant, SystemTime};
161
161
162
162
uniffi:: include_scaffolding!( "ldk_node" ) ;
163
163
164
+ // Config defaults
165
+ const DEFAULT_STORAGE_DIR_PATH : & str = "/tmp/ldk_node/" ;
166
+ const DEFAULT_NETWORK : Network = Network :: Bitcoin ;
167
+ const DEFAULT_LISTENING_ADDR : & str = "0.0.0.0:9735" ;
168
+ const DEFAULT_CLTV_EXPIRY_DELTA : u32 = 144 ;
169
+ const DEFAULT_ESPLORA_SERVER_URL : & str = "https://blockstream.info/api" ;
170
+
164
171
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
165
172
// number of blocks after which BDK stops looking for scripts belonging to the wallet.
166
173
const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -185,11 +192,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
185
192
186
193
#[ derive( Debug , Clone ) ]
187
194
/// Represents the configuration of an [`Node`] instance.
195
+ ///
196
+ /// ### Defaults
197
+ ///
198
+ /// | Parameter | Value |
199
+ /// |-----------------------------|------------------|
200
+ /// | `storage_dir_path` | /tmp/ldk_node/ |
201
+ /// | `network` | Network::Bitcoin |
202
+ /// | `listening_address` | 0.0.0.0:9735 |
203
+ /// | `default_cltv_expiry_delta` | 144 |
204
+ ///
188
205
pub struct Config {
189
206
/// The path where the underlying LDK and BDK persist their data.
190
207
pub storage_dir_path : String ,
191
- /// The URL of the utilized Esplora server.
192
- pub esplora_server_url : String ,
193
208
/// The used Bitcoin network.
194
209
pub network : Network ,
195
210
/// The IP address and TCP port the node will listen on.
@@ -201,15 +216,19 @@ pub struct Config {
201
216
impl Default for Config {
202
217
fn default ( ) -> Self {
203
218
Self {
204
- storage_dir_path : "/tmp/ldk_node/" . to_string ( ) ,
205
- esplora_server_url : "http://localhost:3002" . to_string ( ) ,
206
- network : Network :: Regtest ,
207
- listening_address : Some ( "0.0.0.0:9735" . parse ( ) . unwrap ( ) ) ,
208
- default_cltv_expiry_delta : 144 ,
219
+ storage_dir_path : DEFAULT_STORAGE_DIR_PATH . to_string ( ) ,
220
+ network : DEFAULT_NETWORK ,
221
+ listening_address : Some ( DEFAULT_LISTENING_ADDR . parse ( ) . unwrap ( ) ) ,
222
+ default_cltv_expiry_delta : DEFAULT_CLTV_EXPIRY_DELTA ,
209
223
}
210
224
}
211
225
}
212
226
227
+ #[ derive( Debug , Clone ) ]
228
+ enum ChainDataSourceConfig {
229
+ Esplora ( String ) ,
230
+ }
231
+
213
232
#[ derive( Debug , Clone ) ]
214
233
enum EntropySourceConfig {
215
234
SeedFile ( String ) ,
@@ -225,10 +244,16 @@ enum GossipSourceConfig {
225
244
226
245
/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
227
246
/// the getgo.
247
+ ///
248
+ /// ### Defaults
249
+ /// - Wallet entropy is sourced from a `keys_seed` file located under [`Config::storage_dir_path`]
250
+ /// - Chain data is sourced from the Esplora endpoint `https://blockstream.info/api`
251
+ /// - Gossip data is sourced via the peer-to-peer network
228
252
#[ derive( Debug ) ]
229
253
pub struct Builder {
230
254
config : RwLock < Config > ,
231
255
entropy_source_config : RwLock < Option < EntropySourceConfig > > ,
256
+ chain_data_source_config : RwLock < Option < ChainDataSourceConfig > > ,
232
257
gossip_source_config : RwLock < Option < GossipSourceConfig > > ,
233
258
}
234
259
@@ -237,16 +262,18 @@ impl Builder {
237
262
pub fn new ( ) -> Self {
238
263
let config = RwLock :: new ( Config :: default ( ) ) ;
239
264
let entropy_source_config = RwLock :: new ( None ) ;
265
+ let chain_data_source_config = RwLock :: new ( None ) ;
240
266
let gossip_source_config = RwLock :: new ( None ) ;
241
- Self { config, entropy_source_config, gossip_source_config }
267
+ Self { config, entropy_source_config, chain_data_source_config , gossip_source_config }
242
268
}
243
269
244
270
/// Creates a new builder instance from an [`Config`].
245
271
pub fn from_config ( config : Config ) -> Self {
246
272
let config = RwLock :: new ( config) ;
247
273
let entropy_source_config = RwLock :: new ( None ) ;
274
+ let chain_data_source_config = RwLock :: new ( None ) ;
248
275
let gossip_source_config = RwLock :: new ( None ) ;
249
- Self { config, entropy_source_config, gossip_source_config }
276
+ Self { config, entropy_source_config, chain_data_source_config , gossip_source_config }
250
277
}
251
278
252
279
/// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -278,6 +305,12 @@ impl Builder {
278
305
Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
279
306
}
280
307
308
+ /// Configures the [`Node`] instance to source its chain data from the given Esplora server.
309
+ pub fn set_esplora_server ( & self , esplora_server_url : String ) {
310
+ * self . chain_data_source_config . write ( ) . unwrap ( ) =
311
+ Some ( ChainDataSourceConfig :: Esplora ( esplora_server_url) ) ;
312
+ }
313
+
281
314
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
282
315
/// network.
283
316
pub fn set_gossip_source_p2p ( & self ) {
@@ -292,30 +325,18 @@ impl Builder {
292
325
}
293
326
294
327
/// Sets the used storage directory path.
295
- ///
296
- /// Default: `/tmp/ldk_node/`
297
328
pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
298
329
let mut config = self . config . write ( ) . unwrap ( ) ;
299
330
config. storage_dir_path = storage_dir_path;
300
331
}
301
332
302
- /// Sets the Esplora server URL.
303
- ///
304
- /// Default: `https://blockstream.info/api`
305
- pub fn set_esplora_server_url ( & self , esplora_server_url : String ) {
306
- let mut config = self . config . write ( ) . unwrap ( ) ;
307
- config. esplora_server_url = esplora_server_url;
308
- }
309
-
310
333
/// Sets the Bitcoin network used.
311
334
pub fn set_network ( & self , network : Network ) {
312
335
let mut config = self . config . write ( ) . unwrap ( ) ;
313
336
config. network = network;
314
337
}
315
338
316
339
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
317
- ///
318
- /// Default: `0.0.0.0:9735`
319
340
pub fn set_listening_address ( & self , listening_address : NetAddress ) {
320
341
let mut config = self . config . write ( ) . unwrap ( ) ;
321
342
config. listening_address = Some ( listening_address) ;
@@ -389,14 +410,30 @@ impl Builder {
389
410
)
390
411
. expect ( "Failed to set up on-chain wallet" ) ;
391
412
392
- let tx_sync = Arc :: new ( EsploraSyncClient :: new (
393
- config. esplora_server_url . clone ( ) ,
394
- Arc :: clone ( & logger) ,
395
- ) ) ;
396
-
397
- let blockchain =
398
- EsploraBlockchain :: from_client ( tx_sync. client ( ) . clone ( ) , BDK_CLIENT_STOP_GAP )
399
- . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
413
+ let ( blockchain, tx_sync) = if let Some ( chain_data_source_config) =
414
+ & * self . chain_data_source_config . read ( ) . unwrap ( )
415
+ {
416
+ match chain_data_source_config {
417
+ ChainDataSourceConfig :: Esplora ( server_url) => {
418
+ let tx_sync =
419
+ Arc :: new ( EsploraSyncClient :: new ( server_url. clone ( ) , Arc :: clone ( & logger) ) ) ;
420
+ let blockchain = EsploraBlockchain :: from_client (
421
+ tx_sync. client ( ) . clone ( ) ,
422
+ BDK_CLIENT_STOP_GAP ,
423
+ )
424
+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
425
+ ( blockchain, tx_sync)
426
+ }
427
+ }
428
+ } else {
429
+ // Default to Esplora client with blockstream endpoint.
430
+ let server_url = DEFAULT_ESPLORA_SERVER_URL . to_string ( ) ;
431
+ let tx_sync = Arc :: new ( EsploraSyncClient :: new ( server_url, Arc :: clone ( & logger) ) ) ;
432
+ let blockchain =
433
+ EsploraBlockchain :: from_client ( tx_sync. client ( ) . clone ( ) , BDK_CLIENT_STOP_GAP )
434
+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
435
+ ( blockchain, tx_sync)
436
+ } ;
400
437
401
438
let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
402
439
let wallet = Arc :: new ( Wallet :: new (
0 commit comments