Skip to content

Commit

Permalink
Allow multiple Blockchain variation by generic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rajarshimaitra committed Aug 17, 2022
1 parent 02be2ba commit 5bb51a7
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 153 deletions.
10 changes: 8 additions & 2 deletions src/blockchain/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,15 @@ mod test {
use crate::testutils::configurable_blockchain_tests::ConfigurableBlockchainTester;
use crate::wallet::{AddressIndex, Wallet};

fn init_blockchain(test_client: &TestClient) -> ElectrumBlockchain {
AnyBlockchain::Electrum(Box::new(ElectrumBlockchain::from(
electrum_client::new(&test_client.electrsd.electrum_url).unwrap(),
)))
}

make_blockchain_tests![
blockchain BlockchainType::ElectrumBlockchain,
tests (
init_blockchain,
tests(
test_sync_simple,
test_sync_stop_gap_20,
test_sync_before_and_after_receive,
Expand Down
14 changes: 12 additions & 2 deletions src/blockchain/esplora/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,20 @@ pub mod test {
#[cfg(feature = "test-esplora")]
use crate::testutils::blockchain_tests::BlockchainType;

fn init_blockchain(test_client: &TestClient) -> EsploraBlockchain {
EsploraBlockchain::new(
&format!(
"http://{}",
test_client.electrsd.esplora_url.as_ref().unwrap()
),
20,
)
}

#[cfg(feature = "test-esplora")]
make_blockchain_tests![
blockchain BlockchainType::EsploraBlockchain,
tests (
init_blockchain,
tests(
test_sync_simple,
test_sync_stop_gap_20,
test_sync_before_and_after_receive,
Expand Down
28 changes: 23 additions & 5 deletions src/blockchain/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,6 @@ pub mod test {
use crate::descriptor::into_wallet_descriptor_checked;
use crate::descriptor::AsDerived;
use crate::make_blockchain_tests;
use crate::testutils::blockchain_tests::BlockchainType;
use crate::testutils::blockchain_tests::TestClient;
use crate::wallet::utils::SecpCtx;

Expand All @@ -875,9 +874,28 @@ pub mod test {
use log::LevelFilter;
use miniscript::DescriptorTrait;

pub fn init_blockchain(test_client: &TestClient) -> RpcBlockchain {
let config = RpcConfig {
url: test_client.bitcoind.rpc_url(),
auth: Auth::Cookie {
file: test_client.bitcoind.params.cookie_file.clone(),
},
network: Network::Regtest,
wallet_name: format!(
"client-wallet-test-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
),
sync_params: Some(RpcSyncParams::default()),
};
RpcBlockchain::from_config(&config).unwrap()
}

make_blockchain_tests![
blockchain BlockchainType::RpcBlockchain,
tests (
init_blockchain,
tests(
test_sync_simple,
test_sync_stop_gap_20,
test_sync_before_and_after_receive,
Expand Down Expand Up @@ -905,8 +923,8 @@ pub mod test {

#[cfg(not(feature = "test-rpc-legacy"))]
make_blockchain_tests![
blockchain BlockchainType::RpcBlockchain,
tests (
init_blockchain,
tests(
test_send_to_bech32m_addr,
test_taproot_key_spend,
test_taproot_script_spend,
Expand Down
Loading

0 comments on commit 5bb51a7

Please sign in to comment.