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

Commit

Permalink
there were more tests :) fix remaining things, test with new swarm ✔️
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jul 16, 2017
1 parent 96f2af8 commit 3c41724
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class KadDHT {
// local check
let info
if (this.peerBook.has(peer)) {
info = this.libp2p.peerBook.get(peer)
info = this.peerBook.get(peer)

if (info && info.id.pubKey) {
this._log('getPublicKey: found local copy')
Expand All @@ -349,7 +349,7 @@ class KadDHT {
this._getPublicKeyFromNode(peer, (err, pk) => {
if (!err) {
info.id = new PeerId(peer.id, null, pk)
this.libp2p.peerBook.put(info)
this.peerBook.put(info)

return callback(null, pk)
}
Expand All @@ -363,7 +363,7 @@ class KadDHT {

const pk = crypto.unmarshalPublicKey(value)
info.id = new PeerId(peer, null, pk)
this.libp2p.peerBook.put(info)
this.peerBook.put(info)

callback(null, pk)
})
Expand Down
6 changes: 3 additions & 3 deletions src/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ module.exports = (dht) => ({
// 5. check validity

// 5. if: we are the author, all good
if (record.author.isEqual(dht.self.id)) {
if (record.author.isEqual(dht.peerInfo.id)) {
return callback(null, record)
}

Expand Down Expand Up @@ -308,7 +308,7 @@ module.exports = (dht) => ({

// Send out correction record
waterfall([
(cb) => utils.createPutRecord(key, best, dht.self.id, true, cb),
(cb) => utils.createPutRecord(key, best, dht.peerInfo.id, true, cb),
(fixupRec, cb) => each(vals, (v, cb) => {
// no need to do anything
if (v.val.equals(best)) {
Expand Down Expand Up @@ -523,7 +523,7 @@ module.exports = (dht) => ({
(cb) => dht._findProvidersSingle(peer, key, cb),
(msg, cb) => {
const provs = msg.providerPeers
dht._log('(%s) found %s provider entries', dht.self.id.toB58String(), provs.length)
dht._log('(%s) found %s provider entries', dht.peerInfo.id.toB58String(), provs.length)

provs.forEach((prov) => {
out.push(dht.peerBook.put(prov))
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/handlers/find-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module.exports = (dht) => {

waterfall([
(cb) => {
if (msg.key.equals(dht.self.id.id)) {
return cb(null, [dht.self])
if (msg.key.equals(dht.peerInfo.id.id)) {
return cb(null, [dht.peerInfo])
}

dht._betterPeersToQuery(msg, peer, cb)
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/handlers/get-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = (dht) => {
})

if (has) {
providers.push(dht.self)
providers.push(dht.peerInfo)
}

const response = new Message(msg.type, msg.key, msg.clusterLevel)
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/handlers/get-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ module.exports = (dht) => {
let info

if (dht._isSelf(id)) {
info = dht.self
info = dht.peerInfo
} else if (dht.peerBook.has(id)) {
info = dht.peerBook.get(id)
}

if (info && info.id.pubKey) {
log('returning found public key')
response.record = new Record(key, info.id.pubKey.bytes, dht.self.id)
response.record = new Record(key, info.id.pubKey.bytes, dht.peerInfo.id)
return callback(null, response)
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe('KadDHT', () => {
dhts[0].peerBook.put(dhts[1].peerInfo)
dhts[0].getPublicKey(ids[1], (err, key) => {
expect(err).to.not.exist()
expect(key).to.be.eql(dhts[1].self.id.pubKey)
expect(key).to.be.eql(dhts[1].peerInfo.id.pubKey)
done()
})
})
Expand Down
9 changes: 5 additions & 4 deletions test/rpc/handlers/add-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ const expect = chai.expect
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const _ = require('lodash')
const Buffer = require('safe-buffer').Buffer

const Message = require('../../../src/message')

const handler = require('../../../src/rpc/handlers/add-provider')

const util = require('../../util')
const util = require('../../utils')

describe('rpc - handlers - AddProvider', () => {
let peers
Expand Down Expand Up @@ -46,13 +47,13 @@ describe('rpc - handlers - AddProvider', () => {

describe('invalid messages', () => {
const tests = [{
message: new Message(Message.TYPES.ADD_PROVIDER, new Buffer(0), 0),
message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.alloc(0), 0),
error: /Missing key/
}, {
message: new Message(Message.TYPES.ADD_PROVIDER, new Buffer(0), 0),
message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.alloc(0), 0),
error: /Missing key/
}, {
message: new Message(Message.TYPES.ADD_PROVIDER, new Buffer('hello world'), 0),
message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.from('hello world'), 0),
error: /Invalid CID/
}]

Expand Down
11 changes: 6 additions & 5 deletions test/rpc/handlers/find-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const waterfall = require('async/waterfall')
const Buffer = require('safe-buffer').Buffer

const Message = require('../../../src/message')
const handler = require('../../../src/rpc/handlers/find-node')
const util = require('../../util')
const util = require('../../utils')

const T = Message.TYPES.FIND_NODE

Expand All @@ -35,20 +36,20 @@ describe('rpc - handlers - FindNode', () => {
})

it('returns self, if asked for self', (done) => {
const msg = new Message(T, dht.self.id.id, 0)
const msg = new Message(T, dht.peerInfo.id.id, 0)

handler(dht)(peers[1], msg, (err, response) => {
expect(err).to.not.exist()
expect(response.closerPeers).to.have.length(1)
const peer = response.closerPeers[0]

expect(peer.id.id).to.be.eql(dht.self.id.id)
expect(peer.id.id).to.be.eql(dht.peerInfo.id.id)
done()
})
})

it('returns closer peers', (done) => {
const msg = new Message(T, new Buffer('hello'), 0)
const msg = new Message(T, Buffer.from('hello'), 0)
const other = peers[1]

waterfall([
Expand All @@ -71,7 +72,7 @@ describe('rpc - handlers - FindNode', () => {
})

it('handles no peers found', (done) => {
const msg = new Message(T, new Buffer('hello'), 0)
const msg = new Message(T, Buffer.from('hello'), 0)

handler(dht)(peers[2], msg, (err, response) => {
expect(err).to.not.exist()
Expand Down
20 changes: 9 additions & 11 deletions test/rpc/handlers/get-providers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const Buffer = require('safe-buffer').Buffer

const Message = require('../../../src/message')
const utils = require('../../../src/utils')
const handler = require('../../../src/rpc/handlers/get-providers')
const util = require('../../util')
const util = require('../../utils')

const T = Message.TYPES.GET_PROVIDERS

Expand Down Expand Up @@ -42,7 +43,7 @@ describe('rpc - handlers - GetProviders', () => {
})

it('errors with an invalid key ', (done) => {
const msg = new Message(T, new Buffer('hello'), 0)
const msg = new Message(T, Buffer.from('hello'), 0)

handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.match(/Invalid CID/)
Expand All @@ -65,9 +66,8 @@ describe('rpc - handlers - GetProviders', () => {

expect(response.key).to.be.eql(v.cid.buffer)
expect(response.providerPeers).to.have.length(1)
expect(
response.providerPeers[0].id.toB58String()
).to.be.eql(dht.self.id.toB58String())
expect(response.providerPeers[0].id.toB58String())
.to.eql(dht.peerInfo.id.toB58String())

done()
})
Expand All @@ -89,14 +89,12 @@ describe('rpc - handlers - GetProviders', () => {

expect(response.key).to.be.eql(v.cid.buffer)
expect(response.providerPeers).to.have.length(1)
expect(
response.providerPeers[0].id.toB58String()
).to.be.eql(prov.toB58String())
expect(response.providerPeers[0].id.toB58String())
.to.eql(prov.toB58String())

expect(response.closerPeers).to.have.length(1)
expect(
response.closerPeers[0].id.toB58String()
).to.be.eql(closer.id.toB58String())
expect(response.closerPeers[0].id.toB58String())
.to.eql(closer.id.toB58String())
done()
})
})
Expand Down
16 changes: 8 additions & 8 deletions test/rpc/handlers/get-value.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const waterfall = require('async/waterfall')

const Buffer = require('safe-buffer').Buffer
const Message = require('../../../src/message')
const handler = require('../../../src/rpc/handlers/get-value')
const utils = require('../../../src/utils')
const util = require('../../util')
const util = require('../../utils')

const T = Message.TYPES.GET_VALUE

Expand All @@ -36,7 +36,7 @@ describe('rpc - handlers - GetValue', () => {
})

it('errors when missing key', (done) => {
const msg = new Message(T, new Buffer(0), 0)
const msg = new Message(T, Buffer.alloc(0), 0)

handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.match(/Invalid key/)
Expand All @@ -46,8 +46,8 @@ describe('rpc - handlers - GetValue', () => {
})

it('responds with a local value', (done) => {
const key = new Buffer('hello')
const value = new Buffer('world')
const key = Buffer.from('hello')
const value = Buffer.from('world')
const msg = new Message(T, key, 0)

waterfall([
Expand All @@ -63,7 +63,7 @@ describe('rpc - handlers - GetValue', () => {
})

it('responds with closerPeers returned from the dht', (done) => {
const key = new Buffer('hello')
const key = Buffer.from('hello')
const msg = new Message(T, key, 0)
const other = peers[1]

Expand All @@ -82,7 +82,7 @@ describe('rpc - handlers - GetValue', () => {

describe('public key', () => {
it('self', (done) => {
const key = utils.keyForPublicKey(dht.self.id)
const key = utils.keyForPublicKey(dht.peerInfo.id)

const msg = new Message(T, key, 0)

Expand All @@ -92,7 +92,7 @@ describe('rpc - handlers - GetValue', () => {
expect(err).to.not.exist()
expect(response.record).to.exist()
expect(response.record.value).to.eql(
dht.self.id.pubKey.bytes
dht.peerInfo.id.pubKey.bytes
)
done()
})
Expand Down
6 changes: 3 additions & 3 deletions test/rpc/handlers/ping.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const Buffer = require('safe-buffer').Buffer
const Message = require('../../../src/message')
const handler = require('../../../src/rpc/handlers/ping')

const util = require('../../util')
const util = require('../../utils')

const T = Message.TYPES.PING

Expand All @@ -35,7 +35,7 @@ describe('rpc - handlers - Ping', () => {
})

it('replies with the same message', (done) => {
const msg = new Message(T, new Buffer('hello'), 5)
const msg = new Message(T, Buffer.from('hello'), 5)

handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.not.exist()
Expand Down
17 changes: 11 additions & 6 deletions test/rpc/handlers/put-value.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Record = require('libp2p-record').Record
const Buffer = require('safe-buffer').Buffer

const Message = require('../../../src/message')
const handler = require('../../../src/rpc/handlers/put-value')
const utils = require('../../../src/utils')

const util = require('../../util')
const util = require('../../utils')

const T = Message.TYPES.PUT_VALUE

Expand All @@ -38,28 +39,32 @@ describe('rpc - handlers - PutValue', () => {
})

it('errors on missing record', (done) => {
const msg = new Message(T, new Buffer('hello'), 5)
const msg = new Message(T, Buffer.from('hello'), 5)
handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.match(/Empty record/)
done()
})
})

it('stores the record in the datastore', (done) => {
const msg = new Message(T, new Buffer('hello'), 5)
const record = new Record(new Buffer('hello'), new Buffer('world'), peers[0].id)
const msg = new Message(T, Buffer.from('hello'), 5)
const record = new Record(
Buffer.from('hello'),
Buffer.from('world'),
peers[0].id
)
msg.record = record

handler(dht)(peers[1], msg, (err, response) => {
expect(err).to.not.exist()
expect(response).to.be.eql(msg)

const key = utils.bufferToKey(new Buffer('hello'))
const key = utils.bufferToKey(Buffer.from('hello'))
dht.datastore.get(key, (err, res) => {
expect(err).to.not.exist()
const rec = Record.deserialize(res)

expect(rec).to.have.property('key').eql(new Buffer('hello'))
expect(rec).to.have.property('key').eql(Buffer.from('hello'))

// make sure some time has passed
setTimeout(() => {
Expand Down
Loading

0 comments on commit 3c41724

Please sign in to comment.