@@ -3904,11 +3904,61 @@ 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+ if hold_post_reload_mon_update {
3908+ // When the persister returns InProgress, background events are replayed
3909+ // during check_closed_event but stay pending (InProgress). Only the
3910+ // ChannelClosed event fires.
3911+ check_closed_event (
3912+ & nodes[ 1 ] ,
3913+ 1 ,
3914+ ClosureReason :: CommitmentTxConfirmed ,
3915+ & [ node_a_id] ,
3916+ 100000 ,
3917+ ) ;
3918+ } else {
3919+ // When the persister returns Completed, check_closed_event triggers
3920+ // process_background_events (replaying the A-B preimage and force-close
3921+ // updates) AND process_pending_monitor_events (resolving the deferred
3922+ // completions). This fires both ChannelClosed and PaymentForwarded.
3923+ let evs = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
3924+ assert_eq ! ( evs. len( ) , 2 , "{:?}" , evs) ;
3925+ let mut found_close = false ;
3926+ let mut found_forward = false ;
3927+ for ev in & evs {
3928+ match ev {
3929+ Event :: ChannelClosed {
3930+ reason : ClosureReason :: CommitmentTxConfirmed , ..
3931+ } => {
3932+ found_close = true ;
3933+ } ,
3934+ Event :: PaymentForwarded {
3935+ claim_from_onchain_tx, next_user_channel_id, ..
3936+ } => {
3937+ assert ! ( !claim_from_onchain_tx) ;
3938+ assert ! ( next_user_channel_id. is_some( ) ) ;
3939+ found_forward = true ;
3940+ } ,
3941+ _ => panic ! ( "Unexpected event: {:?}" , ev) ,
3942+ }
3943+ }
3944+ assert ! ( found_close && found_forward) ;
3945+ // The background events replayed mons_added updates to chain_monitor.
3946+ check_added_monitors ( & nodes[ 1 ] , mons_added) ;
3947+ }
39093948 }
39103949 nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3911- check_added_monitors ( & nodes[ 1 ] , mons_added) ;
3950+ if hold_post_reload_mon_update {
3951+ check_added_monitors ( & nodes[ 1 ] , mons_added) ;
3952+ } else if close_chans_before_reload {
3953+ // For close_chans_before_reload with hold=false, the background events are
3954+ // replayed during timer_tick (first process_background_events call).
3955+ check_added_monitors ( & nodes[ 1 ] , mons_added) ;
3956+ } else {
3957+ // For !close_chans_before_reload with hold=false, the background events were
3958+ // already replayed during check_closed_event above. timer_tick is a no-op for
3959+ // monitor updates.
3960+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3961+ }
39123962
39133963 // Finally, check that B created a payment preimage transaction and close out the payment.
39143964 let bs_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
@@ -3923,44 +3973,64 @@ fn do_test_durable_preimages_on_closed_channel(
39233973 check_closed_broadcast ( & nodes[ 0 ] , 1 , false ) ;
39243974 expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
39253975
3976+ if close_chans_before_reload && !hold_post_reload_mon_update {
3977+ // For close_chans_before_reload with hold=false, the deferred completions
3978+ // haven't been processed yet. Trigger process_pending_monitor_events now.
3979+ let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
3980+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3981+ }
3982+
39263983 if !close_chans_before_reload || close_only_a {
39273984 // Make sure the B<->C channel is still alive and well by sending a payment over it.
39283985 let mut reconnect_args = ReconnectArgs :: new ( & nodes[ 1 ] , & nodes[ 2 ] ) ;
39293986 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 ;
3987+ if hold_post_reload_mon_update {
3988+ // When the A-B update is still InProgress, B-C monitor updates are blocked,
3989+ // so the responding commitment_signed is a duplicate that generates no update.
3990+ reconnect_args. pending_responding_commitment_signed_dup_monitor . 1 = true ;
3991+ }
39343992 reconnect_args. pending_raa . 1 = true ;
39353993
39363994 reconnect_nodes ( reconnect_args) ;
39373995 }
39383996
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) ;
3997+ if hold_post_reload_mon_update {
3998+ // When the persister returned InProgress, we need to manually complete the
3999+ // A-B monitor update to unblock the PaymentForwarded completion action.
4000+ let ( _, ab_update_id) = nodes[ 1 ] . chain_monitor . get_latest_mon_update_id ( chan_id_ab) ;
4001+ nodes[ 1 ]
4002+ . chain_monitor
4003+ . chain_monitor
4004+ . force_channel_monitor_updated ( chan_id_ab, ab_update_id) ;
4005+ }
39434006
39444007 // If the A<->B channel was closed before we reload, we'll replay the claim against it on
39454008 // reload, causing the `PaymentForwarded` event to get replayed.
39464009 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_user_channel_id, .. } = 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_user_channel_id. is_some( ) )
4010+ if !close_chans_before_reload && !hold_post_reload_mon_update {
4011+ // PaymentForwarded already fired during check_closed_event above.
4012+ assert ! ( evs. is_empty( ) , "{:?}" , evs) ;
4013+ } else {
4014+ assert_eq ! ( evs. len( ) , if close_chans_before_reload { 2 } else { 1 } , "{:?}" , evs) ;
4015+ for ev in evs {
4016+ if let Event :: PaymentForwarded { claim_from_onchain_tx, next_user_channel_id, .. } = ev
4017+ {
4018+ if !claim_from_onchain_tx {
4019+ // If the outbound channel is still open, the `next_user_channel_id` should
4020+ // be available. This was previously broken.
4021+ assert ! ( next_user_channel_id. is_some( ) )
4022+ }
4023+ } else {
4024+ panic ! ( "Unexpected event: {:?}" , ev) ;
39544025 }
3955- } else {
3956- panic ! ( ) ;
39574026 }
39584027 }
39594028
39604029 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 ) ;
4030+ if hold_post_reload_mon_update {
4031+ // The B-C monitor update from the completion action fires now.
4032+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
4033+ }
39644034 assert ! ( nodes[ 1 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
39654035 send_payment ( & nodes[ 1 ] , & [ & nodes[ 2 ] ] , 100_000 ) ;
39664036 }
@@ -5178,15 +5248,17 @@ fn test_mpp_claim_to_holding_cell() {
51785248}
51795249
51805250#[ test]
5181- #[ should_panic( expected = "Watch::update_channel returned Completed while prior updates are still InProgress" ) ]
5182- fn test_monitor_update_fail_after_funding_spend ( ) {
5183- // When a counterparty commitment transaction confirms (funding spend), the
5184- // ChannelMonitor sets funding_spend_seen. If a commitment_signed from the
5185- // counterparty is then processed (a race between chain events and message
5186- // processing), update_monitor returns Err because no_further_updates_allowed()
5187- // is true. ChainMonitor overrides the result to InProgress, permanently
5188- // freezing the channel. A subsequent preimage claim returning Completed then
5189- // triggers the per-channel assertion.
5251+ fn test_monitor_update_after_funding_spend ( ) {
5252+ // Test that monitor updates still work after a funding spend is detected by the
5253+ // ChainMonitor but before ChannelManager has processed the corresponding block.
5254+ //
5255+ // When the counterparty commitment transaction confirms (funding spend), the
5256+ // ChannelMonitor sets funding_spend_seen and no_further_updates_allowed() returns
5257+ // true. ChainMonitor overrides all subsequent update_channel results to InProgress
5258+ // to freeze the channel. These overridden updates complete via deferred completions
5259+ // in the next release_pending_monitor_events cycle, together with the force-close
5260+ // MonitorEvent, so that MonitorUpdateCompletionActions (like PaymentClaimed) can
5261+ // still fire.
51905262 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
51915263 let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
51925264 let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
@@ -5197,7 +5269,7 @@ fn test_monitor_update_fail_after_funding_spend() {
51975269 let ( _, _, chan_id, _) = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
51985270
51995271 // Route payment 1 fully so B can claim it later.
5200- let ( payment_preimage_1, _payment_hash_1 , ..) =
5272+ let ( payment_preimage_1, payment_hash_1 , ..) =
52015273 route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
52025274
52035275 // Get A's commitment tx (this is the "counterparty" commitment from B's perspective).
@@ -5206,12 +5278,14 @@ fn test_monitor_update_fail_after_funding_spend() {
52065278
52075279 // Confirm A's commitment tx on B's chain_monitor ONLY (not on B's ChannelManager).
52085280 // This sets funding_spend_seen in the monitor, making no_further_updates_allowed() true.
5281+ // We also update the best block on the chain_monitor so the broadcaster height is
5282+ // consistent when claiming HTLCs.
52095283 let ( block_hash, height) = nodes[ 1 ] . best_block_info ( ) ;
52105284 let block = create_dummy_block ( block_hash, height + 1 , vec ! [ as_commitment_tx[ 0 ] . clone( ) ] ) ;
52115285 let txdata: Vec < _ > = block. txdata . iter ( ) . enumerate ( ) . collect ( ) ;
5212- nodes[ 1 ] . chain_monitor . chain_monitor . transactions_confirmed (
5213- & block. header , & txdata , height + 1 ,
5214- ) ;
5286+ nodes[ 1 ] . chain_monitor . chain_monitor . transactions_confirmed ( & block . header , & txdata , height + 1 ) ;
5287+ nodes [ 1 ] . chain_monitor . chain_monitor . best_block_updated ( & block. header , height + 1 ) ;
5288+ nodes [ 1 ] . blocks . lock ( ) . unwrap ( ) . push ( ( block , height + 1 ) ) ;
52155289
52165290 // Send payment 2 from A to B.
52175291 let ( route, payment_hash_2, _, payment_secret_2) =
@@ -5233,15 +5307,38 @@ fn test_monitor_update_fail_after_funding_spend() {
52335307
52345308 nodes[ 1 ] . node . handle_update_add_htlc ( node_a_id, & payment_event. msgs [ 0 ] ) ;
52355309
5236- // B processes commitment_signed. The monitor's update_monitor succeeds on the
5237- // update steps, but returns Err at the end because no_further_updates_allowed()
5238- // is true (funding_spend_seen). ChainMonitor overrides the result to InProgress .
5310+ // B processes commitment_signed. The monitor applies the update but returns Err
5311+ // because no_further_updates_allowed() is true. ChainMonitor overrides to InProgress,
5312+ // freezing the channel .
52395313 nodes[ 1 ] . node . handle_commitment_signed ( node_a_id, & payment_event. commitment_msg [ 0 ] ) ;
52405314 check_added_monitors ( & nodes[ 1 ] , 1 ) ;
5241- assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
52425315
5243- // B claims payment 1. The PaymentPreimage monitor update returns Completed
5244- // (update_monitor succeeds for preimage, and persister returns Completed),
5245- // but the prior InProgress from the commitment_signed is still pending.
5316+ // B claims payment 1. This triggers process_pending_monitor_events internally, which
5317+ // processes the deferred completions and the force-close MonitorEvent together.
5318+ // ChainMonitor also overrides the preimage update to InProgress (channel is post-close),
5319+ // so no Completed-while-InProgress assertion fires.
52465320 nodes[ 1 ] . node . claim_funds ( payment_preimage_1) ;
5321+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
5322+
5323+ // Processing events resolves the deferred completions and force-close together.
5324+ // We get both a PaymentClaimed and a ChannelClosed event.
5325+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
5326+ assert_eq ! ( events. len( ) , 2 ) ;
5327+ match & events[ 0 ] {
5328+ Event :: PaymentClaimed { payment_hash, amount_msat, .. } => {
5329+ assert_eq ! ( payment_hash_1, * payment_hash) ;
5330+ assert_eq ! ( 1_000_000 , * amount_msat) ;
5331+ } ,
5332+ _ => panic ! ( "Unexpected event: {:?}" , events[ 0 ] ) ,
5333+ }
5334+ match & events[ 1 ] {
5335+ Event :: ChannelClosed { reason : ClosureReason :: CommitmentTxConfirmed , .. } => { } ,
5336+ _ => panic ! ( "Unexpected event: {:?}" , events[ 1 ] ) ,
5337+ }
5338+
5339+ // The deferred completions unfreeze the channel, then the force-close MonitorEvent
5340+ // closes it. This generates a ChannelForceClosed monitor update and pending messages
5341+ // (revoke_and_ack, commitment update, error, channel update). Clear them.
5342+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
5343+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
52475344}
0 commit comments