From e46b72b1731ff935a1f0d755cbaf6f3159060ed3 Mon Sep 17 00:00:00 2001 From: Chad Nehemiah Date: Wed, 5 Apr 2023 09:43:37 -0500 Subject: [PATCH] feat: support batch dialling (#351) See https://github.com/libp2p/js-libp2p/issues/1616 for more details --- .../interface-connection-manager/src/index.ts | 6 ++--- packages/interface-libp2p/src/index.ts | 4 +-- .../interface-mocks/src/connection-manager.ts | 25 +++++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/interface-connection-manager/src/index.ts b/packages/interface-connection-manager/src/index.ts index 50954988b..987fa617c 100644 --- a/packages/interface-connection-manager/src/index.ts +++ b/packages/interface-connection-manager/src/index.ts @@ -58,7 +58,7 @@ export interface ConnectionManager extends EventEmitter * const connection = await libp2p.connectionManager.openConnection(peerId) * ``` */ - openConnection: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise + openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise /** * Close our connections to a peer @@ -81,9 +81,9 @@ export interface ConnectionManager extends EventEmitter 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 + dial: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise /** * Request `num` dial tokens. Only the returned number of dials may be attempted. diff --git a/packages/interface-libp2p/src/index.ts b/packages/interface-libp2p/src/index.ts index 037dd0a33..9d6bffcc6 100644 --- a/packages/interface-libp2p/src/index.ts +++ b/packages/interface-libp2p/src/index.ts @@ -348,7 +348,7 @@ export interface Libp2p extends Startable, EventEmitter { * await conn.close() * ``` */ - dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise + dial: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise /** * 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 { * pipe([1, 2, 3], stream, consume) * ``` */ - dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise + dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: AbortOptions) => Promise /** * 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. diff --git a/packages/interface-mocks/src/connection-manager.ts b/packages/interface-mocks/src/connection-manager.ts index 1f30c5227..6165f7988 100644 --- a/packages/interface-mocks/src/connection-manager.ts +++ b/packages/interface-mocks/src/connection-manager.ts @@ -1,13 +1,14 @@ import { EventEmitter } from '@libp2p/interfaces/events' import type { Startable } from '@libp2p/interfaces/startable' import type { Connection } from '@libp2p/interface-connection' -import type { PeerId } from '@libp2p/interface-peer-id' +import { isPeerId, PeerId } from '@libp2p/interface-peer-id' import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interface-connection-manager' import { connectionPair } from './connection.js' 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 @@ -23,10 +24,14 @@ class MockNetwork { this.components.push(components) } - getNode (peerId: PeerId): MockNetworkComponents { - for (const components of this.components) { - if (peerId.equals(components.peerId)) { - return components + getNode (peerId: PeerId | Multiaddr []): MockNetworkComponents { + if (Array.isArray(peerId) && peerId.length > 0) { + peerId = peerIdFromString(peerId[0].getPeerId() ?? '') + } else if (isPeerId(peerId)) { + for (const components of this.components) { + if (peerId.equals(components.peerId)) { + return components + } } } @@ -81,7 +86,7 @@ class MockConnectionManager extends EventEmitter implem return this.connections } - async openConnection (peerId: PeerId | Multiaddr): Promise { + async openConnection (peerId: PeerId | Multiaddr | Multiaddr[]): Promise { if (this.components == null) { throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED') } @@ -90,7 +95,13 @@ class MockConnectionManager extends EventEmitter implem throw new CodeError('Dialing multiaddrs not supported', 'ERR_NOT_SUPPORTED') } - const existingConnections = this.getConnections(peerId) + let existingConnections: Connection[] = [] + + if (Array.isArray(peerId) && peerId.length > 0) { + existingConnections = this.getConnections(peerIdFromString(peerId[0].getPeerId() ?? '')) + } else if (isPeerId(peerId)) { + existingConnections = this.getConnections(peerId) + } if (existingConnections.length > 0) { return existingConnections[0]