Skip to content

Commit 9047014

Browse files
Allow multiple Blockchain variation by generic
Instead of the BlockChainType macro, this adds a init_blockchain generic function which creates a BlockChain type from the a given TestClient. This now can be used for external users of custom BlockChain to run the tests.
1 parent c21b2a7 commit 9047014

File tree

4 files changed

+231
-153
lines changed

4 files changed

+231
-153
lines changed

src/blockchain/electrum.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,15 @@ mod test {
333333
use crate::testutils::configurable_blockchain_tests::ConfigurableBlockchainTester;
334334
use crate::wallet::{AddressIndex, Wallet};
335335

336+
fn init_blockchain(test_client: &TestClient) -> ElectrumBlockchain {
337+
AnyBlockchain::Electrum(Box::new(ElectrumBlockchain::from(
338+
electrum_client::new(&test_client.electrsd.electrum_url).unwrap(),
339+
)))
340+
}
341+
336342
make_blockchain_tests![
337-
blockchain BlockchainType::ElectrumBlockchain,
338-
tests (
343+
init_blockchain,
344+
tests(
339345
test_sync_simple,
340346
test_sync_stop_gap_20,
341347
test_sync_before_and_after_receive,

src/blockchain/esplora/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,20 @@ pub mod test {
159159
#[cfg(feature = "test-esplora")]
160160
use crate::testutils::blockchain_tests::BlockchainType;
161161

162+
fn init_blockchain(test_client: &TestClient) -> EsploraBlockchain {
163+
EsploraBlockchain::new(
164+
&format!(
165+
"http://{}",
166+
test_client.electrsd.esplora_url.as_ref().unwrap()
167+
),
168+
20,
169+
)
170+
}
171+
162172
#[cfg(feature = "test-esplora")]
163173
make_blockchain_tests![
164-
blockchain BlockchainType::EsploraBlockchain,
165-
tests (
174+
init_blockchain,
175+
tests(
166176
test_sync_simple,
167177
test_sync_stop_gap_20,
168178
test_sync_before_and_after_receive,

src/blockchain/rpc.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,6 @@ pub mod test {
866866
use crate::descriptor::into_wallet_descriptor_checked;
867867
use crate::descriptor::AsDerived;
868868
use crate::make_blockchain_tests;
869-
use crate::testutils::blockchain_tests::BlockchainType;
870869
use crate::testutils::blockchain_tests::TestClient;
871870
use crate::wallet::utils::SecpCtx;
872871

@@ -875,9 +874,28 @@ pub mod test {
875874
use log::LevelFilter;
876875
use miniscript::DescriptorTrait;
877876

877+
pub fn init_blockchain(test_client: &TestClient) -> RpcBlockchain {
878+
let config = RpcConfig {
879+
url: test_client.bitcoind.rpc_url(),
880+
auth: Auth::Cookie {
881+
file: test_client.bitcoind.params.cookie_file.clone(),
882+
},
883+
network: Network::Regtest,
884+
wallet_name: format!(
885+
"client-wallet-test-{}",
886+
std::time::SystemTime::now()
887+
.duration_since(std::time::UNIX_EPOCH)
888+
.unwrap()
889+
.as_nanos()
890+
),
891+
sync_params: Some(RpcSyncParams::default()),
892+
};
893+
RpcBlockchain::from_config(&config).unwrap()
894+
}
895+
878896
make_blockchain_tests![
879-
blockchain BlockchainType::RpcBlockchain,
880-
tests (
897+
init_blockchain,
898+
tests(
881899
test_sync_simple,
882900
test_sync_stop_gap_20,
883901
test_sync_before_and_after_receive,
@@ -905,8 +923,8 @@ pub mod test {
905923

906924
#[cfg(not(feature = "test-rpc-legacy"))]
907925
make_blockchain_tests![
908-
blockchain BlockchainType::RpcBlockchain,
909-
tests (
926+
init_blockchain,
927+
tests(
910928
test_send_to_bech32m_addr,
911929
test_taproot_key_spend,
912930
test_taproot_script_spend,

0 commit comments

Comments
 (0)