Fix channelmonitor fuzz test failure
[rust-lightning] / src / util / test_utils.rs
index beb50940e45dbedb7548c1b1dbfc4acf76f64c8f..6647020f6b3d266d9b683796e9c76e1d75f13333 100644 (file)
@@ -1,13 +1,11 @@
 use chain::chaininterface;
 use chain::chaininterface::ConfirmationTarget;
+use chain::transaction::OutPoint;
 use ln::channelmonitor;
-use ln::msgs::HandleError;
 
-use bitcoin::util::hash::Sha256dHash;
 use bitcoin::blockdata::transaction::Transaction;
-use bitcoin::blockdata::script::Script;
 
-use std::sync::Weak;
+use std::sync::{Arc,Mutex};
 
 pub struct TestFeeEstimator {
        pub sat_per_vbyte: u64,
@@ -18,40 +16,34 @@ impl chaininterface::FeeEstimator for TestFeeEstimator {
        }
 }
 
-pub struct TestWatchInterface {
-       pub watch_util: chaininterface::ChainWatchInterfaceUtil,
+pub struct TestChannelMonitor {
+       pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
+       pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
 }
-impl chaininterface::ChainWatchInterface for TestWatchInterface {
-       fn install_watch_script(&self, _script_pub_key: Script) {
-               unimplemented!();
-       }
-       fn install_watch_outpoint(&self, _outpoint: (Sha256dHash, u32)) {
-               unimplemented!();
-       }
-       fn watch_all_txn(&self) {
-               unimplemented!();
-       }
-       fn broadcast_transaction(&self, _tx: &Transaction) {
-               unimplemented!();
-       }
-       fn register_listener(&self, listener: Weak<chaininterface::ChainListener>) {
-               self.watch_util.register_listener(listener);
+impl TestChannelMonitor {
+       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
+               Self {
+                       added_monitors: Mutex::new(Vec::new()),
+                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
+               }
        }
 }
-impl TestWatchInterface {
-       pub fn new() -> TestWatchInterface {
-               TestWatchInterface {
-                       watch_util: chaininterface::ChainWatchInterfaceUtil::new(),
-               }
+impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
+       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
+               // At every point where we get a monitor update, we should be able to send a useful monitor
+               // to a watchtower and disk...
+               assert!(channelmonitor::ChannelMonitor::deserialize(&monitor.serialize_for_disk()[..]).unwrap() == monitor);
+               monitor.serialize_for_watchtower(); // This at least shouldn't crash...
+               self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
+               self.simple_monitor.add_update_monitor(funding_txo, monitor)
        }
 }
 
-pub struct TestChannelMonitor {
-
+pub struct TestBroadcaster {
+       pub txn_broadcasted: Mutex<Vec<Transaction>>,
 }
-impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
-       fn add_update_monitor(&self, _funding_txo: (Sha256dHash, u16), _monitor: channelmonitor::ChannelMonitor) -> Result<(), HandleError> {
-               //TODO!
-               Ok(())
+impl chaininterface::BroadcasterInterface for TestBroadcaster {
+       fn broadcast_transaction(&self, tx: &Transaction) {
+               self.txn_broadcasted.lock().unwrap().push(tx.clone());
        }
 }