Skip to content

Commit

Permalink
Allow each NetworkBehaviour to have their own ConnectionDenied re…
Browse files Browse the repository at this point in the history
…ason
  • Loading branch information
thomaseizinger committed Nov 22, 2022
1 parent 27e1b2d commit 19348e9
Show file tree
Hide file tree
Showing 32 changed files with 296 additions and 145 deletions.
1 change: 1 addition & 0 deletions examples/file-sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ mod network {
&mut self,
event: SwarmEvent<
ComposedEvent,
ComposedBehaviourConnectionDenied,
EitherError<ConnectionHandlerUpgrErr<io::Error>, io::Error>,
>,
) {
Expand Down
6 changes: 4 additions & 2 deletions misc/metrics/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ impl super::Recorder<libp2p_identify::Event> for Metrics {
}
}

impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleErr>> for Metrics {
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, THandleErr>) {
impl<TBvEv, TReason, THandleErr>
super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, TReason, THandleErr>> for Metrics
{
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, TReason, THandleErr>) {
if let libp2p_swarm::SwarmEvent::ConnectionClosed {
peer_id,
num_established,
Expand Down
6 changes: 4 additions & 2 deletions misc/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ impl Recorder<libp2p_relay::v2::relay::Event> for Metrics {
}
}

impl<TBvEv, THandleErr> Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleErr>> for Metrics {
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, THandleErr>) {
impl<TBvEv, TReason, THandleErr> Recorder<libp2p_swarm::SwarmEvent<TBvEv, TReason, THandleErr>>
for Metrics
{
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, TReason, THandleErr>) {
self.swarm.record(event);

#[cfg(feature = "identify")]
Expand Down
7 changes: 5 additions & 2 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ impl Metrics {
}
}

impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleErr>> for Metrics {
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, THandleErr>) {
impl<TBvEv, TReason, THandleErr>
super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, TReason, THandleErr>> for Metrics
{
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, TReason, THandleErr>) {
match event {
libp2p_swarm::SwarmEvent::Behaviour(_) => {}
libp2p_swarm::SwarmEvent::ConnectionEstablished { endpoint, .. } => {
Expand Down Expand Up @@ -269,6 +271,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
libp2p_swarm::SwarmEvent::Dialing(_) => {
self.dial_attempt.inc();
}
libp2p_swarm::SwarmEvent::ConnectionDenied { .. } => {}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions protocols/autonat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ libp2p-request-response = { version = "0.23.0", path = "../request-response" }
log = "0.4"
rand = "0.8"
prost = "0.11"
void = "1.0.2"

[dev-dependencies]
async-std = { version = "1.10", features = ["attributes"] }
Expand Down
6 changes: 4 additions & 2 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use libp2p_request_response::{
ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::{
behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
Expand All @@ -50,6 +50,7 @@ use std::{
task::{Context, Poll},
time::Duration,
};
use void::Void;

/// Config for the [`Behaviour`].
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -410,6 +411,7 @@ impl Behaviour {

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = <RequestResponse<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler;
type ConnectionDenied = Void;
type OutEvent = Event;

fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters) -> Poll<Action> {
Expand Down Expand Up @@ -461,7 +463,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
self.inner.new_handler(peer, connected_point)
}

Expand Down
7 changes: 4 additions & 3 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use libp2p_core::connection::{ConnectedPoint, ConnectionId};
use libp2p_core::multiaddr::Protocol;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{
ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure, FromSwarm,
THandlerInEvent,
ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, THandlerInEvent,
};
use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::{
Expand All @@ -39,6 +38,7 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet, VecDeque};
use std::task::{Context, Poll};
use thiserror::Error;
use void::Void;

const MAX_NUMBER_OF_UPGRADE_ATTEMPTS: u8 = 3;

Expand Down Expand Up @@ -210,13 +210,14 @@ impl Behaviour {

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = Handler;
type ConnectionDenied = Void;
type OutEvent = Event;

fn new_handler(
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
match (
self.awaiting_direct_inbound_connections.entry(*peer),
self.awaiting_direct_outbound_connections.entry(*peer),
Expand Down
1 change: 1 addition & 0 deletions protocols/floodsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ prost = "0.11"
rand = "0.8"
smallvec = "1.6.1"
thiserror = "1.0.37"
void = "1.0.2"

[build-dependencies]
prost-build = "0.11"
Expand Down
6 changes: 4 additions & 2 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::FloodsubConfig;
use cuckoofilter::{CuckooError, CuckooFilter};
use fnv::FnvHashSet;
use libp2p_core::{connection::ConnectionId, ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::ConnectionHandler;
use libp2p_swarm::{
dial_opts::DialOpts, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler,
Expand All @@ -39,6 +39,7 @@ use smallvec::SmallVec;
use std::collections::hash_map::{DefaultHasher, HashMap};
use std::task::{Context, Poll};
use std::{collections::VecDeque, iter};
use void::Void;

/// Network behaviour that handles the floodsub protocol.
pub struct Floodsub {
Expand Down Expand Up @@ -334,13 +335,14 @@ impl Floodsub {

impl NetworkBehaviour for Floodsub {
type ConnectionHandler = OneShotHandler<FloodsubProtocol, FloodsubRpc, InnerMessage>;
type ConnectionDenied = Void;
type OutEvent = FloodsubEvent;

fn new_handler(
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
Ok(Default::default())
}

Expand Down
26 changes: 13 additions & 13 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
libp2p-swarm = { version = "0.41.0", path = "../../swarm" }
libp2p-core = { version = "0.38.0", path = "../../core" }
bytes = "1.0"
asynchronous-codec = "0.6"
base64 = "0.13.0"
byteorder = "1.3.4"
bytes = "1.0"
fnv = "1.0.7"
futures = "0.3.5"
rand = "0.8"
asynchronous-codec = "0.6"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
hex_fmt = "0.3.0"
instant = "0.1.11"
libp2p-core = { version = "0.38.0", path = "../../core" }
libp2p-swarm = { version = "0.41.0", path = "../../swarm" }
log = "0.4.11"
sha2 = "0.10.0"
base64 = "0.13.0"
smallvec = "1.6.1"
prometheus-client = "0.18.0" # Metrics dependencies
prost = "0.11"
prost-codec = { version = "0.3", path = "../../misc/prost-codec" }
hex_fmt = "0.3.0"
rand = "0.8"
regex = "1.5.5"
serde = { version = "1", optional = true, features = ["derive"] }
sha2 = "0.10.0"
smallvec = "1.6.1"
thiserror = "1.0"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
void = "1.0.2"
wasm-timer = "0.2.5"
instant = "0.1.11"
# Metrics dependencies
prometheus-client = "0.18.0"

[dev-dependencies]
async-std = "1.6.3"
Expand Down
5 changes: 3 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use log::{debug, error, trace, warn};
use prometheus_client::registry::Registry;
use prost::Message;
use rand::{seq::SliceRandom, thread_rng};
use void::Void;

use libp2p_core::{
connection::ConnectionId, identity::Keypair, multiaddr::Protocol::Ip4,
Expand Down Expand Up @@ -66,7 +67,6 @@ use crate::types::{
};
use crate::types::{GossipsubRpc, PeerConnections, PeerKind};
use crate::{rpc_proto, TopicScoreParams};
use libp2p_swarm::behaviour::ConnectionDenied;
use std::{cmp::Ordering::Equal, fmt::Debug};
use wasm_timer::Interval;

Expand Down Expand Up @@ -3296,13 +3296,14 @@ where
F: Send + 'static + TopicSubscriptionFilter,
{
type ConnectionHandler = GossipsubHandler;
type ConnectionDenied = Void;
type OutEvent = GossipsubEvent;

fn new_handler(
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
let protocol_config = ProtocolConfig::new(
self.config.protocol_id().clone(),
self.config.custom_id_version().clone(),
Expand Down
7 changes: 4 additions & 3 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use libp2p_core::{
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Multiaddr, PeerId, PublicKey,
};
use libp2p_swarm::behaviour::{
ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure, FromSwarm,
THandlerInEvent,
ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, THandlerInEvent,
};
use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
Expand All @@ -42,6 +41,7 @@ use std::{
task::Poll,
time::Duration,
};
use void::Void;

/// Network behaviour that automatically identifies nodes periodically, returns information
/// about them, and answers identify queries from other nodes.
Expand Down Expand Up @@ -237,13 +237,14 @@ impl Behaviour {

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = Handler;
type ConnectionDenied = Void;
type OutEvent = Event;

fn new_handler(
&mut self,
peer: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
Ok(Handler::new(
self.config.initial_delay,
self.config.interval,
Expand Down
8 changes: 5 additions & 3 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use fnv::{FnvHashMap, FnvHashSet};
use instant::Instant;
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::behaviour::{
AddressChange, ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure,
ExpiredListenAddr, FromSwarm, NewExternalAddr, NewListenAddr, THandlerInEvent,
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr,
FromSwarm, NewExternalAddr, NewListenAddr, THandlerInEvent,
};
use libp2p_swarm::{
dial_opts::{self, DialOpts},
Expand All @@ -57,6 +57,7 @@ use std::task::{Context, Poll};
use std::vec;
use std::{borrow::Cow, time::Duration};
use thiserror::Error;
use void::Void;

pub use crate::query::QueryStats;

Expand Down Expand Up @@ -1937,13 +1938,14 @@ where
TStore: Send + 'static,
{
type ConnectionHandler = KademliaHandler<QueryId>;
type ConnectionDenied = Void;
type OutEvent = KademliaEvent;

fn new_handler(
&mut self,
remote_peer_id: &PeerId,
endpoint: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
Ok(KademliaHandler::new(
KademliaHandlerConfig {
protocol_config: self.protocol_config.clone(),
Expand Down
5 changes: 3 additions & 2 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::Config;
use futures::Stream;
use if_watch::IfEvent;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionDenied, FromSwarm};
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
dummy, ConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
Expand Down Expand Up @@ -166,13 +166,14 @@ where
P: Provider,
{
type ConnectionHandler = dummy::ConnectionHandler;
type ConnectionDenied = Void;
type OutEvent = Event;

fn new_handler(
&mut self,
_: &PeerId,
_: &libp2p_core::ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Self::ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}

Expand Down
6 changes: 4 additions & 2 deletions protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ mod protocol;
use handler::Handler;
pub use handler::{Config, Failure, Success};
use libp2p_core::{connection::ConnectionId, ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::{
behaviour::FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
use std::{
collections::VecDeque,
task::{Context, Poll},
};
use void::Void;

#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Config instead.")]
pub type PingConfig = Config;
Expand Down Expand Up @@ -118,13 +119,14 @@ impl Default for Behaviour {

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = Handler;
type ConnectionDenied = Void;
type OutEvent = Event;

fn new_handler(
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> std::result::Result<Self::ConnectionHandler, ConnectionDenied> {
) -> std::result::Result<Self::ConnectionHandler, Self::ConnectionDenied> {
Ok(Handler::new(self.config.clone()))
}

Expand Down
Loading

0 comments on commit 19348e9

Please sign in to comment.