Skip to content

Commit

Permalink
Merge bitcoindevkit#1545: feat(testenv): Add method new_with_config
Browse files Browse the repository at this point in the history
b3b6551 feat(testenv): Add method `new_with_config` (valued mammal)

Pull request description:

  I think this will be useful for passing a custom bitcoin config including e.g. `-blockfilterindex` that would not otherwise be included in the default config.

  ### Notes to the reviewers

  Any other suggestions let me know @LagginTimes

  ### Changelog notice

  * Added method `TestEnv::new_with_config` for passing custom `bitcoind::Conf`, `electrsd::Conf` config objects

  ### Checklists

  #### All Submissions:

  * [x] I've signed all my commits
  * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
  * [x] I ran `cargo fmt` and `cargo clippy` before committing

ACKs for top commit:
  LagginTimes:
    ACK b3b6551
  evanlinjin:
    ACK b3b6551

Tree-SHA512: f771505bc4e7096e89c697a18cfaa367e2ffa57616ca9f2d02d0ad7672c148bb0eb17f66b5e7ab2bbf16fcca4b7457c0c43d892119b0feb6c1462d4034a81a9b
  • Loading branch information
evanlinjin committed Aug 12, 2024
2 parents 98c4959 + b3b6551 commit d58d75e
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 d58d75e

Please sign in to comment.