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();
@@ -177,11 +177,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
177
177
178
178
#[ derive( Debug , Clone ) ]
179
179
/// Represents the configuration of an [`Node`] instance.
180
+ ///
181
+ /// ### Defaults
182
+ ///
183
+ /// | Parameter | Value |
184
+ /// |-----------------------------|------------------|
185
+ /// | `storage_dir_path` | /tmp/ldk_node/ |
186
+ /// | `network` | Network::Bitcoin |
187
+ /// | `listening_address` | 0.0.0.0:9735 |
188
+ /// | `default_cltv_expiry_delta` | 144 |
189
+ ///
180
190
pub struct Config {
181
191
/// The path where the underlying LDK and BDK persist their data.
182
192
pub storage_dir_path : String ,
183
- /// The URL of the utilized Esplora server.
184
- pub esplora_server_url : String ,
185
193
/// The used Bitcoin network.
186
194
pub network : Network ,
187
195
/// The IP address and TCP port the node will listen on.
@@ -194,14 +202,18 @@ impl Default for Config {
194
202
fn default ( ) -> Self {
195
203
Self {
196
204
storage_dir_path : "/tmp/ldk_node/" . to_string ( ) ,
197
- esplora_server_url : "http://localhost:3002" . to_string ( ) ,
198
- network : Network :: Regtest ,
205
+ network : Network :: Bitcoin ,
199
206
listening_address : Some ( "0.0.0.0:9735" . parse ( ) . unwrap ( ) ) ,
200
207
default_cltv_expiry_delta : 144 ,
201
208
}
202
209
}
203
210
}
204
211
212
+ #[ derive( Debug , Clone ) ]
213
+ enum ChainDataSourceConfig {
214
+ Esplora ( String ) ,
215
+ }
216
+
205
217
#[ derive( Debug , Clone ) ]
206
218
enum EntropySourceConfig {
207
219
SeedFile ( String ) ,
@@ -217,9 +229,15 @@ enum GossipSourceConfig {
217
229
218
230
/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
219
231
/// the getgo.
232
+ ///
233
+ /// ### Defaults
234
+ /// - Chain data is sourced from the Esplora endpoint `https://blockstream.info/api`
235
+ /// - Wallet entropy is sourced from a `keys_seed` file located under [`Config::storage_dir_path`]
236
+ /// - Gossip data is sourced via the peer-to-peer network
220
237
#[ derive( Debug ) ]
221
238
pub struct Builder {
222
239
config : Mutex < Config > ,
240
+ chain_data_source_config : Mutex < Option < ChainDataSourceConfig > > ,
223
241
entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
224
242
gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
225
243
}
@@ -228,17 +246,19 @@ impl Builder {
228
246
/// Creates a new builder instance with the default configuration.
229
247
pub fn new ( ) -> Self {
230
248
let config = Mutex :: new ( Config :: default ( ) ) ;
249
+ let chain_data_source_config = Mutex :: new ( None ) ;
231
250
let entropy_source_config = Mutex :: new ( None ) ;
232
251
let gossip_source_config = Mutex :: new ( None ) ;
233
- Self { config, entropy_source_config, gossip_source_config }
252
+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
234
253
}
235
254
236
255
/// Creates a new builder instance from an [`Config`].
237
256
pub fn from_config ( config : Config ) -> Self {
238
257
let config = Mutex :: new ( config) ;
258
+ let chain_data_source_config = Mutex :: new ( None ) ;
239
259
let entropy_source_config = Mutex :: new ( None ) ;
240
260
let gossip_source_config = Mutex :: new ( None ) ;
241
- Self { config, entropy_source_config, gossip_source_config }
261
+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
242
262
}
243
263
244
264
/// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -270,6 +290,12 @@ impl Builder {
270
290
Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
271
291
}
272
292
293
+ /// Configures the [`Node`] instance to source its chain data from the given Esplora server.
294
+ pub fn set_esplora_server ( & self , esplora_server_url : String ) {
295
+ * self . chain_data_source_config . lock ( ) . unwrap ( ) =
296
+ Some ( ChainDataSourceConfig :: Esplora ( esplora_server_url) ) ;
297
+ }
298
+
273
299
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
274
300
/// network.
275
301
pub fn set_gossip_source_p2p ( & self ) {
@@ -284,30 +310,18 @@ impl Builder {
284
310
}
285
311
286
312
/// Sets the used storage directory path.
287
- ///
288
- /// Default: `/tmp/ldk_node/`
289
313
pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
290
314
let mut config = self . config . lock ( ) . unwrap ( ) ;
291
315
config. storage_dir_path = storage_dir_path;
292
316
}
293
317
294
- /// Sets the Esplora server URL.
295
- ///
296
- /// Default: `https://blockstream.info/api`
297
- pub fn set_esplora_server_url ( & self , esplora_server_url : String ) {
298
- let mut config = self . config . lock ( ) . unwrap ( ) ;
299
- config. esplora_server_url = esplora_server_url;
300
- }
301
-
302
318
/// Sets the Bitcoin network used.
303
319
pub fn set_network ( & self , network : Network ) {
304
320
let mut config = self . config . lock ( ) . unwrap ( ) ;
305
321
config. network = network;
306
322
}
307
323
308
324
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
309
- ///
310
- /// Default: `0.0.0.0:9735`
311
325
pub fn set_listening_address ( & self , listening_address : SocketAddr ) {
312
326
let mut config = self . config . lock ( ) . unwrap ( ) ;
313
327
config. listening_address = Some ( listening_address) ;
@@ -370,14 +384,30 @@ impl Builder {
370
384
)
371
385
. expect ( "Failed to set up on-chain wallet" ) ;
372
386
373
- let tx_sync = Arc :: new ( EsploraSyncClient :: new (
374
- config. esplora_server_url . clone ( ) ,
375
- Arc :: clone ( & logger) ,
376
- ) ) ;
377
-
378
- let blockchain =
379
- EsploraBlockchain :: from_client ( tx_sync. client ( ) . clone ( ) , BDK_CLIENT_STOP_GAP )
380
- . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
387
+ let ( blockchain, tx_sync) = if let Some ( chain_data_source_config) =
388
+ & * self . chain_data_source_config . lock ( ) . unwrap ( )
389
+ {
390
+ match chain_data_source_config {
391
+ ChainDataSourceConfig :: Esplora ( server_url) => {
392
+ let tx_sync =
393
+ Arc :: new ( EsploraSyncClient :: new ( server_url. clone ( ) , Arc :: clone ( & logger) ) ) ;
394
+ let blockchain = EsploraBlockchain :: from_client (
395
+ tx_sync. client ( ) . clone ( ) ,
396
+ BDK_CLIENT_STOP_GAP ,
397
+ )
398
+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
399
+ ( blockchain, tx_sync)
400
+ }
401
+ }
402
+ } else {
403
+ // Default to Esplora client with blockstream endpoint.
404
+ let server_url = "https://blockstream.info/api" . to_string ( ) ;
405
+ let tx_sync = Arc :: new ( EsploraSyncClient :: new ( server_url, Arc :: clone ( & logger) ) ) ;
406
+ let blockchain =
407
+ EsploraBlockchain :: from_client ( tx_sync. client ( ) . clone ( ) , BDK_CLIENT_STOP_GAP )
408
+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
409
+ ( blockchain, tx_sync)
410
+ } ;
381
411
382
412
let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
383
413
let wallet = Arc :: new ( Wallet :: new (
0 commit comments