Skip to content

Commit d8edaad

Browse files
committed
Add cltv_expiry to HTLCPreviousHopData
This is needed in order to fail back an inbound HTLC before our upstream counterparty goes to claim on-chain, which will be added in an upcoming commit.
1 parent ace46b4 commit d8edaad

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ pub(super) enum PendingHTLCRouting {
121121
},
122122
}
123123

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+
124134
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
125135
pub(super) struct PendingHTLCInfo {
126136
pub(super) routing: PendingHTLCRouting,
@@ -185,6 +195,9 @@ pub(crate) struct HTLCPreviousHopData {
185195
// This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
186196
// channel with a preimage provided by the forward channel.
187197
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>,
188201
}
189202

190203
enum OnionPayload {
@@ -3709,13 +3722,14 @@ where
37093722
err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
37103723
})?;
37113724

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 {
37133726
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
37143727
short_channel_id: payment.prev_short_channel_id,
37153728
outpoint: payment.prev_funding_outpoint,
37163729
htlc_id: payment.prev_htlc_id,
37173730
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
37183731
phantom_shared_secret: None,
3732+
cltv_expiry: incoming_cltv_expiry,
37193733
});
37203734

37213735
let failure_reason = HTLCFailReason::from_failure_code(0x4000 | 10);
@@ -3753,6 +3767,7 @@ where
37533767
outgoing_cltv_value, ..
37543768
}
37553769
}) => {
3770+
let cltv_expiry = routing.incoming_cltv_expiry();
37563771
macro_rules! failure_handler {
37573772
($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
37583773
log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
@@ -3763,6 +3778,7 @@ where
37633778
htlc_id: prev_htlc_id,
37643779
incoming_packet_shared_secret: incoming_shared_secret,
37653780
phantom_shared_secret: $phantom_ss,
3781+
cltv_expiry,
37663782
});
37673783

37683784
let reason = if $next_hop_unknown {
@@ -3866,7 +3882,8 @@ where
38663882
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id: _,
38673883
forward_info: PendingHTLCInfo {
38683884
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, ..
38703887
},
38713888
}) => {
38723889
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
38773894
incoming_packet_shared_secret: incoming_shared_secret,
38783895
// Phantom payments are only PendingHTLCRouting::Receive.
38793896
phantom_shared_secret: None,
3897+
cltv_expiry: incoming_cltv_expiry,
38803898
});
38813899
if let Err(e) = chan.get_mut().queue_add_htlc(outgoing_amt_msat,
38823900
payment_hash, outgoing_cltv_value, htlc_source.clone(),
@@ -3956,6 +3974,7 @@ where
39563974
htlc_id: prev_htlc_id,
39573975
incoming_packet_shared_secret: incoming_shared_secret,
39583976
phantom_shared_secret,
3977+
cltv_expiry: Some(cltv_expiry),
39593978
},
39603979
// We differentiate the received value from the sender intended value
39613980
// if possible so that we don't prematurely mark MPP payments complete
@@ -3985,6 +4004,7 @@ where
39854004
htlc_id: $htlc.prev_hop.htlc_id,
39864005
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
39874006
phantom_shared_secret,
4007+
cltv_expiry: Some(cltv_expiry),
39884008
}), payment_hash,
39894009
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
39904010
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
@@ -4676,7 +4696,10 @@ where
46764696
&self.pending_events, &self.logger)
46774697
{ self.push_pending_forwards_ev(); }
46784698
},
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+
}) => {
46804703
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with {:?}", log_bytes!(payment_hash.0), onion_error);
46814704
let err_packet = onion_error.get_encrypted_failure_packet(incoming_packet_shared_secret, phantom_shared_secret);
46824705

@@ -5845,6 +5868,7 @@ where
58455868
htlc_id: prev_htlc_id,
58465869
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
58475870
phantom_shared_secret: None,
5871+
cltv_expiry: forward_info.routing.incoming_cltv_expiry(),
58485872
});
58495873

58505874
failed_intercept_forwards.push((htlc_source, forward_info.payment_hash,
@@ -6963,6 +6987,7 @@ where
69636987
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
69646988
phantom_shared_secret: None,
69656989
outpoint: htlc.prev_funding_outpoint,
6990+
cltv_expiry: htlc.forward_info.routing.incoming_cltv_expiry(),
69666991
});
69676992

69686993
let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
@@ -7722,6 +7747,7 @@ impl_writeable_tlv_based!(HTLCPreviousHopData, {
77227747
(0, short_channel_id, required),
77237748
(1, phantom_shared_secret, option),
77247749
(2, outpoint, required),
7750+
(3, cltv_expiry, option),
77257751
(4, htlc_id, required),
77267752
(6, incoming_packet_shared_secret, required)
77277753
});

0 commit comments

Comments
 (0)