mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-14 18:41:22 +00:00
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 <mail@max-inden.de>
This commit is contained in:
@ -23,7 +23,7 @@ use crate::topic::Topic;
|
|||||||
use crate::FloodsubConfig;
|
use crate::FloodsubConfig;
|
||||||
use cuckoofilter::{CuckooError, CuckooFilter};
|
use cuckoofilter::{CuckooError, CuckooFilter};
|
||||||
use fnv::FnvHashSet;
|
use fnv::FnvHashSet;
|
||||||
use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId};
|
use libp2p_core::{PeerId, connection::ConnectionId};
|
||||||
use libp2p_swarm::{
|
use libp2p_swarm::{
|
||||||
NetworkBehaviour,
|
NetworkBehaviour,
|
||||||
NetworkBehaviourAction,
|
NetworkBehaviourAction,
|
||||||
@ -249,10 +249,6 @@ impl NetworkBehaviour for Floodsub {
|
|||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inject_connected(&mut self, id: &PeerId) {
|
fn inject_connected(&mut self, id: &PeerId) {
|
||||||
// We need to send our subscriptions to the newly-connected node.
|
// We need to send our subscriptions to the newly-connected node.
|
||||||
if self.target_peers.contains(id) {
|
if self.target_peers.contains(id) {
|
||||||
|
@ -2804,10 +2804,6 @@ where
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inject_connected(&mut self, peer_id: &PeerId) {
|
fn inject_connected(&mut self, peer_id: &PeerId) {
|
||||||
// Ignore connections from blacklisted peers.
|
// Ignore connections from blacklisted peers.
|
||||||
if self.blacklisted_peers.contains(peer_id) {
|
if self.blacklisted_peers.contains(peer_id) {
|
||||||
|
@ -201,13 +201,6 @@ impl NetworkBehaviour for Identify {
|
|||||||
IdentifyHandler::new(self.config.initial_delay, self.config.interval)
|
IdentifyHandler::new(self.config.initial_delay, self.config.interval)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inject_connected(&mut self, _: &PeerId) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
|
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
|
||||||
let addr = match endpoint {
|
let addr = match endpoint {
|
||||||
ConnectedPoint::Dialer { address } => address.clone(),
|
ConnectedPoint::Dialer { address } => address.clone(),
|
||||||
|
@ -26,7 +26,7 @@ use if_watch::{IfEvent, IfWatcher};
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use libp2p_core::connection::ListenerId;
|
use libp2p_core::connection::ListenerId;
|
||||||
use libp2p_core::{
|
use libp2p_core::{
|
||||||
address_translation, connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId,
|
address_translation, multiaddr::Protocol, Multiaddr, PeerId,
|
||||||
};
|
};
|
||||||
use libp2p_swarm::{
|
use libp2p_swarm::{
|
||||||
protocols_handler::DummyProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
|
protocols_handler::DummyProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
|
||||||
@ -254,14 +254,10 @@ impl NetworkBehaviour for Mdns {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inject_connected(&mut self, _: &PeerId) {}
|
|
||||||
|
|
||||||
fn inject_disconnected(&mut self, _: &PeerId) {}
|
|
||||||
|
|
||||||
fn inject_event(
|
fn inject_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: PeerId,
|
_: PeerId,
|
||||||
_: ConnectionId,
|
_: libp2p_core::connection::ConnectionId,
|
||||||
ev: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
ev: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||||
) {
|
) {
|
||||||
void::unreachable(ev)
|
void::unreachable(ev)
|
||||||
|
@ -46,7 +46,7 @@ pub mod handler;
|
|||||||
pub use handler::{PingConfig, PingResult, PingSuccess, PingFailure};
|
pub use handler::{PingConfig, PingResult, PingSuccess, PingFailure};
|
||||||
use handler::PingHandler;
|
use handler::PingHandler;
|
||||||
|
|
||||||
use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId};
|
use libp2p_core::{PeerId, connection::ConnectionId};
|
||||||
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||||
use std::{collections::VecDeque, task::Context, task::Poll};
|
use std::{collections::VecDeque, task::Context, task::Poll};
|
||||||
use void::Void;
|
use void::Void;
|
||||||
@ -95,14 +95,6 @@ impl NetworkBehaviour for Ping {
|
|||||||
PingHandler::new(self.config.clone())
|
PingHandler::new(self.config.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec<Multiaddr> {
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inject_connected(&mut self, _: &PeerId) {}
|
|
||||||
|
|
||||||
fn inject_disconnected(&mut self, _: &PeerId) {}
|
|
||||||
|
|
||||||
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: PingResult) {
|
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: PingResult) {
|
||||||
self.events.push_front(PingEvent { peer, result })
|
self.events.push_front(PingEvent { peer, result })
|
||||||
}
|
}
|
||||||
|
@ -1358,24 +1358,13 @@ impl libp2p_swarm::NetworkBehaviour for KeepAliveBehaviour {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
|
||||||
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(
|
fn inject_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: PeerId,
|
_: PeerId,
|
||||||
_: ConnectionId,
|
_: ConnectionId,
|
||||||
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||||
) {
|
) {
|
||||||
|
void::unreachable(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll(
|
fn poll(
|
||||||
|
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
- Update dependencies.
|
- 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]
|
# 0.30.0 [2021-07-12]
|
||||||
|
|
||||||
- Update dependencies.
|
- Update dependencies.
|
||||||
|
@ -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
|
/// 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
|
/// should be ordered by decreasing likelihood of reachability. In other words, the first
|
||||||
/// address should be the most likely to be reachable.
|
/// address should be the most likely to be reachable.
|
||||||
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
|
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
|
||||||
/// Indicate to the behaviour that we connected to the node with the given peer id.
|
/// 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
|
/// This method is only called when the first connection to the peer is established, preceded by
|
||||||
/// [`inject_connection_established`](NetworkBehaviour::inject_connection_established).
|
/// [`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.
|
/// 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,
|
/// This method is only called when the last established connection to the peer is closed,
|
||||||
/// preceded by [`inject_connection_closed`](NetworkBehaviour::inject_connection_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.
|
/// Informs the behaviour about a newly established connection to a peer.
|
||||||
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
|
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
|
||||||
|
@ -1156,21 +1156,15 @@ impl NetworkBehaviour for DummyBehaviour {
|
|||||||
protocols_handler::DummyProtocolsHandler::default()
|
protocols_handler::DummyProtocolsHandler::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
fn inject_event(
|
||||||
Vec::new()
|
&mut self,
|
||||||
|
_: PeerId,
|
||||||
|
_: ConnectionId,
|
||||||
|
event: <Self::ProtocolsHandler as ProtocolsHandler>::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,
|
|
||||||
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent) {}
|
|
||||||
|
|
||||||
fn poll(&mut self, _: &mut Context<'_>, _: &mut impl PollParameters) ->
|
fn poll(&mut self, _: &mut Context<'_>, _: &mut impl PollParameters) ->
|
||||||
Poll<NetworkBehaviourAction<<Self::ProtocolsHandler as
|
Poll<NetworkBehaviourAction<<Self::ProtocolsHandler as
|
||||||
ProtocolsHandler>::InEvent, Self::OutEvent>>
|
ProtocolsHandler>::InEvent, Self::OutEvent>>
|
||||||
|
@ -82,12 +82,6 @@ where
|
|||||||
self.addresses.get(p).map_or(Vec::new(), |v| v.clone())
|
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) {
|
fn inject_event(&mut self, _: PeerId, _: ConnectionId, _: THandler::OutEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user