fix: emit peer:connect after all (#1171)

**Motivation**

In lodestar, when we handle "peer:connect" event, we dial the peer which gives another "peer:connect" event and it causes other issues

**Motivation**

In `onConnect` function, "peer:connect" event should be emitted after we add connection to the `connections` map so that when app dial the peer in "peer:connect" event handler, it uses the same/existing connection
This commit is contained in:
tuyennhv 2022-04-23 02:56:47 +07:00 committed by GitHub
parent fab4f1385c
commit d16817ca44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 12 deletions

View File

@ -135,6 +135,8 @@
"it-stream-types": "^1.0.4", "it-stream-types": "^1.0.4",
"it-take": "^1.0.2", "it-take": "^1.0.2",
"it-to-buffer": "^2.0.2", "it-to-buffer": "^2.0.2",
"@libp2p/tracked-map": "^1.0.4",
"it-pair": "^2.0.2",
"merge-options": "^3.0.4", "merge-options": "^3.0.4",
"mortice": "^3.0.0", "mortice": "^3.0.0",
"multiformats": "^9.6.3", "multiformats": "^9.6.3",

View File

@ -267,8 +267,6 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
const peerIdStr = peerId.toString() const peerIdStr = peerId.toString()
const storedConns = this.connections.get(peerIdStr) const storedConns = this.connections.get(peerIdStr)
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
if (storedConns != null) { if (storedConns != null) {
storedConns.push(connection) storedConns.push(connection)
} else { } else {
@ -284,6 +282,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
} }
await this._checkMaxLimit('maxConnections', this.getConnectionList().length) await this._checkMaxLimit('maxConnections', this.getConnectionList().length)
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
} }
/** /**

View File

@ -29,6 +29,7 @@ import { createFromJSON } from '@libp2p/peer-id-factory'
import Peers from '../fixtures/peers.js' import Peers from '../fixtures/peers.js'
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js' import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
import type { PeerId } from '@libp2p/interfaces/peer-id' import type { PeerId } from '@libp2p/interfaces/peer-id'
import { pEvent } from 'p-event'
const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999') const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999')
@ -430,21 +431,15 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
const identifySpy = sinon.spy(libp2p.identifyService, 'identify') const identifySpy = sinon.spy(libp2p.identifyService, 'identify')
const protobookSetSpy = sinon.spy(libp2p.components.getPeerStore().protoBook, 'set') const protobookSetSpy = sinon.spy(libp2p.components.getPeerStore().protoBook, 'set')
const connectionPromise = pDefer() const connectionPromise = pEvent(libp2p.connectionManager, 'peer:connect')
await libp2p.start() await libp2p.start()
libp2p.components.getUpgrader().addEventListener('connection', () => {
connectionPromise.resolve()
}, {
once: true
})
const connection = await libp2p.dial(MULTIADDRS_WEBSOCKETS[0]) const connection = await libp2p.dial(MULTIADDRS_WEBSOCKETS[0])
expect(connection).to.exist() expect(connection).to.exist()
// Wait for connection event to be emitted // Wait for connection event to be emitted
await connectionPromise.promise await connectionPromise
expect(identifySpy.callCount).to.equal(1) expect(identifySpy.callCount).to.equal(1)
await identifySpy.firstCall.returnValue await identifySpy.firstCall.returnValue

View File

@ -496,9 +496,12 @@ describe('Identify', () => {
const identityServiceIdentifySpy = sinon.spy(libp2p.identifyService, 'identify') const identityServiceIdentifySpy = sinon.spy(libp2p.identifyService, 'identify')
const identityServicePushSpy = sinon.spy(libp2p.identifyService, 'push') const identityServicePushSpy = sinon.spy(libp2p.identifyService, 'push')
const connectionPromise = pEvent(libp2p.connectionManager, 'peer:connect')
const connection = await libp2p.dial(remoteAddr) const connection = await libp2p.dial(remoteAddr)
expect(connection).to.exist() expect(connection).to.exist()
// Wait for connection event to be emitted
await connectionPromise
// Wait for identify to finish // Wait for identify to finish
await identityServiceIdentifySpy.firstCall.returnValue await identityServiceIdentifySpy.firstCall.returnValue
@ -560,9 +563,12 @@ describe('Identify', () => {
const identityServiceIdentifySpy = sinon.spy(libp2p.identifyService, 'identify') const identityServiceIdentifySpy = sinon.spy(libp2p.identifyService, 'identify')
const identityServicePushSpy = sinon.spy(libp2p.identifyService, 'push') const identityServicePushSpy = sinon.spy(libp2p.identifyService, 'push')
const connectionPromise = pEvent(libp2p.connectionManager, 'peer:connect')
const connection = await libp2p.dial(remoteAddr) const connection = await libp2p.dial(remoteAddr)
expect(connection).to.exist() expect(connection).to.exist()
// Wait for connection event to be emitted
await connectionPromise
// Wait for identify to finish // Wait for identify to finish
await identityServiceIdentifySpy.firstCall.returnValue await identityServiceIdentifySpy.firstCall.returnValue

View File

@ -26,6 +26,7 @@ import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/i
import type { Stream } from '@libp2p/interfaces/connection' import type { Stream } from '@libp2p/interfaces/connection'
import pDefer from 'p-defer' import pDefer from 'p-defer'
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js' import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
import { pEvent } from 'p-event'
const addrs = [ const addrs = [
new Multiaddr('/ip4/127.0.0.1/tcp/0'), new Multiaddr('/ip4/127.0.0.1/tcp/0'),
@ -495,10 +496,12 @@ describe('libp2p.upgrader', () => {
const connectionManagerDispatchEventSpy = sinon.spy(libp2p.components.getConnectionManager(), 'dispatchEvent') const connectionManagerDispatchEventSpy = sinon.spy(libp2p.components.getConnectionManager(), 'dispatchEvent')
// Upgrade and check the connect event // Upgrade and check the connect event
const connectionPromise = pEvent(libp2p.connectionManager, 'peer:connect')
const connections = await Promise.all([ const connections = await Promise.all([
libp2p.components.getUpgrader().upgradeOutbound(outbound), libp2p.components.getUpgrader().upgradeOutbound(outbound),
remoteLibp2p.components.getUpgrader().upgradeInbound(inbound) remoteLibp2p.components.getUpgrader().upgradeInbound(inbound)
]) ])
await connectionPromise
expect(connectionManagerDispatchEventSpy.callCount).to.equal(1) expect(connectionManagerDispatchEventSpy.callCount).to.equal(1)
let [event] = connectionManagerDispatchEventSpy.getCall(0).args let [event] = connectionManagerDispatchEventSpy.getCall(0).args