@@ -121,6 +121,16 @@ pub(super) enum PendingHTLCRouting {
121
121
} ,
122
122
}
123
123
124
+ impl PendingHTLCRouting {
125
+ fn incoming_cltv_expiry ( & self ) -> Option < u32 > {
126
+ match self {
127
+ Self :: Forward { incoming_cltv_expiry, .. } => * incoming_cltv_expiry,
128
+ Self :: Receive { incoming_cltv_expiry, .. } => Some ( * incoming_cltv_expiry) ,
129
+ Self :: ReceiveKeysend { incoming_cltv_expiry, .. } => Some ( * incoming_cltv_expiry) ,
130
+ }
131
+ }
132
+ }
133
+
124
134
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
125
135
pub ( super ) struct PendingHTLCInfo {
126
136
pub ( super ) routing : PendingHTLCRouting ,
@@ -185,6 +195,9 @@ pub(crate) struct HTLCPreviousHopData {
185
195
// This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
186
196
// channel with a preimage provided by the forward channel.
187
197
outpoint : OutPoint ,
198
+ // Used to preserve channel by failing back in case forward channel remains unconfirmed for too
199
+ // long.
200
+ pub ( crate ) cltv_expiry : Option < u32 > ,
188
201
}
189
202
190
203
enum OnionPayload {
@@ -3709,13 +3722,14 @@ where
3709
3722
err : format ! ( "Payment with intercept id {} not found" , log_bytes!( intercept_id. 0 ) )
3710
3723
} ) ?;
3711
3724
3712
- if let PendingHTLCRouting :: Forward { short_channel_id, .. } = payment. forward_info . routing {
3725
+ if let PendingHTLCRouting :: Forward { short_channel_id, incoming_cltv_expiry , .. } = payment. forward_info . routing {
3713
3726
let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
3714
3727
short_channel_id : payment. prev_short_channel_id ,
3715
3728
outpoint : payment. prev_funding_outpoint ,
3716
3729
htlc_id : payment. prev_htlc_id ,
3717
3730
incoming_packet_shared_secret : payment. forward_info . incoming_shared_secret ,
3718
3731
phantom_shared_secret : None ,
3732
+ cltv_expiry : incoming_cltv_expiry,
3719
3733
} ) ;
3720
3734
3721
3735
let failure_reason = HTLCFailReason :: from_failure_code ( 0x4000 | 10 ) ;
@@ -3753,6 +3767,7 @@ where
3753
3767
outgoing_cltv_value, ..
3754
3768
}
3755
3769
} ) => {
3770
+ let cltv_expiry = routing. incoming_cltv_expiry( ) ;
3756
3771
macro_rules! failure_handler {
3757
3772
( $msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
3758
3773
log_info!( self . logger, "Failed to accept/forward incoming HTLC: {}" , $msg) ;
@@ -3763,6 +3778,7 @@ where
3763
3778
htlc_id: prev_htlc_id,
3764
3779
incoming_packet_shared_secret: incoming_shared_secret,
3765
3780
phantom_shared_secret: $phantom_ss,
3781
+ cltv_expiry,
3766
3782
} ) ;
3767
3783
3768
3784
let reason = if $next_hop_unknown {
@@ -3866,7 +3882,8 @@ where
3866
3882
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id : _,
3867
3883
forward_info : PendingHTLCInfo {
3868
3884
incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
3869
- routing : PendingHTLCRouting :: Forward { onion_packet, .. } , skimmed_fee_msat, ..
3885
+ routing : PendingHTLCRouting :: Forward { onion_packet, incoming_cltv_expiry, .. } ,
3886
+ skimmed_fee_msat, ..
3870
3887
} ,
3871
3888
} ) => {
3872
3889
log_trace ! ( self . logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay" , prev_short_channel_id, log_bytes!( payment_hash. 0 ) , short_chan_id) ;
@@ -3877,6 +3894,7 @@ where
3877
3894
incoming_packet_shared_secret : incoming_shared_secret,
3878
3895
// Phantom payments are only PendingHTLCRouting::Receive.
3879
3896
phantom_shared_secret : None ,
3897
+ cltv_expiry : incoming_cltv_expiry,
3880
3898
} ) ;
3881
3899
if let Err ( e) = chan. get_mut ( ) . queue_add_htlc ( outgoing_amt_msat,
3882
3900
payment_hash, outgoing_cltv_value, htlc_source. clone ( ) ,
@@ -3956,6 +3974,7 @@ where
3956
3974
htlc_id : prev_htlc_id,
3957
3975
incoming_packet_shared_secret : incoming_shared_secret,
3958
3976
phantom_shared_secret,
3977
+ cltv_expiry : Some ( cltv_expiry) ,
3959
3978
} ,
3960
3979
// We differentiate the received value from the sender intended value
3961
3980
// if possible so that we don't prematurely mark MPP payments complete
@@ -3985,6 +4004,7 @@ where
3985
4004
htlc_id: $htlc. prev_hop. htlc_id,
3986
4005
incoming_packet_shared_secret: $htlc. prev_hop. incoming_packet_shared_secret,
3987
4006
phantom_shared_secret,
4007
+ cltv_expiry: Some ( cltv_expiry) ,
3988
4008
} ) , payment_hash,
3989
4009
HTLCFailReason :: reason( 0x4000 | 15 , htlc_msat_height_data) ,
3990
4010
HTLCDestination :: FailedPayment { payment_hash: $payment_hash } ,
@@ -4676,7 +4696,10 @@ where
4676
4696
& self . pending_events , & self . logger )
4677
4697
{ self . push_pending_forwards_ev ( ) ; }
4678
4698
} ,
4679
- HTLCSource :: PreviousHopData ( HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint } ) => {
4699
+ HTLCSource :: PreviousHopData ( HTLCPreviousHopData { ref short_channel_id, ref htlc_id,
4700
+ ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint,
4701
+ cltv_expiry : _,
4702
+ } ) => {
4680
4703
log_trace ! ( self . logger, "Failing HTLC with payment_hash {} backwards from us with {:?}" , log_bytes!( payment_hash. 0 ) , onion_error) ;
4681
4704
let err_packet = onion_error. get_encrypted_failure_packet ( incoming_packet_shared_secret, phantom_shared_secret) ;
4682
4705
@@ -5845,6 +5868,7 @@ where
5845
5868
htlc_id : prev_htlc_id,
5846
5869
incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
5847
5870
phantom_shared_secret : None ,
5871
+ cltv_expiry : forward_info. routing . incoming_cltv_expiry ( ) ,
5848
5872
} ) ;
5849
5873
5850
5874
failed_intercept_forwards. push ( ( htlc_source, forward_info. payment_hash ,
@@ -6963,6 +6987,7 @@ where
6963
6987
incoming_packet_shared_secret : htlc. forward_info . incoming_shared_secret ,
6964
6988
phantom_shared_secret : None ,
6965
6989
outpoint : htlc. prev_funding_outpoint ,
6990
+ cltv_expiry : htlc. forward_info . routing . incoming_cltv_expiry ( ) ,
6966
6991
} ) ;
6967
6992
6968
6993
let requested_forward_scid /* intercept scid */ = match htlc. forward_info . routing {
@@ -7722,6 +7747,7 @@ impl_writeable_tlv_based!(HTLCPreviousHopData, {
7722
7747
( 0 , short_channel_id, required) ,
7723
7748
( 1 , phantom_shared_secret, option) ,
7724
7749
( 2 , outpoint, required) ,
7750
+ ( 3 , cltv_expiry, option) ,
7725
7751
( 4 , htlc_id, required) ,
7726
7752
( 6 , incoming_packet_shared_secret, required)
7727
7753
} ) ;
0 commit comments