Skip to content

Commit 78221cc

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 e4ca3ee commit 78221cc

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -897,9 +897,9 @@ struct ClaimablePayments {
897897
#[derive(Debug)]
898898
enum BackgroundEvent {
899899
/// Handle a ChannelMonitorUpdate which closes the channel or for an already-closed channel.
900-
/// This is only separated from [`Self::MonitorUpdateRegeneratedOnStartup`] as the
901-
/// maybe-non-closing variant needs a public key to handle channel resumption, whereas if the
902-
/// channel has been force-closed we do not need the counterparty node_id.
900+
/// This is only separated from [`Self::MonitorUpdateRegeneratedOnStartup`] as for truly
901+
/// ancient [`ChannelMonitor`]s that haven't seen an update since LDK 0.0.118 we may not have
902+
/// the counterparty node ID available.
903903
///
904904
/// Note that any such events are lost on shutdown, so in general they must be updates which
905905
/// are regenerated on startup.
@@ -6978,17 +6978,15 @@ where
69786978
// If we're running during init we cannot update a monitor directly - they probably
69796979
// haven't actually been loaded yet. Instead, push the monitor update as a background
69806980
// event.
6981-
// Note that while it's safe to use `ClosedMonitorUpdateRegeneratedOnStartup` here (the
6982-
// channel is already closed) we need to ultimately handle the monitor update
6983-
// completion action only after we've completed the monitor update. This is the only
6984-
// way to guarantee this update *will* be regenerated on startup (otherwise if this was
6985-
// from a forwarded HTLC the downstream preimage may be deleted before we claim
6986-
// upstream). Thus, we need to transition to some new `BackgroundEvent` type which will
6987-
// complete the monitor update completion action from `completion_action`.
6988-
self.pending_background_events.lock().unwrap().push(
6989-
BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((
6990-
prev_hop.outpoint, prev_hop.channel_id, preimage_update,
6991-
)));
6981+
// TODO: Track this update as pending and only complete the completion action when it
6982+
// finishes.
6983+
let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
6984+
counterparty_node_id,
6985+
funding_txo: prev_hop.outpoint,
6986+
channel_id: prev_hop.channel_id,
6987+
update: preimage_update,
6988+
};
6989+
self.pending_background_events.lock().unwrap().push(event);
69926990
}
69936991
// Note that we do process the completion action here. This totally could be a
69946992
// duplicate claim, but we have no way of knowing without interrogating the
@@ -7081,22 +7079,35 @@ where
70817079
// There should be a `BackgroundEvent` pending...
70827080
assert!(background_events.iter().any(|ev| {
70837081
match ev {
7084-
// to apply a monitor update that blocked the claiming channel,
70857082
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
70867083
funding_txo, update, ..
70877084
} => {
70887085
if *funding_txo == claiming_chan_funding_outpoint {
7086+
// to apply a monitor update that blocked the claiming channel,
70897087
assert!(update.updates.iter().any(|upd|
70907088
if let ChannelMonitorUpdateStep::PaymentPreimage {
70917089
payment_preimage: update_preimage
70927090
} = upd {
70937091
payment_preimage == *update_preimage
7094-
} else { false }
7092+
} else {
7093+
false
7094+
}
7095+
), "{:?}", update);
7096+
true
7097+
} else if *funding_txo == next_channel_outpoint {
7098+
// or the channel we'd unblock is already closed,
7099+
assert!(update.updates.iter().any(|upd|
7100+
if let ChannelMonitorUpdateStep::ChannelForceClosed { .. } = upd {
7101+
true
7102+
} else {
7103+
false
7104+
}
70957105
), "{:?}", update);
70967106
true
70977107
} else { false }
70987108
},
7099-
// or the channel we'd unblock is already closed,
7109+
// or the channel we'd unblock is already closed (for an
7110+
// old channel),
71007111
BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup(
71017112
(funding_txo, _channel_id, monitor_update)
71027113
) => {
@@ -12389,7 +12400,23 @@ where
1238912400
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast: true }],
1239012401
channel_id: Some(monitor.channel_id()),
1239112402
};
12392-
close_background_events.push(BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((*funding_txo, channel_id, monitor_update)));
12403+
if let Some(counterparty_node_id) = monitor.get_counterparty_node_id() {
12404+
let update = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
12405+
counterparty_node_id,
12406+
funding_txo: *funding_txo,
12407+
channel_id,
12408+
update: monitor_update,
12409+
};
12410+
close_background_events.push(update);
12411+
} else {
12412+
// This is a fairly old `ChannelMonitor` that hasn't seen an update to its
12413+
// off-chain state since LDK 0.0.118 (as in LDK 0.0.119 any off-chain
12414+
// `ChannelMonitorUpdate` will set the counterparty ID).
12415+
// Thus, we assume that it has no pending HTLCs and we will not need to
12416+
// generate a `ChannelMonitorUpdate` for it aside from this
12417+
// `ChannelForceClosed` one.
12418+
close_background_events.push(BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((*funding_txo, channel_id, monitor_update)));
12419+
}
1239312420
}
1239412421
}
1239512422

0 commit comments

Comments
 (0)