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