Skip to content

Commit 0e39098

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 e94647c commit 0e39098

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
@@ -504,6 +504,12 @@ enum BackgroundEvent {
504504
/// Handle a ChannelMonitorUpdate that closes a channel, broadcasting its current latest holder
505505
/// commitment transaction.
506506
ClosingMonitorUpdate((OutPoint, ChannelMonitorUpdate)),
507+
/// Handle a ChannelMonitorUpdate that does not close a channel, potentially unblocking the
508+
/// channel to continue normal operation.
509+
///
510+
/// Note that any such events are lost on shutdown, so in general they must be updates which
511+
/// are regenerated on startup.
512+
MonitorUpdateRegeneratedOnStartup((PublicKey, OutPoint, ChannelMonitorUpdate)),
507513
}
508514

509515
#[derive(Debug)]
@@ -3778,6 +3784,36 @@ where
37783784
// monitor updating completing.
37793785
let _ = self.chain_monitor.update_channel(funding_txo, &update);
37803786
},
3787+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup((counterparty_id, funding_txo, update)) => {
3788+
// The channel has already been closed, so no use bothering to care about the
3789+
// monitor updating completing.
3790+
let update_res = self.chain_monitor.update_channel(funding_txo, &update);
3791+
3792+
let mut found_chan = false;
3793+
let res = {
3794+
let per_peer_state = self.per_peer_state.read().unwrap();
3795+
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_id) {
3796+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3797+
let peer_state = &mut *peer_state_lock;
3798+
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
3799+
hash_map::Entry::Occupied(mut chan) => {
3800+
found_chan = true;
3801+
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan)
3802+
},
3803+
hash_map::Entry::Vacant(_) => {
3804+
Ok(())
3805+
},
3806+
}
3807+
} else { Ok(()) }
3808+
};
3809+
let _ = handle_error!(self, res, counterparty_id);
3810+
if !found_chan {
3811+
// TODO: If this channel has since closed, we're likely providing a payment
3812+
// preimage update, which we must ensure is durable! We currently don't,
3813+
// however, ensure that, and when we have a strategy therefor we should
3814+
// apply it here.
3815+
}
3816+
},
37813817
}
37823818
}
37833819
true
@@ -7363,14 +7399,18 @@ where
73637399
}
73647400

73657401
let background_events = self.pending_background_events.lock().unwrap();
7366-
(background_events.len() as u64).write(writer)?;
7402+
(background_events.iter()
7403+
.filter(|ev| if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup(_) = ev { false } else { true })
7404+
.count() as u64)
7405+
.write(writer)?;
73677406
for event in background_events.iter() {
73687407
match event {
73697408
BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)) => {
73707409
0u8.write(writer)?;
73717410
funding_txo.write(writer)?;
73727411
monitor_update.write(writer)?;
73737412
},
7413+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(_) => { },
73747414
}
73757415
}
73767416

@@ -7819,8 +7859,9 @@ where
78197859
0 => {
78207860
let (funding_txo, monitor_update): (OutPoint, ChannelMonitorUpdate) = (Readable::read(reader)?, Readable::read(reader)?);
78217861
if pending_background_events.iter().find(|e| {
7822-
let BackgroundEvent::ClosingMonitorUpdate((pending_funding_txo, pending_monitor_update)) = e;
7823-
*pending_funding_txo == funding_txo && *pending_monitor_update == monitor_update
7862+
if let BackgroundEvent::ClosingMonitorUpdate((pending_funding_txo, pending_monitor_update)) = e {
7863+
*pending_funding_txo == funding_txo && *pending_monitor_update == monitor_update
7864+
} else { false }
78247865
}).is_none() {
78257866
pending_background_events.push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)));
78267867
}

0 commit comments

Comments
 (0)