Merge pull request #2009 from TheBlueMatt/2023-02-no-racey-retries
[rust-lightning] / lightning / src / ln / peer_handler.rs
index f24061726553f564c6613f9aaf4e437e8b127c70..8c3ab968af20f92e3a544d7d1fea024837bd26e6 100644 (file)
@@ -46,16 +46,23 @@ use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::sha256::HashEngine as Sha256Engine;
 use bitcoin::hashes::{HashEngine, Hash};
 
-/// Handler for BOLT1-compliant messages.
+/// A handler provided to [`PeerManager`] for reading and handling custom messages.
+///
+/// [BOLT 1] specifies a custom message type range for use with experimental or application-specific
+/// messages. `CustomMessageHandler` allows for user-defined handling of such types. See the
+/// [`lightning_custom_message`] crate for tools useful in composing more than one custom handler.
+///
+/// [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
+/// [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message
 pub trait CustomMessageHandler: wire::CustomMessageReader {
-       /// Called with the message type that was received and the buffer to be read.
-       /// Can return a `MessageHandlingError` if the message could not be handled.
+       /// Handles the given message sent from `sender_node_id`, possibly producing messages for
+       /// [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
+       /// to send.
        fn handle_custom_message(&self, msg: Self::CustomMessage, sender_node_id: &PublicKey) -> Result<(), LightningError>;
 
-       /// Gets the list of pending messages which were generated by the custom message
-       /// handler, clearing the list in the process. The first tuple element must
-       /// correspond to the intended recipients node ids. If no connection to one of the
-       /// specified node does not exist, the message is simply not sent to it.
+       /// Returns the list of pending messages that were generated by the handler, clearing the list
+       /// in the process. Each message is paired with the node id of the intended recipient. If no
+       /// connection to the node exists, then the message is simply not sent.
        fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)>;
 }
 
@@ -390,7 +397,9 @@ const BUFFER_DRAIN_MSGS_PER_TICK: usize = 32;
 
 struct Peer {
        channel_encryptor: PeerChannelEncryptor,
-       their_node_id: Option<PublicKey>,
+       /// We cache a `NodeId` here to avoid serializing peers' keys every time we forward gossip
+       /// messages in `PeerManager`. Use `Peer::set_their_node_id` to modify this field.
+       their_node_id: Option<(PublicKey, NodeId)>,
        their_features: Option<InitFeatures>,
        their_net_address: Option<NetAddress>,
 
@@ -492,6 +501,10 @@ impl Peer {
                total_outbound_buffered > OUTBOUND_BUFFER_LIMIT_DROP_GOSSIP ||
                        self.msgs_sent_since_pong > BUFFER_DRAIN_MSGS_PER_TICK * FORWARD_INIT_SYNC_BUFFER_LIMIT_RATIO
        }
+
+       fn set_their_node_id(&mut self, node_id: PublicKey) {
+               self.their_node_id = Some((node_id, NodeId::from_pubkey(&node_id)));
+       }
 }
 
 /// SimpleArcPeerManager is useful when you need a PeerManager with a static lifetime, e.g.
@@ -665,10 +678,10 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref, NS: Deref> PeerManager<D
 /// This works around `format!()` taking a reference to each argument, preventing
 /// `if let Some(node_id) = peer.their_node_id { format!(.., node_id) } else { .. }` from compiling
 /// due to lifetime errors.
-struct OptionalFromDebugger<'a>(&'a Option<PublicKey>);
+struct OptionalFromDebugger<'a>(&'a Option<(PublicKey, NodeId)>);
 impl core::fmt::Display for OptionalFromDebugger<'_> {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
-               if let Some(node_id) = self.0 { write!(f, " from {}", log_pubkey!(node_id)) } else { Ok(()) }
+               if let Some((node_id, _)) = self.0 { write!(f, " from {}", log_pubkey!(node_id)) } else { Ok(()) }
        }
 }
 
@@ -744,19 +757,25 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                }
        }
 
-       /// Get the list of node ids for peers which have completed the initial handshake.
+       /// Get a list of tuples mapping from node id to network addresses for peers which have
+       /// completed the initial handshake.
+       ///
+       /// For outbound connections, the [`PublicKey`] will be the same as the `their_node_id` parameter
+       /// passed in to [`Self::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
+       /// [`PublicKey`].
        ///
-       /// 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> {
+       /// The returned `Option`s will only be `Some` if an address had been previously given via
+       /// [`Self::new_outbound_connection`] or [`Self::new_inbound_connection`].
+       pub fn get_peer_node_ids(&self) -> Vec<(PublicKey, Option<NetAddress>)> {
                let peers = self.peers.read().unwrap();
                peers.values().filter_map(|peer_mutex| {
                        let p = peer_mutex.lock().unwrap();
-                       if !p.channel_encryptor.is_ready_for_encryption() || p.their_features.is_none() {
+                       if !p.channel_encryptor.is_ready_for_encryption() || p.their_features.is_none() ||
+                               p.their_node_id.is_none() {
                                return None;
                        }
-                       p.their_node_id
+                       Some((p.their_node_id.unwrap().0, p.their_net_address.clone()))
                }).collect()
        }
 
@@ -767,7 +786,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                SecretKey::from_slice(&Sha256::from_engine(ephemeral_hash).into_inner()).expect("You broke SHA-256!")
        }
 
-       /// Indicates a new outbound connection has been established to a node with the given node_id
+       /// Indicates a new outbound connection has been established to a node with the given `node_id`
        /// and an optional remote network address.
        ///
        /// The remote network address adds the option to report a remote IP address back to a connecting
@@ -882,7 +901,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                let mut have_written = false;
                while !peer.awaiting_write_event {
                        if peer.should_buffer_onion_message() {
-                               if let Some(peer_node_id) = peer.their_node_id {
+                               if let Some((peer_node_id, _)) = peer.their_node_id {
                                        if let Some(next_onion_message) =
                                                self.message_handler.onion_message_handler.next_onion_message_for_peer(peer_node_id) {
                                                        self.enqueue_message(peer, &next_onion_message);
@@ -1024,9 +1043,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
        /// Append a message to a peer's pending outbound/write buffer
        fn enqueue_message<M: wire::Type>(&self, peer: &mut Peer, message: &M) {
                if is_gossip_msg(message.type_id()) {
-                       log_gossip!(self.logger, "Enqueueing message {:?} to {}", message, log_pubkey!(peer.their_node_id.unwrap()));
+                       log_gossip!(self.logger, "Enqueueing message {:?} to {}", message, log_pubkey!(peer.their_node_id.unwrap().0));
                } else {
-                       log_trace!(self.logger, "Enqueueing message {:?} to {}", message, log_pubkey!(peer.their_node_id.unwrap()))
+                       log_trace!(self.logger, "Enqueueing message {:?} to {}", message, log_pubkey!(peer.their_node_id.unwrap().0))
                }
                peer.msgs_sent_since_pong += 1;
                peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(message));
@@ -1111,14 +1130,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
 
                                                macro_rules! insert_node_id {
                                                        () => {
-                                                               match self.node_id_to_descriptor.lock().unwrap().entry(peer.their_node_id.unwrap()) {
+                                                               match self.node_id_to_descriptor.lock().unwrap().entry(peer.their_node_id.unwrap().0) {
                                                                        hash_map::Entry::Occupied(_) => {
-                                                                               log_trace!(self.logger, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap()));
+                                                                               log_trace!(self.logger, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap().0));
                                                                                peer.their_node_id = None; // Unset so that we don't generate a peer_disconnected event
                                                                                return Err(PeerHandleError{ no_connection_possible: false })
                                                                        },
                                                                        hash_map::Entry::Vacant(entry) => {
-                                                                               log_debug!(self.logger, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap()));
+                                                                               log_debug!(self.logger, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap().0));
                                                                                entry.insert(peer_descriptor.clone())
                                                                        },
                                                                };
@@ -1142,7 +1161,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
                                                                peer.pending_read_is_header = true;
 
-                                                               peer.their_node_id = Some(their_node_id);
+                                                               peer.set_their_node_id(their_node_id);
                                                                insert_node_id!();
                                                                let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
                                                                        .or(self.message_handler.route_handler.provided_init_features(&their_node_id))
@@ -1156,7 +1175,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                        peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]));
                                                                peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
                                                                peer.pending_read_is_header = true;
-                                                               peer.their_node_id = Some(their_node_id);
+                                                               peer.set_their_node_id(their_node_id);
                                                                insert_node_id!();
                                                                let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
                                                                        .or(self.message_handler.route_handler.provided_init_features(&their_node_id))
@@ -1258,7 +1277,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                }
 
                for msg in msgs_to_forward.drain(..) {
-                       self.forward_broadcast_msg(&*peers, &msg, peer_node_id.as_ref());
+                       self.forward_broadcast_msg(&*peers, &msg, peer_node_id.as_ref().map(|(pk, _)| pk));
                }
 
                Ok(pause_read)
@@ -1272,7 +1291,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                mut peer_lock: MutexGuard<Peer>,
                message: wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>
        ) -> Result<Option<wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>>, MessageHandlingError> {
-               let their_node_id = peer_lock.their_node_id.clone().expect("We know the peer's public key by the time we receive messages");
+               let their_node_id = peer_lock.their_node_id.clone().expect("We know the peer's public key by the time we receive messages").0;
                peer_lock.received_message_since_timer_tick = true;
 
                // Need an Init as first message
@@ -1520,13 +1539,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
                                                continue;
                                        }
-                                       if let Some(their_node_id) = peer.their_node_id {
-                                               let their_node_id = NodeId::from_pubkey(&their_node_id);
+                                       if let Some((_, their_node_id)) = peer.their_node_id {
                                                if their_node_id == msg.contents.node_id_1 || their_node_id == msg.contents.node_id_2 {
                                                        continue;
                                                }
                                        }
-                                       if except_node.is_some() && peer.their_node_id.as_ref() == except_node {
+                                       if except_node.is_some() && peer.their_node_id.as_ref().map(|(pk, _)| pk) == except_node {
                                                continue;
                                        }
                                        self.enqueue_encoded_gossip_broadcast(&mut *peer, encoded_msg.clone());
@@ -1546,12 +1564,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
                                                continue;
                                        }
-                                       if let Some(their_node_id) = peer.their_node_id {
-                                               if NodeId::from_pubkey(&their_node_id) == msg.contents.node_id {
+                                       if let Some((_, their_node_id)) = peer.their_node_id {
+                                               if their_node_id == msg.contents.node_id {
                                                        continue;
                                                }
                                        }
-                                       if except_node.is_some() && peer.their_node_id.as_ref() == except_node {
+                                       if except_node.is_some() && peer.their_node_id.as_ref().map(|(pk, _)| pk) == except_node {
                                                continue;
                                        }
                                        self.enqueue_encoded_gossip_broadcast(&mut *peer, encoded_msg.clone());
@@ -1571,7 +1589,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
                                                continue;
                                        }
-                                       if except_node.is_some() && peer.their_node_id.as_ref() == except_node {
+                                       if except_node.is_some() && peer.their_node_id.as_ref().map(|(pk, _)| pk) == except_node {
                                                continue;
                                        }
                                        self.enqueue_encoded_gossip_broadcast(&mut *peer, encoded_msg.clone());
@@ -1905,7 +1923,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                        },
                        Some(peer_lock) => {
                                let peer = peer_lock.lock().unwrap();
-                               if let Some(node_id) = peer.their_node_id {
+                               if let Some((node_id, _)) = peer.their_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 { "" });
@@ -1945,7 +1963,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                self.node_id_to_descriptor.lock().unwrap().clear();
                let peers = &mut *peers_lock;
                for (mut descriptor, peer) in peers.drain() {
-                       if let Some(node_id) = peer.lock().unwrap().their_node_id {
+                       if let Some((node_id, _)) = peer.lock().unwrap().their_node_id {
                                log_trace!(self.logger, "Disconnecting peer with id {} due to client request to disconnect all peers", node_id);
                                self.message_handler.chan_handler.peer_disconnected(&node_id, false);
                                self.message_handler.onion_message_handler.peer_disconnected(&node_id, false);
@@ -2043,7 +2061,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                let mut peers_lock = self.peers.write().unwrap();
                                for descriptor in descriptors_needing_disconnect.iter() {
                                        if let Some(peer) = peers_lock.remove(descriptor) {
-                                               if let Some(node_id) = peer.lock().unwrap().their_node_id {
+                                               if let Some((node_id, _)) = peer.lock().unwrap().their_node_id {
                                                        log_trace!(self.logger, "Disconnecting peer with id {} due to ping timeout", node_id);
                                                        self.node_id_to_descriptor.lock().unwrap().remove(&node_id);
                                                        self.message_handler.chan_handler.peer_disconnected(&node_id, false);
@@ -2218,11 +2236,14 @@ mod tests {
        }
 
        fn establish_connection<'a>(peer_a: &PeerManager<FileDescriptor, &'a test_utils::TestChannelMessageHandler, &'a test_utils::TestRoutingMessageHandler, IgnoringMessageHandler, &'a test_utils::TestLogger, IgnoringMessageHandler, &'a test_utils::TestNodeSigner>, peer_b: &PeerManager<FileDescriptor, &'a test_utils::TestChannelMessageHandler, &'a test_utils::TestRoutingMessageHandler, IgnoringMessageHandler, &'a test_utils::TestLogger, IgnoringMessageHandler, &'a test_utils::TestNodeSigner>) -> (FileDescriptor, FileDescriptor) {
-               let a_id = peer_a.node_signer.get_node_id(Recipient::Node).unwrap();
+               let id_a = peer_a.node_signer.get_node_id(Recipient::Node).unwrap();
                let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
+               let addr_a = NetAddress::IPv4{addr: [127, 0, 0, 1], port: 1000};
+               let id_b = peer_b.node_signer.get_node_id(Recipient::Node).unwrap();
                let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
-               let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone(), None).unwrap();
-               peer_a.new_inbound_connection(fd_a.clone(), None).unwrap();
+               let addr_b = NetAddress::IPv4{addr: [127, 0, 0, 1], port: 1001};
+               let initial_data = peer_b.new_outbound_connection(id_a, fd_b.clone(), Some(addr_a.clone())).unwrap();
+               peer_a.new_inbound_connection(fd_a.clone(), Some(addr_b.clone())).unwrap();
                assert_eq!(peer_a.read_event(&mut fd_a, &initial_data).unwrap(), false);
                peer_a.process_events();
 
@@ -2237,6 +2258,9 @@ mod tests {
                let a_data = fd_a.outbound_data.lock().unwrap().split_off(0);
                assert_eq!(peer_b.read_event(&mut fd_b, &a_data).unwrap(), false);
 
+               assert!(peer_a.get_peer_node_ids().contains(&(id_b, Some(addr_b))));
+               assert!(peer_b.get_peer_node_ids().contains(&(id_a, Some(addr_a))));
+
                (fd_a.clone(), fd_b.clone())
        }