Skip to content

Commit 3c3fb09

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 cfcd1b2 commit 3c3fb09

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

0 commit comments

Comments
 (0)