@@ -6439,7 +6439,7 @@ fn test_fail_holding_cell_htlc_upon_free() {
6439
6439
// HTLC, but now that the fee has been raised the payment will now fail, causing
6440
6440
// us to surface its failure to the user.
6441
6441
chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
6442
- assert_eq ! ( chan_stat. holding_cell_outbound_amount_msat, max_can_send ) ;
6442
+ assert_eq ! ( chan_stat. holding_cell_outbound_amount_msat, 0 ) ;
6443
6443
nodes[ 0 ] . logger . assert_log ( "lightning::ln::channel" . to_string ( ) , "Freeing holding cell with 1 HTLC updates" . to_string ( ) , 1 ) ;
6444
6444
let failure_log = format ! ( "Failed to send HTLC with payment_hash {} due to Cannot send value that would put us under local channel reserve value" , log_bytes!( our_payment_hash. 0 ) ) ;
6445
6445
nodes[ 0 ] . logger . assert_log ( "lightning::ln::channel" . to_string ( ) , failure_log. to_string ( ) , 1 ) ;
@@ -6458,6 +6458,127 @@ fn test_fail_holding_cell_htlc_upon_free() {
6458
6458
}
6459
6459
}
6460
6460
6461
+ // Test that if multiple HTLCs are released from the holding cell and one is
6462
+ // valid but the other is no longer valid upon release, the valid HTLC can be
6463
+ // successfully completed while the other one fails as expected.
6464
+ #[ test]
6465
+ fn test_free_and_fail_holding_cell_htlcs ( ) {
6466
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
6467
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
6468
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
6469
+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
6470
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 95000000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
6471
+ let logger = test_utils:: TestLogger :: new ( ) ;
6472
+
6473
+ // First nodes[0] generates an update_fee, setting the channel's
6474
+ // pending_update_fee.
6475
+ nodes[ 0 ] . node . update_fee ( chan. 2 , get_feerate ! ( nodes[ 0 ] , chan. 2 ) + 200 ) . unwrap ( ) ;
6476
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
6477
+
6478
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
6479
+ assert_eq ! ( events. len( ) , 1 ) ;
6480
+ let ( update_msg, commitment_signed) = match events[ 0 ] {
6481
+ MessageSendEvent :: UpdateHTLCs { updates : msgs:: CommitmentUpdate { ref update_fee, ref commitment_signed, .. } , .. } => {
6482
+ ( update_fee. as_ref ( ) , commitment_signed)
6483
+ } ,
6484
+ _ => panic ! ( "Unexpected event" ) ,
6485
+ } ;
6486
+
6487
+ nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , update_msg. unwrap ( ) ) ;
6488
+
6489
+ let mut chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
6490
+ let channel_reserve = chan_stat. channel_reserve_msat ;
6491
+ let feerate = get_feerate ! ( nodes[ 0 ] , chan. 2 ) ;
6492
+
6493
+ // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
6494
+ let ( payment_preimage_1, payment_hash_1) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
6495
+ let amt_1 = 20000 ;
6496
+ let ( _, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
6497
+ let amt_2 = 5000000 - channel_reserve - 2 * commit_tx_fee_msat ( feerate, 2 + 1 ) - amt_1;
6498
+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
6499
+ let route_1 = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , amt_1, TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
6500
+ let route_2 = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , amt_2, TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
6501
+
6502
+ // Send 2 payments which pass reserve checks but get stuck in the holding cell.
6503
+ nodes[ 0 ] . node . send_payment ( & route_1, payment_hash_1, & None ) . unwrap ( ) ;
6504
+ chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
6505
+ assert_eq ! ( chan_stat. holding_cell_outbound_amount_msat, amt_1) ;
6506
+ nodes[ 0 ] . node . send_payment ( & route_2, payment_hash_2, & None ) . unwrap ( ) ;
6507
+ chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
6508
+ assert_eq ! ( chan_stat. holding_cell_outbound_amount_msat, amt_1 + amt_2) ;
6509
+
6510
+ // Flush the pending fee update.
6511
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , commitment_signed) ;
6512
+ let ( revoke_and_ack, commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
6513
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
6514
+ nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & revoke_and_ack) ;
6515
+ nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & commitment_signed) ;
6516
+ check_added_monitors ! ( nodes[ 0 ] , 2 ) ;
6517
+
6518
+ // Upon receipt of the RAA, there will be an attempt to resend the holding cell HTLCs,
6519
+ // but now that the fee has been raised the second payment will now fail, causing us
6520
+ // to surface its failure to the user. The first payment should succeed.
6521
+ chan_stat = get_channel_value_stat ! ( nodes[ 0 ] , chan. 2 ) ;
6522
+ assert_eq ! ( chan_stat. holding_cell_outbound_amount_msat, 0 ) ;
6523
+ nodes[ 0 ] . logger . assert_log ( "lightning::ln::channel" . to_string ( ) , "Freeing holding cell with 2 HTLC updates" . to_string ( ) , 1 ) ;
6524
+ let failure_log = format ! ( "Failed to send HTLC with payment_hash {} due to Cannot send value that would put us under local channel reserve value" , log_bytes!( payment_hash_2. 0 ) ) ;
6525
+ nodes[ 0 ] . logger . assert_log ( "lightning::ln::channel" . to_string ( ) , failure_log. to_string ( ) , 1 ) ;
6526
+
6527
+ // Check that the second payment failed to be sent out.
6528
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
6529
+ assert_eq ! ( events. len( ) , 1 ) ;
6530
+ match & events[ 0 ] {
6531
+ & Event :: PaymentFailed { ref payment_hash, ref rejected_by_dest, ref error_code, ref error_data } => {
6532
+ assert_eq ! ( payment_hash_2. clone( ) , * payment_hash) ;
6533
+ assert_eq ! ( * rejected_by_dest, false ) ;
6534
+ assert_eq ! ( * error_code, None ) ;
6535
+ assert_eq ! ( * error_data, None ) ;
6536
+ } ,
6537
+ _ => panic ! ( "Unexpected event" ) ,
6538
+ }
6539
+
6540
+ // Complete the first payment and the RAA from the fee update.
6541
+ let ( payment_event, send_raa_event) = {
6542
+ let mut msgs = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
6543
+ assert_eq ! ( msgs. len( ) , 2 ) ;
6544
+ ( SendEvent :: from_event ( msgs. remove ( 0 ) ) , msgs. remove ( 0 ) )
6545
+ } ;
6546
+ let raa = match send_raa_event {
6547
+ MessageSendEvent :: SendRevokeAndACK { msg, .. } => msg,
6548
+ _ => panic ! ( "Unexpected event" ) ,
6549
+ } ;
6550
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & raa) ;
6551
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
6552
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
6553
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
6554
+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
6555
+ assert_eq ! ( events. len( ) , 1 ) ;
6556
+ match events[ 0 ] {
6557
+ Event :: PendingHTLCsForwardable { .. } => { } ,
6558
+ _ => panic ! ( "Unexpected event" ) ,
6559
+ }
6560
+ nodes[ 1 ] . node . process_pending_htlc_forwards ( ) ;
6561
+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
6562
+ assert_eq ! ( events. len( ) , 1 ) ;
6563
+ match events[ 0 ] {
6564
+ Event :: PaymentReceived { .. } => { } ,
6565
+ _ => panic ! ( "Unexpected event" ) ,
6566
+ }
6567
+ nodes[ 1 ] . node . claim_funds ( payment_preimage_1, & None , amt_1) ;
6568
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
6569
+ let update_msgs = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
6570
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & update_msgs. update_fulfill_htlcs [ 0 ] ) ;
6571
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , update_msgs. commitment_signed, false , true ) ;
6572
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
6573
+ assert_eq ! ( events. len( ) , 1 ) ;
6574
+ match events[ 0 ] {
6575
+ Event :: PaymentSent { ref payment_preimage } => {
6576
+ assert_eq ! ( * payment_preimage, payment_preimage_1) ;
6577
+ }
6578
+ _ => panic ! ( "Unexpected event" ) ,
6579
+ }
6580
+ }
6581
+
6461
6582
// Test that if we fail to forward an HTLC that is being freed from the holding cell that the
6462
6583
// HTLC is failed backwards. We trigger this failure to forward the freed HTLC by increasing
6463
6584
// our fee while the HTLC is in the holding cell such that the HTLC is no longer affordable
0 commit comments