@@ -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
@@ -8272,11 +8272,20 @@ impl<
82728272 continue;
82738273 }
82748274 },
8275- HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
8276- // Channel went away before we could fail it. This implies
8277- // the channel is now on chain and our counterparty is
8278- // trying to broadcast the HTLC-Timeout, but that's their
8279- // problem, not ours.
8275+ HTLCForwardInfo::FailHTLC { htlc_id, upstream_channel_id, .. }
8276+ | HTLCForwardInfo::FailMalformedHTLC { htlc_id, upstream_channel_id, .. } => {
8277+ // Channel went away before we could fail it. This implies the channel is now
8278+ // on chain and our counterparty is trying to broadcast the HTLC-Timeout, but
8279+ // that's their problem, not ours.
8280+
8281+ // If `persistent_monitor_events` is enabled, there may be a monitor event generated by
8282+ // the outbound edge resolving this HTLC. That needs to be acked now or it will dangle
8283+ // forever.
8284+ if let Some(upstream_channel_id) = upstream_channel_id {
8285+ if self.persistent_monitor_events {
8286+ self.handle_monitor_event_htlc_ack(htlc_id, upstream_channel_id);
8287+ }
8288+ }
82808289 },
82818290 }
82828291 }
@@ -8552,6 +8561,11 @@ impl<
85528561 } else {
85538562 panic!("Stated return value requirements in queue_fail_{{malformed_}}htlc() were not met");
85548563 }
8564+ if self.persistent_monitor_events && htlc_not_found {
8565+ // The HTLC was already fully removed from the channel, so no future RAA
8566+ // will fire the usual ack path. Ack the outbound monitor event directly.
8567+ self.handle_monitor_event_htlc_ack(htlc_id, forward_chan_id);
8568+ }
85558569 // fail-backs are best-effort, we probably already have one
85568570 // pending, and if not that's OK, if not, the channel is on
85578571 // the chain and sending the HTLC-Timeout is their problem.
@@ -10796,9 +10810,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1079610810 self.forward_htlcs(htlc_forwards);
1079710811 self.finalize_claims(finalized_claimed_htlcs);
1079810812 for failure in failed_htlcs {
10799- if self.persistent_monitor_events
10800- && matches!(failure.0, HTLCSource::OutboundRoute { .. })
10801- {
10813+ if self.persistent_monitor_events {
1080210814 // The MonitorEvent::HTLCEvent generated when the previous counterparty commitment
1080310815 // is pruned will drive the failure instead.
1080410816 continue;
@@ -14298,20 +14310,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1429814310 Some(channel_id),
1429914311 Some(htlc_update.payment_hash),
1430014312 );
14313+ if self.persistent_monitor_events {
14314+ self.register_pending_forward_monitor_event_acks(
14315+ htlc_update.payment_hash,
14316+ htlc_update.source.previous_hop_data(),
14317+ monitor_event_source,
14318+ );
14319+ }
1430114320 match htlc_update.resolution {
1430214321 OutboundHTLCResolution::Claimed { preimage, skimmed_fee_msat } => {
1430314322 log_trace!(
1430414323 logger,
1430514324 "Claiming HTLC with preimage {} from our monitor",
1430614325 preimage
1430714326 );
14308- if self.persistent_monitor_events {
14309- self.register_pending_forward_monitor_event_acks(
14310- htlc_update.payment_hash,
14311- htlc_update.source.previous_hop_data(),
14312- monitor_event_source,
14313- );
14314- }
1431514327 // Claim the funds from the previous hop, if there is one. In the future we can
1431614328 // store attribution data in the `ChannelMonitor` and provide it here.
1431714329 self.claim_funds_internal(
@@ -14334,35 +14346,41 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1433414346 self.channel_is_closed(&channel_id, &counterparty_node_id);
1433514347 let we_are_sender =
1433614348 matches!(htlc_update.source, HTLCSource::OutboundRoute { .. });
14337- if from_onchain | we_are_sender {
14338- log_trace!(logger, "Failing HTLC from our monitor");
14339- let failure_type = htlc_update
14340- .source
14341- .failure_type(counterparty_node_id, channel_id);
14342-
14343- let completion_update =
14344- if self.persistent_monitor_events && we_are_sender {
14345- EventCompletionAction::AckMonitorEvent {
14346- event_id: monitor_event_source,
14347- }
14348- } else {
14349- EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(PaymentCompleteUpdate {
14350- counterparty_node_id,
14351- channel_funding_outpoint: funding_outpoint,
14352- channel_id,
14353- htlc_id: SentHTLCId::from_source(&htlc_update.source),
14354- })
14355- };
14356-
14357- self.fail_htlc_backwards_internal(
14358- &htlc_update.source,
14359- &htlc_update.payment_hash,
14360- &reason,
14361- failure_type,
14362- Some(completion_update),
14363- );
14364- }
14365- if !we_are_sender {
14349+ log_trace!(logger, "Failing HTLC from our monitor");
14350+ let failure_type = htlc_update
14351+ .source
14352+ .failure_type(counterparty_node_id, channel_id);
14353+
14354+ let completion_update = if self.persistent_monitor_events {
14355+ if we_are_sender {
14356+ // Ack the monitor event once PaymentFailed is processed by the user
14357+ Some(EventCompletionAction::AckMonitorEvent {
14358+ event_id: monitor_event_source,
14359+ })
14360+ } else {
14361+ // We'll ack the monitor event when it's removed from the inbound edge on RAA
14362+ None
14363+ }
14364+ } else {
14365+ if from_onchain {
14366+ Some(EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(PaymentCompleteUpdate {
14367+ counterparty_node_id,
14368+ channel_funding_outpoint: funding_outpoint,
14369+ channel_id,
14370+ htlc_id: SentHTLCId::from_source(&htlc_update.source),
14371+ }))
14372+ } else {
14373+ None
14374+ }
14375+ };
14376+ self.fail_htlc_backwards_internal(
14377+ &htlc_update.source,
14378+ &htlc_update.payment_hash,
14379+ &reason,
14380+ failure_type,
14381+ completion_update,
14382+ );
14383+ if !self.persistent_monitor_events {
1436614384 self.chain_monitor.ack_monitor_event(monitor_event_source);
1436714385 }
1436814386 },
@@ -21423,19 +21441,22 @@ impl<
2142321441 ev_action,
2142421442 );
2142521443 }
21426- for ((_, hash), htlcs) in already_forwarded_htlcs.into_iter() {
21427- for (htlc, _) in htlcs {
21428- let channel_id = htlc.channel_id;
21429- let node_id = htlc.counterparty_node_id;
21430- let source = HTLCSource::PreviousHopData(htlc);
21431- let failure_reason = LocalHTLCFailureReason::TemporaryChannelFailure;
21432- let failure_data = channel_manager.get_htlc_inbound_temp_fail_data(failure_reason);
21433- let reason = HTLCFailReason::reason(failure_reason, failure_data);
21434- let receiver = HTLCHandlingFailureType::Forward { node_id, channel_id };
21435- // The event completion action is only relevant for HTLCs that originate from our node, not
21436- // forwarded HTLCs.
21437- channel_manager
21438- .fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
21444+ if !channel_manager.persistent_monitor_events {
21445+ for ((_, hash), htlcs) in already_forwarded_htlcs.into_iter() {
21446+ for (htlc, _) in htlcs {
21447+ let channel_id = htlc.channel_id;
21448+ let node_id = htlc.counterparty_node_id;
21449+ let source = HTLCSource::PreviousHopData(htlc);
21450+ let failure_reason = LocalHTLCFailureReason::TemporaryChannelFailure;
21451+ let failure_data =
21452+ channel_manager.get_htlc_inbound_temp_fail_data(failure_reason);
21453+ let reason = HTLCFailReason::reason(failure_reason, failure_data);
21454+ let receiver = HTLCHandlingFailureType::Forward { node_id, channel_id };
21455+ // The event completion action is only relevant for HTLCs that originate from our node, not
21456+ // forwarded HTLCs.
21457+ channel_manager
21458+ .fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
21459+ }
2143921460 }
2144021461 }
2144121462
0 commit comments