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 4 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
21 changes: 21 additions & 0 deletions packages/interface-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -94,4 +105,14 @@ 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, AbortOptions>

/**
* Returns true if the peer id is in the pending dials
*/
isPendingDial: (peer: PeerId | Multiaddr) => boolean
maschad marked this conversation as resolved.
Show resolved Hide resolved
}
45 changes: 23 additions & 22 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MockNetwork {
export const mockNetwork = new MockNetwork()

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

Expand All @@ -51,6 +51,10 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
this.components = components
}

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

isStarted (): boolean {
return this.started
}
Expand All @@ -60,20 +64,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 @@ -96,8 +107,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.components.connectionManager.safeDispatchEvent<Connection>('peer:connect', {
detail: aToB
Expand Down Expand Up @@ -133,21 +144,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