Skip to content

Commit 389d0db

Browse files
committed
chore: address review
1 parent 3004543 commit 389d0db

File tree

3 files changed

+77
-17
lines changed

3 files changed

+77
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"mafmt": "^8.0.0",
6565
"merge-options": "^2.0.0",
6666
"moving-average": "^1.0.0",
67-
"multiaddr": "multiformats/js-multiaddr#feat/resolve-multiaddrs",
67+
"multiaddr": "^8.1.0",
6868
"multicodec": "^2.0.0",
6969
"multistream-select": "^1.0.0",
7070
"mutable-proxy": "^1.0.0",

src/dialer/index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ const {
1818
MAX_PER_PEER_DIALS
1919
} = require('../constants')
2020

21-
const dns4Code = 54
22-
const dns6Code = 55
23-
2421
class Dialer {
2522
/**
2623
* @class
@@ -45,9 +42,12 @@ class Dialer {
4542
this.concurrency = concurrency
4643
this.timeout = timeout
4744
this.perPeerLimit = perPeerLimit
48-
this.resolvers = resolvers
4945
this.tokens = [...new Array(concurrency)].map((_, index) => index)
5046
this._pendingDials = new Map()
47+
48+
for (const [key, value] of Object.entries(resolvers)) {
49+
multiaddr.resolvers.set(key, value)
50+
}
5151
}
5252

5353
/**
@@ -211,10 +211,11 @@ class Dialer {
211211
*/
212212
async _resolve (ma) {
213213
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
214-
const resolvableProto = ma.protos().find((p) => p.resolvable)
214+
// Now only supporting resolve for dnsaddr
215+
const resolvableProto = ma.protoNames().includes('dnsaddr')
215216

216-
// Multiaddr is not resolvable (including exception for dns4/dns6)? End recursion!
217-
if (!resolvableProto || resolvableProto.code === dns4Code || resolvableProto === dns6Code) {
217+
// Multiaddr is not resolvable? End recursion!
218+
if (!resolvableProto) {
218219
return [ma]
219220
}
220221

@@ -232,17 +233,19 @@ class Dialer {
232233
}
233234

234235
/**
235-
* Add dialer resolvers to multiaddr and resolve multiaddr.
236+
* Resolve a given multiaddr. If this fails, an empty array will be returned
236237
*
237238
* @param {Multiaddr} ma
238239
* @returns {Promise<Array<Multiaddr>>}
239240
*/
240-
_resolveRecord (ma) {
241-
for (const [key, value] of Object.entries(this.resolvers)) {
242-
ma.resolvers.set(key, value)
241+
async _resolveRecord (ma) {
242+
try {
243+
const multiaddrs = await ma.resolve()
244+
return multiaddrs
245+
} catch (_) {
246+
log.error(`multiaddr ${ma} could not be resolved`)
247+
return []
243248
}
244-
245-
return ma.resolve()
246249
}
247250
}
248251

test/dialing/resolver.spec.js

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const sinon = require('sinon')
77
const multiaddr = require('multiaddr')
88
const { Resolver } = require('multiaddr/src/resolvers/dns')
99

10+
const { codes: ErrorCodes } = require('../../src/errors')
11+
1012
const peerUtils = require('../utils/creators/peer')
1113
const baseOptions = require('../utils/base-options.browser')
1214

@@ -88,10 +90,10 @@ describe('Dialing (resolvable addresses)', () => {
8890

8991
// Resolver stub
9092
const stub = sinon.stub(Resolver.prototype, 'resolveTxt')
91-
let alreadyCalled = false
93+
let firstCall = false
9294
stub.callsFake(() => {
93-
if (!alreadyCalled) {
94-
alreadyCalled = true
95+
if (!firstCall) {
96+
firstCall = true
9597
// Return an array of dnsaddr
9698
return Promise.resolve(getDnsaddrStub(remoteId))
9799
}
@@ -108,6 +110,7 @@ describe('Dialing (resolvable addresses)', () => {
108110
})
109111

110112
// TODO: Temporary solution does not resolve dns4/dns6
113+
// Resolver just returns the received multiaddrs
111114
it('stops recursive resolve if finds dns4/dns6 and dials it', async () => {
112115
const remoteId = remoteLibp2p.peerId.toB58String()
113116
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId}`)
@@ -128,4 +131,58 @@ describe('Dialing (resolvable addresses)', () => {
128131

129132
await libp2p.dial(dialAddr)
130133
})
134+
135+
it('resolves a dnsaddr recursively not failing if one address fails to resolve', async () => {
136+
const remoteId = remoteLibp2p.peerId.toB58String()
137+
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId}`)
138+
const relayedAddrFetched = multiaddr(relayedAddr(remoteId))
139+
140+
// Transport spy
141+
const transport = libp2p.transportManager._transports.get('Circuit')
142+
sinon.spy(transport, 'dial')
143+
144+
// Resolver stub
145+
let firstCall = false
146+
let secondCall = false
147+
148+
const stub = sinon.stub(Resolver.prototype, 'resolveTxt')
149+
stub.callsFake(() => {
150+
if (!firstCall) {
151+
firstCall = true
152+
// Return an array of dnsaddr
153+
return Promise.resolve(getDnsaddrStub(remoteId))
154+
} else if (!secondCall) {
155+
secondCall = true
156+
// Address failed to resolve
157+
return Promise.reject(new Error())
158+
}
159+
return Promise.resolve(getDnsRelayedAddrStub(remoteId))
160+
})
161+
162+
// Dial with address resolve
163+
const connection = await libp2p.dial(dialAddr)
164+
expect(connection).to.exist()
165+
expect(connection.remoteAddr.equals(relayedAddrFetched))
166+
167+
const dialArgs = transport.dial.firstCall.args
168+
expect(dialArgs[0].equals(relayedAddrFetched)).to.eql(true)
169+
})
170+
171+
it('fails to dial if resolve fails and there are no addresses to dial', async () => {
172+
const remoteId = remoteLibp2p.peerId.toB58String()
173+
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId}`)
174+
175+
// Stub resolver
176+
const stubResolve = sinon.stub(Resolver.prototype, 'resolveTxt')
177+
stubResolve.returns(Promise.reject(new Error()))
178+
179+
// Stub transport
180+
const transport = libp2p.transportManager._transports.get('WebSockets')
181+
const spy = sinon.spy(transport, 'dial')
182+
183+
await expect(libp2p.dial(dialAddr))
184+
.to.eventually.be.rejectedWith(Error)
185+
.and.to.have.nested.property('.code', ErrorCodes.ERR_NO_VALID_ADDRESSES)
186+
expect(spy.callCount).to.eql(0)
187+
})
131188
})

0 commit comments

Comments
 (0)