]> Untitled Git - bdk/commitdiff
feat(testenv): Add method `new_with_config`
authorvalued mammal <valuedmammal@protonmail.com>
Fri, 9 Aug 2024 02:43:29 +0000 (22:43 -0400)
committervalued mammal <valuedmammal@protonmail.com>
Sun, 11 Aug 2024 23:53:55 +0000 (19:53 -0400)
This allows passing a custom config for either `BitcoinD` or
`ElectrsD`. We also add a new `pub struct Config` for holding
each of `bitcoind::Conf` and `electrsd::Conf`.

crates/testenv/src/lib.rs

index b0c75b30bacd2727b9839596c3c021b5e52abcf2..a1a9f176688c275559e3712be1323ba53b4a0667 100644 (file)
@@ -11,6 +11,8 @@ use bitcoincore_rpc::{
     bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules},
     RpcApi,
 };
+use electrsd::bitcoind::anyhow::Context;
+
 pub use electrsd;
 pub use electrsd::bitcoind;
 pub use electrsd::bitcoind::anyhow;
@@ -26,35 +28,52 @@ pub struct TestEnv {
     pub electrsd: electrsd::ElectrsD,
 }
 
+/// Configuration parameters.
+#[derive(Debug)]
+pub struct Config<'a> {
+    /// [`bitcoind::Conf`]
+    pub bitcoind: bitcoind::Conf<'a>,
+    /// [`electrsd::Conf`]
+    pub electrsd: electrsd::Conf<'a>,
+}
+
+impl<'a> Default for Config<'a> {
+    /// Use the default configuration plus set `http_enabled = true` for [`electrsd::Conf`]
+    /// which is required for testing `bdk_esplora`.
+    fn default() -> Self {
+        Self {
+            bitcoind: bitcoind::Conf::default(),
+            electrsd: {
+                let mut conf = electrsd::Conf::default();
+                conf.http_enabled = true;
+                conf
+            },
+        }
+    }
+}
+
 impl TestEnv {
-    /// Construct a new [`TestEnv`] instance with default configurations.
+    /// Construct a new [`TestEnv`] instance with the default configuration used by BDK.
     pub fn new() -> anyhow::Result<Self> {
-        let bitcoind = match std::env::var_os("BITCOIND_EXE") {
-            Some(bitcoind_path) => electrsd::bitcoind::BitcoinD::new(bitcoind_path),
-            None => {
-                let bitcoind_exe = electrsd::bitcoind::downloaded_exe_path()
-                    .expect(
+        TestEnv::new_with_config(Config::default())
+    }
+
+    /// Construct a new [`TestEnv`] instance with the provided [`Config`].
+    pub fn new_with_config(config: Config) -> anyhow::Result<Self> {
+        let bitcoind_exe = match std::env::var("BITCOIND_EXE") {
+            Ok(path) => path,
+            Err(_) => bitcoind::downloaded_exe_path().context(
                 "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature",
-                );
-                electrsd::bitcoind::BitcoinD::with_conf(
-                    bitcoind_exe,
-                    &electrsd::bitcoind::Conf::default(),
-                )
-            }
-        }?;
+            )?,
+        };
+        let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_exe, &config.bitcoind)?;
 
-        let mut electrsd_conf = electrsd::Conf::default();
-        electrsd_conf.http_enabled = true;
-        let electrsd = match std::env::var_os("ELECTRS_EXE") {
-            Some(env_electrs_exe) => {
-                electrsd::ElectrsD::with_conf(env_electrs_exe, &bitcoind, &electrsd_conf)
-            }
-            None => {
-                let electrs_exe = electrsd::downloaded_exe_path()
-                    .expect("electrs version feature must be enabled");
-                electrsd::ElectrsD::with_conf(electrs_exe, &bitcoind, &electrsd_conf)
-            }
-        }?;
+        let electrs_exe = match std::env::var("ELECTRS_EXE") {
+            Ok(path) => path,
+            Err(_) => electrsd::downloaded_exe_path()
+                .context("electrs version feature must be enabled")?,
+        };
+        let electrsd = electrsd::ElectrsD::with_conf(electrs_exe, &bitcoind, &config.electrsd)?;
 
         Ok(Self { bitcoind, electrsd })
     }