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>>,
42 impl TestChannelMonitor {
43 pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
45 added_monitors: Mutex::new(Vec::new()),
46 simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
50 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
51 fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
52 // At every point where we get a monitor update, we should be able to send a useful monitor
53 // to a watchtower and disk...
54 let mut w = VecWriter(Vec::new());
55 monitor.write_for_disk(&mut w).unwrap();
56 assert!(channelmonitor::ChannelMonitor::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == monitor);
58 monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
59 self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
60 self.simple_monitor.add_update_monitor(funding_txo, monitor)
64 pub struct TestBroadcaster {
65 pub txn_broadcasted: Mutex<Vec<Transaction>>,
67 impl chaininterface::BroadcasterInterface for TestBroadcaster {
68 fn broadcast_transaction(&self, tx: &Transaction) {
69 self.txn_broadcasted.lock().unwrap().push(tx.clone());
73 pub struct TestChannelMessageHandler {
74 pub pending_events: Mutex<Vec<events::Event>>,
77 impl TestChannelMessageHandler {
78 pub fn new() -> Self {
79 TestChannelMessageHandler {
80 pending_events: Mutex::new(Vec::new()),
85 impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
86 fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
87 Err(HandleError { err: "", action: None })
89 fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
90 Err(HandleError { err: "", action: None })
92 fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
93 Err(HandleError { err: "", action: None })
95 fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
96 Err(HandleError { err: "", action: None })
98 fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
99 Err(HandleError { err: "", action: None })
101 fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), HandleError> {
102 Err(HandleError { err: "", action: None })
104 fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {
105 Err(HandleError { err: "", action: None })
107 fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
108 Err(HandleError { err: "", action: None })
110 fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
111 Err(HandleError { err: "", action: None })
113 fn handle_update_fail_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailHTLC) -> Result<Option<msgs::HTLCFailChannelUpdate>, HandleError> {
114 Err(HandleError { err: "", action: None })
116 fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
117 Err(HandleError { err: "", action: None })
119 fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>), HandleError> {
120 Err(HandleError { err: "", action: None })
122 fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) -> Result<Option<msgs::CommitmentUpdate>, HandleError> {
123 Err(HandleError { err: "", action: None })
125 fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
126 Err(HandleError { err: "", action: None })
128 fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
129 Err(HandleError { err: "", action: None })
131 fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
132 Err(HandleError { err: "", action: None })
134 fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
135 fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
138 fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
141 impl events::EventsProvider for TestChannelMessageHandler {
142 fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
143 let mut pending_events = self.pending_events.lock().unwrap();
144 let mut ret = Vec::new();
145 mem::swap(&mut ret, &mut *pending_events);
150 pub struct TestRoutingMessageHandler {}
152 impl TestRoutingMessageHandler {
153 pub fn new() -> Self {
154 TestRoutingMessageHandler {}
158 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
159 fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
160 Err(HandleError { err: "", action: None })
162 fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
163 Err(HandleError { err: "", action: None })
165 fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
166 Err(HandleError { err: "", action: None })
168 fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
171 pub struct TestLogger {
176 pub fn new() -> TestLogger {
181 pub fn enable(&mut self, level: Level) {
186 impl Logger for TestLogger {
187 fn log(&self, record: &Record) {
188 if self.level >= record.level {
189 println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);