feat(swarm): rename NetworkBehaviourAction to ToSwarm

Resolves #3123.

Pull-Request: #3658.
This commit is contained in:
Thomas Eizinger
2023-03-24 14:43:49 +01:00
committed by GitHub
parent 7ffa63b656
commit dcbc04e89e
32 changed files with 698 additions and 807 deletions

View File

@ -35,8 +35,8 @@ use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses,
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, THandler,
THandlerInEvent, THandlerOutEvent,
NetworkBehaviour, NotifyHandler, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
ToSwarm,
};
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::num::NonZeroU32;
@ -242,7 +242,7 @@ impl Behaviour {
.filter(|c| matches!(c.status, CircuitStatus::Accepted))
{
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitClosed {
ToSwarm::GenerateEvent(Event::CircuitClosed {
src_peer_id: circuit.src_peer_id,
dst_peer_id: circuit.dst_peer_id,
error: Some(std::io::ErrorKind::ConnectionAborted.into()),
@ -377,7 +377,7 @@ impl NetworkBehaviour for Behaviour {
.all(|limiter| {
limiter.try_next(event_source, endpoint.get_remote_address(), now)
}) {
NetworkBehaviourAction::NotifyHandler {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(connection),
peer_id: event_source,
event: Either::Left(handler::In::DenyReservationReq {
@ -411,7 +411,7 @@ impl NetworkBehaviour for Behaviour {
.insert(connection);
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::ReservationReqAccepted {
ToSwarm::GenerateEvent(Event::ReservationReqAccepted {
src_peer_id: event_source,
renewed,
})
@ -420,7 +420,7 @@ impl NetworkBehaviour for Behaviour {
}
handler::Event::ReservationReqAcceptFailed { error } => {
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::ReservationReqAcceptFailed {
ToSwarm::GenerateEvent(Event::ReservationReqAcceptFailed {
src_peer_id: event_source,
error,
})
@ -429,7 +429,7 @@ impl NetworkBehaviour for Behaviour {
}
handler::Event::ReservationReqDenied {} => {
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::ReservationReqDenied {
ToSwarm::GenerateEvent(Event::ReservationReqDenied {
src_peer_id: event_source,
})
.into(),
@ -437,7 +437,7 @@ impl NetworkBehaviour for Behaviour {
}
handler::Event::ReservationReqDenyFailed { error } => {
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::ReservationReqDenyFailed {
ToSwarm::GenerateEvent(Event::ReservationReqDenyFailed {
src_peer_id: event_source,
error,
})
@ -462,7 +462,7 @@ impl NetworkBehaviour for Behaviour {
}
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::ReservationTimedOut {
ToSwarm::GenerateEvent(Event::ReservationTimedOut {
src_peer_id: event_source,
})
.into(),
@ -491,7 +491,7 @@ impl NetworkBehaviour for Behaviour {
limiter.try_next(event_source, endpoint.get_remote_address(), now)
}) {
// Deny circuit exceeding limits.
NetworkBehaviourAction::NotifyHandler {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(connection),
peer_id: event_source,
event: Either::Left(handler::In::DenyCircuitReq {
@ -514,7 +514,7 @@ impl NetworkBehaviour for Behaviour {
dst_connection_id: *dst_conn,
});
NetworkBehaviourAction::NotifyHandler {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(*dst_conn),
peer_id: event_source,
event: Either::Left(handler::In::NegotiateOutboundConnect {
@ -527,7 +527,7 @@ impl NetworkBehaviour for Behaviour {
}
} else {
// Deny circuit request if no reservation present.
NetworkBehaviourAction::NotifyHandler {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(connection),
peer_id: event_source,
event: Either::Left(handler::In::DenyCircuitReq {
@ -541,7 +541,7 @@ impl NetworkBehaviour for Behaviour {
}
handler::Event::CircuitReqReceiveFailed { error } => {
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitReqReceiveFailed {
ToSwarm::GenerateEvent(Event::CircuitReqReceiveFailed {
src_peer_id: event_source,
error,
})
@ -557,7 +557,7 @@ impl NetworkBehaviour for Behaviour {
}
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitReqDenied {
ToSwarm::GenerateEvent(Event::CircuitReqDenied {
src_peer_id: event_source,
dst_peer_id,
})
@ -574,7 +574,7 @@ impl NetworkBehaviour for Behaviour {
}
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitReqDenyFailed {
ToSwarm::GenerateEvent(Event::CircuitReqDenyFailed {
src_peer_id: event_source,
dst_peer_id,
error,
@ -592,7 +592,7 @@ impl NetworkBehaviour for Behaviour {
dst_pending_data,
} => {
self.queued_actions.push_back(
NetworkBehaviourAction::NotifyHandler {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(src_connection_id),
peer_id: src_peer_id,
event: Either::Left(handler::In::AcceptAndDriveCircuit {
@ -616,7 +616,7 @@ impl NetworkBehaviour for Behaviour {
error,
} => {
self.queued_actions.push_back(
NetworkBehaviourAction::NotifyHandler {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(src_connection_id),
peer_id: src_peer_id,
event: Either::Left(handler::In::DenyCircuitReq {
@ -628,7 +628,7 @@ impl NetworkBehaviour for Behaviour {
.into(),
);
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitReqOutboundConnectFailed {
ToSwarm::GenerateEvent(Event::CircuitReqOutboundConnectFailed {
src_peer_id,
dst_peer_id: event_source,
error,
@ -642,7 +642,7 @@ impl NetworkBehaviour for Behaviour {
} => {
self.circuits.accepted(circuit_id);
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitReqAccepted {
ToSwarm::GenerateEvent(Event::CircuitReqAccepted {
src_peer_id: event_source,
dst_peer_id,
})
@ -656,7 +656,7 @@ impl NetworkBehaviour for Behaviour {
} => {
self.circuits.remove(circuit_id);
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitReqAcceptFailed {
ToSwarm::GenerateEvent(Event::CircuitReqAcceptFailed {
src_peer_id: event_source,
dst_peer_id,
error,
@ -672,7 +672,7 @@ impl NetworkBehaviour for Behaviour {
self.circuits.remove(circuit_id);
self.queued_actions.push_back(
NetworkBehaviourAction::GenerateEvent(Event::CircuitClosed {
ToSwarm::GenerateEvent(Event::CircuitClosed {
src_peer_id: event_source,
dst_peer_id,
error,
@ -687,7 +687,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_cx: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses));
}
@ -786,11 +786,11 @@ impl Add<u64> for CircuitId {
}
}
/// A [`NetworkBehaviourAction`], either complete, or still requiring data from [`PollParameters`]
/// A [`ToSwarm`], either complete, or still requiring data from [`PollParameters`]
/// before being returned in [`Behaviour::poll`].
#[allow(clippy::large_enum_variant)]
enum Action {
Done(NetworkBehaviourAction<Event, Either<handler::In, Void>>),
Done(ToSwarm<Event, Either<handler::In, Void>>),
AcceptReservationPrototype {
inbound_reservation_req: inbound_hop::ReservationReq,
handler: NotifyHandler,
@ -798,8 +798,8 @@ enum Action {
},
}
impl From<NetworkBehaviourAction<Event, Either<handler::In, Void>>> for Action {
fn from(action: NetworkBehaviourAction<Event, Either<handler::In, Void>>) -> Self {
impl From<ToSwarm<Event, Either<handler::In, Void>>> for Action {
fn from(action: ToSwarm<Event, Either<handler::In, Void>>) -> Self {
Self::Done(action)
}
}
@ -809,14 +809,14 @@ impl Action {
self,
local_peer_id: PeerId,
external_addresses: &ExternalAddresses,
) -> NetworkBehaviourAction<Event, Either<handler::In, Void>> {
) -> ToSwarm<Event, Either<handler::In, Void>> {
match self {
Action::Done(action) => action,
Action::AcceptReservationPrototype {
inbound_reservation_req,
handler,
peer_id,
} => NetworkBehaviourAction::NotifyHandler {
} => ToSwarm::NotifyHandler {
handler,
peer_id,
event: Either::Left(handler::In::AcceptReservationReq {

View File

@ -40,8 +40,8 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm
use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionHandler, ConnectionHandlerUpgrErr, ConnectionId,
DialFailure, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
DialFailure, NegotiatedSubstream, NetworkBehaviour, NotifyHandler, PollParameters, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::{hash_map, HashMap, VecDeque};
use std::io::{Error, ErrorKind, IoSlice};
@ -104,7 +104,7 @@ pub struct Behaviour {
directly_connected_peers: HashMap<PeerId, Vec<ConnectionId>>,
/// Queue of actions to return when polled.
queued_actions: VecDeque<NetworkBehaviourAction<Event, Either<handler::In, Void>>>,
queued_actions: VecDeque<ToSwarm<Event, Either<handler::In, Void>>>,
pending_handler_commands: HashMap<ConnectionId, handler::In>,
}
@ -219,12 +219,11 @@ impl NetworkBehaviour for Behaviour {
}
if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
self.queued_actions
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: Either::Left(event),
})
self.queued_actions.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: Either::Left(event),
})
}
}
FromSwarm::ConnectionClosed(connection_closed) => {
@ -296,15 +295,14 @@ impl NetworkBehaviour for Behaviour {
}
};
self.queued_actions
.push_back(NetworkBehaviourAction::GenerateEvent(event))
self.queued_actions.push_back(ToSwarm::GenerateEvent(event))
}
fn poll(
&mut self,
cx: &mut Context<'_>,
_poll_parameters: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action);
}
@ -320,7 +318,7 @@ impl NetworkBehaviour for Behaviour {
.get(&relay_peer_id)
.and_then(|cs| cs.get(0))
{
Some(connection_id) => NetworkBehaviourAction::NotifyHandler {
Some(connection_id) => ToSwarm::NotifyHandler {
peer_id: relay_peer_id,
handler: NotifyHandler::One(*connection_id),
event: Either::Left(handler::In::Reserve { to_listener }),
@ -334,7 +332,7 @@ impl NetworkBehaviour for Behaviour {
self.pending_handler_commands
.insert(relayed_connection_id, handler::In::Reserve { to_listener });
NetworkBehaviourAction::Dial { opts }
ToSwarm::Dial { opts }
}
}
}
@ -350,7 +348,7 @@ impl NetworkBehaviour for Behaviour {
.get(&relay_peer_id)
.and_then(|cs| cs.get(0))
{
Some(connection_id) => NetworkBehaviourAction::NotifyHandler {
Some(connection_id) => ToSwarm::NotifyHandler {
peer_id: relay_peer_id,
handler: NotifyHandler::One(*connection_id),
event: Either::Left(handler::In::EstablishCircuit {
@ -373,7 +371,7 @@ impl NetworkBehaviour for Behaviour {
},
);
NetworkBehaviourAction::Dial { opts }
ToSwarm::Dial { opts }
}
}
}