Skip to content

Commit bd2bdf7

Browse files
authored
fix: do not append peer id to path addresses (#1547)
Some multiaddrs have a path address as the final component - these components are terminal in that you cannot append further tuples to them, so do not do that when one is encountered.
1 parent 398e231 commit bd2bdf7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/address-manager/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
153153
return this.announceFilter(Array.from(addrSet)
154154
.map(str => multiaddr(str)))
155155
.map(ma => {
156+
// do not append our peer id to a path multiaddr as it will become invalid
157+
if (ma.protos().pop()?.path === true) {
158+
return ma
159+
}
160+
156161
if (ma.getPeerId() === this.components.peerId.toString()) {
157162
return ma
158163
}

test/addresses/address-manager.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ describe('Address Manager', () => {
179179
expect(am.getObservedAddrs()).to.have.lengthOf(1)
180180
expect(am.getObservedAddrs().map(ma => ma.toString())).to.include(ma)
181181
})
182+
183+
it('should not add our peer id to path multiaddrs', () => {
184+
const ma = '/unix/foo/bar/baz'
185+
const transportManager = stubInterface<TransportManager>()
186+
const am = new DefaultAddressManager({
187+
peerId,
188+
transportManager
189+
}, {
190+
listen: [ma],
191+
announce: []
192+
})
193+
194+
transportManager.getAddrs.returns([multiaddr(ma)])
195+
196+
const addrs = am.getAddresses()
197+
expect(addrs).to.have.lengthOf(1)
198+
expect(addrs[0].toString()).to.not.include(`/p2p/${peerId.toString()}`)
199+
})
182200
})
183201

184202
describe('libp2p.addressManager', () => {

0 commit comments

Comments
 (0)