@@ -4,7 +4,6 @@ const errcode = require('err-code')
4
4
const pTimeout = require ( 'p-timeout' )
5
5
6
6
const PeerId = require ( 'peer-id' )
7
- const PeerInfo = require ( 'peer-info' )
8
7
const crypto = require ( 'libp2p-crypto' )
9
8
10
9
const c = require ( '../constants' )
@@ -24,11 +23,7 @@ module.exports = (dht) => {
24
23
dht . _log ( 'findPeerLocal %s' , peer . toB58String ( ) )
25
24
const p = await dht . routingTable . find ( peer )
26
25
27
- if ( ! p || ! dht . peerStore . has ( p ) ) {
28
- return
29
- }
30
-
31
- return dht . peerStore . get ( p )
26
+ return p && dht . peerStore . get ( p )
32
27
}
33
28
34
29
/**
@@ -57,7 +52,12 @@ module.exports = (dht) => {
57
52
58
53
return msg . closerPeers
59
54
. filter ( ( pInfo ) => ! dht . _isSelf ( pInfo . id ) )
60
- . map ( ( pInfo ) => dht . peerStore . put ( pInfo ) )
55
+ . map ( ( pInfo ) => {
56
+ // Add known address to peer store
57
+ dht . peerStore . addressBook . add ( pInfo . id , pInfo . multiaddrs . toArray ( ) )
58
+
59
+ return pInfo
60
+ } )
61
61
}
62
62
63
63
/**
@@ -128,9 +128,13 @@ module.exports = (dht) => {
128
128
129
129
// sanity check
130
130
const match = peers . find ( ( p ) => p . isEqual ( id ) )
131
- if ( match && dht . peerStore . has ( id ) ) {
132
- dht . _log ( 'found in peerStore' )
133
- return dht . peerStore . get ( id )
131
+ if ( match ) {
132
+ const peer = dht . peerStore . get ( id )
133
+
134
+ if ( peer ) {
135
+ dht . _log ( 'found in peerStore' )
136
+ return peer
137
+ }
134
138
}
135
139
136
140
// query the network
@@ -169,7 +173,7 @@ module.exports = (dht) => {
169
173
result . paths . forEach ( ( result ) => {
170
174
if ( result . success ) {
171
175
success = true
172
- dht . peerStore . put ( result . peer )
176
+ dht . peerStore . addressBook . add ( result . peer . id , result . peer . multiaddrs . toArray ( ) )
173
177
}
174
178
} )
175
179
dht . _log ( 'findPeer %s: %s' , id . toB58String ( ) , success )
@@ -228,16 +232,10 @@ module.exports = (dht) => {
228
232
dht . _log ( 'getPublicKey %s' , peer . toB58String ( ) )
229
233
230
234
// local check
231
- let info
232
- if ( dht . peerStore . has ( peer ) ) {
233
- info = dht . peerStore . get ( peer )
234
-
235
- if ( info && info . id . pubKey ) {
236
- dht . _log ( 'getPublicKey: found local copy' )
237
- return info . id . pubKey
238
- }
239
- } else {
240
- info = dht . peerStore . put ( new PeerInfo ( peer ) )
235
+ const info = dht . peerStore . get ( peer )
236
+ if ( info && info . id . pubKey ) {
237
+ dht . _log ( 'getPublicKey: found local copy' )
238
+ return info . id . pubKey
241
239
}
242
240
243
241
// try the node directly
@@ -252,7 +250,7 @@ module.exports = (dht) => {
252
250
}
253
251
254
252
info . id = new PeerId ( peer . id , null , pk )
255
- dht . peerStore . put ( info )
253
+ dht . peerStore . addressBook . add ( info . id , info . multiaddrs . toArray ( ) )
256
254
257
255
return pk
258
256
}
0 commit comments