Skip to content

Commit 3d1d830

Browse files
committed
Add cltv expiry to HTLCPreviousHopData
1 parent d2873bc commit 3d1d830

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,16 @@ pub(crate) struct HTLCPreviousHopData {
193193
// Note that this may be an outbound SCID alias for the associated channel.
194194
short_channel_id: u64,
195195
user_channel_id: Option<u128>,
196-
htlc_id: u64,
196+
pub(crate) htlc_id: u64,
197197
incoming_packet_shared_secret: [u8; 32],
198198
phantom_shared_secret: Option<[u8; 32]>,
199199

200200
// This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
201201
// channel with a preimage provided by the forward channel.
202202
outpoint: OutPoint,
203+
/// Used to preserve our backwards channel by failing back in case an HTLC claim in the forward
204+
/// channel remains unconfirmed for too long.
205+
pub(crate) cltv_expiry: Option<u32>,
203206
}
204207

205208
enum OnionPayload {
@@ -3821,14 +3824,15 @@ where
38213824
err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
38223825
})?;
38233826

3824-
if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
3827+
if let PendingHTLCRouting::Forward { short_channel_id, incoming_cltv_expiry, .. } = payment.forward_info.routing {
38253828
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
38263829
short_channel_id: payment.prev_short_channel_id,
38273830
user_channel_id: Some(payment.prev_user_channel_id),
38283831
outpoint: payment.prev_funding_outpoint,
38293832
htlc_id: payment.prev_htlc_id,
38303833
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
38313834
phantom_shared_secret: None,
3835+
cltv_expiry: incoming_cltv_expiry,
38323836
});
38333837

38343838
let failure_reason = HTLCFailReason::from_failure_code(0x4000 | 10);
@@ -3866,6 +3870,7 @@ where
38663870
outgoing_cltv_value, ..
38673871
}
38683872
}) => {
3873+
let cltv_expiry = routing.incoming_cltv_expiry();
38693874
macro_rules! failure_handler {
38703875
($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
38713876
log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
@@ -3877,6 +3882,7 @@ where
38773882
htlc_id: prev_htlc_id,
38783883
incoming_packet_shared_secret: incoming_shared_secret,
38793884
phantom_shared_secret: $phantom_ss,
3885+
cltv_expiry,
38803886
});
38813887

38823888
let reason = if $next_hop_unknown {
@@ -3980,7 +3986,7 @@ where
39803986
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
39813987
forward_info: PendingHTLCInfo {
39823988
incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
3983-
routing: PendingHTLCRouting::Forward { onion_packet, .. },
3989+
routing: PendingHTLCRouting::Forward { onion_packet, incoming_cltv_expiry, .. },
39843990
skimmed_fee_msat, ..
39853991
},
39863992
}) => {
@@ -3993,6 +3999,7 @@ where
39933999
incoming_packet_shared_secret: incoming_shared_secret,
39944000
// Phantom payments are only PendingHTLCRouting::Receive.
39954001
phantom_shared_secret: None,
4002+
cltv_expiry: incoming_cltv_expiry,
39964003
});
39974004
if let Err(e) = chan.get_mut().queue_add_htlc(outgoing_amt_msat,
39984005
payment_hash, outgoing_cltv_value, htlc_source.clone(),
@@ -4074,6 +4081,7 @@ where
40744081
htlc_id: prev_htlc_id,
40754082
incoming_packet_shared_secret: incoming_shared_secret,
40764083
phantom_shared_secret,
4084+
cltv_expiry: Some(cltv_expiry),
40774085
},
40784086
// We differentiate the received value from the sender intended value
40794087
// if possible so that we don't prematurely mark MPP payments complete
@@ -4104,6 +4112,7 @@ where
41044112
htlc_id: $htlc.prev_hop.htlc_id,
41054113
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
41064114
phantom_shared_secret,
4115+
cltv_expiry: Some(cltv_expiry),
41074116
}), payment_hash,
41084117
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
41094118
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
@@ -6113,6 +6122,7 @@ where
61136122
htlc_id: prev_htlc_id,
61146123
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
61156124
phantom_shared_secret: None,
6125+
cltv_expiry: forward_info.routing.incoming_cltv_expiry(),
61166126
});
61176127

61186128
failed_intercept_forwards.push((htlc_source, forward_info.payment_hash,
@@ -7242,6 +7252,7 @@ where
72427252
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
72437253
phantom_shared_secret: None,
72447254
outpoint: htlc.prev_funding_outpoint,
7255+
cltv_expiry: htlc.forward_info.routing.incoming_cltv_expiry(),
72457256
});
72467257

72477258
let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
@@ -8053,6 +8064,7 @@ impl_writeable_tlv_based!(HTLCPreviousHopData, {
80538064
(0, short_channel_id, required),
80548065
(1, phantom_shared_secret, option),
80558066
(2, outpoint, required),
8067+
(3, cltv_expiry, option),
80568068
(4, htlc_id, required),
80578069
(6, incoming_packet_shared_secret, required),
80588070
(7, user_channel_id, option),

0 commit comments

Comments
 (0)