Skip to content

Extract encode_and_send_msg into a method. #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ln::features::InitFeatures;
use ln::msgs;
use ln::msgs::ChannelMessageHandler;
use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
use util::ser::VecWriter;
use util::ser::{VecWriter, Writeable};
use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
use ln::wire;
use ln::wire::Encode;
Expand Down Expand Up @@ -459,6 +459,17 @@ impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor,
}
}

/// Append a message to a peer's pending outbound/write buffer, and update the map of peers needing sends accordingly.
fn enqueue_message<M: Encode + Writeable>(&self, peers_needing_send: &mut HashSet<Descriptor>, peer: &mut Peer, descriptor: Descriptor, message: &M) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary to address now, but it would be worth thinking about the various ways we are sending messages in this module. Currently:

  • log, enqueue, mark as needing send (here; called by do_read_event)
  • log, enqueue (macro in do_attempt_write_data)
  • enqueue (process_events and timer_tick_occured)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do_attempt_write_data differs from do_read_event in that it doesn't update the peers needing send map, but it's also a very tiny method where the sending is self-contained. I am open for future brainstorming on its refactor. In principle, all sends should look the same anyway, and having the same method/macro name do different things would have been confusing, too.
At its core, enqueue is comprised of three components: serialization, the enqueueing, and the signaling mechanism. The signaling mechanism doubles the number of necessary arguments. Perhaps splitting that method into two, where one calls the other, would be appropriate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that peers_needing_send is just a historical artifact and thus no longer needed. See discussion in #456. So there may be some refactoring that will get us to one simple method.

The method could also be on Peer rather than PeerManager, though I think Peer would then need a logger...

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<bool, PeerHandleError> {
let pause_read = {
let mut peers_lock = self.peers.lock().unwrap();
Expand All @@ -481,16 +492,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor,
if peer.pending_read_buffer_pos == peer.pending_read_buffer.len() {
peer.pending_read_buffer_pos = 0;

macro_rules! encode_and_send_msg {
($msg: expr) => {
{
log_trace!(self.logger, "Encoding and sending message of type {} to {}", $msg.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(&$msg)[..]));
peers.peers_needing_send.insert(peer_descriptor.clone());
}
}
}

macro_rules! try_potential_handleerror {
($thing: expr) => {
match $thing {
Expand All @@ -508,7 +509,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor,
},
msgs::ErrorAction::SendErrorMessage { msg } => {
log_trace!(self.logger, "Got Err handling message, sending Error message because {}", e.err);
encode_and_send_msg!(msg);
self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &msg);
continue;
},
}
Expand Down Expand Up @@ -554,7 +555,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor,
}

let resp = msgs::Init { features };
encode_and_send_msg!(resp);
self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &resp);
},
NextNoiseStep::ActThree => {
let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]));
Expand Down Expand Up @@ -653,7 +654,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor,
}

let resp = msgs::Init { features };
encode_and_send_msg!(resp);
self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &resp);
}

self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
Expand Down Expand Up @@ -682,7 +683,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor,
wire::Message::Ping(msg) => {
if msg.ponglen < 65532 {
let resp = msgs::Pong { byteslen: msg.ponglen };
encode_and_send_msg!(resp);
self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &resp);
}
},
wire::Message::Pong(_msg) => {
Expand Down