Skip to content

Commit 32df6e8

Browse files
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.
1 parent 89b4bf4 commit 32df6e8

File tree

1 file changed

+82
-145
lines changed

1 file changed

+82
-145
lines changed

protocols/relay/src/behaviour.rs

Lines changed: 82 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use std::num::NonZeroU32;
4141
use std::ops::Add;
4242
use std::task::{Context, Poll};
4343
use std::time::Duration;
44-
use void::Void;
4544

4645
/// Configuration for the relay [`Behaviour`].
4746
///
@@ -230,7 +229,7 @@ pub struct Behaviour {
230229
circuits: CircuitsTracker,
231230

232231
/// Queue of actions to return when polled.
233-
queued_actions: VecDeque<Action>,
232+
queued_actions: VecDeque<ToSwarm<Event, THandlerInEvent<Self>>>,
234233

235234
external_addresses: ExternalAddresses,
236235
}
@@ -269,14 +268,12 @@ impl Behaviour {
269268
// Only emit [`CircuitClosed`] for accepted requests.
270269
.filter(|c| matches!(c.status, CircuitStatus::Accepted))
271270
{
272-
self.queued_actions.push_back(
273-
ToSwarm::GenerateEvent(Event::CircuitClosed {
271+
self.queued_actions
272+
.push_back(ToSwarm::GenerateEvent(Event::CircuitClosed {
274273
src_peer_id: circuit.src_peer_id,
275274
dst_peer_id: circuit.dst_peer_id,
276275
error: Some(std::io::ErrorKind::ConnectionAborted.into()),
277-
})
278-
.into(),
279-
);
276+
}));
280277
}
281278
}
282279
}
@@ -414,18 +411,29 @@ impl NetworkBehaviour for Behaviour {
414411
status: proto::Status::RESOURCE_LIMIT_EXCEEDED,
415412
}),
416413
}
417-
.into()
418414
} else {
419415
// Accept reservation.
420416
self.reservations
421417
.entry(event_source)
422418
.or_default()
423419
.insert(connection);
424420

425-
Action::AcceptReservationPrototype {
421+
ToSwarm::NotifyHandler {
426422
handler: NotifyHandler::One(connection),
427423
peer_id: event_source,
428-
inbound_reservation_req,
424+
event: Either::Left(handler::In::AcceptReservationReq {
425+
inbound_reservation_req,
426+
addrs: self
427+
.external_addresses
428+
.iter()
429+
.cloned()
430+
// Add local peer ID in case it isn't present yet.
431+
.filter_map(|a| match a.iter().last()? {
432+
Protocol::P2p(_) => Some(a),
433+
_ => Some(a.with(Protocol::P2p(self.local_peer_id))),
434+
})
435+
.collect(),
436+
}),
429437
}
430438
};
431439

@@ -439,39 +447,35 @@ impl NetworkBehaviour for Behaviour {
439447
.or_default()
440448
.insert(connection);
441449

442-
self.queued_actions.push_back(
443-
ToSwarm::GenerateEvent(Event::ReservationReqAccepted {
450+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
451+
Event::ReservationReqAccepted {
444452
src_peer_id: event_source,
445453
renewed,
446-
})
447-
.into(),
448-
);
454+
},
455+
));
449456
}
450457
handler::Event::ReservationReqAcceptFailed { error } => {
451-
self.queued_actions.push_back(
452-
ToSwarm::GenerateEvent(Event::ReservationReqAcceptFailed {
458+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
459+
Event::ReservationReqAcceptFailed {
453460
src_peer_id: event_source,
454461
error,
455-
})
456-
.into(),
457-
);
462+
},
463+
));
458464
}
459465
handler::Event::ReservationReqDenied {} => {
460-
self.queued_actions.push_back(
461-
ToSwarm::GenerateEvent(Event::ReservationReqDenied {
466+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
467+
Event::ReservationReqDenied {
462468
src_peer_id: event_source,
463-
})
464-
.into(),
465-
);
469+
},
470+
));
466471
}
467472
handler::Event::ReservationReqDenyFailed { error } => {
468-
self.queued_actions.push_back(
469-
ToSwarm::GenerateEvent(Event::ReservationReqDenyFailed {
473+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
474+
Event::ReservationReqDenyFailed {
470475
src_peer_id: event_source,
471476
error,
472-
})
473-
.into(),
474-
);
477+
},
478+
));
475479
}
476480
handler::Event::ReservationTimedOut {} => {
477481
match self.reservations.entry(event_source) {
@@ -490,12 +494,10 @@ impl NetworkBehaviour for Behaviour {
490494
}
491495
}
492496

493-
self.queued_actions.push_back(
494-
ToSwarm::GenerateEvent(Event::ReservationTimedOut {
497+
self.queued_actions
498+
.push_back(ToSwarm::GenerateEvent(Event::ReservationTimedOut {
495499
src_peer_id: event_source,
496-
})
497-
.into(),
498-
);
500+
}));
499501
}
500502
handler::Event::CircuitReqReceived {
501503
inbound_circuit_req,
@@ -565,7 +567,7 @@ impl NetworkBehaviour for Behaviour {
565567
}),
566568
}
567569
};
568-
self.queued_actions.push_back(action.into());
570+
self.queued_actions.push_back(action);
569571
}
570572
handler::Event::CircuitReqDenied {
571573
circuit_id,
@@ -575,13 +577,11 @@ impl NetworkBehaviour for Behaviour {
575577
self.circuits.remove(circuit_id);
576578
}
577579

578-
self.queued_actions.push_back(
579-
ToSwarm::GenerateEvent(Event::CircuitReqDenied {
580+
self.queued_actions
581+
.push_back(ToSwarm::GenerateEvent(Event::CircuitReqDenied {
580582
src_peer_id: event_source,
581583
dst_peer_id,
582-
})
583-
.into(),
584-
);
584+
}));
585585
}
586586
handler::Event::CircuitReqDenyFailed {
587587
circuit_id,
@@ -592,14 +592,13 @@ impl NetworkBehaviour for Behaviour {
592592
self.circuits.remove(circuit_id);
593593
}
594594

595-
self.queued_actions.push_back(
596-
ToSwarm::GenerateEvent(Event::CircuitReqDenyFailed {
595+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
596+
Event::CircuitReqDenyFailed {
597597
src_peer_id: event_source,
598598
dst_peer_id,
599599
error,
600-
})
601-
.into(),
602-
);
600+
},
601+
));
603602
}
604603
handler::Event::OutboundConnectNegotiated {
605604
circuit_id,
@@ -610,21 +609,18 @@ impl NetworkBehaviour for Behaviour {
610609
dst_stream,
611610
dst_pending_data,
612611
} => {
613-
self.queued_actions.push_back(
614-
ToSwarm::NotifyHandler {
615-
handler: NotifyHandler::One(src_connection_id),
616-
peer_id: src_peer_id,
617-
event: Either::Left(handler::In::AcceptAndDriveCircuit {
618-
circuit_id,
619-
dst_peer_id: event_source,
620-
inbound_circuit_req,
621-
dst_handler_notifier,
622-
dst_stream,
623-
dst_pending_data,
624-
}),
625-
}
626-
.into(),
627-
);
612+
self.queued_actions.push_back(ToSwarm::NotifyHandler {
613+
handler: NotifyHandler::One(src_connection_id),
614+
peer_id: src_peer_id,
615+
event: Either::Left(handler::In::AcceptAndDriveCircuit {
616+
circuit_id,
617+
dst_peer_id: event_source,
618+
inbound_circuit_req,
619+
dst_handler_notifier,
620+
dst_stream,
621+
dst_pending_data,
622+
}),
623+
});
628624
}
629625
handler::Event::OutboundConnectNegotiationFailed {
630626
circuit_id,
@@ -634,54 +630,47 @@ impl NetworkBehaviour for Behaviour {
634630
status,
635631
error,
636632
} => {
637-
self.queued_actions.push_back(
638-
ToSwarm::NotifyHandler {
639-
handler: NotifyHandler::One(src_connection_id),
640-
peer_id: src_peer_id,
641-
event: Either::Left(handler::In::DenyCircuitReq {
642-
circuit_id: Some(circuit_id),
643-
inbound_circuit_req,
644-
status,
645-
}),
646-
}
647-
.into(),
648-
);
649-
self.queued_actions.push_back(
650-
ToSwarm::GenerateEvent(Event::CircuitReqOutboundConnectFailed {
633+
self.queued_actions.push_back(ToSwarm::NotifyHandler {
634+
handler: NotifyHandler::One(src_connection_id),
635+
peer_id: src_peer_id,
636+
event: Either::Left(handler::In::DenyCircuitReq {
637+
circuit_id: Some(circuit_id),
638+
inbound_circuit_req,
639+
status,
640+
}),
641+
});
642+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
643+
Event::CircuitReqOutboundConnectFailed {
651644
src_peer_id,
652645
dst_peer_id: event_source,
653646
error,
654-
})
655-
.into(),
656-
);
647+
},
648+
));
657649
}
658650
handler::Event::CircuitReqAccepted {
659651
dst_peer_id,
660652
circuit_id,
661653
} => {
662654
self.circuits.accepted(circuit_id);
663-
self.queued_actions.push_back(
664-
ToSwarm::GenerateEvent(Event::CircuitReqAccepted {
655+
self.queued_actions
656+
.push_back(ToSwarm::GenerateEvent(Event::CircuitReqAccepted {
665657
src_peer_id: event_source,
666658
dst_peer_id,
667-
})
668-
.into(),
669-
);
659+
}));
670660
}
671661
handler::Event::CircuitReqAcceptFailed {
672662
dst_peer_id,
673663
circuit_id,
674664
error,
675665
} => {
676666
self.circuits.remove(circuit_id);
677-
self.queued_actions.push_back(
678-
ToSwarm::GenerateEvent(Event::CircuitReqAcceptFailed {
667+
self.queued_actions.push_back(ToSwarm::GenerateEvent(
668+
Event::CircuitReqAcceptFailed {
679669
src_peer_id: event_source,
680670
dst_peer_id,
681671
error,
682-
})
683-
.into(),
684-
);
672+
},
673+
));
685674
}
686675
handler::Event::CircuitClosed {
687676
dst_peer_id,
@@ -690,14 +679,12 @@ impl NetworkBehaviour for Behaviour {
690679
} => {
691680
self.circuits.remove(circuit_id);
692681

693-
self.queued_actions.push_back(
694-
ToSwarm::GenerateEvent(Event::CircuitClosed {
682+
self.queued_actions
683+
.push_back(ToSwarm::GenerateEvent(Event::CircuitClosed {
695684
src_peer_id: event_source,
696685
dst_peer_id,
697686
error,
698-
})
699-
.into(),
700-
);
687+
}));
701688
}
702689
}
703690
}
@@ -707,8 +694,8 @@ impl NetworkBehaviour for Behaviour {
707694
_cx: &mut Context<'_>,
708695
_: &mut impl PollParameters,
709696
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
710-
if let Some(action) = self.queued_actions.pop_front() {
711-
return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses));
697+
if let Some(to_swarm) = self.queued_actions.pop_front() {
698+
return Poll::Ready(to_swarm);
712699
}
713700

714701
Poll::Pending
@@ -804,53 +791,3 @@ impl Add<u64> for CircuitId {
804791
CircuitId(self.0 + rhs)
805792
}
806793
}
807-
808-
/// A [`ToSwarm`], either complete, or still requiring data from [`PollParameters`]
809-
/// before being returned in [`Behaviour::poll`].
810-
#[allow(clippy::large_enum_variant)]
811-
enum Action {
812-
Done(ToSwarm<Event, Either<handler::In, Void>>),
813-
AcceptReservationPrototype {
814-
inbound_reservation_req: inbound_hop::ReservationReq,
815-
handler: NotifyHandler,
816-
peer_id: PeerId,
817-
},
818-
}
819-
820-
impl From<ToSwarm<Event, Either<handler::In, Void>>> for Action {
821-
fn from(action: ToSwarm<Event, Either<handler::In, Void>>) -> Self {
822-
Self::Done(action)
823-
}
824-
}
825-
826-
impl Action {
827-
fn build(
828-
self,
829-
local_peer_id: PeerId,
830-
external_addresses: &ExternalAddresses,
831-
) -> ToSwarm<Event, Either<handler::In, Void>> {
832-
match self {
833-
Action::Done(action) => action,
834-
Action::AcceptReservationPrototype {
835-
inbound_reservation_req,
836-
handler,
837-
peer_id,
838-
} => ToSwarm::NotifyHandler {
839-
handler,
840-
peer_id,
841-
event: Either::Left(handler::In::AcceptReservationReq {
842-
inbound_reservation_req,
843-
addrs: external_addresses
844-
.iter()
845-
.cloned()
846-
// Add local peer ID in case it isn't present yet.
847-
.filter_map(|a| match a.iter().last()? {
848-
Protocol::P2p(_) => Some(a),
849-
_ => Some(a.with(Protocol::P2p(local_peer_id))),
850-
})
851-
.collect(),
852-
}),
853-
},
854-
}
855-
}
856-
}

0 commit comments

Comments
 (0)