@@ -3904,11 +3904,28 @@ fn do_test_durable_preimages_on_closed_channel(
39043904 }
39053905 if !close_chans_before_reload {
39063906 check_closed_broadcast ( & nodes[ 1 ] , 1 , false ) ;
3907- let reason = ClosureReason :: CommitmentTxConfirmed ;
3908- check_closed_event ( & nodes[ 1 ] , 1 , reason, & [ node_a_id] , 100000 ) ;
3907+ // When hold=false, get_and_clear_pending_events also triggers
3908+ // process_background_events (replaying the preimage and force-close updates)
3909+ // and resolves the deferred completions, firing PaymentForwarded alongside
3910+ // ChannelClosed. When hold=true, only ChannelClosed fires.
3911+ let evs = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
3912+ let expected = if hold_post_reload_mon_update { 1 } else { 2 } ;
3913+ assert_eq ! ( evs. len( ) , expected, "{:?}" , evs) ;
3914+ assert ! ( evs. iter( ) . any( |e| matches!(
3915+ e,
3916+ Event :: ChannelClosed { reason: ClosureReason :: CommitmentTxConfirmed , .. }
3917+ ) ) ) ;
3918+ if !hold_post_reload_mon_update {
3919+ assert ! ( evs. iter( ) . any( |e| matches!( e, Event :: PaymentForwarded { .. } ) ) ) ;
3920+ check_added_monitors ( & nodes[ 1 ] , mons_added) ;
3921+ }
39093922 }
39103923 nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3911- check_added_monitors ( & nodes[ 1 ] , mons_added) ;
3924+ // For !close_chans_before_reload && !hold, background events were already replayed
3925+ // during get_and_clear_pending_events above, so timer_tick adds no monitors.
3926+ let expected_mons =
3927+ if !close_chans_before_reload && !hold_post_reload_mon_update { 0 } else { mons_added } ;
3928+ check_added_monitors ( & nodes[ 1 ] , expected_mons) ;
39123929
39133930 // Finally, check that B created a payment preimage transaction and close out the payment.
39143931 let bs_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
@@ -3923,44 +3940,61 @@ fn do_test_durable_preimages_on_closed_channel(
39233940 check_closed_broadcast ( & nodes[ 0 ] , 1 , false ) ;
39243941 expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
39253942
3943+ if close_chans_before_reload && !hold_post_reload_mon_update {
3944+ // For close_chans_before_reload with hold=false, the deferred completions
3945+ // haven't been processed yet. Trigger process_pending_monitor_events now.
3946+ let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
3947+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3948+ }
3949+
39263950 if !close_chans_before_reload || close_only_a {
39273951 // Make sure the B<->C channel is still alive and well by sending a payment over it.
39283952 let mut reconnect_args = ReconnectArgs :: new ( & nodes[ 1 ] , & nodes[ 2 ] ) ;
39293953 reconnect_args. pending_responding_commitment_signed . 1 = true ;
3930- // The B<->C `ChannelMonitorUpdate` shouldn't be allowed to complete, which is the
3931- // equivalent to the responding `commitment_signed` being a duplicate for node B, thus we
3932- // need to set the `pending_responding_commitment_signed_dup` flag.
3933- reconnect_args. pending_responding_commitment_signed_dup_monitor . 1 = true ;
3954+ if hold_post_reload_mon_update {
3955+ // When the A-B update is still InProgress, B-C monitor updates are blocked,
3956+ // so the responding commitment_signed is a duplicate that generates no update.
3957+ reconnect_args. pending_responding_commitment_signed_dup_monitor . 1 = true ;
3958+ }
39343959 reconnect_args. pending_raa . 1 = true ;
39353960
39363961 reconnect_nodes ( reconnect_args) ;
39373962 }
39383963
3939- // Once the blocked `ChannelMonitorUpdate` *finally* completes, the pending
3940- // `PaymentForwarded` event will finally be released.
3941- let ( _, ab_update_id) = nodes[ 1 ] . chain_monitor . get_latest_mon_update_id ( chan_id_ab) ;
3942- nodes[ 1 ] . chain_monitor . chain_monitor . force_channel_monitor_updated ( chan_id_ab, ab_update_id) ;
3964+ if hold_post_reload_mon_update {
3965+ // When the persister returned InProgress, we need to manually complete the
3966+ // A-B monitor update to unblock the PaymentForwarded completion action.
3967+ let ( _, ab_update_id) = nodes[ 1 ] . chain_monitor . get_latest_mon_update_id ( chan_id_ab) ;
3968+ nodes[ 1 ]
3969+ . chain_monitor
3970+ . chain_monitor
3971+ . force_channel_monitor_updated ( chan_id_ab, ab_update_id) ;
3972+ }
39433973
39443974 // If the A<->B channel was closed before we reload, we'll replay the claim against it on
39453975 // reload, causing the `PaymentForwarded` event to get replayed.
39463976 let evs = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
3947- assert_eq ! ( evs. len( ) , if close_chans_before_reload { 2 } else { 1 } ) ;
3948- for ev in evs {
3949- if let Event :: PaymentForwarded { claim_from_onchain_tx, next_htlcs, .. } = ev {
3950- if !claim_from_onchain_tx {
3951- // If the outbound channel is still open, the `next_user_channel_id` should be available.
3952- // This was previously broken.
3953- assert ! ( next_htlcs[ 0 ] . user_channel_id. is_some( ) )
3977+ if !close_chans_before_reload && !hold_post_reload_mon_update {
3978+ // PaymentForwarded already fired during get_and_clear_pending_events above.
3979+ assert ! ( evs. is_empty( ) , "{:?}" , evs) ;
3980+ } else {
3981+ assert_eq ! ( evs. len( ) , if close_chans_before_reload { 2 } else { 1 } , "{:?}" , evs) ;
3982+ for ev in evs {
3983+ if let Event :: PaymentForwarded { claim_from_onchain_tx, next_htlcs, .. } = ev {
3984+ if !claim_from_onchain_tx {
3985+ assert ! ( next_htlcs[ 0 ] . user_channel_id. is_some( ) )
3986+ }
3987+ } else {
3988+ panic ! ( "Unexpected event: {:?}" , ev) ;
39543989 }
3955- } else {
3956- panic ! ( ) ;
39573990 }
39583991 }
39593992
39603993 if !close_chans_before_reload || close_only_a {
3961- // Once we call `process_pending_events` the final `ChannelMonitor` for the B<->C channel
3962- // will fly, removing the payment preimage from it.
3963- check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3994+ if hold_post_reload_mon_update {
3995+ // The B-C monitor update from the completion action fires now.
3996+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3997+ }
39643998 assert ! ( nodes[ 1 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
39653999 send_payment ( & nodes[ 1 ] , & [ & nodes[ 2 ] ] , 100_000 ) ;
39664000 }
@@ -5179,17 +5213,16 @@ fn test_mpp_claim_to_holding_cell() {
51795213}
51805214
51815215#[ test]
5182- #[ should_panic(
5183- expected = "Watch::update_channel returned Completed while prior updates are still InProgress"
5184- ) ]
5185- fn test_monitor_update_fail_after_funding_spend ( ) {
5186- // When a counterparty commitment transaction confirms (funding spend), the
5187- // ChannelMonitor sets funding_spend_seen. If a commitment_signed from the
5188- // counterparty is then processed (a race between chain events and message
5189- // processing), update_monitor returns Err because no_further_updates_allowed()
5190- // is true. ChainMonitor overrides the result to InProgress, permanently
5191- // freezing the channel. A subsequent preimage claim returning Completed then
5192- // triggers the per-channel assertion.
5216+ fn test_monitor_update_after_funding_spend ( ) {
5217+ // Test that monitor updates still work after a funding spend is detected by the
5218+ // ChainMonitor but before ChannelManager has processed the corresponding block.
5219+ //
5220+ // When the counterparty commitment transaction confirms (funding spend), the
5221+ // ChannelMonitor sets funding_spend_seen and no_further_updates_allowed() returns
5222+ // true. ChainMonitor overrides all subsequent update_channel results to InProgress
5223+ // to freeze the channel. These overridden updates complete via deferred completions
5224+ // in release_pending_monitor_events, so that MonitorUpdateCompletionActions (like
5225+ // PaymentClaimed) can still fire.
51935226 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
51945227 let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
51955228 let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
@@ -5200,7 +5233,7 @@ fn test_monitor_update_fail_after_funding_spend() {
52005233 let ( _, _, chan_id, _) = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
52015234
52025235 // Route payment 1 fully so B can claim it later.
5203- let ( payment_preimage_1, _payment_hash_1 , ..) =
5236+ let ( payment_preimage_1, payment_hash_1 , ..) =
52045237 route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
52055238
52065239 // Get A's commitment tx (this is the "counterparty" commitment from B's perspective).
@@ -5209,10 +5242,14 @@ fn test_monitor_update_fail_after_funding_spend() {
52095242
52105243 // Confirm A's commitment tx on B's chain_monitor ONLY (not on B's ChannelManager).
52115244 // This sets funding_spend_seen in the monitor, making no_further_updates_allowed() true.
5245+ // We also update the best block on the chain_monitor so the broadcaster height is
5246+ // consistent when claiming HTLCs.
52125247 let ( block_hash, height) = nodes[ 1 ] . best_block_info ( ) ;
52135248 let block = create_dummy_block ( block_hash, height + 1 , vec ! [ as_commitment_tx[ 0 ] . clone( ) ] ) ;
52145249 let txdata: Vec < _ > = block. txdata . iter ( ) . enumerate ( ) . collect ( ) ;
52155250 nodes[ 1 ] . chain_monitor . chain_monitor . transactions_confirmed ( & block. header , & txdata, height + 1 ) ;
5251+ nodes[ 1 ] . chain_monitor . chain_monitor . best_block_updated ( & block. header , height + 1 ) ;
5252+ nodes[ 1 ] . blocks . lock ( ) . unwrap ( ) . push ( ( block, height + 1 ) ) ;
52165253
52175254 // Send payment 2 from A to B.
52185255 let ( route, payment_hash_2, _, payment_secret_2) =
@@ -5234,15 +5271,38 @@ fn test_monitor_update_fail_after_funding_spend() {
52345271
52355272 nodes[ 1 ] . node . handle_update_add_htlc ( node_a_id, & payment_event. msgs [ 0 ] ) ;
52365273
5237- // B processes commitment_signed. The monitor's update_monitor succeeds on the
5238- // update steps, but returns Err at the end because no_further_updates_allowed()
5239- // is true (funding_spend_seen). ChainMonitor overrides the result to InProgress .
5274+ // B processes commitment_signed. The monitor applies the update but returns Err
5275+ // because no_further_updates_allowed() is true. ChainMonitor overrides to InProgress,
5276+ // freezing the channel .
52405277 nodes[ 1 ] . node . handle_commitment_signed ( node_a_id, & payment_event. commitment_msg [ 0 ] ) ;
52415278 check_added_monitors ( & nodes[ 1 ] , 1 ) ;
5242- assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
52435279
5244- // B claims payment 1. The PaymentPreimage monitor update returns Completed
5245- // (update_monitor succeeds for preimage, and persister returns Completed),
5246- // but the prior InProgress from the commitment_signed is still pending.
5280+ // B claims payment 1. The preimage monitor update also returns InProgress (deferred),
5281+ // so no Completed-while-InProgress assertion fires.
52475282 nodes[ 1 ] . node . claim_funds ( payment_preimage_1) ;
5283+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
5284+
5285+ // First event cycle: the force-close MonitorEvent (CommitmentTxConfirmed) fires first,
5286+ // then the deferred completions resolve. The force-close generates a ChannelForceClosed
5287+ // update (also deferred), which blocks completion actions. So we only get ChannelClosed.
5288+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
5289+ assert_eq ! ( events. len( ) , 1 ) ;
5290+ match & events[ 0 ] {
5291+ Event :: ChannelClosed { reason : ClosureReason :: CommitmentTxConfirmed , .. } => { } ,
5292+ _ => panic ! ( "Unexpected event: {:?}" , events[ 0 ] ) ,
5293+ }
5294+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
5295+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
5296+
5297+ // Second event cycle: the ChannelForceClosed deferred completion resolves, unblocking
5298+ // the PaymentClaimed completion action.
5299+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
5300+ assert_eq ! ( events. len( ) , 1 ) ;
5301+ match & events[ 0 ] {
5302+ Event :: PaymentClaimed { payment_hash, amount_msat, .. } => {
5303+ assert_eq ! ( payment_hash_1, * payment_hash) ;
5304+ assert_eq ! ( 1_000_000 , * amount_msat) ;
5305+ } ,
5306+ _ => panic ! ( "Unexpected event: {:?}" , events[ 0 ] ) ,
5307+ }
52485308}
0 commit comments