Skip to content

Commit 98ea9b6

Browse files
Check for dangling monitor events in tests
We want to make sure we don't leak any monitor events by leaving them unacked forever.
1 parent e105065 commit 98ea9b6

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

lightning/src/chain/channelmonitor.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn push_monitor_event(
194194
}
195195

196196
/// An event to be processed by the ChannelManager.
197-
#[derive(Clone, PartialEq, Eq)]
197+
#[derive(Clone, PartialEq, Eq, Debug)]
198198
pub enum MonitorEvent {
199199
/// A monitor event containing an HTLCUpdate.
200200
HTLCEvent(HTLCUpdate),
@@ -256,7 +256,7 @@ impl_writeable_tlv_based_enum_upgradable_legacy!(MonitorEvent,
256256
/// Simple structure sent back by `chain::Watch` when an HTLC from a forward channel is detected on
257257
/// chain. Used to update the corresponding HTLC in the backward channel. Failing to pass the
258258
/// preimage claim backward will lead to loss of funds.
259-
#[derive(Clone, PartialEq, Eq)]
259+
#[derive(Clone, PartialEq, Eq, Debug)]
260260
pub struct HTLCUpdate {
261261
pub(crate) payment_hash: PaymentHash,
262262
pub(crate) payment_preimage: Option<PaymentPreimage>,
@@ -2272,6 +2272,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22722272
self_inner.next_monitor_event_id = next_id;
22732273
}
22742274

2275+
/// Used by test infra to check for monitor event leaks at the end of a test.
2276+
#[cfg(any(test, feature = "_test_utils"))]
2277+
pub fn drain_unacked_monitor_events(&self) -> Vec<(u64, MonitorEvent)> {
2278+
let mut inner = self.inner.lock().unwrap();
2279+
let mut events = Vec::new();
2280+
events.append(&mut inner.pending_monitor_events);
2281+
events.append(&mut inner.provided_monitor_events);
2282+
events
2283+
}
2284+
22752285
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
22762286
///
22772287
/// For channels featuring anchor outputs, this method will also process [`BumpTransaction`]

lightning/src/ln/functional_test_utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,18 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
801801
panic!("Had excess RAA blockers on node {}: {:?}", self.logger.id, raa_blockers);
802802
}
803803

804+
for channel_id in self.chain_monitor.chain_monitor.list_monitors() {
805+
let monitor = self.chain_monitor.chain_monitor.get_monitor(channel_id).unwrap();
806+
let unacked_monitor_events = monitor.drain_unacked_monitor_events();
807+
if !unacked_monitor_events.is_empty() {
808+
panic!(
809+
"Node {} channel {channel_id:?} had {} unacked monitor events at drop: {unacked_monitor_events:#?}",
810+
unacked_monitor_events.len(),
811+
self.logger.id
812+
);
813+
}
814+
}
815+
804816
// Check that if we serialize the network graph, we can deserialize it again.
805817
let network_graph = {
806818
let mut w = test_utils::TestVecWriter(Vec::new());

0 commit comments

Comments
 (0)