diff --git a/.aegir.js b/.aegir.js index 2ae51af7..8bf94327 100644 --- a/.aegir.js +++ b/.aegir.js @@ -1,4 +1,4 @@ module.exports = { - bundlesize: { maxSize: '196kB' } + bundlesize: { maxSize: '197kB' } } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 5671abb1..7134dd11 100644 --- a/src/index.js +++ b/src/index.js @@ -541,7 +541,7 @@ class KadDHT extends EventEmitter { (cb) => this.getClosestPeers(key.buffer, cb), (peers, cb) => { const msg = new Message(Message.TYPES.ADD_PROVIDER, key.buffer, 0) - msg.providerPeers = peers.map((p) => new PeerInfo(p)) + msg.providerPeers = [this.peerInfo] each(peers, (peer, cb) => { this._log('putProvider %s to %s', key.toBaseEncodedString(), peer.toB58String()) diff --git a/test/kad-dht.spec.js b/test/kad-dht.spec.js index 7a7d26ec..17e1464d 100644 --- a/test/kad-dht.spec.js +++ b/test/kad-dht.spec.js @@ -26,6 +26,7 @@ const errcode = require('err-code') const KadDHT = require('../src') const kadUtils = require('../src/utils') const c = require('../src/constants') +const Message = require('../src/message') const createPeerInfo = require('./utils/create-peer-info') const createValues = require('./utils/create-values') @@ -510,6 +511,8 @@ describe('KadDHT', () => { expect(err).to.not.exist() const addrs = dhts.map((d) => d.peerInfo.multiaddrs.toArray()[0]) const ids = dhts.map((d) => d.peerInfo.id) + const idsB58 = ids.map(id => id.toB58String()) + sinon.spy(dhts[3].network, 'sendMessage') series([ (cb) => connect(dhts[0], dhts[1], cb), @@ -519,6 +522,19 @@ describe('KadDHT', () => { dhts[3].provide(v.cid, cb) }, cb), (cb) => { + // Expect an ADD_PROVIDER message to be sent to each peer for each value + const fn = dhts[3].network.sendMessage + const valuesBuffs = values.map(v => v.cid.buffer) + const calls = fn.getCalls().map(c => c.args) + for (const [peerId, msg] of calls) { + expect(idsB58).includes(peerId.toB58String()) + expect(msg.type).equals(Message.TYPES.ADD_PROVIDER) + expect(valuesBuffs).includes(msg.key) + expect(msg.providerPeers.length).equals(1) + expect(msg.providerPeers[0].id.toB58String()).equals(idsB58[3]) + } + + // Expect each DHT to find the provider of each value let n = 0 each(values, (v, cb) => { n = (n + 1) % 3 diff --git a/test/rpc/handlers/add-provider.spec.js b/test/rpc/handlers/add-provider.spec.js index 42942ac7..ceb1e305 100644 --- a/test/rpc/handlers/add-provider.spec.js +++ b/test/rpc/handlers/add-provider.spec.js @@ -52,9 +52,6 @@ describe('rpc - handlers - AddProvider', () => { const tests = [{ message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.alloc(0), 0), error: 'ERR_MISSING_KEY' - }, { - message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.alloc(0), 0), - error: 'ERR_MISSING_KEY' }, { message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.from('hello world'), 0), error: 'ERR_INVALID_CID'