@@ -2419,7 +2419,8 @@ where
2419
2419
2420
2420
fn construct_recv_pending_htlc_info (
2421
2421
& self , hop_data : msgs:: OnionHopData , shared_secret : [ u8 ; 32 ] , payment_hash : PaymentHash ,
2422
- amt_msat : u64 , cltv_expiry : u32 , phantom_shared_secret : Option < [ u8 ; 32 ] > , allow_underpay : bool
2422
+ amt_msat : u64 , cltv_expiry : u32 , phantom_shared_secret : Option < [ u8 ; 32 ] > , allow_underpay : bool ,
2423
+ counterparty_skimmed_fee_msat : Option < u64 > ,
2423
2424
) -> Result < PendingHTLCInfo , ReceiveError > {
2424
2425
// final_incorrect_cltv_expiry
2425
2426
if hop_data. outgoing_cltv_value > cltv_expiry {
@@ -2446,7 +2447,10 @@ where
2446
2447
msg : "The final CLTV expiry is too soon to handle" ,
2447
2448
} ) ;
2448
2449
}
2449
- if !allow_underpay && hop_data. amt_to_forward > amt_msat {
2450
+ if ( !allow_underpay && hop_data. amt_to_forward > amt_msat) ||
2451
+ ( allow_underpay && hop_data. amt_to_forward >
2452
+ amt_msat. saturating_add ( counterparty_skimmed_fee_msat. unwrap_or ( 0 ) ) )
2453
+ {
2450
2454
return Err ( ReceiveError {
2451
2455
err_code : 19 ,
2452
2456
err_data : amt_msat. to_be_bytes ( ) . to_vec ( ) ,
@@ -2745,7 +2749,7 @@ where
2745
2749
onion_utils:: Hop :: Receive ( next_hop_data) => {
2746
2750
// OUR PAYMENT!
2747
2751
match self . construct_recv_pending_htlc_info ( next_hop_data, shared_secret, msg. payment_hash ,
2748
- msg. amount_msat , msg. cltv_expiry , None , allow_underpay)
2752
+ msg. amount_msat , msg. cltv_expiry , None , allow_underpay, msg . skimmed_fee_msat )
2749
2753
{
2750
2754
Ok ( info) => {
2751
2755
// Note that we could obviously respond immediately with an update_fulfill_htlc
@@ -3561,7 +3565,7 @@ where
3561
3565
onion_utils:: Hop :: Receive ( hop_data) => {
3562
3566
match self . construct_recv_pending_htlc_info( hop_data,
3563
3567
incoming_shared_secret, payment_hash, outgoing_amt_msat,
3564
- outgoing_cltv_value, Some ( phantom_shared_secret) , false )
3568
+ outgoing_cltv_value, Some ( phantom_shared_secret) , false , None )
3565
3569
{
3566
3570
Ok ( info) => phantom_receives. push( ( prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![ ( info, prev_htlc_id) ] ) ) ,
3567
3571
Err ( ReceiveError { err_code, err_data, msg } ) => failed_payment!( msg, err_code, err_data, Some ( phantom_shared_secret) )
@@ -9590,6 +9594,50 @@ mod tests {
9590
9594
get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , last_random_pk) ;
9591
9595
}
9592
9596
9597
+ #[ test]
9598
+ fn reject_excessively_underpaying_htlcs ( ) {
9599
+ let chanmon_cfg = create_chanmon_cfgs ( 1 ) ;
9600
+ let node_cfg = create_node_cfgs ( 1 , & chanmon_cfg) ;
9601
+ let node_chanmgr = create_node_chanmgrs ( 1 , & node_cfg, & [ None ] ) ;
9602
+ let node = create_network ( 1 , & node_cfg, & node_chanmgr) ;
9603
+ let sender_intended_amt_msat = 100 ;
9604
+ let extra_fee_msat = 10 ;
9605
+ let hop_data = msgs:: OnionHopData {
9606
+ amt_to_forward : 100 ,
9607
+ outgoing_cltv_value : 42 ,
9608
+ format : msgs:: OnionHopDataFormat :: FinalNode {
9609
+ keysend_preimage : None ,
9610
+ payment_metadata : None ,
9611
+ payment_data : Some ( msgs:: FinalOnionHopData {
9612
+ payment_secret : PaymentSecret ( [ 0 ; 32 ] ) , total_msat : sender_intended_amt_msat,
9613
+ } ) ,
9614
+ }
9615
+ } ;
9616
+ // Check that if the amount we received + the penultimate hop extra fee is less than the sender
9617
+ // intended amount, we fail the payment.
9618
+ if let Err ( crate :: ln:: channelmanager:: ReceiveError { err_code, .. } ) =
9619
+ node[ 0 ] . node . construct_recv_pending_htlc_info ( hop_data, [ 0 ; 32 ] , PaymentHash ( [ 0 ; 32 ] ) ,
9620
+ sender_intended_amt_msat - extra_fee_msat - 1 , 42 , None , true , Some ( extra_fee_msat) )
9621
+ {
9622
+ assert_eq ! ( err_code, 19 ) ;
9623
+ } else { panic ! ( ) ; }
9624
+
9625
+ // If amt_received + extra_fee is equal to the sender intended amount, we're fine.
9626
+ let hop_data = msgs:: OnionHopData { // This is the same hop_data as above, OnionHopData doesn't implement Clone
9627
+ amt_to_forward : 100 ,
9628
+ outgoing_cltv_value : 42 ,
9629
+ format : msgs:: OnionHopDataFormat :: FinalNode {
9630
+ keysend_preimage : None ,
9631
+ payment_metadata : None ,
9632
+ payment_data : Some ( msgs:: FinalOnionHopData {
9633
+ payment_secret : PaymentSecret ( [ 0 ; 32 ] ) , total_msat : sender_intended_amt_msat,
9634
+ } ) ,
9635
+ }
9636
+ } ;
9637
+ assert ! ( node[ 0 ] . node. construct_recv_pending_htlc_info( hop_data, [ 0 ; 32 ] , PaymentHash ( [ 0 ; 32 ] ) ,
9638
+ sender_intended_amt_msat - extra_fee_msat, 42 , None , true , Some ( extra_fee_msat) ) . is_ok( ) ) ;
9639
+ }
9640
+
9593
9641
#[ cfg( anchors) ]
9594
9642
#[ test]
9595
9643
fn test_anchors_zero_fee_htlc_tx_fallback ( ) {
0 commit comments