Skip to content

Commit 2400fbe

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 4eb9dde commit 2400fbe

File tree

1 file changed

+45
-67
lines changed

1 file changed

+45
-67
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4484,84 +4484,65 @@ 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-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4508-
channel_id: msg.channel_id,
4509-
htlc_id: msg.htlc_id,
4510-
reason: HTLCFailReason::reason($reason, $data.to_vec())
4511-
.get_encrypted_failure_packet(&shared_secret, &None),
4512-
}));
4513-
}
4514-
}
4500+
)
45154501
}
4502+
return HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4503+
channel_id: msg.channel_id,
4504+
htlc_id: msg.htlc_id,
4505+
reason: HTLCFailReason::reason(inbound_err.reason, inbound_err.err_data.to_vec())
4506+
.get_encrypted_failure_packet(&shared_secret, &None),
4507+
});
4508+
}
4509+
4510+
fn get_pending_htlc_info<'a>(
4511+
&self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32],
4512+
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4513+
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4514+
) -> Result<PendingHTLCInfo, InboundHTLCErr> {
45164515
match decoded_hop {
45174516
onion_utils::Hop::Receive { .. } | onion_utils::Hop::BlindedReceive { .. } => {
45184517
// OUR PAYMENT!
4518+
// Note that we could obviously respond immediately with an update_fulfill_htlc
4519+
// message, however that would leak that we are the recipient of this payment, so
4520+
// instead we stay symmetric with the forwarding case, only responding (after a
4521+
// delay) once they've send us a commitment_signed!
45194522
let current_height: u32 = self.best_block.read().unwrap().height;
4520-
match create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
4523+
create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
45214524
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45224525
current_height)
4523-
{
4524-
Ok(info) => {
4525-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4526-
// message, however that would leak that we are the recipient of this payment, so
4527-
// instead we stay symmetric with the forwarding case, only responding (after a
4528-
// delay) once they've sent us a commitment_signed!
4529-
PendingHTLCStatus::Forward(info)
4530-
},
4531-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4532-
}
45334526
},
45344527
#[cfg(trampoline)]
45354528
onion_utils::Hop::TrampolineReceive { .. } | onion_utils::Hop::TrampolineBlindedReceive { .. } => {
45364529
// OUR PAYMENT!
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 send us a commitment_signed!
45374534
let current_height: u32 = self.best_block.read().unwrap().height;
4538-
match create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
4535+
create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
45394536
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45404537
current_height)
4541-
{
4542-
Ok(info) => {
4543-
// Note that we could obviously respond immediately with an update_fulfill_htlc
4544-
// message, however that would leak that we are the recipient of this payment, so
4545-
// instead we stay symmetric with the forwarding case, only responding (after a
4546-
// delay) once they've sent us a commitment_signed!
4547-
PendingHTLCStatus::Forward(info)
4548-
},
4549-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason , &err_data)
4550-
}
45514538
},
45524539
onion_utils::Hop::Forward { .. } | onion_utils::Hop::BlindedForward { .. } => {
4553-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4554-
Ok(info) => PendingHTLCStatus::Forward(info),
4555-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4556-
}
4540+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
45574541
},
45584542
#[cfg(trampoline)]
45594543
onion_utils::Hop::TrampolineForward { .. } | onion_utils::Hop::TrampolineBlindedForward { .. } => {
4560-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4561-
Ok(info) => PendingHTLCStatus::Forward(info),
4562-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4563-
}
4564-
}
4544+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
4545+
},
45654546
}
45664547
}
45674548

@@ -5862,16 +5843,14 @@ where
58625843
}
58635844
}
58645845

5865-
match self.construct_pending_htlc_status(
5866-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5867-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5846+
match self.get_pending_htlc_info(
5847+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5848+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58685849
) {
5869-
PendingHTLCStatus::Forward(htlc_forward) => {
5870-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5871-
},
5872-
PendingHTLCStatus::Fail(htlc_fail) => {
5850+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5851+
Err(inbound_err) => {
58735852
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5874-
htlc_fails.push((htlc_fail, htlc_destination));
5853+
htlc_fails.push((self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err), htlc_destination));
58755854
},
58765855
}
58775856
}
@@ -11824,7 +11803,6 @@ where
1182411803
payment.htlcs.retain(|htlc| {
1182511804
// If height is approaching the number of blocks we think it takes us to get
1182611805
// our commitment transaction confirmed before the HTLC expires, plus the
11827-
// number of blocks we generally consider it to take to do a commitment update,
1182811806
// just give up on it and fail the HTLC.
1182911807
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
1183011808
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();

0 commit comments

Comments
 (0)