Skip to content
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

Ensure peer_disconnected is called after a handler refuses a connection #3580

Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ mod tests {
) -> Result<(), ()> {
Ok(())
}
fn peer_disconnected(&self, _their_node_id: PublicKey) {}
fn handle_reply_channel_range(
&self, _their_node_id: PublicKey, _msg: ReplyChannelRange,
) -> Result<(), LightningError> {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,8 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
/// with us. Implementors should be somewhat conservative about doing so, however, as other
/// message handlers may still wish to communicate with this peer.
///
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
fn peer_connected(&self, their_node_id: PublicKey, msg: &Init, inbound: bool) -> Result<(), ()>;
/// Handle an incoming `channel_reestablish` message from the given peer.
fn handle_channel_reestablish(&self, their_node_id: PublicKey, msg: &ChannelReestablish);
Expand Down Expand Up @@ -1656,7 +1658,11 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
/// with us. Implementors should be somewhat conservative about doing so, however, as other
/// message handlers may still wish to communicate with this peer.
///
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
fn peer_connected(&self, their_node_id: PublicKey, init: &Init, inbound: bool) -> Result<(), ()>;
/// Indicates a connection to the peer failed/an existing connection was lost.
fn peer_disconnected(&self, their_node_id: PublicKey);
/// Handles the reply of a query we initiated to learn about channels
/// for a given range of blocks. We can expect to receive one or more
/// replies to a single query.
Expand Down Expand Up @@ -1707,6 +1713,8 @@ pub trait OnionMessageHandler {
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
/// with us. Implementors should be somewhat conservative about doing so, however, as other
/// message handlers may still wish to communicate with this peer.
///
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
fn peer_connected(&self, their_node_id: PublicKey, init: &Init, inbound: bool) -> Result<(), ()>;

/// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
Expand Down
11 changes: 11 additions & 0 deletions lightning/src/ln/peer_handler.rs
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we enforce uniformity going forward by having the respective handler interfaces inherit from a general trait PeerMessageHandler interface and move peer_{connected, disconnected} and provided_{node,init}_features to it?

Note this would also allow us to iterate over all previously-succeeded handlers rather than having to call them individually (which might get stale at some point in the future).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea, we should definitely require uniformity going forward. We could definitely do a higer-level interface, do you want it in this PR or when we add the next handler?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could definitely do a higer-level interface, do you want it in this PR or when we add the next handler?

I'd have a slight preference to do it right away, but feel free to do in a follow-up if you'd rather land this quickly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wrote the patch, but its 36 files changed, 982 insertions(+), 1018 deletions(-), so kinda would prefer to split it up just for sanity.

Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub trait CustomMessageHandler: wire::CustomMessageReader {
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
/// with us. Implementors should be somewhat conservative about doing so, however, as other
/// message handlers may still wish to communicate with this peer.
///
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
fn peer_connected(&self, their_node_id: PublicKey, msg: &Init, inbound: bool) -> Result<(), ()>;

/// Gets the node feature flags which this handler itself supports. All available handlers are
Expand Down Expand Up @@ -119,6 +121,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
Option<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> { None }
fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option<msgs::NodeAnnouncement> { None }
fn peer_connected(&self, _their_node_id: PublicKey, _init: &msgs::Init, _inbound: bool) -> Result<(), ()> { Ok(()) }
fn peer_disconnected(&self, _their_node_id: PublicKey) { }
fn handle_reply_channel_range(&self, _their_node_id: PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
fn handle_reply_short_channel_ids_end(&self, _their_node_id: PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
fn handle_query_channel_range(&self, _their_node_id: PublicKey, _msg: msgs::QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
Expand Down Expand Up @@ -1714,14 +1717,20 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
}
if let Err(()) = self.message_handler.chan_handler.peer_connected(their_node_id, &msg, peer_lock.inbound_connection) {
log_debug!(logger, "Channel Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
self.message_handler.route_handler.peer_disconnected(their_node_id);
return Err(PeerHandleError { }.into());
}
if let Err(()) = self.message_handler.onion_message_handler.peer_connected(their_node_id, &msg, peer_lock.inbound_connection) {
log_debug!(logger, "Onion Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
self.message_handler.route_handler.peer_disconnected(their_node_id);
self.message_handler.chan_handler.peer_disconnected(their_node_id);
return Err(PeerHandleError { }.into());
}
if let Err(()) = self.message_handler.custom_message_handler.peer_connected(their_node_id, &msg, peer_lock.inbound_connection) {
log_debug!(logger, "Custom Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
self.message_handler.route_handler.peer_disconnected(their_node_id);
self.message_handler.chan_handler.peer_disconnected(their_node_id);
self.message_handler.onion_message_handler.peer_disconnected(their_node_id);
return Err(PeerHandleError { }.into());
}

Expand Down Expand Up @@ -2533,6 +2542,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
debug_assert!(peer.their_node_id.is_some());
if let Some((node_id, _)) = peer.their_node_id {
log_trace!(WithContext::from(&self.logger, Some(node_id), None, None), "Disconnecting peer with id {} due to {}", node_id, reason);
self.message_handler.route_handler.peer_disconnected(node_id);
self.message_handler.chan_handler.peer_disconnected(node_id);
self.message_handler.onion_message_handler.peer_disconnected(node_id);
self.message_handler.custom_message_handler.peer_disconnected(node_id);
Expand All @@ -2557,6 +2567,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
let removed = self.node_id_to_descriptor.lock().unwrap().remove(&node_id);
debug_assert!(removed.is_some(), "descriptor maps should be consistent");
if !peer.handshake_complete() { return; }
self.message_handler.route_handler.peer_disconnected(node_id);
self.message_handler.chan_handler.peer_disconnected(node_id);
self.message_handler.onion_message_handler.peer_disconnected(node_id);
self.message_handler.custom_message_handler.peer_disconnected(node_id);
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ where
Ok(())
}

fn peer_disconnected(&self, _their_node_id: PublicKey) {}

fn handle_reply_channel_range(
&self, _their_node_id: PublicKey, _msg: ReplyChannelRange,
) -> Result<(), LightningError> {
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,8 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
Ok(())
}

fn peer_disconnected(&self, _their_node_id: PublicKey) {}

fn handle_reply_channel_range(
&self, _their_node_id: PublicKey, _msg: msgs::ReplyChannelRange,
) -> Result<(), msgs::LightningError> {
Expand Down