refactor(swarm): remove deprecated inject calls (#3264)

Finishes work first started with https://github.com/libp2p/rust-libp2p/issues/2832
This commit is contained in:
João Oliveira
2023-01-12 11:21:02 +00:00
committed by GitHub
parent 3cc824796d
commit 4c65c7d7c7
23 changed files with 1177 additions and 1376 deletions

View File

@ -140,7 +140,8 @@ pub trait NetworkBehaviour: 'static {
///
/// 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 injected with [`NetworkBehaviour::inject_event`],
/// 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 [`NetworkBehaviourAction::NotifyHandler`].
///
@ -159,7 +160,7 @@ pub trait NetworkBehaviour: 'static {
}
/// Informs the behaviour about an event from the [`Swarm`](crate::Swarm).
fn on_swarm_event(&mut self, _event: FromSwarm<Self::ConnectionHandler>) {}
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>);
/// Informs the behaviour about an event generated by the [`ConnectionHandler`] dedicated to the
/// peer identified by `peer_id`. for the behaviour.
@ -175,209 +176,6 @@ pub trait NetworkBehaviour: 'static {
) {
}
/// Informs the behaviour about a newly established connection to a peer.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ConnectionEstablished` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_connection_established(
&mut self,
peer_id: &PeerId,
connection_id: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id: *peer_id,
connection_id: *connection_id,
endpoint,
failed_addresses: failed_addresses
.map(|v| v.as_slice())
.unwrap_or_else(|| &[]),
other_established,
}));
}
/// Informs the behaviour about a closed connection to a peer.
///
/// A call to this method is always paired with an earlier call to
/// [`NetworkBehaviour::inject_connection_established`] with the same peer ID, connection ID and endpoint.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ConnectionClosed` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_connection_closed(
&mut self,
peer_id: &PeerId,
connection_id: &ConnectionId,
endpoint: &ConnectedPoint,
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
remaining_established: usize,
) {
self.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id: *peer_id,
connection_id: *connection_id,
endpoint,
handler,
remaining_established,
}));
}
/// Informs the behaviour that the [`ConnectedPoint`] of an existing connection has changed.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::AddressChange` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_address_change(
&mut self,
peer_id: &PeerId,
connection_id: &ConnectionId,
old: &ConnectedPoint,
new: &ConnectedPoint,
) {
self.on_swarm_event(FromSwarm::AddressChange(AddressChange {
peer_id: *peer_id,
connection_id: *connection_id,
old,
new,
}));
}
/// Informs the behaviour about an event generated by the handler dedicated to the peer identified by `peer_id`.
/// for the behaviour.
///
/// The `peer_id` is guaranteed to be in a connected state. In other words,
/// [`NetworkBehaviour::inject_connection_established`] has previously been called with this `PeerId`.
#[deprecated(
since = "0.40.2",
note = "Implement `NetworkBehaviour::on_connection_handler_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
) {
self.on_connection_handler_event(peer_id, connection, event);
}
/// Indicates to the behaviour that the dial to a known or unknown node failed.
#[deprecated(
since = "0.40.2",
note = "Handle `InEvent::DialFailure` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
handler: Self::ConnectionHandler,
error: &DialError,
) {
self.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error,
}));
}
/// Indicates to the behaviour that an error happened on an incoming connection during its
/// initial handshake.
///
/// This can include, for example, an error during the handshake of the encryption layer, or the
/// connection unexpectedly closed.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ListenFailure` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_listen_failure(
&mut self,
local_addr: &Multiaddr,
send_back_addr: &Multiaddr,
handler: Self::ConnectionHandler,
) {
self.on_swarm_event(FromSwarm::ListenFailure(ListenFailure {
local_addr,
send_back_addr,
handler,
}));
}
/// Indicates to the behaviour that a new listener was created.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::NewListener` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_new_listener(&mut self, id: ListenerId) {
self.on_swarm_event(FromSwarm::NewListener(NewListener { listener_id: id }));
}
/// Indicates to the behaviour that we have started listening on a new multiaddr.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::NewListenAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_new_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
self.on_swarm_event(FromSwarm::NewListenAddr(NewListenAddr {
listener_id: id,
addr,
}));
}
/// Indicates to the behaviour that a multiaddr we were listening on has expired,
/// which means that we are no longer listening on it.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ExpiredListenAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_expired_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
self.on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr {
listener_id: id,
addr,
}));
}
/// A listener experienced an error.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ListenerError` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_listener_error(&mut self, id: ListenerId, err: &(dyn std::error::Error + 'static)) {
self.on_swarm_event(FromSwarm::ListenerError(ListenerError {
listener_id: id,
err,
}));
}
/// A listener closed.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ListenerClosed` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &std::io::Error>) {
self.on_swarm_event(FromSwarm::ListenerClosed(ListenerClosed {
listener_id: id,
reason,
}));
}
/// Indicates to the behaviour that we have discovered a new external address for us.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::NewExternalAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
self.on_swarm_event(FromSwarm::NewExternalAddr(NewExternalAddr { addr }));
}
/// Indicates to the behaviour that an external address was removed.
#[deprecated(
since = "0.40.2",
note = "Handle `FromSwarm::ExpiredExternalAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it."
)]
fn inject_expired_external_addr(&mut self, addr: &Multiaddr) {
self.on_swarm_event(FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr }));
}
/// Polls for things that swarm should do.
///
/// This API mimics the API of the `Stream` trait. The method may register the current task in
@ -447,8 +245,8 @@ pub enum NetworkBehaviourAction<
/// Instructs the swarm to start a dial.
///
/// On success, [`NetworkBehaviour::inject_connection_established`] is invoked.
/// On failure, [`NetworkBehaviour::inject_dial_failure`] is invoked.
/// On success, [`NetworkBehaviour::on_swarm_event`] with `ConnectionEstablished` is invoked.
/// On failure, [`NetworkBehaviour::on_swarm_event`] with `DialFailure` is invoked.
///
/// Note that the provided handler is returned to the [`NetworkBehaviour`] on connection failure
/// and connection closing. Thus it can be used to carry state, which otherwise would have to be
@ -468,10 +266,11 @@ pub enum NetworkBehaviourAction<
/// # use libp2p_core::PeerId;
/// # use libp2p_plaintext::PlainText2Config;
/// # use libp2p_swarm::{
/// # DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream,
/// # FromSwarm, DialFailure, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream,
/// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler,
/// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent,
/// # };
/// # use libp2p_swarm::handler::ConnectionEvent;
/// # use libp2p_swarm::dial_opts::{DialOpts, PeerCondition};
/// # use libp2p_yamux as yamux;
/// # use std::collections::VecDeque;
@ -533,7 +332,7 @@ pub enum NetworkBehaviourAction<
/// # }
/// #
/// #
/// # fn inject_event(
/// # fn on_connection_handler_event(
/// # &mut self,
/// # _: PeerId,
/// # _: ConnectionId,
@ -542,17 +341,17 @@ pub enum NetworkBehaviourAction<
/// # unreachable!();
/// # }
/// #
/// fn inject_dial_failure(
/// fn on_swarm_event(
/// &mut self,
/// _: Option<PeerId>,
/// handler: Self::ConnectionHandler,
/// _: &DialError,
/// event: FromSwarm<Self::ConnectionHandler>,
/// ) {
/// // As expected, sending the message failed. But lucky us, we got the handler back, thus
/// // the precious message is not lost and we can return it back to the user.
/// let msg = handler.message.unwrap();
/// self.outbox_to_swarm
/// .push_back(NetworkBehaviourAction::GenerateEvent(msg))
/// if let FromSwarm::DialFailure(DialFailure { handler, .. }) = event {
/// let msg = handler.message.unwrap();
/// self.outbox_to_swarm
/// .push_back(NetworkBehaviourAction::GenerateEvent(msg))
/// }
/// }
/// #
/// # fn poll(
@ -586,28 +385,17 @@ pub enum NetworkBehaviourAction<
/// # SubstreamProtocol::new(DeniedUpgrade, ())
/// # }
/// #
/// # fn inject_fully_negotiated_inbound(
/// # &mut self,
/// # _: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
/// # _: Self::InboundOpenInfo,
/// # ) {
/// # }
/// # fn on_behaviour_event(&mut self, _event: Self::InEvent) {}
/// #
/// # fn inject_fully_negotiated_outbound(
/// # &mut self,
/// # _: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
/// # _: Self::OutboundOpenInfo,
/// # ) {
/// # }
/// #
/// # fn inject_event(&mut self, _event: Self::InEvent) {}
/// #
/// # fn inject_dial_upgrade_error(
/// # &mut self,
/// # _: Self::OutboundOpenInfo,
/// # _: ConnectionHandlerUpgrErr<Void>,
/// # ) {
/// # }
/// # fn on_connection_event(
/// # &mut self,
/// # event: ConnectionEvent<
/// # Self::InboundProtocol,
/// # Self::OutboundProtocol,
/// # Self::InboundOpenInfo,
/// # Self::OutboundOpenInfo,
/// # >,
/// # ) {}
/// #
/// # fn connection_keep_alive(&self) -> KeepAlive {
/// # KeepAlive::Yes
@ -641,7 +429,7 @@ pub enum NetworkBehaviourAction<
/// If the specified connection no longer exists, the event is silently dropped.
///
/// Typically the connection ID given is the same as the one passed to
/// [`NetworkBehaviour::inject_event`], i.e. whenever the behaviour wishes to
/// [`NetworkBehaviour::on_connection_handler_event`], i.e. whenever the behaviour wishes to
/// respond to a request on the same connection (and possibly the same
/// substream, as per the implementation of [`ConnectionHandler`]).
///
@ -1128,103 +916,3 @@ impl<'a, Handler: IntoConnectionHandler> FromSwarm<'a, Handler> {
}
}
}
/// Helper function to call [`NetworkBehaviour`]'s `inject_*` methods given a `FromSwarm.
/// TODO: Remove this function when we remove the remaining `inject_*` calls
/// from [`Either`] and [`Toggle`].
pub(crate) fn inject_from_swarm<T: NetworkBehaviour>(
behaviour: &mut T,
event: FromSwarm<T::ConnectionHandler>,
) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id,
connection_id,
endpoint,
failed_addresses,
other_established,
}) => {
#[allow(deprecated)]
behaviour.inject_connection_established(
&peer_id,
&connection_id,
endpoint,
Some(&failed_addresses.into()),
other_established,
);
}
FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}) => {
#[allow(deprecated)]
behaviour.inject_connection_closed(
&peer_id,
&connection_id,
endpoint,
handler,
remaining_established,
);
}
FromSwarm::AddressChange(AddressChange {
peer_id,
connection_id,
old,
new,
}) => {
#[allow(deprecated)]
behaviour.inject_address_change(&peer_id, &connection_id, old, new);
}
FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error,
}) => {
#[allow(deprecated)]
behaviour.inject_dial_failure(peer_id, handler, error);
}
FromSwarm::ListenFailure(ListenFailure {
local_addr,
send_back_addr,
handler,
}) => {
#[allow(deprecated)]
behaviour.inject_listen_failure(local_addr, send_back_addr, handler);
}
FromSwarm::NewListener(NewListener { listener_id }) => {
#[allow(deprecated)]
behaviour.inject_new_listener(listener_id);
}
FromSwarm::NewListenAddr(NewListenAddr { listener_id, addr }) => {
#[allow(deprecated)]
behaviour.inject_new_listen_addr(listener_id, addr);
}
FromSwarm::ExpiredListenAddr(ExpiredListenAddr { listener_id, addr }) => {
#[allow(deprecated)]
behaviour.inject_expired_listen_addr(listener_id, addr);
}
FromSwarm::ListenerError(ListenerError { listener_id, err }) => {
#[allow(deprecated)]
behaviour.inject_listener_error(listener_id, err);
}
FromSwarm::ListenerClosed(ListenerClosed {
listener_id,
reason,
}) => {
#[allow(deprecated)]
behaviour.inject_listener_closed(listener_id, reason);
}
FromSwarm::NewExternalAddr(NewExternalAddr { addr }) => {
#[allow(deprecated)]
behaviour.inject_new_external_addr(addr);
}
FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr }) =>
{
#[allow(deprecated)]
behaviour.inject_expired_external_addr(addr)
}
}
}