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

Commit

Permalink
feat: support batch dialling (#351)
Browse files Browse the repository at this point in the history
See libp2p/js-libp2p#1616 for more details
  • Loading branch information
maschad authored Apr 5, 2023
1 parent 9db85bf commit e46b72b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
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
25 changes: 18 additions & 7 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -81,7 +86,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 +95,13 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> 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]
Expand Down

0 comments on commit e46b72b

Please sign in to comment.