From 19cafee7691443033a525a9abc2852b63103778d Mon Sep 17 00:00:00 2001 From: Wei Chen Date: Tue, 17 Oct 2023 18:07:03 +0800 Subject: [PATCH] chore(esplora): updated `esplora` tests to use `TestEnv` Updated the test cases in the `esplora` crate to use the `TestEnv` crate. --- crates/esplora/Cargo.toml | 3 ++ crates/esplora/tests/async_ext.rs | 70 +++++----------------------- crates/esplora/tests/blocking_ext.rs | 69 +++++---------------------- 3 files changed, 25 insertions(+), 117 deletions(-) diff --git a/crates/esplora/Cargo.toml b/crates/esplora/Cargo.toml index bc127c087a..3b22dd18e6 100644 --- a/crates/esplora/Cargo.toml +++ b/crates/esplora/Cargo.toml @@ -21,6 +21,9 @@ futures = { version = "0.3.26", optional = true } bitcoin = { version = "0.30.0", optional = true, default-features = false } miniscript = { version = "10.0.0", optional = true, default-features = false } +[dev-dependencies] +testenv = { path = "../testenv", version = "0.1.0", default_features = false } + [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] electrsd = { version= "0.25.0", features = ["bitcoind_25_0", "esplora_a33e97e1", "legacy"] } tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } diff --git a/crates/esplora/tests/async_ext.rs b/crates/esplora/tests/async_ext.rs index 3b64d7beea..351f76f76d 100644 --- a/crates/esplora/tests/async_ext.rs +++ b/crates/esplora/tests/async_ext.rs @@ -1,67 +1,20 @@ use bdk_esplora::EsploraAsyncExt; +use electrsd::bitcoind::anyhow; use electrsd::bitcoind::bitcoincore_rpc::RpcApi; -use electrsd::bitcoind::{self, anyhow, BitcoinD}; -use electrsd::{Conf, ElectrsD}; -use esplora_client::{self, AsyncClient, Builder}; +use esplora_client::{self, Builder}; use std::str::FromStr; use std::thread::sleep; use std::time::Duration; -use bdk_chain::bitcoin::{Address, Amount, BlockHash, Txid}; - -struct TestEnv { - bitcoind: BitcoinD, - #[allow(dead_code)] - electrsd: ElectrsD, - client: AsyncClient, -} - -impl TestEnv { - fn new() -> Result { - let bitcoind_exe = - bitcoind::downloaded_exe_path().expect("bitcoind version feature must be enabled"); - let bitcoind = BitcoinD::new(bitcoind_exe).unwrap(); - - let mut electrs_conf = Conf::default(); - electrs_conf.http_enabled = true; - let electrs_exe = - electrsd::downloaded_exe_path().expect("electrs version feature must be enabled"); - let electrsd = ElectrsD::with_conf(electrs_exe, &bitcoind, &electrs_conf)?; - - let base_url = format!("http://{}", &electrsd.esplora_url.clone().unwrap()); - let client = Builder::new(base_url.as_str()).build_async()?; - - Ok(Self { - bitcoind, - electrsd, - client, - }) - } - - fn mine_blocks( - &self, - count: usize, - address: Option
, - ) -> anyhow::Result> { - let coinbase_address = match address { - Some(address) => address, - None => self - .bitcoind - .client - .get_new_address(None, None)? - .assume_checked(), - }; - let block_hashes = self - .bitcoind - .client - .generate_to_address(count as _, &coinbase_address)?; - Ok(block_hashes) - } -} +use bdk_chain::bitcoin::{Address, Amount, Txid}; +use testenv::TestEnv; #[tokio::test] pub async fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> { let env = TestEnv::new()?; + let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap()); + let client = Builder::new(base_url.as_str()).build_async()?; + let receive_address0 = Address::from_str("bcrt1qc6fweuf4xjvz4x3gx3t9e0fh4hvqyu2qw4wvxm")?.assume_checked(); let receive_address1 = @@ -72,7 +25,7 @@ pub async fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> { receive_address1.script_pubkey(), ]; - let _block_hashes = env.mine_blocks(101, None)?; + let _block_hashes = env.mine_blocks(101, None, &env.bitcoind)?; let txid1 = env.bitcoind.client.send_to_address( &receive_address1, Amount::from_sat(10000), @@ -93,13 +46,12 @@ pub async fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> { Some(1), None, )?; - let _block_hashes = env.mine_blocks(1, None)?; - while env.client.get_height().await.unwrap() < 102 { + let _block_hashes = env.mine_blocks(1, None, &env.bitcoind)?; + while client.get_height().await.unwrap() < 102 { sleep(Duration::from_millis(10)) } - let graph_update = env - .client + let graph_update = client .scan_txs( misc_spks.into_iter(), vec![].into_iter(), diff --git a/crates/esplora/tests/blocking_ext.rs b/crates/esplora/tests/blocking_ext.rs index 6c319945ba..afa2c8cb1d 100644 --- a/crates/esplora/tests/blocking_ext.rs +++ b/crates/esplora/tests/blocking_ext.rs @@ -1,67 +1,20 @@ use bdk_esplora::EsploraExt; +use electrsd::bitcoind::anyhow; use electrsd::bitcoind::bitcoincore_rpc::RpcApi; -use electrsd::bitcoind::{self, anyhow, BitcoinD}; -use electrsd::{Conf, ElectrsD}; -use esplora_client::{self, BlockingClient, Builder}; +use esplora_client::{self, Builder}; use std::str::FromStr; use std::thread::sleep; use std::time::Duration; -use bdk_chain::bitcoin::{Address, Amount, BlockHash, Txid}; - -struct TestEnv { - bitcoind: BitcoinD, - #[allow(dead_code)] - electrsd: ElectrsD, - client: BlockingClient, -} - -impl TestEnv { - fn new() -> Result { - let bitcoind_exe = - bitcoind::downloaded_exe_path().expect("bitcoind version feature must be enabled"); - let bitcoind = BitcoinD::new(bitcoind_exe).unwrap(); - - let mut electrs_conf = Conf::default(); - electrs_conf.http_enabled = true; - let electrs_exe = - electrsd::downloaded_exe_path().expect("electrs version feature must be enabled"); - let electrsd = ElectrsD::with_conf(electrs_exe, &bitcoind, &electrs_conf)?; - - let base_url = format!("http://{}", &electrsd.esplora_url.clone().unwrap()); - let client = Builder::new(base_url.as_str()).build_blocking()?; - - Ok(Self { - bitcoind, - electrsd, - client, - }) - } - - fn mine_blocks( - &self, - count: usize, - address: Option
, - ) -> anyhow::Result> { - let coinbase_address = match address { - Some(address) => address, - None => self - .bitcoind - .client - .get_new_address(None, None)? - .assume_checked(), - }; - let block_hashes = self - .bitcoind - .client - .generate_to_address(count as _, &coinbase_address)?; - Ok(block_hashes) - } -} +use bdk_chain::bitcoin::{Address, Amount, Txid}; +use testenv::TestEnv; #[test] pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> { let env = TestEnv::new()?; + let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap()); + let client = Builder::new(base_url.as_str()).build_blocking()?; + let receive_address0 = Address::from_str("bcrt1qc6fweuf4xjvz4x3gx3t9e0fh4hvqyu2qw4wvxm")?.assume_checked(); let receive_address1 = @@ -72,7 +25,7 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> { receive_address1.script_pubkey(), ]; - let _block_hashes = env.mine_blocks(101, None)?; + let _block_hashes = env.mine_blocks(101, None, &env.bitcoind)?; let txid1 = env.bitcoind.client.send_to_address( &receive_address1, Amount::from_sat(10000), @@ -93,12 +46,12 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> { Some(1), None, )?; - let _block_hashes = env.mine_blocks(1, None)?; - while env.client.get_height().unwrap() < 102 { + let _block_hashes = env.mine_blocks(1, None, &env.bitcoind)?; + while client.get_height().unwrap() < 102 { sleep(Duration::from_millis(10)) } - let graph_update = env.client.scan_txs( + let graph_update = client.scan_txs( misc_spks.into_iter(), vec![].into_iter(), vec![].into_iter(),