1
- use crate :: p2p:: { addr :: eq_greedy , MultiaddrWithPeerId , MultiaddrWithoutPeerId } ;
1
+ use crate :: p2p:: { MultiaddrWithPeerId , MultiaddrWithoutPeerId } ;
2
2
use crate :: subscription:: { SubscriptionFuture , SubscriptionRegistry } ;
3
3
use core:: task:: { Context , Poll } ;
4
4
use libp2p:: core:: { connection:: ConnectionId , ConnectedPoint , Multiaddr , PeerId } ;
@@ -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
}
@@ -106,21 +106,17 @@ impl SwarmApi {
106
106
. connect_registry
107
107
. create_subscription ( addr. clone ( ) . into ( ) , None ) ;
108
108
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
-
113
109
self . events . push_back ( NetworkBehaviourAction :: DialPeer {
114
- peer_id,
110
+ peer_id : addr . peer_id ,
115
111
// rationale: this is sort of explicit command, perhaps the old address is no longer
116
112
// valid. Always would be even better but it's bugged at the moment.
117
113
condition : DialPeerCondition :: NotDialing ,
118
114
} ) ;
119
115
120
116
self . pending_addresses
121
- . entry ( peer_id)
117
+ . entry ( addr . peer_id )
122
118
. or_insert_with ( || Vec :: with_capacity ( 1 ) )
123
- . push ( multiaddr . into ( ) ) ;
119
+ . push ( addr ) ;
124
120
125
121
Some ( subscription)
126
122
}
@@ -163,7 +159,7 @@ impl NetworkBehaviour for SwarmApi {
163
159
. or_default ( )
164
160
. extend ( addresses. iter ( ) . cloned ( ) ) ;
165
161
166
- addresses
162
+ addresses. into_iter ( ) . map ( |a| a . into ( ) ) . collect ( )
167
163
}
168
164
169
165
fn inject_connection_established (
@@ -194,18 +190,19 @@ impl NetworkBehaviour for SwarmApi {
194
190
match self . pending_connections . entry ( * peer_id) {
195
191
Entry :: Occupied ( mut oe) => {
196
192
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) ;
198
198
if let Some ( just_connected) = just_connected {
199
199
addresses. swap_remove ( just_connected) ;
200
200
if addresses. is_empty ( ) {
201
201
oe. remove ( ) ;
202
202
}
203
203
204
- let addr = MultiaddrWithPeerId :: try_from ( address. clone ( ) )
205
- . expect ( "dialed address contains peerid in libp2p 0.38" ) ;
206
-
207
204
self . connect_registry
208
- . finish_subscription ( addr . into ( ) , Ok ( ( ) ) ) ;
205
+ . finish_subscription ( address . into ( ) , Ok ( ( ) ) ) ;
209
206
}
210
207
}
211
208
Entry :: Vacant ( _) => {
@@ -235,10 +232,6 @@ impl NetworkBehaviour for SwarmApi {
235
232
) ;
236
233
237
234
for addr in all_subs {
238
- let addr = MultiaddrWithoutPeerId :: try_from ( addr)
239
- . expect ( "peerid has been stripped earlier" )
240
- . with ( * peer_id) ;
241
-
242
235
// fail the other than already connected subscriptions in
243
236
// inject_connection_established. while the whole swarmapi is quite unclear on the
244
237
// actual use cases, assume that connecting one is good enough for all outstanding
@@ -290,7 +283,7 @@ impl NetworkBehaviour for SwarmApi {
290
283
match self . pending_connections . entry ( * peer_id) {
291
284
Entry :: Occupied ( mut oe) => {
292
285
let connections = oe. get_mut ( ) ;
293
- let pos = connections. iter ( ) . position ( |x| addr. multiaddr == * x) ;
286
+ let pos = connections. iter ( ) . position ( |x| addr == * x) ;
294
287
295
288
if let Some ( pos) = pos {
296
289
connections. swap_remove ( pos) ;
@@ -335,10 +328,6 @@ impl NetworkBehaviour for SwarmApi {
335
328
) ;
336
329
337
330
for addr in failed {
338
- let addr = MultiaddrWithoutPeerId :: try_from ( addr)
339
- . expect ( "peerid has been stripped earlier" )
340
- . with ( * peer_id) ;
341
-
342
331
self . connect_registry
343
332
. finish_subscription ( addr. into ( ) , Err ( "disconnected" . into ( ) ) ) ;
344
333
}
@@ -361,12 +350,8 @@ impl NetworkBehaviour for SwarmApi {
361
350
// this should not be executed once, but probably will be in case unsupported addresses or something
362
351
// surprising happens.
363
352
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
-
368
353
self . connect_registry
369
- . finish_subscription ( addr . into ( ) , Err ( "addresses exhausted" . into ( ) ) ) ;
354
+ . finish_subscription ( failed . into ( ) , Err ( "addresses exhausted" . into ( ) ) ) ;
370
355
}
371
356
}
372
357
@@ -382,12 +367,12 @@ impl NetworkBehaviour for SwarmApi {
382
367
match self . pending_connections . entry ( * peer_id) {
383
368
Entry :: Occupied ( mut oe) => {
384
369
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) ;
386
373
387
374
if let Some ( pos) = pos {
388
375
addresses. swap_remove ( pos) ;
389
- let addr = MultiaddrWithPeerId :: try_from ( addr. clone ( ) )
390
- . expect ( "dialed address contains peerid in libp2p 0.38" ) ;
391
376
self . connect_registry
392
377
. finish_subscription ( addr. into ( ) , Err ( error. to_string ( ) ) ) ;
393
378
}
0 commit comments