@@ -2082,3 +2082,148 @@ fn test_reload_node_without_preimage_fails_htlc() {
20822082 // nodes[0] should now have received the failure and generate PaymentFailed.
20832083 expect_payment_failed_conditions ( & nodes[ 0 ] , payment_hash, false , PaymentFailedConditions :: new ( ) ) ;
20842084}
2085+
2086+ #[ test]
2087+ fn test_reload_with_mpp_claims_on_same_channel ( ) {
2088+ // Test that if a forwarding node has two HTLCs for the same MPP payment that were both
2089+ // irrevocably removed on the outbound edge via claim but are still forwarded-and-unresolved
2090+ // on the inbound edge, both HTLCs will be claimed backwards on restart.
2091+ //
2092+ // Topology:
2093+ // nodes[0] ----chan_0_1----> nodes[1] ----chan_1_2_a----> nodes[2]
2094+ // \----chan_1_2_b---/
2095+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
2096+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
2097+ let persister;
2098+ let new_chain_monitor;
2099+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
2100+ let nodes_1_deserialized;
2101+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
2102+
2103+ let node_0_id = nodes[ 0 ] . node . get_our_node_id ( ) ;
2104+ let node_1_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
2105+ let node_2_id = nodes[ 2 ] . node . get_our_node_id ( ) ;
2106+
2107+ let chan_0_1 = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 2_000_000 , 0 ) ;
2108+ let chan_1_2_a = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) ;
2109+ let chan_1_2_b = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) ;
2110+
2111+ let chan_id_0_1 = chan_0_1. 2 ;
2112+ let chan_id_1_2_a = chan_1_2_a. 2 ;
2113+ let chan_id_1_2_b = chan_1_2_b. 2 ;
2114+
2115+ // Send an MPP payment large enough that the router must split it across both outbound channels.
2116+ // Each 1M sat outbound channel has 100M msat max in-flight, so 150M msat requires splitting.
2117+ let amt_msat = 150_000_000 ;
2118+ let ( route, payment_hash, payment_preimage, payment_secret) =
2119+ get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 2 ] , amt_msat) ;
2120+
2121+ let payment_id = PaymentId ( nodes[ 0 ] . keys_manager . backing . get_secure_random_bytes ( ) ) ;
2122+ nodes[ 0 ] . node . send_payment_with_route (
2123+ route, payment_hash, RecipientOnionFields :: secret_only ( payment_secret) , payment_id,
2124+ ) . unwrap ( ) ;
2125+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
2126+
2127+ // Forward the first HTLC nodes[0] -> nodes[1] -> nodes[2]. Note that the second HTLC is released
2128+ // from the holding cell during the first HTLC's commitment_signed_dance.
2129+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
2130+ assert_eq ! ( events. len( ) , 1 ) ;
2131+ let payment_event_1 = SendEvent :: from_event ( events. remove ( 0 ) ) ;
2132+
2133+ nodes[ 1 ] . node . handle_update_add_htlc ( node_0_id, & payment_event_1. msgs [ 0 ] ) ;
2134+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
2135+ nodes[ 1 ] . node . handle_commitment_signed_batch_test ( node_0_id, & payment_event_1. commitment_msg ) ;
2136+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
2137+ let ( _, raa, holding_cell_htlcs) =
2138+ do_main_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 0 ] , false ) ;
2139+ assert_eq ! ( holding_cell_htlcs. len( ) , 1 ) ;
2140+ let payment_event_2 = holding_cell_htlcs. into_iter ( ) . next ( ) . unwrap ( ) ;
2141+ nodes[ 1 ] . node . handle_revoke_and_ack ( node_0_id, & raa) ;
2142+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
2143+
2144+ nodes[ 1 ] . node . process_pending_htlc_forwards ( ) ;
2145+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
2146+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
2147+ assert_eq ! ( events. len( ) , 1 ) ;
2148+ let ev_1_2 = events. remove ( 0 ) ;
2149+ pass_along_path (
2150+ & nodes[ 1 ] , & [ & nodes[ 2 ] ] , amt_msat, payment_hash, Some ( payment_secret) , ev_1_2, false , None ,
2151+ ) ;
2152+
2153+ // Second HTLC: full path nodes[0] -> nodes[1] -> nodes[2]. PaymentClaimable expected at end.
2154+ pass_along_path (
2155+ & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , amt_msat, payment_hash, Some ( payment_secret) ,
2156+ payment_event_2, true , None ,
2157+ ) ;
2158+
2159+ // Claim the HTLCs such that they're fully removed from the outbound edge, but disconnect
2160+ // node_0<>node_1 so that they can't be claimed backwards by node_1.
2161+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
2162+ check_added_monitors ( & nodes[ 2 ] , 2 ) ;
2163+ expect_payment_claimed ! ( nodes[ 2 ] , payment_hash, amt_msat) ;
2164+
2165+ nodes[ 0 ] . node . peer_disconnected ( node_1_id) ;
2166+ nodes[ 1 ] . node . peer_disconnected ( node_0_id) ;
2167+
2168+ let mut events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
2169+ assert_eq ! ( events. len( ) , 2 ) ;
2170+ for ev in events {
2171+ match ev {
2172+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates, .. } => {
2173+ assert_eq ! ( * node_id, node_1_id) ;
2174+ assert_eq ! ( updates. update_fulfill_htlcs. len( ) , 1 ) ;
2175+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( node_2_id, updates. update_fulfill_htlcs [ 0 ] . clone ( ) ) ;
2176+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
2177+ do_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 2 ] , & updates. commitment_signed , false , false ) ;
2178+ } ,
2179+ _ => panic ! ( "Unexpected event" ) ,
2180+ }
2181+ }
2182+
2183+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
2184+ assert_eq ! ( events. len( ) , 2 ) ;
2185+ for event in events {
2186+ expect_payment_forwarded (
2187+ event, & nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1000 ) , None , false , false , false ,
2188+ ) ;
2189+ }
2190+
2191+ // Clear the holding cell's claim entries on chan_0_1 before serialization.
2192+ // This simulates a crash where both HTLCs were fully removed on the outbound edges but are
2193+ // still present on the inbound edge without a resolution.
2194+ nodes[ 1 ] . node . test_clear_channel_holding_cell ( node_0_id, chan_id_0_1) ;
2195+
2196+ let node_1_serialized = nodes[ 1 ] . node . encode ( ) ;
2197+ let mon_0_1_serialized = get_monitor ! ( nodes[ 1 ] , chan_id_0_1) . encode ( ) ;
2198+ let mon_1_2_a_serialized = get_monitor ! ( nodes[ 1 ] , chan_id_1_2_a) . encode ( ) ;
2199+ let mon_1_2_b_serialized = get_monitor ! ( nodes[ 1 ] , chan_id_1_2_b) . encode ( ) ;
2200+
2201+ reload_node ! (
2202+ nodes[ 1 ] ,
2203+ node_1_serialized,
2204+ & [ & mon_0_1_serialized, & mon_1_2_a_serialized, & mon_1_2_b_serialized] ,
2205+ persister,
2206+ new_chain_monitor,
2207+ nodes_1_deserialized,
2208+ Some ( true )
2209+ ) ;
2210+
2211+ // When the claims are reconstructed during reload, PaymentForwarded events are regenerated.
2212+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
2213+ assert_eq ! ( events. len( ) , 2 ) ;
2214+ for event in events {
2215+ expect_payment_forwarded (
2216+ event, & nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1000 ) , None , false , false , false ,
2217+ ) ;
2218+ }
2219+ // Fetching events triggers the pending monitor updates (one for each HTLC preimage) to be applied.
2220+ check_added_monitors ( & nodes[ 1 ] , 2 ) ;
2221+
2222+ // Reconnect nodes[1] to nodes[0]. Both claims should be in nodes[1]'s holding cell.
2223+ let mut reconnect_args = ReconnectArgs :: new ( & nodes[ 1 ] , & nodes[ 0 ] ) ;
2224+ reconnect_args. pending_cell_htlc_claims = ( 0 , 2 ) ;
2225+ reconnect_nodes ( reconnect_args) ;
2226+
2227+ // nodes[0] should now have received both fulfills and generate PaymentSent.
2228+ expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
2229+ }
0 commit comments