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

feat: support batch dialling #351

Merged
6 changes: 3 additions & 3 deletions packages/interface-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
* const connection = await libp2p.connectionManager.openConnection(peerId)
* ```
*/
openConnection: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>

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

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

/**
* Request `num` dial tokens. Only the returned number of dials may be attempted.
Expand Down
4 changes: 2 additions & 2 deletions packages/interface-libp2p/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
* await conn.close()
* ```
*/
dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
dial: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>

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

/**
* 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.
Expand Down
19 changes: 16 additions & 3 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CodeError } from '@libp2p/interfaces/errors'
import type { Registrar } from '@libp2p/interface-registrar'
import type { PubSub } from '@libp2p/interface-pubsub'
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'

export interface MockNetworkComponents {
peerId: PeerId
Expand All @@ -23,7 +24,13 @@ class MockNetwork {
this.components.push(components)
}

getNode (peerId: PeerId): MockNetworkComponents {
getNode (peerId: PeerId | Multiaddr []): MockNetworkComponents {
if (Array.isArray(peerId) && peerId.length > 0) {
peerId = peerIdFromString(peerId[0].getPeerId() ?? '')
maschad marked this conversation as resolved.
Show resolved Hide resolved
} else {
peerId = peerId as PeerId
maschad marked this conversation as resolved.
Show resolved Hide resolved
}

for (const components of this.components) {
if (peerId.equals(components.peerId)) {
return components
Expand Down Expand Up @@ -81,7 +88,7 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
return this.connections
}

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

const existingConnections = this.getConnections(peerId)
let existingConnections

if (Array.isArray(peerId)) {
existingConnections = this.getConnections(peerIdFromString(peerId[0].getPeerId() ?? ''))
maschad marked this conversation as resolved.
Show resolved Hide resolved
} else {
existingConnections = this.getConnections(peerId)
}

if (existingConnections.length > 0) {
return existingConnections[0]
Expand Down