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 {
72 fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
73 Err(HandleError { err: "", action: None })
75 fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
76 Err(HandleError { err: "", action: None })
78 fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
79 Err(HandleError { err: "", action: None })
81 fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
82 Err(HandleError { err: "", action: None })
84 fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
85 Err(HandleError { err: "", action: None })
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 })
90 fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {
91 Err(HandleError { err: "", action: None })
93 fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
94 Err(HandleError { err: "", action: None })
96 fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
97 Err(HandleError { err: "", action: None })
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 })
102 fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
103 Err(HandleError { err: "", action: None })
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 })
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 })
111 fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
112 Err(HandleError { err: "", action: None })
114 fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
115 Err(HandleError { err: "", action: None })
117 fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
118 fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
121 impl events::EventsProvider for TestChannelMessageHandler {
122 fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
123 let mut pending_events = self.pending_events.lock().unwrap();
124 let mut ret = Vec::new();
125 mem::swap(&mut ret, &mut *pending_events);
130 pub struct TestRoutingMessageHandler {}
132 impl TestRoutingMessageHandler {
133 pub fn new() -> Self {
134 TestRoutingMessageHandler {}
138 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
139 fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
140 Err(HandleError { err: "", action: None })
142 fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
143 Err(HandleError { err: "", action: None })
145 fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
146 Err(HandleError { err: "", action: None })
148 fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
151 pub struct TestLogger {
156 pub fn new() -> TestLogger {
161 pub fn enable(&mut self, level: Level) {
166 impl Logger for TestLogger {
167 fn log(&self, record: &Record) {
168 if self.level >= record.level {
169 println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);