Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: send correct payload in ADD_PROVIDER RPC (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored and vasco-santos committed May 30, 2019
1 parent 8d6106b commit 8d92d5a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
bundlesize: { maxSize: '196kB' }
bundlesize: { maxSize: '197kB' }
}

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
16 changes: 16 additions & 0 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions test/rpc/handlers/add-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8d92d5a

Please sign in to comment.