diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index cf43354a11f..d1a6e27b909 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -23,7 +23,7 @@ use crate::topic::Topic; use crate::FloodsubConfig; use cuckoofilter::{CuckooError, CuckooFilter}; use fnv::FnvHashSet; -use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId}; +use libp2p_core::{PeerId, connection::ConnectionId}; use libp2p_swarm::{ NetworkBehaviour, NetworkBehaviourAction, @@ -249,10 +249,6 @@ impl NetworkBehaviour for Floodsub { Default::default() } - fn addresses_of_peer(&mut self, _: &PeerId) -> Vec { - Vec::new() - } - fn inject_connected(&mut self, id: &PeerId) { // We need to send our subscriptions to the newly-connected node. if self.target_peers.contains(id) { diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index ea6f6a4aa9f..8a9c1b9efe0 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -2804,10 +2804,6 @@ where ) } - fn addresses_of_peer(&mut self, _: &PeerId) -> Vec { - Vec::new() - } - fn inject_connected(&mut self, peer_id: &PeerId) { // Ignore connections from blacklisted peers. if self.blacklisted_peers.contains(peer_id) { diff --git a/protocols/identify/src/identify.rs b/protocols/identify/src/identify.rs index f9c9b137847..0b60cb03fc9 100644 --- a/protocols/identify/src/identify.rs +++ b/protocols/identify/src/identify.rs @@ -201,13 +201,6 @@ impl NetworkBehaviour for Identify { IdentifyHandler::new(self.config.initial_delay, self.config.interval) } - fn addresses_of_peer(&mut self, _: &PeerId) -> Vec { - Vec::new() - } - - fn inject_connected(&mut self, _: &PeerId) { - } - fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) { let addr = match endpoint { ConnectedPoint::Dialer { address } => address.clone(), @@ -517,8 +510,8 @@ mod tests { pin_mut!(swarm2_fut); match future::select(swarm1_fut, swarm2_fut).await.factor_second().0 { - future::Either::Left(SwarmEvent::Behaviour(IdentifyEvent::Received { - info, + future::Either::Left(SwarmEvent::Behaviour(IdentifyEvent::Received { + info, .. })) => { assert_eq!(info.public_key, pubkey2); @@ -528,8 +521,8 @@ mod tests { assert!(info.listen_addrs.is_empty()); return; } - future::Either::Right(SwarmEvent::Behaviour(IdentifyEvent::Received { - info, + future::Either::Right(SwarmEvent::Behaviour(IdentifyEvent::Received { + info, .. })) => { assert_eq!(info.public_key, pubkey1); diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 95513eda49c..2e88d720ade 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -26,7 +26,7 @@ use if_watch::{IfEvent, IfWatcher}; use lazy_static::lazy_static; use libp2p_core::connection::ListenerId; use libp2p_core::{ - address_translation, connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId, + address_translation, multiaddr::Protocol, Multiaddr, PeerId, }; use libp2p_swarm::{ protocols_handler::DummyProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, @@ -254,14 +254,10 @@ impl NetworkBehaviour for Mdns { .collect() } - fn inject_connected(&mut self, _: &PeerId) {} - - fn inject_disconnected(&mut self, _: &PeerId) {} - fn inject_event( &mut self, _: PeerId, - _: ConnectionId, + _: libp2p_core::connection::ConnectionId, ev: ::OutEvent, ) { void::unreachable(ev) diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 659ab9f02b7..cd9cc227c9d 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -46,7 +46,7 @@ pub mod handler; pub use handler::{PingConfig, PingResult, PingSuccess, PingFailure}; use handler::PingHandler; -use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId}; +use libp2p_core::{PeerId, connection::ConnectionId}; use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use std::{collections::VecDeque, task::Context, task::Poll}; use void::Void; @@ -95,14 +95,6 @@ impl NetworkBehaviour for Ping { PingHandler::new(self.config.clone()) } - fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec { - Vec::new() - } - - fn inject_connected(&mut self, _: &PeerId) {} - - fn inject_disconnected(&mut self, _: &PeerId) {} - fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: PingResult) { self.events.push_front(PingEvent { peer, result }) } diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index 601993f5619..ebdab24d019 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -1358,24 +1358,13 @@ impl libp2p_swarm::NetworkBehaviour for KeepAliveBehaviour { } } - fn addresses_of_peer(&mut self, _: &PeerId) -> Vec { - Vec::new() - } - - fn inject_connected(&mut self, _: &PeerId) {} - - fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {} - - fn inject_disconnected(&mut self, _: &PeerId) {} - - fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {} - fn inject_event( &mut self, _: PeerId, _: ConnectionId, - _: ::OutEvent, + event: ::OutEvent, ) { + void::unreachable(event); } fn poll( diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 05141bb8622..616d45a7f1a 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -2,6 +2,12 @@ - Update dependencies. +- Provide default implementations for all functions of `NetworkBehaviour`, + except for `new_handler`, `inject_event` and `poll`. + This should make it easier to create new implementations. See [PR 2150]. + +[PR 2150]: https://github.com/libp2p/rust-libp2p/pull/2150/ + # 0.30.0 [2021-07-12] - Update dependencies. diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 432cef9514b..e1bde697148 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -80,7 +80,9 @@ pub trait NetworkBehaviour: Send + 'static { /// 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. - fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec; + fn addresses_of_peer(&mut self, _: &PeerId) -> Vec { + vec![] + } /// Indicate to the behaviour that we connected to the node with the given peer id. /// @@ -88,7 +90,7 @@ pub trait NetworkBehaviour: Send + 'static { /// /// This method is only called when the first connection to the peer is established, preceded by /// [`inject_connection_established`](NetworkBehaviour::inject_connection_established). - fn inject_connected(&mut self, peer_id: &PeerId); + fn inject_connected(&mut self, _: &PeerId) { } /// Indicates to the behaviour that we disconnected from the node with the given peer id. /// @@ -97,7 +99,7 @@ pub trait NetworkBehaviour: Send + 'static { /// /// This method is only called when the last established connection to the peer is closed, /// preceded by [`inject_connection_closed`](NetworkBehaviour::inject_connection_closed). - fn inject_disconnected(&mut self, peer_id: &PeerId); + fn inject_disconnected(&mut self, _: &PeerId) { } /// Informs the behaviour about a newly established connection to a peer. fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index f79ef5a0462..00309603c60 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -605,7 +605,7 @@ where TBehaviour: NetworkBehaviour, } this.behaviour.inject_new_listen_addr(listener_id, &listen_addr); return Poll::Ready(SwarmEvent::NewListenAddr { - listener_id, + listener_id, address: listen_addr }); } @@ -1156,21 +1156,15 @@ impl NetworkBehaviour for DummyBehaviour { protocols_handler::DummyProtocolsHandler::default() } - fn addresses_of_peer(&mut self, _: &PeerId) -> Vec { - Vec::new() + fn inject_event( + &mut self, + _: PeerId, + _: ConnectionId, + event: ::OutEvent + ) { + void::unreachable(event) } - fn inject_connected(&mut self, _: &PeerId) {} - - fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {} - - fn inject_disconnected(&mut self, _: &PeerId) {} - - fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {} - - fn inject_event(&mut self, _: PeerId, _: ConnectionId, - _: ::OutEvent) {} - fn poll(&mut self, _: &mut Context<'_>, _: &mut impl PollParameters) -> Poll::InEvent, Self::OutEvent>> diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 6bb11a34cc0..4ae647d38db 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -82,12 +82,6 @@ where self.addresses.get(p).map_or(Vec::new(), |v| v.clone()) } - fn inject_connected(&mut self, _: &PeerId) { - } - - fn inject_disconnected(&mut self, _: &PeerId) { - } - fn inject_event(&mut self, _: PeerId, _: ConnectionId, _: THandler::OutEvent) { }