Skip to content

Commit b3b6551

Browse files
committed
feat(testenv): Add method new_with_config
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`.
1 parent 4545458 commit b3b6551

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

crates/testenv/src/lib.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use bitcoincore_rpc::{
1111
bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules},
1212
RpcApi,
1313
};
14+
use electrsd::bitcoind::anyhow::Context;
15+
1416
pub use electrsd;
1517
pub use electrsd::bitcoind;
1618
pub use electrsd::bitcoind::anyhow;
@@ -26,35 +28,52 @@ pub struct TestEnv {
2628
pub electrsd: electrsd::ElectrsD,
2729
}
2830

31+
/// Configuration parameters.
32+
#[derive(Debug)]
33+
pub struct Config<'a> {
34+
/// [`bitcoind::Conf`]
35+
pub bitcoind: bitcoind::Conf<'a>,
36+
/// [`electrsd::Conf`]
37+
pub electrsd: electrsd::Conf<'a>,
38+
}
39+
40+
impl<'a> Default for Config<'a> {
41+
/// Use the default configuration plus set `http_enabled = true` for [`electrsd::Conf`]
42+
/// which is required for testing `bdk_esplora`.
43+
fn default() -> Self {
44+
Self {
45+
bitcoind: bitcoind::Conf::default(),
46+
electrsd: {
47+
let mut conf = electrsd::Conf::default();
48+
conf.http_enabled = true;
49+
conf
50+
},
51+
}
52+
}
53+
}
54+
2955
impl TestEnv {
30-
/// Construct a new [`TestEnv`] instance with default configurations.
56+
/// Construct a new [`TestEnv`] instance with the default configuration used by BDK.
3157
pub fn new() -> anyhow::Result<Self> {
32-
let bitcoind = match std::env::var_os("BITCOIND_EXE") {
33-
Some(bitcoind_path) => electrsd::bitcoind::BitcoinD::new(bitcoind_path),
34-
None => {
35-
let bitcoind_exe = electrsd::bitcoind::downloaded_exe_path()
36-
.expect(
58+
TestEnv::new_with_config(Config::default())
59+
}
60+
61+
/// Construct a new [`TestEnv`] instance with the provided [`Config`].
62+
pub fn new_with_config(config: Config) -> anyhow::Result<Self> {
63+
let bitcoind_exe = match std::env::var("BITCOIND_EXE") {
64+
Ok(path) => path,
65+
Err(_) => bitcoind::downloaded_exe_path().context(
3766
"you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature",
38-
);
39-
electrsd::bitcoind::BitcoinD::with_conf(
40-
bitcoind_exe,
41-
&electrsd::bitcoind::Conf::default(),
42-
)
43-
}
44-
}?;
67+
)?,
68+
};
69+
let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_exe, &config.bitcoind)?;
4570

46-
let mut electrsd_conf = electrsd::Conf::default();
47-
electrsd_conf.http_enabled = true;
48-
let electrsd = match std::env::var_os("ELECTRS_EXE") {
49-
Some(env_electrs_exe) => {
50-
electrsd::ElectrsD::with_conf(env_electrs_exe, &bitcoind, &electrsd_conf)
51-
}
52-
None => {
53-
let electrs_exe = electrsd::downloaded_exe_path()
54-
.expect("electrs version feature must be enabled");
55-
electrsd::ElectrsD::with_conf(electrs_exe, &bitcoind, &electrsd_conf)
56-
}
57-
}?;
71+
let electrs_exe = match std::env::var("ELECTRS_EXE") {
72+
Ok(path) => path,
73+
Err(_) => electrsd::downloaded_exe_path()
74+
.context("electrs version feature must be enabled")?,
75+
};
76+
let electrsd = electrsd::ElectrsD::with_conf(electrs_exe, &bitcoind, &config.electrsd)?;
5877

5978
Ok(Self { bitcoind, electrsd })
6079
}

0 commit comments

Comments
 (0)