From b278ee7c381500dfba649c34bbbcd7f355174aca Mon Sep 17 00:00:00 2001 From: Cayman Date: Thu, 1 Jun 2023 13:45:42 -0400 Subject: [PATCH 1/9] feat: enable manual identify --- src/identify/identify.ts | 19 ++++++++++++------- src/identify/index.ts | 5 +++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/identify/identify.ts b/src/identify/identify.ts index ad53d5f275..f890cd4350 100644 --- a/src/identify/identify.ts +++ b/src/identify/identify.ts @@ -50,7 +50,8 @@ const defaultValues = { maxPushIncomingStreams: 1, maxPushOutgoingStreams: 1, maxObservedAddresses: 10, - maxIdentifyMessageSize: 8192 + maxIdentifyMessageSize: 8192, + autodialOnConnectionOpen: true } export class DefaultIdentifyService implements Startable { @@ -101,11 +102,13 @@ export class DefaultIdentifyService implements Startable { agentVersion: init.agentVersion ?? defaultValues.agentVersion } - // When a new connection happens, trigger identify - components.events.addEventListener('connection:open', (evt) => { - const connection = evt.detail - this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) }) - }) + if (init.autodialOnConnectionOpen ?? defaultValues.autodialOnConnectionOpen) { + // When a new connection happens, trigger identify + components.events.addEventListener('connection:open', (evt) => { + const connection = evt.detail + this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) }) + }) + } // When self peer record changes, trigger identify-push components.events.addEventListener('self:peer:update', (evt) => { @@ -297,7 +300,7 @@ export class DefaultIdentifyService implements Startable { } } - async identify (connection: Connection, options: AbortOptions = {}): Promise { + async identify (connection: Connection, options: AbortOptions = {}): Promise { const message = await this._identify(connection, options) const { publicKey, @@ -345,6 +348,8 @@ export class DefaultIdentifyService implements Startable { } this.events.safeDispatchEvent('peer:identify', { detail: result }) + + return result } /** diff --git a/src/identify/index.ts b/src/identify/index.ts index 5f2403921b..19cf3b65de 100644 --- a/src/identify/index.ts +++ b/src/identify/index.ts @@ -39,6 +39,11 @@ export interface IdentifyServiceInit { maxPushIncomingStreams?: number maxPushOutgoingStreams?: number maxObservedAddresses?: number + + /** + * Whether to automatically dial identify on newly opened connections (default: true) + */ + autodialOnConnectionOpen?: boolean } export interface IdentifyServiceComponents { From 66672bb8301ba6985da473e9326503392aced971 Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 22 Jun 2023 11:45:56 -0500 Subject: [PATCH 2/9] feat: added manual identify --- packages/libp2p/src/identify/identify.ts | 8 +-- packages/libp2p/src/identify/index.ts | 21 +++++++- .../test/connection-manager/direct.spec.ts | 52 +++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/packages/libp2p/src/identify/identify.ts b/packages/libp2p/src/identify/identify.ts index 4ff7827364..633123361a 100644 --- a/packages/libp2p/src/identify/identify.ts +++ b/packages/libp2p/src/identify/identify.ts @@ -23,7 +23,7 @@ import { MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION } from './consts.js' import { Identify } from './pb/message.js' -import type { IdentifyServiceComponents, IdentifyServiceInit } from './index.js' +import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js' import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } from '@libp2p/interface' import type { Connection, Stream } from '@libp2p/interface/connection' import type { EventEmitter } from '@libp2p/interface/events' @@ -50,10 +50,10 @@ const defaultValues = { maxPushOutgoingStreams: 1, maxObservedAddresses: 10, maxIdentifyMessageSize: 8192, - autodialOnConnectionOpen: true + runOnConnectionOpen: true } -export class DefaultIdentifyService implements Startable { +export class DefaultIdentifyService implements Startable, IdentifyService { private readonly identifyProtocolStr: string private readonly identifyPushProtocolStr: string public readonly host: { @@ -101,7 +101,7 @@ export class DefaultIdentifyService implements Startable { agentVersion: init.agentVersion ?? defaultValues.agentVersion } - if (init.autodialOnConnectionOpen ?? defaultValues.autodialOnConnectionOpen) { + if (init.runOnConnectionOpen ?? defaultValues.runOnConnectionOpen) { // When a new connection happens, trigger identify components.events.addEventListener('connection:open', (evt) => { const connection = evt.detail diff --git a/packages/libp2p/src/identify/index.ts b/packages/libp2p/src/identify/index.ts index 3d67cfb4eb..8cf6756796 100644 --- a/packages/libp2p/src/identify/index.ts +++ b/packages/libp2p/src/identify/index.ts @@ -4,10 +4,11 @@ import { } from './consts.js' import { DefaultIdentifyService } from './identify.js' import { Identify } from './pb/message.js' -import type { Libp2pEvents } from '@libp2p/interface' +import type { AbortOptions, IdentifyResult, Libp2pEvents } from '@libp2p/interface' import type { EventEmitter } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore } from '@libp2p/interface/peer-store' +import type { Connection } from '@libp2p/interface/src/connection/index.js' import type { AddressManager } from '@libp2p/interface-internal/address-manager' import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' import type { Registrar } from '@libp2p/interface-internal/registrar' @@ -43,7 +44,7 @@ export interface IdentifyServiceInit { /** * Whether to automatically dial identify on newly opened connections (default: true) */ - autodialOnConnectionOpen?: boolean + runOnConnectionOpen?: boolean } export interface IdentifyServiceComponents { @@ -65,6 +66,22 @@ export const multicodecs = { export const Message = { Identify } +export interface IdentifyService { + /** + * due to the default limits on inbound/outbound streams for this protocol, + * invoking this method when runOnConnectionOpen is true can lead to unpredictable results + * as streams may be closed by the local or the remote node. + * Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol, + * you may be better off configuring a topology to be notified instead. + * + * @param connection + * @param options + * @returns + */ + identify: (connection: Connection, options: AbortOptions) => Promise + +} + export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService { return (components) => new DefaultIdentifyService(components, init) } diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 1bede4f907..0e8d26c238 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -33,6 +33,7 @@ import type { Connection } from '@libp2p/interface/connection' import type { PeerId } from '@libp2p/interface/peer-id' import type { TransportManager } from '@libp2p/interface-internal/transport-manager' import type { Multiaddr } from '@multiformats/multiaddr' +import { AGENT_VERSION } from '../../src/identify/consts.js' const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999') const relayMultiaddr = multiaddr(process.env.RELAY_MULTIADDR) @@ -402,6 +403,57 @@ describe('libp2p.dialer (direct, WebSockets)', () => { await libp2p.stop() }) + it('should not run identify automatically after connecting', async () => { + libp2p = await createLibp2p({ + peerId, + transports: [ + webSockets({ + filter: filters.all + }) + ], + streamMuxers: [ + yamux(), + ], + connectionEncryption: [ + plaintext() + ], + services: { + identify: identifyService({ + protocolPrefix: 'ipfs', + agentVersion: AGENT_VERSION, + timeout: 60000, + maxInboundStreams: 1, + maxOutboundStreams: 1, + maxPushIncomingStreams: 1, + maxPushOutgoingStreams: 1, + maxObservedAddresses: 10, + maxIdentifyMessageSize: 8192, + runOnConnectionOpen: false + }) + }, + connectionGater: mockConnectionGater() + }) + + if (libp2p.services.identify == null) { + throw new Error('Identify service missing') + } + + const identifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') + const connectionPromise = pEvent(libp2p, 'connection:open') + + await libp2p.start() + + const connection = await libp2p.dial(relayMultiaddr) + expect(connection).to.exist() + + // Wait for connection event to be emitted + await connectionPromise + + expect(identifySpy.callCount).to.equal(0) + + await libp2p.stop() + }) + it('should be able to use hangup to close connections', async () => { libp2p = await createLibp2p({ peerId, From 8b24d6a673fc4c8fe835657a75caf264a9d38006 Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 22 Jun 2023 12:09:05 -0500 Subject: [PATCH 3/9] chore: linting fixes --- .../test/connection-manager/direct.spec.ts | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 0e8d26c238..f6956c74de 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -22,6 +22,7 @@ import { stubInterface } from 'sinon-ts' import { defaultComponents, type Components } from '../../src/components.js' import { DefaultConnectionManager } from '../../src/connection-manager/index.js' import { codes as ErrorCodes } from '../../src/errors.js' +import { AGENT_VERSION } from '../../src/identify/consts.js' import { identifyService } from '../../src/identify/index.js' import { createLibp2p } from '../../src/index.js' import { plaintext } from '../../src/insecure/index.js' @@ -33,7 +34,6 @@ import type { Connection } from '@libp2p/interface/connection' import type { PeerId } from '@libp2p/interface/peer-id' import type { TransportManager } from '@libp2p/interface-internal/transport-manager' import type { Multiaddr } from '@multiformats/multiaddr' -import { AGENT_VERSION } from '../../src/identify/consts.js' const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999') const relayMultiaddr = multiaddr(process.env.RELAY_MULTIADDR) @@ -403,35 +403,35 @@ describe('libp2p.dialer (direct, WebSockets)', () => { await libp2p.stop() }) - it('should not run identify automatically after connecting', async () => { - libp2p = await createLibp2p({ - peerId, - transports: [ - webSockets({ - filter: filters.all - }) - ], - streamMuxers: [ - yamux(), - ], - connectionEncryption: [ - plaintext() - ], - services: { - identify: identifyService({ - protocolPrefix: 'ipfs', - agentVersion: AGENT_VERSION, - timeout: 60000, - maxInboundStreams: 1, - maxOutboundStreams: 1, - maxPushIncomingStreams: 1, - maxPushOutgoingStreams: 1, - maxObservedAddresses: 10, - maxIdentifyMessageSize: 8192, - runOnConnectionOpen: false - }) - }, - connectionGater: mockConnectionGater() + it('should not run identify automatically after connecting', async () => { + libp2p = await createLibp2p({ + peerId, + transports: [ + webSockets({ + filter: filters.all + }) + ], + streamMuxers: [ + yamux() + ], + connectionEncryption: [ + plaintext() + ], + services: { + identify: identifyService({ + protocolPrefix: 'ipfs', + agentVersion: AGENT_VERSION, + timeout: 60000, + maxInboundStreams: 1, + maxOutboundStreams: 1, + maxPushIncomingStreams: 1, + maxPushOutgoingStreams: 1, + maxObservedAddresses: 10, + maxIdentifyMessageSize: 8192, + runOnConnectionOpen: false + }) + }, + connectionGater: mockConnectionGater() }) if (libp2p.services.identify == null) { From 2002f160be8669c6b38c5ffb5ade3c7deb6f8246 Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 28 Jun 2023 15:32:36 -0500 Subject: [PATCH 4/9] test: update tests --- packages/libp2p/src/identify/index.ts | 6 +----- packages/libp2p/test/connection-manager/direct.spec.ts | 10 ---------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/packages/libp2p/src/identify/index.ts b/packages/libp2p/src/identify/index.ts index 8cf6756796..ab44f28896 100644 --- a/packages/libp2p/src/identify/index.ts +++ b/packages/libp2p/src/identify/index.ts @@ -73,15 +73,11 @@ export interface IdentifyService { * as streams may be closed by the local or the remote node. * Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol, * you may be better off configuring a topology to be notified instead. - * - * @param connection - * @param options - * @returns */ identify: (connection: Connection, options: AbortOptions) => Promise } -export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService { +export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService { return (components) => new DefaultIdentifyService(components, init) } diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index f6956c74de..a8adb5b59a 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -22,7 +22,6 @@ import { stubInterface } from 'sinon-ts' import { defaultComponents, type Components } from '../../src/components.js' import { DefaultConnectionManager } from '../../src/connection-manager/index.js' import { codes as ErrorCodes } from '../../src/errors.js' -import { AGENT_VERSION } from '../../src/identify/consts.js' import { identifyService } from '../../src/identify/index.js' import { createLibp2p } from '../../src/index.js' import { plaintext } from '../../src/insecure/index.js' @@ -419,15 +418,6 @@ describe('libp2p.dialer (direct, WebSockets)', () => { ], services: { identify: identifyService({ - protocolPrefix: 'ipfs', - agentVersion: AGENT_VERSION, - timeout: 60000, - maxInboundStreams: 1, - maxOutboundStreams: 1, - maxPushIncomingStreams: 1, - maxPushOutgoingStreams: 1, - maxObservedAddresses: 10, - maxIdentifyMessageSize: 8192, runOnConnectionOpen: false }) }, From 44e9d95168a66d074506da18702d74341abbd5e2 Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 28 Jun 2023 15:40:31 -0500 Subject: [PATCH 5/9] refactor: re-added default identify service --- packages/libp2p/src/identify/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/libp2p/src/identify/index.ts b/packages/libp2p/src/identify/index.ts index ab44f28896..b2b0aa34bb 100644 --- a/packages/libp2p/src/identify/index.ts +++ b/packages/libp2p/src/identify/index.ts @@ -78,6 +78,6 @@ export interface IdentifyService { } -export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService { +export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService { return (components) => new DefaultIdentifyService(components, init) } From c34798f9ac47d138f7cc219419cefa05903b9d37 Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 4 Jul 2023 10:53:40 -0500 Subject: [PATCH 6/9] test: remove identify service cast --- packages/libp2p/test/connection-manager/direct.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index a8adb5b59a..5b6e0d5982 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -22,7 +22,7 @@ import { stubInterface } from 'sinon-ts' import { defaultComponents, type Components } from '../../src/components.js' import { DefaultConnectionManager } from '../../src/connection-manager/index.js' import { codes as ErrorCodes } from '../../src/errors.js' -import { identifyService } from '../../src/identify/index.js' +import { IdentifyService, identifyService } from '../../src/identify/index.js' import { createLibp2p } from '../../src/index.js' import { plaintext } from '../../src/insecure/index.js' import { DefaultTransportManager } from '../../src/transport-manager.js' @@ -342,7 +342,7 @@ describe('dialing (direct, WebSockets)', () => { }) describe('libp2p.dialer (direct, WebSockets)', () => { - let libp2p: Libp2p<{ identify: unknown }> + let libp2p: Libp2p<{ identify: IdentifyService }> let peerId: PeerId beforeEach(async () => { @@ -382,7 +382,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => { throw new Error('Identify service missing') } - const identifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') + const identifySpy = sinon.spy(libp2p.services.identify, 'identify') const peerStorePatchSpy = sinon.spy(libp2p.peerStore, 'patch') const connectionPromise = pEvent(libp2p, 'connection:open') From 6ac838d59b5a30995fbed1113bf7d8da5236209c Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 4 Jul 2023 11:02:34 -0500 Subject: [PATCH 7/9] refactor: remove casts for identify service --- packages/libp2p/src/identify/index.ts | 1 + .../test/connection-manager/direct.spec.ts | 3 +-- packages/libp2p/test/identify/service.spec.ts | 19 +++++++++---------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/libp2p/src/identify/index.ts b/packages/libp2p/src/identify/index.ts index b2b0aa34bb..ce885e2ada 100644 --- a/packages/libp2p/src/identify/index.ts +++ b/packages/libp2p/src/identify/index.ts @@ -76,6 +76,7 @@ export interface IdentifyService { */ identify: (connection: Connection, options: AbortOptions) => Promise + push: () => Promise } export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService { diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 5b6e0d5982..05dc2c7a67 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -27,7 +27,6 @@ import { createLibp2p } from '../../src/index.js' import { plaintext } from '../../src/insecure/index.js' import { DefaultTransportManager } from '../../src/transport-manager.js' import { createPeerId } from '../fixtures/creators/peer.js' -import type { DefaultIdentifyService } from '../../src/identify/identify.js' import type { Libp2p } from '@libp2p/interface' import type { Connection } from '@libp2p/interface/connection' import type { PeerId } from '@libp2p/interface/peer-id' @@ -428,7 +427,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => { throw new Error('Identify service missing') } - const identifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') + const identifySpy = sinon.spy(libp2p.services.identify, 'identify') const connectionPromise = pEvent(libp2p, 'connection:open') await libp2p.start() diff --git a/packages/libp2p/test/identify/service.spec.ts b/packages/libp2p/test/identify/service.spec.ts index 8aebc246c7..4960774789 100644 --- a/packages/libp2p/test/identify/service.spec.ts +++ b/packages/libp2p/test/identify/service.spec.ts @@ -10,17 +10,16 @@ import sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { AGENT_VERSION } from '../../src/identify/consts.js' -import { identifyService } from '../../src/identify/index.js' +import { IdentifyService, identifyService } from '../../src/identify/index.js' import { createLibp2p } from '../../src/index.js' import { createBaseOptions } from '../fixtures/base-options.browser.js' -import type { DefaultIdentifyService } from '../../src/identify/identify.js' import type { Libp2p, IdentifyResult } from '@libp2p/interface' import type { PeerId } from '@libp2p/interface/peer-id' describe('identify', () => { let peerId: PeerId - let libp2p: Libp2p<{ identify: unknown }> - let remoteLibp2p: Libp2p<{ identify: unknown }> + let libp2p: Libp2p<{ identify: IdentifyService }> + let remoteLibp2p: Libp2p<{ identify: IdentifyService }> const remoteAddr = multiaddr(process.env.RELAY_MULTIADDR) before(async () => { @@ -55,7 +54,7 @@ describe('identify', () => { throw new Error('Identity service was not configured') } - const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') + const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify, 'identify') const connection = await libp2p.dial(remoteAddr) expect(connection).to.exist() @@ -110,7 +109,7 @@ describe('identify', () => { throw new Error('Identity service was not configured') } - const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') + const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify, 'identify') const connection = await libp2p.dial(remoteAddr) expect(connection).to.exist() @@ -143,8 +142,8 @@ describe('identify', () => { throw new Error('Identity service was not configured') } - const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') - const identityServicePushSpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'push') + const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify, 'identify') + const identityServicePushSpy = sinon.spy(libp2p.services.identify, 'push') const connectionPromise = pEvent(libp2p, 'connection:open') const connection = await libp2p.dial(remoteAddr) @@ -241,8 +240,8 @@ describe('identify', () => { throw new Error('Identity service was not configured') } - const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'identify') - const identityServicePushSpy = sinon.spy(libp2p.services.identify as DefaultIdentifyService, 'push') + const identityServiceIdentifySpy = sinon.spy(libp2p.services.identify, 'identify') + const identityServicePushSpy = sinon.spy(libp2p.services.identify, 'push') const connectionPromise = pEvent(libp2p, 'connection:open') const connection = await libp2p.dial(remoteAddr) From 25609e5a66999b2dfb2d7048b8ea1e72ff5ecb97 Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 4 Jul 2023 11:50:25 -0500 Subject: [PATCH 8/9] chore: linting fixes --- packages/libp2p/test/connection-manager/direct.spec.ts | 2 +- packages/libp2p/test/identify/service.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 05dc2c7a67..4c74d98d67 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -22,7 +22,7 @@ import { stubInterface } from 'sinon-ts' import { defaultComponents, type Components } from '../../src/components.js' import { DefaultConnectionManager } from '../../src/connection-manager/index.js' import { codes as ErrorCodes } from '../../src/errors.js' -import { IdentifyService, identifyService } from '../../src/identify/index.js' +import { type IdentifyService, identifyService } from '../../src/identify/index.js' import { createLibp2p } from '../../src/index.js' import { plaintext } from '../../src/insecure/index.js' import { DefaultTransportManager } from '../../src/transport-manager.js' diff --git a/packages/libp2p/test/identify/service.spec.ts b/packages/libp2p/test/identify/service.spec.ts index 4960774789..64e7303e2d 100644 --- a/packages/libp2p/test/identify/service.spec.ts +++ b/packages/libp2p/test/identify/service.spec.ts @@ -10,7 +10,7 @@ import sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { AGENT_VERSION } from '../../src/identify/consts.js' -import { IdentifyService, identifyService } from '../../src/identify/index.js' +import { type IdentifyService, identifyService } from '../../src/identify/index.js' import { createLibp2p } from '../../src/index.js' import { createBaseOptions } from '../fixtures/base-options.browser.js' import type { Libp2p, IdentifyResult } from '@libp2p/interface' From 4dd45221aee79d8bb3df9f222334852b33a74b6c Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 5 Jul 2023 15:37:14 -0500 Subject: [PATCH 9/9] test: updated tests to use Identify Service --- interop/test/ping.spec.ts | 7 +++--- packages/libp2p/src/identify/index.ts | 4 +-- packages/libp2p/test/identify/index.spec.ts | 28 ++++++++++----------- packages/libp2p/test/identify/push.spec.ts | 13 +++++----- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/interop/test/ping.spec.ts b/interop/test/ping.spec.ts index 04655c04b0..f182f1a66f 100644 --- a/interop/test/ping.spec.ts +++ b/interop/test/ping.spec.ts @@ -13,9 +13,8 @@ import { webTransport } from '@libp2p/webtransport' import { type Multiaddr, multiaddr } from '@multiformats/multiaddr' import { createLibp2p, type Libp2p, type Libp2pOptions } from 'libp2p' import { circuitRelayTransport } from 'libp2p/circuit-relay' -import { identifyService } from 'libp2p/identify' +import { IdentifyService, identifyService } from 'libp2p/identify' import { pingService, type PingService } from 'libp2p/ping' -import type { DefaultIdentifyService } from 'libp2p/identify/identify' async function redisProxy (commands: any[]): Promise { const res = await fetch(`http://localhost:${process.env.proxyPort ?? ''}/`, { body: JSON.stringify(commands), method: 'POST' }) @@ -25,7 +24,7 @@ async function redisProxy (commands: any[]): Promise { return res.json() } -let node: Libp2p<{ ping: PingService, identify: DefaultIdentifyService }> +let node: Libp2p<{ ping: PingService, identify: IdentifyService }> const isDialer: boolean = process.env.is_dialer === 'true' const timeoutSecs: string = process.env.test_timeout_secs ?? '180' @@ -38,7 +37,7 @@ describe('ping test', () => { const MUXER = process.env.muxer const IP = process.env.ip ?? '0.0.0.0' - const options: Libp2pOptions<{ ping: PingService, identify: DefaultIdentifyService }> = { + const options: Libp2pOptions<{ ping: PingService, identify: IdentifyService }> = { start: true, connectionGater: { denyDialMultiaddr: async () => false diff --git a/packages/libp2p/src/identify/index.ts b/packages/libp2p/src/identify/index.ts index ce885e2ada..95855d61b6 100644 --- a/packages/libp2p/src/identify/index.ts +++ b/packages/libp2p/src/identify/index.ts @@ -74,11 +74,11 @@ export interface IdentifyService { * Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol, * you may be better off configuring a topology to be notified instead. */ - identify: (connection: Connection, options: AbortOptions) => Promise + identify: (connection: Connection, options?: AbortOptions) => Promise push: () => Promise } -export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService { +export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService { return (components) => new DefaultIdentifyService(components, init) } diff --git a/packages/libp2p/test/identify/index.spec.ts b/packages/libp2p/test/identify/index.spec.ts index a786b6db98..0722e81ced 100644 --- a/packages/libp2p/test/identify/index.spec.ts +++ b/packages/libp2p/test/identify/index.spec.ts @@ -105,8 +105,8 @@ describe('identify', () => { }) it('should be able to identify another peer', async () => { - const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + const localIdentify = identifyService(defaultInit)(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify) @@ -128,8 +128,8 @@ describe('identify', () => { }) it('should throw if identified peer is the wrong peer', async () => { - const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + const localIdentify = identifyService(defaultInit)(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify) @@ -191,8 +191,8 @@ describe('identify', () => { }) it('should time out during identify', async () => { - const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + const localIdentify = identifyService(defaultInit)(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify) @@ -237,10 +237,10 @@ describe('identify', () => { it('should limit incoming identify message sizes', async () => { const deferred = pDefer() - const remoteIdentify = new DefaultIdentifyService(remoteComponents, { + const remoteIdentify = identifyService({ ...defaultInit, maxIdentifyMessageSize: 100 - }) + })(remoteComponents) await start(remoteIdentify) const identifySpy = sinon.spy(remoteIdentify, 'identify') @@ -283,10 +283,10 @@ describe('identify', () => { it('should time out incoming identify messages', async () => { const deferred = pDefer() - const remoteIdentify = new DefaultIdentifyService(remoteComponents, { + const remoteIdentify = identifyService({ ...defaultInit, timeout: 100 - }) + })(remoteComponents) await start(remoteIdentify) const identifySpy = sinon.spy(remoteIdentify, 'identify') @@ -336,8 +336,8 @@ describe('identify', () => { }) it('should retain existing peer metadata', async () => { - const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + const localIdentify = identifyService(defaultInit)(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify) @@ -363,8 +363,8 @@ describe('identify', () => { }) it('should ignore older signed peer record', async () => { - const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + const localIdentify = identifyService(defaultInit)(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify) diff --git a/packages/libp2p/test/identify/push.spec.ts b/packages/libp2p/test/identify/push.spec.ts index 02744d27d2..62b22fdbd5 100644 --- a/packages/libp2p/test/identify/push.spec.ts +++ b/packages/libp2p/test/identify/push.spec.ts @@ -21,9 +21,8 @@ import { MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_PUSH } from '../../src/identify/consts.js' -import { DefaultIdentifyService } from '../../src/identify/identify.js' +import { identifyService, type IdentifyServiceInit } from '../../src/identify/index.js' import { DefaultTransportManager } from '../../src/transport-manager.js' -import type { IdentifyServiceInit } from '../../src/identify/index.js' import type { TransportManager } from '@libp2p/interface-internal/transport-manager' const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')] @@ -97,8 +96,8 @@ describe('identify (push)', () => { }) it('should be able to push identify updates to another peer', async () => { - const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + const localIdentify = identifyService(defaultInit)(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify) @@ -171,11 +170,11 @@ describe('identify (push)', () => { it('should time out during push identify', async () => { let streamEnded = false - const localIdentify = new DefaultIdentifyService(localComponents, { + const localIdentify = identifyService({ ...defaultInit, timeout: 10 - }) - const remoteIdentify = new DefaultIdentifyService(remoteComponents, defaultInit) + })(localComponents) + const remoteIdentify = identifyService(defaultInit)(remoteComponents) await start(localIdentify) await start(remoteIdentify)