diff --git a/package.json b/package.json index db27f33d..0c260c6d 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "it-pipe": "^1.1.0", "k-bucket": "^5.0.0", "libp2p-crypto": "~0.17.1", - "libp2p-interfaces": "^0.2.3", + "libp2p-interfaces": "libp2p/js-interfaces#v0.3.x", "libp2p-record": "~0.7.0", "multihashes": "~0.4.15", "multihashing-async": "~0.8.0", @@ -60,7 +60,6 @@ "p-timeout": "^3.2.0", "p-times": "^2.1.0", "peer-id": "~0.13.5", - "peer-info": "~0.17.0", "promise-to-callback": "^1.0.0", "protons": "^1.0.1", "streaming-iterables": "^4.1.1", @@ -76,7 +75,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", + "libp2p": "libp2p/js-libp2p#0.28.x", "lodash": "^4.17.11", "lodash.random": "^3.2.0", "lodash.range": "^3.2.0", diff --git a/src/content-fetching/index.js b/src/content-fetching/index.js index 5ce3dabf..3ca14dd3 100644 --- a/src/content-fetching/index.js +++ b/src/content-fetching/index.js @@ -196,7 +196,7 @@ module.exports = (dht) => { if (localRec) { vals.push({ val: localRec.value, - from: dht.peerInfo.id + from: dht.peerId }) } diff --git a/src/content-routing/index.js b/src/content-routing/index.js index 7754a39d..18d05ff0 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -3,8 +3,6 @@ const errcode = require('err-code') const pTimeout = require('p-timeout') -const PeerInfo = require('peer-info') - const c = require('../constants') const LimitedPeerList = require('../peer-list/limited-peer-list') const Message = require('../message') @@ -38,10 +36,12 @@ module.exports = (dht) => { const errors = [] // Add peer as provider - await dht.providers.addProvider(key, dht.peerInfo.id) + await dht.providers.addProvider(key, dht.peerId) const msg = new Message(Message.TYPES.ADD_PROVIDER, key.buffer, 0) - msg.providerPeers = [dht.peerInfo] + msg.providerPeers = [{ + id: dht.peerId + }] // Notify closest peers for await (const peer of dht.getClosestPeers(key.buffer)) { @@ -58,7 +58,7 @@ module.exports = (dht) => { // This should be infrequent. This means a peer we previously connected // to failed to exchange the provide message. If getClosestPeers was an // iterator, we could continue to pull until we announce to kBucketSize peers. - throw errcode(`Failed to provide to ${errors.length} of ${dht.kBucketSize} peers`, 'ERR_SOME_PROVIDES_FAILED', { errors }) + throw errcode(new Error(`Failed to provide to ${errors.length} of ${dht.kBucketSize} peers`, 'ERR_SOME_PROVIDES_FAILED'), { errors }) } }, @@ -68,7 +68,7 @@ module.exports = (dht) => { * @param {Object} options - findProviders options * @param {number} options.timeout - how long the query should maximally run, in milliseconds (default: 60000) * @param {number} options.maxNumProviders - maximum number of providers to find - * @returns {AsyncIterable} + * @returns {AsyncIterable<{ id: PeerId, addrs: Multiaddr[] }>} */ async * findProviders (key, options = {}) { const providerTimeout = options.timeout || c.minute @@ -80,15 +80,18 @@ module.exports = (dht) => { const provs = await dht.providers.getProviders(key) provs.forEach((id) => { - const info = dht.peerStore.get(id) || new PeerInfo(id) - out.push(info) + const peerData = dht.peerStore.get(id) || {} + out.push({ + id: peerData.id || id, + addrs: (peerData.multiaddrInfos || []).map((mi) => mi.multiaddr) + }) }) // All done if (out.length >= n) { // yield values - for (const pInfo of out.toArray()) { - yield pInfo + for (const pData of out.toArray()) { + yield pData } return } @@ -105,10 +108,10 @@ module.exports = (dht) => { return async (peer) => { const msg = await findProvidersSingle(peer, key) const provs = msg.providerPeers - dht._log('(%s) found %s provider entries', dht.peerInfo.id.toB58String(), provs.length) + dht._log('(%s) found %s provider entries', dht.peerId.toB58String(), provs.length) provs.forEach((prov) => { - pathProviders.push(prov) + pathProviders.push({ id: prov.id }) }) // hooray we have all that we want @@ -147,8 +150,8 @@ module.exports = (dht) => { throw errcode(new Error('no providers found'), 'ERR_NOT_FOUND') } - for (const pInfo of out.toArray()) { - yield pInfo + for (const pData of out.toArray()) { + yield pData } } } diff --git a/src/index.js b/src/index.js index 953dd123..d262c0ed 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,6 @@ const errcode = require('err-code') const libp2pRecord = require('libp2p-record') const { MemoryDatastore } = require('interface-datastore') -const PeerInfo = require('peer-info') const RoutingTable = require('./routing') const utils = require('./utils') @@ -40,7 +39,7 @@ class KadDHT extends EventEmitter { * Create a new KadDHT. * @param {Object} props * @param {Dialer} props.dialer libp2p dialer instance - * @param {PeerInfo} props.peerInfo peer's peerInfo + * @param {PeerId} props.peerId peer's peerId * @param {PeerStore} props.peerStore libp2p peerStore * @param {Object} props.registrar libp2p registrar instance * @param {function} props.registrar.handle @@ -55,7 +54,7 @@ class KadDHT extends EventEmitter { */ constructor ({ dialer, - peerInfo, + peerId, peerStore, registrar, datastore = new MemoryDatastore(), @@ -78,10 +77,10 @@ class KadDHT extends EventEmitter { this.dialer = dialer /** - * Local peer info - * @type {PeerInfo} + * Local peer-id + * @type {PeerId} */ - this.peerInfo = peerInfo + this.peerId = peerId /** * Local PeerStore @@ -120,7 +119,7 @@ class KadDHT extends EventEmitter { * * @type {RoutingTable} */ - this.routingTable = new RoutingTable(this.peerInfo.id, this.kBucketSize) + this.routingTable = new RoutingTable(this.peerId, this.kBucketSize) /** * Reference to the datastore, uses an in-memory store if none given. @@ -134,7 +133,7 @@ class KadDHT extends EventEmitter { * * @type {Providers} */ - this.providers = new Providers(this.datastore, this.peerInfo.id) + this.providers = new Providers(this.datastore, this.peerId) this.validators = { pk: libp2pRecord.validator.validators.pk, @@ -148,7 +147,7 @@ class KadDHT extends EventEmitter { this.network = new Network(this) - this._log = utils.logger(this.peerInfo.id) + this._log = utils.logger(this.peerId) /** * Random walk management @@ -259,11 +258,11 @@ class KadDHT extends EventEmitter { * @param {Object} options - findProviders options * @param {number} options.timeout - how long the query should maximally run, in milliseconds (default: 60000) * @param {number} options.maxNumProviders - maximum number of providers to find - * @returns {AsyncIterable} + * @returns {AsyncIterable<{ id: PeerId, addrs: Multiaddr[] }>} */ async * findProviders (key, options = {}) { - for await (const pInfo of this.contentRouting.findProviders(key, options)) { - yield pInfo + for await (const peerData of this.contentRouting.findProviders(key, options)) { + yield peerData } } @@ -275,7 +274,7 @@ class KadDHT extends EventEmitter { * @param {PeerId} id * @param {Object} options - findPeer options * @param {number} options.timeout - how long the query should maximally run, in milliseconds (default: 60000) - * @returns {Promise} + * @returns {Promise<{ id: PeerId, addrs: Multiaddr[] }>} */ async findPeer (id, options = {}) { // eslint-disable-line require-await return this.peerRouting.findPeer(id, options) @@ -286,7 +285,7 @@ class KadDHT extends EventEmitter { * @param {Buffer} key * @param {Object} [options] * @param {boolean} [options.shallow] shallow query (default: false) - * @returns {AsyncIterable} + * @returns {AsyncIterable<{ id: PeerId, addrs: Multiaddr[] }>} */ async * getClosestPeers (key, options = { shallow: false }) { for await (const pId of this.peerRouting.getClosestPeers(key, options)) { @@ -319,7 +318,7 @@ class KadDHT extends EventEmitter { * the message. * * @param {Message} msg - * @returns {Promise>} + * @returns {Promise>} * @private */ async _nearestPeersToQuery (msg) { @@ -328,14 +327,11 @@ class KadDHT extends EventEmitter { return ids.map((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 { + id: p, + multiaddrs: peer ? peer.multiaddrInfos.map((mi) => mi.multiaddr) : [] } - - return peerInfo }) } @@ -344,12 +340,11 @@ class KadDHT extends EventEmitter { * than self. * * @param {Message} msg - * @param {PeerInfo} peer - * @returns {Promise>} + * @param {PeerId} peerId + * @returns {Promise>} * @private */ - - async _betterPeersToQuery (msg, peer) { + async _betterPeersToQuery (msg, peerId) { this._log('betterPeersToQuery') const closer = await this._nearestPeersToQuery(msg) @@ -360,7 +355,7 @@ class KadDHT extends EventEmitter { return false } - return !closer.id.isEqual(peer.id) + return !closer.id.isEqual(peerId) }) } @@ -411,14 +406,12 @@ class KadDHT extends EventEmitter { /** * Add the peer to the routing table and update it in the peerStore. - * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @returns {Promise} * @private */ - - async _add (peer) { - await this.routingTable.add(peer.id) + async _add (peerId) { + await this.routingTable.add(peerId) } /** @@ -445,7 +438,7 @@ class KadDHT extends EventEmitter { */ _isSelf (other) { - return other && this.peerInfo.id.id.equals(other.id) + return other && this.peerId.id.equals(other.id) } /** @@ -478,7 +471,7 @@ class KadDHT extends EventEmitter { * * @param {PeerId} peer * @param {Buffer} key - * @returns {Promise<{Record, Array} + * @returns {Promise<{Record, Array<{ id: PeerId, multiaddrs: Multiaddr[] }}>} * @private */ diff --git a/src/message/index.js b/src/message/index.js index 0271e6ac..74530f77 100644 --- a/src/message/index.js +++ b/src/message/index.js @@ -1,7 +1,7 @@ 'use strict' -const PeerInfo = require('peer-info') const PeerId = require('peer-id') +const multiaddr = require('multiaddr') const protons = require('protons') const { Record } = require('libp2p-record') @@ -97,25 +97,18 @@ Message.TYPES = MESSAGE_TYPE Message.CONNECTION_TYPES = CONNECTION_TYPE function toPbPeer (peer) { - const res = { + return { id: peer.id.id, - addrs: peer.multiaddrs.toArray().map((m) => m.buffer) + addrs: (peer.multiaddrs || []).map((m) => m && m.buffer), + connection: CONNECTION_TYPE.CONNECTED } - - if (peer.isConnected()) { - res.connection = CONNECTION_TYPE.CONNECTED - } else { - res.connection = CONNECTION_TYPE.NOT_CONNECTED - } - - return res } function fromPbPeer (peer) { - const info = new PeerInfo(new PeerId(peer.id)) - peer.addrs.forEach((a) => info.multiaddrs.add(a)) - - return info + return { + id: new PeerId(peer.id), + multiaddrs: peer.addrs.map((a) => multiaddr(a)) + } } module.exports = Message diff --git a/src/network.js b/src/network.js index 4bd8de7a..6e669ad1 100644 --- a/src/network.js +++ b/src/network.js @@ -25,7 +25,7 @@ class Network { constructor (self) { this.dht = self this.readMessageTimeout = c.READ_MESSAGE_TIMEOUT - this._log = utils.logger(this.dht.peerInfo.id, 'net') + this._log = utils.logger(this.dht.peerId, 'net') this._rpc = rpc(this.dht) this._onPeerConnected = this._onPeerConnected.bind(this) this._running = false @@ -96,12 +96,12 @@ class Network { /** * Registrar notifies a connection successfully with dht protocol. * @private - * @param {PeerInfo} peerInfo remote peer info + * @param {PeerId} peerId remote peer id * @returns {Promise} */ - async _onPeerConnected (peerInfo) { - await this.dht._add(peerInfo) - this._log('added to the routing table: %s', peerInfo.id.toB58String()) + async _onPeerConnected (peerId) { + await this.dht._add(peerId) + this._log('added to the routing table: %s', peerId.toB58String()) } /** diff --git a/src/peer-list/index.js b/src/peer-list/index.js index b846efd8..d0d1379e 100644 --- a/src/peer-list/index.js +++ b/src/peer-list/index.js @@ -1,7 +1,7 @@ 'use strict' /** - * A list of unique peer infos. + * A list of unique peers. */ class PeerList { constructor () { @@ -9,34 +9,34 @@ class PeerList { } /** - * Add a new info. Returns `true` if it was a new one + * Add a new peer. Returns `true` if it was a new one * - * @param {PeerInfo} info + * @param {PeerData} peerData * @returns {bool} */ - push (info) { - if (!this.has(info)) { - this.list.push(info) + push (peerData) { + if (!this.has(peerData.id)) { + this.list.push(peerData) return true } return false } /** - * Check if this PeerInfo is already in here. + * Check if this PeerData is already in here. * - * @param {PeerInfo} info + * @param {PeerId} peerId * @returns {bool} */ - has (info) { - const match = this.list.find((i) => i.id.isEqual(info.id)) + has (peerId) { + const match = this.list.find((i) => i.id.isEqual(peerId)) return Boolean(match) } /** * Get the list as an array. * - * @returns {Array} + * @returns {Array} */ toArray () { return this.list.slice() @@ -45,7 +45,7 @@ class PeerList { /** * Remove the last element * - * @returns {PeerInfo} + * @returns {PeerData} */ pop () { return this.list.pop() diff --git a/src/peer-list/limited-peer-list.js b/src/peer-list/limited-peer-list.js index 0431b699..3e390f7f 100644 --- a/src/peer-list/limited-peer-list.js +++ b/src/peer-list/limited-peer-list.js @@ -17,14 +17,14 @@ class LimitedPeerList extends PeerList { } /** - * Add a PeerInfo if it fits in the list + * Add a PeerData if it fits in the list * - * @param {PeerInfo} info + * @param {PeerData} peerData * @returns {bool} */ - push (info) { + push (peerData) { if (this.length < this.limit) { - return super.push(info) + return super.push(peerData) } return false } diff --git a/src/peer-routing/index.js b/src/peer-routing/index.js index ad8b587e..f7e52510 100644 --- a/src/peer-routing/index.js +++ b/src/peer-routing/index.js @@ -15,15 +15,22 @@ const utils = require('../utils') module.exports = (dht) => { /** * Look if we are connected to a peer with the given id. - * Returns the `PeerInfo` for it, if found, otherwise `undefined`. + * Returns its id and addresses, if found, otherwise `undefined`. * @param {PeerId} peer - * @returns {Promise} + * @returns {Promise<{ id: PeerId, addrs: Multiaddr[] }>} */ const findPeerLocal = async (peer) => { dht._log('findPeerLocal %s', peer.toB58String()) const p = await dht.routingTable.find(peer) - return p && dht.peerStore.get(p) + const peerData = p && dht.peerStore.get(p) + + if (peerData) { + return { + id: peerData.id, + addrs: peerData.multiaddrInfos.map((mi) => mi.multiaddr) + } + } } /** @@ -42,7 +49,7 @@ module.exports = (dht) => { * Find close peers for a given peer * @param {Buffer} key * @param {PeerId} peer - * @returns {Promise>} + * @returns {Promise>} * @private */ @@ -51,12 +58,11 @@ module.exports = (dht) => { const msg = await dht.peerRouting._findPeerSingle(peer, new PeerId(key)) return msg.closerPeers - .filter((pInfo) => !dht._isSelf(pInfo.id)) - .map((pInfo) => { - // Add known address to peer store - dht.peerStore.addressBook.add(pInfo.id, pInfo.multiaddrs.toArray()) + .filter((peerData) => !dht._isSelf(peerData.id)) + .map((peerData) => { + dht.peerStore.addressBook.add(peerData.id, peerData.multiaddrs) - return pInfo + return peerData }) } @@ -104,7 +110,7 @@ module.exports = (dht) => { * @param {PeerId} id * @param {Object} options - findPeer options * @param {number} options.timeout - how long the query should maximally run, in milliseconds (default: 60000) - * @returns {Promise} + * @returns {Promise<{ id: PeerId, addrs: Multiaddr[] }>} */ async findPeer (id, options = {}) { options.timeout = options.timeout || c.minute @@ -133,7 +139,10 @@ module.exports = (dht) => { if (peer) { dht._log('found in peerStore') - return peer + return { + id: peer.id, + addrs: peer.multiaddrInfos.map((mi) => mi.multiaddr) + } } } @@ -173,7 +182,7 @@ module.exports = (dht) => { result.paths.forEach((result) => { if (result.success) { success = true - dht.peerStore.addressBook.add(result.peer.id, result.peer.multiaddrs.toArray()) + dht.peerStore.addressBook.add(result.peer.id, result.peer.multiaddrs) } }) dht._log('findPeer %s: %s', id.toB58String(), success) @@ -181,7 +190,13 @@ module.exports = (dht) => { if (!success) { throw errcode(new Error('No peer found'), 'ERR_NOT_FOUND') } - return dht.peerStore.get(id) + + const peerData = dht.peerStore.get(id) + + return { + id: peerData.id, + addrs: peerData.multiaddrInfos.map((mi) => mi.multiaddr) + } }, /** @@ -232,10 +247,10 @@ module.exports = (dht) => { dht._log('getPublicKey %s', peer.toB58String()) // local check - const info = dht.peerStore.get(peer) - if (info && info.id.pubKey) { + const peerData = dht.peerStore.get(peer) + if (peerData && peerData.id.pubKey) { dht._log('getPublicKey: found local copy') - return info.id.pubKey + return peerData.id.pubKey } // try the node directly @@ -249,8 +264,8 @@ module.exports = (dht) => { pk = crypto.keys.unmarshalPublicKey(value) } - info.id = new PeerId(peer.id, null, pk) - dht.peerStore.addressBook.add(info.id, info.multiaddrs.toArray()) + peerData.id = new PeerId(peer.id, null, pk) + dht.peerStore.addressBook.add(peerData.id, peerData.multiaddrs.toArray()) return pk } diff --git a/src/query/index.js b/src/query/index.js index 3c6bbaa6..a8e49161 100644 --- a/src/query/index.js +++ b/src/query/index.js @@ -38,7 +38,7 @@ class Query { this.dht = dht this.key = key this.makePath = makePath - this._log = utils.logger(this.dht.peerInfo.id, 'query:' + mh.toB58String(key)) + this._log = utils.logger(this.dht.peerId, 'query:' + mh.toB58String(key)) this.running = false diff --git a/src/query/workerQueue.js b/src/query/workerQueue.js index 0c886b88..3f294c87 100644 --- a/src/query/workerQueue.js +++ b/src/query/workerQueue.js @@ -244,7 +244,7 @@ class WorkerQueue { if (this.dht._isSelf(closer.id)) { return } - this.dht._peerDiscovered(closer.id, closer.multiaddrs.toArray()) + this.dht._peerDiscovered(closer.id, closer.multiaddrs) await this.path.addPeerToQuery(closer.id) })) } diff --git a/src/random-walk.js b/src/random-walk.js index 50e4e560..a9207010 100644 --- a/src/random-walk.js +++ b/src/random-walk.js @@ -32,7 +32,7 @@ class RandomWalk { ...options } - this.log = logger(dht.peerInfo.id, 'random-walk') + this.log = logger(dht.peerId, 'random-walk') this._timeoutId = undefined } diff --git a/src/rpc/handlers/add-provider.js b/src/rpc/handlers/add-provider.js index f149f19a..2ba7e71e 100644 --- a/src/rpc/handlers/add-provider.js +++ b/src/rpc/handlers/add-provider.js @@ -6,15 +6,15 @@ const errcode = require('err-code') const utils = require('../../utils') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc:add-provider') + const log = utils.logger(dht.peerId, 'rpc:add-provider') /** * Process `AddProvider` DHT messages. * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Promise} */ - return async function addProvider (peer, msg) { // eslint-disable-line require-await + return async function addProvider (peerId, msg) { // eslint-disable-line require-await log('start') if (!msg.key || msg.key.length === 0) { @@ -31,21 +31,21 @@ module.exports = (dht) => { msg.providerPeers.forEach((pi) => { // Ignore providers not from the originator - if (!pi.id.isEqual(peer.id)) { - log('invalid provider peer %s from %s', pi.id.toB58String(), peer.id.toB58String()) + if (!pi.id.isEqual(peerId)) { + log('invalid provider peer %s from %s', pi.id.toB58String(), peerId.toB58String()) return } - if (pi.multiaddrs.size < 1) { - log('no valid addresses for provider %s. Ignore', peer.id.toB58String()) + if (pi.multiaddrs.length < 1) { + log('no valid addresses for provider %s. Ignore', peerId.toB58String()) return } - log('received provider %s for %s (addrs %s)', peer.id.toB58String(), cid.toBaseEncodedString(), pi.multiaddrs.toArray().map((m) => m.toString())) + log('received provider %s for %s (addrs %s)', peerId.toB58String(), cid.toBaseEncodedString(), pi.multiaddrs.map((m) => m.toString())) if (!dht._isSelf(pi.id)) { // Add known address to peer store - dht.peerStore.addressBook.add(pi.id, pi.multiaddrs.toArray()) + dht.peerStore.addressBook.add(pi.id, pi.multiaddrs) return dht.providers.addProvider(cid, pi.id) } }) @@ -56,6 +56,6 @@ module.exports = (dht) => { // we can't find any valid providers in the payload. // https://github.com/libp2p/js-libp2p-kad-dht/pull/127 // https://github.com/libp2p/js-libp2p-kad-dht/issues/128 - return dht.providers.addProvider(cid, peer.id) + return dht.providers.addProvider(cid, peerId) } } diff --git a/src/rpc/handlers/find-node.js b/src/rpc/handlers/find-node.js index af0102bc..6fee380a 100644 --- a/src/rpc/handlers/find-node.js +++ b/src/rpc/handlers/find-node.js @@ -4,23 +4,25 @@ const Message = require('../../message') const utils = require('../../utils') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc:find-node') + const log = utils.logger(dht.peerId, 'rpc:find-node') /** * Process `FindNode` DHT messages. * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Promise} */ - return async function findNode (peer, msg) { + return async function findNode (peerId, msg) { log('start') let closer - if (msg.key.equals(dht.peerInfo.id.id)) { - closer = [dht.peerInfo] + if (msg.key.equals(dht.peerId.id)) { + closer = [{ + id: dht.peerId + }] } else { - closer = await dht._betterPeersToQuery(msg, peer) + closer = await dht._betterPeersToQuery(msg, peerId) } const response = new Message(msg.type, Buffer.alloc(0), msg.clusterLevel) @@ -28,7 +30,7 @@ module.exports = (dht) => { if (closer.length > 0) { response.closerPeers = closer } else { - log('handle FindNode %s: could not find anything', peer.id.toB58String()) + log('handle FindNode %s: could not find anything', peerId.toB58String()) } return response diff --git a/src/rpc/handlers/get-providers.js b/src/rpc/handlers/get-providers.js index cc36a03a..35cd62c6 100644 --- a/src/rpc/handlers/get-providers.js +++ b/src/rpc/handlers/get-providers.js @@ -1,23 +1,22 @@ 'use strict' const CID = require('cids') -const PeerInfo = require('peer-info') const errcode = require('err-code') const Message = require('../../message') const utils = require('../../utils') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc:get-providers') + const log = utils.logger(dht.peerId, 'rpc:get-providers') /** * Process `GetProviders` DHT messages. * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Promise} */ - return async function getProviders (peer, msg) { + return async function getProviders (peerId, msg) { let cid try { cid = new CID(msg.key) @@ -31,26 +30,29 @@ module.exports = (dht) => { const [has, peers, closer] = await Promise.all([ dht.datastore.has(dsKey), dht.providers.getProviders(cid), - dht._betterPeersToQuery(msg, peer) + dht._betterPeersToQuery(msg, peerId) ]) - const providers = peers.map((p) => new PeerInfo(p)) + const providerPeers = peers.map((peerId) => ({ id: peerId })) + const closerPeers = closer.map((c) => ({ id: c.id })) if (has) { - providers.push(dht.peerInfo) + providerPeers.push({ + id: dht.peerId + }) } const response = new Message(msg.type, msg.key, msg.clusterLevel) - if (providers.length > 0) { - response.providerPeers = providers + if (providerPeers.length > 0) { + response.providerPeers = providerPeers } - if (closer.length > 0) { - response.closerPeers = closer + if (closerPeers.length > 0) { + response.closerPeers = closerPeers } - log('got %s providers %s closerPeers', providers.length, closer.length) + log('got %s providers %s closerPeers', providerPeers.length, closerPeers.length) return response } } diff --git a/src/rpc/handlers/get-value.js b/src/rpc/handlers/get-value.js index 25843244..d4d84db0 100644 --- a/src/rpc/handlers/get-value.js +++ b/src/rpc/handlers/get-value.js @@ -8,16 +8,16 @@ const Message = require('../../message') const utils = require('../../utils') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc:get-value') + const log = utils.logger(dht.peerId, 'rpc:get-value') /** * Process `GetValue` DHT messages. * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Promise} */ - return async function getValue (peer, msg) { + return async function getValue (peerId, msg) { const key = msg.key log('key: %b', key) @@ -30,25 +30,26 @@ module.exports = (dht) => { if (utils.isPublicKeyKey(key)) { log('is public key') - const id = utils.fromPublicKeyKey(key) - let info + const idFromKey = utils.fromPublicKeyKey(key) + let id - if (dht._isSelf(id)) { - info = dht.peerInfo + if (dht._isSelf(idFromKey)) { + id = dht.peerId } else { - info = dht.peerStore.get(id) + const peerData = dht.peerStore.get(idFromKey) + id = peerData && peerData.id } - if (info && info.id.pubKey) { + if (id && id.pubKey) { log('returning found public key') - response.record = new Record(key, info.id.pubKey.bytes) + response.record = new Record(key, id.pubKey.bytes) return response } } const [record, closer] = await Promise.all([ dht._checkLocalDatastore(key), - dht._betterPeersToQuery(msg, peer) + dht._betterPeersToQuery(msg, peerId) ]) if (record) { diff --git a/src/rpc/handlers/index.js b/src/rpc/handlers/index.js index 5c0d909c..2e6aa91e 100644 --- a/src/rpc/handlers/index.js +++ b/src/rpc/handlers/index.js @@ -17,7 +17,7 @@ module.exports = (dht) => { * * @param {number} type * - * @returns {function(PeerInfo, Message, function(Error, Message))} + * @returns {function(PeerId, Message, function(Error, Message))} * * @private */ diff --git a/src/rpc/handlers/ping.js b/src/rpc/handlers/ping.js index dfb3c02b..976d2095 100644 --- a/src/rpc/handlers/ping.js +++ b/src/rpc/handlers/ping.js @@ -3,17 +3,17 @@ const utils = require('../../utils') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc:ping') + const log = utils.logger(dht.peerId, 'rpc:ping') /** * Process `Ping` DHT messages. * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Message} */ - return function ping (peer, msg) { - log('from %s', peer.id.toB58String()) + return function ping (peerId, msg) { + log('from %s', peerId.toB58String()) return msg } } diff --git a/src/rpc/handlers/put-value.js b/src/rpc/handlers/put-value.js index bbdd69ca..95a4f252 100644 --- a/src/rpc/handlers/put-value.js +++ b/src/rpc/handlers/put-value.js @@ -4,23 +4,23 @@ const utils = require('../../utils') const errcode = require('err-code') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc:put-value') + const log = utils.logger(dht.peerId, 'rpc:put-value') /** * Process `PutValue` DHT messages. * - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Promise} */ - return async function putValue (peer, msg) { + return async function putValue (peerId, msg) { const key = msg.key log('key: %b', key) const record = msg.record if (!record) { - const errMsg = `Empty record from: ${peer.id.toB58String()}` + const errMsg = `Empty record from: ${peerId.toB58String()}` log.error(errMsg) throw errcode(new Error(errMsg), 'ERR_EMPTY_RECORD') diff --git a/src/rpc/index.js b/src/rpc/index.js index 795b7ba3..568c0f12 100644 --- a/src/rpc/index.js +++ b/src/rpc/index.js @@ -2,30 +2,29 @@ const pipe = require('it-pipe') const lp = require('it-length-prefixed') -const PeerInfo = require('peer-info') const Message = require('../message') const handlers = require('./handlers') const utils = require('../utils') module.exports = (dht) => { - const log = utils.logger(dht.peerInfo.id, 'rpc') + const log = utils.logger(dht.peerId, 'rpc') const getMessageHandler = handlers(dht) /** * Process incoming DHT messages. - * @param {PeerInfo} peer + * @param {PeerId} peerId * @param {Message} msg * @returns {Promise} * * @private */ - async function handleMessage (peer, msg) { + async function handleMessage (peerId, msg) { // get handler & execute it const handler = getMessageHandler(msg.type) try { - await dht._add(peer) + await dht._add(peerId) } catch (err) { log.error('Failed to update the kbucket store', err) } @@ -35,7 +34,7 @@ module.exports = (dht) => { return } - return handler(peer, msg) + return handler(peerId, msg) } /** @@ -46,15 +45,15 @@ module.exports = (dht) => { * @returns {Promise} */ return async function onIncomingStream ({ stream, connection }) { - const peerInfo = await PeerInfo.create(connection.remotePeer) + const peerId = connection.remotePeer try { - await dht._add(peerInfo) + await dht._add(peerId) } catch (err) { log.error(err) } - const idB58Str = peerInfo.id.toB58String() + const idB58Str = peerId.toB58String() log('from: %s', idB58Str) await pipe( @@ -64,7 +63,7 @@ module.exports = (dht) => { for await (const msg of source) { // handle the message const desMessage = Message.deserialize(msg.slice()) - const res = await handleMessage(peerInfo, desMessage) + const res = await handleMessage(peerId, desMessage) // Not all handlers will return a response if (res) { diff --git a/test/kad-dht.spec.js b/test/kad-dht.spec.js index ed231502..59b6cfeb 100644 --- a/test/kad-dht.spec.js +++ b/test/kad-dht.spec.js @@ -6,6 +6,8 @@ chai.use(require('dirty-chai')) chai.use(require('chai-checkmark')) const expect = chai.expect const sinon = require('sinon') + +const multiaddr = require('multiaddr') const { Record } = require('libp2p-record') const errcode = require('err-code') @@ -18,24 +20,24 @@ const kadUtils = require('../src/utils') const c = require('../src/constants') const Message = require('../src/message') -const createPeerInfo = require('./utils/create-peer-info') +const createPeerId = require('./utils/create-peer-id') const createValues = require('./utils/create-values') const TestDHT = require('./utils/test-dht') const { countDiffPeers } = require('./utils') describe('KadDHT', () => { - let peerInfos + let peerIds let values before(async function () { this.timeout(10 * 1000) const res = await Promise.all([ - createPeerInfo(3), + createPeerId(3), createValues(20) ]) - peerInfos = res[0] + peerIds = res[0] values = res[1] }) @@ -55,7 +57,7 @@ describe('KadDHT', () => { kBucketSize: 5 }) - expect(dht).to.have.property('peerInfo') + expect(dht).to.have.property('peerId') expect(dht).to.have.property('kBucketSize', 5) expect(dht).to.have.property('routingTable') }) @@ -70,7 +72,7 @@ describe('KadDHT', () => { } }) - expect(dht).to.have.property('peerInfo') + expect(dht).to.have.property('peerId') expect(dht).to.have.property('routingTable') expect(dht.validators).to.have.property('ipns') expect(dht.selectors).to.have.property('ipns') @@ -369,7 +371,7 @@ describe('KadDHT', () => { expect(resB).to.eql(valueA) expect(dhtASpy.callCount).to.eql(1) - expect(dhtASpy.getCall(0).args[2].isEqual(dhtB.peerInfo.id)).to.eql(true) // inform B + expect(dhtASpy.getCall(0).args[2].isEqual(dhtB.peerId)).to.eql(true) // inform B return tdht.teardown() }) @@ -410,7 +412,7 @@ describe('KadDHT', () => { const stubs = [ // Simulate returning a peer id to query - sinon.stub(dht.routingTable, 'closestPeers').returns([peerInfos[1].id]), + sinon.stub(dht.routingTable, 'closestPeers').returns([peerIds[1]]), // Simulate going out to the network and returning the record sinon.stub(dht, '_getValueOrPeers').callsFake(async () => ({ record: rec })) // eslint-disable-line require-await ] @@ -433,7 +435,7 @@ describe('KadDHT', () => { const tdht = new TestDHT() const dhts = await tdht.spawn(4) - const ids = dhts.map((d) => d.peerInfo.id) + const ids = dhts.map((d) => d.peerId) const idsB58 = ids.map(id => id.toB58String()) sinon.spy(dhts[3].network, 'sendMessage') @@ -509,7 +511,7 @@ describe('KadDHT', () => { const tdht = new TestDHT() const [dht] = await tdht.spawn(1) - sinon.stub(dht.providers, 'getProviders').returns([dht.peerInfo.id]) + sinon.stub(dht.providers, 'getProviders').returns([dht.peerId]) // Find provider const res = await all(dht.findProviders(val.cid, { maxNumProviders: 1 })) @@ -536,7 +538,7 @@ describe('KadDHT', () => { tdht.connect(dhts[2], dhts[3]) ]) - const ids = dhts.map((d) => d.peerInfo.id) + const ids = dhts.map((d) => d.peerId) const res = await dhts[0].findPeer(ids[3], { timeout: 1000 }) expect(res.id.isEqual(ids[3])).to.eql(true) @@ -551,7 +553,7 @@ describe('KadDHT', () => { const tdht = new TestDHT() const dhts = await tdht.spawn(nDHTs) - const dhtsById = new Map(dhts.map((d) => [d.peerInfo.id, d])) + const dhtsById = new Map(dhts.map((d) => [d.peerId, d])) const ids = [...dhtsById.keys()] // The origin node for the FIND_PEER query @@ -596,7 +598,7 @@ describe('KadDHT', () => { rtableSet[p.toB58String()] = true }) - const guyIndex = ids.findIndex(i => i.id.equals(guy.peerInfo.id.id)) + const guyIndex = ids.findIndex(i => i.id.equals(guy.peerId.id)) const otherIds = ids.slice(0, guyIndex).concat(ids.slice(guyIndex + 1)) // Make the query @@ -647,14 +649,12 @@ describe('KadDHT', () => { const tdht = new TestDHT() const dhts = await tdht.spawn(2) - const ids = dhts.map((d) => d.peerInfo.id) - dhts[0].peerStore.addressBook.add(dhts[1].peerInfo.id, dhts[1].peerInfo.multiaddrs.toArray()) + const ids = dhts.map((d) => d.peerId) + dhts[0].peerStore.addressBook.add(dhts[1].peerId, [multiaddr('/ip4/160.1.1.1/tcp/80')]) const key = await dhts[0].getPublicKey(ids[1]) - expect(key).to.eql(dhts[1].peerInfo.id.pubKey) + expect(key).to.eql(dhts[1].peerId.pubKey) - // TODO: Switch not closing well, but it will be removed - // (invalid transition: STOPPED -> done) await delay(100) return tdht.teardown() @@ -666,15 +666,14 @@ describe('KadDHT', () => { const tdht = new TestDHT() const dhts = await tdht.spawn(2) - const ids = dhts.map((d) => d.peerInfo.id) + const ids = dhts.map((d) => d.peerId) await tdht.connect(dhts[0], dhts[1]) - // remove the pub key to be sure it is fetched - dhts[0].peerStore.addressBook.add(dhts[1].peerInfo.id, dhts[1].peerInfo.multiaddrs.toArray()) + dhts[0].peerStore.addressBook.add(dhts[1].peerId, [multiaddr('/ip4/160.1.1.1/tcp/80')]) const key = await dhts[0].getPublicKey(ids[1]) - expect(key.equals(dhts[1].peerInfo.id.pubKey)).to.eql(true) + expect(key.equals(dhts[1].peerId.pubKey)).to.eql(true) return tdht.teardown() }) @@ -694,23 +693,22 @@ describe('KadDHT', () => { it('_nearestPeersToQuery', async () => { const [dht] = await tdht.spawn(1) - dht.peerStore.addressBook.add(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) - await dht._add(peerInfos[1]) + await dht._add(peerIds[1]) const res = await dht._nearestPeersToQuery({ key: 'hello' }) - expect(res).to.be.eql([peerInfos[1]]) + expect(res).to.be.eql([{ + id: peerIds[1], + multiaddrs: [] + }]) }) it('_betterPeersToQuery', async () => { const [dht] = await tdht.spawn(1) - 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(peerIds[1]) + await dht._add(peerIds[2]) + const res = await dht._betterPeersToQuery({ key: 'hello' }, peerIds[1]) - await dht._add(peerInfos[1]) - await dht._add(peerInfos[2]) - const res = await dht._betterPeersToQuery({ key: 'hello' }, peerInfos[1]) - - expect(res).to.be.eql([peerInfos[2]]) + expect(res[0].id).to.be.eql(peerIds[2]) }) describe('_checkLocalDatastore', () => { @@ -768,9 +766,6 @@ describe('KadDHT', () => { it('_verifyRecordLocally', async () => { const [dht] = await tdht.spawn(1) - - dht.peerStore.addressBook.add(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) - const record = new Record( Buffer.from('hello'), Buffer.from('world') @@ -855,7 +850,7 @@ describe('KadDHT', () => { const tdht = new TestDHT() const dhts = await tdht.spawn(4) - const ids = dhts.map((d) => d.peerInfo.id) + const ids = dhts.map((d) => d.peerId) await Promise.all([ tdht.connect(dhts[0], dhts[1]), tdht.connect(dhts[1], dhts[2]), diff --git a/test/kad-utils.spec.js b/test/kad-utils.spec.js index a460db10..1eb49e81 100644 --- a/test/kad-utils.spec.js +++ b/test/kad-utils.spec.js @@ -9,7 +9,7 @@ const PeerId = require('peer-id') const distance = require('xor-distance') const utils = require('../src/utils') -const createPeerInfo = require('./utils/create-peer-info') +const createPeerId = require('./utils/create-peer-id') describe('kad utils', () => { describe('bufferToKey', () => { @@ -51,7 +51,7 @@ describe('kad utils', () => { }) describe('sortClosestPeers', () => { - it('sorts a list of PeerInfos', async () => { + it('sorts a list of PeerIds', async () => { const rawIds = [ '11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31', '11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32', @@ -102,9 +102,9 @@ describe('kad utils', () => { describe('keyForPublicKey', () => { it('works', async () => { - const peers = await createPeerInfo(1) - expect(utils.keyForPublicKey(peers[0].id)) - .to.eql(Buffer.concat([Buffer.from('/pk/'), peers[0].id.id])) + const peers = await createPeerId(1) + expect(utils.keyForPublicKey(peers[0])) + .to.eql(Buffer.concat([Buffer.from('/pk/'), peers[0].id])) }) }) @@ -112,9 +112,8 @@ describe('kad utils', () => { it('round trips', async function () { this.timeout(40 * 1000) - const peers = await createPeerInfo(50) - peers.forEach((p, i) => { - const id = p.id + const peers = await createPeerId(50) + peers.forEach((id, i) => { expect(utils.isPublicKeyKey(utils.keyForPublicKey(id))).to.eql(true) expect(utils.fromPublicKeyKey(utils.keyForPublicKey(id)).id) .to.eql(id.id) diff --git a/test/limited-peer-list.spec.js b/test/limited-peer-list.spec.js index 754bdf22..78b9888d 100644 --- a/test/limited-peer-list.spec.js +++ b/test/limited-peer-list.spec.js @@ -6,29 +6,34 @@ chai.use(require('dirty-chai')) const expect = chai.expect const LimitedPeerList = require('../src/peer-list/limited-peer-list') -const createPeerInfo = require('./utils/create-peer-info') +const createPeerId = require('./utils/create-peer-id') describe('LimitedPeerList', () => { let peers before(async () => { - peers = await createPeerInfo(5) + peers = await createPeerId(5) }) it('basics', () => { const l = new LimitedPeerList(4) - expect(l.push(peers[0])).to.eql(true) - expect(l.push(peers[0])).to.eql(false) - expect(l.push(peers[1])).to.eql(true) - expect(l.push(peers[2])).to.eql(true) - expect(l.push(peers[3])).to.eql(true) - expect(l.push(peers[4])).to.eql(false) + expect(l.push({ id: peers[0] })).to.eql(true) + expect(l.push({ id: peers[0] })).to.eql(false) + expect(l.push({ id: peers[1] })).to.eql(true) + expect(l.push({ id: peers[2] })).to.eql(true) + expect(l.push({ id: peers[3] })).to.eql(true) + expect(l.push({ id: peers[4] })).to.eql(false) expect(l).to.have.length(4) - expect(l.pop()).to.eql(peers[3]) + expect(l.pop()).to.eql({ id: peers[3] }) expect(l).to.have.length(3) - expect(l.push(peers[4])).to.eql(true) - expect(l.toArray()).to.eql([peers[0], peers[1], peers[2], peers[4]]) + expect(l.push({ id: peers[4] })).to.eql(true) + expect(l.toArray()).to.eql([ + { id: peers[0] }, + { id: peers[1] }, + { id: peers[2] }, + { id: peers[4] } + ]) }) }) diff --git a/test/message.spec.js b/test/message.spec.js index 317fead8..5fb6ebcb 100644 --- a/test/message.spec.js +++ b/test/message.spec.js @@ -4,8 +4,8 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect -const PeerInfo = require('peer-info') const PeerId = require('peer-id') +const multiaddr = require('multiaddr') const range = require('lodash.range') const random = require('lodash.random') const { Record } = require('libp2p-record') @@ -32,23 +32,21 @@ describe('Message', () => { const peers = await Promise.all( Array.from({ length: 5 }).map(() => PeerId.create({ bits: 1024 }))) - const closer = peers.slice(0, 5).map((p) => { - const info = new PeerInfo(p) - const addr = `/ip4/198.176.1.${random(198)}/tcp/1234` - info.multiaddrs.add(addr) - info.multiaddrs.add(`/ip4/100.176.1.${random(198)}`) - info.connect(addr) - - return info - }) - - const provider = peers.slice(0, 5).map((p) => { - const info = new PeerInfo(p) - info.multiaddrs.add(`/ip4/98.176.1.${random(198)}/tcp/1234`) - info.multiaddrs.add(`/ip4/10.176.1.${random(198)}`) - - return info - }) + const closer = peers.slice(0, 5).map((p) => ({ + id: p, + multiaddrs: [ + multiaddr(`/ip4/198.176.1.${random(198)}/tcp/1234`), + multiaddr(`/ip4/100.176.1.${random(198)}`) + ] + })) + + const provider = peers.slice(0, 5).map((p) => ({ + id: p, + multiaddrs: [ + multiaddr(`/ip4/98.176.1.${random(198)}/tcp/1234`), + multiaddr(`/ip4/10.176.1.${random(198)}`) + ] + })) const msg = new Message(Message.TYPES.GET_VALUE, Buffer.from('hello'), 5) const record = new Record(Buffer.from('hello'), Buffer.from('world')) @@ -69,15 +67,13 @@ describe('Message', () => { expect(dec.closerPeers).to.have.length(5) dec.closerPeers.forEach((peer, i) => { expect(peer.id.isEqual(msg.closerPeers[i].id)).to.eql(true) - expect(peer.multiaddrs.toArray()) - .to.eql(msg.closerPeers[i].multiaddrs.toArray()) + expect(peer.multiaddrs).to.eql(msg.closerPeers[i].multiaddrs) }) expect(dec.providerPeers).to.have.length(5) dec.providerPeers.forEach((peer, i) => { expect(peer.id.isEqual(msg.providerPeers[i].id)).to.equal(true) - expect(peer.multiaddrs.toArray()) - .to.eql(msg.providerPeers[i].multiaddrs.toArray()) + expect(peer.multiaddrs).to.eql(msg.providerPeers[i].multiaddrs) }) }) @@ -103,7 +99,7 @@ describe('Message', () => { if (msg.providerPeers.length > 0) { msg.providerPeers.forEach((p) => { - expect(PeerInfo.isPeerInfo(p)).to.eql(true) + expect(PeerId.isPeerId(p.id)).to.eql(true) }) } }) diff --git a/test/network.spec.js b/test/network.spec.js index 8c0a9fb5..5a7bcc63 100644 --- a/test/network.spec.js +++ b/test/network.spec.js @@ -40,7 +40,7 @@ describe('Network', () => { } } - const response = await dht.network.sendRequest(dht.peerInfo.id, msg) + const response = await dht.network.sendRequest(dht.peerId, msg) expect(response.type).to.eql(Message.TYPES.PING) }) @@ -100,7 +100,7 @@ describe('Network', () => { } } - const response = await dht.network.sendRequest(dht.peerInfo.id, msg) + const response = await dht.network.sendRequest(dht.peerId, msg) expect(response.type).to.eql(Message.TYPES.FIND_NODE) finish() @@ -150,7 +150,7 @@ describe('Network', () => { dht.network.readMessageTimeout = 100 try { - await dht.network.sendRequest(dht.peerInfo.id, msg) + await dht.network.sendRequest(dht.peerId, msg) } catch (err) { expect(err).to.exist() expect(err.message).to.match(/timed out/) diff --git a/test/peer-list.spec.js b/test/peer-list.spec.js index 87809304..b18c21be 100644 --- a/test/peer-list.spec.js +++ b/test/peer-list.spec.js @@ -7,24 +7,24 @@ const expect = chai.expect const PeerList = require('../src/peer-list') -const createPeerInfo = require('./utils/create-peer-info') +const createPeerId = require('./utils/create-peer-id') describe('PeerList', () => { let peers before(async () => { - peers = await createPeerInfo(3) + peers = await createPeerId(3) }) it('basics', () => { const l = new PeerList() - expect(l.push(peers[0])).to.eql(true) - expect(l.push(peers[0])).to.eql(false) + expect(l.push({ id: peers[0] })).to.eql(true) + expect(l.push({ id: peers[0] })).to.eql(false) expect(l).to.have.length(1) - expect(l.push(peers[1])).to.eql(true) - expect(l.pop()).to.eql(peers[1]) + expect(l.push({ id: peers[1] })).to.eql(true) + expect(l.pop()).to.eql({ id: peers[1] }) expect(l).to.have.length(1) - expect(l.toArray()).to.eql([peers[0]]) + expect(l.toArray()).to.eql([{ id: peers[0] }]) }) }) diff --git a/test/providers.spec.js b/test/providers.spec.js index 3e09b4f7..2115f478 100644 --- a/test/providers.spec.js +++ b/test/providers.spec.js @@ -13,16 +13,16 @@ const multihashing = require('multihashing-async') const Providers = require('../src/providers') -const createPeerInfo = require('./utils/create-peer-info') +const createPeerId = require('./utils/create-peer-id') const createValues = require('./utils/create-values') describe('Providers', () => { - let infos + let peerIds let providers before(async function () { this.timeout(10 * 1000) - infos = await createPeerInfo(3) + peerIds = await createPeerId(3) }) afterEach(() => { @@ -30,43 +30,43 @@ describe('Providers', () => { }) it('simple add and get of providers', async () => { - providers = new Providers(new MemoryDatastore(), infos[2].id) + providers = new Providers(new MemoryDatastore(), peerIds[2]) const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') await Promise.all([ - providers.addProvider(cid, infos[0].id), - providers.addProvider(cid, infos[1].id) + providers.addProvider(cid, peerIds[0]), + providers.addProvider(cid, peerIds[1]) ]) const provs = await providers.getProviders(cid) const ids = new Set(provs.map((peerId) => peerId.toB58String())) - expect(ids.has(infos[0].id.toB58String())).to.be.eql(true) - expect(ids.has(infos[1].id.toB58String())).to.be.eql(true) + expect(ids.has(peerIds[0].toB58String())).to.be.eql(true) + expect(ids.has(peerIds[1].toB58String())).to.be.eql(true) }) it('duplicate add of provider is deduped', async () => { - providers = new Providers(new MemoryDatastore(), infos[2].id) + providers = new Providers(new MemoryDatastore(), peerIds[2]) const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') await Promise.all([ - providers.addProvider(cid, infos[0].id), - providers.addProvider(cid, infos[0].id), - providers.addProvider(cid, infos[1].id), - providers.addProvider(cid, infos[1].id), - providers.addProvider(cid, infos[1].id) + providers.addProvider(cid, peerIds[0]), + providers.addProvider(cid, peerIds[0]), + providers.addProvider(cid, peerIds[1]), + providers.addProvider(cid, peerIds[1]), + providers.addProvider(cid, peerIds[1]) ]) const provs = await providers.getProviders(cid) expect(provs).to.have.length(2) const ids = new Set(provs.map((peerId) => peerId.toB58String())) - expect(ids.has(infos[0].id.toB58String())).to.be.eql(true) - expect(ids.has(infos[1].id.toB58String())).to.be.eql(true) + expect(ids.has(peerIds[0].toB58String())).to.be.eql(true) + expect(ids.has(peerIds[1].toB58String())).to.be.eql(true) }) it('more providers than space in the lru cache', async () => { - providers = new Providers(new MemoryDatastore(), infos[2].id, 10) + providers = new Providers(new MemoryDatastore(), peerIds[2], 10) const hashes = await Promise.all([...new Array(100)].map((i) => { return multihashing(Buffer.from(`hello ${i}`), 'sha2-256') @@ -74,31 +74,31 @@ describe('Providers', () => { const cids = hashes.map((h) => new CID(h)) - await Promise.all(cids.map(cid => providers.addProvider(cid, infos[0].id))) + await Promise.all(cids.map(cid => providers.addProvider(cid, peerIds[0]))) const provs = await Promise.all(cids.map(cid => providers.getProviders(cid))) expect(provs).to.have.length(100) for (const p of provs) { - expect(p[0].id).to.be.eql(infos[0].id.id) + expect(p[0].id).to.be.eql(peerIds[0].id) } }) it('expires', async () => { - providers = new Providers(new MemoryDatastore(), infos[2].id) + providers = new Providers(new MemoryDatastore(), peerIds[2]) providers.cleanupInterval = 100 providers.provideValidity = 200 const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') await Promise.all([ - providers.addProvider(cid, infos[0].id), - providers.addProvider(cid, infos[1].id) + providers.addProvider(cid, peerIds[0]), + providers.addProvider(cid, peerIds[1]) ]) const provs = await providers.getProviders(cid) expect(provs).to.have.length(2) - expect(provs[0].id).to.be.eql(infos[0].id.id) - expect(provs[1].id).to.be.eql(infos[1].id.id) + expect(provs[0].id).to.be.eql(peerIds[0].id) + expect(provs[1].id).to.be.eql(peerIds[1].id) await new Promise(resolve => setTimeout(resolve, 400)) @@ -112,12 +112,12 @@ describe('Providers', () => { os.tmpdir(), (Math.random() * 100).toString() ) const store = new LevelStore(p) - providers = new Providers(store, infos[2].id, 10) + providers = new Providers(store, peerIds[2], 10) console.log('starting') // eslint-disable-line no-console const res = await Promise.all([ createValues(100), - createPeerInfo(600) + createPeerId(600) ]) console.log('got values and peers') // eslint-disable-line no-console diff --git a/test/query.spec.js b/test/query.spec.js index d22db399..deca780d 100644 --- a/test/query.spec.js +++ b/test/query.spec.js @@ -10,17 +10,17 @@ const delay = require('delay') const Query = require('../src/query') const kadUtils = require('../src/utils') -const createPeerInfo = require('./utils/create-peer-info') +const createPeerId = require('./utils/create-peer-id') const TestDHT = require('./utils/test-dht') const createDisjointTracks = require('./utils/create-disjoint-tracks') describe('Query', () => { - let peerInfos + let peerIds let tdht let dht before(async () => { - peerInfos = await createPeerInfo(40) + peerIds = await createPeerId(40) }) beforeEach(async () => { @@ -33,7 +33,7 @@ describe('Query', () => { }) it('simple run', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} @@ -41,21 +41,21 @@ describe('Query', () => { let i = 0 const queryFunc = async (p) => { // eslint-disable-line require-await if (i++ === 1) { - expect(p.id).to.eql(peerInfos[2].id.id) + expect(p.id).to.eql(peerIds[2].id) return { value: Buffer.from('cool'), pathComplete: true } } - expect(p.id).to.eql(peerInfos[1].id.id) + expect(p.id).to.eql(peerIds[1].id) return { - closerPeers: [peerInfos[2]] + closerPeers: [{ id: peerIds[2] }] } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1]]) expect(res.paths[0].value).to.eql(Buffer.from('cool')) expect(res.paths[0].success).to.eql(true) @@ -63,7 +63,7 @@ describe('Query', () => { }) it('does not return an error if only some queries error', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} @@ -78,35 +78,35 @@ describe('Query', () => { } return { - closerPeers: [peerInfos[2]] + closerPeers: [{ id: peerIds[2] }] } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1]]) // Should have visited - // - the initial peer passed to the query: peerInfos[1] - // - the peer returned in closerPeers: peerInfos[2] - expect(visited).to.eql([peerInfos[1].id, peerInfos[2].id]) + // - the initial peer passed to the query: peerIds[1] + // - the peer returned in closerPeers: peerIds[2] + expect(visited).to.eql([peerIds[1], peerIds[2]]) // The final set should only contain peers that were successfully queried // (ie no errors) expect(res.finalSet.size).to.eql(1) - expect(res.finalSet.has(peerInfos[1].id)).to.equal(true) + expect(res.finalSet.has(peerIds[1])).to.equal(true) }) it('returns an error if all queries error', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} const queryFunc = async (p) => { throw new Error('fail') } // eslint-disable-line require-await - const q = new Query(dht, peer.id.id, () => queryFunc) + const q = new Query(dht, peerId.id, () => queryFunc) try { - await q.run([peerInfos[1].id]) + await q.run([peerIds[1]]) } catch (err) { expect(err).to.exist() expect(err.message).to.eql('fail') @@ -117,10 +117,10 @@ describe('Query', () => { }) it('returns empty run if initial peer list is empty', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId const queryFunc = async (p) => {} - const q = new Query(dht, peer.id.id, () => queryFunc) + const q = new Query(dht, peerId.id, () => queryFunc) const res = await q.run([]) // Should not visit any peers @@ -129,25 +129,25 @@ describe('Query', () => { }) it('only closerPeers', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} const queryFunc = async (p) => { // eslint-disable-line require-await return { - closerPeers: [peerInfos[2]] + closerPeers: [{ id: peerIds[2] }] } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1]]) expect(res.finalSet.size).to.eql(2) }) it('only closerPeers concurrent', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} @@ -157,26 +157,25 @@ describe('Query', () => { // 6 -> 7 // 3 -> 9 -> 10 const topology = { - [peerInfos[1].id.toB58String()]: [ - peerInfos[8] + [peerIds[1].toB58String()]: [ + { id: peerIds[8] } ], - [peerInfos[2].id.toB58String()]: [ - peerInfos[4], - peerInfos[6] + [peerIds[2].toB58String()]: [ + { id: peerIds[4] }, + { id: peerIds[6] } ], - [peerInfos[4].id.toB58String()]: [ - peerInfos[5] + [peerIds[4].toB58String()]: [ + { id: peerIds[5] } ], - [peerInfos[6].id.toB58String()]: [ - peerInfos[7] + [peerIds[6].toB58String()]: [ + { id: peerIds[7] } ], - - [peerInfos[3].id.toB58String()]: [ - peerInfos[9] + [peerIds[3].toB58String()]: [ + { id: peerIds[9] } ], - [peerInfos[9].id.toB58String()]: [ - peerInfos[10] + [peerIds[9].toB58String()]: [ + { id: peerIds[10] } ] } @@ -187,32 +186,32 @@ describe('Query', () => { } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id, peerInfos[2].id, peerInfos[3].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1], peerIds[2], peerIds[3]]) // Should visit all peers expect(res.finalSet.size).to.eql(10) }) it('early success', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} // 1 -> 2 -> 3 -> 4 const topology = { - [peerInfos[1].id.toB58String()]: { - closer: [peerInfos[2]] + [peerIds[1].toB58String()]: { + closer: [{ id: peerIds[2] }] }, // Should stop here because pathComplete is true - [peerInfos[2].id.toB58String()]: { - closer: [peerInfos[3]], + [peerIds[2].toB58String()]: { + closer: [{ id: peerIds[3] }], pathComplete: true }, // Should not reach here because previous query returns pathComplete - [peerInfos[3].id.toB58String()]: { - closer: [peerInfos[4]] + [peerIds[3].toB58String()]: { + closer: [{ id: peerIds[4] }] } } @@ -225,8 +224,8 @@ describe('Query', () => { } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1]]) // Should complete successfully expect(res.paths.length).to.eql(1) @@ -239,22 +238,22 @@ describe('Query', () => { it('all queries stop after shutdown', async () => { const deferShutdown = pDefer() const [dhtA] = await tdht.spawn(1) - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dhtA.dialer.dial = (peer) => {} // 1 -> 2 -> 3 -> 4 const topology = { - [peerInfos[1].id.toB58String()]: { - closer: [peerInfos[2]] + [peerIds[1].toB58String()]: { + closer: [{ id: peerIds[2] }] }, - [peerInfos[2].id.toB58String()]: { - closer: [peerInfos[3]] + [peerIds[2].toB58String()]: { + closer: [{ id: peerIds[3] }] }, // Should not reach here because query gets shut down - [peerInfos[3].id.toB58String()]: { - closer: [peerInfos[4]] + [peerIds[3].toB58String()]: { + closer: [{ id: peerIds[4] }] } } @@ -272,8 +271,8 @@ describe('Query', () => { } } - // Shut down after visiting peerInfos[2] - if (p.toB58String() === peerInfos[2].id.toB58String()) { + // Shut down after visiting peerIds[2] + if (p.toB58String() === peerIds[2].toB58String()) { await dhtA.stop() setTimeout(checkExpectations, 100) return getResult() @@ -281,12 +280,12 @@ describe('Query', () => { return getResult() } - const q = new Query(dhtA, peer.id.id, () => queryFunc) - await q.run([peerInfos[1].id]) + const q = new Query(dhtA, peerId.id, () => queryFunc) + await q.run([peerIds[1]]) function checkExpectations () { // Should only visit peers up to the point where we shut down - expect(visited).to.eql([peerInfos[1].id, peerInfos[2].id]) + expect(visited).to.eql([peerIds[1], peerIds[2]]) deferShutdown.resolve() } @@ -296,18 +295,18 @@ describe('Query', () => { it('queries run after shutdown return immediately', async () => { const [dhtA] = await tdht.spawn(1) - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dhtA.dialer.dial = (peer, callback) => callback() // 1 -> 2 -> 3 const topology = { - [peerInfos[1].id.toB58String()]: { - closer: [peerInfos[2]] + [peerIds[1].toB58String()]: { + closer: [{ id: peerIds[2] }] }, - [peerInfos[2].id.toB58String()]: { - closer: [peerInfos[3]] + [peerIds[2].toB58String()]: { + closer: [{ id: peerIds[3] }] } } @@ -318,10 +317,10 @@ describe('Query', () => { } } - const q = new Query(dhtA, peer.id.id, () => queryFunc) + const q = new Query(dhtA, peerId.id, () => queryFunc) await dhtA.stop() - const res = await q.run([peerInfos[1].id]) + const res = await q.run([peerIds[1]]) // Should not visit any peers expect(res.paths.length).to.eql(0) @@ -329,7 +328,7 @@ describe('Query', () => { }) it('disjoint path values', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId const values = ['v0', 'v1'].map(Buffer.from) // mock this so we can dial non existing peers @@ -339,24 +338,24 @@ describe('Query', () => { // 4 -> 5 (v1) const topology = { // Top level node - [peerInfos[1].id.toB58String()]: { - closer: [peerInfos[2]] + [peerIds[1].toB58String()]: { + closer: [{ id: peerIds[2] }] }, - [peerInfos[2].id.toB58String()]: { - closer: [peerInfos[3]] + [peerIds[2].toB58String()]: { + closer: [{ id: peerIds[3] }] }, // v0 - [peerInfos[3].id.toB58String()]: { + [peerIds[3].toB58String()]: { value: values[0], pathComplete: true }, // Top level node - [peerInfos[4].id.toB58String()]: { - closer: [peerInfos[5]] + [peerIds[4].toB58String()]: { + closer: [{ id: peerIds[5] }] }, // v1 - [peerInfos[5].id.toB58String()]: { + [peerIds[5].toB58String()]: { value: values[1], pathComplete: true } @@ -372,8 +371,8 @@ describe('Query', () => { } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id, peerInfos[4].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1], peerIds[4]]) // We should get back the values from both paths expect(res.paths.length).to.eql(2) @@ -384,7 +383,7 @@ describe('Query', () => { }) it('disjoint path values with early completion', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId const values = ['v0', 'v1'].map(Buffer.from) // mock this so we can dial non existing peers @@ -394,29 +393,29 @@ describe('Query', () => { // 4 -> 5 [query complete] const topology = { // Top level node - [peerInfos[1].id.toB58String()]: { - closer: [peerInfos[2]] + [peerIds[1].toB58String()]: { + closer: [{ id: peerIds[2] }] }, // This query has a delay which means it only returns after the other // path has already indicated the query is complete, so its result // should be ignored - [peerInfos[2].id.toB58String()]: { + [peerIds[2].toB58String()]: { delay: 100, - closer: [peerInfos[3]] + closer: [{ id: peerIds[3] }] }, // Query has stopped by the time we reach here, should be ignored - [peerInfos[3].id.toB58String()]: { + [peerIds[3].toB58String()]: { value: values[0], pathComplete: true }, // Top level node - [peerInfos[4].id.toB58String()]: { - closer: [peerInfos[5]] + [peerIds[4].toB58String()]: { + closer: [{ id: peerIds[5] }] }, // This peer indicates that the query is complete - [peerInfos[5].id.toB58String()]: { - closer: [peerInfos[2]], + [peerIds[5].toB58String()]: { + closer: [{ id: peerIds[2] }], value: values[1], queryComplete: true } @@ -436,8 +435,8 @@ describe('Query', () => { } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id, peerInfos[4].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1], peerIds[4]]) // We should only get back the value from the path 4 -> 5 expect(res.paths.length).to.eql(1) @@ -447,13 +446,13 @@ describe('Query', () => { // Wait a little bit to make sure we don't continue down another path // after finding a successful path await delay(300) - if (visited.indexOf(peerInfos[3].id) !== -1) { + if (visited.indexOf(peerIds[3]) !== -1) { expect.fail('Query continued after success was returned') } }) it('disjoint path continue other paths after error on one path', async () => { - const peer = dht.peerInfo + const peerId = dht.peerId const values = ['v0', 'v1'].map(Buffer.from) // mock this so we can dial non existing peers @@ -463,32 +462,32 @@ describe('Query', () => { // 4 -> 5 [error] -> 6 const topology = { // Top level node - [peerInfos[1].id.toB58String()]: { - closer: [peerInfos[2]] + [peerIds[1].toB58String()]: { + closer: [{ id: peerIds[2] }] }, // This query has a delay which means it only returns after the other // path has already returned an error - [peerInfos[2].id.toB58String()]: { + [peerIds[2].toB58String()]: { delay: 100, - closer: [peerInfos[3]] + closer: [{ id: peerIds[3] }] }, // Success peer, should get this value back at the end - [peerInfos[3].id.toB58String()]: { + [peerIds[3].toB58String()]: { value: values[0], pathComplete: true }, // Top level node - [peerInfos[4].id.toB58String()]: { - closer: [peerInfos[5]] + [peerIds[4].toB58String()]: { + closer: [{ id: peerIds[5] }] }, // Return an error at this point - [peerInfos[5].id.toB58String()]: { - closer: [peerInfos[6]], + [peerIds[5].toB58String()]: { + closer: [{ id: peerIds[6] }], error: true }, // Should never reach here - [peerInfos[6].id.toB58String()]: { + [peerIds[6].toB58String()]: { value: values[1], pathComplete: true } @@ -510,8 +509,8 @@ describe('Query', () => { } } - const q = new Query(dht, peer.id.id, () => queryFunc) - const res = await q.run([peerInfos[1].id, peerInfos[4].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + const res = await q.run([peerIds[1], peerIds[4]]) // We should only get back the value from the path 1 -> 2 -> 3 expect(res.paths.length).to.eql(1) @@ -523,9 +522,8 @@ describe('Query', () => { // mock this so we can dial non existing peers dht.dialer.dial = () => {} - // Sort peers by distance from dht.peerInfo - const peerZeroDhtKey = await kadUtils.convertPeerId(dht.peerInfo.id) - const peerIds = peerInfos.map(pi => pi.id) + // Sort peers by distance from dht.peerId + const peerZeroDhtKey = await kadUtils.convertPeerId(dht.peerId) const sorted = await kadUtils.sortClosestPeers(peerIds, peerZeroDhtKey) // Local node has nodes 10, 16 and 18 in k-bucket @@ -569,18 +567,18 @@ describe('Query', () => { } const peerIndex = (peerId) => sorted.findIndex(p => p === peerId) - const peerIdToInfo = (peerId) => peerInfos.find(pi => pi.id === peerId) + const peerIdToPeerData = (peerId) => peerIds.find(pi => pi === peerId) const visited = [] const queryFunc = async (peerId) => { // eslint-disable-line require-await visited.push(peerId) const i = peerIndex(peerId) const closerIndexes = topology[i] || [] - const closerPeers = closerIndexes.map(j => peerIdToInfo(sorted[j])) + const closerPeers = closerIndexes.map(j => peerIdToPeerData(sorted[j])).map((p) => ({ id: p })) return { closerPeers } } - const q = new Query(dht, dht.peerInfo.id.id, () => queryFunc) + const q = new Query(dht, dht.peerId.id, () => queryFunc) const res = await q.run(initial) // Should query 19 peers, then find some peers closer to the key, and @@ -624,19 +622,19 @@ describe('Query', () => { */ it('uses disjoint paths', async () => { const goodLength = 3 - const samplePeerInfos = peerInfos.slice(0, 12) + const samplePeerIds = peerIds.slice(0, 12) const { targetId, starts, getResponse - } = await createDisjointTracks(samplePeerInfos, goodLength) + } = await createDisjointTracks(samplePeerIds, goodLength) // mock this so we can dial non existing peers dht.dialer.dial = () => {} let badEndVisited = false let targetVisited = false - const q = new Query(dht, targetId, (trackNum) => { + const q = new Query(dht, targetId.id, (trackNum) => { return async (p) => { // eslint-disable-line require-await const response = getResponse(p, trackNum) expect(response).to.exist() // or we aren't on the right track @@ -655,29 +653,29 @@ describe('Query', () => { // we should reach the target node expect(targetVisited).to.eql(true) // we should visit all nodes (except the target) - expect(res.finalSet.size).to.eql(samplePeerInfos.length - 1) + expect(res.finalSet.size).to.eql(samplePeerIds.length - 1) // there should be one successful path expect(res.paths.length).to.eql(1) }) it('should discover closer peers', () => { const discoverDefer = pDefer() - const peer = dht.peerInfo + const peerId = dht.peerId // mock this so we can dial non existing peers dht.dialer.dial = () => {} const queryFunc = async (p) => { // eslint-disable-line require-await return { - closerPeers: [peerInfos[2]] + closerPeers: [{ id: peerIds[2] }] } } - const q = new Query(dht, peer.id.id, () => queryFunc) - q.run([peerInfos[1].id]) + const q = new Query(dht, peerId.id, () => queryFunc) + q.run([peerIds[1]]) - dht.once('peer', (peerInfo) => { - expect(peerInfo.id).to.eql(peerInfos[2].id) + dht.once('peer', (peerData) => { + expect(peerData.id).to.eql(peerIds[2]) discoverDefer.resolve() }) diff --git a/test/query/index.spec.js b/test/query/index.spec.js index c6d2b123..917b77bf 100644 --- a/test/query/index.spec.js +++ b/test/query/index.spec.js @@ -15,19 +15,19 @@ const Path = require('../../src/query/path') const Run = require('../../src/query/run') const DHT = require('../../src') const c = require('../../src/constants') -const createPeerInfo = require('../utils/create-peer-info') -const { sortClosestPeerInfos } = require('../utils') +const createPeerId = require('../utils/create-peer-id') +const { sortClosestPeers } = require('../../src/utils') const { convertBuffer } = require('../../src/utils') const NUM_IDS = 101 describe('Query', () => { - let peerInfos - let ourPeerInfo + let peerIds + let ourPeerId before(async () => { - const peers = await createPeerInfo(NUM_IDS) + const peers = await createPeerId(NUM_IDS) - ourPeerInfo = peers.shift() - peerInfos = peers + ourPeerId = peers.shift() + peerIds = peers }) describe('get closest peers', () => { @@ -42,7 +42,7 @@ describe('Query', () => { const dhtKey = await convertBuffer(targetKey.key) targetKey.dhtKey = dhtKey - sortedPeers = await sortClosestPeerInfos(peerInfos, targetKey.dhtKey) + sortedPeers = await sortClosestPeers(peerIds, targetKey.dhtKey) }) before('create a dht', () => { @@ -50,7 +50,7 @@ describe('Query', () => { dht = new DHT({ dialer: {}, peerStore, - peerInfo: ourPeerInfo + peerId: ourPeerId }) }) @@ -71,7 +71,7 @@ describe('Query', () => { // Add the sorted peers into 5 paths. This will weight // the paths with increasingly further peers - const sortedPeerIds = sortedPeers.map(peerInfo => peerInfo.id) + const sortedPeerIds = sortedPeers const peersPerPath = sortedPeerIds.length / PATHS const paths = [...new Array(PATHS)].map((_, index) => { const path = new Path(run, query.makePath()) @@ -117,7 +117,7 @@ describe('Query', () => { await run.init() - const sortedPeerIds = sortedPeers.map(peerInfo => peerInfo.id) + const sortedPeerIds = sortedPeers // Take the top 15 peers and peers 20 - 25 to seed `run.peersQueried` // This leaves us with only 16 - 19 as closer peers diff --git a/test/random-walk.spec.js b/test/random-walk.spec.js index d2c6621e..5d07f6d4 100644 --- a/test/random-walk.spec.js +++ b/test/random-walk.spec.js @@ -17,10 +17,8 @@ const { describe('Random Walk', () => { const mockDHT = { - peerInfo: { - id: { - toB58String: () => 'QmRLoXS3E73psYaUsma1VSbboTa2J8Z9kso1tpiGLk9WQ4' - } + peerId: { + toB58String: () => 'QmRLoXS3E73psYaUsma1VSbboTa2J8Z9kso1tpiGLk9WQ4' }, findPeer: () => {}, _log: { diff --git a/test/rpc/handlers/add-provider.spec.js b/test/rpc/handlers/add-provider.spec.js index 606982c9..b0c0bfd5 100644 --- a/test/rpc/handlers/add-provider.spec.js +++ b/test/rpc/handlers/add-provider.spec.js @@ -5,24 +5,25 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect -const _ = require('lodash') + +const multiaddr = require('multiaddr') const Message = require('../../../src/message') const handler = require('../../../src/rpc/handlers/add-provider') -const createPeerInfo = require('../../utils/create-peer-info') +const createPeerId = require('../../utils/create-peer-id') const createValues = require('../../utils/create-values') const TestDHT = require('../../utils/test-dht') describe('rpc - handlers - AddProvider', () => { - let peers + let peerIds let values let tdht let dht before(async () => { - [peers, values] = await Promise.all([ - createPeerInfo(3), + [peerIds, values] = await Promise.all([ + createPeerId(3), createValues(2) ]) }) @@ -48,7 +49,7 @@ describe('rpc - handlers - AddProvider', () => { await Promise.all(tests.map((t) => { it(t.error.toString(), async () => { try { - await handler(dht)(peers[0], t.message) + await handler(dht)(peerIds[0], t.message) } catch (err) { expect(err).to.exist() expect(err.code).to.eql(t.error) @@ -61,47 +62,47 @@ describe('rpc - handlers - AddProvider', () => { it('ignore providers that do not match the sender', async () => { const cid = values[0].cid - const msg = new Message(Message.TYPES.ADD_PROVIDER, cid.buffer, 0) - const sender = _.cloneDeep(peers[0]) - const provider = _.cloneDeep(peers[0]) - provider.multiaddrs.add('/ip4/127.0.0.1/tcp/1234') - const other = _.cloneDeep(peers[1]) - other.multiaddrs.add('/ip4/127.0.0.1/tcp/2345') + const ma1 = multiaddr('/ip4/127.0.0.1/tcp/1234') + const ma2 = multiaddr('/ip4/127.0.0.1/tcp/2345') + msg.providerPeers = [ - provider, - other + { + id: peerIds[0], + multiaddrs: [ma1] + }, + { + id: peerIds[1], + multiaddrs: [ma2] + } ] - await handler(dht)(sender, msg) + await handler(dht)(peerIds[0], msg) const provs = await dht.providers.getProviders(cid) - expect(provs).to.have.length(1) - expect(provs[0].id).to.eql(provider.id.id) - const bookEntry = dht.peerStore.get(provider.id) + expect(provs[0].id).to.eql(peerIds[0].id) - // Favour peerInfo from payload over peerInfo from sender - expect(bookEntry.multiaddrInfos.map((mi) => mi.multiaddr)).to.eql( - provider.multiaddrs.toArray() - ) + const bookEntry = dht.peerStore.get(peerIds[0]) + expect(bookEntry.multiaddrInfos.map((mi) => mi.multiaddr)).to.eql([ma1]) }) it('fall back to sender if providers have no multiaddrs', async () => { const cid = values[0].cid const msg = new Message(Message.TYPES.ADD_PROVIDER, cid.buffer, 0) - const sender = _.cloneDeep(peers[0]) - const provider = _.cloneDeep(peers[0]) - provider.multiaddrs.clear() - msg.providerPeers = [provider] - await handler(dht)(sender, msg) + msg.providerPeers = [{ + id: peerIds[0], + multiaddrs: [] + }] + + await handler(dht)(peerIds[0], msg) const provs = await dht.providers.getProviders(cid) - expect(dht.peerStore.get(provider.id)).to.equal(undefined) + expect(dht.peerStore.get(peerIds[0])).to.equal(undefined) expect(provs).to.have.length(1) - expect(provs[0].id).to.eql(provider.id.id) + expect(provs[0].id).to.eql(peerIds[0].id) }) }) diff --git a/test/rpc/handlers/find-node.spec.js b/test/rpc/handlers/find-node.spec.js index 99b52c61..7538dea5 100644 --- a/test/rpc/handlers/find-node.spec.js +++ b/test/rpc/handlers/find-node.spec.js @@ -10,16 +10,16 @@ const handler = require('../../../src/rpc/handlers/find-node') const T = Message.TYPES.FIND_NODE -const createPeerInfo = require('../../utils/create-peer-info') +const createPeerId = require('../../utils/create-peer-id') const TestDHT = require('../../utils/test-dht') describe('rpc - handlers - FindNode', () => { - let peers + let peerIds let tdht let dht before(async () => { - peers = await createPeerInfo(3) + peerIds = await createPeerId(3) }) beforeEach(async () => { @@ -32,37 +32,33 @@ describe('rpc - handlers - FindNode', () => { afterEach(() => tdht.teardown()) it('returns self, if asked for self', async () => { - const msg = new Message(T, dht.peerInfo.id.id, 0) + const msg = new Message(T, dht.peerId.id, 0) - const response = await handler(dht)(peers[1], msg) + const response = await handler(dht)(peerIds[1], msg) expect(response.closerPeers).to.have.length(1) const peer = response.closerPeers[0] - expect(peer.id.id).to.be.eql(dht.peerInfo.id.id) + expect(peer.id.id).to.be.eql(dht.peerId.id) }) it('returns closer peers', async () => { const msg = new Message(T, Buffer.from('hello'), 0) - const other = peers[1] + const other = peerIds[1] await dht._add(other) - const response = await handler(dht)(peers[2], msg) + const response = await handler(dht)(peerIds[2].id, msg) expect(response.closerPeers).to.have.length(1) const peer = response.closerPeers[0] - expect(peer.id.id).to.be.eql(peers[1].id.id) - expect( - peer.multiaddrs.toArray() - ).to.be.eql( - peers[1].multiaddrs.toArray() - ) + expect(peer.id.id).to.be.eql(peerIds[1].id) + expect(peer.multiaddrs).to.be.eql([]) }) it('handles no peers found', async () => { const msg = new Message(T, Buffer.from('hello'), 0) - const response = await handler(dht)(peers[2], msg) + const response = await handler(dht)(peerIds[2], msg) expect(response.closerPeers).to.have.length(0) }) diff --git a/test/rpc/handlers/get-providers.spec.js b/test/rpc/handlers/get-providers.spec.js index 5684989a..76560f66 100644 --- a/test/rpc/handlers/get-providers.spec.js +++ b/test/rpc/handlers/get-providers.spec.js @@ -11,19 +11,19 @@ const handler = require('../../../src/rpc/handlers/get-providers') const T = Message.TYPES.GET_PROVIDERS -const createPeerInfo = require('../../utils/create-peer-info') +const createPeerId = require('../../utils/create-peer-id') const createValues = require('../../utils/create-values') const TestDHT = require('../../utils/test-dht') describe('rpc - handlers - GetProviders', () => { - let peers + let peerIds let values let tdht let dht before(async () => { - [peers, values] = await Promise.all([ - createPeerInfo(3), + [peerIds, values] = await Promise.all([ + createPeerId(3), createValues(2) ]) }) @@ -41,7 +41,7 @@ describe('rpc - handlers - GetProviders', () => { const msg = new Message(T, Buffer.from('hello'), 0) try { - await handler(dht)(peers[0], msg) + await handler(dht)(peerIds[0], msg) } catch (err) { expect(err.code).to.eql('ERR_INVALID_CID') } @@ -54,24 +54,24 @@ describe('rpc - handlers - GetProviders', () => { const dsKey = utils.bufferToKey(v.cid.buffer) await dht.datastore.put(dsKey, v.value) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.key).to.be.eql(v.cid.buffer) expect(response.providerPeers).to.have.length(1) expect(response.providerPeers[0].id.toB58String()) - .to.eql(dht.peerInfo.id.toB58String()) + .to.eql(dht.peerId.toB58String()) }) it('responds with listed providers and closer peers', async () => { const v = values[0] const msg = new Message(T, v.cid.buffer, 0) - const prov = peers[1].id - const closer = peers[2] + const prov = peerIds[1] + const closer = peerIds[2] await dht._add(closer) await dht.providers.addProvider(v.cid, prov) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.key).to.be.eql(v.cid.buffer) expect(response.providerPeers).to.have.length(1) @@ -80,6 +80,6 @@ describe('rpc - handlers - GetProviders', () => { expect(response.closerPeers).to.have.length(1) expect(response.closerPeers[0].id.toB58String()) - .to.eql(closer.id.toB58String()) + .to.eql(closer.toB58String()) }) }) diff --git a/test/rpc/handlers/get-value.spec.js b/test/rpc/handlers/get-value.spec.js index 08eb1fad..c3fcd9ca 100644 --- a/test/rpc/handlers/get-value.spec.js +++ b/test/rpc/handlers/get-value.spec.js @@ -11,16 +11,16 @@ const utils = require('../../../src/utils') const T = Message.TYPES.GET_VALUE -const createPeerInfo = require('../../utils/create-peer-info') +const createPeerId = require('../../utils/create-peer-id') const TestDHT = require('../../utils/test-dht') describe('rpc - handlers - GetValue', () => { - let peers + let peerIds let tdht let dht before(async () => { - peers = await createPeerInfo(2) + peerIds = await createPeerId(2) }) beforeEach(async () => { @@ -36,7 +36,7 @@ describe('rpc - handlers - GetValue', () => { const msg = new Message(T, Buffer.alloc(0), 0) try { - await handler(dht)(peers[0], msg) + await handler(dht)(peerIds[0], msg) } catch (err) { expect(err.code).to.eql('ERR_INVALID_KEY') return @@ -51,7 +51,7 @@ describe('rpc - handlers - GetValue', () => { const msg = new Message(T, key, 0) await dht.put(key, value) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.record).to.exist() expect(response.record.key).to.eql(key) @@ -61,45 +61,45 @@ describe('rpc - handlers - GetValue', () => { it('responds with closerPeers returned from the dht', async () => { const key = Buffer.from('hello') const msg = new Message(T, key, 0) - const other = peers[1] + const other = peerIds[1] await dht._add(other) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.closerPeers).to.have.length(1) - expect(response.closerPeers[0].id.toB58String()).to.be.eql(other.id.toB58String()) + expect(response.closerPeers[0].id.toB58String()).to.be.eql(other.toB58String()) }) describe('public key', () => { it('self', async () => { - const key = utils.keyForPublicKey(dht.peerInfo.id) + const key = utils.keyForPublicKey(dht.peerId) const msg = new Message(T, key, 0) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.record).to.exist() - expect(response.record.value).to.eql(dht.peerInfo.id.pubKey.bytes) + expect(response.record.value).to.eql(dht.peerId.pubKey.bytes) }) it('other in peerstore', async () => { - const other = peers[1] - const key = utils.keyForPublicKey(other.id) + const other = peerIds[1] + const key = utils.keyForPublicKey(other) const msg = new Message(T, key, 0) - dht.peerStore.addressBook.add(other.id, other.multiaddrs.toArray()) + dht.peerStore.addressBook.add(other, []) await dht._add(other) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.record).to.exist() - expect(response.record.value).to.eql(other.id.pubKey.bytes) + expect(response.record.value).to.eql(other.pubKey.bytes) }) it('other unkown', async () => { - const other = peers[1] - const key = utils.keyForPublicKey(other.id) + const other = peerIds[1] + const key = utils.keyForPublicKey(other) const msg = new Message(T, key, 0) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response.record).to.not.exist() }) }) diff --git a/test/rpc/handlers/ping.spec.js b/test/rpc/handlers/ping.spec.js index 7f03f326..1df3b031 100644 --- a/test/rpc/handlers/ping.spec.js +++ b/test/rpc/handlers/ping.spec.js @@ -10,16 +10,16 @@ const handler = require('../../../src/rpc/handlers/ping') const T = Message.TYPES.PING -const createPeerInfo = require('../../utils/create-peer-info') +const createPeerId = require('../../utils/create-peer-id') const TestDHT = require('../../utils/test-dht') describe('rpc - handlers - Ping', () => { - let peers + let peerIds let tdht let dht before(async () => { - peers = await createPeerInfo(2) + peerIds = await createPeerId(2) }) beforeEach(async () => { @@ -33,7 +33,7 @@ describe('rpc - handlers - Ping', () => { it('replies with the same message', async () => { const msg = new Message(T, Buffer.from('hello'), 5) - const response = await handler(dht)(peers[0], msg) + const response = await handler(dht)(peerIds[0], msg) expect(response).to.be.eql(msg) }) diff --git a/test/rpc/handlers/put-value.spec.js b/test/rpc/handlers/put-value.spec.js index 3548616d..723d33c4 100644 --- a/test/rpc/handlers/put-value.spec.js +++ b/test/rpc/handlers/put-value.spec.js @@ -12,18 +12,18 @@ const Message = require('../../../src/message') const handler = require('../../../src/rpc/handlers/put-value') const utils = require('../../../src/utils') -const createPeerInfo = require('../../utils/create-peer-info') +const createPeerId = require('../../utils/create-peer-id') const TestDHT = require('../../utils/test-dht') const T = Message.TYPES.PUT_VALUE describe('rpc - handlers - PutValue', () => { - let peers + let peerIds let tdht let dht before(async () => { - peers = await createPeerInfo(2) + peerIds = await createPeerId(2) }) beforeEach(async () => { @@ -39,7 +39,7 @@ describe('rpc - handlers - PutValue', () => { const msg = new Message(T, Buffer.from('hello'), 5) try { - await handler(dht)(peers[0], msg) + await handler(dht)(peerIds[0], msg) } catch (err) { expect(err.code).to.eql('ERR_EMPTY_RECORD') return @@ -56,7 +56,7 @@ describe('rpc - handlers - PutValue', () => { ) msg.record = record - const response = await handler(dht)(peers[1], msg) + const response = await handler(dht)(peerIds[1], msg) expect(response).to.be.eql(msg) const key = utils.bufferToKey(Buffer.from('hello')) diff --git a/test/rpc/index.spec.js b/test/rpc/index.spec.js index 15e484a2..472629ac 100644 --- a/test/rpc/index.spec.js +++ b/test/rpc/index.spec.js @@ -12,16 +12,16 @@ const { collect } = require('streaming-iterables') const Message = require('../../src/message') const rpc = require('../../src/rpc') -const createPeerInfo = require('../utils/create-peer-info') +const createPeerId = require('../utils/create-peer-id') const TestDHT = require('../utils/test-dht') const toBuffer = require('../utils/to-buffer') describe('rpc', () => { - let peerInfos + let peerIds let tdht before(async () => { - peerInfos = await createPeerInfo(2) + peerIds = await createPeerId(2) tdht = new TestDHT() }) @@ -29,8 +29,6 @@ describe('rpc', () => { const defer = pDefer() const [dht] = await tdht.spawn(1) - dht.peerStore.addressBook.set(peerInfos[1].id, peerInfos[1].multiaddrs.toArray()) - const msg = new Message(Message.TYPES.GET_VALUE, Buffer.from('hello'), 5) const validateMessage = (res) => { @@ -63,7 +61,7 @@ describe('rpc', () => { protocol: 'protocol', stream: duplexStream, connection: { - remotePeer: peerInfos[1].id + remotePeer: peerIds[1] } }) diff --git a/test/simulation/index.js b/test/simulation/index.js index 967f3b72..79ab88b5 100644 --- a/test/simulation/index.js +++ b/test/simulation/index.js @@ -99,7 +99,7 @@ async function GetClosestPeersSimulation () { // Add random peers to our table const ourPeers = randomMembers(peers, randomInteger(MIN_PEERS_KNOWN, MAX_PEERS_KNOWN)) for (const peer of ourPeers) { - await dht._add(peer) + await dht._add(peer.id) } dht.network.sendRequest = (to, message, callback) => { diff --git a/test/utils/create-disjoint-tracks.js b/test/utils/create-disjoint-tracks.js index 7e8f72cc..aaa6db7c 100644 --- a/test/utils/create-disjoint-tracks.js +++ b/test/utils/create-disjoint-tracks.js @@ -6,12 +6,11 @@ const { } = require('../../src/utils') /* - * Given an array of peerInfos, decide on a target, start peers, and + * Given an array of peerIds, decide on a target, start peers, and * "next", a successor function for the query to use. See comment * where this is called for details. */ -async function createDisjointTracks (peerInfos, goodLength) { - const ids = peerInfos.map((info) => info.id) +async function createDisjointTracks (ids, goodLength) { const us = ids[0] const ourId = await convertPeerId(us) @@ -52,13 +51,13 @@ async function createDisjointTracks (peerInfos, goodLength) { } else { const infoIdx = ids.indexOf(track[nextPos]) return { - closerPeers: [peerInfos[infoIdx]] + closerPeers: [{ id: ids[infoIdx] }] } } } return { - targetId: target.id, + targetId: target, starts: [goodTrack[0], badTrack[0]], getResponse: next } diff --git a/test/utils/create-peer-info.js b/test/utils/create-peer-info.js deleted file mode 100644 index aa0f4304..00000000 --- a/test/utils/create-peer-info.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict' - -const PeerId = require('peer-id') -const PeerInfo = require('peer-info') - -/** - * Creates multiple PeerInfos - * @param {number} length The number of `PeerInfo` to create - * @returns {Promise>} - */ -function createPeerInfo (length) { - return Promise.all( - Array.from({ length }).map(async () => { - const id = await PeerId.create({ bits: 512 }) - return new PeerInfo(id) - }) - ) -} - -module.exports = createPeerInfo diff --git a/test/utils/index.js b/test/utils/index.js index 1350af3e..32faa1df 100644 --- a/test/utils/index.js +++ b/test/utils/index.js @@ -5,8 +5,6 @@ const pRetry = require('p-retry') const pTimeout = require('p-timeout') const duplexPair = require('it-pair/duplex') -const { sortClosestPeers } = require('../../src/utils') - const createMockRegistrar = (registrarRecord) => ({ handle: (multicodec, handler) => { const rec = registrarRecord[multicodec] || {} @@ -51,23 +49,6 @@ const ConnectionPair = () => { exports.ConnectionPair = ConnectionPair -/** - * Like `sortClosestPeers`, expect it takes and returns `PeerInfo`s - * - * @param {Array} peers - * @param {Buffer} target - * @returns {Array} - */ -exports.sortClosestPeerInfos = async (peers, target) => { - const sortedPeerIds = await sortClosestPeers(peers.map(peerInfo => peerInfo.id), target) - - return sortedPeerIds.map((peerId) => { - return peers.find((peerInfo) => { - return peerInfo.id.isEqual(peerId) - }) - }) -} - exports.bootstrap = (dhts) => { dhts.forEach((dht) => { dht.randomWalk._walk(1, 10000) diff --git a/test/utils/test-dht.js b/test/utils/test-dht.js index 8ff07eae..498c4875 100644 --- a/test/utils/test-dht.js +++ b/test/utils/test-dht.js @@ -7,7 +7,7 @@ const delay = require('delay') const KadDHT = require('../../src') const { PROTOCOL_DHT } = require('../../src/constants') -const createPeerInfo = require('./create-peer-info') +const createPeerId = require('./create-peer-id') const { createMockRegistrar, ConnectionPair @@ -37,15 +37,12 @@ class TestDHT { ...options } - const [p] = await createPeerInfo(1) - const port = index !== undefined ? 8000 + index : 0 - - p.multiaddrs.add(`/ip4/127.0.0.1/tcp/${port}/p2p/${p.id.toB58String()}`) + const [peerId] = await createPeerId(1) const connectToPeer = async (peer) => { const remotePeerB58 = peer.toB58String() const remoteDht = this.nodes.find( - (node) => node.peerInfo.id.toB58String() === remotePeerB58 + (node) => node.peerId.toB58String() === remotePeerB58 ) const localOnConnect = regRecord[PROTOCOL_DHT].onConnect @@ -55,14 +52,14 @@ class TestDHT { // Notice peers of connection const [c0, c1] = ConnectionPair() - await localOnConnect(remoteDht.peerInfo, c1) - await remoteOnConnect(p, c0) + await localOnConnect(remoteDht.peerId, c1) + await remoteOnConnect(peerId, c0) await remoteHandler({ protocol: PROTOCOL_DHT, stream: c0.stream, connection: { - remotePeer: p.id + remotePeer: peerId } }) @@ -79,7 +76,7 @@ class TestDHT { }, registrar: createMockRegistrar(regRecord), peerStore, - peerInfo: p, + peerId: peerId, validators: { v: { func () { @@ -114,12 +111,12 @@ class TestDHT { const [c0, c1] = ConnectionPair() // Notice peers of connection - await onConnectA(dhtB.peerInfo, c0) - await onConnectB(dhtA.peerInfo, c1) + await onConnectA(dhtB.peerId, c0) + await onConnectB(dhtA.peerId, c1) return Promise.all([ pRetry(async () => { - const match = await dhtA.routingTable.find(dhtB.peerInfo.id) + const match = await dhtA.routingTable.find(dhtB.peerId) if (!match) { await delay(100) @@ -129,7 +126,7 @@ class TestDHT { return match }, { retries: 50 }), pRetry(async () => { - const match = await dhtB.routingTable.find(dhtA.peerInfo.id) + const match = await dhtB.routingTable.find(dhtA.peerId) if (!match) { await delay(100)