Skip to content

Commit a7f185f

Browse files
TheBlueMattvalentinewallace
authored andcommitted
Test + fuzz that Channel{,Monitor} would_broadcast are identical
1 parent 7aa9a94 commit a7f185f

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
135135
self.update_ret.lock().unwrap().clone()
136136
}
137137

138+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool {
139+
self.simple_monitor.get_monitor_would_broadcast(funding_txo, height)
140+
}
141+
138142
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
139143
return self.simple_monitor.get_and_clear_pending_htlcs_updated();
140144
}

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,16 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
31043104
}
31053105
}
31063106
}
3107+
#[cfg(any(test, feature = "fuzztarget"))]
3108+
// In testing/fuzzing, check that the channel's would_broadcast_at_height
3109+
// implementation is always identical to the ChannelMonitor equivalent.
3110+
let funding_initiated = channel.is_funding_initiated();
3111+
#[cfg(any(test, feature = "fuzztarget"))]
3112+
for i in height..height + 144 {
3113+
assert_eq!(funding_initiated && channel.monitor_would_broadcast_at_height(i, &self.logger),
3114+
funding_initiated && self.monitor.get_monitor_would_broadcast(&channel.get_funding_txo().unwrap(), i));
3115+
}
3116+
31073117
if channel.is_funding_initiated() && channel.monitor_would_broadcast_at_height(height, &self.logger) {
31083118
if let Some(short_id) = channel.get_short_channel_id() {
31093119
short_to_id.remove(&short_id);

lightning/src/ln/channelmonitor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
292292
}
293293
}
294294

295+
#[cfg(any(test, feature = "fuzztarget"))]
296+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool {
297+
self.monitors.lock().unwrap().get(funding_txo).unwrap().would_broadcast_at_height(height, &self.logger)
298+
}
299+
295300
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
296301
let mut pending_htlcs_updated = Vec::new();
297302
for chan in self.monitors.lock().unwrap().values_mut() {
@@ -877,6 +882,11 @@ pub trait ManyChannelMonitor: Send + Sync {
877882
/// ChannelMonitors via block_connected may result in FUNDS LOSS.
878883
fn update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
879884

885+
#[cfg(any(test, feature = "fuzztarget"))]
886+
/// Calls would_broadcast_at_height() on the given monitor. Used in testing to check that the
887+
/// ChannelMonitor copy can never get out of sync with the Channel copy.
888+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool;
889+
880890
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
881891
/// with success or failure.
882892
///

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ impl<'a> channelmonitor::ManyChannelMonitor for TestChannelMonitor<'a> {
129129
ret
130130
}
131131

132+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool {
133+
self.simple_monitor.get_monitor_would_broadcast(funding_txo, height)
134+
}
135+
132136
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
133137
return self.simple_monitor.get_and_clear_pending_htlcs_updated();
134138
}

0 commit comments

Comments
 (0)