Skip to content

Commit dc8433e

Browse files
swarm/src/behaviour: Merge inject_* paired methods (#2445)
Co-authored-by: Max Inden <[email protected]>
1 parent 5a95a46 commit dc8433e

File tree

30 files changed

+584
-482
lines changed

30 files changed

+584
-482
lines changed

protocols/autonat/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Update to `libp2p-request-response` `v0.16.0`.
88

9+
- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).
10+
11+
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445
12+
913
# 0.1.0 [2022-01-27]
1014

11-
- Initial release.
15+
- Initial release.

protocols/autonat/src/behaviour.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,15 @@ impl NetworkBehaviour for Behaviour {
303303
conn: &ConnectionId,
304304
endpoint: &ConnectedPoint,
305305
failed_addresses: Option<&Vec<Multiaddr>>,
306+
other_established: usize,
306307
) {
307-
self.inner
308-
.inject_connection_established(peer, conn, endpoint, failed_addresses);
308+
self.inner.inject_connection_established(
309+
peer,
310+
conn,
311+
endpoint,
312+
failed_addresses,
313+
other_established,
314+
);
309315
let connections = self.connected.entry(*peer).or_default();
310316
let addr = if endpoint.is_relayed() {
311317
None
@@ -342,11 +348,16 @@ impl NetworkBehaviour for Behaviour {
342348
conn: &ConnectionId,
343349
endpoint: &ConnectedPoint,
344350
handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
351+
remaining_established: usize,
345352
) {
346353
self.inner
347-
.inject_connection_closed(peer, conn, endpoint, handler);
348-
let connections = self.connected.get_mut(peer).expect("Peer is connected.");
349-
connections.remove(conn);
354+
.inject_connection_closed(peer, conn, endpoint, handler, remaining_established);
355+
if remaining_established == 0 {
356+
self.connected.remove(peer);
357+
} else {
358+
let connections = self.connected.get_mut(peer).expect("Peer is connected.");
359+
connections.remove(conn);
360+
}
350361
}
351362

352363
fn inject_dial_failure(
@@ -362,11 +373,6 @@ impl NetworkBehaviour for Behaviour {
362373
}
363374
}
364375

365-
fn inject_disconnected(&mut self, peer: &PeerId) {
366-
self.inner.inject_disconnected(peer);
367-
self.connected.remove(peer);
368-
}
369-
370376
fn inject_address_change(
371377
&mut self,
372378
peer: &PeerId,
@@ -461,10 +467,6 @@ impl NetworkBehaviour for Behaviour {
461467
self.inner.addresses_of_peer(peer)
462468
}
463469

464-
fn inject_connected(&mut self, peer: &PeerId) {
465-
self.inner.inject_connected(peer)
466-
}
467-
468470
fn inject_event(
469471
&mut self,
470472
peer_id: PeerId,

protocols/dcutr/src/behaviour.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl NetworkBehaviour for Behaviour {
100100
connection_id: &ConnectionId,
101101
connected_point: &ConnectedPoint,
102102
_failed_addresses: Option<&Vec<Multiaddr>>,
103+
_other_established: usize,
103104
) {
104105
if connected_point.is_relayed() {
105106
if connected_point.is_listener() && !self.direct_connections.contains_key(peer_id) {
@@ -181,16 +182,13 @@ impl NetworkBehaviour for Behaviour {
181182
}
182183
}
183184

184-
fn inject_disconnected(&mut self, peer_id: &PeerId) {
185-
assert!(!self.direct_connections.contains_key(peer_id));
186-
}
187-
188185
fn inject_connection_closed(
189186
&mut self,
190187
peer_id: &PeerId,
191188
connection_id: &ConnectionId,
192189
connected_point: &ConnectedPoint,
193190
_handler: <<Self as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler,
191+
_remaining_established: usize,
194192
) {
195193
if !connected_point.is_relayed() {
196194
let connections = self

protocols/floodsub/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Update to `libp2p-swarm` `v0.34.0`.
66

7+
- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).
8+
9+
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445
10+
711
# 0.33.0 [2022-01-27]
812

913
- Update dependencies.

protocols/floodsub/src/layer.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::FloodsubConfig;
2727
use cuckoofilter::{CuckooError, CuckooFilter};
2828
use fnv::FnvHashSet;
2929
use libp2p_core::{connection::ConnectionId, PeerId};
30+
use libp2p_core::{ConnectedPoint, Multiaddr};
3031
use libp2p_swarm::{
3132
dial_opts::{self, DialOpts},
3233
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler, PollParameters,
@@ -287,7 +288,19 @@ impl NetworkBehaviour for Floodsub {
287288
Default::default()
288289
}
289290

290-
fn inject_connected(&mut self, id: &PeerId) {
291+
fn inject_connection_established(
292+
&mut self,
293+
id: &PeerId,
294+
_: &ConnectionId,
295+
_: &ConnectedPoint,
296+
_: Option<&Vec<Multiaddr>>,
297+
other_established: usize,
298+
) {
299+
if other_established > 0 {
300+
// We only care about the first time a peer connects.
301+
return;
302+
}
303+
291304
// We need to send our subscriptions to the newly-connected node.
292305
if self.target_peers.contains(id) {
293306
for topic in self.subscribed_topics.iter().cloned() {
@@ -309,7 +322,19 @@ impl NetworkBehaviour for Floodsub {
309322
self.connected_peers.insert(*id, SmallVec::new());
310323
}
311324

312-
fn inject_disconnected(&mut self, id: &PeerId) {
325+
fn inject_connection_closed(
326+
&mut self,
327+
id: &PeerId,
328+
_: &ConnectionId,
329+
_: &ConnectedPoint,
330+
_: Self::ProtocolsHandler,
331+
remaining_established: usize,
332+
) {
333+
if remaining_established > 0 {
334+
// we only care about peer disconnections
335+
return;
336+
}
337+
313338
let was_in = self.connected_peers.remove(id);
314339
debug_assert!(was_in.is_some());
315340

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
- Emit gossip of all non empty topics (see [PR 2481]).
1010

11+
- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).
12+
1113
[PR 2442]: https://github.com/libp2p/rust-libp2p/pull/2442
1214
[PR 2481]: https://github.com/libp2p/rust-libp2p/pull/2481
15+
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445
1316

1417
# 0.35.0 [2022-01-27]
1518

0 commit comments

Comments
 (0)