Skip to content

Commit 1bf5a51

Browse files
committed
Handle BackgroundEvents replaying non-closing monitor updates
`BackgroundEvent` was 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`.
1 parent 87fc313 commit 1bf5a51

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ enum BackgroundEvent {
509509
/// Note that any such events are lost on shutdown, so in general they must be updates which
510510
/// are regenerated on startup.
511511
ClosingMonitorUpdateRegeneratedOnStartup((OutPoint, ChannelMonitorUpdate)),
512-
/// Handle a ChannelMonitorUpdate.
512+
/// Handle a ChannelMonitorUpdate. This may or may not close the channel and may unblock the
513+
/// channel to continue normal operation.
513514
///
514515
/// Note that any such events are lost on shutdown, so in general they must be updates which
515516
/// are regenerated on startup.
@@ -3787,10 +3788,33 @@ where
37873788
// monitor updating completing.
37883789
let _ = self.chain_monitor.update_channel(funding_txo, &update);
37893790
},
3790-
BackgroundEvent::MonitorUpdateRegeneratedOnStartup((_, funding_txo, update)) => {
3791-
// The channel has already been closed, so no use bothering to care about the
3792-
// monitor updating completing.
3793-
let _ = self.chain_monitor.update_channel(funding_txo, &update);
3791+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup((counterparty_id, funding_txo, update)) => {
3792+
let update_res = self.chain_monitor.update_channel(funding_txo, &update);
3793+
3794+
let mut found_chan = false;
3795+
let res = {
3796+
let per_peer_state = self.per_peer_state.read().unwrap();
3797+
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_id) {
3798+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3799+
let peer_state = &mut *peer_state_lock;
3800+
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
3801+
hash_map::Entry::Occupied(mut chan) => {
3802+
found_chan = true;
3803+
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan)
3804+
},
3805+
hash_map::Entry::Vacant(_) => {
3806+
Ok(())
3807+
},
3808+
}
3809+
} else { Ok(()) }
3810+
};
3811+
let _ = handle_error!(self, res, counterparty_id);
3812+
if !found_chan {
3813+
// TODO: If this channel has since closed, we're likely providing a payment
3814+
// preimage update, which we must ensure is durable! We currently don't,
3815+
// however, ensure that, and when we have a strategy therefor we should
3816+
// apply it here.
3817+
}
37943818
},
37953819
}
37963820
}

0 commit comments

Comments
 (0)