33
44use ldk_node:: io:: sqlite_store:: SqliteStore ;
55use 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
810use lightning:: ln:: msgs:: SocketAddress ;
911use 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
362366pub ( 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 ! ( "\n B close_channel" ) ;
586- node_b. close_channel ( & user_channel_id, node_a. node_id ( ) , false ) . unwrap ( ) ;
589+ println ! ( "\n B 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 ) ;
0 commit comments