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();
@@ -159,6 +159,13 @@ use std::time::{Duration, Instant, SystemTime};
159
159
160
160
uniffi:: include_scaffolding!( "ldk_node" ) ;
161
161
162
+ // Config defaults
163
+ const DEFAULT_STORAGE_DIR_PATH : & str = "/tmp/ldk_node/" ;
164
+ const DEFAULT_NETWORK : Network = Network :: Bitcoin ;
165
+ const DEFAULT_LISTENING_ADDR : & str = "0.0.0.0:9735" ;
166
+ const DEFAULT_CLTV_EXPIRY_DELTA : u32 = 144 ;
167
+ const DEFAULT_ESPLORA_SERVER_URL : & str = "https://blockstream.info/api" ;
168
+
162
169
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
163
170
// number of blocks after which BDK stops looking for scripts belonging to the wallet.
164
171
const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -177,11 +184,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
177
184
178
185
#[ derive( Debug , Clone ) ]
179
186
/// Represents the configuration of an [`Node`] instance.
187
+ ///
188
+ /// ### Defaults
189
+ ///
190
+ /// | Parameter | Value |
191
+ /// |-----------------------------|------------------|
192
+ /// | `storage_dir_path` | /tmp/ldk_node/ |
193
+ /// | `network` | Network::Bitcoin |
194
+ /// | `listening_address` | 0.0.0.0:9735 |
195
+ /// | `default_cltv_expiry_delta` | 144 |
196
+ ///
180
197
pub struct Config {
181
198
/// The path where the underlying LDK and BDK persist their data.
182
199
pub storage_dir_path : String ,
183
- /// The URL of the utilized Esplora server.
184
- pub esplora_server_url : String ,
185
200
/// The used Bitcoin network.
186
201
pub network : Network ,
187
202
/// The IP address and TCP port the node will listen on.
@@ -193,15 +208,19 @@ pub struct Config {
193
208
impl Default for Config {
194
209
fn default ( ) -> Self {
195
210
Self {
196
- storage_dir_path : "/tmp/ldk_node/" . to_string ( ) ,
197
- esplora_server_url : "http://localhost:3002" . to_string ( ) ,
198
- network : Network :: Regtest ,
199
- listening_address : Some ( "0.0.0.0:9735" . parse ( ) . unwrap ( ) ) ,
200
- default_cltv_expiry_delta : 144 ,
211
+ storage_dir_path : DEFAULT_STORAGE_DIR_PATH . to_string ( ) ,
212
+ network : DEFAULT_NETWORK ,
213
+ listening_address : Some ( DEFAULT_LISTENING_ADDR . parse ( ) . unwrap ( ) ) ,
214
+ default_cltv_expiry_delta : DEFAULT_CLTV_EXPIRY_DELTA ,
201
215
}
202
216
}
203
217
}
204
218
219
+ #[ derive( Debug , Clone ) ]
220
+ enum ChainDataSourceConfig {
221
+ Esplora ( String ) ,
222
+ }
223
+
205
224
#[ derive( Debug , Clone ) ]
206
225
enum EntropySourceConfig {
207
226
SeedFile ( String ) ,
@@ -217,9 +236,15 @@ enum GossipSourceConfig {
217
236
218
237
/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
219
238
/// the getgo.
239
+ ///
240
+ /// ### Defaults
241
+ /// - Chain data is sourced from the Esplora endpoint `https://blockstream.info/api`
242
+ /// - Wallet entropy is sourced from a `keys_seed` file located under [`Config::storage_dir_path`]
243
+ /// - Gossip data is sourced via the peer-to-peer network
220
244
#[ derive( Debug ) ]
221
245
pub struct Builder {
222
246
config : Mutex < Config > ,
247
+ chain_data_source_config : Mutex < Option < ChainDataSourceConfig > > ,
223
248
entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
224
249
gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
225
250
}
@@ -228,17 +253,19 @@ impl Builder {
228
253
/// Creates a new builder instance with the default configuration.
229
254
pub fn new ( ) -> Self {
230
255
let config = Mutex :: new ( Config :: default ( ) ) ;
256
+ let chain_data_source_config = Mutex :: new ( None ) ;
231
257
let entropy_source_config = Mutex :: new ( None ) ;
232
258
let gossip_source_config = Mutex :: new ( None ) ;
233
- Self { config, entropy_source_config, gossip_source_config }
259
+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
234
260
}
235
261
236
262
/// Creates a new builder instance from an [`Config`].
237
263
pub fn from_config ( config : Config ) -> Self {
238
264
let config = Mutex :: new ( config) ;
265
+ let chain_data_source_config = Mutex :: new ( None ) ;
239
266
let entropy_source_config = Mutex :: new ( None ) ;
240
267
let gossip_source_config = Mutex :: new ( None ) ;
241
- Self { config, entropy_source_config, gossip_source_config }
268
+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
242
269
}
243
270
244
271
/// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -270,6 +297,12 @@ impl Builder {
270
297
Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
271
298
}
272
299
300
+ /// Configures the [`Node`] instance to source its chain data from the given Esplora server.
301
+ pub fn set_esplora_server ( & self , esplora_server_url : String ) {
302
+ * self . chain_data_source_config . lock ( ) . unwrap ( ) =
303
+ Some ( ChainDataSourceConfig :: Esplora ( esplora_server_url) ) ;
304
+ }
305
+
273
306
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
274
307
/// network.
275
308
pub fn set_gossip_source_p2p ( & self ) {
@@ -284,30 +317,18 @@ impl Builder {
284
317
}
285
318
286
319
/// Sets the used storage directory path.
287
- ///
288
- /// Default: `/tmp/ldk_node/`
289
320
pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
290
321
let mut config = self . config . lock ( ) . unwrap ( ) ;
291
322
config. storage_dir_path = storage_dir_path;
292
323
}
293
324
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
325
/// Sets the Bitcoin network used.
303
326
pub fn set_network ( & self , network : Network ) {
304
327
let mut config = self . config . lock ( ) . unwrap ( ) ;
305
328
config. network = network;
306
329
}
307
330
308
331
/// 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
332
pub fn set_listening_address ( & self , listening_address : SocketAddr ) {
312
333
let mut config = self . config . lock ( ) . unwrap ( ) ;
313
334
config. listening_address = Some ( listening_address) ;
@@ -370,14 +391,30 @@ impl Builder {
370
391
)
371
392
. expect ( "Failed to set up on-chain wallet" ) ;
372
393
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 ) ;
394
+ let ( blockchain, tx_sync) = if let Some ( chain_data_source_config) =
395
+ & * self . chain_data_source_config . lock ( ) . unwrap ( )
396
+ {
397
+ match chain_data_source_config {
398
+ ChainDataSourceConfig :: Esplora ( server_url) => {
399
+ let tx_sync =
400
+ Arc :: new ( EsploraSyncClient :: new ( server_url. clone ( ) , Arc :: clone ( & logger) ) ) ;
401
+ let blockchain = EsploraBlockchain :: from_client (
402
+ tx_sync. client ( ) . clone ( ) ,
403
+ BDK_CLIENT_STOP_GAP ,
404
+ )
405
+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
406
+ ( blockchain, tx_sync)
407
+ }
408
+ }
409
+ } else {
410
+ // Default to Esplora client with blockstream endpoint.
411
+ let server_url = DEFAULT_ESPLORA_SERVER_URL . to_string ( ) ;
412
+ let tx_sync = Arc :: new ( EsploraSyncClient :: new ( server_url, Arc :: clone ( & logger) ) ) ;
413
+ let blockchain =
414
+ EsploraBlockchain :: from_client ( tx_sync. client ( ) . clone ( ) , BDK_CLIENT_STOP_GAP )
415
+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
416
+ ( blockchain, tx_sync)
417
+ } ;
381
418
382
419
let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
383
420
let wallet = Arc :: new ( Wallet :: new (
0 commit comments