From 0551eb6b55992cb88ec70f01039d4f3e3c687f77 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 7 Apr 2023 12:48:18 +0100 Subject: [PATCH 1/2] fix: do not append peer id to advertised addresses The libp2p address manager does this so we can just treat the addresses as opaque here. Fixes https://github.com/libp2p/js-libp2p/issues/1673 --- src/index.ts | 5 ++--- test/multicast-dns.spec.ts | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 70235b1..43828c0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,12 +92,11 @@ class MulticastDNS extends EventEmitter implements PeerDisc } log.trace('received incoming mDNS query') - const localPeerId = this.components.peerId query.gotQuery( event, this.mdns, this.peerName, - this.components.addressManager.getAddresses().map((ma) => ma.encapsulate('/p2p/' + localPeerId.toString())), + this.components.addressManager.getAddresses(), this.serviceTag, this.broadcast) } @@ -109,7 +108,7 @@ class MulticastDNS extends EventEmitter implements PeerDisc const foundPeer = query.gotResponse(event, this.peerName, this.serviceTag) if (foundPeer != null) { - log('discovered peer in mDNS qeury response %p', foundPeer.id) + log('discovered peer in mDNS query response %p', foundPeer.id) this.dispatchEvent(new CustomEvent('peer', { detail: foundPeer diff --git a/test/multicast-dns.spec.ts b/test/multicast-dns.spec.ts index ed96b07..3e738e9 100644 --- a/test/multicast-dns.spec.ts +++ b/test/multicast-dns.spec.ts @@ -14,7 +14,7 @@ import { start, stop } from '@libp2p/interfaces/startable' function getComponents (peerId: PeerId, multiaddrs: Multiaddr[]): { peerId: PeerId, addressManager: StubbedInstance } { const addressManager = stubInterface() - addressManager.getAddresses.returns(multiaddrs) + addressManager.getAddresses.returns(multiaddrs.map(ma => ma.encapsulate(`/p2p/${peerId.toString()}`))) return { peerId, addressManager } } From 1881f7ecbcd84d2dd34a3ec216fa40df4ec21342 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 7 Apr 2023 12:51:50 +0100 Subject: [PATCH 2/2] chore: remove unused deps --- package.json | 2 +- src/index.ts | 2 -- test/compliance.spec.ts | 1 - test/multicast-dns.spec.ts | 8 ++++---- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 93a19a4..c9f83f0 100644 --- a/package.json +++ b/package.json @@ -136,7 +136,6 @@ }, "dependencies": { "@libp2p/interface-peer-discovery": "^1.0.5", - "@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-info": "^1.0.8", "@libp2p/interfaces": "^3.3.1", "@libp2p/logger": "^2.0.5", @@ -149,6 +148,7 @@ "devDependencies": { "@libp2p/interface-address-manager": "^2.0.1", "@libp2p/interface-peer-discovery-compliance-tests": "^2.0.1", + "@libp2p/interface-peer-id": "^2.0.1", "@libp2p/peer-id-factory": "^2.0.0", "aegir": "^38.1.2", "p-wait-for": "^5.0.0", diff --git a/src/index.ts b/src/index.ts index 43828c0..7aa0480 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer- import type { PeerInfo } from '@libp2p/interface-peer-info' import { symbol } from '@libp2p/interface-peer-discovery' import { stringGen } from './utils.js' -import type { PeerId } from '@libp2p/interface-peer-id' import type { AddressManager } from '@libp2p/interface-address-manager' const log = logger('libp2p:mdns') @@ -21,7 +20,6 @@ export interface MulticastDNSInit { } export interface MulticastDNSComponents { - peerId: PeerId addressManager: AddressManager } diff --git a/test/compliance.spec.ts b/test/compliance.spec.ts index 1cb42b4..72e02c8 100644 --- a/test/compliance.spec.ts +++ b/test/compliance.spec.ts @@ -28,7 +28,6 @@ describe('compliance tests', () => { broadcast: false, port: 50001 })({ - peerId: peerId1, addressManager }) diff --git a/test/multicast-dns.spec.ts b/test/multicast-dns.spec.ts index 3e738e9..38a5564 100644 --- a/test/multicast-dns.spec.ts +++ b/test/multicast-dns.spec.ts @@ -5,18 +5,18 @@ import type { Multiaddr } from '@multiformats/multiaddr' import { multiaddr } from '@multiformats/multiaddr' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import pWaitFor from 'p-wait-for' -import { mdns } from './../src/index.js' +import { mdns, MulticastDNSComponents } from './../src/index.js' import type { PeerId } from '@libp2p/interface-peer-id' import type { PeerInfo } from '@libp2p/interface-peer-info' -import { StubbedInstance, stubInterface } from 'ts-sinon' +import { stubInterface } from 'ts-sinon' import type { AddressManager } from '@libp2p/interface-address-manager' import { start, stop } from '@libp2p/interfaces/startable' -function getComponents (peerId: PeerId, multiaddrs: Multiaddr[]): { peerId: PeerId, addressManager: StubbedInstance } { +function getComponents (peerId: PeerId, multiaddrs: Multiaddr[]): MulticastDNSComponents { const addressManager = stubInterface() addressManager.getAddresses.returns(multiaddrs.map(ma => ma.encapsulate(`/p2p/${peerId.toString()}`))) - return { peerId, addressManager } + return { addressManager } } describe('MulticastDNS', () => {