Skip to content

Commit dcbc04e

Browse files
feat(swarm): rename NetworkBehaviourAction to ToSwarm
Resolves #3123. Pull-Request: #3658.
1 parent 7ffa63b commit dcbc04e

File tree

32 files changed

+698
-807
lines changed

32 files changed

+698
-807
lines changed

docs/coding-guidelines.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ request without having to guess.
310310

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

315315
``` rust
316316
struct Command {

misc/allow-block-list/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use libp2p_core::{Endpoint, Multiaddr};
6565
use libp2p_identity::PeerId;
6666
use libp2p_swarm::{
6767
dummy, CloseConnection, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour,
68-
NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
68+
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
6969
};
7070
use std::collections::{HashSet, VecDeque};
7171
use std::fmt;
@@ -261,9 +261,9 @@ where
261261
&mut self,
262262
cx: &mut Context<'_>,
263263
_: &mut impl PollParameters,
264-
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
264+
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
265265
if let Some(peer) = self.close_connections.pop_front() {
266-
return Poll::Ready(NetworkBehaviourAction::CloseConnection {
266+
return Poll::Ready(ToSwarm::CloseConnection {
267267
peer_id: peer,
268268
connection: CloseConnection::All,
269269
});

misc/connection-limits/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use libp2p_core::{Endpoint, Multiaddr};
2222
use libp2p_identity::PeerId;
2323
use libp2p_swarm::{
2424
dummy, ConnectionClosed, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour,
25-
NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
25+
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
2626
};
2727
use std::collections::{HashMap, HashSet};
2828
use std::fmt;
@@ -355,7 +355,7 @@ impl NetworkBehaviour for Behaviour {
355355
&mut self,
356356
_: &mut Context<'_>,
357357
_: &mut impl PollParameters,
358-
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
358+
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
359359
Poll::Pending
360360
}
361361
}

protocols/autonat/src/behaviour.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use libp2p_swarm::{
3939
ExpiredListenAddr, FromSwarm,
4040
},
4141
ConnectionDenied, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour,
42-
NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
42+
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
4343
};
4444
use std::{
4545
collections::{HashMap, VecDeque},
@@ -208,9 +208,7 @@ pub struct Behaviour {
208208

209209
last_probe: Option<Instant>,
210210

211-
pending_actions: VecDeque<
212-
NetworkBehaviourAction<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>,
213-
>,
211+
pending_actions: VecDeque<ToSwarm<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>>,
214212

215213
probe_id: ProbeId,
216214

@@ -336,9 +334,7 @@ impl Behaviour {
336334
} => {
337335
if let Some(event) = self.as_server().on_outbound_connection(&peer, address) {
338336
self.pending_actions
339-
.push_back(NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
340-
event,
341-
)));
337+
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
342338
}
343339
}
344340
ConnectedPoint::Dialer {
@@ -399,9 +395,7 @@ impl Behaviour {
399395
}));
400396
if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) {
401397
self.pending_actions
402-
.push_back(NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
403-
event,
404-
)));
398+
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
405399
}
406400
}
407401

@@ -441,7 +435,7 @@ impl NetworkBehaviour for Behaviour {
441435
}
442436

443437
match self.inner.poll(cx, params) {
444-
Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
438+
Poll::Ready(ToSwarm::GenerateEvent(event)) => {
445439
let actions = match event {
446440
request_response::Event::Message {
447441
message: request_response::Message::Response { .. },
@@ -474,9 +468,7 @@ impl NetworkBehaviour for Behaviour {
474468
match self.as_client().poll_auto_probe(cx) {
475469
Poll::Ready(event) => {
476470
self.pending_actions
477-
.push_back(NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe(
478-
event,
479-
)));
471+
.push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event)));
480472
continue;
481473
}
482474
Poll::Pending => {}
@@ -601,8 +593,7 @@ impl NetworkBehaviour for Behaviour {
601593
}
602594
}
603595

604-
type Action =
605-
NetworkBehaviourAction<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;
596+
type Action = ToSwarm<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;
606597

607598
// Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol.
608599
trait HandleInnerEvent {

protocols/autonat/src/behaviour/as_client.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use libp2p_core::Multiaddr;
3131
use libp2p_identity::PeerId;
3232
use libp2p_request_response::{self as request_response, OutboundFailure, RequestId};
3333
use libp2p_swarm::{
34-
AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviourAction,
35-
PollParameters,
34+
AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, PollParameters, ToSwarm,
3635
};
3736
use rand::{seq::SliceRandom, thread_rng};
3837
use std::{
@@ -143,17 +142,13 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
143142

144143
let mut actions = VecDeque::with_capacity(3);
145144

146-
actions.push_back(NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe(
147-
event,
148-
)));
145+
actions.push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event)));
149146

150147
if let Some(old) = self.handle_reported_status(response.result.clone().into()) {
151-
actions.push_back(NetworkBehaviourAction::GenerateEvent(
152-
Event::StatusChanged {
153-
old,
154-
new: self.nat_status.clone(),
155-
},
156-
));
148+
actions.push_back(ToSwarm::GenerateEvent(Event::StatusChanged {
149+
old,
150+
new: self.nat_status.clone(),
151+
}));
157152
}
158153

159154
if let Ok(address) = response.result {
@@ -165,7 +160,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
165160
.find_map(|r| (r.addr == address).then_some(r.score))
166161
.unwrap_or(AddressScore::Finite(0));
167162
if let AddressScore::Finite(finite_score) = score {
168-
actions.push_back(NetworkBehaviourAction::ReportObservedAddr {
163+
actions.push_back(ToSwarm::ReportObservedAddr {
169164
address,
170165
score: AddressScore::Finite(finite_score + 1),
171166
});
@@ -191,7 +186,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
191186

192187
self.schedule_probe.reset(Duration::ZERO);
193188

194-
VecDeque::from([NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe(
189+
VecDeque::from([ToSwarm::GenerateEvent(Event::OutboundProbe(
195190
OutboundProbeEvent::Error {
196191
probe_id,
197192
peer: Some(peer),

protocols/autonat/src/behaviour/as_server.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use libp2p_request_response::{
3030
};
3131
use libp2p_swarm::{
3232
dial_opts::{DialOpts, PeerCondition},
33-
ConnectionId, DialError, NetworkBehaviourAction, PollParameters,
33+
ConnectionId, DialError, PollParameters, ToSwarm,
3434
};
3535
use std::{
3636
collections::{HashMap, HashSet, VecDeque},
@@ -124,14 +124,14 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
124124
self.throttled_clients.push((peer, Instant::now()));
125125

126126
VecDeque::from([
127-
NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
127+
ToSwarm::GenerateEvent(Event::InboundProbe(
128128
InboundProbeEvent::Request {
129129
probe_id,
130130
peer,
131131
addresses: addrs.clone(),
132132
},
133133
)),
134-
NetworkBehaviourAction::Dial {
134+
ToSwarm::Dial {
135135
opts: DialOpts::peer_id(peer)
136136
.condition(PeerCondition::Always)
137137
.override_dial_concurrency_factor(
@@ -155,13 +155,13 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
155155
};
156156
let _ = self.inner.send_response(channel, response);
157157

158-
VecDeque::from([NetworkBehaviourAction::GenerateEvent(
159-
Event::InboundProbe(InboundProbeEvent::Error {
158+
VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe(
159+
InboundProbeEvent::Error {
160160
probe_id,
161161
peer,
162162
error: InboundProbeError::Response(error),
163-
}),
164-
)])
163+
},
164+
))])
165165
}
166166
}
167167
}
@@ -183,7 +183,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
183183
_ => self.probe_id.next(),
184184
};
185185

186-
VecDeque::from([NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
186+
VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe(
187187
InboundProbeEvent::Error {
188188
probe_id,
189189
peer,

0 commit comments

Comments
 (0)