Merge pull request #78 from TheBlueMatt/2018-07-43-rebased
[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 use ln::msgs;
6 use ln::msgs::{HandleError};
7 use util::events;
8
9 use bitcoin::blockdata::transaction::Transaction;
10
11 use secp256k1::PublicKey;
12
13 use std::sync::{Arc,Mutex};
14 use std::{mem};
15
16 pub struct TestFeeEstimator {
17         pub sat_per_vbyte: u64,
18 }
19 impl chaininterface::FeeEstimator for TestFeeEstimator {
20         fn get_est_sat_per_vbyte(&self, _confirmation_target: ConfirmationTarget) -> u64 {
21                 self.sat_per_vbyte
22         }
23 }
24
25 pub struct TestChannelMonitor {
26         pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
27         pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
28 }
29 impl TestChannelMonitor {
30         pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
31                 Self {
32                         added_monitors: Mutex::new(Vec::new()),
33                         simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
34                 }
35         }
36 }
37 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
38         fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
39                 // At every point where we get a monitor update, we should be able to send a useful monitor
40                 // to a watchtower and disk...
41                 assert!(channelmonitor::ChannelMonitor::deserialize(&monitor.serialize_for_disk()[..]).unwrap() == monitor);
42                 monitor.serialize_for_watchtower(); // This at least shouldn't crash...
43                 self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
44                 self.simple_monitor.add_update_monitor(funding_txo, monitor)
45         }
46 }
47
48 pub struct TestBroadcaster {
49         pub txn_broadcasted: Mutex<Vec<Transaction>>,
50 }
51 impl chaininterface::BroadcasterInterface for TestBroadcaster {
52         fn broadcast_transaction(&self, tx: &Transaction) {
53                 self.txn_broadcasted.lock().unwrap().push(tx.clone());
54         }
55 }
56
57 pub struct TestChannelMessageHandler {
58         pub pending_events: Mutex<Vec<events::Event>>,
59 }
60
61 impl TestChannelMessageHandler {
62         pub fn new() -> Self {
63                 TestChannelMessageHandler {
64                         pending_events: Mutex::new(Vec::new()),
65                 }
66         }
67 }
68
69 impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
70
71         fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
72                 Err(HandleError { err: "", action: None })
73         }
74         fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
75                 Err(HandleError { err: "", action: None })
76         }
77         fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
78                 Err(HandleError { err: "", action: None })
79         }
80         fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
81                 Err(HandleError { err: "", action: None })
82         }
83         fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
84                 Err(HandleError { err: "", action: None })
85         }
86         fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), HandleError> {
87                 Err(HandleError { err: "", action: None })
88         }
89         fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {
90                 Err(HandleError { err: "", action: None })
91         }
92         fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
93                 Err(HandleError { err: "", action: None })
94         }
95         fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
96                 Err(HandleError { err: "", action: None })
97         }
98         fn handle_update_fail_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailHTLC) -> Result<Option<msgs::HTLCFailChannelUpdate>, HandleError> {
99                 Err(HandleError { err: "", action: None })
100         }
101         fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
102                 Err(HandleError { err: "", action: None })
103         }
104         fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>), HandleError> {
105                 Err(HandleError { err: "", action: None })
106         }
107         fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) -> Result<Option<msgs::CommitmentUpdate>, HandleError> {
108                 Err(HandleError { err: "", action: None })
109         }
110         fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
111                 Err(HandleError { err: "", action: None })
112         }
113         fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
114                 Err(HandleError { err: "", action: None })
115         }
116         fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
117 }
118
119 impl events::EventsProvider for TestChannelMessageHandler {
120         fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
121                 let mut pending_events = self.pending_events.lock().unwrap();
122                 let mut ret = Vec::new();
123                 mem::swap(&mut ret, &mut *pending_events);
124                 ret
125         }
126 }
127
128 pub struct TestRoutingMessageHandler {}
129
130 impl TestRoutingMessageHandler {
131         pub fn new() -> Self {
132                 TestRoutingMessageHandler {}
133         }
134 }
135
136 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
137         fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<(), HandleError> {
138                 Err(HandleError { err: "", action: None })
139         }
140         fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
141                 Err(HandleError { err: "", action: None })
142         }
143         fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<(), HandleError> {
144                 Err(HandleError { err: "", action: None })
145         }
146         fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
147 }