From e5a00821c52a9d06f95d1fee54dfd24e9aa2a893 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 31 Mar 2020 17:46:23 +0200 Subject: [PATCH] chore: use new peer store api BREAKING CHANGE: uses new peer-store api --- package.json | 2 +- src/content-routing/index.js | 9 ++---- src/index.js | 12 +++++--- src/peer-routing/index.js | 42 ++++++++++++-------------- src/query/workerQueue.js | 1 - src/rpc/handlers/add-provider.js | 3 +- src/rpc/handlers/get-providers.js | 8 +---- src/rpc/handlers/get-value.js | 2 +- test/kad-dht.spec.js | 12 ++++---- test/query/index.spec.js | 4 +-- test/rpc/handlers/add-provider.spec.js | 4 +-- test/rpc/handlers/get-value.spec.js | 2 +- test/rpc/index.spec.js | 2 +- test/simulation/index.js | 4 +-- test/utils/test-dht.js | 4 +-- 15 files changed, 51 insertions(+), 60 deletions(-) diff --git a/package.json b/package.json index d3f33572..4607d91f 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "delay": "^4.3.0", "dirty-chai": "^2.0.1", "it-pair": "^1.0.0", + "libp2p": "libp2p/js-libp2p#chore/deprecate-old-peer-store-api", "lodash": "^4.17.11", "lodash.random": "^3.2.0", "lodash.range": "^3.2.0", @@ -83,7 +84,6 @@ "p-each-series": "^2.1.0", "p-map-series": "^2.1.0", "p-retry": "^4.2.0", - "peer-book": "~0.9.2", "sinon": "^9.0.0" }, "contributors": [ diff --git a/src/content-routing/index.js b/src/content-routing/index.js index ee836c8d..7754a39d 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -80,12 +80,7 @@ module.exports = (dht) => { const provs = await dht.providers.getProviders(key) provs.forEach((id) => { - let info - if (dht.peerStore.has(id)) { - info = dht.peerStore.get(id) - } else { - info = dht.peerStore.put(new PeerInfo(id)) - } + const info = dht.peerStore.get(id) || new PeerInfo(id) out.push(info) }) @@ -113,7 +108,7 @@ module.exports = (dht) => { dht._log('(%s) found %s provider entries', dht.peerInfo.id.toB58String(), provs.length) provs.forEach((prov) => { - pathProviders.push(dht.peerStore.put(prov)) + pathProviders.push(prov) }) // hooray we have all that we want diff --git a/src/index.js b/src/index.js index 51d08f61..21f295a2 100644 --- a/src/index.js +++ b/src/index.js @@ -321,14 +321,18 @@ class KadDHT extends EventEmitter { */ async _nearestPeersToQuery (msg) { const key = await utils.convertBuffer(msg.key) - const ids = this.routingTable.closestPeers(key, this.kBucketSize) return ids.map((p) => { - if (this.peerStore.has(p)) { - return this.peerStore.get(p) + const peer = this.peerStore.get(p) + const peerInfo = new PeerInfo(p) + + if (peer) { + peer.protocols.forEach((p) => peerInfo.protocols.add(p)) + peer.multiaddrInfos.forEach((mi) => peerInfo.multiaddrs.add(mi.multiaddr)) } - return this.peerStore.put(new PeerInfo(p)) + + return peerInfo }) } diff --git a/src/peer-routing/index.js b/src/peer-routing/index.js index 291999ea..ad8b587e 100644 --- a/src/peer-routing/index.js +++ b/src/peer-routing/index.js @@ -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()) } }) 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 } diff --git a/src/query/workerQueue.js b/src/query/workerQueue.js index 7bd11450..cc2e3516 100644 --- a/src/query/workerQueue.js +++ b/src/query/workerQueue.js @@ -244,7 +244,6 @@ class WorkerQueue { if (this.dht._isSelf(closer.id)) { return } - closer = this.dht.peerStore.put(closer) this.dht._peerDiscovered(closer) await this.path.addPeerToQuery(closer.id) })) diff --git a/src/rpc/handlers/add-provider.js b/src/rpc/handlers/add-provider.js index a95961d2..f149f19a 100644 --- a/src/rpc/handlers/add-provider.js +++ b/src/rpc/handlers/add-provider.js @@ -44,7 +44,8 @@ module.exports = (dht) => { log('received provider %s for %s (addrs %s)', peer.id.toB58String(), cid.toBaseEncodedString(), pi.multiaddrs.toArray().map((m) => m.toString())) if (!dht._isSelf(pi.id)) { - dht.peerStore.put(pi) + // Add known address to peer store + dht.peerStore.addressBook.add(pi.id, pi.multiaddrs.toArray()) return dht.providers.addProvider(cid, pi.id) } }) diff --git a/src/rpc/handlers/get-providers.js b/src/rpc/handlers/get-providers.js index fc5208a2..cc36a03a 100644 --- a/src/rpc/handlers/get-providers.js +++ b/src/rpc/handlers/get-providers.js @@ -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)) - }) + const providers = peers.map((p) => new PeerInfo(p)) if (has) { providers.push(dht.peerInfo) diff --git a/src/rpc/handlers/get-value.js b/src/rpc/handlers/get-value.js index ee9524a1..25843244 100644 --- a/src/rpc/handlers/get-value.js +++ b/src/rpc/handlers/get-value.js @@ -35,7 +35,7 @@ module.exports = (dht) => { if (dht._isSelf(id)) { info = dht.peerInfo - } else if (dht.peerStore.has(id)) { + } else { info = dht.peerStore.get(id) } diff --git a/test/kad-dht.spec.js b/test/kad-dht.spec.js index f23b2f1e..ed231502 100644 --- a/test/kad-dht.spec.js +++ b/test/kad-dht.spec.js @@ -648,7 +648,7 @@ describe('KadDHT', () => { const dhts = await tdht.spawn(2) const ids = dhts.map((d) => d.peerInfo.id) - dhts[0].peerStore.put(dhts[1].peerInfo) + dhts[0].peerStore.addressBook.add(dhts[1].peerInfo.id, dhts[1].peerInfo.multiaddrs.toArray()) const key = await dhts[0].getPublicKey(ids[1]) expect(key).to.eql(dhts[1].peerInfo.id.pubKey) @@ -671,7 +671,7 @@ describe('KadDHT', () => { await tdht.connect(dhts[0], dhts[1]) // remove the pub key to be sure it is fetched - dhts[0].peerStore.put(dhts[1].peerInfo, true) + dhts[0].peerStore.addressBook.add(dhts[1].peerInfo.id, dhts[1].peerInfo.multiaddrs.toArray()) const key = await dhts[0].getPublicKey(ids[1]) expect(key.equals(dhts[1].peerInfo.id.pubKey)).to.eql(true) @@ -694,7 +694,7 @@ describe('KadDHT', () => { it('_nearestPeersToQuery', async () => { const [dht] = await tdht.spawn(1) - dht.peerStore.put(peerInfos[1]) + dht.peerStore.addressBook.add(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) await dht._add(peerInfos[1]) const res = await dht._nearestPeersToQuery({ key: 'hello' }) expect(res).to.be.eql([peerInfos[1]]) @@ -703,8 +703,8 @@ describe('KadDHT', () => { it('_betterPeersToQuery', async () => { const [dht] = await tdht.spawn(1) - dht.peerStore.put(peerInfos[1]) - dht.peerStore.put(peerInfos[2]) + dht.peerStore.addressBook.add(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) + dht.peerStore.addressBook.add(peerInfos[2].id, peerInfos[2].multiaddrs.toArray()) await dht._add(peerInfos[1]) await dht._add(peerInfos[2]) @@ -769,7 +769,7 @@ describe('KadDHT', () => { it('_verifyRecordLocally', async () => { const [dht] = await tdht.spawn(1) - dht.peerStore.put(peerInfos[1]) + dht.peerStore.addressBook.add(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) const record = new Record( Buffer.from('hello'), diff --git a/test/query/index.spec.js b/test/query/index.spec.js index aa92f699..c6d2b123 100644 --- a/test/query/index.spec.js +++ b/test/query/index.spec.js @@ -8,7 +8,7 @@ chai.use(require('chai-checkmark')) const expect = chai.expect const sinon = require('sinon') const delay = require('delay') -const PeerBook = require('peer-book') +const PeerStore = require('libp2p/src/peer-store') const Query = require('../../src/query') const Path = require('../../src/query/path') @@ -46,7 +46,7 @@ describe('Query', () => { }) before('create a dht', () => { - const peerStore = new PeerBook() + const peerStore = new PeerStore() dht = new DHT({ dialer: {}, peerStore, diff --git a/test/rpc/handlers/add-provider.spec.js b/test/rpc/handlers/add-provider.spec.js index 5ab9b35b..606982c9 100644 --- a/test/rpc/handlers/add-provider.spec.js +++ b/test/rpc/handlers/add-provider.spec.js @@ -83,7 +83,7 @@ describe('rpc - handlers - AddProvider', () => { const bookEntry = dht.peerStore.get(provider.id) // Favour peerInfo from payload over peerInfo from sender - expect(bookEntry.multiaddrs.toArray()).to.eql( + expect(bookEntry.multiaddrInfos.map((mi) => mi.multiaddr)).to.eql( provider.multiaddrs.toArray() ) }) @@ -100,7 +100,7 @@ describe('rpc - handlers - AddProvider', () => { const provs = await dht.providers.getProviders(cid) - expect(dht.peerStore.has(provider.id)).to.equal(false) + expect(dht.peerStore.get(provider.id)).to.equal(undefined) expect(provs).to.have.length(1) expect(provs[0].id).to.eql(provider.id.id) }) diff --git a/test/rpc/handlers/get-value.spec.js b/test/rpc/handlers/get-value.spec.js index 417b7c10..08eb1fad 100644 --- a/test/rpc/handlers/get-value.spec.js +++ b/test/rpc/handlers/get-value.spec.js @@ -87,7 +87,7 @@ describe('rpc - handlers - GetValue', () => { const msg = new Message(T, key, 0) - dht.peerStore.put(other) + dht.peerStore.addressBook.add(other.id, other.multiaddrs.toArray()) await dht._add(other) const response = await handler(dht)(peers[0], msg) expect(response.record).to.exist() diff --git a/test/rpc/index.spec.js b/test/rpc/index.spec.js index d4e59a28..15e484a2 100644 --- a/test/rpc/index.spec.js +++ b/test/rpc/index.spec.js @@ -29,7 +29,7 @@ describe('rpc', () => { const defer = pDefer() const [dht] = await tdht.spawn(1) - dht.peerStore.put(peerInfos[1]) + dht.peerStore.addressBook.set(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) const msg = new Message(Message.TYPES.GET_VALUE, Buffer.from('hello'), 5) diff --git a/test/simulation/index.js b/test/simulation/index.js index eb7487bf..967f3b72 100644 --- a/test/simulation/index.js +++ b/test/simulation/index.js @@ -3,7 +3,7 @@ /* eslint-disable no-console */ 'use strict' -const PeerBook = require('peer-book') +const PeerStore = require('libp2p/src/peer-store') const PeerId = require('peer-id') const PeerInfo = require('peer-info') const multihashes = require('multihashes') @@ -85,7 +85,7 @@ async function setup () { async function GetClosestPeersSimulation () { const dht = new DHT({ _peerInfo: ourPeerInfo, - _peerBook: new PeerBook(), + _peerBook: new PeerStore(), handle: () => {}, on: () => {} }, { diff --git a/test/utils/test-dht.js b/test/utils/test-dht.js index 0acf3ec6..8ff07eae 100644 --- a/test/utils/test-dht.js +++ b/test/utils/test-dht.js @@ -1,6 +1,6 @@ 'use strict' -const PeerBook = require('peer-book') +const PeerStore = require('libp2p/src/peer-store') const pRetry = require('p-retry') const delay = require('delay') @@ -27,7 +27,7 @@ class TestDHT { async _spawnOne (index, options = {}) { const regRecord = {} - const peerStore = new PeerBook() + const peerStore = new PeerStore() // Disable random walk by default for more controlled testing options = {