Skip to content

Commit 894cb69

Browse files
chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
1 parent ab20987 commit 894cb69

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

doc/API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ Once a content router succeeds, the iteration will stop. If the DHT is enabled,
411411
```js
412412
// Iterate over the providers found for the given cid
413413
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
414-
console.log(provider.id, provider.addrs)
414+
console.log(provider.id, provider.multiaddrs)
415415
}
416416
```
417417

src/dialer/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const log = debug('libp2p:dialer')
99
log.error = debug('libp2p:dialer:error')
1010

1111
const { DialRequest } = require('./dial-request')
12-
const getPeerId = require('../get-peer-id')
12+
const getPeer = require('../get-peer')
1313

1414
const { codes } = require('../errors')
1515
const {
@@ -106,8 +106,13 @@ class Dialer {
106106
* @returns {DialTarget}
107107
*/
108108
_createDialTarget (peer) {
109-
const peerId = getPeerId(peer, this.peerStore)
110-
let addrs = this.peerStore.addressBook.getMultiaddrsForPeer(peerId)
109+
const { id, multiaddrs } = getPeer(peer)
110+
111+
if (multiaddrs) {
112+
this.peerStore.addressBook.add(id, multiaddrs)
113+
}
114+
115+
let addrs = this.peerStore.addressBook.getMultiaddrsForPeer(id)
111116

112117
// If received a multiaddr to dial, it should be the first to use
113118
// But, if we know other multiaddrs for the peer, we should try them too.
@@ -117,7 +122,7 @@ class Dialer {
117122
}
118123

119124
return {
120-
id: peerId.toB58String(),
125+
id: id.toB58String(),
121126
addrs
122127
}
123128
}

src/get-peer-id.js renamed to src/get-peer.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const errCode = require('err-code')
77
const { codes } = require('./errors')
88

99
/**
10-
* Converts the given `peer` to a `PeerId` instance.
10+
* Converts the given `peer` to a `Peer` object.
1111
* If a multiaddr is received, the addressBook is updated.
1212
* @param {PeerId|Multiaddr|string} peer
1313
* @param {PeerStore} peerStore
14-
* @returns {PeerId}
14+
* @returns {{ id: PeerId, multiaddrs: Array<Multiaddr> }}
1515
*/
16-
function getPeerId (peer, peerStore) {
16+
function getPeer (peer) {
1717
if (typeof peer === 'string') {
1818
peer = multiaddr(peer)
1919
}
@@ -31,11 +31,10 @@ function getPeerId (peer, peerStore) {
3131
}
3232
}
3333

34-
if (addr && peerStore) {
35-
peerStore.addressBook.add(peer, [addr])
34+
return {
35+
id: peer,
36+
multiaddrs: addr ? [addr] : undefined
3637
}
37-
38-
return peer
3938
}
4039

41-
module.exports = getPeerId
40+
module.exports = getPeer

src/identify/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class IdentifyService {
4848
* @param {Registrar} options.registrar
4949
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
5050
* @param {PeerId} options.peerId The peer running the identify service
51-
* @param {{ listen: Array<Multiaddr>}} options.addresses The peer aaddresses
51+
* @param {{ listen: Array<Multiaddr>}} options.addresses The peer addresses
5252
*/
5353
constructor (options) {
5454
/**

src/index.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const PeerId = require('peer-id')
1010
const peerRouting = require('./peer-routing')
1111
const contentRouting = require('./content-routing')
1212
const pubsub = require('./pubsub')
13-
const getPeerId = require('./get-peer-id')
13+
const getPeer = require('./get-peer')
1414
const { validate: validateConfig } = require('./config')
1515
const { codes } = require('./errors')
1616

@@ -290,11 +290,13 @@ class Libp2p extends EventEmitter {
290290
* @returns {Promise<Connection|*>}
291291
*/
292292
async dialProtocol (peer, protocols, options) {
293-
const peerId = getPeerId(peer, this.peerStore)
294-
let connection = this.registrar.getConnection(peerId)
293+
const { id, multiaddrs } = getPeer(peer, this.peerStore)
294+
let connection = this.registrar.getConnection(id)
295295

296296
if (!connection) {
297297
connection = await this.dialer.connectToPeer(peer, options)
298+
} else {
299+
this.peerStore.addressBook.add(id, multiaddrs)
298300
}
299301

300302
// If a protocol was provided, create a new stream
@@ -311,10 +313,10 @@ class Libp2p extends EventEmitter {
311313
* @returns {Promise<void>}
312314
*/
313315
hangUp (peer) {
314-
const peerId = getPeerId(peer)
316+
const { id } = getPeer(peer)
315317

316318
return Promise.all(
317-
this.registrar.connections.get(peerId.toB58String()).map(connection => {
319+
this.registrar.connections.get(id.toB58String()).map(connection => {
318320
return connection.close()
319321
})
320322
)
@@ -326,9 +328,9 @@ class Libp2p extends EventEmitter {
326328
* @returns {Promise<number>}
327329
*/
328330
ping (peer) {
329-
const peerId = getPeerId(peer)
331+
const { id } = getPeer(peer)
330332

331-
return ping(this, peerId)
333+
return ping(this, id)
332334
}
333335

334336
/**

0 commit comments

Comments
 (0)