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;
11 use bitcoin::blockdata::transaction::Transaction;
13 use secp256k1::PublicKey;
15 use std::sync::{Arc,Mutex};
18 pub struct TestFeeEstimator {
21 impl chaininterface::FeeEstimator for TestFeeEstimator {
22 fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
27 pub struct TestChannelMonitor {
28 pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
29 pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
31 impl TestChannelMonitor {
32 pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
34 added_monitors: Mutex::new(Vec::new()),
35 simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
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)
50 pub struct TestBroadcaster {
51 pub txn_broadcasted: Mutex<Vec<Transaction>>,
53 impl chaininterface::BroadcasterInterface for TestBroadcaster {
54 fn broadcast_transaction(&self, tx: &Transaction) {
55 self.txn_broadcasted.lock().unwrap().push(tx.clone());
59 pub struct TestChannelMessageHandler {
60 pub pending_events: Mutex<Vec<events::Event>>,
63 impl TestChannelMessageHandler {
64 pub fn new() -> Self {
65 TestChannelMessageHandler {
66 pending_events: Mutex::new(Vec::new()),
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 })
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 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 })
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> {
124 fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
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);
136 pub struct TestRoutingMessageHandler {}
138 impl TestRoutingMessageHandler {
139 pub fn new() -> Self {
140 TestRoutingMessageHandler {}
144 impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
145 fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
146 Err(HandleError { err: "", action: None })
148 fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
149 Err(HandleError { err: "", action: None })
151 fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
152 Err(HandleError { err: "", action: None })
154 fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
157 pub struct TestLogger {
162 pub fn new() -> TestLogger {
167 pub fn enable(&mut self, level: Level) {
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);