Skip to content

Commit 3125760

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 494bbe5 commit 3125760

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
@@ -119,6 +119,7 @@ pub trait FeeEstimator: Sync + Send {
119119
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
120120

121121
/// Utility for tracking registered txn/outpoints and checking for matches
122+
#[cfg_attr(test, derive(PartialEq))]
122123
pub struct ChainWatchedUtil {
123124
watch_all: bool,
124125

@@ -284,6 +285,17 @@ pub struct ChainWatchInterfaceUtil {
284285
logger: Arc<Logger>,
285286
}
286287

288+
// We only expose PartialEq in test since its somewhat unclear exactly what it should do and we're
289+
// only comparing a subset of fields (essentially just checking that the set of things we're
290+
// watching is the same).
291+
#[cfg(test)]
292+
impl PartialEq for ChainWatchInterfaceUtil {
293+
fn eq(&self, o: &Self) -> bool {
294+
self.network == o.network &&
295+
*self.watched.lock().unwrap() == *o.watched.lock().unwrap()
296+
}
297+
}
298+
287299
/// Register listener
288300
impl ChainWatchInterface for ChainWatchInterfaceUtil {
289301
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
@@ -5,6 +5,7 @@ use chain::chaininterface;
55
use chain::transaction::OutPoint;
66
use chain::keysinterface::KeysInterface;
77
use ln::channelmanager::{ChannelManager,RAACommitmentOrder, PaymentPreimage, PaymentHash};
8+
use ln::channelmonitor::{ChannelMonitor, ManyChannelMonitor};
89
use ln::router::{Route, Router};
910
use ln::features::InitFeatures;
1011
use ln::msgs;
@@ -15,6 +16,7 @@ use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsPro
1516
use util::errors::APIError;
1617
use util::logger::Logger;
1718
use util::config::UserConfig;
19+
use util::ser::ReadableArgs;
1820

1921
use bitcoin::util::hash::BitcoinHash;
2022
use bitcoin::blockdata::block::BlockHeader;
@@ -76,6 +78,27 @@ impl Drop for Node {
7678
assert!(self.node.get_and_clear_pending_msg_events().is_empty());
7779
assert!(self.node.get_and_clear_pending_events().is_empty());
7880
assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
81+
82+
// Check that if we serialize and then deserialize all our channel monitors we the same
83+
// set of outputs to watch for on chain as we have now. Note that if we write tests
84+
// that fully close channels and remove the monitors at some point this may break.
85+
let new_watch = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&self.logger) as Arc<Logger>));
86+
let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
87+
let new_monitor = test_utils::TestChannelMonitor::new(new_watch.clone(), self.tx_broadcaster.clone(), self.logger.clone(), feeest);
88+
let old_monitors = self.chan_monitor.simple_monitor.monitors.lock().unwrap();
89+
for (_, monitor) in old_monitors.iter() {
90+
let mut w = test_utils::TestVecWriter(Vec::new());
91+
monitor.write_for_disk(&mut w).unwrap();
92+
let (_, new_mon) = <(Sha256d, ChannelMonitor)>::read(
93+
&mut ::std::io::Cursor::new(&w.0), Arc::clone(&self.logger) as Arc<Logger>).unwrap();
94+
if let Err(_) = new_monitor.add_update_monitor(new_mon.get_funding_txo().unwrap(), new_mon) {
95+
panic!();
96+
}
97+
}
98+
99+
if *new_watch != *self.chain_monitor {
100+
panic!();
101+
}
79102
}
80103
}
81104
}

0 commit comments

Comments
 (0)