Skip to content

Commit 97e54ee

Browse files
Add prev_channel_outpoint and prev_counterparty_id to previous hop data
These will be used in the next commit to pipe said outpoint and node ID to HTLCPreviousHopData, which will then allow us to update a channel with a preimage after said channel has closed.
1 parent cda8fb7 commit 97e54ee

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ pub(super) enum HTLCForwardInfo {
120120
AddHTLC {
121121
prev_short_channel_id: u64,
122122
prev_htlc_id: u64,
123+
prev_channel_outpoint: OutPoint,
124+
prev_counterparty_node_id: PublicKey,
123125
forward_info: PendingHTLCInfo,
124126
},
125127
FailHTLC {
@@ -132,6 +134,8 @@ pub(super) enum HTLCForwardInfo {
132134
#[derive(Clone, PartialEq)]
133135
pub(crate) struct HTLCPreviousHopData {
134136
short_channel_id: u64,
137+
outpoint: OutPoint,
138+
counterparty_node_id: PublicKey,
135139
htlc_id: u64,
136140
incoming_packet_shared_secret: [u8; 32],
137141
}
@@ -1554,9 +1558,12 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
15541558
failed_forwards.reserve(pending_forwards.len());
15551559
for forward_info in pending_forwards.drain(..) {
15561560
match forward_info {
1557-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1561+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info,
1562+
prev_channel_outpoint, prev_counterparty_node_id } => {
15581563
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
15591564
short_channel_id: prev_short_channel_id,
1565+
outpoint: prev_channel_outpoint,
1566+
counterparty_node_id: prev_counterparty_node_id,
15601567
htlc_id: prev_htlc_id,
15611568
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
15621569
});
@@ -1583,10 +1590,13 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
15831590
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
15841591
routing: PendingHTLCRouting::Forward {
15851592
onion_packet, ..
1586-
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value }, } => {
1593+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
1594+
prev_channel_outpoint, prev_counterparty_node_id } => {
15871595
log_trace!(self.logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", log_bytes!(payment_hash.0), prev_short_channel_id, short_chan_id);
15881596
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
15891597
short_channel_id: prev_short_channel_id,
1598+
outpoint: prev_channel_outpoint,
1599+
counterparty_node_id: prev_counterparty_node_id,
15901600
htlc_id: prev_htlc_id,
15911601
incoming_packet_shared_secret: incoming_shared_secret,
15921602
});
@@ -1701,9 +1711,12 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
17011711
match forward_info {
17021712
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
17031713
routing: PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry },
1704-
incoming_shared_secret, payment_hash, amt_to_forward, .. }, } => {
1714+
incoming_shared_secret, payment_hash, amt_to_forward, .. },
1715+
prev_channel_outpoint, prev_counterparty_node_id } => {
17051716
let prev_hop = HTLCPreviousHopData {
17061717
short_channel_id: prev_short_channel_id,
1718+
outpoint: prev_channel_outpoint,
1719+
counterparty_node_id: prev_counterparty_node_id,
17071720
htlc_id: prev_htlc_id,
17081721
incoming_packet_shared_secret: incoming_shared_secret,
17091722
};
@@ -1738,6 +1751,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
17381751
);
17391752
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
17401753
short_channel_id: htlc.prev_hop.short_channel_id,
1754+
outpoint: prev_channel_outpoint,
1755+
counterparty_node_id: prev_counterparty_node_id,
17411756
htlc_id: htlc.prev_hop.htlc_id,
17421757
incoming_packet_shared_secret: htlc.prev_hop.incoming_packet_shared_secret,
17431758
}), payment_hash,
@@ -1940,7 +1955,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
19401955
}
19411956
}
19421957
},
1943-
HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret }) => {
1958+
HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret, .. }) => {
19441959
let err_packet = match onion_error {
19451960
HTLCFailReason::Reason { failure_code, data } => {
19461961
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
@@ -2201,7 +2216,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
22012216

22022217
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored(&self.logger);
22032218
if !pending_forwards.is_empty() {
2204-
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), pending_forwards));
2219+
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), funding_txo.clone(), channel.get_counterparty_node_id(), pending_forwards));
22052220
}
22062221
htlc_failures.append(&mut pending_failures);
22072222

@@ -2685,8 +2700,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
26852700
}
26862701

26872702
#[inline]
2688-
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Vec<(PendingHTLCInfo, u64)>)]) {
2689-
for &mut (prev_short_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
2703+
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, PublicKey, Vec<(PendingHTLCInfo, u64)>)]) {
2704+
for &mut (prev_short_channel_id, prev_channel_outpoint, prev_counterparty_node_id, ref mut pending_forwards) in per_source_pending_forwards {
26902705
let mut forward_event = None;
26912706
if !pending_forwards.is_empty() {
26922707
let mut channel_state = self.channel_state.lock().unwrap();
@@ -2699,10 +2714,12 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
26992714
PendingHTLCRouting::Receive { .. } => 0,
27002715
}) {
27012716
hash_map::Entry::Occupied(mut entry) => {
2702-
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
2717+
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_channel_outpoint,
2718+
prev_htlc_id, forward_info, prev_counterparty_node_id });
27032719
},
27042720
hash_map::Entry::Vacant(entry) => {
2705-
entry.insert(vec!(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info }));
2721+
entry.insert(vec!(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_channel_outpoint, prev_htlc_id,
2722+
forward_info, prev_counterparty_node_id }));
27062723
}
27072724
}
27082725
}
@@ -2755,18 +2772,19 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
27552772
msg,
27562773
});
27572774
}
2758-
break Ok((pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel")))
2775+
break Ok((pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"), chan.get().get_funding_txo().unwrap(), chan.get().get_counterparty_node_id()))
27592776
},
27602777
hash_map::Entry::Vacant(_) => break Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
27612778
}
27622779
};
27632780
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id);
27642781
match res {
2765-
Ok((pending_forwards, mut pending_failures, short_channel_id)) => {
2782+
Ok((pending_forwards, mut pending_failures, short_channel_id, channel_outpoint,
2783+
counterparty_node_id)) => {
27662784
for failure in pending_failures.drain(..) {
27672785
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
27682786
}
2769-
self.forward_htlcs(&mut [(short_channel_id, pending_forwards)]);
2787+
self.forward_htlcs(&mut [(short_channel_id, channel_outpoint, counterparty_node_id, pending_forwards)]);
27702788
Ok(())
27712789
},
27722790
Err(e) => Err(e)
@@ -3543,6 +3561,8 @@ impl Readable for PendingHTLCStatus {
35433561

35443562
impl_writeable!(HTLCPreviousHopData, 0, {
35453563
short_channel_id,
3564+
outpoint,
3565+
counterparty_node_id,
35463566
htlc_id,
35473567
incoming_packet_shared_secret
35483568
});
@@ -3619,9 +3639,11 @@ impl Readable for HTLCFailReason {
36193639
impl Writeable for HTLCForwardInfo {
36203640
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
36213641
match self {
3622-
&HTLCForwardInfo::AddHTLC { ref prev_short_channel_id, ref prev_htlc_id, ref forward_info } => {
3642+
&HTLCForwardInfo::AddHTLC { ref prev_short_channel_id, ref prev_channel_outpoint, ref prev_counterparty_node_id, ref prev_htlc_id, ref forward_info } => {
36233643
0u8.write(writer)?;
36243644
prev_short_channel_id.write(writer)?;
3645+
prev_channel_outpoint.write(writer)?;
3646+
prev_counterparty_node_id.write(writer)?;
36253647
prev_htlc_id.write(writer)?;
36263648
forward_info.write(writer)?;
36273649
},
@@ -3640,6 +3662,8 @@ impl Readable for HTLCForwardInfo {
36403662
match <u8 as Readable>::read(reader)? {
36413663
0 => Ok(HTLCForwardInfo::AddHTLC {
36423664
prev_short_channel_id: Readable::read(reader)?,
3665+
prev_channel_outpoint: Readable::read(reader)?,
3666+
prev_counterparty_node_id: Readable::read(reader)?,
36433667
prev_htlc_id: Readable::read(reader)?,
36443668
forward_info: Readable::read(reader)?,
36453669
}),

0 commit comments

Comments
 (0)