]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Pretty-shitty implement TODOs for msg forwarding
authorMatt Corallo <git@bluematt.me>
Mon, 24 Feb 2020 23:14:40 +0000 (18:14 -0500)
committerMatt Corallo <git@bluematt.me>
Mon, 4 May 2020 17:58:13 +0000 (13:58 -0400)
lightning/src/ln/peer_handler.rs

index 36c9bf5e9acd2bfe0a7ea71b03e8983d0514a67a..81b71affc2cb890b28b18bdc85fe6bd86af1fe32 100644 (file)
@@ -151,6 +151,12 @@ impl Peer {
        }
 }
 
+enum AnnouncementMsg {
+       ChanUpdate(msgs::ChannelUpdate),
+       ChanAnnounce(msgs::ChannelAnnouncement),
+       NodeAnnounce(msgs::NodeAnnouncement),
+}
+
 struct PeerHolder<Descriptor: SocketDescriptor> {
        peers: HashMap<Descriptor, Peer>,
        /// Added to by do_read_event for cases where we pushed a message onto the send buffer but
@@ -158,6 +164,7 @@ struct PeerHolder<Descriptor: SocketDescriptor> {
        peers_needing_send: HashSet<Descriptor>,
        /// Only add to this set when noise completes:
        node_id_to_descriptor: HashMap<PublicKey, Descriptor>,
+       pending_broadcasts: Vec<(PublicKey, AnnouncementMsg)>,
 }
 
 #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
@@ -226,7 +233,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                        peers: Mutex::new(PeerHolder {
                                peers: HashMap::new(),
                                peers_needing_send: HashSet::new(),
-                               node_id_to_descriptor: HashMap::new()
+                               node_id_to_descriptor: HashMap::new(),
+                               pending_broadcasts: Vec::new(),
                        }),
                        our_node_secret: our_node_secret,
                        ephemeral_key_midstate,
@@ -749,21 +757,21 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                                                                let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
 
                                                                                                if should_forward {
-                                                                                                       // TODO: forward msg along to all our other peers!
+                                                                                                       peers.pending_broadcasts.push((peer.their_node_id.unwrap().clone(), AnnouncementMsg::ChanAnnounce(msg)));
                                                                                                }
                                                                                        },
                                                                                        wire::Message::NodeAnnouncement(msg) => {
                                                                                                let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
 
                                                                                                if should_forward {
-                                                                                                       // TODO: forward msg along to all our other peers!
+                                                                                                       peers.pending_broadcasts.push((peer.their_node_id.unwrap().clone(), AnnouncementMsg::NodeAnnounce(msg)));
                                                                                                }
                                                                                        },
                                                                                        wire::Message::ChannelUpdate(msg) => {
                                                                                                let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
 
                                                                                                if should_forward {
-                                                                                                       // TODO: forward msg along to all our other peers!
+                                                                                                       peers.pending_broadcasts.push((peer.their_node_id.unwrap().clone(), AnnouncementMsg::ChanUpdate(msg)));
                                                                                                }
                                                                                        },
 
@@ -807,6 +815,54 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                        let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
                        let mut peers_lock = self.peers.lock().unwrap();
                        let peers = &mut *peers_lock;
+
+                       macro_rules! broadcast_msgs {
+                               ({ $($except_check: stmt), * }, { $($encoded_msg: expr), * }) => { {
+                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
+                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() {
+                                                       continue
+                                               }
+                                               match peer.their_node_id {
+                                                       None => continue,
+                                                       Some(their_node_id) => {
+                                                               $(
+                                                                       if { $except_check }(&peer, their_node_id) { continue }
+                                                               )*
+                                                       }
+                                               }
+                                               $(peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&$encoded_msg));)*
+                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
+                                       }
+                               } }
+                       }
+
+                       for (from_node_id, broadcast) in peers.pending_broadcasts.drain(..) {
+                               match broadcast {
+                                       AnnouncementMsg::ChanUpdate(msg) => {
+                                               let encoded_msg = encode_msg!(&msg);
+                                               broadcast_msgs!({ |peer: & &mut Peer, _| !peer.should_forward_channel_announcement(msg.contents.short_channel_id),
+                                                                                 |_, their_node_id| their_node_id == from_node_id },
+                                                                               { encoded_msg });
+                                       },
+                                       AnnouncementMsg::ChanAnnounce(msg) => {
+                                               let encoded_msg = encode_msg!(&msg);
+                                               broadcast_msgs!({ |peer: & &mut Peer, _| !peer.should_forward_channel_announcement(msg.contents.short_channel_id),
+                                                                                 |_, their_node_id| their_node_id == msg.contents.node_id_1,
+                                                                                 |_, their_node_id| their_node_id == msg.contents.node_id_2,
+                                                                                 |_, their_node_id| their_node_id == from_node_id },
+                                                                               { encoded_msg });
+                                       },
+                                       AnnouncementMsg::NodeAnnounce(msg) => {
+                                               let encoded_msg = encode_msg!(&msg);
+
+                                               broadcast_msgs!({ |peer: & &mut Peer, _| !peer.should_forward_node_announcement(msg.contents.node_id),
+                                                                                 |_, their_node_id| their_node_id == msg.contents.node_id,
+                                                                                 |_, their_node_id| their_node_id == from_node_id },
+                                                                               { encoded_msg });
+                                       }
+                               }
+                       }
+
                        for event in events_generated.drain(..) {
                                macro_rules! get_peer_for_forwarding {
                                        ($node_id: expr, $handle_no_such_peer: block) => {
@@ -969,24 +1025,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                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);
                                                        let encoded_update_msg = encode_msg!(update_msg);
-
-                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
-                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
-                                                                               !peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
-                                                                       continue
-                                                               }
-                                                               match peer.their_node_id {
-                                                                       None => continue,
-                                                                       Some(their_node_id) => {
-                                                                               if their_node_id == msg.contents.node_id_1 || their_node_id == msg.contents.node_id_2 {
-                                                                                       continue
-                                                                               }
-                                                                       }
-                                                               }
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_update_msg[..]));
-                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
-                                                       }
+                                                       broadcast_msgs!({ |peer: & &mut Peer, _| !peer.should_forward_channel_announcement(msg.contents.short_channel_id),
+                                                                                         |_, their_node_id| their_node_id == msg.contents.node_id_1,
+                                                                                         |_, their_node_id| their_node_id == msg.contents.node_id_2 },
+                                                                                       { encoded_msg, encoded_update_msg });
                                                }
                                        },
                                        MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => {
@@ -994,14 +1036,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                if self.message_handler.route_handler.handle_node_announcement(msg).is_ok() {
                                                        let encoded_msg = encode_msg!(msg);
 
-                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
-                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
-                                                                               !peer.should_forward_node_announcement(msg.contents.node_id) {
-                                                                       continue
-                                                               }
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
-                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
-                                                       }
+                                                       broadcast_msgs!({ |peer: & &mut Peer, _| !peer.should_forward_node_announcement(msg.contents.node_id),
+                                                                                         |_, their_node_id| their_node_id == msg.contents.node_id },
+                                                                                       { encoded_msg });
                                                }
                                        },
                                        MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
@@ -1009,14 +1046,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                if self.message_handler.route_handler.handle_channel_update(msg).is_ok() {
                                                        let encoded_msg = encode_msg!(msg);
 
-                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
-                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
-                                                                               !peer.should_forward_channel_announcement(msg.contents.short_channel_id)  {
-                                                                       continue
-                                                               }
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
-                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
-                                                       }
+                                                       broadcast_msgs!({ |peer: & &mut Peer, _| !peer.should_forward_channel_announcement(msg.contents.short_channel_id) },
+                                                                                       { encoded_msg });
                                                }
                                        },
                                        MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {