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

feat: Update Connection Manager and Dialler Interfaces and Configs. #342

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/interface-connection-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@
},
"dependencies": {
"@libp2p/interface-connection": "^3.0.0",
"@libp2p/interface-connection-gater": "^1.0.0",
"@libp2p/interface-metrics": "^4.0.5",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-transport": "^2.1.1",
"@libp2p/interfaces": "^3.0.0",
"@multiformats/multiaddr": "^11.0.0"
},
Expand Down
50 changes: 50 additions & 0 deletions packages/interface-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import type { AbortOptions } from '@libp2p/interfaces'
import type { EventEmitter } from '@libp2p/interfaces/events'
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Metrics } from '@libp2p/interface-metrics'
import type { PeerStore } from '@libp2p/interface-peer-store'
import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
export interface ConnectionManagerComponents {
peerId: PeerId
metrics?: Metrics
upgrader: Upgrader
peerStore: PeerStore
dialer: Dialer
}

export interface ConnectionManagerEvents {
/**
Expand Down Expand Up @@ -49,6 +60,17 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
*/
getConnections: (peerId?: PeerId) => Connection[]

/**
* Return a map of all connections with their associated PeerIds
*
* @example
*
* ```js
* const connectionsMap = libp2p.connectionManager.getConnectionsMap()
* ```
*/
getConnectionsMap: () => Map<string, Connection[]>

/**
* Open a connection to a remote peer
*
Expand Down Expand Up @@ -77,6 +99,19 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
* Invoked after upgrading a multiaddr connection has finished
*/
afterUpgradeInbound: () => void

/**
* Return the components of the connection manager
*/
getComponents: () => ConnectionManagerComponents
}

export interface DialerComponents {
peerId: PeerId
metrics?: Metrics
peerStore: PeerStore
transportManager: TransportManager
connectionGater: ConnectionGater
}

export interface Dialer {
Expand All @@ -94,4 +129,19 @@ export interface Dialer {
* After a dial attempt succeeds or fails, return the passed token to the pool
*/
releaseToken: (token: number) => void

/**
* Get the current dial targets which are pending
*/
getPendingDialTargets: () => Map<string, AbortController>

/**
* Returns true if the peer id is in the pending dials
*/
hasPendingDial: (peer: PeerId | Multiaddr) => boolean

/**
* Return the components of the dialer
*/
getComponents: () => DialerComponents
}
1 change: 1 addition & 0 deletions packages/interface-mocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"@libp2p/interface-peer-discovery": "^1.0.0",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-info": "^1.0.0",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-pubsub": "^3.0.0",
"@libp2p/interface-registrar": "^2.0.0",
"@libp2p/interface-stream-muxer": "^3.0.0",
Expand Down
68 changes: 44 additions & 24 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ 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 type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interface-connection-manager'
import type { ConnectionManager, ConnectionManagerEvents, Dialer } 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 type { Metrics } from '@libp2p/interface-metrics'
import type { PeerStore } from '@libp2p/interface-peer-store'
import type { Upgrader } from '@libp2p/interface-transport'

export interface MockNetworkComponents {
peerId: PeerId
registrar: Registrar
connectionManager: ConnectionManager
pubsub?: PubSub
metrics?: Metrics
upgrader: Upgrader
peerStore: PeerStore
dialer: Dialer
}

export interface MockConnectionManagerConfig {
maxConnections: number
minConnections: number
}

class MockNetwork {
Expand Down Expand Up @@ -42,20 +54,31 @@ export const mockNetwork = new MockNetwork()

export interface MockConnectionManagerComponents {
peerId: PeerId
metrics?: Metrics
upgrader: Upgrader
peerStore: PeerStore
dialer: Dialer
registrar: Registrar
}

class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
private connections: Connection[] = []
private readonly connections: Map<string, Connection[]> = new Map()
private readonly components: MockConnectionManagerComponents
private started = false

constructor (components: MockConnectionManagerComponents) {
super()

this.components = components
}

getComponents (): MockConnectionManagerComponents {
return this.components
}

getConnectionsMap (): Map<string, Connection[]> {
return this.connections
}

isStarted (): boolean {
return this.started
}
Expand All @@ -65,20 +88,27 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
}

async stop (): Promise<void> {
for (const connection of this.connections) {
await this.closeConnections(connection.remotePeer)
for (const connectionList of this.connections.values()) {
for (const connection of connectionList) {
await connection.close()
}
}

this.started = false
}

getConnections (peerId?: PeerId): Connection[] {
if (peerId != null) {
return this.connections
.filter(c => c.remotePeer.toString() === peerId.toString())
return this.connections.get(peerId.toString()) ?? []
}

return this.connections
let conns: Connection[] = []

for (const c of this.connections.values()) {
conns = conns.concat(c)
}

return conns
}

async openConnection (peerId: PeerId | Multiaddr): Promise<Connection> {
Expand All @@ -101,8 +131,8 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
const [aToB, bToA] = connectionPair(this.components, componentsB)

// track connections
this.connections.push(aToB)
;(componentsB.connectionManager as MockConnectionManager).connections.push(bToA)
this.connections.set(peerId.toString(), [aToB])
this.connections.set(componentsB.peerId.toString(), [bToA])

this.safeDispatchEvent<Connection>('peer:connect', {
detail: aToB
Expand Down Expand Up @@ -138,21 +168,11 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
return
}

const componentsB = mockNetwork.getNode(peerId)

for (const protocol of this.components.registrar.getProtocols()) {
this.components.registrar.getTopologies(protocol).forEach(topology => {
topology.onDisconnect(componentsB.peerId)
await Promise.all(
connections.map(async connection => {
await connection.close()
})
}

for (const conn of connections) {
await conn.close()
}

this.connections = this.connections.filter(c => c.remotePeer.equals(peerId))

await componentsB.connectionManager?.closeConnections(this.components.peerId)
)
}

async acceptIncomingConnection (): Promise<boolean> {
Expand Down
1 change: 1 addition & 0 deletions packages/interface-pubsub-compliance-tests/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function createComponents (): Promise<MockNetworkComponents> {
peerId: await createEd25519PeerId(),
registrar: mockRegistrar()
}

components.connectionManager = mockConnectionManager(components)

mockNetwork.addNode(components)
Expand Down