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::task::{Context, Poll};
use std::time::Duration;
use void::Void;
/// Configuration for the relay [`Behaviour`].
///
@ -230,7 +229,7 @@ pub struct Behaviour {
circuits: CircuitsTracker,
/// Queue of actions to return when polled.
queued_actions: VecDeque<Action>,
queued_actions: VecDeque<ToSwarm<Event, THandlerInEvent<Self>>>,
external_addresses: ExternalAddresses,
}
@ -269,14 +268,12 @@ impl Behaviour {
// Only emit [`CircuitClosed`] for accepted requests.
.filter(|c| matches!(c.status, CircuitStatus::Accepted))
{
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitClosed {
self.queued_actions
.push_back(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()),
})
.into(),
);
}));
}
}
}
@ -414,7 +411,6 @@ impl NetworkBehaviour for Behaviour {
status: proto::Status::RESOURCE_LIMIT_EXCEEDED,
}),
}
.into()
} else {
// Accept reservation.
self.reservations
@ -422,10 +418,22 @@ impl NetworkBehaviour for Behaviour {
.or_default()
.insert(connection);
Action::AcceptReservationPrototype {
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(connection),
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()
.insert(connection);
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::ReservationReqAccepted {
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::ReservationReqAccepted {
src_peer_id: event_source,
renewed,
})
.into(),
);
},
));
}
handler::Event::ReservationReqAcceptFailed { error } => {
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::ReservationReqAcceptFailed {
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::ReservationReqAcceptFailed {
src_peer_id: event_source,
error,
})
.into(),
);
},
));
}
handler::Event::ReservationReqDenied {} => {
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::ReservationReqDenied {
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::ReservationReqDenied {
src_peer_id: event_source,
})
.into(),
);
},
));
}
handler::Event::ReservationReqDenyFailed { error } => {
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::ReservationReqDenyFailed {
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::ReservationReqDenyFailed {
src_peer_id: event_source,
error,
})
.into(),
);
},
));
}
handler::Event::ReservationTimedOut {} => {
match self.reservations.entry(event_source) {
@ -490,12 +494,10 @@ impl NetworkBehaviour for Behaviour {
}
}
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::ReservationTimedOut {
self.queued_actions
.push_back(ToSwarm::GenerateEvent(Event::ReservationTimedOut {
src_peer_id: event_source,
})
.into(),
);
}));
}
handler::Event::CircuitReqReceived {
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 {
circuit_id,
@ -575,13 +577,11 @@ impl NetworkBehaviour for Behaviour {
self.circuits.remove(circuit_id);
}
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitReqDenied {
self.queued_actions
.push_back(ToSwarm::GenerateEvent(Event::CircuitReqDenied {
src_peer_id: event_source,
dst_peer_id,
})
.into(),
);
}));
}
handler::Event::CircuitReqDenyFailed {
circuit_id,
@ -592,14 +592,13 @@ impl NetworkBehaviour for Behaviour {
self.circuits.remove(circuit_id);
}
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitReqDenyFailed {
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::CircuitReqDenyFailed {
src_peer_id: event_source,
dst_peer_id,
error,
})
.into(),
);
},
));
}
handler::Event::OutboundConnectNegotiated {
circuit_id,
@ -610,21 +609,18 @@ impl NetworkBehaviour for Behaviour {
dst_stream,
dst_pending_data,
} => {
self.queued_actions.push_back(
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(src_connection_id),
peer_id: src_peer_id,
event: Either::Left(handler::In::AcceptAndDriveCircuit {
circuit_id,
dst_peer_id: event_source,
inbound_circuit_req,
dst_handler_notifier,
dst_stream,
dst_pending_data,
}),
}
.into(),
);
self.queued_actions.push_back(ToSwarm::NotifyHandler {
handler: NotifyHandler::One(src_connection_id),
peer_id: src_peer_id,
event: Either::Left(handler::In::AcceptAndDriveCircuit {
circuit_id,
dst_peer_id: event_source,
inbound_circuit_req,
dst_handler_notifier,
dst_stream,
dst_pending_data,
}),
});
}
handler::Event::OutboundConnectNegotiationFailed {
circuit_id,
@ -634,39 +630,33 @@ impl NetworkBehaviour for Behaviour {
status,
error,
} => {
self.queued_actions.push_back(
ToSwarm::NotifyHandler {
handler: NotifyHandler::One(src_connection_id),
peer_id: src_peer_id,
event: Either::Left(handler::In::DenyCircuitReq {
circuit_id: Some(circuit_id),
inbound_circuit_req,
status,
}),
}
.into(),
);
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitReqOutboundConnectFailed {
self.queued_actions.push_back(ToSwarm::NotifyHandler {
handler: NotifyHandler::One(src_connection_id),
peer_id: src_peer_id,
event: Either::Left(handler::In::DenyCircuitReq {
circuit_id: Some(circuit_id),
inbound_circuit_req,
status,
}),
});
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::CircuitReqOutboundConnectFailed {
src_peer_id,
dst_peer_id: event_source,
error,
})
.into(),
);
},
));
}
handler::Event::CircuitReqAccepted {
dst_peer_id,
circuit_id,
} => {
self.circuits.accepted(circuit_id);
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitReqAccepted {
self.queued_actions
.push_back(ToSwarm::GenerateEvent(Event::CircuitReqAccepted {
src_peer_id: event_source,
dst_peer_id,
})
.into(),
);
}));
}
handler::Event::CircuitReqAcceptFailed {
dst_peer_id,
@ -674,14 +664,13 @@ impl NetworkBehaviour for Behaviour {
error,
} => {
self.circuits.remove(circuit_id);
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitReqAcceptFailed {
self.queued_actions.push_back(ToSwarm::GenerateEvent(
Event::CircuitReqAcceptFailed {
src_peer_id: event_source,
dst_peer_id,
error,
})
.into(),
);
},
));
}
handler::Event::CircuitClosed {
dst_peer_id,
@ -690,14 +679,12 @@ impl NetworkBehaviour for Behaviour {
} => {
self.circuits.remove(circuit_id);
self.queued_actions.push_back(
ToSwarm::GenerateEvent(Event::CircuitClosed {
self.queued_actions
.push_back(ToSwarm::GenerateEvent(Event::CircuitClosed {
src_peer_id: event_source,
dst_peer_id,
error,
})
.into(),
);
}));
}
}
}
@ -707,8 +694,8 @@ impl NetworkBehaviour for Behaviour {
_cx: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses));
if let Some(to_swarm) = self.queued_actions.pop_front() {
return Poll::Ready(to_swarm);
}
Poll::Pending
@ -804,53 +791,3 @@ impl Add<u64> for CircuitId {
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(),
}),
},
}
}
}