Skip to content

Commit 07a6665

Browse files
committed
Add a BackgroundEvent to replay non-closing monitor updates
`BackgroundEvent` is used to store `ChannelMonitorUpdate`s which result in a channel force-close, avoiding relying on `ChannelMonitor`s having been loaded while `ChannelManager` block-connection methods are called during startup. In the coming commit(s) we'll also generate non-channel-closing `ChannelMonitorUpdate`s during startup, which will need to be replayed prior to any other `ChannelMonitorUpdate`s generated from normal operation. In the next commit we'll handle that by handling `BackgroundEvent`s immediately after locking the `total_consistency_lock`. The new `BackgroundEvent` variant is restricted to only `ChannelMonitorUpdate`s which will be regenerated on the next restart, as they're not persisted to disk.
1 parent 571adb1 commit 07a6665

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ enum BackgroundEvent {
495495
/// Handle a ChannelMonitorUpdate that closes a channel, broadcasting its current latest holder
496496
/// commitment transaction.
497497
ClosingMonitorUpdate((OutPoint, ChannelMonitorUpdate)),
498+
/// Handle a ChannelMonitorUpdate that does not close a channel, potentially unblocking the
499+
/// channel to continue normal operation.
500+
///
501+
/// Note that any such events are lost on shutdown, so in general they must be updates which
502+
/// are regenerated on startup.
503+
MonitorUpdateRegeneratedOnStartup((PublicKey, OutPoint, ChannelMonitorUpdate)),
498504
}
499505

500506
#[derive(Debug)]
@@ -3698,6 +3704,36 @@ where
36983704
// monitor updating completing.
36993705
let _ = self.chain_monitor.update_channel(funding_txo, &update);
37003706
},
3707+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup((counterparty_id, funding_txo, update)) => {
3708+
// The channel has already been closed, so no use bothering to care about the
3709+
// monitor updating completing.
3710+
let update_res = self.chain_monitor.update_channel(funding_txo, &update);
3711+
3712+
let mut found_chan = false;
3713+
let res = {
3714+
let per_peer_state = self.per_peer_state.read().unwrap();
3715+
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_id) {
3716+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3717+
let peer_state = &mut *peer_state_lock;
3718+
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
3719+
hash_map::Entry::Occupied(mut chan) => {
3720+
found_chan = true;
3721+
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan)
3722+
},
3723+
hash_map::Entry::Vacant(_) => {
3724+
Ok(())
3725+
},
3726+
}
3727+
} else { Ok(()) }
3728+
};
3729+
let _ = handle_error!(self, res, counterparty_id);
3730+
if !found_chan {
3731+
// TODO: If this channel has since closed, we're likely providing a payment
3732+
// preimage update, which we must ensure is durable! We currently don't,
3733+
// however, ensure that, and when we have a strategy therefor we should
3734+
// apply it here.
3735+
}
3736+
},
37013737
}
37023738
}
37033739
true
@@ -7275,14 +7311,18 @@ where
72757311
}
72767312

72777313
let background_events = self.pending_background_events.lock().unwrap();
7278-
(background_events.len() as u64).write(writer)?;
7314+
(background_events.iter()
7315+
.filter(|ev| if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup(_) = ev { false } else { true })
7316+
.count() as u64)
7317+
.write(writer)?;
72797318
for event in background_events.iter() {
72807319
match event {
72817320
BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)) => {
72827321
0u8.write(writer)?;
72837322
funding_txo.write(writer)?;
72847323
monitor_update.write(writer)?;
72857324
},
7325+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(_) => { },
72867326
}
72877327
}
72887328

@@ -7730,8 +7770,9 @@ where
77307770
0 => {
77317771
let (funding_txo, monitor_update): (OutPoint, ChannelMonitorUpdate) = (Readable::read(reader)?, Readable::read(reader)?);
77327772
if pending_background_events.iter().find(|e| {
7733-
let BackgroundEvent::ClosingMonitorUpdate((pending_funding_txo, pending_monitor_update)) = e;
7734-
*pending_funding_txo == funding_txo && *pending_monitor_update == monitor_update
7773+
if let BackgroundEvent::ClosingMonitorUpdate((pending_funding_txo, pending_monitor_update)) = e {
7774+
*pending_funding_txo == funding_txo && *pending_monitor_update == monitor_update
7775+
} else { false }
77357776
}).is_none() {
77367777
pending_background_events.push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)));
77377778
}

0 commit comments

Comments
 (0)