diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ab7a3403..e9ecbb60 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,7 +37,7 @@ jobs: with: node-version: ${{ matrix.node }} - run: npm install - - run: npx aegir test -t node --cov --bail + - run: npm run test:node -- --cov --bail - uses: codecov/codecov-action@v1 test-chrome: needs: check @@ -48,7 +48,7 @@ jobs: with: node-version: lts/* - run: npm install - - run: npx aegir test -t browser -t webworker --bail + - run: npm run test:browser -- -t browser -t webworker --bail test-firefox: needs: check runs-on: ubuntu-latest @@ -58,7 +58,7 @@ jobs: with: node-version: lts/* - run: npm install - - run: npx aegir test -t browser -t webworker --bail -- --browser firefox + - run: npm run test:browser -- -t browser -t webworker --bail -- --browser firefox test-ts: needs: check runs-on: ubuntu-latest diff --git a/package.json b/package.json index 83b05384..82b207c2 100644 --- a/package.json +++ b/package.json @@ -151,7 +151,7 @@ "libp2p": ".", "libp2p-bootstrap": "^0.14.0", "libp2p-delegated-content-routing": "^0.11.0", - "libp2p-delegated-peer-routing": "^0.11.0", + "libp2p-delegated-peer-routing": "^0.11.1", "libp2p-floodsub": "^0.27.0", "libp2p-gossipsub": "^0.12.1", "libp2p-interfaces-compliance-tests": "^2.0.1", diff --git a/src/circuit/index.js b/src/circuit/index.js index 4d180b6e..06d4107a 100644 --- a/src/circuit/index.js +++ b/src/circuit/index.js @@ -4,7 +4,7 @@ const debug = require('debug') const log = Object.assign(debug('libp2p:relay'), { error: debug('libp2p:relay:err') }) - +const { codes } = require('./../errors') const { setDelayedInterval, clearDelayedInterval @@ -88,7 +88,7 @@ class Relay { const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) await this._libp2p.contentRouting.provide(cid) } catch (/** @type {any} */ err) { - if (err.code === 'NO_ROUTERS_AVAILABLE') { + if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) { log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err) // Stop the advertise this.stop() diff --git a/src/config.js b/src/config.js index 9542c700..0d9bbc4a 100644 --- a/src/config.js +++ b/src/config.js @@ -60,13 +60,7 @@ const DefaultConfig = { protocolPrefix: 'ipfs', dht: { enabled: false, - kBucketSize: 20, - randomWalk: { - enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86 - queriesPerPeriod: 1, - interval: 300e3, - timeout: 10e3 - } + kBucketSize: 20 }, nat: { enabled: true, diff --git a/src/content-routing/index.js b/src/content-routing/index.js index 924b9873..df04225d 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -54,7 +54,7 @@ class ContentRouting { */ async * findProviders (key, options = {}) { if (!this.routers.length) { - throw errCode(new Error('No content this.routers available'), 'NO_ROUTERS_AVAILABLE') + throw errCode(new Error('No content this.routers available'), codes.ERR_NO_ROUTERS_AVAILABLE) } yield * pipe( @@ -77,7 +77,7 @@ class ContentRouting { */ async provide (key) { if (!this.routers.length) { - throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE') + throw errCode(new Error('No content routers available'), codes.ERR_NO_ROUTERS_AVAILABLE) } await Promise.all(this.routers.map((router) => router.provide(key))) diff --git a/src/dht/dht-peer-routing.js b/src/dht/dht-peer-routing.js index 762abc80..61d79f92 100644 --- a/src/dht/dht-peer-routing.js +++ b/src/dht/dht-peer-routing.js @@ -27,8 +27,12 @@ class DHTPeerRouting { */ async findPeer (peerId, options = {}) { for await (const event of this._dht.findPeer(peerId, options)) { - if (event.name === 'FINAL_PEER') { - return event.peer + if (event.name === 'PEER_RESPONSE') { + const peer = event.closer.find(peerData => peerData.id.equals(peerId)) + + if (peer) { + return peer + } } } diff --git a/src/errors.js b/src/errors.js index a5db3901..61f30866 100644 --- a/src/errors.js +++ b/src/errors.js @@ -38,7 +38,7 @@ exports.codes = { ERR_INVALID_MULTIADDR: 'ERR_INVALID_MULTIADDR', ERR_SIGNATURE_NOT_VALID: 'ERR_SIGNATURE_NOT_VALID', ERR_FIND_SELF: 'ERR_FIND_SELF', - ERR_NO_ROUTERS: 'ERR_NO_ROUTERS', + ERR_NO_ROUTERS_AVAILABLE: 'ERR_NO_ROUTERS_AVAILABLE', ERR_CONNECTION_NOT_MULTIPLEXED: 'ERR_CONNECTION_NOT_MULTIPLEXED', ERR_NO_DIAL_TOKENS: 'ERR_NO_DIAL_TOKENS', ERR_KEYCHAIN_REQUIRED: 'ERR_KEYCHAIN_REQUIRED', diff --git a/src/index.js b/src/index.js index 2b428223..5e890479 100644 --- a/src/index.js +++ b/src/index.js @@ -56,16 +56,9 @@ const { updateSelfPeerRecord } = require('./record/utils') * @property {MuxedStream} stream * @property {string} protocol * - * @typedef {Object} RandomWalkOptions - * @property {boolean} [enabled = false] - * @property {number} [queriesPerPeriod = 1] - * @property {number} [interval = 300e3] - * @property {number} [timeout = 10e3] - * * @typedef {Object} DhtOptions * @property {boolean} [enabled = false] * @property {number} [kBucketSize = 20] - * @property {RandomWalkOptions} [randomWalk] * @property {boolean} [clientMode] * @property {import('libp2p-interfaces/src/types').DhtSelectors} [selectors] * @property {import('libp2p-interfaces/src/types').DhtValidators} [validators] diff --git a/src/peer-routing.js b/src/peer-routing.js index 09de96df..8225a5a0 100644 --- a/src/peer-routing.js +++ b/src/peer-routing.js @@ -105,7 +105,7 @@ class PeerRouting { */ async findPeer (id, options) { // eslint-disable-line require-await if (!this._routers.length) { - throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS) + throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS_AVAILABLE) } if (id.toB58String() === this._peerId.toB58String()) { @@ -145,7 +145,7 @@ class PeerRouting { */ async * getClosestPeers (key, options = { timeout: 30e3 }) { if (!this._routers.length) { - throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS) + throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS_AVAILABLE) } if (options.timeout) { diff --git a/test/content-routing/content-routing.node.js b/test/content-routing/content-routing.node.js index 7206b199..e35651e4 100644 --- a/test/content-routing/content-routing.node.js +++ b/test/content-routing/content-routing.node.js @@ -36,14 +36,14 @@ describe('content-routing', () => { throw new Error('.findProviders should return an error') } catch (/** @type {any} */ err) { expect(err).to.exist() - expect(err.code).to.equal('NO_ROUTERS_AVAILABLE') + expect(err.code).to.equal('ERR_NO_ROUTERS_AVAILABLE') } }) it('.provide should return an error', async () => { await expect(node.contentRouting.provide('a cid')) .to.eventually.be.rejected() - .and.to.have.property('code', 'NO_ROUTERS_AVAILABLE') + .and.to.have.property('code', 'ERR_NO_ROUTERS_AVAILABLE') }) }) @@ -87,8 +87,11 @@ describe('content-routing', () => { sinon.stub(nodes[0]._dht, 'findProviders').callsFake(function * () { deferred.resolve() yield { - id: providerPeerId, - multiaddrs: [] + name: 'PROVIDER', + providers: [{ + id: providerPeerId, + multiaddrs: [] + }] } }) @@ -361,7 +364,12 @@ describe('content-routing', () => { } sinon.stub(node._dht, 'findProviders').callsFake(async function * () { - yield result1 + yield { + name: 'PROVIDER', + providers: [ + result1 + ] + } }) sinon.stub(delegate, 'findProviders').callsFake(async function * () { yield result2 @@ -382,7 +390,8 @@ describe('content-routing', () => { const dhtDeferred = pDefer() const delegatedDeferred = pDefer() - sinon.stub(node._dht, 'provide').callsFake(() => { + sinon.stub(node._dht, 'provide').callsFake(async function * () { + yield dhtDeferred.resolve() }) @@ -406,7 +415,12 @@ describe('content-routing', () => { }] sinon.stub(node._dht, 'findProviders').callsFake(function * () { - yield results[0] + yield { + name: 'PROVIDER', + providers: [ + results[0] + ] + } }) sinon.stub(delegate, 'findProviders').callsFake(function * () { // eslint-disable-line require-yield diff --git a/test/content-routing/dht/configuration.node.js b/test/content-routing/dht/configuration.node.js index 9213d6e2..ed22d3e9 100644 --- a/test/content-routing/dht/configuration.node.js +++ b/test/content-routing/dht/configuration.node.js @@ -38,13 +38,13 @@ describe('DHT subsystem is configurable', () => { }) libp2p = await create(customOptions) - expect(libp2p._dht.isStarted).to.equal(false) + expect(libp2p._dht.isStarted()).to.equal(false) await libp2p.start() - expect(libp2p._dht.isStarted).to.equal(true) + expect(libp2p._dht.isStarted()).to.equal(true) await libp2p.stop() - expect(libp2p._dht.isStarted).to.equal(false) + expect(libp2p._dht.isStarted()).to.equal(false) }) it('should not start if disabled once libp2p starts', async () => { @@ -63,10 +63,10 @@ describe('DHT subsystem is configurable', () => { }) libp2p = await create(customOptions) - expect(libp2p._dht.isStarted).to.equal(false) + expect(libp2p._dht.isStarted()).to.equal(false) await libp2p.start() - expect(libp2p._dht.isStarted).to.equal(false) + expect(libp2p._dht.isStarted()).to.equal(false) }) it('should allow a manual start', async () => { @@ -86,9 +86,9 @@ describe('DHT subsystem is configurable', () => { libp2p = await create(customOptions) await libp2p.start() - expect(libp2p._dht.isStarted).to.equal(false) + expect(libp2p._dht.isStarted()).to.equal(false) await libp2p._dht.start() - expect(libp2p._dht.isStarted).to.equal(true) + expect(libp2p._dht.isStarted()).to.equal(true) }) }) diff --git a/test/content-routing/dht/operation.node.js b/test/content-routing/dht/operation.node.js index c1cfbb13..646e8431 100644 --- a/test/content-routing/dht/operation.node.js +++ b/test/content-routing/dht/operation.node.js @@ -60,8 +60,8 @@ describe('DHT subsystem operates correctly', () => { expect(connection).to.exist() return Promise.all([ - pWaitFor(() => libp2p._dht.routingTable.size === 1), - pWaitFor(() => remoteLibp2p._dht.routingTable.size === 1) + pWaitFor(() => libp2p._dht._lan._routingTable.size === 1), + pWaitFor(() => remoteLibp2p._dht._lan._routingTable.size === 1) ]) }) @@ -71,14 +71,14 @@ describe('DHT subsystem operates correctly', () => { await libp2p.dialProtocol(remAddr, subsystemMulticodecs) await Promise.all([ - pWaitFor(() => libp2p._dht.routingTable.size === 1), - pWaitFor(() => remoteLibp2p._dht.routingTable.size === 1) + pWaitFor(() => libp2p._dht._lan._routingTable.size === 1), + pWaitFor(() => remoteLibp2p._dht._lan._routingTable.size === 1) ]) await libp2p.contentRouting.put(key, value) - const fetchedValue = await remoteLibp2p.contentRouting.get(key) - expect(fetchedValue).to.eql(value) + const fetchedValue = await remoteLibp2p.contentRouting.get(key) + expect(fetchedValue).to.have.property('val').that.equalBytes(value) }) }) @@ -119,11 +119,13 @@ describe('DHT subsystem operates correctly', () => { const connection = await libp2p.dial(remAddr) expect(connection).to.exist() - expect(libp2p._dht.routingTable.size).to.be.eql(0) - expect(remoteLibp2p._dht.routingTable.size).to.be.eql(0) + expect(libp2p._dht._lan._routingTable.size).to.be.eql(0) await remoteLibp2p._dht.start() - return pWaitFor(() => libp2p._dht.routingTable.size === 1) + // should be 0 directly after start - TODO this may be susceptible to timing bugs, we should have + // the ability to report stats on the DHT routing table instead of reaching into it's heart like this + expect(remoteLibp2p._dht._lan._routingTable.size).to.be.eql(0) + return pWaitFor(() => libp2p._dht._lan._routingTable.size === 1) }) it('should put on a peer and get from the other', async () => { @@ -133,12 +135,12 @@ describe('DHT subsystem operates correctly', () => { const value = uint8ArrayFromString('world') await remoteLibp2p._dht.start() - await pWaitFor(() => libp2p._dht.routingTable.size === 1) + await pWaitFor(() => libp2p._dht._lan._routingTable.size === 1) await libp2p.contentRouting.put(key, value) const fetchedValue = await remoteLibp2p.contentRouting.get(key) - expect(fetchedValue).to.eql(value) + expect(fetchedValue).to.have.property('val').that.equalBytes(value) }) }) }) diff --git a/test/content-routing/dht/utils.js b/test/content-routing/dht/utils.js index c879f8d0..0a37de41 100644 --- a/test/content-routing/dht/utils.js +++ b/test/content-routing/dht/utils.js @@ -1,7 +1,6 @@ 'use strict' const KadDht = require('libp2p-kad-dht') -const { multicodec } = require('libp2p-kad-dht') const Crypto = require('../../../src/insecure/plaintext') const Muxer = require('libp2p-mplex') const Transport = require('libp2p-tcp') @@ -25,13 +24,12 @@ const subsystemOptions = mergeOptions(baseOptions, { config: { dht: { kBucketSize: 20, - randomWalk: { - enabled: true - }, enabled: true } } }) module.exports.subsystemOptions = subsystemOptions -module.exports.subsystemMulticodecs = [multicodec] +module.exports.subsystemMulticodecs = [ + '/ipfs/lan/kad/1.0.0' +] diff --git a/test/content-routing/utils.js b/test/content-routing/utils.js index 120f7281..7b43d050 100644 --- a/test/content-routing/utils.js +++ b/test/content-routing/utils.js @@ -13,9 +13,6 @@ const routingOptions = mergeOptions(baseOptions, { config: { dht: { kBucketSize: 20, - randomWalk: { - enabled: true - }, enabled: true } } diff --git a/test/nat-manager/nat-manager.node.js b/test/nat-manager/nat-manager.node.js index 5f5562e0..9dcf2b57 100644 --- a/test/nat-manager/nat-manager.node.js +++ b/test/nat-manager/nat-manager.node.js @@ -244,6 +244,10 @@ describe('Nat Manager (TCP)', () => { }) it('shuts the nat api down when stopping', async function () { + if (process.env.CI) { + return this.skip('CI environments will not let us map external ports') + } + function findRoutableAddress () { const interfaces = networkInterfaces() @@ -261,7 +265,7 @@ describe('Nat Manager (TCP)', () => { if (!addr) { // skip test if no non-loopback address is found - this.skip() + return this.skip() } const { diff --git a/test/peer-discovery/index.node.js b/test/peer-discovery/index.node.js index c7db9646..bdf45879 100644 --- a/test/peer-discovery/index.node.js +++ b/test/peer-discovery/index.node.js @@ -161,20 +161,13 @@ describe('peer discovery scenarios', () => { autoDial: false }, dht: { - randomWalk: { - enabled: false, - delay: 1000, // start the first query quickly - interval: 10000, - timeout: 5000 - }, enabled: true } } }) const localConfig = getConfig(peerId) - // Only run random walk on our local node - localConfig.config.dht.randomWalk.enabled = true + libp2p = new Libp2p(localConfig) const remoteLibp2p1 = new Libp2p(getConfig(remotePeerId1)) diff --git a/test/peer-routing/peer-routing.node.js b/test/peer-routing/peer-routing.node.js index a1c66917..f7835898 100644 --- a/test/peer-routing/peer-routing.node.js +++ b/test/peer-routing/peer-routing.node.js @@ -36,7 +36,7 @@ describe('peer-routing', () => { it('.findPeer should return an error', async () => { await expect(node.peerRouting.findPeer('a cid')) .to.eventually.be.rejected() - .and.to.have.property('code', 'NO_ROUTERS_AVAILABLE') + .and.to.have.property('code', 'ERR_NO_ROUTERS_AVAILABLE') }) it('.getClosestPeers should return an error', async () => { @@ -45,7 +45,7 @@ describe('peer-routing', () => { throw new Error('.getClosestPeers should return an error') } catch (/** @type {any} */ err) { expect(err).to.exist() - expect(err.code).to.equal('NO_ROUTERS_AVAILABLE') + expect(err.code).to.equal('ERR_NO_ROUTERS_AVAILABLE') } }) }) @@ -72,33 +72,38 @@ describe('peer-routing', () => { after(() => Promise.all(nodes.map((n) => n.stop()))) - it('should use the nodes dht', () => { - const deferred = pDefer() - - sinon.stub(nodes[0]._dht, 'findPeer').callsFake(() => { - deferred.resolve() - return nodes[1].peerId - }) - - nodes[0].peerRouting.findPeer() - return deferred.promise - }) - - it('should use the nodes dht to get the closest peers', async () => { - const deferred = pDefer() - const [remotePeerId] = await peerUtils.createPeerId({ fixture: false }) - - sinon.stub(nodes[0]._dht, 'getClosestPeers').callsFake(function * () { - deferred.resolve() + it('should use the nodes dht', async () => { + sinon.stub(nodes[0]._dht, 'findPeer').callsFake(async function * () { yield { - id: remotePeerId, - multiaddrs: [] + name: 'PEER_RESPONSE', + closer: [{ + id: nodes[1].peerId, + multiaddrs: [] + }] } }) - await nodes[0].peerRouting.getClosestPeers().next() + expect(nodes[0]._dht.findPeer.called).to.be.false() + await nodes[0].peerRouting.findPeer(nodes[1].peerId) + expect(nodes[0]._dht.findPeer.called).to.be.true() + nodes[0]._dht.findPeer.restore() + }) - return deferred.promise + it('should use the nodes dht to get the closest peers', async () => { + sinon.stub(nodes[0]._dht, 'getClosestPeers').callsFake(async function * () { + yield { + name: 'PEER_RESPONSE', + closer: [{ + id: nodes[1].peerId, + multiaddrs: [] + }] + } + }) + + expect(nodes[0]._dht.getClosestPeers.called).to.be.false() + await drain(nodes[0].peerRouting.getClosestPeers(nodes[1].peerId)) + expect(nodes[0]._dht.getClosestPeers.called).to.be.true() + nodes[0]._dht.getClosestPeers.restore() }) it('should error when peer tries to find itself', async () => { @@ -234,36 +239,35 @@ describe('peer-routing', () => { }) it('should use the delegate router to find peers', async () => { - const deferred = pDefer() const [remotePeerId] = await peerUtils.createPeerId({ fixture: false }) sinon.stub(delegate, 'findPeer').callsFake(() => { - deferred.resolve() return { id: remotePeerId, multiaddrs: [] } }) - await node.peerRouting.findPeer() - return deferred.promise + expect(delegate.findPeer.called).to.be.false() + await node.peerRouting.findPeer(remotePeerId) + expect(delegate.findPeer.called).to.be.true() + delegate.findPeer.restore() }) it('should use the delegate router to get the closest peers', async () => { - const deferred = pDefer() const [remotePeerId] = await peerUtils.createPeerId({ fixture: false }) sinon.stub(delegate, 'getClosestPeers').callsFake(function * () { - deferred.resolve() yield { id: remotePeerId, multiaddrs: [] } }) - await node.peerRouting.getClosestPeers().next() - - return deferred.promise + expect(delegate.getClosestPeers.called).to.be.false() + await drain(node.peerRouting.getClosestPeers(remotePeerId)) + expect(delegate.getClosestPeers.called).to.be.true() + delegate.getClosestPeers.restore() }) it('should be able to find a peer', async () => { @@ -289,7 +293,7 @@ describe('peer-routing', () => { }) it('should error when a peer cannot be found', async () => { - const peerKey = 'key of a peer not on the network' + const peerId = await PeerId.create({ keyType: 'ed25519' }) const mockApi = nock('http://0.0.0.0:60197') .post('/api/v0/dht/findpeer') .query(true) @@ -298,20 +302,20 @@ describe('peer-routing', () => { 'X-Chunked-Output', '1' ]) - await expect(node.peerRouting.findPeer(peerKey)) + await expect(node.peerRouting.findPeer(peerId)) .to.eventually.be.rejected() expect(mockApi.isDone()).to.equal(true) }) it('should handle errors from the api', async () => { - const peerKey = 'key of a peer not on the network' + const peerId = await PeerId.create({ keyType: 'ed25519' }) const mockApi = nock('http://0.0.0.0:60197') .post('/api/v0/dht/findpeer') .query(true) .reply(502) - await expect(node.peerRouting.findPeer(peerKey)) + await expect(node.peerRouting.findPeer(peerId)) .to.eventually.be.rejected() expect(mockApi.isDone()).to.equal(true) @@ -319,7 +323,6 @@ describe('peer-routing', () => { it('should be able to get the closest peers', async () => { const peerId = await PeerId.create({ keyType: 'ed25519' }) - const closest1 = '12D3KooWLewYMMdGWAtuX852n4rgCWkK7EBn4CWbwwBzhsVoKxk3' const closest2 = '12D3KooWDtoQbpKhtnWddfj72QmpFvvLDTsBLTFkjvgQm6cde2AK' @@ -338,15 +341,12 @@ describe('peer-routing', () => { 'X-Chunked-Output', '1' ]) - const closestPeers = [] - for await (const peer of node.peerRouting.getClosestPeers(peerId.id, { timeout: 1000 })) { - closestPeers.push(peer) - } + const closestPeers = await all(node.peerRouting.getClosestPeers(peerId.id, { timeout: 1000 })) expect(closestPeers).to.have.length(2) - expect(closestPeers[0].id.toB58String()).to.equal(closest2) + expect(closestPeers[0].id.toB58String()).to.equal(closest1) expect(closestPeers[0].multiaddrs).to.have.lengthOf(2) - expect(closestPeers[1].id.toB58String()).to.equal(closest1) + expect(closestPeers[1].id.toB58String()).to.equal(closest2) expect(closestPeers[1].multiaddrs).to.have.lengthOf(2) expect(mockApi.isDone()).to.equal(true) }) @@ -405,7 +405,7 @@ describe('peer-routing', () => { multiaddrs: [] } - sinon.stub(node._dht, 'findPeer').callsFake(() => {}) + sinon.stub(node._dht, 'findPeer').callsFake(async function * () {}) sinon.stub(delegate, 'findPeer').callsFake(() => { return results }) @@ -423,7 +423,8 @@ describe('peer-routing', () => { const defer = pDefer() - sinon.stub(node._dht, 'findPeer').callsFake(async () => { + sinon.stub(node._dht, 'findPeer').callsFake(async function * () { + yield await defer.promise }) sinon.stub(delegate, 'findPeer').callsFake(() => { @@ -438,29 +439,34 @@ describe('peer-routing', () => { it('should not wait for the delegate to return if the dht does first', async () => { const [remotePeerId] = await peerUtils.createPeerId({ fixture: false }) - const results = { + const result = { id: remotePeerId, multiaddrs: [] } const defer = pDefer() - sinon.stub(node._dht, 'findPeer').callsFake(() => { - return results + sinon.stub(node._dht, 'findPeer').callsFake(async function * () { + yield { + name: 'PEER_RESPONSE', + closer: [ + result + ] + } }) sinon.stub(delegate, 'findPeer').callsFake(async () => { await defer.promise }) const peer = await node.peerRouting.findPeer(remotePeerId) - expect(peer).to.eql(results) + expect(peer).to.eql(result) defer.resolve() }) it('should store the addresses of the found peer', async () => { const [remotePeerId] = await peerUtils.createPeerId({ fixture: false }) - const results = { + const result = { id: remotePeerId, multiaddrs: [ new Multiaddr('/ip4/123.123.123.123/tcp/38982') @@ -469,14 +475,19 @@ describe('peer-routing', () => { const spy = sinon.spy(node.peerStore.addressBook, 'add') - sinon.stub(node._dht, 'findPeer').callsFake(() => { - return results + sinon.stub(node._dht, 'findPeer').callsFake(async function * () { + yield { + name: 'PEER_RESPONSE', + closer: [ + result + ] + } }) sinon.stub(delegate, 'findPeer').callsFake(() => {}) await node.peerRouting.findPeer(remotePeerId) - expect(spy.calledWith(results.id, results.multiaddrs)).to.be.true() + expect(spy.calledWith(result.id, result.multiaddrs)).to.be.true() }) it('should use the delegate if the dht fails to get the closest peer', async () => { @@ -576,8 +587,18 @@ describe('peer-routing', () => { sinon.spy(node.peerStore.addressBook, 'add') sinon.stub(node._dht, 'getClosestPeers').callsFake(function * () { - yield results[0] - yield results[1] + yield { + name: 'PEER_RESPONSE', + closer: [ + results[0] + ] + } + yield { + name: 'PEER_RESPONSE', + closer: [ + results[1] + ] + } }) await node.start() @@ -611,7 +632,7 @@ describe('peer-routing', () => { started: false }) - sinon.stub(node._dht, 'getClosestPeers').callsFake(function * () { + sinon.stub(node._dht, 'getClosestPeers').callsFake(async function * () { yield throw new Error('should not be called') }) diff --git a/test/peer-routing/utils.js b/test/peer-routing/utils.js index 120f7281..7b43d050 100644 --- a/test/peer-routing/utils.js +++ b/test/peer-routing/utils.js @@ -13,9 +13,6 @@ const routingOptions = mergeOptions(baseOptions, { config: { dht: { kBucketSize: 20, - randomWalk: { - enabled: true - }, enabled: true } } diff --git a/test/transports/transport-manager.node.js b/test/transports/transport-manager.node.js index 13854ce0..49876a05 100644 --- a/test/transports/transport-manager.node.js +++ b/test/transports/transport-manager.node.js @@ -13,6 +13,7 @@ const { Multiaddr } = require('multiaddr') const mockUpgrader = require('../utils/mockUpgrader') const sinon = require('sinon') const Peers = require('../fixtures/peers') +const pWaitFor = require('p-wait-for') const addrs = [ new Multiaddr('/ip4/127.0.0.1/tcp/0'), new Multiaddr('/ip4/127.0.0.1/tcp/0') @@ -72,9 +73,13 @@ describe('Transport Manager (TCP)', () => { tm.add(Transport.prototype[Symbol.toStringTag], Transport) await tm.listen(addrs) - // Should created Self Peer record on new listen address - signedPeerRecord = await tm.libp2p.peerStore.addressBook.getPeerRecord(localPeer) - expect(signedPeerRecord).to.exist() + // Should created Self Peer record on new listen address, but it is done async + // with no event so we have to wait a bit + await pWaitFor(async () => { + signedPeerRecord = await tm.libp2p.peerStore.addressBook.getPeerRecord(localPeer) + + return signedPeerRecord != null + }, { interval: 100, timeout: 2000 }) const record = PeerRecord.createFromProtobuf(signedPeerRecord.payload) expect(record).to.exist() diff --git a/test/ts-use/package.json b/test/ts-use/package.json index 8593e192..ac98a7b4 100644 --- a/test/ts-use/package.json +++ b/test/ts-use/package.json @@ -7,7 +7,7 @@ "libp2p": "file:../..", "libp2p-bootstrap": "^0.13.0", "libp2p-delegated-content-routing": "^0.11.0", - "libp2p-delegated-peer-routing": "^0.10.0", + "libp2p-delegated-peer-routing": "^0.11.1", "libp2p-gossipsub": "^0.9.0", "libp2p-interfaces": "^1.0.1", "libp2p-kad-dht": "^0.26.5", diff --git a/test/ts-use/src/main.ts b/test/ts-use/src/main.ts index d75a79f4..17c5e4d6 100644 --- a/test/ts-use/src/main.ts +++ b/test/ts-use/src/main.ts @@ -123,11 +123,6 @@ async function main() { dht: { enabled: true, kBucketSize: 20, - randomWalk: { - enabled: true, // Allows to disable discovery (enabled by default) - interval: 300e3, - timeout: 10e3 - }, clientMode: true, validators: { pk: Libp2pRecord.validator.validators.pk