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

Commit

Permalink
refactor(swarm): simplify usage of multiaddr after libp2p upgrade to …
Browse files Browse the repository at this point in the history
…0.39

Get rid of eq_greedy.
  • Loading branch information
CHr15F0x committed Aug 23, 2021
1 parent ede42eb commit 136496f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 65 deletions.
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))
));
}
}
31 changes: 12 additions & 19 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 @@ -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.into());

Some(subscription)
}
Expand Down Expand Up @@ -194,7 +190,7 @@ 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 just_connected = addresses.iter().position(|a| a == address);
if let Some(just_connected) = just_connected {
addresses.swap_remove(just_connected);
if addresses.is_empty() {
Expand Down Expand Up @@ -235,9 +231,8 @@ impl NetworkBehaviour for SwarmApi {
);

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

// fail the other than already connected subscriptions in
// inject_connection_established. while the whole swarmapi is quite unclear on the
Expand Down Expand Up @@ -335,9 +330,8 @@ impl NetworkBehaviour for SwarmApi {
);

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

self.connect_registry
.finish_subscription(addr.into(), Err("disconnected".into()));
Expand All @@ -361,9 +355,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);
let addr = MultiaddrWithPeerId::try_from(failed)
.expect("dialed address contains peerid in libp2p 0.38");

self.connect_registry
.finish_subscription(addr.into(), Err("addresses exhausted".into()));
Expand All @@ -382,7 +375,7 @@ 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 pos = addresses.iter().position(|a| a == addr);

if let Some(pos) = pos {
addresses.swap_remove(pos);
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

0 comments on commit 136496f

Please sign in to comment.