Skip to content

Commit 9160c9c

Browse files
TheBlueMattvalentinewallace
authored andcommitted
Test + fuzz that Channel{,Monitor} would_broadcast are identical
Also lower the number of block_connected's in chanmon_consistency, because these changes cause the fuzz tests to run much longer than before.
1 parent 95d92fc commit 9160c9c

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 1 deletion
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
}
@@ -323,7 +327,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
323327
posn.push(i + 1);
324328
}
325329
$node.block_connected(&header, 1, &txn, &posn);
326-
for i in 2..100 {
330+
for i in 2..7 {
327331
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
328332
$node.block_connected(&header, i, &Vec::new(), &[0; 0]);
329333
}

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
@@ -293,6 +293,11 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
293293
}
294294
}
295295

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

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

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)