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

Commit 136496f

Browse files
committed
refactor(swarm): simplify usage of multiaddr after libp2p upgrade to 0.39
Get rid of eq_greedy.
1 parent ede42eb commit 136496f

File tree

3 files changed

+14
-65
lines changed

3 files changed

+14
-65
lines changed

src/p2p/addr.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,6 @@ pub(crate) fn could_be_bound_from_ephemeral(
215215
}
216216
}
217217

218-
// Checks if two instances of multiaddr are equal comparing as many protocol segments as possible
219-
pub(crate) fn eq_greedy(addr0: &Multiaddr, addr1: &Multiaddr) -> bool {
220-
if addr0.is_empty() != addr1.is_empty() {
221-
return false;
222-
}
223-
addr0.iter().zip(addr1.iter()).all(|(a, b)| a == b)
224-
}
225-
226218
#[cfg(test)]
227219
mod tests {
228220
use super::*;
@@ -309,40 +301,4 @@ mod tests {
309301
&build_multiaddr!(Ip4([127, 0, 0, 1]), Tcp(44444u16))
310302
));
311303
}
312-
313-
#[test]
314-
fn greedy_multiaddr_comparison() {
315-
assert!(eq_greedy(&Multiaddr::empty(), &Multiaddr::empty()));
316-
assert!(eq_greedy(
317-
&build_multiaddr!(Ip4([192, 168, 0, 1])),
318-
&build_multiaddr!(Ip4([192, 168, 0, 1]))
319-
));
320-
assert!(eq_greedy(
321-
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16)),
322-
&build_multiaddr!(Ip4([192, 168, 0, 1]))
323-
));
324-
assert!(eq_greedy(
325-
&build_multiaddr!(Ip4([192, 168, 0, 1])),
326-
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16))
327-
));
328-
329-
// At least one protocol segment needs to be there
330-
assert!(!eq_greedy(
331-
&Multiaddr::empty(),
332-
&build_multiaddr!(Ip4([192, 168, 0, 1]))
333-
));
334-
assert!(!eq_greedy(
335-
&build_multiaddr!(Ip4([192, 168, 0, 1])),
336-
&Multiaddr::empty()
337-
));
338-
339-
assert!(!eq_greedy(
340-
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16)),
341-
&build_multiaddr!(Ip4([192, 168, 0, 2]))
342-
));
343-
assert!(!eq_greedy(
344-
&build_multiaddr!(Ip4([192, 168, 0, 2])),
345-
&build_multiaddr!(Ip4([192, 168, 0, 1]), Tcp(44444u16))
346-
));
347-
}
348304
}

src/p2p/swarm.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::p2p::{addr::eq_greedy, MultiaddrWithPeerId, MultiaddrWithoutPeerId};
1+
use crate::p2p::{MultiaddrWithPeerId, MultiaddrWithoutPeerId};
22
use crate::subscription::{SubscriptionFuture, SubscriptionRegistry};
33
use core::task::{Context, Poll};
44
use libp2p::core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
@@ -106,21 +106,17 @@ impl SwarmApi {
106106
.connect_registry
107107
.create_subscription(addr.clone().into(), None);
108108

109-
// libp2p currently doesn't support dialing with the P2p protocol, so only consider the
110-
// "bare" Multiaddr
111-
let MultiaddrWithPeerId { multiaddr, peer_id } = addr;
112-
113109
self.events.push_back(NetworkBehaviourAction::DialPeer {
114-
peer_id,
110+
peer_id: addr.peer_id,
115111
// rationale: this is sort of explicit command, perhaps the old address is no longer
116112
// valid. Always would be even better but it's bugged at the moment.
117113
condition: DialPeerCondition::NotDialing,
118114
});
119115

120116
self.pending_addresses
121-
.entry(peer_id)
117+
.entry(addr.peer_id)
122118
.or_insert_with(|| Vec::with_capacity(1))
123-
.push(multiaddr.into());
119+
.push(addr.into());
124120

125121
Some(subscription)
126122
}
@@ -194,7 +190,7 @@ impl NetworkBehaviour for SwarmApi {
194190
match self.pending_connections.entry(*peer_id) {
195191
Entry::Occupied(mut oe) => {
196192
let addresses = oe.get_mut();
197-
let just_connected = addresses.iter().position(|x| eq_greedy(x, address));
193+
let just_connected = addresses.iter().position(|a| a == address);
198194
if let Some(just_connected) = just_connected {
199195
addresses.swap_remove(just_connected);
200196
if addresses.is_empty() {
@@ -235,9 +231,8 @@ impl NetworkBehaviour for SwarmApi {
235231
);
236232

237233
for addr in all_subs {
238-
let addr = MultiaddrWithoutPeerId::try_from(addr)
239-
.expect("peerid has been stripped earlier")
240-
.with(*peer_id);
234+
let addr = MultiaddrWithPeerId::try_from(addr)
235+
.expect("dialed address contains peerid in libp2p 0.38");
241236

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

337332
for addr in failed {
338-
let addr = MultiaddrWithoutPeerId::try_from(addr)
339-
.expect("peerid has been stripped earlier")
340-
.with(*peer_id);
333+
let addr = MultiaddrWithPeerId::try_from(addr)
334+
.expect("dialed address contains peerid in libp2p 0.38");
341335

342336
self.connect_registry
343337
.finish_subscription(addr.into(), Err("disconnected".into()));
@@ -361,9 +355,8 @@ impl NetworkBehaviour for SwarmApi {
361355
// this should not be executed once, but probably will be in case unsupported addresses or something
362356
// surprising happens.
363357
for failed in self.pending_connections.remove(peer_id).unwrap_or_default() {
364-
let addr = MultiaddrWithoutPeerId::try_from(failed)
365-
.expect("peerid has been stripped earlier")
366-
.with(*peer_id);
358+
let addr = MultiaddrWithPeerId::try_from(failed)
359+
.expect("dialed address contains peerid in libp2p 0.38");
367360

368361
self.connect_registry
369362
.finish_subscription(addr.into(), Err("addresses exhausted".into()));
@@ -382,7 +375,7 @@ impl NetworkBehaviour for SwarmApi {
382375
match self.pending_connections.entry(*peer_id) {
383376
Entry::Occupied(mut oe) => {
384377
let addresses = oe.get_mut();
385-
let pos = addresses.iter().position(|a| eq_greedy(a, addr));
378+
let pos = addresses.iter().position(|a| a == addr);
386379

387380
if let Some(pos) = pos {
388381
addresses.swap_remove(pos);

unixfs/src/dir/builder/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl PostOrderIterator {
187187
};
188188

189189
self.pending.push(Visited::PostRoot { leaves });
190-
self.pending.extend(children.drain(..));
190+
self.pending.append(children);
191191
}
192192
Visited::Descent {
193193
node,
@@ -215,7 +215,7 @@ impl PostOrderIterator {
215215
index,
216216
});
217217

218-
self.pending.extend(children.drain(..));
218+
self.pending.append(children);
219219
}
220220
Visited::Post {
221221
parent_id,

0 commit comments

Comments
 (0)