Skip to content

Commit e72d4a3

Browse files
committed
Test force-closure 'happy' case
.. i.e., without bumping.
1 parent c871f57 commit e72d4a3

3 files changed

Lines changed: 119 additions & 13 deletions

File tree

tests/common/mod.rs

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
use ldk_node::io::sqlite_store::SqliteStore;
55
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
6-
use ldk_node::{Builder, Config, Event, LogLevel, Node, NodeError};
6+
use ldk_node::{
7+
Builder, Config, Event, LightningBalance, LogLevel, Node, NodeError, PendingSweepBalance,
8+
};
79

810
use lightning::ln::msgs::SocketAddress;
911
use lightning::util::persist::KVStore;
@@ -171,6 +173,8 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
171173
}
172174

173175
config.network = Network::Regtest;
176+
config.onchain_wallet_sync_interval_secs = 100000;
177+
config.wallet_sync_interval_secs = 100000;
174178
println!("Setting network: {}", config.network);
175179

176180
let rand_dir = random_storage_path();
@@ -361,7 +365,7 @@ pub fn open_channel(
361365

362366
pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
363367
node_a: TestNode, node_b: TestNode, bitcoind: &BitcoindClient, electrsd: &E, allow_0conf: bool,
364-
expect_anchor_channel: bool,
368+
expect_anchor_channel: bool, force_close: bool,
365369
) {
366370
let addr_a = node_a.onchain_payment().new_address().unwrap();
367371
let addr_b = node_b.onchain_payment().new_address().unwrap();
@@ -582,8 +586,14 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
582586
assert_eq!(node_a.list_payments().len(), 4);
583587
assert_eq!(node_b.list_payments().len(), 5);
584588

585-
println!("\nB close_channel");
586-
node_b.close_channel(&user_channel_id, node_a.node_id(), false).unwrap();
589+
println!("\nB close_channel (force: {})", force_close);
590+
if force_close {
591+
std::thread::sleep(Duration::from_secs(1));
592+
node_b.close_channel(&user_channel_id, node_a.node_id(), true).unwrap();
593+
} else {
594+
node_b.close_channel(&user_channel_id, node_a.node_id(), false).unwrap();
595+
}
596+
587597
expect_event!(node_a, ChannelClosed);
588598
expect_event!(node_b, ChannelClosed);
589599

@@ -593,6 +603,87 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
593603
node_a.sync_wallets().unwrap();
594604
node_b.sync_wallets().unwrap();
595605

606+
if force_close {
607+
// Check node_a properly sees all balances and sweeps them.
608+
assert_eq!(node_a.list_balances().lightning_balances.len(), 1);
609+
match node_a.list_balances().lightning_balances[0] {
610+
LightningBalance::ClaimableAwaitingConfirmations {
611+
counterparty_node_id,
612+
confirmation_height,
613+
..
614+
} => {
615+
assert_eq!(counterparty_node_id, node_b.node_id());
616+
let cur_height = node_a.status().current_best_block.height;
617+
let blocks_to_go = confirmation_height - cur_height;
618+
generate_blocks_and_wait(&bitcoind, electrsd, blocks_to_go as usize);
619+
node_a.sync_wallets().unwrap();
620+
node_b.sync_wallets().unwrap();
621+
},
622+
_ => panic!("Unexpected balance state!"),
623+
}
624+
625+
assert!(node_a.list_balances().lightning_balances.is_empty());
626+
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
627+
match node_a.list_balances().pending_balances_from_channel_closures[0] {
628+
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
629+
_ => panic!("Unexpected balance state!"),
630+
}
631+
generate_blocks_and_wait(&bitcoind, electrsd, 1);
632+
node_a.sync_wallets().unwrap();
633+
node_b.sync_wallets().unwrap();
634+
635+
assert!(node_a.list_balances().lightning_balances.is_empty());
636+
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
637+
match node_a.list_balances().pending_balances_from_channel_closures[0] {
638+
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
639+
_ => panic!("Unexpected balance state!"),
640+
}
641+
generate_blocks_and_wait(&bitcoind, electrsd, 5);
642+
node_a.sync_wallets().unwrap();
643+
node_b.sync_wallets().unwrap();
644+
645+
assert!(node_a.list_balances().lightning_balances.is_empty());
646+
assert!(node_a.list_balances().pending_balances_from_channel_closures.is_empty());
647+
648+
// Check node_b properly sees all balances and sweeps them.
649+
assert_eq!(node_b.list_balances().lightning_balances.len(), 1);
650+
match node_b.list_balances().lightning_balances[0] {
651+
LightningBalance::ClaimableAwaitingConfirmations {
652+
counterparty_node_id,
653+
confirmation_height,
654+
..
655+
} => {
656+
assert_eq!(counterparty_node_id, node_a.node_id());
657+
let cur_height = node_b.status().current_best_block.height;
658+
let blocks_to_go = confirmation_height - cur_height;
659+
generate_blocks_and_wait(&bitcoind, electrsd, blocks_to_go as usize);
660+
node_b.sync_wallets().unwrap();
661+
node_a.sync_wallets().unwrap();
662+
},
663+
_ => panic!("Unexpected balance state!"),
664+
}
665+
666+
assert!(node_b.list_balances().lightning_balances.is_empty());
667+
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
668+
match node_b.list_balances().pending_balances_from_channel_closures[0] {
669+
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
670+
_ => panic!("Unexpected balance state!"),
671+
}
672+
generate_blocks_and_wait(&bitcoind, electrsd, 1);
673+
node_b.sync_wallets().unwrap();
674+
node_a.sync_wallets().unwrap();
675+
676+
assert!(node_b.list_balances().lightning_balances.is_empty());
677+
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
678+
match node_b.list_balances().pending_balances_from_channel_closures[0] {
679+
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
680+
_ => panic!("Unexpected balance state!"),
681+
}
682+
generate_blocks_and_wait(&bitcoind, electrsd, 5);
683+
node_b.sync_wallets().unwrap();
684+
node_a.sync_wallets().unwrap();
685+
}
686+
596687
let sum_of_all_payments_sat = (push_msat
597688
+ invoice_amount_1_msat
598689
+ overpaid_amount_msat
@@ -604,11 +695,11 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
604695
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
605696
assert!(node_a.list_balances().spendable_onchain_balance_sats > node_a_lower_bound_sat);
606697
assert!(node_a.list_balances().spendable_onchain_balance_sats < node_a_upper_bound_sat);
607-
let expected_final_amount_node_b_sat = premine_amount_sat + sum_of_all_payments_sat;
608-
assert_eq!(
609-
node_b.list_balances().spendable_onchain_balance_sats,
610-
expected_final_amount_node_b_sat
611-
);
698+
699+
let node_b_upper_bound_sat = premine_amount_sat + sum_of_all_payments_sat;
700+
let node_b_lower_bound_sat = node_b_upper_bound_sat - onchain_fee_buffer_sat;
701+
assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_lower_bound_sat);
702+
assert!(node_b.list_balances().spendable_onchain_balance_sats <= node_b_upper_bound_sat);
612703

613704
// Check we handled all events
614705
assert_eq!(node_a.next_event(), None);

tests/integration_tests_rust.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,28 @@ use crate::common::expect_channel_ready_event;
2323
fn channel_full_cycle() {
2424
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
2525
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
26-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true);
26+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
27+
}
28+
29+
#[test]
30+
fn channel_full_cycle_force_close() {
31+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
32+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
33+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
2734
}
2835

2936
#[test]
3037
fn channel_full_cycle_0conf() {
3138
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
3239
let (node_a, node_b) = setup_two_nodes(&electrsd, true, true);
33-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true)
40+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true, false)
3441
}
3542

3643
#[test]
3744
fn channel_full_cycle_legacy_staticremotekey() {
3845
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
3946
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false);
40-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false);
47+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false, false);
4148
}
4249

4350
#[test]

tests/integration_tests_vss.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,13 @@ fn channel_full_cycle_with_vss_store() {
2424
let node_b = builder_b.build_with_vss_store(vss_base_url, "node_2_store".to_string()).unwrap();
2525
node_b.start().unwrap();
2626

27-
common::do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true);
27+
common::do_channel_full_cycle(
28+
node_a,
29+
node_b,
30+
&bitcoind.client,
31+
&electrsd.client,
32+
false,
33+
true,
34+
false,
35+
);
2836
}

0 commit comments

Comments
 (0)