Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testenv): Add method new_with_config #1545

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading