Skip to content

Commit 751c693

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 11c94dc commit 751c693

File tree

1 file changed

+42
-57
lines changed

1 file changed

+42
-57
lines changed

lightning/src/ln/channelmanager.rs

+42-57
Original file line numberDiff line numberDiff line change
@@ -4487,67 +4487,55 @@ where
44874487
})
44884488
}
44894489

4490-
fn construct_pending_htlc_status<'a>(
4491-
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, shared_secret: [u8; 32],
4492-
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4493-
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4494-
) -> PendingHTLCStatus {
4495-
macro_rules! return_err {
4496-
($msg: expr, $reason: expr, $data: expr) => {
4497-
{
4498-
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4499-
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
4500-
if msg.blinding_point.is_some() {
4501-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(
4502-
msgs::UpdateFailMalformedHTLC {
4503-
channel_id: msg.channel_id,
4504-
htlc_id: msg.htlc_id,
4505-
sha256_of_onion: [0; 32],
4506-
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
4507-
}
4508-
))
4490+
fn construct_pending_htlc_fail_msg<'a>(&self, msg: &msgs::UpdateAddHTLC,
4491+
counterparty_node_id: &PublicKey, shared_secret: [u8; 32], inbound_err: InboundHTLCErr) -> HTLCFailureMsg {
4492+
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
4493+
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", inbound_err.msg);
4494+
4495+
if msg.blinding_point.is_some() {
4496+
return HTLCFailureMsg::Malformed(
4497+
msgs::UpdateFailMalformedHTLC {
4498+
channel_id: msg.channel_id,
4499+
htlc_id: msg.htlc_id,
4500+
sha256_of_onion: [0; 32],
4501+
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
45094502
}
4510-
let failure = HTLCFailReason::reason($reason, $data.to_vec())
4511-
.get_encrypted_failure_packet(&shared_secret, &None);
4512-
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4513-
channel_id: msg.channel_id,
4514-
htlc_id: msg.htlc_id,
4515-
reason: failure.data,
4516-
}));
4517-
}
4518-
}
4503+
)
45194504
}
4505+
4506+
let failure = HTLCFailReason::reason(inbound_err.reason, inbound_err.err_data.to_vec())
4507+
.get_encrypted_failure_packet(&shared_secret, &None);
4508+
return HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4509+
channel_id: msg.channel_id,
4510+
htlc_id: msg.htlc_id,
4511+
reason: failure.data,
4512+
});
4513+
}
4514+
4515+
fn get_pending_htlc_info<'a>(
4516+
&self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32],
4517+
decoded_hop: onion_utils::Hop, allow_underpay: bool,
4518+
next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
4519+
) -> Result<PendingHTLCInfo, InboundHTLCErr> {
45204520
match decoded_hop {
45214521
onion_utils::Hop::Receive { .. } | onion_utils::Hop::BlindedReceive { .. } |
45224522
onion_utils::Hop::TrampolineReceive { .. } | onion_utils::Hop::TrampolineBlindedReceive { .. } => {
45234523
// OUR PAYMENT!
4524+
// Note that we could obviously respond immediately with an update_fulfill_htlc
4525+
// message, however that would leak that we are the recipient of this payment, so
4526+
// instead we stay symmetric with the forwarding case, only responding (after a
4527+
// delay) once they've send us a commitment_signed!
45244528
let current_height: u32 = self.best_block.read().unwrap().height;
4525-
match create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
4529+
create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
45264530
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
45274531
current_height)
4528-
{
4529-
Ok(info) => {
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 sent us a commitment_signed!
4534-
PendingHTLCStatus::Forward(info)
4535-
},
4536-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason , &err_data)
4537-
}
45384532
},
45394533
onion_utils::Hop::Forward { .. } | onion_utils::Hop::BlindedForward { .. } => {
4540-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4541-
Ok(info) => PendingHTLCStatus::Forward(info),
4542-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4543-
}
4534+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
45444535
},
45454536
onion_utils::Hop::TrampolineForward { .. } | onion_utils::Hop::TrampolineBlindedForward { .. } => {
4546-
match create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt) {
4547-
Ok(info) => PendingHTLCStatus::Forward(info),
4548-
Err(InboundHTLCErr { reason, err_data, msg }) => return_err!(msg, reason, &err_data)
4549-
}
4550-
}
4537+
create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
4538+
},
45514539
}
45524540
}
45534541

@@ -5839,16 +5827,14 @@ where
58395827
}
58405828
}
58415829

5842-
match self.construct_pending_htlc_status(
5843-
&update_add_htlc, &incoming_counterparty_node_id, shared_secret, next_hop,
5844-
incoming_accept_underpaying_htlcs, next_packet_details_opt.map(|d| d.next_packet_pubkey),
5830+
match self.get_pending_htlc_info(
5831+
&update_add_htlc, shared_secret, next_hop, incoming_accept_underpaying_htlcs,
5832+
next_packet_details_opt.map(|d| d.next_packet_pubkey),
58455833
) {
5846-
PendingHTLCStatus::Forward(htlc_forward) => {
5847-
htlc_forwards.push((htlc_forward, update_add_htlc.htlc_id));
5848-
},
5849-
PendingHTLCStatus::Fail(htlc_fail) => {
5834+
Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
5835+
Err(inbound_err) => {
58505836
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5851-
htlc_fails.push((htlc_fail, htlc_destination));
5837+
htlc_fails.push((self.construct_pending_htlc_fail_msg(&update_add_htlc, &incoming_counterparty_node_id, shared_secret, inbound_err), htlc_destination));
58525838
},
58535839
}
58545840
}
@@ -11784,7 +11770,6 @@ where
1178411770
payment.htlcs.retain(|htlc| {
1178511771
// If height is approaching the number of blocks we think it takes us to get
1178611772
// our commitment transaction confirmed before the HTLC expires, plus the
11787-
// number of blocks we generally consider it to take to do a commitment update,
1178811773
// just give up on it and fail the HTLC.
1178911774
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
1179011775
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();

0 commit comments

Comments
 (0)