Skip to content

Commit f26e373

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 e8a8fd0 commit f26e373

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
@@ -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 PendingHTLCRouting {
73+
Forward {
74+
onion_packet: msgs::OnionPacket,
75+
short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
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+
routing: PendingHTLCRouting,
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
}
@@ -1005,9 +1013,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
10051013
// delay) once they've send us a commitment_signed!
10061014

10071015
PendingHTLCStatus::Forward(PendingHTLCInfo {
1008-
onion_packet: None,
1016+
routing: PendingHTLCRouting::Receive {},
10091017
payment_hash: msg.payment_hash.clone(),
1010-
short_channel_id: 0,
10111018
incoming_shared_secret: shared_secret,
10121019
amt_to_forward: next_hop_data.amt_to_forward,
10131020
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
@@ -1051,24 +1058,29 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
10511058
let short_channel_id = match next_hop_data.format {
10521059
msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
10531060
msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
1054-
msgs::OnionHopDataFormat::FinalNode => {
1061+
msgs::OnionHopDataFormat::FinalNode { .. } => {
10551062
return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
10561063
},
10571064
};
10581065

10591066
PendingHTLCStatus::Forward(PendingHTLCInfo {
1060-
onion_packet: Some(outgoing_packet),
1067+
routing: PendingHTLCRouting::Forward {
1068+
onion_packet: outgoing_packet,
1069+
short_channel_id: short_channel_id,
1070+
},
10611071
payment_hash: msg.payment_hash.clone(),
1062-
short_channel_id: short_channel_id,
10631072
incoming_shared_secret: shared_secret,
10641073
amt_to_forward: next_hop_data.amt_to_forward,
10651074
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
10661075
})
10671076
};
10681077

10691078
channel_state = Some(self.channel_state.lock().unwrap());
1070-
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1071-
if onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
1079+
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref routing, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1080+
// If short_channel_id is 0 here, we'll reject the HTLC as there cannot be a channel
1081+
// with a short_channel_id of 0. This is important as various things later assume
1082+
// short_channel_id is non-0 in any ::Forward.
1083+
if let &PendingHTLCRouting::Forward { ref short_channel_id, .. } = routing {
10721084
let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
10731085
let forwarding_id = match id_option {
10741086
None => { // unknown_next_peer
@@ -1450,22 +1462,25 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
14501462
let mut fail_htlc_msgs = Vec::new();
14511463
for forward_info in pending_forwards.drain(..) {
14521464
match forward_info {
1453-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1454-
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);
1465+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1466+
routing: PendingHTLCRouting::Forward {
1467+
onion_packet, ..
1468+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value }, } => {
1469+
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);
14551470
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
14561471
short_channel_id: prev_short_channel_id,
14571472
htlc_id: prev_htlc_id,
1458-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1473+
incoming_packet_shared_secret: incoming_shared_secret,
14591474
});
1460-
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()) {
1475+
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet) {
14611476
Err(e) => {
14621477
if let ChannelError::Ignore(msg) = e {
1463-
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(forward_info.payment_hash.0), msg);
1478+
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
14641479
} else {
14651480
panic!("Stated return value requirements in send_htlc() were not met");
14661481
}
14671482
let chan_update = self.get_channel_update(chan.get()).unwrap();
1468-
failed_forwards.push((htlc_source, forward_info.payment_hash, 0x1000 | 7, Some(chan_update)));
1483+
failed_forwards.push((htlc_source, payment_hash, 0x1000 | 7, Some(chan_update)));
14691484
continue;
14701485
},
14711486
Ok(update_add) => {
@@ -1484,6 +1499,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
14841499
}
14851500
}
14861501
},
1502+
HTLCForwardInfo::AddHTLC { .. } => {
1503+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
1504+
},
14871505
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
14881506
log_trace!(self, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
14891507
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
@@ -1561,21 +1579,26 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
15611579
} else {
15621580
for forward_info in pending_forwards.drain(..) {
15631581
match forward_info {
1564-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1582+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1583+
routing: PendingHTLCRouting::Receive { },
1584+
incoming_shared_secret, payment_hash, amt_to_forward, .. }, } => {
15651585
let prev_hop_data = HTLCPreviousHopData {
15661586
short_channel_id: prev_short_channel_id,
15671587
htlc_id: prev_htlc_id,
1568-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1588+
incoming_packet_shared_secret: incoming_shared_secret,
15691589
};
1570-
match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
1571-
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((forward_info.amt_to_forward, prev_hop_data)),
1572-
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(forward_info.amt_to_forward, prev_hop_data)]); },
1590+
match channel_state.claimable_htlcs.entry(payment_hash) {
1591+
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((amt_to_forward, prev_hop_data)),
1592+
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(amt_to_forward, prev_hop_data)]); },
15731593
};
15741594
new_events.push(events::Event::PaymentReceived {
1575-
payment_hash: forward_info.payment_hash,
1576-
amt: forward_info.amt_to_forward,
1595+
payment_hash: payment_hash,
1596+
amt: amt_to_forward,
15771597
});
15781598
},
1599+
HTLCForwardInfo::AddHTLC { .. } => {
1600+
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
1601+
},
15791602
HTLCForwardInfo::FailHTLC { .. } => {
15801603
panic!("Got pending fail of our own HTLC");
15811604
}
@@ -2388,7 +2411,10 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
23882411
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
23892412
}
23902413
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2391-
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
2414+
match channel_state.forward_htlcs.entry(match forward_info.routing {
2415+
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
2416+
PendingHTLCRouting::Receive { .. } => 0,
2417+
}) {
23922418
hash_map::Entry::Occupied(mut entry) => {
23932419
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
23942420
},
@@ -3066,10 +3092,18 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
30663092

30673093
impl Writeable for PendingHTLCInfo {
30683094
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3069-
self.onion_packet.write(writer)?;
3095+
match &self.routing {
3096+
&PendingHTLCRouting::Forward { ref onion_packet, ref short_channel_id } => {
3097+
0u8.write(writer)?;
3098+
onion_packet.write(writer)?;
3099+
short_channel_id.write(writer)?;
3100+
},
3101+
&PendingHTLCRouting::Receive { } => {
3102+
1u8.write(writer)?;
3103+
},
3104+
}
30703105
self.incoming_shared_secret.write(writer)?;
30713106
self.payment_hash.write(writer)?;
3072-
self.short_channel_id.write(writer)?;
30733107
self.amt_to_forward.write(writer)?;
30743108
self.outgoing_cltv_value.write(writer)?;
30753109
Ok(())
@@ -3079,10 +3113,16 @@ impl Writeable for PendingHTLCInfo {
30793113
impl Readable for PendingHTLCInfo {
30803114
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<PendingHTLCInfo, DecodeError> {
30813115
Ok(PendingHTLCInfo {
3082-
onion_packet: Readable::read(reader)?,
3116+
routing: match Readable::read(reader)? {
3117+
0u8 => PendingHTLCRouting::Forward {
3118+
onion_packet: Readable::read(reader)?,
3119+
short_channel_id: Readable::read(reader)?,
3120+
},
3121+
1u8 => PendingHTLCRouting::Receive { },
3122+
_ => return Err(DecodeError::InvalidValue),
3123+
},
30833124
incoming_shared_secret: Readable::read(reader)?,
30843125
payment_hash: Readable::read(reader)?,
3085-
short_channel_id: Readable::read(reader)?,
30863126
amt_to_forward: Readable::read(reader)?,
30873127
outgoing_cltv_value: Readable::read(reader)?,
30883128
})

0 commit comments

Comments
 (0)