Skip to content

Commit

Permalink
Re-claim forwarded HTLCs on startup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheBlueMatt committed May 10, 2023
1 parent 998c0e1 commit 6f0c817
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
},
_ => {},
}
}
}
}

Expand Down

0 comments on commit 6f0c817

Please sign in to comment.