Skip to content

Commit f35923b

Browse files
committed
Test force-closure 'happy' case
.. i.e., without bumping.
1 parent 8610e67 commit f35923b

File tree

3 files changed

+118
-13
lines changed

3 files changed

+118
-13
lines changed

tests/common/mod.rs

Lines changed: 99 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, 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;
@@ -169,6 +171,8 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
169171
}
170172

171173
config.network = Network::Regtest;
174+
config.onchain_wallet_sync_interval_secs = 100000;
175+
config.wallet_sync_interval_secs = 100000;
172176
println!("Setting network: {}", config.network);
173177

174178
let rand_dir = random_storage_path();
@@ -359,7 +363,7 @@ pub fn open_channel(
359363

360364
pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
361365
node_a: TestNode, node_b: TestNode, bitcoind: &BitcoindClient, electrsd: &E, allow_0conf: bool,
362-
expect_anchor_channel: bool,
366+
expect_anchor_channel: bool, force_close: bool,
363367
) {
364368
let addr_a = node_a.onchain_payment().new_address().unwrap();
365369
let addr_b = node_b.onchain_payment().new_address().unwrap();
@@ -564,8 +568,13 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
564568
assert_eq!(node_b.payment(&keysend_payment_id).unwrap().direction, PaymentDirection::Inbound);
565569
assert_eq!(node_b.payment(&keysend_payment_id).unwrap().amount_msat, Some(keysend_amount_msat));
566570

567-
println!("\nB close_channel");
568-
node_b.close_channel(&user_channel_id, node_a.node_id(), false).unwrap();
571+
println!("\nB close_channel (force: {})", force_close);
572+
if force_close {
573+
node_b.close_channel(&user_channel_id, node_a.node_id(), true).unwrap();
574+
} else {
575+
node_b.close_channel(&user_channel_id, node_a.node_id(), false).unwrap();
576+
}
577+
569578
expect_event!(node_a, ChannelClosed);
570579
expect_event!(node_b, ChannelClosed);
571580

@@ -575,6 +584,87 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
575584
node_a.sync_wallets().unwrap();
576585
node_b.sync_wallets().unwrap();
577586

587+
if force_close {
588+
// Check node_a properly sees all balances and sweeps them.
589+
assert_eq!(node_a.list_balances().lightning_balances.len(), 1);
590+
match node_a.list_balances().lightning_balances[0] {
591+
LightningBalance::ClaimableAwaitingConfirmations {
592+
counterparty_node_id,
593+
confirmation_height,
594+
..
595+
} => {
596+
assert_eq!(counterparty_node_id, node_b.node_id());
597+
let cur_height = node_a.status().current_best_block.height;
598+
let blocks_to_go = confirmation_height - cur_height;
599+
generate_blocks_and_wait(&bitcoind, electrsd, blocks_to_go as usize);
600+
node_a.sync_wallets().unwrap();
601+
node_b.sync_wallets().unwrap();
602+
},
603+
_ => panic!("Unexpected balance state!"),
604+
}
605+
606+
assert!(node_a.list_balances().lightning_balances.is_empty());
607+
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
608+
match node_a.list_balances().pending_balances_from_channel_closures[0] {
609+
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
610+
_ => panic!("Unexpected balance state!"),
611+
}
612+
generate_blocks_and_wait(&bitcoind, electrsd, 1);
613+
node_a.sync_wallets().unwrap();
614+
node_b.sync_wallets().unwrap();
615+
616+
assert!(node_a.list_balances().lightning_balances.is_empty());
617+
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
618+
match node_a.list_balances().pending_balances_from_channel_closures[0] {
619+
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
620+
_ => panic!("Unexpected balance state!"),
621+
}
622+
generate_blocks_and_wait(&bitcoind, electrsd, 5);
623+
node_a.sync_wallets().unwrap();
624+
node_b.sync_wallets().unwrap();
625+
626+
assert!(node_a.list_balances().lightning_balances.is_empty());
627+
assert!(node_a.list_balances().pending_balances_from_channel_closures.is_empty());
628+
629+
// Check node_b properly sees all balances and sweeps them.
630+
assert_eq!(node_b.list_balances().lightning_balances.len(), 1);
631+
match node_b.list_balances().lightning_balances[0] {
632+
LightningBalance::ClaimableAwaitingConfirmations {
633+
counterparty_node_id,
634+
confirmation_height,
635+
..
636+
} => {
637+
assert_eq!(counterparty_node_id, node_a.node_id());
638+
let cur_height = node_b.status().current_best_block.height;
639+
let blocks_to_go = confirmation_height - cur_height;
640+
generate_blocks_and_wait(&bitcoind, electrsd, blocks_to_go as usize);
641+
node_b.sync_wallets().unwrap();
642+
node_a.sync_wallets().unwrap();
643+
},
644+
_ => panic!("Unexpected balance state!"),
645+
}
646+
647+
assert!(node_b.list_balances().lightning_balances.is_empty());
648+
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
649+
match node_b.list_balances().pending_balances_from_channel_closures[0] {
650+
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
651+
_ => panic!("Unexpected balance state!"),
652+
}
653+
generate_blocks_and_wait(&bitcoind, electrsd, 1);
654+
node_b.sync_wallets().unwrap();
655+
node_a.sync_wallets().unwrap();
656+
657+
assert!(node_b.list_balances().lightning_balances.is_empty());
658+
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
659+
match node_b.list_balances().pending_balances_from_channel_closures[0] {
660+
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
661+
_ => panic!("Unexpected balance state!"),
662+
}
663+
generate_blocks_and_wait(&bitcoind, electrsd, 5);
664+
node_b.sync_wallets().unwrap();
665+
node_a.sync_wallets().unwrap();
666+
}
667+
578668
let sum_of_all_payments_sat = (push_msat
579669
+ invoice_amount_1_msat
580670
+ overpaid_amount_msat
@@ -586,11 +676,11 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
586676
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
587677
assert!(node_a.list_balances().spendable_onchain_balance_sats > node_a_lower_bound_sat);
588678
assert!(node_a.list_balances().spendable_onchain_balance_sats < node_a_upper_bound_sat);
589-
let expected_final_amount_node_b_sat = premine_amount_sat + sum_of_all_payments_sat;
590-
assert_eq!(
591-
node_b.list_balances().spendable_onchain_balance_sats,
592-
expected_final_amount_node_b_sat
593-
);
679+
680+
let node_b_upper_bound_sat = premine_amount_sat + sum_of_all_payments_sat;
681+
let node_b_lower_bound_sat = node_b_upper_bound_sat - onchain_fee_buffer_sat;
682+
assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_lower_bound_sat);
683+
assert!(node_b.list_balances().spendable_onchain_balance_sats <= node_b_upper_bound_sat);
594684

595685
// Check we handled all events
596686
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
@@ -19,21 +19,28 @@ use std::sync::Arc;
1919
fn channel_full_cycle() {
2020
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
2121
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
22-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true);
22+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
23+
}
24+
25+
#[test]
26+
fn channel_full_cycle_force_close() {
27+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
28+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
29+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
2330
}
2431

2532
#[test]
2633
fn channel_full_cycle_0conf() {
2734
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
2835
let (node_a, node_b) = setup_two_nodes(&electrsd, true, true);
29-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true)
36+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true, false)
3037
}
3138

3239
#[test]
3340
fn channel_full_cycle_legacy_staticremotekey() {
3441
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
3542
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false);
36-
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false);
43+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false, false);
3744
}
3845

3946
#[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)