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,14 @@ 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
+ std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
574
+ node_b. close_channel ( & user_channel_id, node_a. node_id ( ) , true ) . unwrap ( ) ;
575
+ } else {
576
+ node_b. close_channel ( & user_channel_id, node_a. node_id ( ) , false ) . unwrap ( ) ;
577
+ }
578
+
569
579
expect_event ! ( node_a, ChannelClosed ) ;
570
580
expect_event ! ( node_b, ChannelClosed ) ;
571
581
@@ -575,6 +585,87 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
575
585
node_a. sync_wallets ( ) . unwrap ( ) ;
576
586
node_b. sync_wallets ( ) . unwrap ( ) ;
577
587
588
+ if force_close {
589
+ // Check node_a properly sees all balances and sweeps them.
590
+ assert_eq ! ( node_a. list_balances( ) . lightning_balances. len( ) , 1 ) ;
591
+ match node_a. list_balances ( ) . lightning_balances [ 0 ] {
592
+ LightningBalance :: ClaimableAwaitingConfirmations {
593
+ counterparty_node_id,
594
+ confirmation_height,
595
+ ..
596
+ } => {
597
+ assert_eq ! ( counterparty_node_id, node_b. node_id( ) ) ;
598
+ let cur_height = node_a. status ( ) . current_best_block . height ;
599
+ let blocks_to_go = confirmation_height - cur_height;
600
+ generate_blocks_and_wait ( & bitcoind, electrsd, blocks_to_go as usize ) ;
601
+ node_a. sync_wallets ( ) . unwrap ( ) ;
602
+ node_b. sync_wallets ( ) . unwrap ( ) ;
603
+ } ,
604
+ _ => panic ! ( "Unexpected balance state!" ) ,
605
+ }
606
+
607
+ assert ! ( node_a. list_balances( ) . lightning_balances. is_empty( ) ) ;
608
+ assert_eq ! ( node_a. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
609
+ match node_a. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
610
+ PendingSweepBalance :: BroadcastAwaitingConfirmation { .. } => { } ,
611
+ _ => panic ! ( "Unexpected balance state!" ) ,
612
+ }
613
+ generate_blocks_and_wait ( & bitcoind, electrsd, 1 ) ;
614
+ node_a. sync_wallets ( ) . unwrap ( ) ;
615
+ node_b. sync_wallets ( ) . unwrap ( ) ;
616
+
617
+ assert ! ( node_a. list_balances( ) . lightning_balances. is_empty( ) ) ;
618
+ assert_eq ! ( node_a. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
619
+ match node_a. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
620
+ PendingSweepBalance :: AwaitingThresholdConfirmations { .. } => { } ,
621
+ _ => panic ! ( "Unexpected balance state!" ) ,
622
+ }
623
+ generate_blocks_and_wait ( & bitcoind, electrsd, 5 ) ;
624
+ node_a. sync_wallets ( ) . unwrap ( ) ;
625
+ node_b. sync_wallets ( ) . unwrap ( ) ;
626
+
627
+ assert ! ( node_a. list_balances( ) . lightning_balances. is_empty( ) ) ;
628
+ assert ! ( node_a. list_balances( ) . pending_balances_from_channel_closures. is_empty( ) ) ;
629
+
630
+ // Check node_b properly sees all balances and sweeps them.
631
+ assert_eq ! ( node_b. list_balances( ) . lightning_balances. len( ) , 1 ) ;
632
+ match node_b. list_balances ( ) . lightning_balances [ 0 ] {
633
+ LightningBalance :: ClaimableAwaitingConfirmations {
634
+ counterparty_node_id,
635
+ confirmation_height,
636
+ ..
637
+ } => {
638
+ assert_eq ! ( counterparty_node_id, node_a. node_id( ) ) ;
639
+ let cur_height = node_b. status ( ) . current_best_block . height ;
640
+ let blocks_to_go = confirmation_height - cur_height;
641
+ generate_blocks_and_wait ( & bitcoind, electrsd, blocks_to_go as usize ) ;
642
+ node_b. sync_wallets ( ) . unwrap ( ) ;
643
+ node_a. sync_wallets ( ) . unwrap ( ) ;
644
+ } ,
645
+ _ => panic ! ( "Unexpected balance state!" ) ,
646
+ }
647
+
648
+ assert ! ( node_b. list_balances( ) . lightning_balances. is_empty( ) ) ;
649
+ assert_eq ! ( node_b. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
650
+ match node_b. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
651
+ PendingSweepBalance :: BroadcastAwaitingConfirmation { .. } => { } ,
652
+ _ => panic ! ( "Unexpected balance state!" ) ,
653
+ }
654
+ generate_blocks_and_wait ( & bitcoind, electrsd, 1 ) ;
655
+ node_b. sync_wallets ( ) . unwrap ( ) ;
656
+ node_a. sync_wallets ( ) . unwrap ( ) ;
657
+
658
+ assert ! ( node_b. list_balances( ) . lightning_balances. is_empty( ) ) ;
659
+ assert_eq ! ( node_b. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
660
+ match node_b. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
661
+ PendingSweepBalance :: AwaitingThresholdConfirmations { .. } => { } ,
662
+ _ => panic ! ( "Unexpected balance state!" ) ,
663
+ }
664
+ generate_blocks_and_wait ( & bitcoind, electrsd, 5 ) ;
665
+ node_b. sync_wallets ( ) . unwrap ( ) ;
666
+ node_a. sync_wallets ( ) . unwrap ( ) ;
667
+ }
668
+
578
669
let sum_of_all_payments_sat = ( push_msat
579
670
+ invoice_amount_1_msat
580
671
+ overpaid_amount_msat
@@ -586,11 +677,11 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
586
677
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
587
678
assert ! ( node_a. list_balances( ) . spendable_onchain_balance_sats > node_a_lower_bound_sat) ;
588
679
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
- ) ;
680
+
681
+ let node_b_upper_bound_sat = premine_amount_sat + sum_of_all_payments_sat ;
682
+ let node_b_lower_bound_sat = node_b_upper_bound_sat - onchain_fee_buffer_sat ;
683
+ assert ! ( node_b . list_balances ( ) . spendable_onchain_balance_sats > node_b_lower_bound_sat ) ;
684
+ assert ! ( node_b . list_balances ( ) . spendable_onchain_balance_sats <= node_b_upper_bound_sat ) ;
594
685
595
686
// Check we handled all events
596
687
assert_eq ! ( node_a. next_event( ) , None ) ;
0 commit comments