Skip to content

Commit 613ff14

Browse files
TheBlueMattvalentinewallace
authored andcommitted
Test + fuzz that Channel{,Monitor} would_broadcast are identical
1 parent 6b693e6 commit 613ff14

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

fuzz/src/chanmon_consistency.rs

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

129+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool {
130+
self.simple_monitor.get_monitor_would_broadcast(funding_txo, height)
131+
}
132+
129133
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
130134
return self.simple_monitor.get_and_clear_pending_htlcs_updated();
131135
}

lightning/src/ln/channelmanager.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,15 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
30443044
}
30453045
}
30463046
}
3047+
#[cfg(any(test, feature = "fuzztarget"))]
3048+
if channel.is_funding_initiated() {
3049+
// In testing/fuzzing, check that the channel's would_broadcast_at_height
3050+
// implementation is always identical to the ChannelMonitor equivalent.
3051+
for i in height..height + 144 {
3052+
assert_eq!(channel.monitor_would_broadcast_at_height(i, &self.logger),
3053+
self.monitor.get_monitor_would_broadcast(&channel.get_funding_txo().unwrap(), i));
3054+
}
3055+
}
30473056
if channel.is_funding_initiated() && channel.monitor_would_broadcast_at_height(height, &self.logger) {
30483057
if let Some(short_id) = channel.get_short_channel_id() {
30493058
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
@@ -285,6 +285,11 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
285285
}
286286
}
287287

288+
#[cfg(any(test, feature = "fuzztarget"))]
289+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool {
290+
self.monitors.lock().unwrap().get(funding_txo).unwrap().would_broadcast_at_height(height, &self.logger)
291+
}
292+
288293
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
289294
let mut pending_htlcs_updated = Vec::new();
290295
for chan in self.monitors.lock().unwrap().values_mut() {
@@ -870,6 +875,11 @@ pub trait ManyChannelMonitor: Send + Sync {
870875
/// ChannelMonitors via block_connected may result in FUNDS LOSS.
871876
fn update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
872877

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

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl<'a> channelmonitor::ManyChannelMonitor for TestChannelMonitor<'a> {
120120
ret
121121
}
122122

123+
fn get_monitor_would_broadcast(&self, funding_txo: &OutPoint, height: u32) -> bool {
124+
self.simple_monitor.get_monitor_would_broadcast(funding_txo, height)
125+
}
126+
123127
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
124128
return self.simple_monitor.get_and_clear_pending_htlcs_updated();
125129
}

0 commit comments

Comments
 (0)