Ensure we always at least log at TRACE when a peer disconnects
[rust-lightning] / lightning / src / ln / peer_handler.rs
index ec7500190d7ec3b062636fca716e71805ffddb05..234b509564dc2b7440eb4d1f0fd1028cf995d0a6 100644 (file)
@@ -31,13 +31,14 @@ use util::logger::Logger;
 use routing::network_graph::NetGraphMsgHandler;
 
 use prelude::*;
+use io;
 use alloc::collections::LinkedList;
 use alloc::fmt::Debug;
 use sync::{Arc, Mutex};
 use core::sync::atomic::{AtomicUsize, Ordering};
 use core::{cmp, hash, fmt, mem};
 use core::ops::Deref;
-use std::error;
+#[cfg(feature = "std")] use std::error;
 
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::sha256::HashEngine as Sha256Engine;
@@ -230,6 +231,8 @@ impl fmt::Display for PeerHandleError {
                formatter.write_str("Peer Sent Invalid Data")
        }
 }
+
+#[cfg(feature = "std")]
 impl error::Error for PeerHandleError {
        fn description(&self) -> &str {
                "Peer Sent Invalid Data"
@@ -664,6 +667,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
                match self.do_read_event(peer_descriptor, data) {
                        Ok(res) => Ok(res),
                        Err(e) => {
+                               log_trace!(self.logger, "Peer sent invalid data or we decided to disconnect due to a protocol error");
                                self.disconnect_event_internal(peer_descriptor, e.no_connection_possible);
                                Err(e)
                        }
@@ -801,7 +805,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
                                                                                peer.pending_read_buffer = [0; 18].to_vec();
                                                                                peer.pending_read_is_header = true;
 
-                                                                               let mut reader = ::std::io::Cursor::new(&msg_data[..]);
+                                                                               let mut reader = io::Cursor::new(&msg_data[..]);
                                                                                let message_result = wire::read(&mut reader);
                                                                                let message = match message_result {
                                                                                        Ok(x) => x,
@@ -1341,6 +1345,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
                        Some(peer) => {
                                match peer.their_node_id {
                                        Some(node_id) => {
+                                               log_trace!(self.logger,
+                                                       "Handling disconnection of peer {}, with {}future connection to the peer possible.",
+                                                       log_pubkey!(node_id), if no_connection_possible { "no " } else { "" });
                                                peers.node_id_to_descriptor.remove(&node_id);
                                                self.message_handler.chan_handler.peer_disconnected(&node_id, no_connection_possible);
                                        },
@@ -1369,9 +1376,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
                }
        }
 
-       /// This function should be called roughly once every 30 seconds.
-       /// It will send pings to each peer and disconnect those which did not respond to the last
-       /// round of pings.
+       /// Send pings to each peer and disconnect those which did not respond to the last round of
+       /// pings.
+       ///
+       /// This may be called on any timescale you want, however, roughly once every five to ten
+       /// seconds is preferred. The call rate determines both how often we send a ping to our peers
+       /// and how much time they have to respond before we disconnect them.
        ///
        /// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
        /// issues!