@@ -49,11 +49,11 @@ pub struct SwarmApi {
49
49
50
50
/// The connections which have been requested, but the swarm/network is yet to ask for
51
51
/// 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 > > ,
53
53
54
54
/// The connections which have been requested, and the swarm/network has requested the
55
55
/// addresses of. Used to keep finishing all of the subscriptions.
56
- pending_connections : HashMap < PeerId , Vec < Multiaddr > > ,
56
+ pending_connections : HashMap < PeerId , Vec < MultiaddrWithPeerId > > ,
57
57
58
58
pub ( crate ) bootstrappers : HashSet < MultiaddrWithPeerId > ,
59
59
}
@@ -116,7 +116,7 @@ impl SwarmApi {
116
116
self . pending_addresses
117
117
. entry ( addr. peer_id )
118
118
. or_insert_with ( || Vec :: with_capacity ( 1 ) )
119
- . push ( addr. into ( ) ) ;
119
+ . push ( addr) ;
120
120
121
121
Some ( subscription)
122
122
}
@@ -159,7 +159,7 @@ impl NetworkBehaviour for SwarmApi {
159
159
. or_default ( )
160
160
. extend ( addresses. iter ( ) . cloned ( ) ) ;
161
161
162
- addresses
162
+ addresses. into_iter ( ) . map ( |a| a . into ( ) ) . collect ( )
163
163
}
164
164
165
165
fn inject_connection_established (
@@ -190,18 +190,19 @@ impl NetworkBehaviour for SwarmApi {
190
190
match self . pending_connections . entry ( * peer_id) {
191
191
Entry :: Occupied ( mut oe) => {
192
192
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) ;
194
198
if let Some ( just_connected) = just_connected {
195
199
addresses. swap_remove ( just_connected) ;
196
200
if addresses. is_empty ( ) {
197
201
oe. remove ( ) ;
198
202
}
199
203
200
- let addr = MultiaddrWithPeerId :: try_from ( address. clone ( ) )
201
- . expect ( "dialed address contains peerid in libp2p 0.38" ) ;
202
-
203
204
self . connect_registry
204
- . finish_subscription ( addr . into ( ) , Ok ( ( ) ) ) ;
205
+ . finish_subscription ( address . into ( ) , Ok ( ( ) ) ) ;
205
206
}
206
207
}
207
208
Entry :: Vacant ( _) => {
@@ -231,9 +232,6 @@ impl NetworkBehaviour for SwarmApi {
231
232
) ;
232
233
233
234
for addr in all_subs {
234
- let addr = MultiaddrWithPeerId :: try_from ( addr)
235
- . expect ( "dialed address contains peerid in libp2p 0.38" ) ;
236
-
237
235
// fail the other than already connected subscriptions in
238
236
// inject_connection_established. while the whole swarmapi is quite unclear on the
239
237
// actual use cases, assume that connecting one is good enough for all outstanding
@@ -285,7 +283,7 @@ impl NetworkBehaviour for SwarmApi {
285
283
match self . pending_connections . entry ( * peer_id) {
286
284
Entry :: Occupied ( mut oe) => {
287
285
let connections = oe. get_mut ( ) ;
288
- let pos = connections. iter ( ) . position ( |x| addr. multiaddr == * x) ;
286
+ let pos = connections. iter ( ) . position ( |x| addr == * x) ;
289
287
290
288
if let Some ( pos) = pos {
291
289
connections. swap_remove ( pos) ;
@@ -330,9 +328,6 @@ impl NetworkBehaviour for SwarmApi {
330
328
) ;
331
329
332
330
for addr in failed {
333
- let addr = MultiaddrWithPeerId :: try_from ( addr)
334
- . expect ( "dialed address contains peerid in libp2p 0.38" ) ;
335
-
336
331
self . connect_registry
337
332
. finish_subscription ( addr. into ( ) , Err ( "disconnected" . into ( ) ) ) ;
338
333
}
@@ -355,11 +350,8 @@ impl NetworkBehaviour for SwarmApi {
355
350
// this should not be executed once, but probably will be in case unsupported addresses or something
356
351
// surprising happens.
357
352
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
-
361
353
self . connect_registry
362
- . finish_subscription ( addr . into ( ) , Err ( "addresses exhausted" . into ( ) ) ) ;
354
+ . finish_subscription ( failed . into ( ) , Err ( "addresses exhausted" . into ( ) ) ) ;
363
355
}
364
356
}
365
357
@@ -375,12 +367,12 @@ impl NetworkBehaviour for SwarmApi {
375
367
match self . pending_connections . entry ( * peer_id) {
376
368
Entry :: Occupied ( mut oe) => {
377
369
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) ;
379
373
380
374
if let Some ( pos) = pos {
381
375
addresses. swap_remove ( pos) ;
382
- let addr = MultiaddrWithPeerId :: try_from ( addr. clone ( ) )
383
- . expect ( "dialed address contains peerid in libp2p 0.38" ) ;
384
376
self . connect_registry
385
377
. finish_subscription ( addr. into ( ) , Err ( error. to_string ( ) ) ) ;
386
378
}
0 commit comments