PendingHTLCsForwardable really should just be upstreamed.
[rust-lightning] / src / ln / peer_handler.rs
index 9df01489f214071d146f46cdb307180e1d01307e..7e213c037dc6be81ac550cd5b383d75b714d94be 100644 (file)
@@ -8,7 +8,8 @@ use util::events::{EventsProvider,Event};
 
 use std::collections::{HashMap,LinkedList};
 use std::sync::{Arc, Mutex};
-use std::{cmp,mem,hash,fmt};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::{cmp,error,mem,hash,fmt};
 
 pub struct MessageHandler {
        pub chan_handler: Arc<msgs::ChannelMessageHandler>,
@@ -20,7 +21,9 @@ pub struct MessageHandler {
 /// implement Hash to meet the PeerManager API.
 /// For efficiency, Clone should be relatively cheap for this type.
 /// You probably want to just extend an int and put a file descriptor in a struct and implement
-/// send_data.
+/// send_data. Note that if you are using a higher-level net library that may close() itself, be
+/// careful to ensure you don't have races whereby you might register a new connection with an fd
+/// the same as a yet-to-be-disconnect_event()-ed.
 pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
        /// Attempts to send some data from the given Vec starting at the given offset to the peer.
        /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
@@ -34,6 +37,12 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
        /// indicating that read events on this descriptor should resume. A resume_read of false does
        /// *not* imply that further read events should be paused.
        fn send_data(&mut self, data: &Vec<u8>, write_offset: usize, resume_read: bool) -> usize;
+       /// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
+       /// more calls to write_event, read_event or disconnect_event may be made with this descriptor.
+       /// No disconnect_event should be generated as a result of this call, though obviously races
+       /// may occur whereby disconnect_socket is called after a call to disconnect_event but prior to
+       /// that event completing.
+       fn disconnect_socket(&mut self);
 }
 
 /// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
@@ -48,6 +57,16 @@ impl fmt::Debug for PeerHandleError {
                formatter.write_str("Peer Sent Invalid Data")
        }
 }
+impl fmt::Display for PeerHandleError {
+       fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+               formatter.write_str("Peer Sent Invalid Data")
+       }
+}
+impl error::Error for PeerHandleError {
+       fn description(&self) -> &str {
+               "Peer Sent Invalid Data"
+       }
+}
 
 struct Peer {
        channel_encryptor: PeerChannelEncryptor,
@@ -76,6 +95,7 @@ pub struct PeerManager<Descriptor: SocketDescriptor> {
        peers: Mutex<PeerHolder<Descriptor>>,
        pending_events: Mutex<Vec<Event>>,
        our_node_secret: SecretKey,
+       initial_syncs_sent: AtomicUsize,
 }
 
 
@@ -91,6 +111,9 @@ macro_rules! encode_msg {
        }
 }
 
+//TODO: Really should do something smarter for this
+const INITIAL_SYNCS_TO_SEND: usize = 5;
+
 /// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
 /// PeerIds may repeat, but only after disconnect_event() has been called.
 impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
@@ -100,9 +123,19 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                        peers: Mutex::new(PeerHolder { peers: HashMap::new(), node_id_to_descriptor: HashMap::new() }),
                        pending_events: Mutex::new(Vec::new()),
                        our_node_secret: our_node_secret,
+                       initial_syncs_sent: AtomicUsize::new(0),
                }
        }
 
+       /// Get the list of node ids for peers which have completed the initial handshake.
+       /// For outbound connections, this will be the same as the their_node_id parameter passed in to
+       /// new_outbound_connection, however entries will only appear once the initial handshake has
+       /// completed and we are sure the remote peer has the private key for the given node_id.
+       pub fn get_peer_node_ids(&self) -> Vec<PublicKey> {
+               let peers = self.peers.lock().unwrap();
+               peers.peers.values().filter_map(|p| p.their_node_id).collect()
+       }
+
        /// Indicates a new outbound connection has been established to a node with the given node_id.
        /// Note that if an Err is returned here you MUST NOT call disconnect_event for the new
        /// descriptor but must disconnect the connection immediately.
@@ -262,16 +295,23 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                        match $thing {
                                                                                Ok(x) => x,
                                                                                Err(e) => {
-                                                                                       // TODO: Log e.err
-                                                                                       if let Some(action) = e.msg {
+                                                                                       println!("Got error handling message: {}!", e.err);
+                                                                                       if let Some(action) = e.action {
                                                                                                match action {
                                                                                                        msgs::ErrorAction::UpdateFailHTLC { msg } => {
                                                                                                                encode_and_send_msg!(msg, 131);
                                                                                                                continue;
                                                                                                        },
-                                                                                                       msgs::ErrorAction::DisconnectPeer {} => {
+                                                                                                       msgs::ErrorAction::DisconnectPeer { msg: _ } => {
                                                                                                                return Err(PeerHandleError{ no_connection_possible: false });
                                                                                                        },
+                                                                                                       msgs::ErrorAction::IgnoreError => {
+                                                                                                               continue;
+                                                                                                       },
+                                                                                                       msgs::ErrorAction::SendErrorMessage { msg } => {
+                                                                                                               encode_and_send_msg!(msg, 17);
+                                                                                                               continue;
+                                                                                                       },
                                                                                                }
                                                                                        } else {
                                                                                                return Err(PeerHandleError{ no_connection_possible: false });
@@ -286,6 +326,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                        match $thing {
                                                                                Ok(x) => x,
                                                                                Err(_e) => {
+                                                                                       println!("Error decoding message");
                                                                                        //TODO: Handle e?
                                                                                        return Err(PeerHandleError{ no_connection_possible: false });
                                                                                }
@@ -293,6 +334,18 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                }
                                                        }
 
+                                                       macro_rules! try_ignore_potential_decodeerror {
+                                                               ($thing: expr) => {
+                                                                       match $thing {
+                                                                               Ok(x) => x,
+                                                                               Err(_e) => {
+                                                                                       println!("Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
+                                                                                       continue;
+                                                                               }
+                                                                       };
+                                                               }
+                                                       }
+
                                                        let next_step = peer.channel_encryptor.get_noise_step();
                                                        match next_step {
                                                                NextNoiseStep::ActOne => {
@@ -307,9 +360,14 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                        peer.pending_read_is_header = true;
 
                                                                        insert_node_id = Some(peer.their_node_id.unwrap());
+                                                                       let mut local_features = msgs::LocalFeatures::new();
+                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
+                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+                                                                               local_features.set_initial_routing_sync();
+                                                                       }
                                                                        encode_and_send_msg!(msgs::Init {
                                                                                global_features: msgs::GlobalFeatures::new(),
-                                                                               local_features: msgs::LocalFeatures::new(),
+                                                                               local_features,
                                                                        }, 16);
                                                                },
                                                                NextNoiseStep::ActThree => {
@@ -355,17 +413,31 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                peer.their_local_features = Some(msg.local_features);
 
                                                                                                if !peer.outbound {
+                                                                                                       let mut local_features = msgs::LocalFeatures::new();
+                                                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
+                                                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+                                                                                                               local_features.set_initial_routing_sync();
+                                                                                                       }
                                                                                                        encode_and_send_msg!(msgs::Init {
                                                                                                                global_features: msgs::GlobalFeatures::new(),
-                                                                                                               local_features: msgs::LocalFeatures::new(),
+                                                                                                               local_features,
                                                                                                        }, 16);
                                                                                                }
                                                                                        },
                                                                                        17 => {
                                                                                                // Error msg
                                                                                        },
-                                                                                       18 => { }, // ping
-                                                                                       19 => { }, // pong
+
+                                                                                       18 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::Ping::decode(&msg_data[2..]));
+                                                                                               if msg.ponglen < 65532 {
+                                                                                                       let resp = msgs::Pong { byteslen: msg.ponglen };
+                                                                                                       encode_and_send_msg!(resp, 19);
+                                                                                               }
+                                                                                       },
+                                                                                       19 => {
+                                                                                               try_potential_decodeerror!(msgs::Pong::decode(&msg_data[2..]));
+                                                                                       },
 
                                                                                        // Channel control:
                                                                                        32 => {
@@ -481,7 +553,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                }
                                                                                        },
                                                                                        257 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::NodeAnnouncement::decode(&msg_data[2..]));
+                                                                                               let msg = try_ignore_potential_decodeerror!(msgs::NodeAnnouncement::decode(&msg_data[2..]));
                                                                                                try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
                                                                                        },
                                                                                        258 => {
@@ -556,10 +628,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                        Event::PaymentReceived {..} => { /* Hand upstream */ },
                                        Event::PaymentSent {..} => { /* Hand upstream */ },
                                        Event::PaymentFailed {..} => { /* Hand upstream */ },
+                                       Event::PendingHTLCsForwardable {..} => { /* Hand upstream */ },
 
-                                       Event::PendingHTLCsForwardable {..} => {
-                                               //TODO: Handle upstream in some confused form so that upstream just knows
-                                               //to call us somehow?
+                                       Event::SendOpenChannel { ref node_id, ref msg } => {
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Drop the pending channel? (or just let it timeout, but that sucks)
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 32)));
+                                               Self::do_attempt_write_data(&mut descriptor, peer);
+                                               continue;
                                        },
                                        Event::SendFundingCreated { ref node_id, ref msg } => {
                                                let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
@@ -611,6 +688,14 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                Self::do_attempt_write_data(&mut descriptor, peer);
                                                continue;
                                        },
+                                       Event::SendShutdown { ref node_id, ref msg } => {
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 38)));
+                                               Self::do_attempt_write_data(&mut descriptor, peer);
+                                               continue;
+                                       },
                                        Event::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
                                                if self.message_handler.route_handler.handle_channel_announcement(msg).is_ok() && self.message_handler.route_handler.handle_channel_update(update_msg).is_ok() {
                                                        let encoded_msg = encode_msg!(msg, 256);
@@ -649,6 +734,20 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                }
                                                continue;
                                        },
+                                       Event::DisconnectPeer { ref node_id, ref msg } => {
+                                               if let Some(mut descriptor) = peers.node_id_to_descriptor.remove(node_id) {
+                                                       if let Some(mut peer) = peers.peers.remove(&descriptor) {
+                                                               if let Some(ref msg) = *msg {
+                                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
+                                                                       // This isn't guaranteed to work, but if there is enough free
+                                                                       // room in the send buffer, put the error message there...
+                                                                       Self::do_attempt_write_data(&mut descriptor, &mut peer);
+                                                               }
+                                                       }
+                                                       descriptor.disconnect_socket();
+                                                       self.message_handler.chan_handler.peer_disconnected(&node_id, false);
+                                               }
+                                       },
                                }
 
                                upstream_events.push(event);
@@ -695,3 +794,83 @@ impl<Descriptor: SocketDescriptor> EventsProvider for PeerManager<Descriptor> {
                ret
        }
 }
+
+#[cfg(test)]
+mod tests {
+       use ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};
+       use util::events;
+       use util::test_utils;
+
+       use secp256k1::Secp256k1;
+       use secp256k1::key::{SecretKey, PublicKey};
+
+       use rand::{thread_rng, Rng};
+
+       use std::sync::{Arc};
+
+       #[derive(PartialEq, Eq, Clone, Hash)]
+       struct FileDescriptor {
+               fd: u16,
+       }
+
+       impl SocketDescriptor for FileDescriptor {
+               fn send_data(&mut self, data: &Vec<u8>, write_offset: usize, _resume_read: bool) -> usize {
+                       assert!(write_offset < data.len());
+                       data.len() - write_offset
+               }
+
+               fn disconnect_socket(&mut self) {}
+       }
+
+       fn create_network(peer_count: usize) -> Vec<PeerManager<FileDescriptor>> {
+               let secp_ctx = Secp256k1::new();
+               let mut peers = Vec::new();
+               let mut rng = thread_rng();
+
+               for _ in 0..peer_count {
+                       let chan_handler = test_utils::TestChannelMessageHandler::new();
+                       let router = test_utils::TestRoutingMessageHandler::new();
+                       let node_id = {
+                               let mut key_slice = [0;32];
+                               rng.fill_bytes(&mut key_slice);
+                               SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
+                       };
+                       let msg_handler = MessageHandler { chan_handler: Arc::new(chan_handler), route_handler: Arc::new(router) };
+                       let peer = PeerManager::new(msg_handler, node_id);
+                       peers.push(peer);
+               }
+
+               peers
+       }
+
+       fn establish_connection(peer_a: &PeerManager<FileDescriptor>, peer_b: &PeerManager<FileDescriptor>) {
+               let secp_ctx = Secp256k1::new();
+               let their_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret).unwrap();
+               let fd = FileDescriptor { fd: 1};
+               peer_a.new_inbound_connection(fd.clone()).unwrap();
+               peer_a.peers.lock().unwrap().node_id_to_descriptor.insert(their_id, fd.clone());
+       }
+
+       #[test]
+       fn test_disconnect_peer() {
+               // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and
+               // push an DisconnectPeer event to remove the node flagged by id
+               let mut peers = create_network(2);
+               establish_connection(&peers[0], &peers[1]);
+               assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1);
+
+               let secp_ctx = Secp256k1::new();
+               let their_id = PublicKey::from_secret_key(&secp_ctx, &peers[1].our_node_secret).unwrap();
+
+               let chan_handler = test_utils::TestChannelMessageHandler::new();
+               chan_handler.pending_events.lock().unwrap().push(events::Event::DisconnectPeer {
+                       node_id: their_id,
+                       msg: None,
+               });
+               assert_eq!(chan_handler.pending_events.lock().unwrap().len(), 1);
+               peers[0].message_handler.chan_handler = Arc::new(chan_handler);
+
+               peers[0].process_events();
+               assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 0);
+       }
+}