Skip to content

Commit 48a688a

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 9f2e9f1 commit 48a688a

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
@@ -8290,6 +8290,55 @@ where
82908290
}
82918291
}
82928292
}
8293+
8294+
// Whether the downstream channel was closed or not, try to re-apply any payment
8295+
// preimages from it which may be needed in upstream channels for forwarded
8296+
// payments.
8297+
for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() {
8298+
match htlc_source {
8299+
HTLCSource::PreviousHopData(prev_hop_data) => {
8300+
if let Some(payment_preimage) = preimage_opt {
8301+
let mut is_chan_open = false;
8302+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
8303+
if let Some(mut peer) = per_peer_state.get_mut(node_id).map(|node| node.lock().unwrap()) {
8304+
if let Some(chan) = peer.channel_by_id.get_mut(chan_id) {
8305+
is_chan_open = true;
8306+
match chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, payment_preimage, &args.logger) {
8307+
UpdateFulfillCommitFetch::DuplicateClaim {} => {},
8308+
UpdateFulfillCommitFetch::NewClaim { monitor_update, .. } => {
8309+
// The ChannelMonitor that gave us this
8310+
// preimage is for a now-closed channel -
8311+
// no further updates to that channel can
8312+
// happen which would result in the
8313+
// preimage being removed, thus we're
8314+
// guaranteed to regenerate this claim on
8315+
// restart as long as the source monitor
8316+
// sticks around.
8317+
pending_background_events.push(
8318+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8319+
counterparty_node_id: *node_id,
8320+
funding_txo: prev_hop_data.outpoint,
8321+
update: monitor_update.clone()
8322+
});
8323+
},
8324+
}
8325+
}
8326+
}
8327+
}
8328+
if !is_chan_open {
8329+
let monitor_update = ChannelMonitorUpdate {
8330+
update_id: CLOSED_CHANNEL_UPDATE_ID,
8331+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage }],
8332+
};
8333+
pending_background_events.push(BackgroundEvent::
8334+
ClosingMonitorUpdateRegeneratedOnStartup(
8335+
(prev_hop_data.outpoint, monitor_update)));
8336+
}
8337+
}
8338+
},
8339+
_ => {},
8340+
}
8341+
}
82938342
}
82948343
}
82958344

0 commit comments

Comments
 (0)