1 use chain::chaininterface;
2 use chain::chaininterface::ConfirmationTarget;
3 use chain::transaction::OutPoint;
4 use ln::channelmonitor;
6 use ln::msgs::{HandleError};
8 use util::logger::{Logger, Level, Record};
9 use util::ser::{Readable, Writer};
11 use bitcoin::blockdata::transaction::Transaction;
13 use secp256k1::PublicKey;
15 use std::sync::{Arc,Mutex};
18 struct VecWriter(Vec<u8>);
19 impl Writer for VecWriter {
20 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
21 self.0.extend_from_slice(buf);
24 fn size_hint(&mut self, size: usize) {
25 self.0.reserve_exact(size);
29 pub struct TestFeeEstimator {
32 impl chaininterface::FeeEstimator for TestFeeEstimator {
33 fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
38 pub struct TestChannelMonitor {
39 pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
40 pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
41 pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
43 impl TestChannelMonitor {
44 pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
46 added_monitors: Mutex::new(Vec::new()),
47 simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
48 update_ret: Mutex::new(Ok(())),
52 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
53 fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
54 // At every point where we get a monitor update, we should be able to send a useful monitor
55 // to a watchtower and disk...
56 let mut w = VecWriter(Vec::new());
57 monitor.write_for_disk(&mut w).unwrap();
58 assert!(channelmonitor::ChannelMonitor::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == monitor);
60 monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
61 self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
62 assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
63 self.update_ret.lock().unwrap().clone()
67 pub struct TestBroadcaster {
68 pub txn_broadcasted: Mutex<Vec<Transaction>>,
70 impl chaininterface::BroadcasterInterface for TestBroadcaster {
71 fn broadcast_transaction(&self, tx: &Transaction) {
72 self.txn_broadcasted.lock().unwrap().push(tx.clone());
76 pub struct TestChannelMessageHandler {
77 pub pending_events: Mutex<Vec<events::MessageSendEvent>>,
80 impl TestChannelMessageHandler {
81 pub fn new() -> Self {
82 TestChannelMessageHandler {
83 pending_events: Mutex::new(Vec::new()),
88 impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
89 fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<(), HandleError> {
90 Err(HandleError { err: "", action: None })
92 fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
93 Err(HandleError { err: "", action: None })
95 fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<(), HandleError> {
96 Err(HandleError { err: "", action: None })
98 fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
99 Err(HandleError { err: "", action: None })
101 fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<(), HandleError> {
102 Err(HandleError { err: "", action: None })
104 fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), HandleError> {
105 Err(HandleError { err: "", action: None })
107 fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {
108 Err(HandleError { err: "", action: None })
110 fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
111 Err(HandleError { err: "", action: None })
113 fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
114 Err(HandleError { err: "", action: None })
116 fn handle_update_fail_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailHTLC) -> Result<(), HandleError> {
117 Err(HandleError { err: "", action: None })
119 fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
120 Err(HandleError { err: "", action: None })
122 fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>), HandleError> {
123 Err(HandleError { err: "", action: None })
125 fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) -> Result<Option<msgs::CommitmentUpdate>, HandleError> {
126 Err(HandleError { err: "", action: None })
128 fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
129 Err(HandleError { err: "", action: None })
131 fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
132 Err(HandleError { err: "", action: None })
134 fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, msgs::RAACommitmentOrder), HandleError> {
135 Err(HandleError { err: "", action: None })
137 fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
138 fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
141 fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
144 impl events::MessageSendEventsProvider for TestChannelMessageHandler {
145 fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
146 let mut pending_events = self.pending_events.lock().unwrap();
147 let mut ret = Vec::new();
148 mem::swap(&mut ret, &mut *pending_events);
153 pub struct TestRoutingMessageHandler {}
155 impl TestRoutingMessageHandler {
156 pub fn new() -> Self {
157 TestRoutingMessageHandler {}
161 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
162 fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
163 Err(HandleError { err: "", action: None })
165 fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
166 Err(HandleError { err: "", action: None })
168 fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
169 Err(HandleError { err: "", action: None })
171 fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
174 pub struct TestLogger {
179 pub fn new() -> TestLogger {
184 pub fn enable(&mut self, level: Level) {
189 impl Logger for TestLogger {
190 fn log(&self, record: &Record) {
191 if self.level >= record.level {
192 println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);