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

Commit 111f116

Browse files
bors[bot]CHr15F0x
andauthored
Merge #473
473: Swarm cleanup following libp2p upgrade to v0.39.1 r=koivunej a=CHr15F0x This is a follow up of #472. There are two changes introduced: * remove eq_greedy as `Multiaddr` in `SwarmApi::pending_{addresses, connections}` contains p2p, align conversions accordingly (136496f), * explicitly use `MultiaddrWithPeerId` in `SwarmApi::pending_{addresses, connections}` (3b6f695). The former (136496f) works fine without the latter (3b6f695), however I am leaning more towards keeping both changes. Encoding the information about peer id presence/absence in Multiaddr in _type_ instead of relying on comments and `expect()`s improves readability and _developer experience_. Please let me know what your thoughts are. Co-authored-by: Krzysztof Lis <klis33@gmail.com>
2 parents ede42eb + 05d420e commit 111f116

File tree

4 files changed

+22
-79
lines changed

4 files changed

+22
-79
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* fix: compilation error when used as a dependency [#470]
1212
* perf: use hash_hasher where the key is Cid [#467]
1313
* chore: upgrade to libp2p 0.39.1, update most of the other deps with the notable exception of cid and multihash [#472]
14+
* refactor(swarm): swarm cleanup following libp2p upgrade to v0.39.1 [#473]
1415

1516
[#429]: https://github.com/rs-ipfs/rust-ipfs/pull/429
1617
[#428]: https://github.com/rs-ipfs/rust-ipfs/pull/428
@@ -25,6 +26,7 @@
2526
[#470]: https://github.com/rs-ipfs/rust-ipfs/pull/470
2627
[#467]: https://github.com/rs-ipfs/rust-ipfs/pull/467
2728
[#472]: https://github.com/rs-ipfs/rust-ipfs/pull/472
29+
[#473]: https://github.com/rs-ipfs/rust-ipfs/pull/473
2830

2931
# 0.2.1
3032

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: 18 additions & 33 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};
@@ -49,11 +49,11 @@ pub struct SwarmApi {
4949

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

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

5858
pub(crate) bootstrappers: HashSet<MultiaddrWithPeerId>,
5959
}
@@ -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);
124120

125121
Some(subscription)
126122
}
@@ -163,7 +159,7 @@ impl NetworkBehaviour for SwarmApi {
163159
.or_default()
164160
.extend(addresses.iter().cloned());
165161

166-
addresses
162+
addresses.into_iter().map(|a| a.into()).collect()
167163
}
168164

169165
fn inject_connection_established(
@@ -194,18 +190,19 @@ 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 address: MultiaddrWithPeerId = address
194+
.clone()
195+
.try_into()
196+
.expect("dialed address contains peerid in libp2p 0.38");
197+
let just_connected = addresses.iter().position(|x| *x == address);
198198
if let Some(just_connected) = just_connected {
199199
addresses.swap_remove(just_connected);
200200
if addresses.is_empty() {
201201
oe.remove();
202202
}
203203

204-
let addr = MultiaddrWithPeerId::try_from(address.clone())
205-
.expect("dialed address contains peerid in libp2p 0.38");
206-
207204
self.connect_registry
208-
.finish_subscription(addr.into(), Ok(()));
205+
.finish_subscription(address.into(), Ok(()));
209206
}
210207
}
211208
Entry::Vacant(_) => {
@@ -235,10 +232,6 @@ impl NetworkBehaviour for SwarmApi {
235232
);
236233

237234
for addr in all_subs {
238-
let addr = MultiaddrWithoutPeerId::try_from(addr)
239-
.expect("peerid has been stripped earlier")
240-
.with(*peer_id);
241-
242235
// fail the other than already connected subscriptions in
243236
// inject_connection_established. while the whole swarmapi is quite unclear on the
244237
// actual use cases, assume that connecting one is good enough for all outstanding
@@ -290,7 +283,7 @@ impl NetworkBehaviour for SwarmApi {
290283
match self.pending_connections.entry(*peer_id) {
291284
Entry::Occupied(mut oe) => {
292285
let connections = oe.get_mut();
293-
let pos = connections.iter().position(|x| addr.multiaddr == *x);
286+
let pos = connections.iter().position(|x| addr == *x);
294287

295288
if let Some(pos) = pos {
296289
connections.swap_remove(pos);
@@ -335,10 +328,6 @@ impl NetworkBehaviour for SwarmApi {
335328
);
336329

337330
for addr in failed {
338-
let addr = MultiaddrWithoutPeerId::try_from(addr)
339-
.expect("peerid has been stripped earlier")
340-
.with(*peer_id);
341-
342331
self.connect_registry
343332
.finish_subscription(addr.into(), Err("disconnected".into()));
344333
}
@@ -361,12 +350,8 @@ impl NetworkBehaviour for SwarmApi {
361350
// this should not be executed once, but probably will be in case unsupported addresses or something
362351
// surprising happens.
363352
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);
367-
368353
self.connect_registry
369-
.finish_subscription(addr.into(), Err("addresses exhausted".into()));
354+
.finish_subscription(failed.into(), Err("addresses exhausted".into()));
370355
}
371356
}
372357

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

387374
if let Some(pos) = pos {
388375
addresses.swap_remove(pos);
389-
let addr = MultiaddrWithPeerId::try_from(addr.clone())
390-
.expect("dialed address contains peerid in libp2p 0.38");
391376
self.connect_registry
392377
.finish_subscription(addr.into(), Err(error.to_string()));
393378
}

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)