feat(swarm): remove deprecated IntoConnectionHandler

This removes the deprecated `IntoConnectionHandler` trait and all its implementations. Consequently, `NetworkBehaviour::new_handler` and `NetworkBehaviour::addresses_of_peer` are now gone and the two `handle_` functions are now required to implement.

Related: #3647.

Pull-Request: #3884.
This commit is contained in:
Thomas Eizinger
2023-05-08 10:30:29 +02:00
committed by GitHub
parent b4e724dd72
commit 0e36c7c072
15 changed files with 38 additions and 371 deletions

View File

@ -28,11 +28,9 @@ pub use listen_addresses::ListenAddresses;
use crate::connection::ConnectionId;
use crate::dial_opts::DialOpts;
#[allow(deprecated)]
use crate::handler::IntoConnectionHandler;
use crate::{
AddressRecord, AddressScore, ConnectionDenied, DialError, ListenError, THandler,
THandlerInEvent, THandlerOutEvent,
AddressRecord, AddressScore, ConnectionDenied, ConnectionHandler, DialError, ListenError,
THandler, THandlerInEvent, THandlerOutEvent,
};
use libp2p_core::{transport::ListenerId, ConnectedPoint, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
@ -77,9 +75,7 @@ use std::{task::Context, task::Poll};
/// implementation for the custom `struct`. Each [`NetworkBehaviour`] trait method is simply
/// delegated to each `struct` member in the order the `struct` is defined. For example for
/// [`NetworkBehaviour::poll`] it will first poll the first `struct` member until it returns
/// [`Poll::Pending`] before moving on to later members. For [`NetworkBehaviour::addresses_of_peer`]
/// it will delegate to each `struct` member and return a concatenated array of all addresses
/// returned by the struct members.
/// [`Poll::Pending`] before moving on to later members.
///
/// Events ([`NetworkBehaviour::OutEvent`]) returned by each `struct` member are wrapped in a new
/// `enum` event, with an `enum` variant for each `struct` member. Users can define this event
@ -122,37 +118,11 @@ use std::{task::Context, task::Poll};
/// ```
pub trait NetworkBehaviour: 'static {
/// Handler for all the protocols the network behaviour supports.
#[allow(deprecated)]
type ConnectionHandler: IntoConnectionHandler;
type ConnectionHandler: ConnectionHandler;
/// Event generated by the `NetworkBehaviour` and that the swarm will report back.
type OutEvent: Send + 'static;
/// Creates a new [`ConnectionHandler`](crate::ConnectionHandler) for a connection with a peer.
///
/// Every time an incoming connection is opened, and every time another [`NetworkBehaviour`]
/// emitted a dial request, this method is called.
///
/// The returned object is a handler for that specific connection, and will be moved to a
/// background task dedicated to that connection.
///
/// The network behaviour (ie. the implementation of this trait) and the handlers it has spawned
/// (ie. the objects returned by `new_handler`) can communicate by passing messages. Messages
/// sent from the handler to the behaviour are invoked with
/// [`NetworkBehaviour::on_connection_handler_event`],
/// and the behaviour can send a message to the handler by making [`NetworkBehaviour::poll`]
/// return [`ToSwarm::NotifyHandler`].
///
/// Note that the handler is returned to the [`NetworkBehaviour`] on connection failure and
/// connection closing.
#[deprecated(
since = "0.42.0",
note = "Use one or more of `NetworkBehaviour::{handle_pending_inbound_connection,handle_established_inbound_connection,handle_pending_outbound_connection,handle_established_outbound_connection}` instead."
)]
fn new_handler(&mut self) -> Self::ConnectionHandler {
panic!("You must implement `handle_established_inbound_connection` and `handle_established_outbound_connection`.")
}
/// Callback that is invoked for every new inbound connection.
///
/// At this point in the connection lifecycle, only the remote's and our local address are known.
@ -181,16 +151,7 @@ pub trait NetworkBehaviour: 'static {
peer: PeerId,
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
#[allow(deprecated)]
Ok(self.new_handler().into_handler(
&peer,
&ConnectedPoint::Listener {
local_addr: local_addr.clone(),
send_back_addr: remote_addr.clone(),
},
))
}
) -> Result<THandler<Self>, ConnectionDenied>;
/// Callback that is invoked for every outbound connection attempt.
///
@ -207,16 +168,11 @@ pub trait NetworkBehaviour: 'static {
fn handle_pending_outbound_connection(
&mut self,
_connection_id: ConnectionId,
maybe_peer: Option<PeerId>,
_maybe_peer: Option<PeerId>,
_addresses: &[Multiaddr],
_effective_role: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
#[allow(deprecated)]
if let Some(peer_id) = maybe_peer {
Ok(self.addresses_of_peer(&peer_id))
} else {
Ok(vec![])
}
Ok(vec![])
}
/// Callback that is invoked for every established outbound connection.
@ -231,27 +187,7 @@ pub trait NetworkBehaviour: 'static {
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
#[allow(deprecated)]
Ok(self.new_handler().into_handler(
&peer,
&ConnectedPoint::Dialer {
address: addr.clone(),
role_override,
},
))
}
/// Addresses that this behaviour is aware of for this specific peer, and that may allow
/// reaching the peer.
///
/// The addresses will be tried in the order returned by this function, which means that they
/// should be ordered by decreasing likelihood of reachability. In other words, the first
/// address should be the most likely to be reachable.
#[deprecated(note = "Use `NetworkBehaviour::handle_pending_outbound_connection` instead.")]
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
vec![]
}
) -> Result<THandler<Self>, ConnectionDenied>;
/// Informs the behaviour about an event from the [`Swarm`](crate::Swarm).
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>);
@ -480,7 +416,7 @@ pub enum CloseConnection {
/// Enumeration with the list of the possible events
/// to pass to [`on_swarm_event`](NetworkBehaviour::on_swarm_event).
#[allow(deprecated)]
pub enum FromSwarm<'a, Handler: IntoConnectionHandler> {
pub enum FromSwarm<'a, Handler> {
/// Informs the behaviour about a newly established connection to a peer.
ConnectionEstablished(ConnectionEstablished<'a>),
/// Informs the behaviour about a closed connection to a peer.
@ -535,11 +471,11 @@ pub struct ConnectionEstablished<'a> {
/// [`FromSwarm::ConnectionEstablished`] with the same peer ID, connection ID
/// and endpoint.
#[allow(deprecated)]
pub struct ConnectionClosed<'a, Handler: IntoConnectionHandler> {
pub struct ConnectionClosed<'a, Handler> {
pub peer_id: PeerId,
pub connection_id: ConnectionId,
pub endpoint: &'a ConnectedPoint,
pub handler: <Handler as IntoConnectionHandler>::Handler,
pub handler: Handler,
pub remaining_established: usize,
}
@ -625,30 +561,19 @@ pub struct ExpiredExternalAddr<'a> {
pub addr: &'a Multiaddr,
}
#[allow(deprecated)]
impl<'a, Handler: IntoConnectionHandler> FromSwarm<'a, Handler> {
impl<'a, Handler> FromSwarm<'a, Handler> {
fn map_handler<NewHandler>(
self,
map_handler: impl FnOnce(
<Handler as IntoConnectionHandler>::Handler,
) -> <NewHandler as IntoConnectionHandler>::Handler,
) -> FromSwarm<'a, NewHandler>
where
NewHandler: IntoConnectionHandler,
{
map_handler: impl FnOnce(Handler) -> NewHandler,
) -> FromSwarm<'a, NewHandler> {
self.maybe_map_handler(|h| Some(map_handler(h)))
.expect("To return Some as all closures return Some.")
}
fn maybe_map_handler<NewHandler>(
self,
map_handler: impl FnOnce(
<Handler as IntoConnectionHandler>::Handler,
) -> Option<<NewHandler as IntoConnectionHandler>::Handler>,
) -> Option<FromSwarm<'a, NewHandler>>
where
NewHandler: IntoConnectionHandler,
{
map_handler: impl FnOnce(Handler) -> Option<NewHandler>,
) -> Option<FromSwarm<'a, NewHandler>> {
match self {
FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,