@@ -5,6 +5,7 @@ import { NotFoundError } from '@libp2p/interface'
55import { matchMultiaddr } from '@libp2p/interface-compliance-tests/matchers'
66import { mockConnection , mockDuplex , mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks'
77import { peerLogger } from '@libp2p/logger'
8+ import { PeerMap } from '@libp2p/peer-collections'
89import { peerIdFromPrivateKey } from '@libp2p/peer-id'
910import { multiaddr , resolvers } from '@multiformats/multiaddr'
1011import { WebRTC } from '@multiformats/multiaddr-matcher'
@@ -325,4 +326,74 @@ describe('dial queue', () => {
325326 dialer = new DialQueue ( components )
326327 await expect ( dialer . dial ( remotePeer ) ) . to . eventually . equal ( connection )
327328 } )
329+
330+ it ( 'should return existing connection when dialing a multiaddr without a peer id' , async ( ) => {
331+ const remotePeer = peerIdFromPrivateKey ( await generateKeyPair ( 'Ed25519' ) )
332+ const ip = multiaddr ( '/ip4/123.123.123.123' )
333+ const addr1 = ip . encapsulate ( '/tcp/123' )
334+ const addr2 = ip . encapsulate ( '/tcp/321' )
335+
336+ const existingConnection = stubInterface < Connection > ( {
337+ limits : {
338+ bytes : 100n
339+ } ,
340+ remotePeer,
341+ remoteAddr : addr1 . encapsulate ( `/p2p/${ remotePeer } ` ) ,
342+ status : 'open'
343+ } )
344+
345+ const newConnection = stubInterface < Connection > ( {
346+ limits : {
347+ bytes : 100n
348+ } ,
349+ remotePeer,
350+ remoteAddr : addr2 . encapsulate ( `/p2p/${ remotePeer } ` ) ,
351+ status : 'open'
352+ } )
353+
354+ const connections = new PeerMap < Connection [ ] > ( )
355+ connections . set ( remotePeer , [ existingConnection ] )
356+
357+ components . transportManager . dialTransportForMultiaddr . callsFake ( ma => {
358+ return stubInterface < Transport > ( )
359+ } )
360+ components . transportManager . dial . callsFake ( async ( ma , opts = { } ) => newConnection )
361+ dialer = new DialQueue ( components , { connections } )
362+
363+ await expect ( dialer . dial ( addr2 ) ) . to . eventually . equal ( existingConnection )
364+ } )
365+
366+ it ( 'should return new connection when existing connection to same peer is worse' , async ( ) => {
367+ const remotePeer = peerIdFromPrivateKey ( await generateKeyPair ( 'Ed25519' ) )
368+ const ip = multiaddr ( '/ip4/123.123.123.123' )
369+ const addr1 = ip . encapsulate ( '/tcp/123' )
370+ const addr2 = ip . encapsulate ( '/tcp/321' )
371+
372+ const existingConnection = stubInterface < Connection > ( {
373+ limits : {
374+ bytes : 100n
375+ } ,
376+ remotePeer,
377+ remoteAddr : addr1 . encapsulate ( `/p2p/${ remotePeer } ` ) ,
378+ status : 'open'
379+ } )
380+
381+ const newConnection = stubInterface < Connection > ( {
382+ limits : undefined ,
383+ remotePeer,
384+ remoteAddr : addr2 . encapsulate ( `/p2p/${ remotePeer } ` ) ,
385+ status : 'open'
386+ } )
387+
388+ const connections = new PeerMap < Connection [ ] > ( )
389+ connections . set ( remotePeer , [ existingConnection ] )
390+
391+ components . transportManager . dialTransportForMultiaddr . callsFake ( ma => {
392+ return stubInterface < Transport > ( )
393+ } )
394+ components . transportManager . dial . callsFake ( async ( ma , opts = { } ) => newConnection )
395+ dialer = new DialQueue ( components , { connections } )
396+
397+ await expect ( dialer . dial ( addr2 ) ) . to . eventually . equal ( newConnection )
398+ } )
328399} )
0 commit comments