Skip to content

Commit

Permalink
feat(testenv): Add method new_with_config
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ValuedMammal committed Aug 11, 2024
1 parent 4545458 commit b3b6551
Showing 1 changed file with 44 additions and 25 deletions.
69 changes: 44 additions & 25 deletions crates/testenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 })
}
Expand Down

0 comments on commit b3b6551

Please sign in to comment.