Move ChannelMonitor deserialization to new ser framework
[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 use util::ser::Readable;
10
11 use bitcoin::blockdata::transaction::Transaction;
12
13 use secp256k1::PublicKey;
14
15 use std::sync::{Arc,Mutex};
16 use std::{mem};
17
18 pub struct TestFeeEstimator {
19         pub sat_per_kw: u64,
20 }
21 impl chaininterface::FeeEstimator for TestFeeEstimator {
22         fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
23                 self.sat_per_kw
24         }
25 }
26
27 pub struct TestChannelMonitor {
28         pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
29         pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
30 }
31 impl TestChannelMonitor {
32         pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
33                 Self {
34                         added_monitors: Mutex::new(Vec::new()),
35                         simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
36                 }
37         }
38 }
39 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
40         fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
41                 // At every point where we get a monitor update, we should be able to send a useful monitor
42                 // to a watchtower and disk...
43                 assert!(channelmonitor::ChannelMonitor::read(&mut ::std::io::Cursor::new(&monitor.serialize_for_disk()[..])).unwrap() == monitor);
44                 monitor.serialize_for_watchtower(); // This at least shouldn't crash...
45                 self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
46                 self.simple_monitor.add_update_monitor(funding_txo, monitor)
47         }
48 }
49
50 pub struct TestBroadcaster {
51         pub txn_broadcasted: Mutex<Vec<Transaction>>,
52 }
53 impl chaininterface::BroadcasterInterface for TestBroadcaster {
54         fn broadcast_transaction(&self, tx: &Transaction) {
55                 self.txn_broadcasted.lock().unwrap().push(tx.clone());
56         }
57 }
58
59 pub struct TestChannelMessageHandler {
60         pub pending_events: Mutex<Vec<events::Event>>,
61 }
62
63 impl TestChannelMessageHandler {
64         pub fn new() -> Self {
65                 TestChannelMessageHandler {
66                         pending_events: Mutex::new(Vec::new()),
67                 }
68         }
69 }
70
71 impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
72         fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
73                 Err(HandleError { err: "", action: None })
74         }
75         fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
76                 Err(HandleError { err: "", action: None })
77         }
78         fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
79                 Err(HandleError { err: "", action: None })
80         }
81         fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
82                 Err(HandleError { err: "", action: None })
83         }
84         fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
85                 Err(HandleError { err: "", action: None })
86         }
87         fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), HandleError> {
88                 Err(HandleError { err: "", action: None })
89         }
90         fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {
91                 Err(HandleError { err: "", action: None })
92         }
93         fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
94                 Err(HandleError { err: "", action: None })
95         }
96         fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
97                 Err(HandleError { err: "", action: None })
98         }
99         fn handle_update_fail_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailHTLC) -> Result<Option<msgs::HTLCFailChannelUpdate>, HandleError> {
100                 Err(HandleError { err: "", action: None })
101         }
102         fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
103                 Err(HandleError { err: "", action: None })
104         }
105         fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>), HandleError> {
106                 Err(HandleError { err: "", action: None })
107         }
108         fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) -> Result<Option<msgs::CommitmentUpdate>, HandleError> {
109                 Err(HandleError { err: "", action: None })
110         }
111         fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
112                 Err(HandleError { err: "", action: None })
113         }
114         fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
115                 Err(HandleError { err: "", action: None })
116         }
117         fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
118                 Err(HandleError { err: "", action: None })
119         }
120         fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
121         fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
122                 Vec::new()
123         }
124         fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
125 }
126
127 impl events::EventsProvider for TestChannelMessageHandler {
128         fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
129                 let mut pending_events = self.pending_events.lock().unwrap();
130                 let mut ret = Vec::new();
131                 mem::swap(&mut ret, &mut *pending_events);
132                 ret
133         }
134 }
135
136 pub struct TestRoutingMessageHandler {}
137
138 impl TestRoutingMessageHandler {
139         pub fn new() -> Self {
140                 TestRoutingMessageHandler {}
141         }
142 }
143
144 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
145         fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
146                 Err(HandleError { err: "", action: None })
147         }
148         fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
149                 Err(HandleError { err: "", action: None })
150         }
151         fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
152                 Err(HandleError { err: "", action: None })
153         }
154         fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
155 }
156
157 pub struct TestLogger {
158         level: Level,
159 }
160
161 impl TestLogger {
162         pub fn new() -> TestLogger {
163                 TestLogger {
164                         level: Level::Trace,
165                 }
166         }
167         pub fn enable(&mut self, level: Level) {
168                 self.level = level;
169         }
170 }
171
172 impl Logger for TestLogger {
173         fn log(&self, record: &Record) {
174                 if self.level >= record.level {
175                         println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
176                 }
177         }
178 }