@@ -271,19 +271,24 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
271271
272272 // ...and make sure we can force-close a frozen channel
273273 let message = "Channel force-closed" . to_owned ( ) ;
274- let reason = ClosureReason :: HolderForceClosed {
275- broadcasted_latest_txn : Some ( true ) ,
276- message : message. clone ( ) ,
277- } ;
278274 nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & channel_id, & node_b_id, message) . unwrap ( ) ;
279275 check_added_monitors ( & nodes[ 0 ] , 1 ) ;
280276 check_closed_broadcast ( & nodes[ 0 ] , 1 , true ) ;
281277
282- // TODO: Once we hit the chain with the failure transaction we should check that we get a
283- // PaymentPathFailed event
284-
285278 assert_eq ! ( nodes[ 0 ] . node. list_channels( ) . len( ) , 0 ) ;
286- check_closed_event ( & nodes[ 0 ] , 1 , reason, & [ node_b_id] , 100000 ) ;
279+
280+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
281+ assert_eq ! ( events. len( ) , 3 ) ;
282+ assert ! ( matches!(
283+ events. pop( ) . unwrap( ) ,
284+ Event :: ChannelClosed { reason: ClosureReason :: HolderForceClosed { .. } , .. }
285+ ) ) ;
286+ expect_payment_failed_conditions_event (
287+ events,
288+ payment_hash_2,
289+ false ,
290+ PaymentFailedConditions :: new ( ) ,
291+ ) ;
287292}
288293
289294#[ test]
@@ -5167,3 +5172,66 @@ fn test_mpp_claim_to_holding_cell() {
51675172 expect_payment_claimable ! ( nodes[ 3 ] , paymnt_hash_2, payment_secret_2, 400_000 ) ;
51685173 claim_payment ( & nodes[ 2 ] , & [ & nodes[ 3 ] ] , preimage_2) ;
51695174}
5175+
5176+ #[ test]
5177+ fn test_force_close_with_in_progress_monitor_update_drops_htlc ( ) {
5178+ // When a channel is force-closed while a monitor update is InProgress, any HTLC in
5179+ // LocalAnnounced state (committed to the channel but monitor update not yet persisted) may
5180+ // not be included in the ChannelMonitor. Verify that the payment is properly failed back
5181+ // via PaymentPathFailed/PaymentFailed events rather than being silently dropped.
5182+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
5183+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
5184+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
5185+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
5186+
5187+ let node_b_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
5188+
5189+ let channel_id = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
5190+
5191+ let ( route, payment_hash, _, payment_secret) =
5192+ get_route_and_payment_hash ! ( & nodes[ 0 ] , nodes[ 1 ] , 1_000_000 ) ;
5193+
5194+ // Set node A's monitor persistence to InProgress so the HTLC monitor update won't complete.
5195+ chanmon_cfgs[ 0 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
5196+
5197+ let onion = RecipientOnionFields :: secret_only ( payment_secret) ;
5198+ let payment_id = PaymentId ( payment_hash. 0 ) ;
5199+ nodes[ 0 ] . node . send_payment_with_route ( route, payment_hash, onion, payment_id) . unwrap ( ) ;
5200+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
5201+
5202+ // The HTLC is now LocalAnnounced but the monitor update hasn't been persisted.
5203+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5204+
5205+ // Force-close the channel while the monitor update is still InProgress.
5206+ nodes[ 0 ]
5207+ . node
5208+ . force_close_broadcasting_latest_txn ( & channel_id, & node_b_id, "force close" . to_owned ( ) )
5209+ . unwrap ( ) ;
5210+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
5211+ check_closed_broadcast ( & nodes[ 0 ] , 1 , true ) ;
5212+
5213+ // The payment should be failed back since the channel is closed and the HTLC was never
5214+ // committed by the counterparty.
5215+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
5216+ assert_eq ! ( events. len( ) , 3 ) ;
5217+ assert ! ( matches!(
5218+ events. pop( ) . unwrap( ) ,
5219+ Event :: ChannelClosed { reason: ClosureReason :: HolderForceClosed { .. } , .. }
5220+ ) ) ;
5221+ expect_payment_failed_conditions_event (
5222+ events,
5223+ payment_hash,
5224+ false ,
5225+ PaymentFailedConditions :: new ( ) ,
5226+ ) ;
5227+
5228+ // Now complete the pending monitor update. The ChannelMonitor will learn about the HTLC,
5229+ // but the broadcast commitment transaction does not include it (it was never committed by
5230+ // the counterparty). Completing the update should not produce duplicate payment failure
5231+ // events or panics.
5232+ chanmon_cfgs[ 0 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: Completed ) ;
5233+ let ( latest_update, _) = nodes[ 0 ] . chain_monitor . get_latest_mon_update_id ( channel_id) ;
5234+ nodes[ 0 ] . chain_monitor . chain_monitor . force_channel_monitor_updated ( channel_id, latest_update) ;
5235+
5236+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
5237+ }
0 commit comments