Skip to content

Commit

Permalink
swarm/src/behaviour: Merge inject_* paired methods (#2445)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Inden <mail@max-inden.de>
  • Loading branch information
divagant-martian and mxinden authored Feb 9, 2022
1 parent 5a95a46 commit dc8433e
Show file tree
Hide file tree
Showing 30 changed files with 584 additions and 482 deletions.
6 changes: 5 additions & 1 deletion protocols/autonat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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

- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).

[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445

# 0.1.0 [2022-01-27]

- Initial release.
- Initial release.
30 changes: 16 additions & 14 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,15 @@ impl NetworkBehaviour for Behaviour {
conn: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.inner
.inject_connection_established(peer, conn, endpoint, failed_addresses);
self.inner.inject_connection_established(
peer,
conn,
endpoint,
failed_addresses,
other_established,
);
let connections = self.connected.entry(*peer).or_default();
let addr = if endpoint.is_relayed() {
None
Expand Down Expand Up @@ -342,11 +348,16 @@ impl NetworkBehaviour for Behaviour {
conn: &ConnectionId,
endpoint: &ConnectedPoint,
handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
remaining_established: usize,
) {
self.inner
.inject_connection_closed(peer, conn, endpoint, handler);
let connections = self.connected.get_mut(peer).expect("Peer is connected.");
connections.remove(conn);
.inject_connection_closed(peer, conn, endpoint, handler, remaining_established);
if remaining_established == 0 {
self.connected.remove(peer);
} else {
let connections = self.connected.get_mut(peer).expect("Peer is connected.");
connections.remove(conn);
}
}

fn inject_dial_failure(
Expand All @@ -362,11 +373,6 @@ impl NetworkBehaviour for Behaviour {
}
}

fn inject_disconnected(&mut self, peer: &PeerId) {
self.inner.inject_disconnected(peer);
self.connected.remove(peer);
}

fn inject_address_change(
&mut self,
peer: &PeerId,
Expand Down Expand Up @@ -461,10 +467,6 @@ impl NetworkBehaviour for Behaviour {
self.inner.addresses_of_peer(peer)
}

fn inject_connected(&mut self, peer: &PeerId) {
self.inner.inject_connected(peer)
}

fn inject_event(
&mut self,
peer_id: PeerId,
Expand Down
6 changes: 2 additions & 4 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl NetworkBehaviour for Behaviour {
connection_id: &ConnectionId,
connected_point: &ConnectedPoint,
_failed_addresses: Option<&Vec<Multiaddr>>,
_other_established: usize,
) {
if connected_point.is_relayed() {
if connected_point.is_listener() && !self.direct_connections.contains_key(peer_id) {
Expand Down Expand Up @@ -181,16 +182,13 @@ impl NetworkBehaviour for Behaviour {
}
}

fn inject_disconnected(&mut self, peer_id: &PeerId) {
assert!(!self.direct_connections.contains_key(peer_id));
}

fn inject_connection_closed(
&mut self,
peer_id: &PeerId,
connection_id: &ConnectionId,
connected_point: &ConnectedPoint,
_handler: <<Self as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler,
_remaining_established: usize,
) {
if !connected_point.is_relayed() {
let connections = self
Expand Down
4 changes: 4 additions & 0 deletions protocols/floodsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

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

- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).

[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445

# 0.33.0 [2022-01-27]

- Update dependencies.
Expand Down
29 changes: 27 additions & 2 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::FloodsubConfig;
use cuckoofilter::{CuckooError, CuckooFilter};
use fnv::FnvHashSet;
use libp2p_core::{connection::ConnectionId, PeerId};
use libp2p_core::{ConnectedPoint, Multiaddr};
use libp2p_swarm::{
dial_opts::{self, DialOpts},
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler, PollParameters,
Expand Down Expand Up @@ -287,7 +288,19 @@ impl NetworkBehaviour for Floodsub {
Default::default()
}

fn inject_connected(&mut self, id: &PeerId) {
fn inject_connection_established(
&mut self,
id: &PeerId,
_: &ConnectionId,
_: &ConnectedPoint,
_: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
if other_established > 0 {
// We only care about the first time a peer connects.
return;
}

// We need to send our subscriptions to the newly-connected node.
if self.target_peers.contains(id) {
for topic in self.subscribed_topics.iter().cloned() {
Expand All @@ -309,7 +322,19 @@ impl NetworkBehaviour for Floodsub {
self.connected_peers.insert(*id, SmallVec::new());
}

fn inject_disconnected(&mut self, id: &PeerId) {
fn inject_connection_closed(
&mut self,
id: &PeerId,
_: &ConnectionId,
_: &ConnectedPoint,
_: Self::ProtocolsHandler,
remaining_established: usize,
) {
if remaining_established > 0 {
// we only care about peer disconnections
return;
}

let was_in = self.connected_peers.remove(id);
debug_assert!(was_in.is_some());

Expand Down
3 changes: 3 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

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

- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).

[PR 2442]: https://github.com/libp2p/rust-libp2p/pull/2442
[PR 2481]: https://github.com/libp2p/rust-libp2p/pull/2481
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445

# 0.35.0 [2022-01-27]

Expand Down
Loading

0 comments on commit dc8433e

Please sign in to comment.