Skip to content

Commit c1ade72

Browse files
committed
f: DRY it up somewhat
1 parent 1484fee commit c1ade72

File tree

2 files changed

+26
-65
lines changed

2 files changed

+26
-65
lines changed

lightning/src/ln/channelmanager.rs

+16-30
Original file line numberDiff line numberDiff line change
@@ -5884,36 +5884,22 @@ where
58845884
failed_payment!(err_msg, err_code, Vec::new(), Some(phantom_shared_secret));
58855885
},
58865886
};
5887-
match next_hop {
5888-
onion_utils::Hop::Receive(hop_data) => {
5889-
let current_height: u32 = self.best_block.read().unwrap().height;
5890-
match create_recv_pending_htlc_info(msgs::InboundOnionPayload::Receive(hop_data),
5891-
incoming_shared_secret, payment_hash, outgoing_amt_msat,
5892-
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
5893-
current_height)
5894-
{
5895-
Ok(info) => phantom_receives.push((
5896-
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
5897-
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
5898-
)),
5899-
Err(InboundHTLCErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
5900-
}
5901-
},
5902-
onion_utils::Hop::BlindedReceive(hop_data) => {
5903-
let current_height: u32 = self.best_block.read().unwrap().height;
5904-
match create_recv_pending_htlc_info(msgs::InboundOnionPayload::BlindedReceive(hop_data),
5905-
incoming_shared_secret, payment_hash, outgoing_amt_msat,
5906-
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
5907-
current_height)
5908-
{
5909-
Ok(info) => phantom_receives.push((
5910-
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
5911-
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
5912-
)),
5913-
Err(InboundHTLCErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
5914-
}
5915-
},
5916-
_ => panic!(),
5887+
let inbound_onion_payload = match next_hop {
5888+
onion_utils::Hop::Receive(hop_data) => msgs::InboundOnionPayload::Receive(hop_data),
5889+
onion_utils::Hop::BlindedReceive(hop_data) => msgs::InboundOnionPayload::BlindedReceive(hop_data),
5890+
_ => panic!()
5891+
};
5892+
let current_height: u32 = self.best_block.read().unwrap().height;
5893+
match create_recv_pending_htlc_info(inbound_onion_payload,
5894+
incoming_shared_secret, payment_hash, outgoing_amt_msat,
5895+
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
5896+
current_height)
5897+
{
5898+
Ok(info) => phantom_receives.push((
5899+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
5900+
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
5901+
)),
5902+
Err(InboundHTLCErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
59175903
}
59185904
} else {
59195905
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);

lightning/src/ln/onion_payment.rs

+10-35
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ where
296296
InboundHTLCErr { msg, err_code, err_data }
297297
})?;
298298
Ok(match hop {
299-
onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
299+
onion_utils::Hop::Forward { next_hop_hmac, new_packet_bytes, .. } | onion_utils::Hop::BlindedForward { next_hop_hmac, new_packet_bytes, .. } => {
300300
let NextPacketDetails {
301301
next_packet_pubkey, outgoing_amt_msat: _, outgoing_scid: _, outgoing_cltv_value
302302
} = match next_packet_details_opt {
@@ -310,7 +310,7 @@ where
310310
};
311311

312312
if let Err((err_msg, code)) = check_incoming_htlc_cltv(
313-
cur_height, outgoing_cltv_value, msg.cltv_expiry
313+
cur_height, outgoing_cltv_value, msg.cltv_expiry,
314314
) {
315315
return Err(InboundHTLCErr {
316316
msg: err_msg,
@@ -322,52 +322,27 @@ where
322322
// TODO: If this is potentially a phantom payment we should decode the phantom payment
323323
// onion here and check it.
324324

325-
create_fwd_pending_htlc_info(
326-
msg, msgs::InboundOnionPayload::Forward(next_hop_data), next_hop_hmac, new_packet_bytes, shared_secret,
327-
Some(next_packet_pubkey)
328-
)?
329-
},
330-
onion_utils::Hop::BlindedForward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
331-
let NextPacketDetails {
332-
next_packet_pubkey, outgoing_amt_msat: _, outgoing_scid: _, outgoing_cltv_value
333-
} = match next_packet_details_opt {
334-
Some(next_packet_details) => next_packet_details,
335-
// Forward should always include the next hop details
336-
None => return Err(InboundHTLCErr {
337-
msg: "Failed to decode update add htlc onion",
338-
err_code: 0x4000 | 22,
339-
err_data: Vec::new(),
340-
}),
325+
let inbound_onion_payload = match hop {
326+
onion_utils::Hop::Forward { next_hop_data, .. } => msgs::InboundOnionPayload::Forward(next_hop_data),
327+
onion_utils::Hop::BlindedForward { next_hop_data, .. } => msgs::InboundOnionPayload::BlindedForward(next_hop_data),
328+
_ => unreachable!()
341329
};
342330

343-
if let Err((err_msg, code)) = check_incoming_htlc_cltv(
344-
cur_height, outgoing_cltv_value, msg.cltv_expiry
345-
) {
346-
return Err(InboundHTLCErr {
347-
msg: err_msg,
348-
err_code: code,
349-
err_data: Vec::new(),
350-
});
351-
}
352-
353-
// TODO: If this is potentially a phantom payment we should decode the phantom payment
354-
// onion here and check it.
355-
356331
create_fwd_pending_htlc_info(
357-
msg, msgs::InboundOnionPayload::BlindedForward(next_hop_data), next_hop_hmac, new_packet_bytes, shared_secret,
358-
Some(next_packet_pubkey)
332+
msg, inbound_onion_payload, next_hop_hmac, new_packet_bytes, shared_secret,
333+
Some(next_packet_pubkey),
359334
)?
360335
},
361336
onion_utils::Hop::Receive(received_data) => {
362337
create_recv_pending_htlc_info(
363338
msgs::InboundOnionPayload::Receive(received_data), shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry,
364-
None, allow_skimmed_fees, msg.skimmed_fee_msat, cur_height
339+
None, allow_skimmed_fees, msg.skimmed_fee_msat, cur_height,
365340
)?
366341
},
367342
onion_utils::Hop::BlindedReceive(received_data) => {
368343
create_recv_pending_htlc_info(
369344
msgs::InboundOnionPayload::BlindedReceive(received_data), shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry,
370-
None, allow_skimmed_fees, msg.skimmed_fee_msat, cur_height
345+
None, allow_skimmed_fees, msg.skimmed_fee_msat, cur_height,
371346
)?
372347
}
373348
})

0 commit comments

Comments
 (0)