Skip to content

Commit e99fc10

Browse files
committed
Split only-receive/forward data out of PendingHTLCInfo into an enum
This should avoid blowing up the size of the struct when we add additional data that is only relevant for receive.
1 parent 83c9eb4 commit e99fc10

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

lightning/src/ln/channelmanager.rs

+67-27
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,20 @@ use std::ops::Deref;
6868
// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
6969
// our payment, which we can use to decode errors or inform the user that the payment was sent.
7070

71+
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
72+
enum PendingForwardReceiveHTLCInfo {
73+
Forward {
74+
onion_packet: msgs::OnionPacket,
75+
short_channel_id: u64, // This should be NonZero<u64> eventually
76+
},
77+
Receive {},
78+
}
79+
7180
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
7281
pub(super) struct PendingHTLCInfo {
73-
onion_packet: Option<msgs::OnionPacket>,
82+
type_data: PendingForwardReceiveHTLCInfo,
7483
incoming_shared_secret: [u8; 32],
7584
payment_hash: PaymentHash,
76-
short_channel_id: u64,
7785
pub(super) amt_to_forward: u64,
7886
pub(super) outgoing_cltv_value: u32,
7987
}
@@ -987,9 +995,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
987995
// delay) once they've send us a commitment_signed!
988996

989997
PendingHTLCStatus::Forward(PendingHTLCInfo {
990-
onion_packet: None,
998+
type_data: PendingForwardReceiveHTLCInfo::Receive {},
991999
payment_hash: msg.payment_hash.clone(),
992-
short_channel_id: 0,
9931000
incoming_shared_secret: shared_secret,
9941001
amt_to_forward: next_hop_data.amt_to_forward,
9951002
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
@@ -1033,24 +1040,29 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
10331040
let short_channel_id = match next_hop_data.format {
10341041
msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
10351042
msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
1036-
msgs::OnionHopDataFormat::FinalNode => {
1043+
msgs::OnionHopDataFormat::FinalNode { .. } => {
10371044
return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
10381045
},
10391046
};
10401047

10411048
PendingHTLCStatus::Forward(PendingHTLCInfo {
1042-
onion_packet: Some(outgoing_packet),
1049+
type_data: PendingForwardReceiveHTLCInfo::Forward {
1050+
onion_packet: outgoing_packet,
1051+
short_channel_id: short_channel_id,
1052+
},
10431053
payment_hash: msg.payment_hash.clone(),
1044-
short_channel_id: short_channel_id,
10451054
incoming_shared_secret: shared_secret,
10461055
amt_to_forward: next_hop_data.amt_to_forward,
10471056
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
10481057
})
10491058
};
10501059

10511060
channel_state = Some(self.channel_state.lock().unwrap());
1052-
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1053-
if onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
1061+
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref type_data, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1062+
// If short_channel_id is 0 here, we'll reject them in the body here (which is
1063+
// important as various things later assume we are a ::Receive if short_channel_id is
1064+
// non-0.
1065+
if let &PendingForwardReceiveHTLCInfo::Forward { ref short_channel_id, .. } = type_data {
10541066
let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
10551067
let forwarding_id = match id_option {
10561068
None => { // unknown_next_peer
@@ -1437,22 +1449,25 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
14371449
let mut fail_htlc_msgs = Vec::new();
14381450
for forward_info in pending_forwards.drain(..) {
14391451
match forward_info {
1440-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1441-
log_trace!(self, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", log_bytes!(forward_info.payment_hash.0), prev_short_channel_id, short_chan_id);
1452+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1453+
type_data: PendingForwardReceiveHTLCInfo::Forward {
1454+
onion_packet, ..
1455+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value }, } => {
1456+
log_trace!(self, "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);
14421457
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
14431458
short_channel_id: prev_short_channel_id,
14441459
htlc_id: prev_htlc_id,
1445-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1460+
incoming_packet_shared_secret: incoming_shared_secret,
14461461
});
1447-
match chan.get_mut().send_htlc(forward_info.amt_to_forward, forward_info.payment_hash, forward_info.outgoing_cltv_value, htlc_source.clone(), forward_info.onion_packet.unwrap()) {
1462+
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet) {
14481463
Err(e) => {
14491464
if let ChannelError::Ignore(msg) = e {
1450-
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(forward_info.payment_hash.0), msg);
1465+
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
14511466
} else {
14521467
panic!("Stated return value requirements in send_htlc() were not met");
14531468
}
14541469
let chan_update = self.get_channel_update(chan.get()).unwrap();
1455-
failed_forwards.push((htlc_source, forward_info.payment_hash, 0x1000 | 7, Some(chan_update)));
1470+
failed_forwards.push((htlc_source, payment_hash, 0x1000 | 7, Some(chan_update)));
14561471
continue;
14571472
},
14581473
Ok(update_add) => {
@@ -1471,6 +1486,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
14711486
}
14721487
}
14731488
},
1489+
HTLCForwardInfo::AddHTLC { .. } => {
1490+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
1491+
},
14741492
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
14751493
log_trace!(self, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
14761494
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
@@ -1550,21 +1568,26 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
15501568
} else {
15511569
for forward_info in pending_forwards.drain(..) {
15521570
match forward_info {
1553-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1571+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1572+
type_data: PendingForwardReceiveHTLCInfo::Receive { },
1573+
incoming_shared_secret, payment_hash, amt_to_forward, .. }, } => {
15541574
let prev_hop_data = HTLCPreviousHopData {
15551575
short_channel_id: prev_short_channel_id,
15561576
htlc_id: prev_htlc_id,
1557-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1577+
incoming_packet_shared_secret: incoming_shared_secret,
15581578
};
1559-
match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
1560-
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((forward_info.amt_to_forward, prev_hop_data)),
1561-
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(forward_info.amt_to_forward, prev_hop_data)]); },
1579+
match channel_state.claimable_htlcs.entry(payment_hash) {
1580+
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((amt_to_forward, prev_hop_data)),
1581+
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(amt_to_forward, prev_hop_data)]); },
15621582
};
15631583
new_events.push(events::Event::PaymentReceived {
1564-
payment_hash: forward_info.payment_hash,
1565-
amt: forward_info.amt_to_forward,
1584+
payment_hash: payment_hash,
1585+
amt: amt_to_forward,
15661586
});
15671587
},
1588+
HTLCForwardInfo::AddHTLC { .. } => {
1589+
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
1590+
},
15681591
HTLCForwardInfo::FailHTLC { .. } => {
15691592
panic!("Got pending fail of our own HTLC");
15701593
}
@@ -2379,7 +2402,10 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
23792402
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
23802403
}
23812404
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2382-
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
2405+
match channel_state.forward_htlcs.entry(match forward_info.type_data {
2406+
PendingForwardReceiveHTLCInfo::Forward { short_channel_id, .. } => short_channel_id,
2407+
PendingForwardReceiveHTLCInfo::Receive { .. } => 0,
2408+
}) {
23832409
hash_map::Entry::Occupied(mut entry) => {
23842410
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
23852411
},
@@ -3122,10 +3148,18 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
31223148

31233149
impl Writeable for PendingHTLCInfo {
31243150
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3125-
self.onion_packet.write(writer)?;
3151+
match &self.type_data {
3152+
&PendingForwardReceiveHTLCInfo::Forward { ref onion_packet, ref short_channel_id } => {
3153+
0u8.write(writer)?;
3154+
onion_packet.write(writer)?;
3155+
short_channel_id.write(writer)?;
3156+
},
3157+
&PendingForwardReceiveHTLCInfo::Receive { } => {
3158+
1u8.write(writer)?;
3159+
},
3160+
}
31263161
self.incoming_shared_secret.write(writer)?;
31273162
self.payment_hash.write(writer)?;
3128-
self.short_channel_id.write(writer)?;
31293163
self.amt_to_forward.write(writer)?;
31303164
self.outgoing_cltv_value.write(writer)?;
31313165
Ok(())
@@ -3135,10 +3169,16 @@ impl Writeable for PendingHTLCInfo {
31353169
impl Readable for PendingHTLCInfo {
31363170
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<PendingHTLCInfo, DecodeError> {
31373171
Ok(PendingHTLCInfo {
3138-
onion_packet: Readable::read(reader)?,
3172+
type_data: match Readable::read(reader)? {
3173+
0u8 => PendingForwardReceiveHTLCInfo::Forward {
3174+
onion_packet: Readable::read(reader)?,
3175+
short_channel_id: Readable::read(reader)?,
3176+
},
3177+
1u8 => PendingForwardReceiveHTLCInfo::Receive { },
3178+
_ => return Err(DecodeError::InvalidValue),
3179+
},
31393180
incoming_shared_secret: Readable::read(reader)?,
31403181
payment_hash: Readable::read(reader)?,
3141-
short_channel_id: Readable::read(reader)?,
31423182
amt_to_forward: Readable::read(reader)?,
31433183
outgoing_cltv_value: Readable::read(reader)?,
31443184
})

0 commit comments

Comments
 (0)