Skip to content

Commit 34c6292

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 73345bc commit 34c6292

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
@@ -8273,6 +8273,55 @@ where
82738273
}
82748274
}
82758275
}
8276+
8277+
// Whether the downstream channel was closed or not, try to re-apply any payment
8278+
// preimages from it which may be needed in upstream channels for forwarded
8279+
// payments.
8280+
for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() {
8281+
match htlc_source {
8282+
HTLCSource::PreviousHopData(prev_hop_data) => {
8283+
if let Some(payment_preimage) = preimage_opt {
8284+
let mut is_chan_open = false;
8285+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
8286+
if let Some(mut peer) = per_peer_state.get_mut(node_id).map(|node| node.lock().unwrap()) {
8287+
if let Some(chan) = peer.channel_by_id.get_mut(chan_id) {
8288+
is_chan_open = true;
8289+
match chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, payment_preimage, &args.logger) {
8290+
UpdateFulfillCommitFetch::DuplicateClaim {} => {},
8291+
UpdateFulfillCommitFetch::NewClaim { monitor_update, .. } => {
8292+
// The ChannelMonitor that gave us this
8293+
// preimage is for a now-closed channel -
8294+
// no further updates to that channel can
8295+
// happen which would result in the
8296+
// preimage being removed, thus we're
8297+
// guaranteed to regenerate this claim on
8298+
// restart as long as the source monitor
8299+
// sticks around.
8300+
pending_background_events.push(
8301+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8302+
counterparty_node_id: *node_id,
8303+
funding_txo: prev_hop_data.outpoint,
8304+
update: monitor_update.clone()
8305+
});
8306+
},
8307+
}
8308+
}
8309+
}
8310+
}
8311+
if !is_chan_open {
8312+
let monitor_update = ChannelMonitorUpdate {
8313+
update_id: CLOSED_CHANNEL_UPDATE_ID,
8314+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage }],
8315+
};
8316+
pending_background_events.push(BackgroundEvent::
8317+
ClosingMonitorUpdateRegeneratedOnStartup(
8318+
(prev_hop_data.outpoint, monitor_update)));
8319+
}
8320+
}
8321+
},
8322+
_ => {},
8323+
}
8324+
}
82768325
}
82778326
}
82788327

0 commit comments

Comments
 (0)