Skip to content

Commit 63aa480

Browse files
authoredJan 21, 2022
fix: try all peer addresses when dialing a relay (#1140)
The order of the addresses can affect our success rate in dialing a relay - if it's a loopback address or similar it won't work. Instead try dialing every address.
1 parent b7e8706 commit 63aa480

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed
 

‎src/circuit/auto-relay.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,24 @@ class AutoRelay {
161161
connection.remotePeer, this._addressSorter
162162
)
163163

164-
if (!remoteAddrs || !remoteAddrs.length) {
165-
return
166-
}
167-
168-
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
169-
this._listenRelays.add(id)
170-
171164
// Attempt to listen on relay
172-
await this._transportManager.listen([new Multiaddr(listenAddr)])
165+
const result = await Promise.all(
166+
remoteAddrs.map(async addr => {
167+
try {
168+
// Announce multiaddrs will update on listen success by TransportManager event being triggered
169+
await this._transportManager.listen([new Multiaddr(`${addr.toString()}/p2p-circuit`)])
170+
return true
171+
} catch (/** @type {any} */ err) {
172+
this._onError(err)
173+
}
174+
175+
return false
176+
})
177+
)
173178

174-
// Announce multiaddrs will update on listen success by TransportManager event being triggered
179+
if (result.includes(true)) {
180+
this._listenRelays.add(id)
181+
}
175182
} catch (/** @type {any} */ err) {
176183
this._onError(err)
177184
this._listenRelays.delete(id)

‎src/connection-manager/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class ConnectionManager extends EventEmitter {
112112
latencyCheckIntervalMs: this._options.pollInterval,
113113
dataEmitIntervalMs: this._options.pollInterval
114114
})
115+
116+
// This emitter gets listened to a lot
117+
this.setMaxListeners(Infinity)
115118
}
116119

117120
/**

‎test/relay/auto-relay.node.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('auto-relay', () => {
224224
expect(knownProtocols3).to.include(relayMulticodec)
225225
})
226226

227-
it('should not listen on a relayed address if peer disconnects', async () => {
227+
it('should not listen on a relayed address we disconnect from peer', async () => {
228228
// Spy if identify push is fired on adding/removing listen addr
229229
sinon.spy(relayLibp2p1.identifyService, 'pushToPeerStore')
230230

@@ -236,19 +236,13 @@ describe('auto-relay', () => {
236236
// Wait for listening on the relay
237237
await usingAsRelay(relayLibp2p1, relayLibp2p2)
238238

239-
// Identify push for adding listen relay multiaddr
240-
expect(relayLibp2p1.identifyService.pushToPeerStore.callCount).to.equal(1)
241-
242239
// Disconnect from peer used for relay
243240
await relayLibp2p1.hangUp(relayLibp2p2.peerId)
244241

245242
// Wait for removed listening on the relay
246243
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
247244
timeout: 1000
248245
})).to.eventually.be.rejected()
249-
250-
// Identify push for removing listen relay multiaddr
251-
await pWaitFor(() => relayLibp2p1.identifyService.pushToPeerStore.callCount === 2)
252246
})
253247

254248
it('should try to listen on other connected peers relayed address if one used relay disconnects', async () => {
@@ -271,6 +265,11 @@ describe('auto-relay', () => {
271265
// Disconnect from peer used for relay
272266
await relayLibp2p2.stop()
273267

268+
// Should not be using the relay any more
269+
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
270+
timeout: 1000
271+
})).to.eventually.be.rejected()
272+
274273
// Wait for other peer connected to be added as listen addr
275274
await usingAsRelay(relayLibp2p1, relayLibp2p3)
276275
})

0 commit comments

Comments
 (0)
Please sign in to comment.