Skip to content

Commit 5e11836

Browse files
committed
Test that we don't forget to track any outputs at monitor-load
This tests, after each functional test, that if we serialize and reload all of our ChannelMonitors we end up tracking the same set of outputs as before.
1 parent 45825f5 commit 5e11836

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lightning/src/chain/chaininterface.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub trait FeeEstimator: Sync + Send {
121121
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
122122

123123
/// Utility for tracking registered txn/outpoints and checking for matches
124+
#[cfg_attr(test, derive(PartialEq))]
124125
pub struct ChainWatchedUtil {
125126
watch_all: bool,
126127

@@ -302,6 +303,17 @@ pub struct ChainWatchInterfaceUtil {
302303
logger: Arc<Logger>,
303304
}
304305

306+
// We only expose PartialEq in test since its somewhat unclear exactly what it should do and we're
307+
// only comparing a subset of fields (essentially just checking that the set of things we're
308+
// watching is the same).
309+
#[cfg(test)]
310+
impl PartialEq for ChainWatchInterfaceUtil {
311+
fn eq(&self, o: &Self) -> bool {
312+
self.network == o.network &&
313+
*self.watched.lock().unwrap() == *o.watched.lock().unwrap()
314+
}
315+
}
316+
305317
/// Register listener
306318
impl ChainWatchInterface for ChainWatchInterfaceUtil {
307319
fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script) {

lightning/src/ln/functional_test_utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use chain::chaininterface;
55
use chain::transaction::OutPoint;
66
use chain::keysinterface::KeysInterface;
7+
use ln::channelmonitor::{ChannelMonitor, ManyChannelMonitor};
78
use ln::channelmanager::{ChannelManager,RAACommitmentOrder, PaymentPreimage, PaymentHash};
89
use ln::router::{Route, Router};
910
use ln::features::InitFeatures;
@@ -16,6 +17,7 @@ use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsPro
1617
use util::errors::APIError;
1718
use util::logger::Logger;
1819
use util::config::UserConfig;
20+
use util::ser::ReadableArgs;
1921

2022
use bitcoin::util::hash::BitcoinHash;
2123
use bitcoin::blockdata::block::BlockHeader;
@@ -88,6 +90,27 @@ impl<'a, 'b> Drop for Node<'a, 'b> {
8890
assert!(self.node.get_and_clear_pending_msg_events().is_empty());
8991
assert!(self.node.get_and_clear_pending_events().is_empty());
9092
assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
93+
94+
// Check that if we serialize and then deserialize all our channel monitors we the same
95+
// set of outputs to watch for on chain as we have now. Note that if we write tests
96+
// that fully close channels and remove the monitors at some point this may break.
97+
let new_watch = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&self.logger) as Arc<Logger>));
98+
let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
99+
let new_monitor = test_utils::TestChannelMonitor::new(new_watch.clone(), self.tx_broadcaster.clone(), self.logger.clone(), feeest);
100+
let old_monitors = self.chan_monitor.simple_monitor.monitors.lock().unwrap();
101+
for (_, monitor) in old_monitors.iter() {
102+
let mut w = test_utils::TestVecWriter(Vec::new());
103+
monitor.write_for_disk(&mut w).unwrap();
104+
let (_, new_mon) = <(Sha256d, ChannelMonitor<EnforcingChannelKeys>)>::read(
105+
&mut ::std::io::Cursor::new(&w.0), Arc::clone(&self.logger) as Arc<Logger>).unwrap();
106+
if let Err(_) = new_monitor.add_update_monitor(new_mon.get_funding_txo().unwrap(), new_mon) {
107+
panic!();
108+
}
109+
}
110+
111+
if *new_watch != *self.chain_monitor {
112+
panic!();
113+
}
91114
}
92115
}
93116
}

0 commit comments

Comments
 (0)