From: Matt Corallo Date: Thu, 10 Jun 2021 18:46:24 +0000 (+0000) Subject: Drop peers_needing_send tracking and try to write for each peer X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=41bb42a99cd9df2d297ae938303b15deefe66e62;p=rust-lightning Drop peers_needing_send tracking and try to write for each peer We do a lot of work to track which peers have buffers which need to be flushed, when we could instead just try to flush ever peer's buffer. --- diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 8695ffdb8..dd3955c19 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -32,7 +32,7 @@ use routing::network_graph::NetGraphMsgHandler; use prelude::*; use alloc::collections::LinkedList; -use std::collections::{HashMap,hash_map,HashSet}; +use std::collections::{HashMap,hash_map}; use std::sync::{Arc, Mutex}; use core::sync::atomic::{AtomicUsize, Ordering}; use core::{cmp, hash, fmt, mem}; @@ -286,9 +286,6 @@ impl Peer { struct PeerHolder { peers: HashMap, - /// Added to by do_read_event for cases where we pushed a message onto the send buffer but - /// didn't call do_attempt_write_data to avoid reentrancy. Cleared in process_events() - peers_needing_send: HashSet, /// Only add to this set when noise completes: node_id_to_descriptor: HashMap, } @@ -419,7 +416,6 @@ impl PeerManager PeerManager(&self, peers_needing_send: &mut HashSet, peer: &mut Peer, descriptor: Descriptor, message: &M) { + fn enqueue_message(&self, peer: &mut Peer, message: &M) { let mut buffer = VecWriter(Vec::new()); wire::write(message, &mut buffer).unwrap(); // crash if the write failed let encoded_message = buffer.0; log_trace!(self.logger, "Enqueueing message of type {} to {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap())); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_message[..])); - peers_needing_send.insert(descriptor); } fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: &[u8]) -> Result { @@ -702,7 +697,7 @@ impl PeerManager { log_trace!(self.logger, "Got Err handling message, sending Error message because {}", e.err); - self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &msg); + self.enqueue_message(peer, &msg); continue; }, } @@ -744,7 +739,7 @@ impl PeerManager { let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..])); @@ -754,7 +749,7 @@ impl PeerManager { if peer.pending_read_is_header { @@ -802,7 +797,7 @@ impl PeerManager match handling_error { MessageHandlingError::PeerHandleError(e) => { return Err(e) }, MessageHandlingError::LightningError(e) => { @@ -837,7 +832,7 @@ impl PeerManager, peer: &mut Peer, peer_descriptor: Descriptor, message: wire::Message) -> Result, MessageHandlingError> { + fn handle_message(&self, peer: &mut Peer, message: wire::Message) -> Result, MessageHandlingError> { log_trace!(self.logger, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap())); // Need an Init as first message @@ -872,7 +867,6 @@ impl PeerManager PeerManager { if msg.ponglen < 65532 { let resp = msgs::Pong { byteslen: msg.ponglen }; - self.enqueue_message(peers_needing_send, peer, peer_descriptor.clone(), &resp); + self.enqueue_message(peer, &resp); } }, wire::Message::Pong(_msg) => { @@ -1029,7 +1023,7 @@ impl PeerManager { let encoded_msg = encode_msg!(msg); - for (ref descriptor, ref mut peer) in peers.peers.iter_mut() { + for (_, 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 @@ -1045,13 +1039,12 @@ impl PeerManager { let encoded_msg = encode_msg!(msg); - for (ref descriptor, ref mut peer) in peers.peers.iter_mut() { + for (_, 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 @@ -1066,13 +1059,12 @@ impl PeerManager { let encoded_msg = encode_msg!(msg); - for (ref descriptor, ref mut peer) in peers.peers.iter_mut() { + for (_, 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 @@ -1084,7 +1076,6 @@ impl PeerManager debug_assert!(false, "We shouldn't attempt to forward anything but gossip messages"), @@ -1131,17 +1122,15 @@ impl PeerManager { log_trace!(self.logger, "Handling SendOpenChannel event in peer_handler for node {} for channel {}", log_pubkey!(node_id), log_bytes!(msg.temporary_channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => { log_trace!(self.logger, "Handling SendFundingCreated event in peer_handler for node {} for channel {} (which becomes {})", @@ -1150,9 +1139,8 @@ impl PeerManager { log_trace!(self.logger, "Handling SendFundingSigned event in peer_handler for node {} for channel {}", @@ -1160,25 +1148,22 @@ impl PeerManager { log_trace!(self.logger, "Handling SendFundingLocked event in peer_handler for node {} for channel {}", log_pubkey!(node_id), log_bytes!(msg.channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => { log_trace!(self.logger, "Handling SendAnnouncementSignatures event in peer_handler for node {} for channel {})", log_pubkey!(node_id), log_bytes!(msg.channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => { log_trace!(self.logger, "Handling UpdateHTLCs event in peer_handler for node {} with {} adds, {} fulfills, {} fails for channel {}", @@ -1187,7 +1172,7 @@ impl PeerManager PeerManager { log_trace!(self.logger, "Handling SendRevokeAndACK event in peer_handler for node {} for channel {}", log_pubkey!(node_id), log_bytes!(msg.channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => { log_trace!(self.logger, "Handling SendClosingSigned event in peer_handler for node {} for channel {}", log_pubkey!(node_id), log_bytes!(msg.channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::SendShutdown { ref node_id, ref msg } => { log_trace!(self.logger, "Handling Shutdown event in peer_handler for node {} for channel {}", log_pubkey!(node_id), log_bytes!(msg.channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => { log_trace!(self.logger, "Handling SendChannelReestablish event in peer_handler for node {} for channel {}", log_pubkey!(node_id), log_bytes!(msg.channel_id)); - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::BroadcastChannelAnnouncement { msg, update_msg } => { log_trace!(self.logger, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id); @@ -1264,7 +1244,6 @@ impl PeerManager { if let Some(mut descriptor) = peers.node_id_to_descriptor.remove(node_id) { - peers.peers_needing_send.remove(&descriptor); if let Some(mut peer) = peers.peers.remove(&descriptor) { if let Some(ref msg) = *msg { log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}", @@ -1287,21 +1266,18 @@ impl PeerManager { - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); }, MessageSendEvent::SendShortIdsQuery { ref node_id, ref msg } => { - let (mut descriptor, peer) = get_peer_for_forwarding!(node_id); + let (_, peer) = get_peer_for_forwarding!(node_id); peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg))); - self.do_attempt_write_data(&mut descriptor, peer); } MessageSendEvent::SendReplyChannelRange { ref node_id, ref msg } => { log_trace!(self.logger, "Handling SendReplyChannelRange event in peer_handler for node {} with num_scids={} first_blocknum={} number_of_blocks={}, sync_complete={}", @@ -1310,18 +1286,14 @@ impl PeerManager self.do_attempt_write_data(&mut descriptor, peer), - None => panic!("Inconsistent peers set state!"), - } + for (descriptor, ref mut peer) in peers.peers.iter_mut() { + self.do_attempt_write_data(&mut (*descriptor).clone(), peer); } } } @@ -1340,7 +1312,6 @@ impl PeerManager panic!("Descriptor for disconnect_event is not already known to PeerManager"), @@ -1368,7 +1339,6 @@ impl PeerManager PeerManager {