From 03f9fb215b47354ddc5201bff662b8fc6f84515c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 24 Mar 2023 14:43:49 +0100 Subject: [PATCH] feat(swarm): rename `NetworkBehaviourAction` to `ToSwarm` Resolves #3123. Pull-Request: #3658. --- src/behaviour.rs | 23 +++++++---------------- src/behaviour/as_client.rs | 21 ++++++++------------- src/behaviour/as_server.rs | 16 ++++++++-------- 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/behaviour.rs b/src/behaviour.rs index 47f15b8def6..85b3fa2e7c9 100644 --- a/src/behaviour.rs +++ b/src/behaviour.rs @@ -39,7 +39,7 @@ use libp2p_swarm::{ ExpiredListenAddr, FromSwarm, }, ConnectionDenied, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour, - NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent, + PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, }; use std::{ collections::{HashMap, VecDeque}, @@ -208,9 +208,7 @@ pub struct Behaviour { last_probe: Option, - pending_actions: VecDeque< - NetworkBehaviourAction<::OutEvent, THandlerInEvent>, - >, + pending_actions: VecDeque::OutEvent, THandlerInEvent>>, probe_id: ProbeId, @@ -336,9 +334,7 @@ impl Behaviour { } => { if let Some(event) = self.as_server().on_outbound_connection(&peer, address) { self.pending_actions - .push_back(NetworkBehaviourAction::GenerateEvent(Event::InboundProbe( - event, - ))); + .push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event))); } } ConnectedPoint::Dialer { @@ -399,9 +395,7 @@ impl Behaviour { })); if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) { self.pending_actions - .push_back(NetworkBehaviourAction::GenerateEvent(Event::InboundProbe( - event, - ))); + .push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event))); } } @@ -441,7 +435,7 @@ impl NetworkBehaviour for Behaviour { } match self.inner.poll(cx, params) { - Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) => { + Poll::Ready(ToSwarm::GenerateEvent(event)) => { let actions = match event { request_response::Event::Message { message: request_response::Message::Response { .. }, @@ -474,9 +468,7 @@ impl NetworkBehaviour for Behaviour { match self.as_client().poll_auto_probe(cx) { Poll::Ready(event) => { self.pending_actions - .push_back(NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe( - event, - ))); + .push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event))); continue; } Poll::Pending => {} @@ -601,8 +593,7 @@ impl NetworkBehaviour for Behaviour { } } -type Action = - NetworkBehaviourAction<::OutEvent, THandlerInEvent>; +type Action = ToSwarm<::OutEvent, THandlerInEvent>; // Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol. trait HandleInnerEvent { diff --git a/src/behaviour/as_client.rs b/src/behaviour/as_client.rs index 2ab66f8bffc..d544e8398c7 100644 --- a/src/behaviour/as_client.rs +++ b/src/behaviour/as_client.rs @@ -31,8 +31,7 @@ use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use libp2p_request_response::{self as request_response, OutboundFailure, RequestId}; use libp2p_swarm::{ - AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviourAction, - PollParameters, + AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, PollParameters, ToSwarm, }; use rand::{seq::SliceRandom, thread_rng}; use std::{ @@ -143,17 +142,13 @@ impl<'a> HandleInnerEvent for AsClient<'a> { let mut actions = VecDeque::with_capacity(3); - actions.push_back(NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe( - event, - ))); + actions.push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event))); if let Some(old) = self.handle_reported_status(response.result.clone().into()) { - actions.push_back(NetworkBehaviourAction::GenerateEvent( - Event::StatusChanged { - old, - new: self.nat_status.clone(), - }, - )); + actions.push_back(ToSwarm::GenerateEvent(Event::StatusChanged { + old, + new: self.nat_status.clone(), + })); } if let Ok(address) = response.result { @@ -165,7 +160,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> { .find_map(|r| (r.addr == address).then_some(r.score)) .unwrap_or(AddressScore::Finite(0)); if let AddressScore::Finite(finite_score) = score { - actions.push_back(NetworkBehaviourAction::ReportObservedAddr { + actions.push_back(ToSwarm::ReportObservedAddr { address, score: AddressScore::Finite(finite_score + 1), }); @@ -191,7 +186,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> { self.schedule_probe.reset(Duration::ZERO); - VecDeque::from([NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe( + VecDeque::from([ToSwarm::GenerateEvent(Event::OutboundProbe( OutboundProbeEvent::Error { probe_id, peer: Some(peer), diff --git a/src/behaviour/as_server.rs b/src/behaviour/as_server.rs index 69f0474e621..822b0552e7f 100644 --- a/src/behaviour/as_server.rs +++ b/src/behaviour/as_server.rs @@ -30,7 +30,7 @@ use libp2p_request_response::{ }; use libp2p_swarm::{ dial_opts::{DialOpts, PeerCondition}, - ConnectionId, DialError, NetworkBehaviourAction, PollParameters, + ConnectionId, DialError, PollParameters, ToSwarm, }; use std::{ collections::{HashMap, HashSet, VecDeque}, @@ -124,14 +124,14 @@ impl<'a> HandleInnerEvent for AsServer<'a> { self.throttled_clients.push((peer, Instant::now())); VecDeque::from([ - NetworkBehaviourAction::GenerateEvent(Event::InboundProbe( + ToSwarm::GenerateEvent(Event::InboundProbe( InboundProbeEvent::Request { probe_id, peer, addresses: addrs.clone(), }, )), - NetworkBehaviourAction::Dial { + ToSwarm::Dial { opts: DialOpts::peer_id(peer) .condition(PeerCondition::Always) .override_dial_concurrency_factor( @@ -155,13 +155,13 @@ impl<'a> HandleInnerEvent for AsServer<'a> { }; let _ = self.inner.send_response(channel, response); - VecDeque::from([NetworkBehaviourAction::GenerateEvent( - Event::InboundProbe(InboundProbeEvent::Error { + VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe( + InboundProbeEvent::Error { probe_id, peer, error: InboundProbeError::Response(error), - }), - )]) + }, + ))]) } } } @@ -183,7 +183,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> { _ => self.probe_id.next(), }; - VecDeque::from([NetworkBehaviourAction::GenerateEvent(Event::InboundProbe( + VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe( InboundProbeEvent::Error { probe_id, peer,