Skip to content

Commit 53dd1be

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 134f2cb commit 53dd1be

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8305,6 +8305,55 @@ where
83058305
}
83068306
}
83078307
}
8308+
8309+
// Whether the downstream channel was closed or not, try to re-apply any payment
8310+
// preimages from it which may be needed in upstream channels for forwarded
8311+
// payments.
8312+
for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() {
8313+
match htlc_source {
8314+
HTLCSource::PreviousHopData(prev_hop_data) => {
8315+
if let Some(payment_preimage) = preimage_opt {
8316+
let mut is_chan_open = false;
8317+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
8318+
if let Some(mut peer) = per_peer_state.get_mut(node_id).map(|node| node.lock().unwrap()) {
8319+
if let Some(chan) = peer.channel_by_id.get_mut(chan_id) {
8320+
is_chan_open = true;
8321+
match chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, payment_preimage, &args.logger) {
8322+
UpdateFulfillCommitFetch::DuplicateClaim {} => {},
8323+
UpdateFulfillCommitFetch::NewClaim { monitor_update, .. } => {
8324+
// The ChannelMonitor that gave us this
8325+
// preimage is for a now-closed channel -
8326+
// no further updates to that channel can
8327+
// happen which would result in the
8328+
// preimage being removed, thus we're
8329+
// guaranteed to regenerate this claim on
8330+
// restart as long as the source monitor
8331+
// sticks around.
8332+
pending_background_events.push(
8333+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8334+
counterparty_node_id: *node_id,
8335+
funding_txo: prev_hop_data.outpoint,
8336+
update: monitor_update.clone()
8337+
});
8338+
},
8339+
}
8340+
}
8341+
}
8342+
}
8343+
if !is_chan_open {
8344+
let monitor_update = ChannelMonitorUpdate {
8345+
update_id: CLOSED_CHANNEL_UPDATE_ID,
8346+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage }],
8347+
};
8348+
pending_background_events.push(BackgroundEvent::
8349+
ClosingMonitorUpdateRegeneratedOnStartup(
8350+
(prev_hop_data.outpoint, monitor_update)));
8351+
}
8352+
}
8353+
},
8354+
_ => {},
8355+
}
8356+
}
83088357
}
83098358
}
83108359

0 commit comments

Comments
 (0)