Skip to content

Commit ae178c9

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 7bbb177 commit ae178c9

File tree

2 files changed

+48
-69
lines changed

2 files changed

+48
-69
lines changed

lightning/src/ln/async_payments_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn invalid_keysend_payment_secret() {
194194
nodes[1].node.get_our_node_id(),
195195
&updates_1_0.update_fail_htlcs[0],
196196
);
197-
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
197+
commitment_signed_dance!(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
198198
expect_payment_failed_conditions(
199199
&nodes[0],
200200
payment_hash,

lightning/src/ln/channelmanager.rs

+47-68
Original file line numberDiff line numberDiff line change
@@ -4484,85 +4484,67 @@ where
44844484
})
44854485
}
44864486

4487-
fn construct_pending_htlc_status<'a>(
4488-
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, shared_secret: [u8; 32],
4489-
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4490-
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4491-
) -> PendingHTLCStatus {
4492-
macro_rules! return_err {
4493-
($msg: expr, $reason: expr, $data: expr) => {
4494-
{
4495-
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4496-
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
4497-
if msg.blinding_point.is_some() {
4498-
return PendingHTLCStatus::Fail(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: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
4504-
}
4505-
))
4487+
fn construct_pending_htlc_fail_msg<'a>(&self, msg: &msgs::UpdateAddHTLC,
4488+
counterparty_node_id: &PublicKey, shared_secret: [u8; 32], inbound_err: InboundHTLCErr) -> HTLCFailureMsg {
4489+
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4490+
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", inbound_err.msg);
4491+
4492+
if msg.blinding_point.is_some() {
4493+
return HTLCFailureMsg::Malformed(
4494+
msgs::UpdateFailMalformedHTLC {
4495+
channel_id: msg.channel_id,
4496+
htlc_id: msg.htlc_id,
4497+
sha256_of_onion: [0; 32],
4498+
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
45064499
}
4507-
let failure = HTLCFailReason::reason($reason, $data.to_vec())
4508-
.get_encrypted_failure_packet(&shared_secret, &None);
4509-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4510-
channel_id: msg.channel_id,
4511-
htlc_id: msg.htlc_id,
4512-
reason: failure.data,
4513-
}));
4514-
}
4515-
}
4500+
)
45164501
}
4502+
4503+
let failure = HTLCFailReason::reason(inbound_err.reason, inbound_err.err_data.to_vec())
4504+
.get_encrypted_failure_packet(&shared_secret, &None);
4505+
return HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4506+
channel_id: msg.channel_id,
4507+
htlc_id: msg.htlc_id,
4508+
reason: failure.data,
4509+
});
4510+
}
4511+
4512+
fn get_pending_htlc_info<'a>(
4513+
&self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32],
4514+
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4515+
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4516+
) -> Result<PendingHTLCInfo, InboundHTLCErr> {
45174517
match decoded_hop {
45184518
onion_utils::Hop::Receive { .. } | onion_utils::Hop::BlindedReceive { .. } => {
45194519
// OUR PAYMENT!
4520+
// Note that we could obviously respond immediately with an update_fulfill_htlc
4521+
// message, however that would leak that we are the recipient of this payment, so
4522+
// instead we stay symmetric with the forwarding case, only responding (after a
4523+
// delay) once they've send us a commitment_signed!
45204524
let current_height: u32 = self.best_block.read().unwrap().height;
4521-
match create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
4525+
create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
45224526
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45234527
current_height)
4524-
{
4525-
Ok(info) => {
4526-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4527-
// message, however that would leak that we are the recipient of this payment, so
4528-
// instead we stay symmetric with the forwarding case, only responding (after a
4529-
// delay) once they've sent us a commitment_signed!
4530-
PendingHTLCStatus::Forward(info)
4531-
},
4532-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4533-
}
45344528
},
45354529
#[cfg(trampoline)]
45364530
onion_utils::Hop::TrampolineReceive { .. } | onion_utils::Hop::TrampolineBlindedReceive { .. } => {
45374531
// 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!
45384536
let current_height: u32 = self.best_block.read().unwrap().height;
4539-
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,
45404538
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45414539
current_height)
4542-
{
4543-
Ok(info) => {
4544-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4545-
// message, however that would leak that we are the recipient of this payment, so
4546-
// instead we stay symmetric with the forwarding case, only responding (after a
4547-
// delay) once they've sent us a commitment_signed!
4548-
PendingHTLCStatus::Forward(info)
4549-
},
4550-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason , &err_data)
4551-
}
45524540
},
45534541
onion_utils::Hop::Forward { .. } | onion_utils::Hop::BlindedForward { .. } => {
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-
}
4542+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
45584543
},
45594544
#[cfg(trampoline)]
45604545
onion_utils::Hop::TrampolineForward { .. } | onion_utils::Hop::TrampolineBlindedForward { .. } => {
4561-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4562-
Ok(info) => PendingHTLCStatus::Forward(info),
4563-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4564-
}
4565-
}
4546+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
4547+
},
45664548
}
45674549
}
45684550

@@ -5863,16 +5845,14 @@ where
58635845
}
58645846
}
58655847

5866-
match self.construct_pending_htlc_status(
5867-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5868-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5848+
match self.get_pending_htlc_info(
5849+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5850+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58695851
) {
5870-
PendingHTLCStatus::Forward(htlc_forward) => {
5871-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5872-
},
5873-
PendingHTLCStatus::Fail(htlc_fail) => {
5852+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5853+
Err(inbound_err) => {
58745854
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5875-
htlc_fails.push((htlc_fail, htlc_destination));
5855+
htlc_fails.push((self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err), htlc_destination));
58765856
},
58775857
}
58785858
}
@@ -11825,7 +11805,6 @@ where
1182511805
payment.htlcs.retain(|htlc| {
1182611806
// If height is approaching the number of blocks we think it takes us to get
1182711807
// our commitment transaction confirmed before the HTLC expires, plus the
11828-
// number of blocks we generally consider it to take to do a commitment update,
1182911808
// just give up on it and fail the HTLC.
1183011809
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
1183111810
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();

0 commit comments

Comments
 (0)