@@ -19,12 +19,13 @@ use crate::chain::transaction::OutPoint;
1919use crate :: chain:: { ChannelMonitorUpdateStatus , Listen , Watch } ;
2020use crate :: events:: { ClosureReason , Event , HTLCHandlingFailureType , PaymentPurpose } ;
2121use crate :: ln:: channel:: AnnouncementSigsState ;
22- use crate :: ln:: channelmanager:: { PaymentId , RAACommitmentOrder , RecipientOnionFields } ;
22+ use crate :: ln:: channelmanager:: { PaymentId , RAACommitmentOrder , RecipientOnionFields , Retry } ;
2323use crate :: ln:: msgs;
2424use crate :: ln:: msgs:: {
2525 BaseMessageHandler , ChannelMessageHandler , MessageSendEvent , RoutingMessageHandler ,
2626} ;
2727use crate :: ln:: types:: ChannelId ;
28+ use crate :: routing:: router:: { PaymentParameters , RouteParameters } ;
2829use crate :: sign:: NodeSigner ;
2930use crate :: util:: native_async:: FutureQueue ;
3031use crate :: util:: persist:: {
@@ -3535,7 +3536,7 @@ fn do_test_blocked_chan_preimage_release(completion_mode: BlockedUpdateComplMode
35353536 assert ! ( a. is_none( ) ) ;
35363537
35373538 nodes[ 1 ] . node . handle_revoke_and_ack ( node_a_id, & raa) ;
3538- check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3539+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
35393540 assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
35403541 }
35413542
@@ -3554,8 +3555,8 @@ fn do_test_blocked_chan_preimage_release(completion_mode: BlockedUpdateComplMode
35543555 panic ! ( ) ;
35553556 }
35563557
3557- // The event processing should release the last RAA updates on both channels .
3558- check_added_monitors ( & nodes[ 1 ] , 2 ) ;
3558+ // The event processing should release the last RAA update .
3559+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
35593560
35603561 // When we fetch the next update the message getter will generate the next update for nodes[2],
35613562 // generating a further monitor update.
@@ -5055,3 +5056,111 @@ fn native_async_persist() {
50555056 panic ! ( ) ;
50565057 }
50575058}
5059+
5060+ #[ test]
5061+ fn test_mpp_claim_to_holding_cell ( ) {
5062+ // Previously, if an MPP payment was claimed while one channel was AwaitingRAA (causing the
5063+ // HTLC claim to go into the holding cell), and the RAA came in before the async monitor
5064+ // update with the preimage completed, the channel could hang waiting on itself.
5065+ // This tests that behavior.
5066+ let chanmon_cfgs = create_chanmon_cfgs ( 4 ) ;
5067+ let node_cfgs = create_node_cfgs ( 4 , & chanmon_cfgs) ;
5068+ let node_chanmgrs = create_node_chanmgrs ( 4 , & node_cfgs, & [ None , None , None , None ] ) ;
5069+ let nodes = create_network ( 4 , & node_cfgs, & node_chanmgrs) ;
5070+
5071+ let node_b_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
5072+ let node_c_id = nodes[ 2 ] . node . get_our_node_id ( ) ;
5073+ let node_d_id = nodes[ 3 ] . node . get_our_node_id ( ) ;
5074+
5075+ // First open channels in a diamond and deliver the MPP payment.
5076+ let chan_1_scid = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 0 . contents . short_channel_id ;
5077+ let chan_2_scid = create_announced_chan_between_nodes ( & nodes, 0 , 2 ) . 0 . contents . short_channel_id ;
5078+ let ( chan_3_update, _, chan_3_id, ..) = create_announced_chan_between_nodes ( & nodes, 1 , 3 ) ;
5079+ let chan_3_scid = chan_3_update. contents . short_channel_id ;
5080+ let ( chan_4_update, _, chan_4_id, ..) = create_announced_chan_between_nodes ( & nodes, 2 , 3 ) ;
5081+ let chan_4_scid = chan_4_update. contents . short_channel_id ;
5082+
5083+ let ( mut route, paymnt_hash_1, preimage_1, payment_secret) =
5084+ get_route_and_payment_hash ! ( & nodes[ 0 ] , nodes[ 3 ] , 500_000 ) ;
5085+ let path = route. paths [ 0 ] . clone ( ) ;
5086+ route. paths . push ( path) ;
5087+ route. paths [ 0 ] . hops [ 0 ] . pubkey = node_b_id;
5088+ route. paths [ 0 ] . hops [ 0 ] . short_channel_id = chan_1_scid;
5089+ route. paths [ 0 ] . hops [ 1 ] . short_channel_id = chan_3_scid;
5090+ route. paths [ 0 ] . hops [ 1 ] . fee_msat = 250_000 ;
5091+ route. paths [ 1 ] . hops [ 0 ] . pubkey = node_c_id;
5092+ route. paths [ 1 ] . hops [ 0 ] . short_channel_id = chan_2_scid;
5093+ route. paths [ 1 ] . hops [ 1 ] . short_channel_id = chan_4_scid;
5094+ route. paths [ 1 ] . hops [ 1 ] . fee_msat = 250_000 ;
5095+ let paths = & [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] [ ..] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] [ ..] ] ;
5096+ send_along_route_with_secret ( & nodes[ 0 ] , route, paths, 500_000 , paymnt_hash_1, payment_secret) ;
5097+
5098+ // Put the C <-> D channel into AwaitingRaa
5099+ let ( preimage_2, paymnt_hash_2, payment_secret_2) = get_payment_preimage_hash ! ( nodes[ 3 ] ) ;
5100+ let onion = RecipientOnionFields :: secret_only ( payment_secret_2) ;
5101+ let id = PaymentId ( [ 42 ; 32 ] ) ;
5102+ let pay_params = PaymentParameters :: from_node_id ( node_d_id, TEST_FINAL_CLTV ) ;
5103+ let route_params = RouteParameters :: from_payment_params_and_value ( pay_params, 400_000 ) ;
5104+ nodes[ 2 ] . node . send_payment ( paymnt_hash_2, onion, id, route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
5105+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
5106+
5107+ let mut payment_event = SendEvent :: from_node ( & nodes[ 2 ] ) ;
5108+ nodes[ 3 ] . node . handle_update_add_htlc ( node_c_id, & payment_event. msgs [ 0 ] ) ;
5109+ nodes[ 3 ] . node . handle_commitment_signed_batch_test ( node_c_id, & payment_event. commitment_msg ) ;
5110+ check_added_monitors ( & nodes[ 3 ] , 1 ) ;
5111+
5112+ let ( raa, cs) = get_revoke_commit_msgs ( & nodes[ 3 ] , & node_c_id) ;
5113+ nodes[ 2 ] . node . handle_revoke_and_ack ( node_d_id, & raa) ;
5114+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
5115+
5116+ nodes[ 2 ] . node . handle_commitment_signed_batch_test ( node_d_id, & cs) ;
5117+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
5118+
5119+ let cs_raa = get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendRevokeAndACK , node_d_id) ;
5120+
5121+ // Now claim the payment, completing both channel monitor updates async
5122+ // In the current code, the C <-> D channel happens to be the `durable_preimage_channel`,
5123+ // improving coverage somewhat but it isn't strictly critical to the test.
5124+ chanmon_cfgs[ 3 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
5125+ chanmon_cfgs[ 3 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
5126+ nodes[ 3 ] . node . claim_funds ( preimage_1) ;
5127+ check_added_monitors ( & nodes[ 3 ] , 2 ) ;
5128+
5129+ // Complete the B <-> D monitor update, freeing the first fulfill.
5130+ let ( latest_id, _) = get_latest_mon_update_id ( & nodes[ 3 ] , chan_3_id) ;
5131+ nodes[ 3 ] . chain_monitor . chain_monitor . channel_monitor_updated ( chan_3_id, latest_id) . unwrap ( ) ;
5132+ let mut b_claim = get_htlc_update_msgs ( & nodes[ 3 ] , & node_b_id) ;
5133+
5134+ // When we deliver the pre-claim RAA, node D will shove the monitor update into the blocked
5135+ // state since we have a pending MPP payment which is blocking RAA monitor updates.
5136+ nodes[ 3 ] . node . handle_revoke_and_ack ( node_c_id, & cs_raa) ;
5137+ check_added_monitors ( & nodes[ 3 ] , 0 ) ;
5138+
5139+ // Finally, complete the C <-> D monitor update. Previously, this unlock failed to be processed
5140+ // due to the existence of the blocked RAA update above.
5141+ let ( latest_id, _) = get_latest_mon_update_id ( & nodes[ 3 ] , chan_4_id) ;
5142+ nodes[ 3 ] . chain_monitor . chain_monitor . channel_monitor_updated ( chan_4_id, latest_id) . unwrap ( ) ;
5143+ // Once we process monitor events (in this case by checking for the `PaymentClaimed` event, the
5144+ // RAA monitor update blocked above will be released.
5145+ expect_payment_claimed ! ( nodes[ 3 ] , paymnt_hash_1, 500_000 ) ;
5146+ check_added_monitors ( & nodes[ 3 ] , 1 ) ;
5147+
5148+ // After the RAA monitor update completes, the C <-> D channel will be able to generate its
5149+ // fulfill updates as well.
5150+ let mut c_claim = get_htlc_update_msgs ( & nodes[ 3 ] , & node_c_id) ;
5151+ check_added_monitors ( & nodes[ 3 ] , 1 ) ;
5152+
5153+ // Finally, clear all the pending payments.
5154+ let path = [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] [ ..] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] [ ..] ] ;
5155+ let mut args = ClaimAlongRouteArgs :: new ( & nodes[ 0 ] , & path[ ..] , preimage_1) ;
5156+ let b_claim_msgs = ( b_claim. update_fulfill_htlcs . pop ( ) . unwrap ( ) , b_claim. commitment_signed ) ;
5157+ let c_claim_msgs = ( c_claim. update_fulfill_htlcs . pop ( ) . unwrap ( ) , c_claim. commitment_signed ) ;
5158+ let claims = vec ! [ ( b_claim_msgs, node_b_id) , ( c_claim_msgs, node_c_id) ] ;
5159+ pass_claimed_payment_along_route_from_ev ( 250_000 , claims, args) ;
5160+
5161+ expect_payment_sent ( & nodes[ 0 ] , preimage_1, None , true , true ) ;
5162+
5163+ expect_and_process_pending_htlcs ( & nodes[ 3 ] , false ) ;
5164+ expect_payment_claimable ! ( nodes[ 3 ] , paymnt_hash_2, payment_secret_2, 400_000 ) ;
5165+ claim_payment ( & nodes[ 2 ] , & [ & nodes[ 3 ] ] , preimage_2) ;
5166+ }
0 commit comments