Skip to content

Commit

Permalink
feat(swarm): rename NetworkBehaviourAction to ToSwarm
Browse files Browse the repository at this point in the history
Resolves #3123.

Pull-Request: #3658.
  • Loading branch information
thomaseizinger committed Mar 24, 2023
1 parent 7ffa63b commit dcbc04e
Show file tree
Hide file tree
Showing 32 changed files with 698 additions and 807 deletions.
2 changes: 1 addition & 1 deletion docs/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ request without having to guess.

When accepting a **command** that eventually results in a response through an event require that
command to contain a unique ID, which is later on contained in the asynchronous response event. One
such example is the `Swarm` accepting a `NetworkBehaviourAction::Dial` from the `NetworkBehaviour`.
such example is the `Swarm` accepting a `ToSwarm::Dial` from the `NetworkBehaviour`.

``` rust
struct Command {
Expand Down
6 changes: 3 additions & 3 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
dummy, CloseConnection, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour,
NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::{HashSet, VecDeque};
use std::fmt;
Expand Down Expand Up @@ -261,9 +261,9 @@ where
&mut self,
cx: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
if let Some(peer) = self.close_connections.pop_front() {
return Poll::Ready(NetworkBehaviourAction::CloseConnection {
return Poll::Ready(ToSwarm::CloseConnection {
peer_id: peer,
connection: CloseConnection::All,
});
Expand Down
4 changes: 2 additions & 2 deletions misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
dummy, ConnectionClosed, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour,
NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::{HashMap, HashSet};
use std::fmt;
Expand Down Expand Up @@ -355,7 +355,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
Poll::Pending
}
}
Expand Down
23 changes: 7 additions & 16 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -208,9 +208,7 @@ pub struct Behaviour {

last_probe: Option<Instant>,

pending_actions: VecDeque<
NetworkBehaviourAction<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>,
>,
pending_actions: VecDeque<ToSwarm<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>>,

probe_id: ProbeId,

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)));
}
}

Expand Down Expand Up @@ -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 { .. },
Expand Down Expand Up @@ -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 => {}
Expand Down Expand Up @@ -601,8 +593,7 @@ impl NetworkBehaviour for Behaviour {
}
}

type Action =
NetworkBehaviourAction<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;
type Action = ToSwarm<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;

// Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol.
trait HandleInnerEvent {
Expand Down
21 changes: 8 additions & 13 deletions protocols/autonat/src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
});
Expand All @@ -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),
Expand Down
16 changes: 8 additions & 8 deletions protocols/autonat/src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(
Expand All @@ -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),
}),
)])
},
))])
}
}
}
Expand All @@ -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,
Expand Down

0 comments on commit dcbc04e

Please sign in to comment.