Skip to content

Commit ad90167

Browse files
swarm/: Provide additional default impls on NetworkBehaviour (#2150)
Not all implementations of `NetworkBehaviour` need all callbacks. We've have been adding new callbacks with default implementations for a while now. There is no reason the initial ones cannot also be defaulted, thus making it easier create new implementations. Co-authored-by: Max Inden <[email protected]>
1 parent 76f1fcb commit ad90167

File tree

10 files changed

+29
-71
lines changed

10 files changed

+29
-71
lines changed

protocols/floodsub/src/layer.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::topic::Topic;
2323
use crate::FloodsubConfig;
2424
use cuckoofilter::{CuckooError, CuckooFilter};
2525
use fnv::FnvHashSet;
26-
use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId};
26+
use libp2p_core::{PeerId, connection::ConnectionId};
2727
use libp2p_swarm::{
2828
NetworkBehaviour,
2929
NetworkBehaviourAction,
@@ -249,10 +249,6 @@ impl NetworkBehaviour for Floodsub {
249249
Default::default()
250250
}
251251

252-
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
253-
Vec::new()
254-
}
255-
256252
fn inject_connected(&mut self, id: &PeerId) {
257253
// We need to send our subscriptions to the newly-connected node.
258254
if self.target_peers.contains(id) {

protocols/gossipsub/src/behaviour.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,10 +2804,6 @@ where
28042804
)
28052805
}
28062806

2807-
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
2808-
Vec::new()
2809-
}
2810-
28112807
fn inject_connected(&mut self, peer_id: &PeerId) {
28122808
// Ignore connections from blacklisted peers.
28132809
if self.blacklisted_peers.contains(peer_id) {

protocols/identify/src/identify.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,6 @@ impl NetworkBehaviour for Identify {
201201
IdentifyHandler::new(self.config.initial_delay, self.config.interval)
202202
}
203203

204-
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
205-
Vec::new()
206-
}
207-
208-
fn inject_connected(&mut self, _: &PeerId) {
209-
}
210-
211204
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
212205
let addr = match endpoint {
213206
ConnectedPoint::Dialer { address } => address.clone(),
@@ -517,8 +510,8 @@ mod tests {
517510
pin_mut!(swarm2_fut);
518511

519512
match future::select(swarm1_fut, swarm2_fut).await.factor_second().0 {
520-
future::Either::Left(SwarmEvent::Behaviour(IdentifyEvent::Received {
521-
info,
513+
future::Either::Left(SwarmEvent::Behaviour(IdentifyEvent::Received {
514+
info,
522515
..
523516
})) => {
524517
assert_eq!(info.public_key, pubkey2);
@@ -528,8 +521,8 @@ mod tests {
528521
assert!(info.listen_addrs.is_empty());
529522
return;
530523
}
531-
future::Either::Right(SwarmEvent::Behaviour(IdentifyEvent::Received {
532-
info,
524+
future::Either::Right(SwarmEvent::Behaviour(IdentifyEvent::Received {
525+
info,
533526
..
534527
})) => {
535528
assert_eq!(info.public_key, pubkey1);

protocols/mdns/src/behaviour.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use if_watch::{IfEvent, IfWatcher};
2626
use lazy_static::lazy_static;
2727
use libp2p_core::connection::ListenerId;
2828
use libp2p_core::{
29-
address_translation, connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId,
29+
address_translation, multiaddr::Protocol, Multiaddr, PeerId,
3030
};
3131
use libp2p_swarm::{
3232
protocols_handler::DummyProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
@@ -254,14 +254,10 @@ impl NetworkBehaviour for Mdns {
254254
.collect()
255255
}
256256

257-
fn inject_connected(&mut self, _: &PeerId) {}
258-
259-
fn inject_disconnected(&mut self, _: &PeerId) {}
260-
261257
fn inject_event(
262258
&mut self,
263259
_: PeerId,
264-
_: ConnectionId,
260+
_: libp2p_core::connection::ConnectionId,
265261
ev: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
266262
) {
267263
void::unreachable(ev)

protocols/ping/src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub mod handler;
4646
pub use handler::{PingConfig, PingResult, PingSuccess, PingFailure};
4747
use handler::PingHandler;
4848

49-
use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId};
49+
use libp2p_core::{PeerId, connection::ConnectionId};
5050
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
5151
use std::{collections::VecDeque, task::Context, task::Poll};
5252
use void::Void;
@@ -95,14 +95,6 @@ impl NetworkBehaviour for Ping {
9595
PingHandler::new(self.config.clone())
9696
}
9797

98-
fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec<Multiaddr> {
99-
Vec::new()
100-
}
101-
102-
fn inject_connected(&mut self, _: &PeerId) {}
103-
104-
fn inject_disconnected(&mut self, _: &PeerId) {}
105-
10698
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: PingResult) {
10799
self.events.push_front(PingEvent { peer, result })
108100
}

protocols/relay/tests/lib.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,24 +1358,13 @@ impl libp2p_swarm::NetworkBehaviour for KeepAliveBehaviour {
13581358
}
13591359
}
13601360

1361-
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
1362-
Vec::new()
1363-
}
1364-
1365-
fn inject_connected(&mut self, _: &PeerId) {}
1366-
1367-
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}
1368-
1369-
fn inject_disconnected(&mut self, _: &PeerId) {}
1370-
1371-
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}
1372-
13731361
fn inject_event(
13741362
&mut self,
13751363
_: PeerId,
13761364
_: ConnectionId,
1377-
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
1365+
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
13781366
) {
1367+
void::unreachable(event);
13791368
}
13801369

13811370
fn poll(

swarm/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
- Update dependencies.
44

5+
- Provide default implementations for all functions of `NetworkBehaviour`,
6+
except for `new_handler`, `inject_event` and `poll`.
7+
This should make it easier to create new implementations. See [PR 2150].
8+
9+
[PR 2150]: https://github.com/libp2p/rust-libp2p/pull/2150/
10+
511
# 0.30.0 [2021-07-12]
612

713
- Update dependencies.

swarm/src/behaviour.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ pub trait NetworkBehaviour: Send + 'static {
8080
/// The addresses will be tried in the order returned by this function, which means that they
8181
/// should be ordered by decreasing likelihood of reachability. In other words, the first
8282
/// address should be the most likely to be reachable.
83-
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
83+
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
84+
vec![]
85+
}
8486

8587
/// Indicate to the behaviour that we connected to the node with the given peer id.
8688
///
8789
/// This node now has a handler (as spawned by `new_handler`) running in the background.
8890
///
8991
/// This method is only called when the first connection to the peer is established, preceded by
9092
/// [`inject_connection_established`](NetworkBehaviour::inject_connection_established).
91-
fn inject_connected(&mut self, peer_id: &PeerId);
93+
fn inject_connected(&mut self, _: &PeerId) { }
9294

9395
/// Indicates to the behaviour that we disconnected from the node with the given peer id.
9496
///
@@ -97,7 +99,7 @@ pub trait NetworkBehaviour: Send + 'static {
9799
///
98100
/// This method is only called when the last established connection to the peer is closed,
99101
/// preceded by [`inject_connection_closed`](NetworkBehaviour::inject_connection_closed).
100-
fn inject_disconnected(&mut self, peer_id: &PeerId);
102+
fn inject_disconnected(&mut self, _: &PeerId) { }
101103

102104
/// Informs the behaviour about a newly established connection to a peer.
103105
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)

swarm/src/lib.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
605605
}
606606
this.behaviour.inject_new_listen_addr(listener_id, &listen_addr);
607607
return Poll::Ready(SwarmEvent::NewListenAddr {
608-
listener_id,
608+
listener_id,
609609
address: listen_addr
610610
});
611611
}
@@ -1156,21 +1156,15 @@ impl NetworkBehaviour for DummyBehaviour {
11561156
protocols_handler::DummyProtocolsHandler::default()
11571157
}
11581158

1159-
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
1160-
Vec::new()
1159+
fn inject_event(
1160+
&mut self,
1161+
_: PeerId,
1162+
_: ConnectionId,
1163+
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent
1164+
) {
1165+
void::unreachable(event)
11611166
}
11621167

1163-
fn inject_connected(&mut self, _: &PeerId) {}
1164-
1165-
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}
1166-
1167-
fn inject_disconnected(&mut self, _: &PeerId) {}
1168-
1169-
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}
1170-
1171-
fn inject_event(&mut self, _: PeerId, _: ConnectionId,
1172-
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent) {}
1173-
11741168
fn poll(&mut self, _: &mut Context<'_>, _: &mut impl PollParameters) ->
11751169
Poll<NetworkBehaviourAction<<Self::ProtocolsHandler as
11761170
ProtocolsHandler>::InEvent, Self::OutEvent>>

swarm/src/test.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ where
8282
self.addresses.get(p).map_or(Vec::new(), |v| v.clone())
8383
}
8484

85-
fn inject_connected(&mut self, _: &PeerId) {
86-
}
87-
88-
fn inject_disconnected(&mut self, _: &PeerId) {
89-
}
90-
9185
fn inject_event(&mut self, _: PeerId, _: ConnectionId, _: THandler::OutEvent) {
9286
}
9387

0 commit comments

Comments
 (0)