@@ -18005,156 +18005,165 @@ where
1800518005 is_channel_closed = !peer_state.channel_by_id.contains_key(channel_id);
1800618006 }
1800718007
18008- if is_channel_closed {
18009- for (htlc_source, (htlc, preimage_opt)) in
18010- monitor.get_all_current_outbound_htlcs()
18011- {
18012- let logger = WithChannelMonitor::from(
18013- &args.logger,
18014- monitor,
18015- Some(htlc.payment_hash),
18016- );
18017- let htlc_id = SentHTLCId::from_source(&htlc_source);
18018- match htlc_source {
18019- HTLCSource::PreviousHopData(prev_hop_data) => {
18020- let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
18021- info.prev_funding_outpoint == prev_hop_data.outpoint
18022- && info.prev_htlc_id == prev_hop_data.htlc_id
18023- };
18024- // The ChannelMonitor is now responsible for this HTLC's
18025- // failure/success and will let us know what its outcome is. If we
18026- // still have an entry for this HTLC in `forward_htlcs`,
18027- // `pending_intercepted_htlcs`, or `decode_update_add_htlcs`, we were apparently not
18028- // persisted after the monitor was when forwarding the payment.
18029- dedup_decode_update_add_htlcs(
18030- &mut decode_update_add_htlcs,
18031- &prev_hop_data,
18032- "HTLC was forwarded to the closed channel",
18033- &args.logger,
18034- );
18035- dedup_decode_update_add_htlcs(
18036- &mut decode_update_add_htlcs_legacy,
18037- &prev_hop_data,
18038- "HTLC was forwarded to the closed channel",
18039- &args.logger,
18040- );
18041- forward_htlcs_legacy.retain(|_, forwards| {
18042- forwards.retain(|forward| {
18043- if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
18044- if pending_forward_matches_htlc(&htlc_info) {
18045- log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
18046- &htlc.payment_hash, &monitor.channel_id());
18047- false
18048- } else { true }
18008+ for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs()
18009+ {
18010+ let logger =
18011+ WithChannelMonitor::from(&args.logger, monitor, Some(htlc.payment_hash));
18012+ let htlc_id = SentHTLCId::from_source(&htlc_source);
18013+ match htlc_source {
18014+ HTLCSource::PreviousHopData(prev_hop_data) => {
18015+ let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
18016+ info.prev_funding_outpoint == prev_hop_data.outpoint
18017+ && info.prev_htlc_id == prev_hop_data.htlc_id
18018+ };
18019+ if !is_channel_closed {
18020+ continue;
18021+ }
18022+ // The ChannelMonitor is now responsible for this HTLC's
18023+ // failure/success and will let us know what its outcome is. If we
18024+ // still have an entry for this HTLC in `forward_htlcs`,
18025+ // `pending_intercepted_htlcs`, or `decode_update_add_htlcs`, we were apparently not
18026+ // persisted after the monitor was when forwarding the payment.
18027+ dedup_decode_update_add_htlcs(
18028+ &mut decode_update_add_htlcs,
18029+ &prev_hop_data,
18030+ "HTLC was forwarded to the closed channel",
18031+ &args.logger,
18032+ );
18033+ dedup_decode_update_add_htlcs(
18034+ &mut decode_update_add_htlcs_legacy,
18035+ &prev_hop_data,
18036+ "HTLC was forwarded to the closed channel",
18037+ &args.logger,
18038+ );
18039+ forward_htlcs_legacy.retain(|_, forwards| {
18040+ forwards.retain(|forward| {
18041+ if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
18042+ if pending_forward_matches_htlc(&htlc_info) {
18043+ log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
18044+ &htlc.payment_hash, &monitor.channel_id());
18045+ false
1804918046 } else { true }
18050- });
18051- !forwards.is_empty()
18052- });
18053- pending_intercepted_htlcs_legacy.retain(|intercepted_id, htlc_info| {
18054- if pending_forward_matches_htlc(&htlc_info) {
18055- log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
18056- &htlc.payment_hash, &monitor.channel_id());
18057- pending_events_read.retain(|(event, _)| {
18058- if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
18059- intercepted_id != ev_id
18060- } else { true }
18061- });
18062- false
1806318047 } else { true }
1806418048 });
18065- },
18066- HTLCSource::OutboundRoute {
18067- payment_id,
18068- session_priv,
18069- path,
18070- bolt12_invoice,
18071- ..
18072- } => {
18073- if let Some(preimage) = preimage_opt {
18074- let pending_events = Mutex::new(pending_events_read);
18075- let update = PaymentCompleteUpdate {
18076- counterparty_node_id: monitor.get_counterparty_node_id(),
18077- channel_funding_outpoint: monitor.get_funding_txo(),
18078- channel_id: monitor.channel_id(),
18079- htlc_id,
18080- };
18081- let mut compl_action = Some(
18082- EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(update)
18083- );
18084- pending_outbounds.claim_htlc(
18085- payment_id,
18086- preimage,
18087- bolt12_invoice,
18088- session_priv,
18089- path,
18090- true,
18091- &mut compl_action,
18092- &pending_events,
18093- );
18094- // If the completion action was not consumed, then there was no
18095- // payment to claim, and we need to tell the `ChannelMonitor`
18096- // we don't need to hear about the HTLC again, at least as long
18097- // as the PaymentSent event isn't still sitting around in our
18098- // event queue.
18099- let have_action = if compl_action.is_some() {
18100- let pending_events = pending_events.lock().unwrap();
18101- pending_events.iter().any(|(_, act)| *act == compl_action)
18102- } else {
18103- false
18104- };
18105- if !have_action && compl_action.is_some() {
18106- let mut peer_state = per_peer_state
18107- .get(&counterparty_node_id)
18108- .map(|state| state.lock().unwrap())
18109- .expect("Channels originating a preimage must have peer state");
18110- let update_id = peer_state
18111- .closed_channel_monitor_update_ids
18112- .get_mut(channel_id)
18113- .expect("Channels originating a preimage must have a monitor");
18114- // Note that for channels closed pre-0.1, the latest
18115- // update_id is `u64::MAX`.
18116- *update_id = update_id.saturating_add(1);
18117-
18118- pending_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
18119- counterparty_node_id: monitor.get_counterparty_node_id(),
18049+ !forwards.is_empty()
18050+ });
18051+ pending_intercepted_htlcs_legacy.retain(|intercepted_id, htlc_info| {
18052+ if pending_forward_matches_htlc(&htlc_info) {
18053+ log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
18054+ &htlc.payment_hash, &monitor.channel_id());
18055+ pending_events_read.retain(|(event, _)| {
18056+ if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
18057+ intercepted_id != ev_id
18058+ } else { true }
18059+ });
18060+ false
18061+ } else { true }
18062+ });
18063+ },
18064+ HTLCSource::OutboundRoute {
18065+ payment_id,
18066+ session_priv,
18067+ path,
18068+ bolt12_invoice,
18069+ ..
18070+ } => {
18071+ if !is_channel_closed {
18072+ continue;
18073+ }
18074+ if let Some(preimage) = preimage_opt {
18075+ let pending_events = Mutex::new(pending_events_read);
18076+ let update = PaymentCompleteUpdate {
18077+ counterparty_node_id: monitor.get_counterparty_node_id(),
18078+ channel_funding_outpoint: monitor.get_funding_txo(),
18079+ channel_id: monitor.channel_id(),
18080+ htlc_id,
18081+ };
18082+ let mut compl_action = Some(
18083+ EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(update)
18084+ );
18085+ pending_outbounds.claim_htlc(
18086+ payment_id,
18087+ preimage,
18088+ bolt12_invoice,
18089+ session_priv,
18090+ path,
18091+ true,
18092+ &mut compl_action,
18093+ &pending_events,
18094+ );
18095+ // If the completion action was not consumed, then there was no
18096+ // payment to claim, and we need to tell the `ChannelMonitor`
18097+ // we don't need to hear about the HTLC again, at least as long
18098+ // as the PaymentSent event isn't still sitting around in our
18099+ // event queue.
18100+ let have_action = if compl_action.is_some() {
18101+ let pending_events = pending_events.lock().unwrap();
18102+ pending_events.iter().any(|(_, act)| *act == compl_action)
18103+ } else {
18104+ false
18105+ };
18106+ if !have_action && compl_action.is_some() {
18107+ let mut peer_state = per_peer_state
18108+ .get(&counterparty_node_id)
18109+ .map(|state| state.lock().unwrap())
18110+ .expect(
18111+ "Channels originating a preimage must have peer state",
18112+ );
18113+ let update_id = peer_state
18114+ .closed_channel_monitor_update_ids
18115+ .get_mut(channel_id)
18116+ .expect(
18117+ "Channels originating a preimage must have a monitor",
18118+ );
18119+ // Note that for channels closed pre-0.1, the latest
18120+ // update_id is `u64::MAX`.
18121+ *update_id = update_id.saturating_add(1);
18122+
18123+ pending_background_events.push(
18124+ BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
18125+ counterparty_node_id: monitor
18126+ .get_counterparty_node_id(),
1812018127 funding_txo: monitor.get_funding_txo(),
1812118128 channel_id: monitor.channel_id(),
1812218129 update: ChannelMonitorUpdate {
1812318130 update_id: *update_id,
1812418131 channel_id: Some(monitor.channel_id()),
18125- updates: vec![ChannelMonitorUpdateStep::ReleasePaymentComplete {
18126- htlc: htlc_id,
18127- }],
18132+ updates: vec![
18133+ ChannelMonitorUpdateStep::ReleasePaymentComplete {
18134+ htlc: htlc_id,
18135+ },
18136+ ],
1812818137 },
18129- });
18130- }
18131- pending_events_read = pending_events.into_inner().unwrap();
18138+ },
18139+ );
1813218140 }
18133- },
18134- }
18141+ pending_events_read = pending_events.into_inner().unwrap();
18142+ }
18143+ },
1813518144 }
18136- for (htlc_source, payment_hash) in monitor.get_onchain_failed_outbound_htlcs() {
18137- log_info!(
18138- args.logger,
18139- "Failing HTLC with payment hash {} as it was resolved on-chain.",
18140- payment_hash
18141- );
18142- let completion_action = Some(PaymentCompleteUpdate {
18143- counterparty_node_id: monitor.get_counterparty_node_id(),
18144- channel_funding_outpoint: monitor.get_funding_txo(),
18145- channel_id: monitor.channel_id(),
18146- htlc_id: SentHTLCId::from_source(&htlc_source),
18147- });
18145+ }
18146+ for (htlc_source, payment_hash) in monitor.get_onchain_failed_outbound_htlcs() {
18147+ log_info!(
18148+ args.logger,
18149+ "Failing HTLC with payment hash {} as it was resolved on-chain.",
18150+ payment_hash
18151+ );
18152+ let completion_action = Some(PaymentCompleteUpdate {
18153+ counterparty_node_id: monitor.get_counterparty_node_id(),
18154+ channel_funding_outpoint: monitor.get_funding_txo(),
18155+ channel_id: monitor.channel_id(),
18156+ htlc_id: SentHTLCId::from_source(&htlc_source),
18157+ });
1814818158
18149- failed_htlcs.push((
18150- htlc_source,
18151- payment_hash,
18152- monitor.get_counterparty_node_id(),
18153- monitor.channel_id(),
18154- LocalHTLCFailureReason::OnChainTimeout,
18155- completion_action,
18156- ));
18157- }
18159+ failed_htlcs.push((
18160+ htlc_source,
18161+ payment_hash,
18162+ monitor.get_counterparty_node_id(),
18163+ monitor.channel_id(),
18164+ LocalHTLCFailureReason::OnChainTimeout,
18165+ completion_action,
18166+ ));
1815818167 }
1815918168
1816018169 // Whether the downstream channel was closed or not, try to re-apply any payment
0 commit comments