fix: when creating dial targets, encapsulate PeerIds last (#1389)

It turns out because `Multiaddr.encapsulate` stringifies the `Multiaddr`
it's a [suprisingly expensive operation](https://github.com/multiformats/js-multiaddr/pull/275#issuecomment-1254981709)
so here we switch the order of our `Multiaddr` pipeline around so
we filter undialable addresses (e.g. unsupported transports etc) before
encapsulating the `PeerId` onto a `Multiaddr` we'd then just ignore.
This commit is contained in:
Alex Potsides 2022-09-23 09:33:42 +01:00 committed by GitHub
parent 3f57edaf3b
commit ec02351e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -232,15 +232,23 @@ export class DefaultDialer implements Startable, Dialer {
* Multiaddrs not supported by the available transports will be filtered out. * Multiaddrs not supported by the available transports will be filtered out.
*/ */
async _createDialTarget (peer: PeerId, options: AbortOptions): Promise<DialTarget> { async _createDialTarget (peer: PeerId, options: AbortOptions): Promise<DialTarget> {
const knownAddrs = await pipe( const _resolve = this._resolve.bind(this)
const addrs = await pipe(
await this.components.getPeerStore().addressBook.get(peer), await this.components.getPeerStore().addressBook.get(peer),
(source) => filter(source, async (address) => { (source) => filter(source, async (address) => {
return !(await this.components.getConnectionGater().denyDialMultiaddr(peer, address.multiaddr)) return !(await this.components.getConnectionGater().denyDialMultiaddr(peer, address.multiaddr))
}), }),
// Sort addresses so, for example, we try certified public address first
(source) => sort(source, this.addressSorter), (source) => sort(source, this.addressSorter),
(source) => map(source, (address) => { async function * resolve (source) {
const ma = address.multiaddr for await (const a of source) {
yield * await _resolve(a.multiaddr, options)
}
},
// Multiaddrs not supported by the available transports will be filtered out.
(source) => filter(source, (ma) => Boolean(this.components.getTransportManager().transportForMultiaddr(ma))),
(source) => map(source, (ma) => {
if (peer.toString() === ma.getPeerId()) { if (peer.toString() === ma.getPeerId()) {
return ma return ma
} }
@ -250,23 +258,14 @@ export class DefaultDialer implements Startable, Dialer {
async (source) => await all(source) async (source) => await all(source)
) )
const addrs: Multiaddr[] = [] if (addrs.length > this.maxAddrsToDial) {
for (const a of knownAddrs) {
const resolvedAddrs = await this._resolve(a, options)
resolvedAddrs.forEach(ra => addrs.push(ra))
}
// Multiaddrs not supported by the available transports will be filtered out.
const supportedAddrs = addrs.filter(a => this.components.getTransportManager().transportForMultiaddr(a))
if (supportedAddrs.length > this.maxAddrsToDial) {
await this.components.getPeerStore().delete(peer) await this.components.getPeerStore().delete(peer)
throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES) throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES)
} }
return { return {
id: peer.toString(), id: peer.toString(),
addrs: supportedAddrs addrs
} }
} }