Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use libp2p_swarm::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm,
},
ConnectionHandler, ConnectionId, ExternalAddresses, IntoConnectionHandler, ListenAddresses,
NetworkBehaviour, NetworkBehaviourAction, PollParameters,
ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction,
PollParameters, THandlerOutEvent,
};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -553,8 +553,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
event: THandlerOutEvent<Self>,
) {
self.inner
.on_connection_handler_event(peer_id, connection_id, event)
Expand Down
9 changes: 4 additions & 5 deletions protocols/dcutr/src/behaviour_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ use libp2p_core::multiaddr::Protocol;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, IntoConnectionHandler,
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters,
};
use libp2p_swarm::{ConnectionId, THandlerOutEvent};
use std::collections::{HashMap, HashSet, VecDeque};
use std::task::{Context, Poll};
use thiserror::Error;
Expand Down Expand Up @@ -222,8 +222,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
event_source: PeerId,
connection: ConnectionId,
handler_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
handler_event: THandlerOutEvent<Self>,
) {
match handler_event {
Either::Left(handler::relayed::Event::InboundConnectRequest {
Expand Down
6 changes: 2 additions & 4 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::{
dial_opts::DialOpts, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
OneShotHandler, PollParameters,
OneShotHandler, PollParameters, THandlerOutEvent,
};
use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler};
use log::warn;
use smallvec::SmallVec;
use std::collections::hash_map::{DefaultHasher, HashMap};
Expand Down Expand Up @@ -352,8 +351,7 @@ impl NetworkBehaviour for Floodsub {
&mut self,
propagation_source: PeerId,
_connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
event: THandlerOutEvent<Self>,
) {
// We ignore successful sends or timeouts.
let event = match event {
Expand Down
7 changes: 3 additions & 4 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use libp2p_core::{
use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm},
dial_opts::DialOpts,
ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
THandlerOutEvent,
};
use wasm_timer::Instant;

Expand Down Expand Up @@ -3313,8 +3313,7 @@ where
&mut self,
propagation_source: PeerId,
_connection_id: ConnectionId,
handler_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
handler_event: THandlerOutEvent<Self>,
) {
match handler_event {
HandlerEvent::PeerKind(kind) => {
Expand Down
9 changes: 4 additions & 5 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ use crate::handler::{self, InEvent, Proto};
use crate::protocol::{Info, Protocol, UpgradeError};
use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
dial_opts::DialOpts, AddressScore, ConnectionHandlerUpgrErr, DialError, ExternalAddresses,
ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
use libp2p_swarm::{ConnectionId, THandlerOutEvent};
use lru::LruCache;
use std::num::NonZeroUsize;
use std::{
Expand Down Expand Up @@ -253,7 +252,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
event: THandlerOutEvent<Self>,
) {
match event {
handler::Event::Identified(mut info) => {
Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use libp2p_swarm::behaviour::{
use libp2p_swarm::{
dial_opts::{self, DialOpts},
ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
};
use log::{debug, info, warn};
use smallvec::SmallVec;
Expand Down Expand Up @@ -2026,7 +2026,7 @@ where
&mut self,
source: PeerId,
connection: ConnectionId,
event: KademliaHandlerEvent<QueryId>,
event: THandlerOutEvent<Self>,
) {
match event {
KademliaHandlerEvent::ProtocolConfirmed { endpoint } => {
Expand Down
6 changes: 3 additions & 3 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use if_watch::IfEvent;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
dummy, ConnectionHandler, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction,
PollParameters,
dummy, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
THandlerOutEvent,
};
use smallvec::SmallVec;
use std::collections::hash_map::{Entry, HashMap};
Expand Down Expand Up @@ -190,7 +190,7 @@ where
&mut self,
_: PeerId,
_: libp2p_swarm::ConnectionId,
ev: <Self::ConnectionHandler as ConnectionHandler>::OutEvent,
ev: THandlerOutEvent<Self>,
) {
void::unreachable(ev)
}
Expand Down
8 changes: 7 additions & 1 deletion protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub use handler::{Config, Failure, Success};
use libp2p_core::PeerId;
use libp2p_swarm::{
behaviour::FromSwarm, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
THandlerOutEvent,
};
use std::{
collections::VecDeque,
Expand Down Expand Up @@ -123,7 +124,12 @@ impl NetworkBehaviour for Behaviour {
Handler::new(self.config.clone())
}

fn on_connection_handler_event(&mut self, peer: PeerId, _: ConnectionId, result: Result) {
fn on_connection_handler_event(
&mut self,
peer: PeerId,
_: ConnectionId,
result: THandlerOutEvent<Self>,
) {
self.events.push_front(Event { peer, result })
}

Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
};
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::num::NonZeroU32;
Expand Down Expand Up @@ -287,7 +287,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
event_source: PeerId,
connection: ConnectionId,
event: Either<handler::Event, void::Void>,
event: THandlerOutEvent<Self>,
) {
let event = match event {
Either::Left(e) => e,
Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm
use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::{
ConnectionHandlerUpgrErr, ConnectionId, NegotiatedSubstream, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
};
use std::collections::{hash_map, HashMap, VecDeque};
use std::io::{Error, ErrorKind, IoSlice};
Expand Down Expand Up @@ -194,7 +194,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
event_source: PeerId,
_connection: ConnectionId,
handler_event: Either<handler::Event, void::Void>,
handler_event: THandlerOutEvent<Self>,
) {
let handler_event = match handler_event {
Either::Left(e) => e,
Expand Down
4 changes: 2 additions & 2 deletions protocols/rendezvous/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use libp2p_core::{Multiaddr, PeerId, PeerRecord};
use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::{
CloseConnection, ConnectionId, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters,
NotifyHandler, PollParameters, THandlerOutEvent,
};
use std::collections::{HashMap, VecDeque};
use std::iter::FromIterator;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: handler::OutboundOutEvent,
event: THandlerOutEvent<Self>,
) {
let new_events = match event {
handler::OutboundOutEvent::InboundEvent { message, .. } => void::unreachable(message),
Expand Down
4 changes: 2 additions & 2 deletions protocols/rendezvous/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use libp2p_core::PeerId;
use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::{
CloseConnection, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
PollParameters, THandlerOutEvent,
};
use std::collections::{HashMap, HashSet, VecDeque};
use std::iter::FromIterator;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: handler::InboundOutEvent,
event: THandlerOutEvent<Self>,
) {
let new_events = match event {
handler::InboundOutEvent::InboundEvent { id, message } => {
Expand Down
7 changes: 3 additions & 4 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
dial_opts::DialOpts,
ConnectionId, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
THandlerOutEvent,
};
use smallvec::SmallVec;
use std::{
Expand Down Expand Up @@ -776,8 +776,7 @@ where
&mut self,
peer: PeerId,
connection: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
libp2p_swarm::ConnectionHandler>::OutEvent,
event: THandlerOutEvent<Self>,
) {
match event {
handler::Event::Response {
Expand Down
12 changes: 4 additions & 8 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ pub use listen_addresses::ListenAddresses;
use crate::connection::ConnectionId;
use crate::dial_opts::DialOpts;
use crate::handler::{ConnectionHandler, IntoConnectionHandler};
use crate::{AddressRecord, AddressScore, DialError};
use crate::{AddressRecord, AddressScore, DialError, THandlerOutEvent};
use libp2p_core::{transport::ListenerId, ConnectedPoint, Multiaddr, PeerId};
use std::{task::Context, task::Poll};

/// Custom event that can be received by the [`ConnectionHandler`].
pub(crate) type THandlerInEvent<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;

pub(crate) type THandlerOutEvent<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;

/// A [`NetworkBehaviour`] defines the behaviour of the local node on the network.
///
/// In contrast to [`Transport`](libp2p_core::Transport) which defines **how** to send bytes on the
Expand Down Expand Up @@ -122,7 +119,7 @@ pub(crate) type THandlerOutEvent<THandler> =
/// }
/// }
/// ```
pub trait NetworkBehaviour: 'static {
pub trait NetworkBehaviour: Sized + 'static {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This requires a Sized bound now but I'd honestly not know why a NetworkBehaviourwouldn't be sized ...

Copy link
Member

Choose a reason for hiding this comment

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

How is the requirement to be Sized related to this change?

Copy link
Contributor Author

@thomaseizinger thomaseizinger Jan 23, 2023

Choose a reason for hiding this comment

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

I think it is because I am now using Self within a function signature via a type-alias. To be frank, I just followed the compiler 🙃

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to avoid casually introducing trait bounds without careful consideration. Is there any way around introducing this trait bound? Is this pull request worth doing given the added complexity.

I would have to dig deeper here. Based on intuition, adding Sized, Send or Sync unintentionally has not proven to be good ideas in the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point! I never looked deeply into it but apparently, requiring Sized prohibits the use as a trait object: https://doc.rust-lang.org/std/marker/trait.Sized.html

I'll try to work around it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out it is not needed, lol 😂

Copy link
Member

Choose a reason for hiding this comment

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

Fair point! I never looked deeply into it but apparently, requiring Sized prohibits the use as a trait object:

I'll try to work around it.

yeah, that's why you can't have Box<dyn Clone> for example

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for investigating. Great to see it not being needed.

/// Handler for all the protocols the network behaviour supports.
type ConnectionHandler: IntoConnectionHandler;

Expand Down Expand Up @@ -170,8 +167,7 @@ pub trait NetworkBehaviour: 'static {
&mut self,
_peer_id: PeerId,
_connection_id: ConnectionId,
_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
_event: THandlerOutEvent<Self>,
) {
}

Expand Down Expand Up @@ -335,7 +331,7 @@ pub enum NetworkBehaviourAction<
/// # &mut self,
/// # _: PeerId,
/// # _: ConnectionId,
/// # _: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
/// # _: THandlerOutEvent<Self>,
/// # ) {
/// # unreachable!();
/// # }
Expand Down
3 changes: 2 additions & 1 deletion swarm/src/behaviour/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::connection::ConnectionId;
use crate::handler::either::IntoEitherHandler;
use crate::THandlerOutEvent;
use either::Either;
use libp2p_core::{Multiaddr, PeerId};
use std::{task::Context, task::Poll};
Expand Down Expand Up @@ -71,7 +72,7 @@ where
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: crate::THandlerOutEvent<Self>,
event: THandlerOutEvent<Self>,
) {
match (self, event) {
(Either::Left(left), Either::Left(event)) => {
Expand Down
4 changes: 2 additions & 2 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::handler::{
IntoConnectionHandler, KeepAlive, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::SendWrapper;
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, THandlerOutEvent};
use either::Either;
use libp2p_core::{
either::EitherOutput, upgrade::DeniedUpgrade, ConnectedPoint, Multiaddr, PeerId,
Expand Down Expand Up @@ -95,7 +95,7 @@ where
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: crate::THandlerOutEvent<Self>,
event: THandlerOutEvent<Self>,
) {
if let Some(behaviour) = &mut self.inner {
behaviour.on_connection_handler_event(peer_id, connection_id, event)
Expand Down
4 changes: 2 additions & 2 deletions swarm/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use crate::connection::{Connection, ConnectionId, PendingPoint};
use crate::{
behaviour::{THandlerInEvent, THandlerOutEvent},
behaviour::THandlerInEvent,
connection::{
Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError,
PendingInboundConnectionError, PendingOutboundConnectionError,
Expand Down Expand Up @@ -297,7 +297,7 @@ pub enum PoolEvent<THandler: IntoConnectionHandler> {
id: ConnectionId,
peer_id: PeerId,
/// The produced event.
event: THandlerOutEvent<THandler>,
event: <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I temporarily inlined this one so that I can delete the 2nd type alias named THandlerOutEvent defined in swarm::behaviour.

This will be simplified to ConnectionHandler::OutEvent in #3254.

},

/// The connection to a node has changed its address.
Expand Down
12 changes: 10 additions & 2 deletions swarm/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::connection::ConnectionId;
use crate::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol};
use crate::{
ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol,
THandlerOutEvent,
};
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::PeerId;
use libp2p_core::UpgradeError;
Expand All @@ -21,7 +24,12 @@ impl NetworkBehaviour for Behaviour {
ConnectionHandler
}

fn on_connection_handler_event(&mut self, _: PeerId, _: ConnectionId, event: Void) {
fn on_connection_handler_event(
&mut self,
_: PeerId,
_: ConnectionId,
event: THandlerOutEvent<Self>,
) {
void::unreachable(event)
}

Expand Down
Loading