3
3
4
4
use ldk_node:: io:: sqlite_store:: SqliteStore ;
5
5
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
+ } ;
7
9
8
10
use lightning:: ln:: msgs:: SocketAddress ;
9
11
use lightning:: util:: persist:: KVStore ;
@@ -169,6 +171,8 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
169
171
}
170
172
171
173
config. network = Network :: Regtest ;
174
+ config. onchain_wallet_sync_interval_secs = 100000 ;
175
+ config. wallet_sync_interval_secs = 100000 ;
172
176
println ! ( "Setting network: {}" , config. network) ;
173
177
174
178
let rand_dir = random_storage_path ( ) ;
@@ -359,7 +363,7 @@ pub fn open_channel(
359
363
360
364
pub ( crate ) fn do_channel_full_cycle < E : ElectrumApi > (
361
365
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 ,
363
367
) {
364
368
let addr_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
365
369
let addr_b = node_b. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
@@ -564,8 +568,13 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
564
568
assert_eq ! ( node_b. payment( & keysend_payment_id) . unwrap( ) . direction, PaymentDirection :: Inbound ) ;
565
569
assert_eq ! ( node_b. payment( & keysend_payment_id) . unwrap( ) . amount_msat, Some ( keysend_amount_msat) ) ;
566
570
567
- println ! ( "\n B close_channel" ) ;
568
- node_b. close_channel ( & user_channel_id, node_a. node_id ( ) , false ) . unwrap ( ) ;
571
+ println ! ( "\n B 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
+
569
578
expect_event ! ( node_a, ChannelClosed ) ;
570
579
expect_event ! ( node_b, ChannelClosed ) ;
571
580
@@ -575,6 +584,87 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
575
584
node_a. sync_wallets ( ) . unwrap ( ) ;
576
585
node_b. sync_wallets ( ) . unwrap ( ) ;
577
586
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
+
578
668
let sum_of_all_payments_sat = ( push_msat
579
669
+ invoice_amount_1_msat
580
670
+ overpaid_amount_msat
@@ -586,11 +676,11 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
586
676
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
587
677
assert ! ( node_a. list_balances( ) . spendable_onchain_balance_sats > node_a_lower_bound_sat) ;
588
678
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 ) ;
594
684
595
685
// Check we handled all events
596
686
assert_eq ! ( node_a. next_event( ) , None ) ;
0 commit comments