Skip to content

Commit de71abf

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 6083956 commit de71abf

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
@@ -4496,68 +4496,58 @@ where
44964496
})
44974497
}
44984498

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

@@ -5849,15 +5839,14 @@ where
58495839
}
58505840
}
58515841

5852-
match self.construct_pending_htlc_status(
5853-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5854-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5842+
match self.get_pending_htlc_info(
5843+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5844+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58555845
) {
5856-
PendingHTLCStatus::Forward(htlc_forward) => {
5857-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5858-
},
5859-
PendingHTLCStatus::Fail(htlc_fail) => {
5846+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5847+
Err(inbound_err) => {
58605848
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5849+
let htlc_fail = self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err);
58615850
htlc_fails.push((htlc_fail, htlc_destination));
58625851
},
58635852
}

0 commit comments

Comments
 (0)