@@ -1633,9 +1633,9 @@ pub(crate) enum MonitorUpdateCompletionAction {
16331633 /// via [`ChannelManager::handle_monitor_event_htlc_ack`]. Only generated when
16341634 /// [`ChannelManager::persistent_monitor_events`] is enabled.
16351635 ///
1636- /// If the inbound edge is open, this action fires when an inbound edge RAA update removing an
1637- /// HTLC completes. If closed, it fires when the inbound edge has durably persisted the preimage
1638- /// monitor update .
1636+ /// If the inbound edge is open, this action fires when an inbound edge RAA update removing the
1637+ /// HTLC completes. If the inbound edge is closed and we're claiming, we'll also use this variant
1638+ /// to block acking the monitor event until the inbound edge has durably persisted the preimage .
16391639 ///
16401640 /// For claims, we could theoretically ack the outbound edge event once the preimage is durably
16411641 /// persisted in the inbound edge monitor, but if we stop persisting the holding cell in the
@@ -8267,11 +8267,20 @@ impl<
82678267 continue;
82688268 }
82698269 },
8270- HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
8271- // Channel went away before we could fail it. This implies
8272- // the channel is now on chain and our counterparty is
8273- // trying to broadcast the HTLC-Timeout, but that's their
8274- // problem, not ours.
8270+ HTLCForwardInfo::FailHTLC { htlc_id, upstream_channel_id, .. }
8271+ | HTLCForwardInfo::FailMalformedHTLC { htlc_id, upstream_channel_id, .. } => {
8272+ // Channel went away before we could fail it. This implies the channel is now
8273+ // on chain and our counterparty is trying to broadcast the HTLC-Timeout, but
8274+ // that's their problem, not ours.
8275+
8276+ // If `persistent_monitor_events` is enabled, there may be a monitor event generated by
8277+ // the outbound edge resolving this HTLC. That needs to be acked now or it will dangle
8278+ // forever.
8279+ if let Some(upstream_channel_id) = upstream_channel_id {
8280+ if self.persistent_monitor_events {
8281+ self.handle_monitor_event_htlc_ack(htlc_id, upstream_channel_id);
8282+ }
8283+ }
82758284 },
82768285 }
82778286 }
@@ -8547,6 +8556,11 @@ impl<
85478556 } else {
85488557 panic!("Stated return value requirements in queue_fail_{{malformed_}}htlc() were not met");
85498558 }
8559+ if self.persistent_monitor_events && htlc_not_found {
8560+ // The HTLC was already fully removed from the channel, so no future RAA
8561+ // will fire the usual ack path. Ack the outbound monitor event directly.
8562+ self.handle_monitor_event_htlc_ack(htlc_id, forward_chan_id);
8563+ }
85508564 // fail-backs are best-effort, we probably already have one
85518565 // pending, and if not that's OK, if not, the channel is on
85528566 // the chain and sending the HTLC-Timeout is their problem.
@@ -10791,9 +10805,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1079110805 self.forward_htlcs(htlc_forwards);
1079210806 self.finalize_claims(finalized_claimed_htlcs);
1079310807 for failure in failed_htlcs {
10794- if self.persistent_monitor_events
10795- && matches!(failure.0, HTLCSource::OutboundRoute { .. })
10796- {
10808+ if self.persistent_monitor_events {
1079710809 // The MonitorEvent::HTLCEvent generated when the previous counterparty commitment
1079810810 // is pruned will drive the failure instead.
1079910811 continue;
@@ -14199,20 +14211,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1419914211 Some(channel_id),
1420014212 Some(htlc_update.payment_hash),
1420114213 );
14214+ if self.persistent_monitor_events {
14215+ self.register_pending_forward_monitor_event_acks(
14216+ htlc_update.payment_hash,
14217+ htlc_update.source.previous_hop_data(),
14218+ monitor_event_source,
14219+ );
14220+ }
1420214221 match htlc_update.resolution {
1420314222 OutboundHTLCResolution::Claimed { preimage, skimmed_fee_msat } => {
1420414223 log_trace!(
1420514224 logger,
1420614225 "Claiming HTLC with preimage {} from our monitor",
1420714226 preimage
1420814227 );
14209- if self.persistent_monitor_events {
14210- self.register_pending_forward_monitor_event_acks(
14211- htlc_update.payment_hash,
14212- htlc_update.source.previous_hop_data(),
14213- monitor_event_source,
14214- );
14215- }
1421614228 // Claim the funds from the previous hop, if there is one. In the future we can
1421714229 // store attribution data in the `ChannelMonitor` and provide it here.
1421814230 self.claim_funds_internal(
@@ -14235,35 +14247,41 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1423514247 self.channel_is_closed(&channel_id, &counterparty_node_id);
1423614248 let we_are_sender =
1423714249 matches!(htlc_update.source, HTLCSource::OutboundRoute { .. });
14238- if from_onchain | we_are_sender {
14239- log_trace!(logger, "Failing HTLC from our monitor");
14240- let failure_type = htlc_update
14241- .source
14242- .failure_type(counterparty_node_id, channel_id);
14243-
14244- let completion_update =
14245- if self.persistent_monitor_events && we_are_sender {
14246- EventCompletionAction::AckMonitorEvent {
14247- event_id: monitor_event_source,
14248- }
14249- } else {
14250- EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(PaymentCompleteUpdate {
14251- counterparty_node_id,
14252- channel_funding_outpoint: funding_outpoint,
14253- channel_id,
14254- htlc_id: SentHTLCId::from_source(&htlc_update.source),
14255- })
14256- };
14257-
14258- self.fail_htlc_backwards_internal(
14259- &htlc_update.source,
14260- &htlc_update.payment_hash,
14261- &reason,
14262- failure_type,
14263- Some(completion_update),
14264- );
14265- }
14266- if !we_are_sender {
14250+ log_trace!(logger, "Failing HTLC from our monitor");
14251+ let failure_type = htlc_update
14252+ .source
14253+ .failure_type(counterparty_node_id, channel_id);
14254+
14255+ let completion_update = if self.persistent_monitor_events {
14256+ if we_are_sender {
14257+ // Ack the monitor event once PaymentFailed is processed by the user
14258+ Some(EventCompletionAction::AckMonitorEvent {
14259+ event_id: monitor_event_source,
14260+ })
14261+ } else {
14262+ // We'll ack the monitor event when it's removed from the inbound edge on RAA
14263+ None
14264+ }
14265+ } else {
14266+ if from_onchain {
14267+ Some(EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(PaymentCompleteUpdate {
14268+ counterparty_node_id,
14269+ channel_funding_outpoint: funding_outpoint,
14270+ channel_id,
14271+ htlc_id: SentHTLCId::from_source(&htlc_update.source),
14272+ }))
14273+ } else {
14274+ None
14275+ }
14276+ };
14277+ self.fail_htlc_backwards_internal(
14278+ &htlc_update.source,
14279+ &htlc_update.payment_hash,
14280+ &reason,
14281+ failure_type,
14282+ completion_update,
14283+ );
14284+ if !self.persistent_monitor_events {
1426714285 self.chain_monitor.ack_monitor_event(monitor_event_source);
1426814286 }
1426914287 },
@@ -21327,19 +21345,22 @@ impl<
2132721345 ev_action,
2132821346 );
2132921347 }
21330- for ((_, hash), htlcs) in already_forwarded_htlcs.into_iter() {
21331- for (htlc, _) in htlcs {
21332- let channel_id = htlc.channel_id;
21333- let node_id = htlc.counterparty_node_id;
21334- let source = HTLCSource::PreviousHopData(htlc);
21335- let failure_reason = LocalHTLCFailureReason::TemporaryChannelFailure;
21336- let failure_data = channel_manager.get_htlc_inbound_temp_fail_data(failure_reason);
21337- let reason = HTLCFailReason::reason(failure_reason, failure_data);
21338- let receiver = HTLCHandlingFailureType::Forward { node_id, channel_id };
21339- // The event completion action is only relevant for HTLCs that originate from our node, not
21340- // forwarded HTLCs.
21341- channel_manager
21342- .fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
21348+ if !channel_manager.persistent_monitor_events {
21349+ for ((_, hash), htlcs) in already_forwarded_htlcs.into_iter() {
21350+ for (htlc, _) in htlcs {
21351+ let channel_id = htlc.channel_id;
21352+ let node_id = htlc.counterparty_node_id;
21353+ let source = HTLCSource::PreviousHopData(htlc);
21354+ let failure_reason = LocalHTLCFailureReason::TemporaryChannelFailure;
21355+ let failure_data =
21356+ channel_manager.get_htlc_inbound_temp_fail_data(failure_reason);
21357+ let reason = HTLCFailReason::reason(failure_reason, failure_data);
21358+ let receiver = HTLCHandlingFailureType::Forward { node_id, channel_id };
21359+ // The event completion action is only relevant for HTLCs that originate from our node, not
21360+ // forwarded HTLCs.
21361+ channel_manager
21362+ .fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
21363+ }
2134321364 }
2134421365 }
2134521366
0 commit comments