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 ;
@@ -182,11 +189,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
182189
183190#[ derive( Debug , Clone ) ]
184191/// 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+ ///
185202pub struct Config {
186203 /// The path where the underlying LDK and BDK persist their data.
187204 pub storage_dir_path : String ,
188- /// The URL of the utilized Esplora server.
189- pub esplora_server_url : String ,
190205 /// The used Bitcoin network.
191206 pub network : Network ,
192207 /// The IP address and TCP port the node will listen on.
@@ -198,15 +213,19 @@ pub struct Config {
198213impl Default for Config {
199214 fn default ( ) -> Self {
200215 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 ,
206220 }
207221 }
208222}
209223
224+ #[ derive( Debug , Clone ) ]
225+ enum ChainDataSourceConfig {
226+ Esplora ( String ) ,
227+ }
228+
210229#[ derive( Debug , Clone ) ]
211230enum EntropySourceConfig {
212231 SeedFile ( String ) ,
@@ -222,9 +241,15 @@ enum GossipSourceConfig {
222241
223242/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
224243/// 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
225249#[ derive( Debug ) ]
226250pub struct Builder {
227251 config : Mutex < Config > ,
252+ chain_data_source_config : Mutex < Option < ChainDataSourceConfig > > ,
228253 entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
229254 gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
230255}
@@ -233,17 +258,19 @@ impl Builder {
233258 /// Creates a new builder instance with the default configuration.
234259 pub fn new ( ) -> Self {
235260 let config = Mutex :: new ( Config :: default ( ) ) ;
261+ let chain_data_source_config = Mutex :: new ( None ) ;
236262 let entropy_source_config = Mutex :: new ( None ) ;
237263 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 }
239265 }
240266
241267 /// Creates a new builder instance from an [`Config`].
242268 pub fn from_config ( config : Config ) -> Self {
243269 let config = Mutex :: new ( config) ;
270+ let chain_data_source_config = Mutex :: new ( None ) ;
244271 let entropy_source_config = Mutex :: new ( None ) ;
245272 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 }
247274 }
248275
249276 /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -275,6 +302,12 @@ impl Builder {
275302 Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
276303 }
277304
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+
278311 /// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
279312 /// network.
280313 pub fn set_gossip_source_p2p ( & self ) {
@@ -289,30 +322,18 @@ impl Builder {
289322 }
290323
291324 /// Sets the used storage directory path.
292- ///
293- /// Default: `/tmp/ldk_node/`
294325 pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
295326 let mut config = self . config . lock ( ) . unwrap ( ) ;
296327 config. storage_dir_path = storage_dir_path;
297328 }
298329
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-
307330 /// Sets the Bitcoin network used.
308331 pub fn set_network ( & self , network : Network ) {
309332 let mut config = self . config . lock ( ) . unwrap ( ) ;
310333 config. network = network;
311334 }
312335
313336 /// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
314- ///
315- /// Default: `0.0.0.0:9735`
316337 pub fn set_listening_address ( & self , listening_address : NetAddress ) {
317338 let mut config = self . config . lock ( ) . unwrap ( ) ;
318339 config. listening_address = Some ( listening_address) ;
@@ -375,14 +396,30 @@ impl Builder {
375396 )
376397 . expect ( "Failed to set up on-chain wallet" ) ;
377398
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+ } ;
386423
387424 let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
388425 let wallet = Arc :: new ( Wallet :: new (
0 commit comments