Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit e46b72b

Browse files
authored
feat: support batch dialling (#351)
See libp2p/js-libp2p#1616 for more details
1 parent 9db85bf commit e46b72b

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

packages/interface-connection-manager/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
5858
* const connection = await libp2p.connectionManager.openConnection(peerId)
5959
* ```
6060
*/
61-
openConnection: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
61+
openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>
6262

6363
/**
6464
* Close our connections to a peer
@@ -81,9 +81,9 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
8181

8282
export interface Dialer {
8383
/**
84-
* Dial a peer or multiaddr and return the promise of a connection
84+
* Dial a peer or multiaddr, or multiple multiaddrs and return the promise of a connection
8585
*/
86-
dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
86+
dial: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>
8787

8888
/**
8989
* Request `num` dial tokens. Only the returned number of dials may be attempted.

packages/interface-libp2p/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
348348
* await conn.close()
349349
* ```
350350
*/
351-
dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
351+
dial: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>
352352

353353
/**
354354
* Dials to the provided peer and tries to handshake with the given protocols in order.
@@ -366,7 +366,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
366366
* pipe([1, 2, 3], stream, consume)
367367
* ```
368368
*/
369-
dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<Stream>
369+
dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: AbortOptions) => Promise<Stream>
370370

371371
/**
372372
* Attempts to gracefully close an open connection to the given peer. If the connection is not closed in the grace period, it will be forcefully closed.

packages/interface-mocks/src/connection-manager.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { EventEmitter } from '@libp2p/interfaces/events'
22
import type { Startable } from '@libp2p/interfaces/startable'
33
import type { Connection } from '@libp2p/interface-connection'
4-
import type { PeerId } from '@libp2p/interface-peer-id'
4+
import { isPeerId, PeerId } from '@libp2p/interface-peer-id'
55
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interface-connection-manager'
66
import { connectionPair } from './connection.js'
77
import { CodeError } from '@libp2p/interfaces/errors'
88
import type { Registrar } from '@libp2p/interface-registrar'
99
import type { PubSub } from '@libp2p/interface-pubsub'
1010
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'
11+
import { peerIdFromString } from '@libp2p/peer-id'
1112

1213
export interface MockNetworkComponents {
1314
peerId: PeerId
@@ -23,10 +24,14 @@ class MockNetwork {
2324
this.components.push(components)
2425
}
2526

26-
getNode (peerId: PeerId): MockNetworkComponents {
27-
for (const components of this.components) {
28-
if (peerId.equals(components.peerId)) {
29-
return components
27+
getNode (peerId: PeerId | Multiaddr []): MockNetworkComponents {
28+
if (Array.isArray(peerId) && peerId.length > 0) {
29+
peerId = peerIdFromString(peerId[0].getPeerId() ?? '')
30+
} else if (isPeerId(peerId)) {
31+
for (const components of this.components) {
32+
if (peerId.equals(components.peerId)) {
33+
return components
34+
}
3035
}
3136
}
3237

@@ -81,7 +86,7 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
8186
return this.connections
8287
}
8388

84-
async openConnection (peerId: PeerId | Multiaddr): Promise<Connection> {
89+
async openConnection (peerId: PeerId | Multiaddr | Multiaddr[]): Promise<Connection> {
8590
if (this.components == null) {
8691
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED')
8792
}
@@ -90,7 +95,13 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
9095
throw new CodeError('Dialing multiaddrs not supported', 'ERR_NOT_SUPPORTED')
9196
}
9297

93-
const existingConnections = this.getConnections(peerId)
98+
let existingConnections: Connection[] = []
99+
100+
if (Array.isArray(peerId) && peerId.length > 0) {
101+
existingConnections = this.getConnections(peerIdFromString(peerId[0].getPeerId() ?? ''))
102+
} else if (isPeerId(peerId)) {
103+
existingConnections = this.getConnections(peerId)
104+
}
94105

95106
if (existingConnections.length > 0) {
96107
return existingConnections[0]

0 commit comments

Comments
 (0)