Skip to content

Commit 5fd2cea

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 21c9858 commit 5fd2cea

File tree

1 file changed

+43
-58
lines changed

1 file changed

+43
-58
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,68 +4494,56 @@ where
44944494
})
44954495
}
44964496

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

@@ -5847,16 +5835,14 @@ where
58475835
}
58485836
}
58495837

5850-
match self.construct_pending_htlc_status(
5851-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5852-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5838+
match self.get_pending_htlc_info(
5839+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5840+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58535841
) {
5854-
PendingHTLCStatus::Forward(htlc_forward) => {
5855-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5856-
},
5857-
PendingHTLCStatus::Fail(htlc_fail) => {
5842+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5843+
Err(inbound_err) => {
58585844
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5859-
htlc_fails.push((htlc_fail, htlc_destination));
5845+
htlc_fails.push((self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err), htlc_destination));
58605846
},
58615847
}
58625848
}
@@ -11827,7 +11813,6 @@ where
1182711813
payment.htlcs.retain(|htlc| {
1182811814
// If height is approaching the number of blocks we think it takes us to get
1182911815
// our commitment transaction confirmed before the HTLC expires, plus the
11830-
// number of blocks we generally consider it to take to do a commitment update,
1183111816
// just give up on it and fail the HTLC.
1183211817
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
1183311818
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();

0 commit comments

Comments
 (0)