Skip to content

Commit 3f36890

Browse files
committed
Prefer to use MonitorUpdateRegeneratedOnStartup where possible
In the next commit we'll drop the magic `u64::MAX` `ChannelMonitorUpdate::update_id` value used when we don't know the `ChannelMonitor`'s `latest_update_id` (i.e. when the channel is closed). In order to do so, we will store further information about `ChannelMonitor`s in the per-peer structure, keyed by the counterparty's node ID, which will be used when applying `ChannelMonitorUpdate`s to closed channels. By taking advantage of the change in the previous commit, that information is now reliably available when we generate the `ChannelMonitorUpdate` (when claiming HTLCs), but in order to ensure it is available when applying the `ChannelMonitorUpdate` we need to use `BackgroundEvent::MonitorUpdateRegeneratedOnStartup` instead of `BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup` where possible. Here we do this, leaving `ClosedMonitorUpdateRegeneratedOnStartup` only used to ensure very old channels (created in 0.0.118 or earlier) which are not in the `ChannelManager` are force-closed on startup.
1 parent 6f023f8 commit 3f36890

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

lightning/src/ln/channelmanager.rs

+45-18
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,9 @@ impl ClaimablePayments {
973973
#[derive(Debug)]
974974
enum BackgroundEvent {
975975
/// Handle a ChannelMonitorUpdate which closes the channel or for an already-closed channel.
976-
/// This is only separated from [`Self::MonitorUpdateRegeneratedOnStartup`] as the
977-
/// maybe-non-closing variant needs a public key to handle channel resumption, whereas if the
978-
/// channel has been force-closed we do not need the counterparty node_id.
976+
/// This is only separated from [`Self::MonitorUpdateRegeneratedOnStartup`] as for truly
977+
/// ancient [`ChannelMonitor`]s that haven't seen an update since LDK 0.0.118 we may not have
978+
/// the counterparty node ID available.
979979
///
980980
/// Note that any such events are lost on shutdown, so in general they must be updates which
981981
/// are regenerated on startup.
@@ -7109,17 +7109,15 @@ where
71097109
// If we're running during init we cannot update a monitor directly - they probably
71107110
// haven't actually been loaded yet. Instead, push the monitor update as a background
71117111
// event.
7112-
// Note that while it's safe to use `ClosedMonitorUpdateRegeneratedOnStartup` here (the
7113-
// channel is already closed) we need to ultimately handle the monitor update
7114-
// completion action only after we've completed the monitor update. This is the only
7115-
// way to guarantee this update *will* be regenerated on startup (otherwise if this was
7116-
// from a forwarded HTLC the downstream preimage may be deleted before we claim
7117-
// upstream). Thus, we need to transition to some new `BackgroundEvent` type which will
7118-
// complete the monitor update completion action from `completion_action`.
7119-
self.pending_background_events.lock().unwrap().push(
7120-
BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((
7121-
prev_hop.funding_txo, prev_hop.channel_id, preimage_update,
7122-
)));
7112+
// TODO: Track this update as pending and only complete the completion action when it
7113+
// finishes.
7114+
let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7115+
counterparty_node_id,
7116+
funding_txo: prev_hop.funding_txo,
7117+
channel_id: prev_hop.channel_id,
7118+
update: preimage_update,
7119+
};
7120+
self.pending_background_events.lock().unwrap().push(event);
71237121
}
71247122
// Note that we do process the completion action here. This totally could be a
71257123
// duplicate claim, but we have no way of knowing without interrogating the
@@ -7212,22 +7210,35 @@ where
72127210
// There should be a `BackgroundEvent` pending...
72137211
assert!(background_events.iter().any(|ev| {
72147212
match ev {
7215-
// to apply a monitor update that blocked the claiming channel,
72167213
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
72177214
funding_txo, update, ..
72187215
} => {
72197216
if *funding_txo == claiming_chan_funding_outpoint {
7217+
// to apply a monitor update that blocked the claiming channel,
72207218
assert!(update.updates.iter().any(|upd|
72217219
if let ChannelMonitorUpdateStep::PaymentPreimage {
72227220
payment_preimage: update_preimage, ..
72237221
} = upd {
72247222
payment_preimage == *update_preimage
7225-
} else { false }
7223+
} else {
7224+
false
7225+
}
7226+
), "{:?}", update);
7227+
true
7228+
} else if *funding_txo == next_channel_outpoint {
7229+
// or the channel we'd unblock is already closed,
7230+
assert!(update.updates.iter().any(|upd|
7231+
if let ChannelMonitorUpdateStep::ChannelForceClosed { .. } = upd {
7232+
true
7233+
} else {
7234+
false
7235+
}
72267236
), "{:?}", update);
72277237
true
72287238
} else { false }
72297239
},
7230-
// or the channel we'd unblock is already closed,
7240+
// or the channel we'd unblock is already closed (for an
7241+
// old channel),
72317242
BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup(
72327243
(funding_txo, _channel_id, monitor_update)
72337244
) => {
@@ -12543,7 +12554,23 @@ where
1254312554
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast: true }],
1254412555
channel_id: Some(monitor.channel_id()),
1254512556
};
12546-
close_background_events.push(BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((*funding_txo, channel_id, monitor_update)));
12557+
if let Some(counterparty_node_id) = monitor.get_counterparty_node_id() {
12558+
let update = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
12559+
counterparty_node_id,
12560+
funding_txo: *funding_txo,
12561+
channel_id,
12562+
update: monitor_update,
12563+
};
12564+
close_background_events.push(update);
12565+
} else {
12566+
// This is a fairly old `ChannelMonitor` that hasn't seen an update to its
12567+
// off-chain state since LDK 0.0.118 (as in LDK 0.0.119 any off-chain
12568+
// `ChannelMonitorUpdate` will set the counterparty ID).
12569+
// Thus, we assume that it has no pending HTLCs and we will not need to
12570+
// generate a `ChannelMonitorUpdate` for it aside from this
12571+
// `ChannelForceClosed` one.
12572+
close_background_events.push(BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((*funding_txo, channel_id, monitor_update)));
12573+
}
1254712574
}
1254812575
}
1254912576

0 commit comments

Comments
 (0)