Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Swarm cleanup following libp2p upgrade to v0.39.1 #473

Merged
merged 3 commits into from
Aug 23, 2021
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* fix: compilation error when used as a dependency [#470]
* perf: use hash_hasher where the key is Cid [#467]
* chore: upgrade to libp2p 0.39.1, update most of the other deps with the notable exception of cid and multihash [#472]
* refactor(swarm): swarm cleanup following libp2p upgrade to v0.39.1 [#473]

[#429]: https://github.com/rs-ipfs/rust-ipfs/pull/429
[#428]: https://github.com/rs-ipfs/rust-ipfs/pull/428
Expand All @@ -25,6 +26,7 @@
[#470]: https://github.com/rs-ipfs/rust-ipfs/pull/470
[#467]: https://github.com/rs-ipfs/rust-ipfs/pull/467
[#472]: https://github.com/rs-ipfs/rust-ipfs/pull/472
[#473]: https://github.com/rs-ipfs/rust-ipfs/pull/473

# 0.2.1

Expand Down
44 changes: 0 additions & 44 deletions src/p2p/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,6 @@ pub(crate) fn could_be_bound_from_ephemeral(
}
}

// Checks if two instances of multiaddr are equal comparing as many protocol segments as possible
pub(crate) fn eq_greedy(addr0: &Multiaddr, addr1: &Multiaddr) -> bool {
if addr0.is_empty() != addr1.is_empty() {
return false;
}
addr0.iter().zip(addr1.iter()).all(|(a, b)| a == b)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -309,40 +301,4 @@ mod tests {
&build_multiaddr!(Ip4([127, 0, 0, 1]), Tcp(44444u16))
));
}

#[test]
fn greedy_multiaddr_comparison() {
assert!(eq_greedy(&Multiaddr::empty(), &Multiaddr::empty()));
assert!(eq_greedy(
&build_multiaddr!(Ip4([192, 168, 0, 1])),
&build_multiaddr!(Ip4([192, 168, 0, 1]))
));
assert!(eq_greedy(
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16)),
&build_multiaddr!(Ip4([192, 168, 0, 1]))
));
assert!(eq_greedy(
&build_multiaddr!(Ip4([192, 168, 0, 1])),
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16))
));

// At least one protocol segment needs to be there
assert!(!eq_greedy(
&Multiaddr::empty(),
&build_multiaddr!(Ip4([192, 168, 0, 1]))
));
assert!(!eq_greedy(
&build_multiaddr!(Ip4([192, 168, 0, 1])),
&Multiaddr::empty()
));

assert!(!eq_greedy(
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16)),
&build_multiaddr!(Ip4([192, 168, 0, 2]))
));
assert!(!eq_greedy(
&build_multiaddr!(Ip4([192, 168, 0, 2])),
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16))
));
}
}
51 changes: 18 additions & 33 deletions src/p2p/swarm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::p2p::{addr::eq_greedy, MultiaddrWithPeerId, MultiaddrWithoutPeerId};
use crate::p2p::{MultiaddrWithPeerId, MultiaddrWithoutPeerId};
use crate::subscription::{SubscriptionFuture, SubscriptionRegistry};
use core::task::{Context, Poll};
use libp2p::core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
Expand Down Expand Up @@ -49,11 +49,11 @@ pub struct SwarmApi {

/// The connections which have been requested, but the swarm/network is yet to ask for
/// addresses; currently filled in the order of adding, with the default size of one.
pending_addresses: HashMap<PeerId, Vec<Multiaddr>>,
pending_addresses: HashMap<PeerId, Vec<MultiaddrWithPeerId>>,

/// The connections which have been requested, and the swarm/network has requested the
/// addresses of. Used to keep finishing all of the subscriptions.
pending_connections: HashMap<PeerId, Vec<Multiaddr>>,
pending_connections: HashMap<PeerId, Vec<MultiaddrWithPeerId>>,

pub(crate) bootstrappers: HashSet<MultiaddrWithPeerId>,
}
Expand Down Expand Up @@ -106,21 +106,17 @@ impl SwarmApi {
.connect_registry
.create_subscription(addr.clone().into(), None);

// libp2p currently doesn't support dialing with the P2p protocol, so only consider the
// "bare" Multiaddr
let MultiaddrWithPeerId { multiaddr, peer_id } = addr;

self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id,
peer_id: addr.peer_id,
// rationale: this is sort of explicit command, perhaps the old address is no longer
// valid. Always would be even better but it's bugged at the moment.
condition: DialPeerCondition::NotDialing,
});

self.pending_addresses
.entry(peer_id)
.entry(addr.peer_id)
.or_insert_with(|| Vec::with_capacity(1))
.push(multiaddr.into());
.push(addr);

Some(subscription)
}
Expand Down Expand Up @@ -163,7 +159,7 @@ impl NetworkBehaviour for SwarmApi {
.or_default()
.extend(addresses.iter().cloned());

addresses
addresses.into_iter().map(|a| a.into()).collect()
}

fn inject_connection_established(
Expand Down Expand Up @@ -194,18 +190,19 @@ impl NetworkBehaviour for SwarmApi {
match self.pending_connections.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let addresses = oe.get_mut();
let just_connected = addresses.iter().position(|x| eq_greedy(x, address));
let address: MultiaddrWithPeerId = address
.clone()
.try_into()
.expect("dialed address contains peerid in libp2p 0.38");
let just_connected = addresses.iter().position(|x| *x == address);
if let Some(just_connected) = just_connected {
addresses.swap_remove(just_connected);
if addresses.is_empty() {
oe.remove();
}

let addr = MultiaddrWithPeerId::try_from(address.clone())
.expect("dialed address contains peerid in libp2p 0.38");

self.connect_registry
.finish_subscription(addr.into(), Ok(()));
.finish_subscription(address.into(), Ok(()));
}
}
Entry::Vacant(_) => {
Expand Down Expand Up @@ -235,10 +232,6 @@ impl NetworkBehaviour for SwarmApi {
);

for addr in all_subs {
let addr = MultiaddrWithoutPeerId::try_from(addr)
.expect("peerid has been stripped earlier")
.with(*peer_id);

// fail the other than already connected subscriptions in
// inject_connection_established. while the whole swarmapi is quite unclear on the
// actual use cases, assume that connecting one is good enough for all outstanding
Expand Down Expand Up @@ -290,7 +283,7 @@ impl NetworkBehaviour for SwarmApi {
match self.pending_connections.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let connections = oe.get_mut();
let pos = connections.iter().position(|x| addr.multiaddr == *x);
let pos = connections.iter().position(|x| addr == *x);

if let Some(pos) = pos {
connections.swap_remove(pos);
Expand Down Expand Up @@ -335,10 +328,6 @@ impl NetworkBehaviour for SwarmApi {
);

for addr in failed {
let addr = MultiaddrWithoutPeerId::try_from(addr)
.expect("peerid has been stripped earlier")
.with(*peer_id);

self.connect_registry
.finish_subscription(addr.into(), Err("disconnected".into()));
}
Expand All @@ -361,12 +350,8 @@ impl NetworkBehaviour for SwarmApi {
// this should not be executed once, but probably will be in case unsupported addresses or something
// surprising happens.
for failed in self.pending_connections.remove(peer_id).unwrap_or_default() {
let addr = MultiaddrWithoutPeerId::try_from(failed)
.expect("peerid has been stripped earlier")
.with(*peer_id);

self.connect_registry
.finish_subscription(addr.into(), Err("addresses exhausted".into()));
.finish_subscription(failed.into(), Err("addresses exhausted".into()));
}
}

Expand All @@ -382,12 +367,12 @@ impl NetworkBehaviour for SwarmApi {
match self.pending_connections.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let addresses = oe.get_mut();
let pos = addresses.iter().position(|a| eq_greedy(a, addr));
let addr = MultiaddrWithPeerId::try_from(addr.clone())
.expect("dialed address contains peerid in libp2p 0.38");
let pos = addresses.iter().position(|a| *a == addr);

if let Some(pos) = pos {
addresses.swap_remove(pos);
let addr = MultiaddrWithPeerId::try_from(addr.clone())
.expect("dialed address contains peerid in libp2p 0.38");
self.connect_registry
.finish_subscription(addr.into(), Err(error.to_string()));
}
Expand Down
4 changes: 2 additions & 2 deletions unixfs/src/dir/builder/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl PostOrderIterator {
};

self.pending.push(Visited::PostRoot { leaves });
self.pending.extend(children.drain(..));
self.pending.append(children);
}
Visited::Descent {
node,
Expand Down Expand Up @@ -215,7 +215,7 @@ impl PostOrderIterator {
index,
});

self.pending.extend(children.drain(..));
self.pending.append(children);
}
Visited::Post {
parent_id,
Expand Down