Merge pull request #269 from TheBlueMatt/2018-12-198-review
[rust-lightning] / src / util / test_utils.rs
index 4fa29fd8e95c94b3a52850dc5867aa4f0f1caed0..3acb0d02e78e2bdec6214dd82b5ea117e341d16d 100644 (file)
@@ -4,11 +4,13 @@ use chain::transaction::OutPoint;
 use ln::channelmonitor;
 use ln::msgs;
 use ln::msgs::{HandleError};
+use ln::channelmonitor::HTLCUpdate;
 use util::events;
 use util::logger::{Logger, Level, Record};
-use util::ser::{Readable, Writer};
+use util::ser::{ReadableArgs, Writer};
 
 use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::util::hash::Sha256dHash;
 
 use secp256k1::PublicKey;
 
@@ -41,10 +43,10 @@ pub struct TestChannelMonitor {
        pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
 }
 impl TestChannelMonitor {
-       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
+       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>) -> Self {
                Self {
                        added_monitors: Mutex::new(Vec::new()),
-                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
+                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger),
                        update_ret: Mutex::new(Ok(())),
                }
        }
@@ -55,13 +57,18 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
                // to a watchtower and disk...
                let mut w = VecWriter(Vec::new());
                monitor.write_for_disk(&mut w).unwrap();
-               assert!(channelmonitor::ChannelMonitor::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == monitor);
+               assert!(<(Sha256dHash, channelmonitor::ChannelMonitor)>::read(
+                               &mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1 == monitor);
                w.0.clear();
                monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
                self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
                assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
                self.update_ret.lock().unwrap().clone()
        }
+
+       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
+               return self.simple_monitor.fetch_pending_htlc_updated();
+       }
 }
 
 pub struct TestBroadcaster {
@@ -155,7 +162,6 @@ impl TestRoutingMessageHandler {
                TestRoutingMessageHandler {}
        }
 }
-
 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
        fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
                Err(HandleError { err: "", action: None })
@@ -167,16 +173,27 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
                Err(HandleError { err: "", action: None })
        }
        fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
+       fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
+               Vec::new()
+       }
+       fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
+               Vec::new()
+       }
 }
 
 pub struct TestLogger {
        level: Level,
+       id: String,
 }
 
 impl TestLogger {
        pub fn new() -> TestLogger {
+               Self::with_id("".to_owned())
+       }
+       pub fn with_id(id: String) -> TestLogger {
                TestLogger {
                        level: Level::Trace,
+                       id,
                }
        }
        pub fn enable(&mut self, level: Level) {
@@ -187,7 +204,7 @@ impl TestLogger {
 impl Logger for TestLogger {
        fn log(&self, record: &Record) {
                if self.level >= record.level {
-                       println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
+                       println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
                }
        }
 }