Skip to content

Commit 7e345e5

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 24c1d63 commit 7e345e5

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,13 @@ 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 which may or may not close the channel. In general this
513-
/// should be used rather than [`Self::ClosingMonitorUpdateRegeneratedOnStartup`], however in
514-
/// cases where the `counterparty_node_id` is not available as the channel has closed from a
515-
/// [`ChannelMonitor`] error the other variant is acceptable.
512+
/// Handle a ChannelMonitorUpdate which may or may not close the channel and may unblock the
513+
/// channel to continue normal operation.
514+
///
515+
/// In general this should be used rather than
516+
/// [`Self::ClosingMonitorUpdateRegeneratedOnStartup`], however in cases where the
517+
/// `counterparty_node_id` is not available as the channel has closed from a [`ChannelMonitor`]
518+
/// error the other variant is acceptable.
516519
///
517520
/// Note that any such events are lost on shutdown, so in general they must be updates which
518521
/// are regenerated on startup.
@@ -3794,10 +3797,33 @@ where
37943797
// monitor updating completing.
37953798
let _ = self.chain_monitor.update_channel(funding_txo, &update);
37963799
},
3797-
BackgroundEvent::MonitorUpdateRegeneratedOnStartup { funding_txo, update, .. } => {
3798-
// The channel has already been closed, so no use bothering to care about the
3799-
// monitor updating completing.
3800-
let _ = self.chain_monitor.update_channel(funding_txo, &update);
3800+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup { counterparty_node_id, funding_txo, update } => {
3801+
let update_res = self.chain_monitor.update_channel(funding_txo, &update);
3802+
3803+
let mut found_chan = false;
3804+
let res = {
3805+
let per_peer_state = self.per_peer_state.read().unwrap();
3806+
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
3807+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3808+
let peer_state = &mut *peer_state_lock;
3809+
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
3810+
hash_map::Entry::Occupied(mut chan) => {
3811+
found_chan = true;
3812+
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan)
3813+
},
3814+
hash_map::Entry::Vacant(_) => {
3815+
Ok(())
3816+
},
3817+
}
3818+
} else { Ok(()) }
3819+
};
3820+
let _ = handle_error!(self, res, counterparty_node_id);
3821+
if !found_chan {
3822+
// TODO: If this channel has since closed, we're likely providing a payment
3823+
// preimage update, which we must ensure is durable! We currently don't,
3824+
// however, ensure that, and when we have a strategy therefor we should
3825+
// apply it here.
3826+
}
38013827
},
38023828
}
38033829
}

0 commit comments

Comments
 (0)