Merge pull request #178 from TheBlueMatt/2018-09-channel_reestablish
[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 use util::logger::{Logger, Level, Record};
9
10 use bitcoin::blockdata::transaction::Transaction;
11
12 use secp256k1::PublicKey;
13
14 use std::sync::{Arc,Mutex};
15 use std::{mem};
16
17 pub struct TestFeeEstimator {
18         pub sat_per_kw: u64,
19 }
20 impl chaininterface::FeeEstimator for TestFeeEstimator {
21         fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
22                 self.sat_per_kw
23         }
24 }
25
26 pub struct TestChannelMonitor {
27         pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
28         pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
29 }
30 impl TestChannelMonitor {
31         pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
32                 Self {
33                         added_monitors: Mutex::new(Vec::new()),
34                         simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
35                 }
36         }
37 }
38 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
39         fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
40                 // At every point where we get a monitor update, we should be able to send a useful monitor
41                 // to a watchtower and disk...
42                 assert!(channelmonitor::ChannelMonitor::deserialize(&monitor.serialize_for_disk()[..]).unwrap() == monitor);
43                 monitor.serialize_for_watchtower(); // This at least shouldn't crash...
44                 self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
45                 self.simple_monitor.add_update_monitor(funding_txo, monitor)
46         }
47 }
48
49 pub struct TestBroadcaster {
50         pub txn_broadcasted: Mutex<Vec<Transaction>>,
51 }
52 impl chaininterface::BroadcasterInterface for TestBroadcaster {
53         fn broadcast_transaction(&self, tx: &Transaction) {
54                 self.txn_broadcasted.lock().unwrap().push(tx.clone());
55         }
56 }
57
58 pub struct TestChannelMessageHandler {
59         pub pending_events: Mutex<Vec<events::Event>>,
60 }
61
62 impl TestChannelMessageHandler {
63         pub fn new() -> Self {
64                 TestChannelMessageHandler {
65                         pending_events: Mutex::new(Vec::new()),
66                 }
67         }
68 }
69
70 impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
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 handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
117                 Err(HandleError { err: "", action: None })
118         }
119         fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
120         fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
121                 Vec::new()
122         }
123         fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
124 }
125
126 impl events::EventsProvider for TestChannelMessageHandler {
127         fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
128                 let mut pending_events = self.pending_events.lock().unwrap();
129                 let mut ret = Vec::new();
130                 mem::swap(&mut ret, &mut *pending_events);
131                 ret
132         }
133 }
134
135 pub struct TestRoutingMessageHandler {}
136
137 impl TestRoutingMessageHandler {
138         pub fn new() -> Self {
139                 TestRoutingMessageHandler {}
140         }
141 }
142
143 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
144         fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
145                 Err(HandleError { err: "", action: None })
146         }
147         fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
148                 Err(HandleError { err: "", action: None })
149         }
150         fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
151                 Err(HandleError { err: "", action: None })
152         }
153         fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
154 }
155
156 pub struct TestLogger {
157         level: Level,
158 }
159
160 impl TestLogger {
161         pub fn new() -> TestLogger {
162                 TestLogger {
163                         level: Level::Trace,
164                 }
165         }
166         pub fn enable(&mut self, level: Level) {
167                 self.level = level;
168         }
169 }
170
171 impl Logger for TestLogger {
172         fn log(&self, record: &Record) {
173                 if self.level >= record.level {
174                         println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
175                 }
176         }
177 }