Merge pull request #47 from ariard/block_disconnected_close_chan
[rust-lightning] / src / util / test_utils.rs
1 use chain::chaininterface;
2 use chain::chaininterface::ConfirmationTarget;
3 use chain::transaction::OutPoint;
4 use ln::channelmonitor;
5
6 use bitcoin::blockdata::transaction::Transaction;
7
8 use std::sync::{Arc,Mutex};
9
10 pub struct TestFeeEstimator {
11         pub sat_per_vbyte: u64,
12 }
13 impl chaininterface::FeeEstimator for TestFeeEstimator {
14         fn get_est_sat_per_vbyte(&self, _confirmation_target: ConfirmationTarget) -> u64 {
15                 self.sat_per_vbyte
16         }
17 }
18
19 pub struct TestChannelMonitor {
20         pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
21         pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
22 }
23 impl TestChannelMonitor {
24         pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
25                 Self {
26                         added_monitors: Mutex::new(Vec::new()),
27                         simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
28                 }
29         }
30 }
31 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
32         fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
33                 // At every point where we get a monitor update, we should be able to send a useful monitor
34                 // to a watchtower and disk...
35                 assert!(channelmonitor::ChannelMonitor::deserialize(&monitor.serialize_for_disk()[..]).unwrap() == monitor);
36                 monitor.serialize_for_watchtower(); // This at least shouldn't crash...
37                 self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
38                 self.simple_monitor.add_update_monitor(funding_txo, monitor)
39         }
40 }
41
42 pub struct TestBroadcaster {
43         pub txn_broadcasted: Mutex<Vec<Transaction>>,
44 }
45 impl chaininterface::BroadcasterInterface for TestBroadcaster {
46         fn broadcast_transaction(&self, tx: &Transaction) {
47                 self.txn_broadcasted.lock().unwrap().push(tx.clone());
48         }
49 }