From: Matt Corallo Date: Mon, 24 Feb 2020 23:14:40 +0000 (-0500) Subject: Pretty-shitty implement TODOs for msg forwarding X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=2ce646f575b2f81e4f0f56a4ca0f11551866279e;p=rust-lightning Pretty-shitty implement TODOs for msg forwarding --- diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index e75c2c96e..c02e351be 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -151,6 +151,12 @@ impl Peer { } } +enum AnnouncementMsg { + ChanUpdate(msgs::ChannelUpdate), + ChanAnnounce(msgs::ChannelAnnouncement), + NodeAnnounce(msgs::NodeAnnouncement), +} + struct PeerHolder { peers: HashMap, /// Added to by do_read_event for cases where we pushed a message onto the send buffer but @@ -158,6 +164,7 @@ struct PeerHolder { peers_needing_send: HashSet, /// Only add to this set when noise completes: node_id_to_descriptor: HashMap, + pending_broadcasts: Vec<(PublicKey, AnnouncementMsg)>, } #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))] @@ -226,7 +233,8 @@ impl PeerManager PeerManager { 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))); } }, @@ -808,6 +816,54 @@ impl PeerManager { { + 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) => { @@ -970,24 +1026,10 @@ impl PeerManager 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 } => { @@ -995,14 +1037,9 @@ impl PeerManager { @@ -1010,14 +1047,8 @@ impl PeerManager {