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.
This commit is contained in:
Alex Potsides
2022-01-21 17:57:09 +00:00
committed by GitHub
parent b7e87066a6
commit 63aa480800
3 changed files with 25 additions and 16 deletions

View File

@ -161,17 +161,24 @@ class AutoRelay {
connection.remotePeer, this._addressSorter
)
if (!remoteAddrs || !remoteAddrs.length) {
return
}
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
this._listenRelays.add(id)
// Attempt to listen on relay
await this._transportManager.listen([new Multiaddr(listenAddr)])
const result = await Promise.all(
remoteAddrs.map(async addr => {
try {
// Announce multiaddrs will update on listen success by TransportManager event being triggered
await this._transportManager.listen([new Multiaddr(`${addr.toString()}/p2p-circuit`)])
return true
} catch (/** @type {any} */ err) {
this._onError(err)
}
// Announce multiaddrs will update on listen success by TransportManager event being triggered
return false
})
)
if (result.includes(true)) {
this._listenRelays.add(id)
}
} catch (/** @type {any} */ err) {
this._onError(err)
this._listenRelays.delete(id)

View File

@ -112,6 +112,9 @@ class ConnectionManager extends EventEmitter {
latencyCheckIntervalMs: this._options.pollInterval,
dataEmitIntervalMs: this._options.pollInterval
})
// This emitter gets listened to a lot
this.setMaxListeners(Infinity)
}
/**

View File

@ -224,7 +224,7 @@ describe('auto-relay', () => {
expect(knownProtocols3).to.include(relayMulticodec)
})
it('should not listen on a relayed address if peer disconnects', async () => {
it('should not listen on a relayed address we disconnect from peer', async () => {
// Spy if identify push is fired on adding/removing listen addr
sinon.spy(relayLibp2p1.identifyService, 'pushToPeerStore')
@ -236,9 +236,6 @@ describe('auto-relay', () => {
// Wait for listening on the relay
await usingAsRelay(relayLibp2p1, relayLibp2p2)
// Identify push for adding listen relay multiaddr
expect(relayLibp2p1.identifyService.pushToPeerStore.callCount).to.equal(1)
// Disconnect from peer used for relay
await relayLibp2p1.hangUp(relayLibp2p2.peerId)
@ -246,9 +243,6 @@ describe('auto-relay', () => {
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
timeout: 1000
})).to.eventually.be.rejected()
// Identify push for removing listen relay multiaddr
await pWaitFor(() => relayLibp2p1.identifyService.pushToPeerStore.callCount === 2)
})
it('should try to listen on other connected peers relayed address if one used relay disconnects', async () => {
@ -271,6 +265,11 @@ describe('auto-relay', () => {
// Disconnect from peer used for relay
await relayLibp2p2.stop()
// Should not be using the relay any more
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
timeout: 1000
})).to.eventually.be.rejected()
// Wait for other peer connected to be added as listen addr
await usingAsRelay(relayLibp2p1, relayLibp2p3)
})