diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 040bd7a5ead..88957661dcd 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -8263,6 +8263,53 @@ where } } } + + // Whether the downstream channel was closed or not, try to re-apply any payment + // preimages from it which may be needed in upstream channels for forwarded + // payments. + for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() { + match htlc_source { + HTLCSource::PreviousHopData(prev_hop_data) => { + if let Some(payment_preimage) = preimage_opt { + let mut is_chan_open = false; + if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) { + if let Some(mut peer) = per_peer_state.get_mut(node_id).map(|node| node.lock().unwrap()) { + if let Some(chan) = peer.channel_by_id.get_mut(chan_id) { + is_chan_open = true; + match chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, payment_preimage, &args.logger) { + UpdateFulfillCommitFetch::DuplicateClaim {} => {}, + UpdateFulfillCommitFetch::NewClaim { monitor_update, .. } => { + // The ChannelMonitor that gave us this + // preimage is for a now-closed channel - + // no further updates to that channel can + // happen which would result in the + // preimage being removed, thus we're + // guaranteed to regenerate this claim on + // restart as long as the source monitor + // sticks around. + pending_background_events.push( + BackgroundEvent::MonitorUpdateRegeneratedOnStartup( + (*node_id, prev_hop_data.outpoint, + monitor_update.clone()))); + }, + } + } + } + } + if !is_chan_open { + let monitor_update = ChannelMonitorUpdate { + update_id: CLOSED_CHANNEL_UPDATE_ID, + updates: vec![ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage }], + }; + pending_background_events.push(BackgroundEvent:: + ClosingMonitorUpdateRegeneratedOnStartup( + (prev_hop_data.outpoint, monitor_update))); + } + } + }, + _ => {}, + } + } } }