Skip to content

Commit 1652f33

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 73bb6e9 commit 1652f33

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 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
}
@@ -974,9 +982,8 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
974982
// delay) once they've send us a commitment_signed!
975983

976984
PendingHTLCStatus::Forward(PendingHTLCInfo {
977-
onion_packet: None,
985+
type_data: PendingForwardReceiveHTLCInfo::Receive {},
978986
payment_hash: msg.payment_hash.clone(),
979-
short_channel_id: 0,
980987
incoming_shared_secret: shared_secret,
981988
amt_to_forward: next_hop_data.amt_to_forward,
982989
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
@@ -1020,24 +1027,29 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
10201027
let short_channel_id = match next_hop_data.format {
10211028
msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
10221029
msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
1023-
msgs::OnionHopDataFormat::FinalNode => {
1030+
msgs::OnionHopDataFormat::FinalNode { .. } => {
10241031
return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
10251032
},
10261033
};
10271034

10281035
PendingHTLCStatus::Forward(PendingHTLCInfo {
1029-
onion_packet: Some(outgoing_packet),
1036+
type_data: PendingForwardReceiveHTLCInfo::Forward {
1037+
onion_packet: outgoing_packet,
1038+
short_channel_id: short_channel_id,
1039+
},
10301040
payment_hash: msg.payment_hash.clone(),
1031-
short_channel_id: short_channel_id,
10321041
incoming_shared_secret: shared_secret,
10331042
amt_to_forward: next_hop_data.amt_to_forward,
10341043
outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
10351044
})
10361045
};
10371046

10381047
channel_state = Some(self.channel_state.lock().unwrap());
1039-
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1040-
if onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
1048+
if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref type_data, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1049+
// If short_channel_id is 0 here, we'll reject them in the body here (which is
1050+
// important as various things later assume we are a ::Receive if short_channel_id is
1051+
// non-0.
1052+
if let &PendingForwardReceiveHTLCInfo::Forward { ref short_channel_id, .. } = type_data {
10411053
let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
10421054
let forwarding_id = match id_option {
10431055
None => { // unknown_next_peer
@@ -1406,22 +1418,25 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
14061418
let mut fail_htlc_msgs = Vec::new();
14071419
for forward_info in pending_forwards.drain(..) {
14081420
match forward_info {
1409-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1410-
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);
1421+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1422+
type_data: PendingForwardReceiveHTLCInfo::Forward {
1423+
onion_packet, ..
1424+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value }, } => {
1425+
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);
14111426
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
14121427
short_channel_id: prev_short_channel_id,
14131428
htlc_id: prev_htlc_id,
1414-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1429+
incoming_packet_shared_secret: incoming_shared_secret,
14151430
});
1416-
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()) {
1431+
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet) {
14171432
Err(e) => {
14181433
if let ChannelError::Ignore(msg) = e {
1419-
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(forward_info.payment_hash.0), msg);
1434+
log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
14201435
} else {
14211436
panic!("Stated return value requirements in send_htlc() were not met");
14221437
}
14231438
let chan_update = self.get_channel_update(chan.get()).unwrap();
1424-
failed_forwards.push((htlc_source, forward_info.payment_hash, 0x1000 | 7, Some(chan_update)));
1439+
failed_forwards.push((htlc_source, payment_hash, 0x1000 | 7, Some(chan_update)));
14251440
continue;
14261441
},
14271442
Ok(update_add) => {
@@ -1440,6 +1455,9 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
14401455
}
14411456
}
14421457
},
1458+
HTLCForwardInfo::AddHTLC { .. } => {
1459+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
1460+
},
14431461
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
14441462
log_trace!(self, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
14451463
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
@@ -1519,21 +1537,26 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
15191537
} else {
15201538
for forward_info in pending_forwards.drain(..) {
15211539
match forward_info {
1522-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1540+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1541+
type_data: PendingForwardReceiveHTLCInfo::Receive { },
1542+
incoming_shared_secret, payment_hash, amt_to_forward, .. }, } => {
15231543
let prev_hop_data = HTLCPreviousHopData {
15241544
short_channel_id: prev_short_channel_id,
15251545
htlc_id: prev_htlc_id,
1526-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1546+
incoming_packet_shared_secret: incoming_shared_secret,
15271547
};
1528-
match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
1529-
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((forward_info.amt_to_forward, prev_hop_data)),
1530-
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(forward_info.amt_to_forward, prev_hop_data)]); },
1548+
match channel_state.claimable_htlcs.entry(payment_hash) {
1549+
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((amt_to_forward, prev_hop_data)),
1550+
hash_map::Entry::Vacant(entry) => { entry.insert(vec![(amt_to_forward, prev_hop_data)]); },
15311551
};
15321552
new_events.push(events::Event::PaymentReceived {
1533-
payment_hash: forward_info.payment_hash,
1534-
amt: forward_info.amt_to_forward,
1553+
payment_hash: payment_hash,
1554+
amt: amt_to_forward,
15351555
});
15361556
},
1557+
HTLCForwardInfo::AddHTLC { .. } => {
1558+
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
1559+
},
15371560
HTLCForwardInfo::FailHTLC { .. } => {
15381561
panic!("Got pending fail of our own HTLC");
15391562
}
@@ -2335,7 +2358,10 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
23352358
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
23362359
}
23372360
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2338-
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
2361+
match channel_state.forward_htlcs.entry(match forward_info.type_data {
2362+
PendingForwardReceiveHTLCInfo::Forward { short_channel_id, .. } => short_channel_id,
2363+
PendingForwardReceiveHTLCInfo::Receive { .. } => 0,
2364+
}) {
23392365
hash_map::Entry::Occupied(mut entry) => {
23402366
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
23412367
},
@@ -3044,10 +3070,18 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
30443070

30453071
impl Writeable for PendingHTLCInfo {
30463072
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3047-
self.onion_packet.write(writer)?;
3073+
match &self.type_data {
3074+
&PendingForwardReceiveHTLCInfo::Forward { ref onion_packet, ref short_channel_id } => {
3075+
0u8.write(writer)?;
3076+
onion_packet.write(writer)?;
3077+
short_channel_id.write(writer)?;
3078+
},
3079+
&PendingForwardReceiveHTLCInfo::Receive { } => {
3080+
1u8.write(writer)?;
3081+
},
3082+
}
30483083
self.incoming_shared_secret.write(writer)?;
30493084
self.payment_hash.write(writer)?;
3050-
self.short_channel_id.write(writer)?;
30513085
self.amt_to_forward.write(writer)?;
30523086
self.outgoing_cltv_value.write(writer)?;
30533087
Ok(())
@@ -3057,10 +3091,16 @@ impl Writeable for PendingHTLCInfo {
30573091
impl<R: ::std::io::Read> Readable<R> for PendingHTLCInfo {
30583092
fn read(reader: &mut R) -> Result<PendingHTLCInfo, DecodeError> {
30593093
Ok(PendingHTLCInfo {
3060-
onion_packet: Readable::read(reader)?,
3094+
type_data: match Readable::read(reader)? {
3095+
0u8 => PendingForwardReceiveHTLCInfo::Forward {
3096+
onion_packet: Readable::read(reader)?,
3097+
short_channel_id: Readable::read(reader)?,
3098+
},
3099+
1u8 => PendingForwardReceiveHTLCInfo::Receive { },
3100+
_ => return Err(DecodeError::InvalidValue),
3101+
},
30613102
incoming_shared_secret: Readable::read(reader)?,
30623103
payment_hash: Readable::read(reader)?,
3063-
short_channel_id: Readable::read(reader)?,
30643104
amt_to_forward: Readable::read(reader)?,
30653105
outgoing_cltv_value: Readable::read(reader)?,
30663106
})

0 commit comments

Comments
 (0)