refactor(relay): delete now unused Action

This is a relict from when we still had to delay the construction of `ToSwarm` commands because some data was only available in `poll` via `PollParameters`. This is now resolved and `PollParameters` will go away.

Pull-Request: #4536.
This commit is contained in:
Thomas Eizinger
2023-09-22 14:50:05 +10:00
committed by GitHub
parent 89b4bf4d7f
commit 32df6e8747

View File

@ -41,7 +41,6 @@ use std::num::NonZeroU32;
use std::ops::Add; use std::ops::Add;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use std::time::Duration; use std::time::Duration;
use void::Void;
/// Configuration for the relay [`Behaviour`]. /// Configuration for the relay [`Behaviour`].
/// ///
@ -230,7 +229,7 @@ pub struct Behaviour {
circuits: CircuitsTracker, circuits: CircuitsTracker,
/// Queue of actions to return when polled. /// Queue of actions to return when polled.
queued_actions: VecDeque<Action>, queued_actions: VecDeque<ToSwarm<Event, THandlerInEvent<Self>>>,
external_addresses: ExternalAddresses, external_addresses: ExternalAddresses,
} }
@ -269,14 +268,12 @@ impl Behaviour {
// Only emit [`CircuitClosed`] for accepted requests. // Only emit [`CircuitClosed`] for accepted requests.
.filter(|c| matches!(c.status, CircuitStatus::Accepted)) .filter(|c| matches!(c.status, CircuitStatus::Accepted))
{ {
self.queued_actions.push_back( self.queued_actions
ToSwarm::GenerateEvent(Event::CircuitClosed { .push_back(ToSwarm::GenerateEvent(Event::CircuitClosed {
src_peer_id: circuit.src_peer_id, src_peer_id: circuit.src_peer_id,
dst_peer_id: circuit.dst_peer_id, dst_peer_id: circuit.dst_peer_id,
error: Some(std::io::ErrorKind::ConnectionAborted.into()), error: Some(std::io::ErrorKind::ConnectionAborted.into()),
}) }));
.into(),
);
} }
} }
} }
@ -414,7 +411,6 @@ impl NetworkBehaviour for Behaviour {
status: proto::Status::RESOURCE_LIMIT_EXCEEDED, status: proto::Status::RESOURCE_LIMIT_EXCEEDED,
}), }),
} }
.into()
} else { } else {
// Accept reservation. // Accept reservation.
self.reservations self.reservations
@ -422,10 +418,22 @@ impl NetworkBehaviour for Behaviour {
.or_default() .or_default()
.insert(connection); .insert(connection);
Action::AcceptReservationPrototype { ToSwarm::NotifyHandler {
handler: NotifyHandler::One(connection), handler: NotifyHandler::One(connection),
peer_id: event_source, peer_id: event_source,
inbound_reservation_req, event: Either::Left(handler::In::AcceptReservationReq {
inbound_reservation_req,
addrs: self
.external_addresses
.iter()
.cloned()
// Add local peer ID in case it isn't present yet.
.filter_map(|a| match a.iter().last()? {
Protocol::P2p(_) => Some(a),
_ => Some(a.with(Protocol::P2p(self.local_peer_id))),
})
.collect(),
}),
} }
}; };
@ -439,39 +447,35 @@ impl NetworkBehaviour for Behaviour {
.or_default() .or_default()
.insert(connection); .insert(connection);
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::GenerateEvent(
ToSwarm::GenerateEvent(Event::ReservationReqAccepted { Event::ReservationReqAccepted {
src_peer_id: event_source, src_peer_id: event_source,
renewed, renewed,
}) },
.into(), ));
);
} }
handler::Event::ReservationReqAcceptFailed { error } => { handler::Event::ReservationReqAcceptFailed { error } => {
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::GenerateEvent(
ToSwarm::GenerateEvent(Event::ReservationReqAcceptFailed { Event::ReservationReqAcceptFailed {
src_peer_id: event_source, src_peer_id: event_source,
error, error,
}) },
.into(), ));
);
} }
handler::Event::ReservationReqDenied {} => { handler::Event::ReservationReqDenied {} => {
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::GenerateEvent(
ToSwarm::GenerateEvent(Event::ReservationReqDenied { Event::ReservationReqDenied {
src_peer_id: event_source, src_peer_id: event_source,
}) },
.into(), ));
);
} }
handler::Event::ReservationReqDenyFailed { error } => { handler::Event::ReservationReqDenyFailed { error } => {
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::GenerateEvent(
ToSwarm::GenerateEvent(Event::ReservationReqDenyFailed { Event::ReservationReqDenyFailed {
src_peer_id: event_source, src_peer_id: event_source,
error, error,
}) },
.into(), ));
);
} }
handler::Event::ReservationTimedOut {} => { handler::Event::ReservationTimedOut {} => {
match self.reservations.entry(event_source) { match self.reservations.entry(event_source) {
@ -490,12 +494,10 @@ impl NetworkBehaviour for Behaviour {
} }
} }
self.queued_actions.push_back( self.queued_actions
ToSwarm::GenerateEvent(Event::ReservationTimedOut { .push_back(ToSwarm::GenerateEvent(Event::ReservationTimedOut {
src_peer_id: event_source, src_peer_id: event_source,
}) }));
.into(),
);
} }
handler::Event::CircuitReqReceived { handler::Event::CircuitReqReceived {
inbound_circuit_req, inbound_circuit_req,
@ -565,7 +567,7 @@ impl NetworkBehaviour for Behaviour {
}), }),
} }
}; };
self.queued_actions.push_back(action.into()); self.queued_actions.push_back(action);
} }
handler::Event::CircuitReqDenied { handler::Event::CircuitReqDenied {
circuit_id, circuit_id,
@ -575,13 +577,11 @@ impl NetworkBehaviour for Behaviour {
self.circuits.remove(circuit_id); self.circuits.remove(circuit_id);
} }
self.queued_actions.push_back( self.queued_actions
ToSwarm::GenerateEvent(Event::CircuitReqDenied { .push_back(ToSwarm::GenerateEvent(Event::CircuitReqDenied {
src_peer_id: event_source, src_peer_id: event_source,
dst_peer_id, dst_peer_id,
}) }));
.into(),
);
} }
handler::Event::CircuitReqDenyFailed { handler::Event::CircuitReqDenyFailed {
circuit_id, circuit_id,
@ -592,14 +592,13 @@ impl NetworkBehaviour for Behaviour {
self.circuits.remove(circuit_id); self.circuits.remove(circuit_id);
} }
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::GenerateEvent(
ToSwarm::GenerateEvent(Event::CircuitReqDenyFailed { Event::CircuitReqDenyFailed {
src_peer_id: event_source, src_peer_id: event_source,
dst_peer_id, dst_peer_id,
error, error,
}) },
.into(), ));
);
} }
handler::Event::OutboundConnectNegotiated { handler::Event::OutboundConnectNegotiated {
circuit_id, circuit_id,
@ -610,21 +609,18 @@ impl NetworkBehaviour for Behaviour {
dst_stream, dst_stream,
dst_pending_data, dst_pending_data,
} => { } => {
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::NotifyHandler {
ToSwarm::NotifyHandler { handler: NotifyHandler::One(src_connection_id),
handler: NotifyHandler::One(src_connection_id), peer_id: src_peer_id,
peer_id: src_peer_id, event: Either::Left(handler::In::AcceptAndDriveCircuit {
event: Either::Left(handler::In::AcceptAndDriveCircuit { circuit_id,
circuit_id, dst_peer_id: event_source,
dst_peer_id: event_source, inbound_circuit_req,
inbound_circuit_req, dst_handler_notifier,
dst_handler_notifier, dst_stream,
dst_stream, dst_pending_data,
dst_pending_data, }),
}), });
}
.into(),
);
} }
handler::Event::OutboundConnectNegotiationFailed { handler::Event::OutboundConnectNegotiationFailed {
circuit_id, circuit_id,
@ -634,39 +630,33 @@ impl NetworkBehaviour for Behaviour {
status, status,
error, error,
} => { } => {
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::NotifyHandler {
ToSwarm::NotifyHandler { handler: NotifyHandler::One(src_connection_id),
handler: NotifyHandler::One(src_connection_id), peer_id: src_peer_id,
peer_id: src_peer_id, event: Either::Left(handler::In::DenyCircuitReq {
event: Either::Left(handler::In::DenyCircuitReq { circuit_id: Some(circuit_id),
circuit_id: Some(circuit_id), inbound_circuit_req,
inbound_circuit_req, status,
status, }),
}), });
} self.queued_actions.push_back(ToSwarm::GenerateEvent(
.into(), Event::CircuitReqOutboundConnectFailed {
);
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitReqOutboundConnectFailed {
src_peer_id, src_peer_id,
dst_peer_id: event_source, dst_peer_id: event_source,
error, error,
}) },
.into(), ));
);
} }
handler::Event::CircuitReqAccepted { handler::Event::CircuitReqAccepted {
dst_peer_id, dst_peer_id,
circuit_id, circuit_id,
} => { } => {
self.circuits.accepted(circuit_id); self.circuits.accepted(circuit_id);
self.queued_actions.push_back( self.queued_actions
ToSwarm::GenerateEvent(Event::CircuitReqAccepted { .push_back(ToSwarm::GenerateEvent(Event::CircuitReqAccepted {
src_peer_id: event_source, src_peer_id: event_source,
dst_peer_id, dst_peer_id,
}) }));
.into(),
);
} }
handler::Event::CircuitReqAcceptFailed { handler::Event::CircuitReqAcceptFailed {
dst_peer_id, dst_peer_id,
@ -674,14 +664,13 @@ impl NetworkBehaviour for Behaviour {
error, error,
} => { } => {
self.circuits.remove(circuit_id); self.circuits.remove(circuit_id);
self.queued_actions.push_back( self.queued_actions.push_back(ToSwarm::GenerateEvent(
ToSwarm::GenerateEvent(Event::CircuitReqAcceptFailed { Event::CircuitReqAcceptFailed {
src_peer_id: event_source, src_peer_id: event_source,
dst_peer_id, dst_peer_id,
error, error,
}) },
.into(), ));
);
} }
handler::Event::CircuitClosed { handler::Event::CircuitClosed {
dst_peer_id, dst_peer_id,
@ -690,14 +679,12 @@ impl NetworkBehaviour for Behaviour {
} => { } => {
self.circuits.remove(circuit_id); self.circuits.remove(circuit_id);
self.queued_actions.push_back( self.queued_actions
ToSwarm::GenerateEvent(Event::CircuitClosed { .push_back(ToSwarm::GenerateEvent(Event::CircuitClosed {
src_peer_id: event_source, src_peer_id: event_source,
dst_peer_id, dst_peer_id,
error, error,
}) }));
.into(),
);
} }
} }
} }
@ -707,8 +694,8 @@ impl NetworkBehaviour for Behaviour {
_cx: &mut Context<'_>, _cx: &mut Context<'_>,
_: &mut impl PollParameters, _: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { ) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(action) = self.queued_actions.pop_front() { if let Some(to_swarm) = self.queued_actions.pop_front() {
return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses)); return Poll::Ready(to_swarm);
} }
Poll::Pending Poll::Pending
@ -804,53 +791,3 @@ impl Add<u64> for CircuitId {
CircuitId(self.0 + rhs) CircuitId(self.0 + rhs)
} }
} }
/// A [`ToSwarm`], either complete, or still requiring data from [`PollParameters`]
/// before being returned in [`Behaviour::poll`].
#[allow(clippy::large_enum_variant)]
enum Action {
Done(ToSwarm<Event, Either<handler::In, Void>>),
AcceptReservationPrototype {
inbound_reservation_req: inbound_hop::ReservationReq,
handler: NotifyHandler,
peer_id: PeerId,
},
}
impl From<ToSwarm<Event, Either<handler::In, Void>>> for Action {
fn from(action: ToSwarm<Event, Either<handler::In, Void>>) -> Self {
Self::Done(action)
}
}
impl Action {
fn build(
self,
local_peer_id: PeerId,
external_addresses: &ExternalAddresses,
) -> ToSwarm<Event, Either<handler::In, Void>> {
match self {
Action::Done(action) => action,
Action::AcceptReservationPrototype {
inbound_reservation_req,
handler,
peer_id,
} => ToSwarm::NotifyHandler {
handler,
peer_id,
event: Either::Left(handler::In::AcceptReservationReq {
inbound_reservation_req,
addrs: external_addresses
.iter()
.cloned()
// Add local peer ID in case it isn't present yet.
.filter_map(|a| match a.iter().last()? {
Protocol::P2p(_) => Some(a),
_ => Some(a.with(Protocol::P2p(local_peer_id))),
})
.collect(),
}),
},
}
}
}