Skip to content

Commit 63b28eb

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 07bb041 commit 63b28eb

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,20 @@ use std::time::Duration;
6666
// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
6767
// our payment, which we can use to decode errors or inform the user that the payment was sent.
6868

69+
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
70+
enum PendingForwardReceiveHTLCInfo {
71+
Forward {
72+
onion_packet: msgs::OnionPacket,
73+
short_channel_id: u64, // This should be NonZero<u64> eventually
74+
},
75+
Receive {},
76+
}
77+
6978
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
7079
pub(super) struct PendingHTLCInfo {
71-
onion_packet: Option<msgs::OnionPacket>,
80+
type_data: PendingForwardReceiveHTLCInfo,
7281
incoming_shared_secret: [u8; 32],
7382
payment_hash: PaymentHash,
74-
short_channel_id: u64,
7583
pub(super) amt_to_forward: u64,
7684
pub(super) outgoing_cltv_value: u32,
7785
}
@@ -991,9 +999,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
991999
// delay) once they've send us a commitment_signed!
9921000

9931001
PendingHTLCStatus::Forward(PendingHTLCInfo {
994-
onion_packet: None,
1002+
type_data: PendingForwardReceiveHTLCInfo::Receive {},
9951003
payment_hash: msg.payment_hash.clone(),
996-
short_channel_id: 0,
9971004
incoming_shared_secret: shared_secret,
9981005
amt_to_forward: next_hop_data.amt_to_forward,
9991006
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
@@ -1035,24 +1042,29 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
10351042
let short_channel_id = match next_hop_data.format {
10361043
msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
10371044
msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
1038-
msgs::OnionHopDataFormat::FinalNode => {
1045+
msgs::OnionHopDataFormat::FinalNode { .. } => {
10391046
return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
10401047
},
10411048
};
10421049

10431050
PendingHTLCStatus::Forward(PendingHTLCInfo {
1044-
onion_packet: Some(outgoing_packet),
1051+
type_data: PendingForwardReceiveHTLCInfo::Forward {
1052+
onion_packet: outgoing_packet,
1053+
short_channel_id: short_channel_id,
1054+
},
10451055
payment_hash: msg.payment_hash.clone(),
1046-
short_channel_id: short_channel_id,
10471056
incoming_shared_secret: shared_secret,
10481057
amt_to_forward: next_hop_data.amt_to_forward,
10491058
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
10501059
})
10511060
};
10521061

10531062
channel_state = Some(self.channel_state.lock().unwrap());
1054-
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1055-
if onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
1063+
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref type_data, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1064+
// If short_channel_id is 0 here, we'll reject them in the body here (which is
1065+
// important as various things later assume we are a ::Receive if short_channel_id is
1066+
// non-0.
1067+
if let &PendingForwardReceiveHTLCInfo::Forward { ref short_channel_id, .. } = type_data {
10561068
let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
10571069
let forwarding_id = match id_option {
10581070
None => { // unknown_next_peer
@@ -1416,22 +1428,25 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
14161428
let mut fail_htlc_msgs = Vec::new();
14171429
for forward_info in pending_forwards.drain(..) {
14181430
match forward_info {
1419-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1420-
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);
1431+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1432+
type_data: PendingForwardReceiveHTLCInfo::Forward {
1433+
onion_packet, ..
1434+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value }, } => {
1435+
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);
14211436
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
14221437
short_channel_id: prev_short_channel_id,
14231438
htlc_id: prev_htlc_id,
1424-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1439+
incoming_packet_shared_secret: incoming_shared_secret,
14251440
});
1426-
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()) {
1441+
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet) {
14271442
Err(e) => {
14281443
if let ChannelError::Ignore(msg) = e {
1429-
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(forward_info.payment_hash.0), msg);
1444+
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
14301445
} else {
14311446
panic!("Stated return value requirements in send_htlc() were not met");
14321447
}
14331448
let chan_update = self.get_channel_update(chan.get()).unwrap();
1434-
failed_forwards.push((htlc_source, forward_info.payment_hash, 0x1000 | 7, Some(chan_update)));
1449+
failed_forwards.push((htlc_source, payment_hash, 0x1000 | 7, Some(chan_update)));
14351450
continue;
14361451
},
14371452
Ok(update_add) => {
@@ -1450,6 +1465,9 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
14501465
}
14511466
}
14521467
},
1468+
HTLCForwardInfo::AddHTLC { .. } => {
1469+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
1470+
},
14531471
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
14541472
log_trace!(self, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
14551473
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
@@ -1529,21 +1547,26 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
15291547
} else {
15301548
for forward_info in pending_forwards.drain(..) {
15311549
match forward_info {
1532-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1550+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1551+
type_data: PendingForwardReceiveHTLCInfo::Receive { },
1552+
incoming_shared_secret, payment_hash, amt_to_forward, .. }, } => {
15331553
let prev_hop_data = HTLCPreviousHopData {
15341554
short_channel_id: prev_short_channel_id,
15351555
htlc_id: prev_htlc_id,
1536-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1556+
incoming_packet_shared_secret: incoming_shared_secret,
15371557
};
1538-
match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
1539-
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((forward_info.amt_to_forward, prev_hop_data)),
1540-
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(forward_info.amt_to_forward, prev_hop_data)]); },
1558+
match channel_state.claimable_htlcs.entry(payment_hash) {
1559+
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((amt_to_forward, prev_hop_data)),
1560+
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(amt_to_forward, prev_hop_data)]); },
15411561
};
15421562
new_events.push(events::Event::PaymentReceived {
1543-
payment_hash: forward_info.payment_hash,
1544-
amt: forward_info.amt_to_forward,
1563+
payment_hash: payment_hash,
1564+
amt: amt_to_forward,
15451565
});
15461566
},
1567+
HTLCForwardInfo::AddHTLC { .. } => {
1568+
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
1569+
},
15471570
HTLCForwardInfo::FailHTLC { .. } => {
15481571
panic!("Got pending fail of our own HTLC");
15491572
}
@@ -2343,7 +2366,10 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
23432366
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
23442367
}
23452368
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2346-
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
2369+
match channel_state.forward_htlcs.entry(match forward_info.type_data {
2370+
PendingForwardReceiveHTLCInfo::Forward { short_channel_id, .. } => short_channel_id,
2371+
PendingForwardReceiveHTLCInfo::Receive { .. } => 0,
2372+
}) {
23472373
hash_map::Entry::Occupied(mut entry) => {
23482374
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
23492375
},
@@ -3047,10 +3073,18 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
30473073

30483074
impl Writeable for PendingHTLCInfo {
30493075
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3050-
self.onion_packet.write(writer)?;
3076+
match &self.type_data {
3077+
&PendingForwardReceiveHTLCInfo::Forward { ref onion_packet, ref short_channel_id } => {
3078+
0u8.write(writer)?;
3079+
onion_packet.write(writer)?;
3080+
short_channel_id.write(writer)?;
3081+
},
3082+
&PendingForwardReceiveHTLCInfo::Receive { } => {
3083+
1u8.write(writer)?;
3084+
},
3085+
}
30513086
self.incoming_shared_secret.write(writer)?;
30523087
self.payment_hash.write(writer)?;
3053-
self.short_channel_id.write(writer)?;
30543088
self.amt_to_forward.write(writer)?;
30553089
self.outgoing_cltv_value.write(writer)?;
30563090
Ok(())
@@ -3060,10 +3094,16 @@ impl Writeable for PendingHTLCInfo {
30603094
impl<R: ::std::io::Read> Readable<R> for PendingHTLCInfo {
30613095
fn read(reader: &mut R) -> Result<PendingHTLCInfo, DecodeError> {
30623096
Ok(PendingHTLCInfo {
3063-
onion_packet: Readable::read(reader)?,
3097+
type_data: match Readable::read(reader)? {
3098+
0u8 => PendingForwardReceiveHTLCInfo::Forward {
3099+
onion_packet: Readable::read(reader)?,
3100+
short_channel_id: Readable::read(reader)?,
3101+
},
3102+
1u8 => PendingForwardReceiveHTLCInfo::Receive { },
3103+
_ => return Err(DecodeError::InvalidValue),
3104+
},
30643105
incoming_shared_secret: Readable::read(reader)?,
30653106
payment_hash: Readable::read(reader)?,
3066-
short_channel_id: Readable::read(reader)?,
30673107
amt_to_forward: Readable::read(reader)?,
30683108
outgoing_cltv_value: Readable::read(reader)?,
30693109
})

0 commit comments

Comments
 (0)