Skip to content

Commit 6f0c817

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 998c0e1 commit 6f0c817

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8263,6 +8263,53 @@ where
82638263
}
82648264
}
82658265
}
8266+
8267+
// Whether the downstream channel was closed or not, try to re-apply any payment
8268+
// preimages from it which may be needed in upstream channels for forwarded
8269+
// payments.
8270+
for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() {
8271+
match htlc_source {
8272+
HTLCSource::PreviousHopData(prev_hop_data) => {
8273+
if let Some(payment_preimage) = preimage_opt {
8274+
let mut is_chan_open = false;
8275+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
8276+
if let Some(mut peer) = per_peer_state.get_mut(node_id).map(|node| node.lock().unwrap()) {
8277+
if let Some(chan) = peer.channel_by_id.get_mut(chan_id) {
8278+
is_chan_open = true;
8279+
match chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, payment_preimage, &args.logger) {
8280+
UpdateFulfillCommitFetch::DuplicateClaim {} => {},
8281+
UpdateFulfillCommitFetch::NewClaim { monitor_update, .. } => {
8282+
// The ChannelMonitor that gave us this
8283+
// preimage is for a now-closed channel -
8284+
// no further updates to that channel can
8285+
// happen which would result in the
8286+
// preimage being removed, thus we're
8287+
// guaranteed to regenerate this claim on
8288+
// restart as long as the source monitor
8289+
// sticks around.
8290+
pending_background_events.push(
8291+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(
8292+
(*node_id, prev_hop_data.outpoint,
8293+
monitor_update.clone())));
8294+
},
8295+
}
8296+
}
8297+
}
8298+
}
8299+
if !is_chan_open {
8300+
let monitor_update = ChannelMonitorUpdate {
8301+
update_id: CLOSED_CHANNEL_UPDATE_ID,
8302+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage }],
8303+
};
8304+
pending_background_events.push(BackgroundEvent::
8305+
ClosingMonitorUpdateRegeneratedOnStartup(
8306+
(prev_hop_data.outpoint, monitor_update)));
8307+
}
8308+
}
8309+
},
8310+
_ => {},
8311+
}
8312+
}
82668313
}
82678314
}
82688315

0 commit comments

Comments
 (0)