From a35d6fc201d918898426267a0bdcb8130e90e711 Mon Sep 17 00:00:00 2001 From: Sacha Froment Date: Wed, 25 Dec 2024 19:40:44 +0100 Subject: [PATCH] fix: start listener before AfterStart Signed-off-by: Sacha Froment --- packages/libp2p/src/transport-manager.ts | 8 ++-- packages/transport-websockets/test/node.ts | 46 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/libp2p/src/transport-manager.ts b/packages/libp2p/src/transport-manager.ts index ed48e8fdd7..f577f9d3cd 100644 --- a/packages/libp2p/src/transport-manager.ts +++ b/packages/libp2p/src/transport-manager.ts @@ -67,17 +67,17 @@ export class DefaultTransportManager implements TransportManager, Startable { return this.started } - start (): void { + async start (): Promise { this.started = true - } - - async afterStart (): Promise { // Listen on the provided transports for the provided addresses const addrs = this.components.addressManager.getListenAddrs() await this.listen(addrs) } + async afterStart (): Promise { + } + /** * Stops all listeners */ diff --git a/packages/transport-websockets/test/node.ts b/packages/transport-websockets/test/node.ts index 6baae5a67d..d49524023a 100644 --- a/packages/transport-websockets/test/node.ts +++ b/packages/transport-websockets/test/node.ts @@ -9,13 +9,15 @@ import { multiaddr } from '@multiformats/multiaddr' import { WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher' import { expect } from 'aegir/chai' import { isLoopbackAddr } from 'is-loopback-addr' +import { createLibp2p } from 'libp2p' import { pEvent } from 'p-event' import pWaitFor from 'p-wait-for' import Sinon from 'sinon' import { stubInterface } from 'sinon-ts' import * as filters from '../src/filters.js' import { webSockets } from '../src/index.js' -import type { Connection, Libp2pEvents, Listener, Transport, Upgrader, TLSCertificate } from '@libp2p/interface' +import type { Connection, Libp2pEvents, Listener, Transport, Upgrader, TLSCertificate, PeerDiscoveryEvents, PeerDiscovery, Startable } from '@libp2p/interface' +import type { AddressManager } from '@libp2p/interface-internal' import type { StubbedInstance } from 'sinon-ts' describe('instantiate the transport', () => { @@ -746,3 +748,45 @@ describe('auto-tls (IPv6)', () => { expect(wsOptions.port).to.equal(wssOptions.port) }) }) + +class TestPeerDiscovery extends TypedEventEmitter implements PeerDiscovery, Startable { + private readonly addressManager: AddressManager + + constructor (components: { + addressManager: AddressManager + }) { + super() + this.addressManager = components.addressManager + } + + async start (): Promise { + } + + async stop (): Promise { } + + afterStart (): void { + this.addressManager.getAddresses() + } + + readonly [Symbol.toStringTag] = '@libp2p/test-peer-discovery' +} + +describe('discovery-websockets', () => { + it('should discover peers over websockets', async () => { + const libp2p = await createLibp2p({ + addresses: { + listen: [ + '/ip4/0.0.0.0/tcp/0/ws' + ] + }, + peerDiscovery: [(component: { + addressManager: AddressManager + }) => new TestPeerDiscovery(component)], + transports: [ + webSockets() + ] + }) + + await libp2p.start() + }) +})