Skip to content

Commit f25624a

Browse files
committed
Add ChannelManager::fail_htlc_backwards_with_reason
1 parent 8a35172 commit f25624a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,6 +3477,35 @@ where
34773477
}
34783478
}
34793479

3480+
/// This is a variant of [`ChannelManager::fail_htlc_backwards`] that allows you to specify the
3481+
/// reason for the failure.
3482+
///
3483+
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
3484+
/// after a PaymentClaimable event, failing the HTLC back to its origin and freeing resources
3485+
/// along the path (including in our own channel on which we received it).
3486+
///
3487+
/// Note that in some cases around unclean shutdown, it is possible the payment may have
3488+
/// already been claimed by you via [`ChannelManager::claim_funds`] prior to you seeing (a
3489+
/// second copy of) the [`events::Event::PaymentClaimable`] event. Alternatively, the payment
3490+
/// may have already been failed automatically by LDK if it was nearing its expiration time.
3491+
///
3492+
/// While LDK will never claim a payment automatically on your behalf (i.e. without you calling
3493+
/// [`ChannelManager::claim_funds`]), you should still monitor for
3494+
/// [`events::Event::PaymentClaimed`] events even for payments you intend to fail, especially on
3495+
/// startup during which time claims that were in-progress at shutdown may be replayed.
3496+
pub fn fail_htlc_backwards_with_reason(&self, payment_hash: &PaymentHash, reason: &HTLCFailReason) {
3497+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
3498+
3499+
let removed_source = self.claimable_payments.lock().unwrap().claimable_htlcs.remove(payment_hash);
3500+
if let Some((_, mut sources)) = removed_source {
3501+
for htlc in sources.drain(..) {
3502+
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
3503+
let receiver = HTLCDestination::FailedPayment { payment_hash: *payment_hash };
3504+
self.fail_htlc_backwards_internal(&source, &payment_hash, reason, receiver);
3505+
}
3506+
}
3507+
}
3508+
34803509
/// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code
34813510
/// that we want to return and a channel.
34823511
///

0 commit comments

Comments
 (0)