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 ( ) ;
@@ -203,7 +207,7 @@ macro_rules! setup_builder {
203207pub ( crate ) use setup_builder;
204208
205209pub ( crate ) fn setup_two_nodes (
206- electrsd : & ElectrsD , allow_0conf : bool , anchor_channels : bool ,
210+ electrsd : & ElectrsD , allow_0conf : bool , anchor_channels : bool , anchors_trusted_no_reserve : bool ,
207211) -> ( TestNode , TestNode ) {
208212 println ! ( "== Node A ==" ) ;
209213 let config_a = random_config ( anchor_channels) ;
@@ -214,6 +218,14 @@ pub(crate) fn setup_two_nodes(
214218 if allow_0conf {
215219 config_b. trusted_peers_0conf . push ( node_a. node_id ( ) ) ;
216220 }
221+ if anchor_channels && anchors_trusted_no_reserve {
222+ config_b
223+ . anchor_channels_config
224+ . as_mut ( )
225+ . unwrap ( )
226+ . trusted_peers_no_reserve
227+ . push ( node_a. node_id ( ) ) ;
228+ }
217229 let node_b = setup_node ( electrsd, config_b) ;
218230 ( node_a, node_b)
219231}
@@ -361,7 +373,7 @@ pub fn open_channel(
361373
362374pub ( crate ) fn do_channel_full_cycle < E : ElectrumApi > (
363375 node_a : TestNode , node_b : TestNode , bitcoind : & BitcoindClient , electrsd : & E , allow_0conf : bool ,
364- expect_anchor_channel : bool ,
376+ expect_anchor_channel : bool , force_close : bool ,
365377) {
366378 let addr_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
367379 let addr_b = node_b. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
@@ -413,20 +425,40 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
413425 node_b. sync_wallets ( ) . unwrap ( ) ;
414426
415427 let onchain_fee_buffer_sat = 1500 ;
416- let anchor_reserve_sat = if expect_anchor_channel { 25_000 } else { 0 } ;
417- let node_a_upper_bound_sat = premine_amount_sat - anchor_reserve_sat - funding_amount_sat;
418- let node_a_lower_bound_sat =
419- premine_amount_sat - anchor_reserve_sat - funding_amount_sat - onchain_fee_buffer_sat;
428+ let node_a_anchor_reserve_sat = if expect_anchor_channel { 25_000 } else { 0 } ;
429+ let node_a_upper_bound_sat =
430+ premine_amount_sat - node_a_anchor_reserve_sat - funding_amount_sat;
431+ let node_a_lower_bound_sat = premine_amount_sat
432+ - node_a_anchor_reserve_sat
433+ - funding_amount_sat
434+ - onchain_fee_buffer_sat;
420435 assert ! ( node_a. list_balances( ) . spendable_onchain_balance_sats < node_a_upper_bound_sat) ;
421436 assert ! ( node_a. list_balances( ) . spendable_onchain_balance_sats > node_a_lower_bound_sat) ;
422437 assert_eq ! (
423- node_b . list_balances( ) . spendable_onchain_balance_sats ,
424- premine_amount_sat - anchor_reserve_sat
438+ node_a . list_balances( ) . total_anchor_channels_reserve_sats ,
439+ node_a_anchor_reserve_sat
425440 ) ;
426441
427- expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
442+ let node_b_anchor_reserve_sat = if node_b
443+ . config ( )
444+ . anchor_channels_config
445+ . map_or ( true , |acc| acc. trusted_peers_no_reserve . contains ( & node_a. node_id ( ) ) )
446+ {
447+ 0
448+ } else {
449+ 25_000
450+ } ;
451+ assert_eq ! (
452+ node_b. list_balances( ) . spendable_onchain_balance_sats,
453+ premine_amount_sat - node_b_anchor_reserve_sat
454+ ) ;
455+ assert_eq ! (
456+ node_b. list_balances( ) . total_anchor_channels_reserve_sats,
457+ node_b_anchor_reserve_sat
458+ ) ;
428459
429- let user_channel_id = expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
460+ let user_channel_id = expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
461+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
430462
431463 println ! ( "\n B receive" ) ;
432464 let invoice_amount_1_msat = 2500_000 ;
@@ -582,8 +614,14 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
582614 assert_eq ! ( node_a. list_payments( ) . len( ) , 4 ) ;
583615 assert_eq ! ( node_b. list_payments( ) . len( ) , 5 ) ;
584616
585- println ! ( "\n B close_channel" ) ;
586- node_b. close_channel ( & user_channel_id, node_a. node_id ( ) ) . unwrap ( ) ;
617+ println ! ( "\n B close_channel (force: {})" , force_close) ;
618+ if force_close {
619+ std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
620+ node_a. force_close_channel ( & user_channel_id, node_b. node_id ( ) ) . unwrap ( ) ;
621+ } else {
622+ node_a. close_channel ( & user_channel_id, node_b. node_id ( ) ) . unwrap ( ) ;
623+ }
624+
587625 expect_event ! ( node_a, ChannelClosed ) ;
588626 expect_event ! ( node_b, ChannelClosed ) ;
589627
@@ -593,6 +631,87 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
593631 node_a. sync_wallets ( ) . unwrap ( ) ;
594632 node_b. sync_wallets ( ) . unwrap ( ) ;
595633
634+ if force_close {
635+ // Check node_b properly sees all balances and sweeps them.
636+ assert_eq ! ( node_b. list_balances( ) . lightning_balances. len( ) , 1 ) ;
637+ match node_b. list_balances ( ) . lightning_balances [ 0 ] {
638+ LightningBalance :: ClaimableAwaitingConfirmations {
639+ counterparty_node_id,
640+ confirmation_height,
641+ ..
642+ } => {
643+ assert_eq ! ( counterparty_node_id, node_a. node_id( ) ) ;
644+ let cur_height = node_b. status ( ) . current_best_block . height ;
645+ let blocks_to_go = confirmation_height - cur_height;
646+ generate_blocks_and_wait ( & bitcoind, electrsd, blocks_to_go as usize ) ;
647+ node_b. sync_wallets ( ) . unwrap ( ) ;
648+ node_a. sync_wallets ( ) . unwrap ( ) ;
649+ } ,
650+ _ => panic ! ( "Unexpected balance state!" ) ,
651+ }
652+
653+ assert ! ( node_b. list_balances( ) . lightning_balances. is_empty( ) ) ;
654+ assert_eq ! ( node_b. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
655+ match node_b. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
656+ PendingSweepBalance :: BroadcastAwaitingConfirmation { .. } => { } ,
657+ _ => panic ! ( "Unexpected balance state!" ) ,
658+ }
659+ generate_blocks_and_wait ( & bitcoind, electrsd, 1 ) ;
660+ node_b. sync_wallets ( ) . unwrap ( ) ;
661+ node_a. sync_wallets ( ) . unwrap ( ) ;
662+
663+ assert ! ( node_b. list_balances( ) . lightning_balances. is_empty( ) ) ;
664+ assert_eq ! ( node_b. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
665+ match node_b. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
666+ PendingSweepBalance :: AwaitingThresholdConfirmations { .. } => { } ,
667+ _ => panic ! ( "Unexpected balance state!" ) ,
668+ }
669+ generate_blocks_and_wait ( & bitcoind, electrsd, 5 ) ;
670+ node_b. sync_wallets ( ) . unwrap ( ) ;
671+ node_a. sync_wallets ( ) . unwrap ( ) ;
672+
673+ assert ! ( node_b. list_balances( ) . lightning_balances. is_empty( ) ) ;
674+ assert ! ( node_b. list_balances( ) . pending_balances_from_channel_closures. is_empty( ) ) ;
675+
676+ // Check node_a properly sees all balances and sweeps them.
677+ assert_eq ! ( node_a. list_balances( ) . lightning_balances. len( ) , 1 ) ;
678+ match node_a. list_balances ( ) . lightning_balances [ 0 ] {
679+ LightningBalance :: ClaimableAwaitingConfirmations {
680+ counterparty_node_id,
681+ confirmation_height,
682+ ..
683+ } => {
684+ assert_eq ! ( counterparty_node_id, node_b. node_id( ) ) ;
685+ let cur_height = node_a. status ( ) . current_best_block . height ;
686+ let blocks_to_go = confirmation_height - cur_height;
687+ generate_blocks_and_wait ( & bitcoind, electrsd, blocks_to_go as usize ) ;
688+ node_a. sync_wallets ( ) . unwrap ( ) ;
689+ node_b. sync_wallets ( ) . unwrap ( ) ;
690+ } ,
691+ _ => panic ! ( "Unexpected balance state!" ) ,
692+ }
693+
694+ assert ! ( node_a. list_balances( ) . lightning_balances. is_empty( ) ) ;
695+ assert_eq ! ( node_a. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
696+ match node_a. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
697+ PendingSweepBalance :: BroadcastAwaitingConfirmation { .. } => { } ,
698+ _ => panic ! ( "Unexpected balance state!" ) ,
699+ }
700+ generate_blocks_and_wait ( & bitcoind, electrsd, 1 ) ;
701+ node_a. sync_wallets ( ) . unwrap ( ) ;
702+ node_b. sync_wallets ( ) . unwrap ( ) ;
703+
704+ assert ! ( node_a. list_balances( ) . lightning_balances. is_empty( ) ) ;
705+ assert_eq ! ( node_a. list_balances( ) . pending_balances_from_channel_closures. len( ) , 1 ) ;
706+ match node_a. list_balances ( ) . pending_balances_from_channel_closures [ 0 ] {
707+ PendingSweepBalance :: AwaitingThresholdConfirmations { .. } => { } ,
708+ _ => panic ! ( "Unexpected balance state!" ) ,
709+ }
710+ generate_blocks_and_wait ( & bitcoind, electrsd, 5 ) ;
711+ node_a. sync_wallets ( ) . unwrap ( ) ;
712+ node_b. sync_wallets ( ) . unwrap ( ) ;
713+ }
714+
596715 let sum_of_all_payments_sat = ( push_msat
597716 + invoice_amount_1_msat
598717 + overpaid_amount_msat
@@ -604,11 +723,14 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
604723 let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
605724 assert ! ( node_a. list_balances( ) . spendable_onchain_balance_sats > node_a_lower_bound_sat) ;
606725 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- ) ;
726+
727+ let node_b_upper_bound_sat = premine_amount_sat + sum_of_all_payments_sat;
728+ let node_b_lower_bound_sat = node_b_upper_bound_sat - onchain_fee_buffer_sat;
729+ assert ! ( node_b. list_balances( ) . spendable_onchain_balance_sats > node_b_lower_bound_sat) ;
730+ assert ! ( node_b. list_balances( ) . spendable_onchain_balance_sats <= node_b_upper_bound_sat) ;
731+
732+ assert_eq ! ( node_a. list_balances( ) . total_anchor_channels_reserve_sats, 0 ) ;
733+ assert_eq ! ( node_b. list_balances( ) . total_anchor_channels_reserve_sats, 0 ) ;
612734
613735 // Check we handled all events
614736 assert_eq ! ( node_a. next_event( ) , None ) ;
0 commit comments