Skip to content

Commit 5179d8a

Browse files
committed
ln/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 8315a95 commit 5179d8a

File tree

1 file changed

+45
-56
lines changed

1 file changed

+45
-56
lines changed

lightning/src/ln/channelmanager.rs

+45-56
Original file line numberDiff line numberDiff line change
@@ -4495,68 +4495,58 @@ where
44954495
})
44964496
}
44974497

4498-
fn construct_pending_htlc_status<'a>(
4499-
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, shared_secret: [u8; 32],
4500-
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4501-
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4502-
) -> PendingHTLCStatus {
4503-
macro_rules! return_err {
4504-
($msg: expr, $reason: expr, $data: expr) => {
4505-
{
4506-
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4507-
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
4508-
if msg.blinding_point.is_some() {
4509-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(
4510-
msgs::UpdateFailMalformedHTLC {
4511-
channel_id: msg.channel_id,
4512-
htlc_id: msg.htlc_id,
4513-
sha256_of_onion: [0; 32],
4514-
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
4515-
}
4516-
))
4517-
}
4518-
let failure = HTLCFailReason::reason($reason, $data.to_vec())
4519-
.get_encrypted_failure_packet(&shared_secret, &None);
4520-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4521-
channel_id: msg.channel_id,
4522-
htlc_id: msg.htlc_id,
4523-
reason: failure.data,
4524-
attribution_data: failure.attribution_data,
4525-
}));
4498+
fn construct_pending_htlc_fail_msg<'a>(
4499+
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey,
4500+
shared_secret: [u8; 32], inbound_err: InboundHTLCErr
4501+
) -> HTLCFailureMsg {
4502+
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4503+
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", inbound_err.msg);
4504+
4505+
if msg.blinding_point.is_some() {
4506+
return HTLCFailureMsg::Malformed(
4507+
msgs::UpdateFailMalformedHTLC {
4508+
channel_id: msg.channel_id,
4509+
htlc_id: msg.htlc_id,
4510+
sha256_of_onion: [0; 32],
4511+
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
45264512
}
4527-
}
4513+
)
45284514
}
4515+
4516+
let failure = HTLCFailReason::reason(inbound_err.reason, inbound_err.err_data.to_vec())
4517+
.get_encrypted_failure_packet(&shared_secret, &None);
4518+
return HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4519+
channel_id: msg.channel_id,
4520+
htlc_id: msg.htlc_id,
4521+
reason: failure.data,
4522+
attribution_data: failure.attribution_data,
4523+
});
4524+
}
4525+
4526+
fn get_pending_htlc_info<'a>(
4527+
&self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32],
4528+
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4529+
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4530+
) -> Result<PendingHTLCInfo, InboundHTLCErr> {
45294531
match decoded_hop {
45304532
onion_utils::Hop::Receive { .. } | onion_utils::Hop::BlindedReceive { .. } |
45314533
onion_utils::Hop::TrampolineReceive { .. } | onion_utils::Hop::TrampolineBlindedReceive { .. } => {
45324534
// 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!
45334539
let current_height: u32 = self.best_block.read().unwrap().height;
4534-
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,
45354541
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45364542
current_height)
4537-
{
4538-
Ok(info) => {
4539-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4540-
// message, however that would leak that we are the recipient of this payment, so
4541-
// instead we stay symmetric with the forwarding case, only responding (after a
4542-
// delay) once they've sent us a commitment_signed!
4543-
PendingHTLCStatus::Forward(info)
4544-
},
4545-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason , &err_data)
4546-
}
45474543
},
45484544
onion_utils::Hop::Forward { .. } | onion_utils::Hop::BlindedForward { .. } => {
4549-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4550-
Ok(info) => PendingHTLCStatus::Forward(info),
4551-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4552-
}
4545+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
45534546
},
45544547
onion_utils::Hop::TrampolineForward { .. } | onion_utils::Hop::TrampolineBlindedForward { .. } => {
4555-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4556-
Ok(info) => PendingHTLCStatus::Forward(info),
4557-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4558-
}
4559-
}
4548+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
4549+
},
45604550
}
45614551
}
45624552

@@ -5848,15 +5838,14 @@ where
58485838
}
58495839
}
58505840

5851-
match self.construct_pending_htlc_status(
5852-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5853-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5841+
match self.get_pending_htlc_info(
5842+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5843+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58545844
) {
5855-
PendingHTLCStatus::Forward(htlc_forward) => {
5856-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5857-
},
5858-
PendingHTLCStatus::Fail(htlc_fail) => {
5845+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5846+
Err(inbound_err) => {
58595847
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5848+
let htlc_fail = self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err);
58605849
htlc_fails.push((htlc_fail, htlc_destination));
58615850
},
58625851
}

0 commit comments

Comments
 (0)