-
Notifications
You must be signed in to change notification settings - Fork 60
chore: use new peer store api #179
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ const errcode = require('err-code') | |
const pTimeout = require('p-timeout') | ||
|
||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const crypto = require('libp2p-crypto') | ||
|
||
const c = require('../constants') | ||
|
@@ -24,11 +23,7 @@ module.exports = (dht) => { | |
dht._log('findPeerLocal %s', peer.toB58String()) | ||
const p = await dht.routingTable.find(peer) | ||
|
||
if (!p || !dht.peerStore.has(p)) { | ||
return | ||
} | ||
|
||
return dht.peerStore.get(p) | ||
return p && dht.peerStore.get(p) | ||
} | ||
|
||
/** | ||
|
@@ -57,7 +52,12 @@ module.exports = (dht) => { | |
|
||
return msg.closerPeers | ||
.filter((pInfo) => !dht._isSelf(pInfo.id)) | ||
.map((pInfo) => dht.peerStore.put(pInfo)) | ||
.map((pInfo) => { | ||
// Add known address to peer store | ||
dht.peerStore.addressBook.add(pInfo.id, pInfo.multiaddrs.toArray()) | ||
|
||
return pInfo | ||
}) | ||
} | ||
|
||
/** | ||
|
@@ -128,9 +128,13 @@ module.exports = (dht) => { | |
|
||
// sanity check | ||
const match = peers.find((p) => p.isEqual(id)) | ||
if (match && dht.peerStore.has(id)) { | ||
dht._log('found in peerStore') | ||
return dht.peerStore.get(id) | ||
if (match) { | ||
const peer = dht.peerStore.get(id) | ||
|
||
if (peer) { | ||
dht._log('found in peerStore') | ||
return peer | ||
} | ||
} | ||
|
||
// query the network | ||
|
@@ -169,7 +173,7 @@ module.exports = (dht) => { | |
result.paths.forEach((result) => { | ||
if (result.success) { | ||
success = true | ||
dht.peerStore.put(result.peer) | ||
dht.peerStore.addressBook.add(result.peer.id, result.peer.multiaddrs.toArray()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that this is now a discrete action. It's much clearer what's happening than a |
||
} | ||
}) | ||
dht._log('findPeer %s: %s', id.toB58String(), success) | ||
|
@@ -228,16 +232,10 @@ module.exports = (dht) => { | |
dht._log('getPublicKey %s', peer.toB58String()) | ||
|
||
// local check | ||
let info | ||
if (dht.peerStore.has(peer)) { | ||
info = dht.peerStore.get(peer) | ||
|
||
if (info && info.id.pubKey) { | ||
dht._log('getPublicKey: found local copy') | ||
return info.id.pubKey | ||
} | ||
} else { | ||
info = dht.peerStore.put(new PeerInfo(peer)) | ||
const info = dht.peerStore.get(peer) | ||
if (info && info.id.pubKey) { | ||
dht._log('getPublicKey: found local copy') | ||
return info.id.pubKey | ||
} | ||
|
||
// try the node directly | ||
|
@@ -252,7 +250,7 @@ module.exports = (dht) => { | |
} | ||
|
||
info.id = new PeerId(peer.id, null, pk) | ||
dht.peerStore.put(info) | ||
dht.peerStore.addressBook.add(info.id, info.multiaddrs.toArray()) | ||
|
||
return pk | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,7 +244,6 @@ class WorkerQueue { | |
if (this.dht._isSelf(closer.id)) { | ||
return | ||
} | ||
closer = this.dht.peerStore.put(closer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No new information of the peer at this point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it's unnecessary as the emit below will cause libp2p to update the peer store |
||
this.dht._peerDiscovered(closer) | ||
await this.path.addPeerToQuery(closer.id) | ||
})) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,13 +34,7 @@ module.exports = (dht) => { | |
dht._betterPeersToQuery(msg, peer) | ||
]) | ||
|
||
const providers = peers.map((p) => { | ||
if (dht.peerStore.has(p)) { | ||
return dht.peerStore.get(p) | ||
} | ||
|
||
return dht.peerStore.put(new PeerInfo(p)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already known providers, no need to add to the peerStore. Moreover, the peerInfo is just created here, so there is nothing relevant to add |
||
}) | ||
const providers = peers.map((p) => new PeerInfo(p)) | ||
|
||
if (has) { | ||
providers.push(dht.peerInfo) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobheun I am not sure if mocking the
peerStore
is the best thing to do here, or depend onlibp2p
as a dev dependency again. What do you think? It could be worth having a mocks repo for this kind of stuff, but we would need to make sure it was properly updated with new changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vasco-santos something I think could be a better setup is to have libp2p as a dev dependency of the modules, and then have the libp2p test suite just try to run these tests as part of CI. I know we'd need to overcome some hurdles of what happens during breaking change releases, but this might be an opportunity to trial that flow with the 0.28 release and maybe just do the DHT to start with. If we're able to make that a smooth process, we could do that for the other modules.
This gets us a step in that direction, so I am good with having it as a dev dep.