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