Skip to content

Commit ab7c327

Browse files
committed
Re-claim forwarded HTLCs on startup
Now that we let `commitment_signed` `ChannelMonitorUpdate`s from a downstream channel complete prior to the preimage `ChannelMonitorUpdate` on the upstream channel, we may not get a `update_fulfill_htlc` replay on startup. Thus, we have to ensure any payment preimages contained in that downstream update are re-claimed on startup. Here we do this during the existing walk of the `ChannelMonitor` preimages for closed channels.
1 parent fc643f5 commit ab7c327

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8190,6 +8190,51 @@ where
81908190
}
81918191
}
81928192
}
8193+
8194+
// Whether the downstream channel was closed or not, try to re-apply any payment
8195+
// preimages from it which may be needed in upstream channels for forwarded
8196+
// payments.
8197+
for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() {
8198+
match htlc_source {
8199+
HTLCSource::PreviousHopData(prev_hop_data) => {
8200+
if let Some(payment_preimage) = preimage_opt {
8201+
let mut is_chan_open = false;
8202+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
8203+
if let Some(mut peer) = per_peer_state.get_mut(node_id).map(|node| node.lock().unwrap()) {
8204+
if let Some(chan) = peer.channel_by_id.get_mut(chan_id) {
8205+
is_chan_open = true;
8206+
match chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, payment_preimage, &args.logger) {
8207+
UpdateFulfillCommitFetch::DuplicateClaim {} => {},
8208+
UpdateFulfillCommitFetch::NewClaim { monitor_update, .. } => {
8209+
// The ChannelMonitor that gave us this
8210+
// preimage is for a now-closed channel -
8211+
// no further updates to that channel can
8212+
// happen which would result in the
8213+
// preimage being removed, thus we're
8214+
// guaranteed to regenerate this claim on
8215+
// restart as long as the source monitor
8216+
// sticks around.
8217+
pending_background_events.push(
8218+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(
8219+
(*node_id, prev_hop_data.outpoint,
8220+
monitor_update.clone())));
8221+
},
8222+
}
8223+
}
8224+
}
8225+
}
8226+
if !is_chan_open {
8227+
let monitor_update = ChannelMonitorUpdate {
8228+
update_id: CLOSED_CHANNEL_UPDATE_ID,
8229+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage }],
8230+
};
8231+
pending_background_events.push(BackgroundEvent::ClosingMonitorUpdate((prev_hop_data.outpoint, monitor_update)));
8232+
}
8233+
}
8234+
},
8235+
_ => {},
8236+
}
8237+
}
81938238
}
81948239
}
81958240

0 commit comments

Comments
 (0)