Skip to content

Commit 9af5699

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 c4404a8 commit 9af5699

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

lightning/src/ln/channelmanager.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ pub(super) enum PendingHTLCRouting {
125125
},
126126
}
127127

128+
impl PendingHTLCRouting {
129+
fn incoming_cltv_expiry(&self) -> Option<u32> {
130+
match self {
131+
Self::Forward { incoming_cltv_expiry, .. } => *incoming_cltv_expiry,
132+
Self::Receive { incoming_cltv_expiry, .. } => Some(*incoming_cltv_expiry),
133+
Self::ReceiveKeysend { incoming_cltv_expiry, .. } => Some(*incoming_cltv_expiry),
134+
}
135+
}
136+
}
137+
128138
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
129139
pub(super) struct PendingHTLCInfo {
130140
pub(super) routing: PendingHTLCRouting,
@@ -189,6 +199,9 @@ pub(crate) struct HTLCPreviousHopData {
189199
// This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
190200
// channel with a preimage provided by the forward channel.
191201
outpoint: OutPoint,
202+
/// Used to preserve our backwards channel by failing back in case an HTLC claim in the forward
203+
/// channel remains unconfirmed for too long.
204+
pub(crate) cltv_expiry: Option<u32>,
192205
}
193206

194207
enum OnionPayload {
@@ -3746,13 +3759,14 @@ where
37463759
err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
37473760
})?;
37483761

3749-
if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
3762+
if let PendingHTLCRouting::Forward { short_channel_id, incoming_cltv_expiry, .. } = payment.forward_info.routing {
37503763
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
37513764
short_channel_id: payment.prev_short_channel_id,
37523765
outpoint: payment.prev_funding_outpoint,
37533766
htlc_id: payment.prev_htlc_id,
37543767
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
37553768
phantom_shared_secret: None,
3769+
cltv_expiry: incoming_cltv_expiry,
37563770
});
37573771

37583772
let failure_reason = HTLCFailReason::from_failure_code(0x4000 | 10);
@@ -3790,6 +3804,7 @@ where
37903804
outgoing_cltv_value, ..
37913805
}
37923806
}) => {
3807+
let cltv_expiry = routing.incoming_cltv_expiry();
37933808
macro_rules! failure_handler {
37943809
($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
37953810
log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
@@ -3800,6 +3815,7 @@ where
38003815
htlc_id: prev_htlc_id,
38013816
incoming_packet_shared_secret: incoming_shared_secret,
38023817
phantom_shared_secret: $phantom_ss,
3818+
cltv_expiry,
38033819
});
38043820

38053821
let reason = if $next_hop_unknown {
@@ -3903,7 +3919,8 @@ where
39033919
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id: _,
39043920
forward_info: PendingHTLCInfo {
39053921
incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
3906-
routing: PendingHTLCRouting::Forward { onion_packet, .. }, skimmed_fee_msat, ..
3922+
routing: PendingHTLCRouting::Forward { onion_packet, incoming_cltv_expiry, .. },
3923+
skimmed_fee_msat, ..
39073924
},
39083925
}) => {
39093926
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);
@@ -3914,6 +3931,7 @@ where
39143931
incoming_packet_shared_secret: incoming_shared_secret,
39153932
// Phantom payments are only PendingHTLCRouting::Receive.
39163933
phantom_shared_secret: None,
3934+
cltv_expiry: incoming_cltv_expiry,
39173935
});
39183936
if let Err(e) = chan.get_mut().queue_add_htlc(outgoing_amt_msat,
39193937
payment_hash, outgoing_cltv_value, htlc_source.clone(),
@@ -3994,6 +4012,7 @@ where
39944012
htlc_id: prev_htlc_id,
39954013
incoming_packet_shared_secret: incoming_shared_secret,
39964014
phantom_shared_secret,
4015+
cltv_expiry: Some(cltv_expiry),
39974016
},
39984017
// We differentiate the received value from the sender intended value
39994018
// if possible so that we don't prematurely mark MPP payments complete
@@ -4023,6 +4042,7 @@ where
40234042
htlc_id: $htlc.prev_hop.htlc_id,
40244043
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
40254044
phantom_shared_secret,
4045+
cltv_expiry: Some(cltv_expiry),
40264046
}), payment_hash,
40274047
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
40284048
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
@@ -4721,7 +4741,10 @@ where
47214741
&self.pending_events, &self.logger)
47224742
{ self.push_pending_forwards_ev(); }
47234743
},
4724-
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint }) => {
4744+
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id,
4745+
ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint,
4746+
cltv_expiry: _,
4747+
}) => {
47254748
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with {:?}", log_bytes!(payment_hash.0), onion_error);
47264749
let err_packet = onion_error.get_encrypted_failure_packet(incoming_packet_shared_secret, phantom_shared_secret);
47274750

@@ -5929,6 +5952,7 @@ where
59295952
htlc_id: prev_htlc_id,
59305953
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
59315954
phantom_shared_secret: None,
5955+
cltv_expiry: forward_info.routing.incoming_cltv_expiry(),
59325956
});
59335957

59345958
failed_intercept_forwards.push((htlc_source, forward_info.payment_hash,
@@ -7047,6 +7071,7 @@ where
70477071
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
70487072
phantom_shared_secret: None,
70497073
outpoint: htlc.prev_funding_outpoint,
7074+
cltv_expiry: htlc.forward_info.routing.incoming_cltv_expiry(),
70507075
});
70517076

70527077
let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
@@ -7810,6 +7835,7 @@ impl_writeable_tlv_based!(HTLCPreviousHopData, {
78107835
(0, short_channel_id, required),
78117836
(1, phantom_shared_secret, option),
78127837
(2, outpoint, required),
7838+
(3, cltv_expiry, option),
78137839
(4, htlc_id, required),
78147840
(6, incoming_packet_shared_secret, required)
78157841
});

0 commit comments

Comments
 (0)