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();
@@ -159,6 +159,13 @@ use std::time::{Duration, Instant, SystemTime};
159159
160160uniffi:: include_scaffolding!( "ldk_node" ) ;
161161
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+
162169// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
163170// number of blocks after which BDK stops looking for scripts belonging to the wallet.
164171const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -177,11 +184,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
177184
178185#[ derive( Debug , Clone ) ]
179186/// 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+ ///
180197pub struct Config {
181198 /// The path where the underlying LDK and BDK persist their data.
182199 pub storage_dir_path : String ,
183- /// The URL of the utilized Esplora server.
184- pub esplora_server_url : String ,
185200 /// The used Bitcoin network.
186201 pub network : Network ,
187202 /// The IP address and TCP port the node will listen on.
@@ -193,15 +208,19 @@ pub struct Config {
193208impl Default for Config {
194209 fn default ( ) -> Self {
195210 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 ,
201215 }
202216 }
203217}
204218
219+ #[ derive( Debug , Clone ) ]
220+ enum ChainDataSourceConfig {
221+ Esplora ( String ) ,
222+ }
223+
205224#[ derive( Debug , Clone ) ]
206225enum EntropySourceConfig {
207226 SeedFile ( String ) ,
@@ -217,9 +236,15 @@ enum GossipSourceConfig {
217236
218237/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
219238/// 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
220244#[ derive( Debug ) ]
221245pub struct Builder {
222246 config : Mutex < Config > ,
247+ chain_data_source_config : Mutex < Option < ChainDataSourceConfig > > ,
223248 entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
224249 gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
225250}
@@ -228,17 +253,19 @@ impl Builder {
228253 /// Creates a new builder instance with the default configuration.
229254 pub fn new ( ) -> Self {
230255 let config = Mutex :: new ( Config :: default ( ) ) ;
256+ let chain_data_source_config = Mutex :: new ( None ) ;
231257 let entropy_source_config = Mutex :: new ( None ) ;
232258 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 }
234260 }
235261
236262 /// Creates a new builder instance from an [`Config`].
237263 pub fn from_config ( config : Config ) -> Self {
238264 let config = Mutex :: new ( config) ;
265+ let chain_data_source_config = Mutex :: new ( None ) ;
239266 let entropy_source_config = Mutex :: new ( None ) ;
240267 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 }
242269 }
243270
244271 /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -270,6 +297,12 @@ impl Builder {
270297 Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
271298 }
272299
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+
273306 /// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
274307 /// network.
275308 pub fn set_gossip_source_p2p ( & self ) {
@@ -284,30 +317,18 @@ impl Builder {
284317 }
285318
286319 /// Sets the used storage directory path.
287- ///
288- /// Default: `/tmp/ldk_node/`
289320 pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
290321 let mut config = self . config . lock ( ) . unwrap ( ) ;
291322 config. storage_dir_path = storage_dir_path;
292323 }
293324
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-
302325 /// Sets the Bitcoin network used.
303326 pub fn set_network ( & self , network : Network ) {
304327 let mut config = self . config . lock ( ) . unwrap ( ) ;
305328 config. network = network;
306329 }
307330
308331 /// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
309- ///
310- /// Default: `0.0.0.0:9735`
311332 pub fn set_listening_address ( & self , listening_address : SocketAddr ) {
312333 let mut config = self . config . lock ( ) . unwrap ( ) ;
313334 config. listening_address = Some ( listening_address) ;
@@ -370,14 +391,30 @@ impl Builder {
370391 )
371392 . expect ( "Failed to set up on-chain wallet" ) ;
372393
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+ } ;
381418
382419 let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
383420 let wallet = Arc :: new ( Wallet :: new (
0 commit comments