3535//! fn main() {
3636//! let mut builder = Builder::new();
3737//! 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());
3939//!
4040//! let node = builder.build();
4141//! node.start().unwrap();
@@ -161,6 +161,13 @@ use std::time::{Duration, Instant, SystemTime};
161161
162162uniffi:: include_scaffolding!( "ldk_node" ) ;
163163
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+
164171// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
165172// number of blocks after which BDK stops looking for scripts belonging to the wallet.
166173const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -185,11 +192,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
185192
186193#[ derive( Debug , Clone ) ]
187194/// 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+ ///
188205pub struct Config {
189206 /// The path where the underlying LDK and BDK persist their data.
190207 pub storage_dir_path : String ,
191- /// The URL of the utilized Esplora server.
192- pub esplora_server_url : String ,
193208 /// The used Bitcoin network.
194209 pub network : Network ,
195210 /// The IP address and TCP port the node will listen on.
@@ -201,15 +216,19 @@ pub struct Config {
201216impl Default for Config {
202217 fn default ( ) -> Self {
203218 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 ,
209223 }
210224 }
211225}
212226
227+ #[ derive( Debug , Clone ) ]
228+ enum ChainDataSourceConfig {
229+ Esplora ( String ) ,
230+ }
231+
213232#[ derive( Debug , Clone ) ]
214233enum EntropySourceConfig {
215234 SeedFile ( String ) ,
@@ -225,9 +244,15 @@ enum GossipSourceConfig {
225244
226245/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
227246/// the getgo.
247+ ///
248+ /// ### Defaults
249+ /// - Chain data is sourced from the Esplora endpoint `https://blockstream.info/api`
250+ /// - Wallet entropy is sourced from a `keys_seed` file located under [`Config::storage_dir_path`]
251+ /// - Gossip data is sourced via the peer-to-peer network
228252#[ derive( Debug ) ]
229253pub struct Builder {
230254 config : Mutex < Config > ,
255+ chain_data_source_config : Mutex < Option < ChainDataSourceConfig > > ,
231256 entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
232257 gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
233258}
@@ -236,17 +261,19 @@ impl Builder {
236261 /// Creates a new builder instance with the default configuration.
237262 pub fn new ( ) -> Self {
238263 let config = Mutex :: new ( Config :: default ( ) ) ;
264+ let chain_data_source_config = Mutex :: new ( None ) ;
239265 let entropy_source_config = Mutex :: new ( None ) ;
240266 let gossip_source_config = Mutex :: new ( None ) ;
241- Self { config, entropy_source_config, gossip_source_config }
267+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
242268 }
243269
244270 /// Creates a new builder instance from an [`Config`].
245271 pub fn from_config ( config : Config ) -> Self {
246272 let config = Mutex :: new ( config) ;
273+ let chain_data_source_config = Mutex :: new ( None ) ;
247274 let entropy_source_config = Mutex :: new ( None ) ;
248275 let gossip_source_config = Mutex :: new ( None ) ;
249- Self { config, entropy_source_config, gossip_source_config }
276+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
250277 }
251278
252279 /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -278,6 +305,12 @@ impl Builder {
278305 Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
279306 }
280307
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 . lock ( ) . unwrap ( ) =
311+ Some ( ChainDataSourceConfig :: Esplora ( esplora_server_url) ) ;
312+ }
313+
281314 /// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
282315 /// network.
283316 pub fn set_gossip_source_p2p ( & self ) {
@@ -292,30 +325,18 @@ impl Builder {
292325 }
293326
294327 /// Sets the used storage directory path.
295- ///
296- /// Default: `/tmp/ldk_node/`
297328 pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
298329 let mut config = self . config . lock ( ) . unwrap ( ) ;
299330 config. storage_dir_path = storage_dir_path;
300331 }
301332
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 . lock ( ) . unwrap ( ) ;
307- config. esplora_server_url = esplora_server_url;
308- }
309-
310333 /// Sets the Bitcoin network used.
311334 pub fn set_network ( & self , network : Network ) {
312335 let mut config = self . config . lock ( ) . unwrap ( ) ;
313336 config. network = network;
314337 }
315338
316339 /// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
317- ///
318- /// Default: `0.0.0.0:9735`
319340 pub fn set_listening_address ( & self , listening_address : NetAddress ) {
320341 let mut config = self . config . lock ( ) . unwrap ( ) ;
321342 config. listening_address = Some ( listening_address) ;
@@ -389,14 +410,30 @@ impl Builder {
389410 )
390411 . expect ( "Failed to set up on-chain wallet" ) ;
391412
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 . lock ( ) . 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+ } ;
400437
401438 let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
402439 let wallet = Arc :: new ( Wallet :: new (
0 commit comments