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

Commit 3b6f695

Browse files
committed
refactor(swarm): pending_{addresses, connections} use MultiaddrWithPeerId
Enforce internal represenatation for `SwarmApi::pending_{addresses, connections}` to explicitly show that all multiaddrs include peer IDs by using `MultiaddrWithPeerId`.
1 parent 136496f commit 3b6f695

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/p2p/swarm.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -116,7 +116,7 @@ impl SwarmApi {
116116
self.pending_addresses
117117
.entry(addr.peer_id)
118118
.or_insert_with(|| Vec::with_capacity(1))
119-
.push(addr.into());
119+
.push(addr);
120120

121121
Some(subscription)
122122
}
@@ -159,7 +159,7 @@ impl NetworkBehaviour for SwarmApi {
159159
.or_default()
160160
.extend(addresses.iter().cloned());
161161

162-
addresses
162+
addresses.into_iter().map(|a| a.into()).collect()
163163
}
164164

165165
fn inject_connection_established(
@@ -190,18 +190,19 @@ impl NetworkBehaviour for SwarmApi {
190190
match self.pending_connections.entry(*peer_id) {
191191
Entry::Occupied(mut oe) => {
192192
let addresses = oe.get_mut();
193-
let just_connected = addresses.iter().position(|a| a == 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);
194198
if let Some(just_connected) = just_connected {
195199
addresses.swap_remove(just_connected);
196200
if addresses.is_empty() {
197201
oe.remove();
198202
}
199203

200-
let addr = MultiaddrWithPeerId::try_from(address.clone())
201-
.expect("dialed address contains peerid in libp2p 0.38");
202-
203204
self.connect_registry
204-
.finish_subscription(addr.into(), Ok(()));
205+
.finish_subscription(address.into(), Ok(()));
205206
}
206207
}
207208
Entry::Vacant(_) => {
@@ -231,9 +232,6 @@ impl NetworkBehaviour for SwarmApi {
231232
);
232233

233234
for addr in all_subs {
234-
let addr = MultiaddrWithPeerId::try_from(addr)
235-
.expect("dialed address contains peerid in libp2p 0.38");
236-
237235
// fail the other than already connected subscriptions in
238236
// inject_connection_established. while the whole swarmapi is quite unclear on the
239237
// actual use cases, assume that connecting one is good enough for all outstanding
@@ -285,7 +283,7 @@ impl NetworkBehaviour for SwarmApi {
285283
match self.pending_connections.entry(*peer_id) {
286284
Entry::Occupied(mut oe) => {
287285
let connections = oe.get_mut();
288-
let pos = connections.iter().position(|x| addr.multiaddr == *x);
286+
let pos = connections.iter().position(|x| addr == *x);
289287

290288
if let Some(pos) = pos {
291289
connections.swap_remove(pos);
@@ -330,9 +328,6 @@ impl NetworkBehaviour for SwarmApi {
330328
);
331329

332330
for addr in failed {
333-
let addr = MultiaddrWithPeerId::try_from(addr)
334-
.expect("dialed address contains peerid in libp2p 0.38");
335-
336331
self.connect_registry
337332
.finish_subscription(addr.into(), Err("disconnected".into()));
338333
}
@@ -355,11 +350,8 @@ impl NetworkBehaviour for SwarmApi {
355350
// this should not be executed once, but probably will be in case unsupported addresses or something
356351
// surprising happens.
357352
for failed in self.pending_connections.remove(peer_id).unwrap_or_default() {
358-
let addr = MultiaddrWithPeerId::try_from(failed)
359-
.expect("dialed address contains peerid in libp2p 0.38");
360-
361353
self.connect_registry
362-
.finish_subscription(addr.into(), Err("addresses exhausted".into()));
354+
.finish_subscription(failed.into(), Err("addresses exhausted".into()));
363355
}
364356
}
365357

@@ -375,12 +367,12 @@ impl NetworkBehaviour for SwarmApi {
375367
match self.pending_connections.entry(*peer_id) {
376368
Entry::Occupied(mut oe) => {
377369
let addresses = oe.get_mut();
378-
let pos = addresses.iter().position(|a| 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);
379373

380374
if let Some(pos) = pos {
381375
addresses.swap_remove(pos);
382-
let addr = MultiaddrWithPeerId::try_from(addr.clone())
383-
.expect("dialed address contains peerid in libp2p 0.38");
384376
self.connect_registry
385377
.finish_subscription(addr.into(), Err(error.to_string()));
386378
}

0 commit comments

Comments
 (0)