Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swarm): rename NetworkBehaviourAction to ToSwarm #3658

Merged
merged 5 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change libp2p-allow-block-list depends on libp2p-swarm >=0.42.1, but its Cargo.toml allows libp2p-swarm v0.42.0.

I don't think this is a pressing issue, thus not sure whether to release a patch release of libp2p-allow-block-list with the above restriction. What do you think @thomaseizinger?

The same applies to other crates below that we have released since.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it is an issue. We should have bumped the dependency version but not the version of each dependent crate.

I'll send a fix.

Can we mitigate this with an automated check somehow? Perhaps -Z minimal-versions helps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here: #3711.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mitigate this with an automated check somehow? Perhaps -Z minimal-versions helps?

I think I found a solution: #3715

};
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
Loading