Skip to content

Commit 37a595d

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 066a89d commit 37a595d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Diff for: lightning/src/ln/channelmanager.rs

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

0 commit comments

Comments
 (0)