Skip to content

Commit 325c936

Browse files
committed
Randomize chain sources in tests
.. all of our tests should be robust against switching chain sources. We here opt to pick a random one each time to considerably extend our test coverage, instead of just running some cases against non-Esplora chain sources. Signed-off-by: Elias Rohrer <dev@tnull.de>
1 parent b476e05 commit 325c936

4 files changed

Lines changed: 68 additions & 78 deletions

File tree

benches/payments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bitcoin::hex::DisplayHex;
88
use bitcoin::Amount;
99
use common::{
1010
expect_channel_ready_event, generate_blocks_and_wait, premine_and_distribute_funds,
11-
setup_bitcoind_and_electrsd, setup_two_nodes_with_store, TestChainSource,
11+
random_chain_source, setup_bitcoind_and_electrsd, setup_two_nodes_with_store,
1212
};
1313
use criterion::{criterion_group, criterion_main, Criterion};
1414
use ldk_node::{Event, Node};
@@ -119,7 +119,7 @@ async fn send_payments(node_a: Arc<Node>, node_b: Arc<Node>) -> std::time::Durat
119119
fn payment_benchmark(c: &mut Criterion) {
120120
// Set up two nodes. Because this is slow, we reuse the same nodes for each sample.
121121
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
122-
let chain_source = TestChainSource::Esplora(&electrsd);
122+
let chain_source = random_chain_source(&bitcoind, &electrsd);
123123

124124
let (node_a, node_b) = setup_two_nodes_with_store(
125125
&chain_source,

tests/common/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,31 @@ pub(crate) fn setup_bitcoind_and_electrsd() -> (BitcoinD, ElectrsD) {
206206
(bitcoind, electrsd)
207207
}
208208

209+
pub(crate) fn random_chain_source<'a>(
210+
bitcoind: &'a BitcoinD, electrsd: &'a ElectrsD,
211+
) -> TestChainSource<'a> {
212+
let r = rand::random_range(0..3);
213+
match r {
214+
0 => {
215+
println!("Randomly setting up Esplora chain syncing...");
216+
TestChainSource::Esplora(electrsd)
217+
},
218+
1 => {
219+
println!("Randomly setting up Electrum chain syncing...");
220+
TestChainSource::Electrum(electrsd)
221+
},
222+
2 => {
223+
println!("Randomly setting up Bitcoind RPC chain syncing...");
224+
TestChainSource::BitcoindRpcSync(bitcoind)
225+
},
226+
3 => {
227+
println!("Randomly setting up Bitcoind REST chain syncing...");
228+
TestChainSource::BitcoindRestSync(bitcoind)
229+
},
230+
_ => unreachable!(),
231+
}
232+
}
233+
209234
pub(crate) fn random_storage_path() -> PathBuf {
210235
let mut temp_path = std::env::temp_dir();
211236
let mut rng = rng();

tests/integration_tests_rust.rs

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use common::{
2121
expect_channel_pending_event, expect_channel_ready_event, expect_event,
2222
expect_payment_claimable_event, expect_payment_received_event, expect_payment_successful_event,
2323
expect_splice_pending_event, generate_blocks_and_wait, open_channel, open_channel_push_amt,
24-
premine_and_distribute_funds, premine_blocks, prepare_rbf, random_config,
24+
premine_and_distribute_funds, premine_blocks, prepare_rbf, random_chain_source, random_config,
2525
random_listening_addresses, setup_bitcoind_and_electrsd, setup_builder, setup_node,
2626
setup_two_nodes, wait_for_tx, TestChainSource, TestStoreType, TestSyncStore,
2727
};
@@ -43,34 +43,7 @@ use log::LevelFilter;
4343
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
4444
async fn channel_full_cycle() {
4545
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
46-
let chain_source = TestChainSource::Esplora(&electrsd);
47-
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
48-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false)
49-
.await;
50-
}
51-
52-
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
53-
async fn channel_full_cycle_electrum() {
54-
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
55-
let chain_source = TestChainSource::Electrum(&electrsd);
56-
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
57-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false)
58-
.await;
59-
}
60-
61-
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
62-
async fn channel_full_cycle_bitcoind_rpc_sync() {
63-
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
64-
let chain_source = TestChainSource::BitcoindRpcSync(&bitcoind);
65-
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
66-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false)
67-
.await;
68-
}
69-
70-
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
71-
async fn channel_full_cycle_bitcoind_rest_sync() {
72-
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
73-
let chain_source = TestChainSource::BitcoindRestSync(&bitcoind);
46+
let chain_source = random_chain_source(&bitcoind, &electrsd);
7447
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
7548
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false)
7649
.await;
@@ -79,7 +52,7 @@ async fn channel_full_cycle_bitcoind_rest_sync() {
7952
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
8053
async fn channel_full_cycle_force_close() {
8154
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
82-
let chain_source = TestChainSource::Esplora(&electrsd);
55+
let chain_source = random_chain_source(&bitcoind, &electrsd);
8356
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
8457
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true)
8558
.await;
@@ -88,7 +61,7 @@ async fn channel_full_cycle_force_close() {
8861
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
8962
async fn channel_full_cycle_force_close_trusted_no_reserve() {
9063
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
91-
let chain_source = TestChainSource::Esplora(&electrsd);
64+
let chain_source = random_chain_source(&bitcoind, &electrsd);
9265
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, true);
9366
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true)
9467
.await;
@@ -97,7 +70,7 @@ async fn channel_full_cycle_force_close_trusted_no_reserve() {
9770
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
9871
async fn channel_full_cycle_0conf() {
9972
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
100-
let chain_source = TestChainSource::Esplora(&electrsd);
73+
let chain_source = random_chain_source(&bitcoind, &electrsd);
10174
let (node_a, node_b) = setup_two_nodes(&chain_source, true, true, false);
10275
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true, false)
10376
.await;
@@ -106,7 +79,7 @@ async fn channel_full_cycle_0conf() {
10679
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
10780
async fn channel_full_cycle_legacy_staticremotekey() {
10881
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
109-
let chain_source = TestChainSource::Esplora(&electrsd);
82+
let chain_source = random_chain_source(&bitcoind, &electrsd);
11083
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);
11184
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false, false)
11285
.await;
@@ -115,7 +88,7 @@ async fn channel_full_cycle_legacy_staticremotekey() {
11588
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
11689
async fn channel_open_fails_when_funds_insufficient() {
11790
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
118-
let chain_source = TestChainSource::Esplora(&electrsd);
91+
let chain_source = random_chain_source(&bitcoind, &electrsd);
11992
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
12093

12194
let addr_a = node_a.onchain_payment().new_address().unwrap();
@@ -322,7 +295,7 @@ async fn start_stop_reinit() {
322295
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
323296
async fn onchain_send_receive() {
324297
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
325-
let chain_source = TestChainSource::Esplora(&electrsd);
298+
let chain_source = random_chain_source(&bitcoind, &electrsd);
326299
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
327300

328301
let addr_a = node_a.onchain_payment().new_address().unwrap();
@@ -523,7 +496,7 @@ async fn onchain_send_receive() {
523496
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
524497
async fn onchain_send_all_retains_reserve() {
525498
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
526-
let chain_source = TestChainSource::Esplora(&electrsd);
499+
let chain_source = random_chain_source(&bitcoind, &electrsd);
527500
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
528501

529502
// Setup nodes
@@ -608,7 +581,7 @@ async fn onchain_send_all_retains_reserve() {
608581
async fn onchain_wallet_recovery() {
609582
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
610583

611-
let chain_source = TestChainSource::Esplora(&electrsd);
584+
let chain_source = random_chain_source(&bitcoind, &electrsd);
612585

613586
let original_config = random_config(true);
614587
let original_node_entropy = original_config.node_entropy;
@@ -823,9 +796,9 @@ async fn run_rbf_test(is_insert_block: bool) {
823796

824797
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
825798
async fn sign_verify_msg() {
826-
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
799+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
827800
let config = random_config(true);
828-
let chain_source = TestChainSource::Esplora(&electrsd);
801+
let chain_source = random_chain_source(&bitcoind, &electrsd);
829802
let node = setup_node(&chain_source, config);
830803

831804
// Tests arbitrary message signing and later verification
@@ -837,8 +810,8 @@ async fn sign_verify_msg() {
837810

838811
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
839812
async fn connection_multi_listen() {
840-
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
841-
let chain_source = TestChainSource::Esplora(&electrsd);
813+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
814+
let chain_source = random_chain_source(&bitcoind, &electrsd);
842815
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);
843816

844817
let node_id_b = node_b.node_id();
@@ -857,8 +830,8 @@ async fn connection_restart_behavior() {
857830
}
858831

859832
async fn do_connection_restart_behavior(persist: bool) {
860-
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
861-
let chain_source = TestChainSource::Esplora(&electrsd);
833+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
834+
let chain_source = random_chain_source(&bitcoind, &electrsd);
862835
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);
863836

864837
let node_id_a = node_a.node_id();
@@ -904,8 +877,8 @@ async fn do_connection_restart_behavior(persist: bool) {
904877

905878
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
906879
async fn concurrent_connections_succeed() {
907-
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
908-
let chain_source = TestChainSource::Esplora(&electrsd);
880+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
881+
let chain_source = random_chain_source(&bitcoind, &electrsd);
909882
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
910883

911884
let node_a = Arc::new(node_a);
@@ -929,13 +902,11 @@ async fn concurrent_connections_succeed() {
929902
}
930903
}
931904

932-
async fn run_splice_channel_test(bitcoind_chain_source: bool) {
905+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
906+
async fn splice_channel() {
933907
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
934-
let chain_source = if bitcoind_chain_source {
935-
TestChainSource::BitcoindRpcSync(&bitcoind)
936-
} else {
937-
TestChainSource::Esplora(&electrsd)
938-
};
908+
let chain_source = random_chain_source(&bitcoind, &electrsd);
909+
939910
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
940911

941912
let address_a = node_a.onchain_payment().new_address().unwrap();
@@ -1071,16 +1042,10 @@ async fn run_splice_channel_test(bitcoind_chain_source: bool) {
10711042
);
10721043
}
10731044

1074-
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
1075-
async fn splice_channel() {
1076-
run_splice_channel_test(false).await;
1077-
run_splice_channel_test(true).await;
1078-
}
1079-
10801045
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
10811046
async fn simple_bolt12_send_receive() {
10821047
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1083-
let chain_source = TestChainSource::Esplora(&electrsd);
1048+
let chain_source = random_chain_source(&bitcoind, &electrsd);
10841049
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
10851050

10861051
let address_a = node_a.onchain_payment().new_address().unwrap();
@@ -1309,7 +1274,7 @@ async fn simple_bolt12_send_receive() {
13091274
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
13101275
async fn async_payment() {
13111276
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1312-
let chain_source = TestChainSource::Esplora(&electrsd);
1277+
let chain_source = random_chain_source(&bitcoind, &electrsd);
13131278

13141279
let mut config_sender = random_config(true);
13151280
config_sender.node_config.listening_addresses = None;
@@ -1435,7 +1400,7 @@ async fn async_payment() {
14351400
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
14361401
async fn test_node_announcement_propagation() {
14371402
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1438-
let chain_source = TestChainSource::Esplora(&electrsd);
1403+
let chain_source = random_chain_source(&bitcoind, &electrsd);
14391404

14401405
// Node A will use both listening and announcement addresses
14411406
let mut config_a = random_config(true);
@@ -1527,7 +1492,7 @@ async fn test_node_announcement_propagation() {
15271492
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
15281493
async fn generate_bip21_uri() {
15291494
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1530-
let chain_source = TestChainSource::Esplora(&electrsd);
1495+
let chain_source = random_chain_source(&bitcoind, &electrsd);
15311496

15321497
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
15331498

@@ -1582,7 +1547,7 @@ async fn generate_bip21_uri() {
15821547
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
15831548
async fn unified_send_receive_bip21_uri() {
15841549
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1585-
let chain_source = TestChainSource::Esplora(&electrsd);
1550+
let chain_source = random_chain_source(&bitcoind, &electrsd);
15861551

15871552
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
15881553

@@ -1921,8 +1886,8 @@ async fn do_lsps2_client_service_integration(client_trusts_lsp: bool) {
19211886

19221887
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
19231888
async fn facade_logging() {
1924-
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1925-
let chain_source = TestChainSource::Esplora(&electrsd);
1889+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1890+
let chain_source = random_chain_source(&bitcoind, &electrsd);
19261891

19271892
let logger = init_log_logger(LevelFilter::Trace);
19281893
let mut config = random_config(false);
@@ -1940,7 +1905,7 @@ async fn facade_logging() {
19401905
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
19411906
async fn spontaneous_send_with_custom_preimage() {
19421907
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1943-
let chain_source = TestChainSource::Esplora(&electrsd);
1908+
let chain_source = random_chain_source(&bitcoind, &electrsd);
19441909
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
19451910

19461911
let address_a = node_a.onchain_payment().new_address().unwrap();
@@ -2006,8 +1971,8 @@ async fn spontaneous_send_with_custom_preimage() {
20061971

20071972
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
20081973
async fn drop_in_async_context() {
2009-
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
2010-
let chain_source = TestChainSource::Esplora(&electrsd);
1974+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1975+
let chain_source = random_chain_source(&bitcoind, &electrsd);
20111976
let config = random_config(true);
20121977
let node = setup_node(&chain_source, config);
20131978
node.stop().unwrap();
@@ -2318,7 +2283,7 @@ async fn lsps2_lsp_trusts_client_but_client_does_not_claim() {
23182283
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
23192284
async fn payment_persistence_after_restart() {
23202285
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
2321-
let chain_source = TestChainSource::Esplora(&electrsd);
2286+
let chain_source = random_chain_source(&bitcoind, &electrsd);
23222287

23232288
// Setup nodes manually so we can restart node_a with the same config
23242289
println!("== Node A ==");

tests/reorg_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use proptest::proptest;
99

1010
use crate::common::{
1111
expect_event, generate_blocks_and_wait, invalidate_blocks, open_channel,
12-
premine_and_distribute_funds, random_config, setup_bitcoind_and_electrsd, setup_node,
13-
wait_for_outpoint_spend, TestChainSource,
12+
premine_and_distribute_funds, random_chain_source, random_config, setup_bitcoind_and_electrsd,
13+
setup_node, wait_for_outpoint_spend,
1414
};
1515

1616
proptest! {
@@ -24,9 +24,9 @@ proptest! {
2424
rt.block_on(async {
2525
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
2626

27-
let chain_source_bitcoind = TestChainSource::BitcoindRpcSync(&bitcoind);
28-
let chain_source_electrsd = TestChainSource::Electrum(&electrsd);
29-
let chain_source_esplora = TestChainSource::Esplora(&electrsd);
27+
let chain_source_a = random_chain_source(&bitcoind, &electrsd);
28+
let chain_source_b = random_chain_source(&bitcoind, &electrsd);
29+
let chain_source_c = random_chain_source(&bitcoind, &electrsd);
3030

3131
macro_rules! config_node {
3232
($chain_source: expr, $anchor_channels: expr) => {{
@@ -37,9 +37,9 @@ proptest! {
3737
}
3838
let anchor_channels = true;
3939
let nodes = vec![
40-
config_node!(chain_source_electrsd, anchor_channels),
41-
config_node!(chain_source_bitcoind, anchor_channels),
42-
config_node!(chain_source_esplora, anchor_channels),
40+
config_node!(chain_source_a, anchor_channels),
41+
config_node!(chain_source_b, anchor_channels),
42+
config_node!(chain_source_c, anchor_channels),
4343
];
4444

4545
let (bitcoind, electrs) = (&bitcoind.client, &electrsd.client);

0 commit comments

Comments
 (0)