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};
10 use bitcoin::blockdata::transaction::Transaction;
12 use secp256k1::PublicKey;
14 use std::sync::{Arc,Mutex};
17 pub struct TestFeeEstimator {
20 impl chaininterface::FeeEstimator for TestFeeEstimator {
21 fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
26 pub struct TestChannelMonitor {
27 pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
28 pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
30 impl TestChannelMonitor {
31 pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
33 added_monitors: Mutex::new(Vec::new()),
34 simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
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)
49 pub struct TestBroadcaster {
50 pub txn_broadcasted: Mutex<Vec<Transaction>>,
52 impl chaininterface::BroadcasterInterface for TestBroadcaster {
53 fn broadcast_transaction(&self, tx: &Transaction) {
54 self.txn_broadcasted.lock().unwrap().push(tx.clone());
58 pub struct TestChannelMessageHandler {
59 pub pending_events: Mutex<Vec<events::Event>>,
62 impl TestChannelMessageHandler {
63 pub fn new() -> Self {
64 TestChannelMessageHandler {
65 pending_events: Mutex::new(Vec::new()),
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 })
74 fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
75 Err(HandleError { err: "", action: None })
77 fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
78 Err(HandleError { err: "", action: None })
80 fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
81 Err(HandleError { err: "", action: None })
83 fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
84 Err(HandleError { err: "", action: None })
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 })
89 fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {
90 Err(HandleError { err: "", action: None })
92 fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
93 Err(HandleError { err: "", action: None })
95 fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
96 Err(HandleError { err: "", action: None })
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 })
101 fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
102 Err(HandleError { err: "", action: None })
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 })
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 })
110 fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
111 Err(HandleError { err: "", action: None })
113 fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
114 Err(HandleError { err: "", action: None })
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 })
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> {
123 fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
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);
135 pub struct TestRoutingMessageHandler {}
137 impl TestRoutingMessageHandler {
138 pub fn new() -> Self {
139 TestRoutingMessageHandler {}
143 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
144 fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
145 Err(HandleError { err: "", action: None })
147 fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
148 Err(HandleError { err: "", action: None })
150 fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
151 Err(HandleError { err: "", action: None })
153 fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
156 pub struct TestLogger {
161 pub fn new() -> TestLogger {
166 pub fn enable(&mut self, level: Level) {
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);