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();
@@ -177,11 +177,19 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
177177
178178#[ derive( Debug , Clone ) ]
179179/// Represents the configuration of an [`Node`] instance.
180+ ///
181+ /// **Defaults:**
182+ /// |-----------------------------+------------------|
183+ /// | Parameter | Value |
184+ /// |-----------------------------+------------------|
185+ /// | `storage_dir_path` | "/tmp/ldk_node/" |
186+ /// | `network` | Network::Bitcoin |
187+ /// | `listening_address` | 0.0.0.0:9735 |
188+ /// | `default_cltv_expiry_delta` | 144 |
189+ /// |-----------------------------+------------------|
180190pub struct Config {
181191 /// The path where the underlying LDK and BDK persist their data.
182192 pub storage_dir_path : String ,
183- /// The URL of the utilized Esplora server.
184- pub esplora_server_url : String ,
185193 /// The used Bitcoin network.
186194 pub network : Network ,
187195 /// The IP address and TCP port the node will listen on.
@@ -194,14 +202,18 @@ impl Default for Config {
194202 fn default ( ) -> Self {
195203 Self {
196204 storage_dir_path : "/tmp/ldk_node/" . to_string ( ) ,
197- esplora_server_url : "http://localhost:3002" . to_string ( ) ,
198- network : Network :: Regtest ,
205+ network : Network :: Bitcoin ,
199206 listening_address : Some ( "0.0.0.0:9735" . parse ( ) . unwrap ( ) ) ,
200207 default_cltv_expiry_delta : 144 ,
201208 }
202209 }
203210}
204211
212+ #[ derive( Debug , Clone ) ]
213+ enum ChainDataSourceConfig {
214+ Esplora ( String ) ,
215+ }
216+
205217#[ derive( Debug , Clone ) ]
206218enum EntropySourceConfig {
207219 SeedFile ( String ) ,
@@ -217,9 +229,15 @@ enum GossipSourceConfig {
217229
218230/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
219231/// the getgo.
232+ ///
233+ /// **Defaults:**
234+ /// - Chain data is sourced from the Esplora endpoint `https://blockstream.info/api`
235+ /// - Wallet entropy is sourced from a `keys_seed` file located under [`Config::storage_dir_path`]
236+ /// - Gossip data is sourced via the peer-to-peer network
220237#[ derive( Debug ) ]
221238pub struct Builder {
222239 config : Mutex < Config > ,
240+ chain_data_source_config : Mutex < Option < ChainDataSourceConfig > > ,
223241 entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
224242 gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
225243}
@@ -228,17 +246,19 @@ impl Builder {
228246 /// Creates a new builder instance with the default configuration.
229247 pub fn new ( ) -> Self {
230248 let config = Mutex :: new ( Config :: default ( ) ) ;
249+ let chain_data_source_config = Mutex :: new ( None ) ;
231250 let entropy_source_config = Mutex :: new ( None ) ;
232251 let gossip_source_config = Mutex :: new ( None ) ;
233- Self { config, entropy_source_config, gossip_source_config }
252+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
234253 }
235254
236255 /// Creates a new builder instance from an [`Config`].
237256 pub fn from_config ( config : Config ) -> Self {
238257 let config = Mutex :: new ( config) ;
258+ let chain_data_source_config = Mutex :: new ( None ) ;
239259 let entropy_source_config = Mutex :: new ( None ) ;
240260 let gossip_source_config = Mutex :: new ( None ) ;
241- Self { config, entropy_source_config, gossip_source_config }
261+ Self { config, chain_data_source_config , entropy_source_config, gossip_source_config }
242262 }
243263
244264 /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
@@ -270,6 +290,12 @@ impl Builder {
270290 Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
271291 }
272292
293+ /// Configures the [`Node`] instance to source its chain data from the given Esplora server.
294+ pub fn set_esplora_server ( & self , esplora_server_url : String ) {
295+ * self . chain_data_source_config . lock ( ) . unwrap ( ) =
296+ Some ( ChainDataSourceConfig :: Esplora ( esplora_server_url) ) ;
297+ }
298+
273299 /// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
274300 /// network.
275301 pub fn set_gossip_source_p2p ( & self ) {
@@ -284,30 +310,18 @@ impl Builder {
284310 }
285311
286312 /// Sets the used storage directory path.
287- ///
288- /// Default: `/tmp/ldk_node/`
289313 pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
290314 let mut config = self . config . lock ( ) . unwrap ( ) ;
291315 config. storage_dir_path = storage_dir_path;
292316 }
293317
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-
302318 /// Sets the Bitcoin network used.
303319 pub fn set_network ( & self , network : Network ) {
304320 let mut config = self . config . lock ( ) . unwrap ( ) ;
305321 config. network = network;
306322 }
307323
308324 /// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
309- ///
310- /// Default: `0.0.0.0:9735`
311325 pub fn set_listening_address ( & self , listening_address : SocketAddr ) {
312326 let mut config = self . config . lock ( ) . unwrap ( ) ;
313327 config. listening_address = Some ( listening_address) ;
@@ -370,14 +384,30 @@ impl Builder {
370384 )
371385 . expect ( "Failed to set up on-chain wallet" ) ;
372386
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 ) ;
387+ let ( blockchain, tx_sync) = if let Some ( chain_data_source_config) =
388+ & * self . chain_data_source_config . lock ( ) . unwrap ( )
389+ {
390+ match chain_data_source_config {
391+ ChainDataSourceConfig :: Esplora ( server_url) => {
392+ let tx_sync =
393+ Arc :: new ( EsploraSyncClient :: new ( server_url. clone ( ) , Arc :: clone ( & logger) ) ) ;
394+ let blockchain = EsploraBlockchain :: from_client (
395+ tx_sync. client ( ) . clone ( ) ,
396+ BDK_CLIENT_STOP_GAP ,
397+ )
398+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
399+ ( blockchain, tx_sync)
400+ }
401+ }
402+ } else {
403+ // Default to Esplora client with blockstream endpoint.
404+ let server_url = "https://blockstream.info/api" . to_string ( ) ;
405+ let tx_sync = Arc :: new ( EsploraSyncClient :: new ( server_url, Arc :: clone ( & logger) ) ) ;
406+ let blockchain =
407+ EsploraBlockchain :: from_client ( tx_sync. client ( ) . clone ( ) , BDK_CLIENT_STOP_GAP )
408+ . with_concurrency ( BDK_CLIENT_CONCURRENCY ) ;
409+ ( blockchain, tx_sync)
410+ } ;
381411
382412 let runtime = Arc :: new ( RwLock :: new ( None ) ) ;
383413 let wallet = Arc :: new ( Wallet :: new (
0 commit comments