Skip to content

Commit 1edae58

Browse files
committed
lightning/refactor: split up construct_pending_htlc_status to get error
To be able to obtain the underlying error reason for the pending HTLC, break up the helper method into two parts. This also removes some unnecessary wrapping/unwrapping of messages in PendingHTLCStatus types.
1 parent ee817d5 commit 1edae58

File tree

1 file changed

+45
-66
lines changed

1 file changed

+45
-66
lines changed

lightning/src/ln/channelmanager.rs

+45-66
Original file line numberDiff line numberDiff line change
@@ -4489,84 +4489,65 @@ where
44894489
})
44904490
}
44914491

4492-
fn construct_pending_htlc_status<'a>(
4493-
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, shared_secret: [u8; 32],
4494-
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4495-
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4496-
) -> PendingHTLCStatus {
4497-
macro_rules! return_err {
4498-
($msg: expr, $reason: expr, $data: expr) => {
4499-
{
4500-
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4501-
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
4502-
if msg.blinding_point.is_some() {
4503-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(
4504-
msgs::UpdateFailMalformedHTLC {
4505-
channel_id: msg.channel_id,
4506-
htlc_id: msg.htlc_id,
4507-
sha256_of_onion: [0; 32],
4508-
failure_code: INVALID_ONION_BLINDING,
4509-
}
4510-
))
4511-
}
4512-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4513-
channel_id: msg.channel_id,
4514-
htlc_id: msg.htlc_id,
4515-
reason: HTLCFailReason::reason($reason.failure_code(), $data.to_vec())
4516-
.get_encrypted_failure_packet(&shared_secret, &None),
4517-
}));
4492+
fn construct_pending_htlc_fail_msg<'a>(&self, msg: &msgs::UpdateAddHTLC,
4493+
counterparty_node_id: &PublicKey, shared_secret: [u8; 32], inbound_err: InboundHTLCErr) -> HTLCFailureMsg {
4494+
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4495+
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", inbound_err.msg);
4496+
4497+
if msg.blinding_point.is_some() {
4498+
return HTLCFailureMsg::Malformed(
4499+
msgs::UpdateFailMalformedHTLC {
4500+
channel_id: msg.channel_id,
4501+
htlc_id: msg.htlc_id,
4502+
sha256_of_onion: [0; 32],
4503+
failure_code: INVALID_ONION_BLINDING,
45184504
}
4519-
}
4505+
)
45204506
}
4507+
return HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4508+
channel_id: msg.channel_id,
4509+
htlc_id: msg.htlc_id,
4510+
reason: HTLCFailReason::reason(inbound_err.reason.failure_code(), inbound_err.err_data.to_vec())
4511+
.get_encrypted_failure_packet(&shared_secret, &None),
4512+
});
4513+
}
4514+
4515+
fn get_pending_htlc_info<'a>(
4516+
&self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32],
4517+
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4518+
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4519+
) -> Result<PendingHTLCInfo, InboundHTLCErr> {
45214520
match decoded_hop {
45224521
onion_utils::Hop::Receive { .. } | onion_utils::Hop::BlindedReceive { .. } => {
45234522
// OUR PAYMENT!
4523+
// Note that we could obviously respond immediately with an update_fulfill_htlc
4524+
// message, however that would leak that we are the recipient of this payment, so
4525+
// instead we stay symmetric with the forwarding case, only responding (after a
4526+
// delay) once they've send us a commitment_signed!
45244527
let current_height: u32 = self.best_block.read().unwrap().height;
4525-
match create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
4528+
create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
45264529
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45274530
current_height)
4528-
{
4529-
Ok(info) => {
4530-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4531-
// message, however that would leak that we are the recipient of this payment, so
4532-
// instead we stay symmetric with the forwarding case, only responding (after a
4533-
// delay) once they've sent us a commitment_signed!
4534-
PendingHTLCStatus::Forward(info)
4535-
},
4536-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4537-
}
45384531
},
45394532
#[cfg(trampoline)]
45404533
onion_utils::Hop::TrampolineReceive { .. } | onion_utils::Hop::TrampolineBlindedReceive { .. } => {
45414534
// OUR PAYMENT!
4535+
// Note that we could obviously respond immediately with an update_fulfill_htlc
4536+
// message, however that would leak that we are the recipient of this payment, so
4537+
// instead we stay symmetric with the forwarding case, only responding (after a
4538+
// delay) once they've send us a commitment_signed!
45424539
let current_height: u32 = self.best_block.read().unwrap().height;
4543-
match create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
4540+
create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
45444541
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45454542
current_height)
4546-
{
4547-
Ok(info) => {
4548-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4549-
// message, however that would leak that we are the recipient of this payment, so
4550-
// instead we stay symmetric with the forwarding case, only responding (after a
4551-
// delay) once they've sent us a commitment_signed!
4552-
PendingHTLCStatus::Forward(info)
4553-
},
4554-
Err(InboundHTLCErr { err_code, err_data, msg }) => return_err!(msg, err_code, &err_data)
4555-
}
45564543
},
45574544
onion_utils::Hop::Forward { .. } | onion_utils::Hop::BlindedForward { .. } => {
4558-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4559-
Ok(info) => PendingHTLCStatus::Forward(info),
4560-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4561-
}
4545+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
45624546
},
45634547
#[cfg(trampoline)]
45644548
onion_utils::Hop::TrampolineForward { .. } | onion_utils::Hop::TrampolineBlindedForward { .. } => {
4565-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4566-
Ok(info) => PendingHTLCStatus::Forward(info),
4567-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4568-
}
4569-
}
4549+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
4550+
},
45704551
}
45714552
}
45724553

@@ -5867,16 +5848,14 @@ where
58675848
}
58685849
}
58695850

5870-
match self.construct_pending_htlc_status(
5871-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5872-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5851+
match self.get_pending_htlc_info(
5852+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5853+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58735854
) {
5874-
PendingHTLCStatus::Forward(htlc_forward) => {
5875-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5876-
},
5877-
PendingHTLCStatus::Fail(htlc_fail) => {
5855+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5856+
Err(inbound_err) => {
58785857
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5879-
htlc_fails.push((htlc_fail, htlc_destination));
5858+
htlc_fails.push((self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err), htlc_destination));
58805859
},
58815860
}
58825861
}

0 commit comments

Comments
 (0)