use bdk_bitcoind_client::{Auth, Client};
use bitcoin::{Address, BlockHash};
use bitcoind::{BitcoinD, Conf, exe_path};
use corepc_types::bitcoin;
#[derive(Debug)]
pub struct TestEnv {
pub client: Client,
pub bitcoind: BitcoinD,
}
impl TestEnv {
pub fn setup() -> anyhow::Result<Self> {
let exe = exe_path()?;
let mut conf = Conf::default();
conf.args.push("-blockfilterindex=1");
conf.args.push("-txindex=1");
let bitcoind = BitcoinD::with_conf(exe, &conf)?;
let rpc_url = bitcoind.rpc_url();
let cookie_file = &bitcoind.params.cookie_file;
let auth = Auth::CookieFile(cookie_file.clone());
let client = Client::with_auth(&rpc_url, auth)?;
Ok(Self { client, bitcoind })
}
pub fn mine_blocks(
&self,
nblocks: usize,
address: Option<Address>,
) -> anyhow::Result<Vec<BlockHash>> {
let address = match address {
Some(addr) => addr,
None => self.bitcoind.client.new_address()?,
};
Ok(self
.bitcoind
.client
.generate_to_address(nblocks, &address)?
.into_model()?
.0)
}
}