From 3c417243630266d239aea8fab3574f5afde53eea Mon Sep 17 00:00:00 2001 From: David Dias Date: Sun, 16 Jul 2017 19:17:25 +0100 Subject: [PATCH] =?UTF-8?q?there=20were=20more=20tests=20:)=20fix=20remain?= =?UTF-8?q?ing=20things,=20test=20with=20new=20swarm=20=E2=9C=94=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 6 +++--- src/private.js | 6 +++--- src/rpc/handlers/find-node.js | 4 ++-- src/rpc/handlers/get-providers.js | 2 +- src/rpc/handlers/get-value.js | 4 ++-- test/kad-dht.spec.js | 2 +- test/rpc/handlers/add-provider.spec.js | 9 +++++---- test/rpc/handlers/find-node.spec.js | 11 ++++++----- test/rpc/handlers/get-providers.spec.js | 20 +++++++++----------- test/rpc/handlers/get-value.spec.js | 16 ++++++++-------- test/rpc/handlers/ping.spec.js | 6 +++--- test/rpc/handlers/put-value.spec.js | 17 +++++++++++------ test/rpc/index.spec.js | 25 ++++++++++++++++--------- 13 files changed, 70 insertions(+), 58 deletions(-) diff --git a/src/index.js b/src/index.js index 67ac5989..d9e1646b 100644 --- a/src/index.js +++ b/src/index.js @@ -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') @@ -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) } @@ -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) }) diff --git a/src/private.js b/src/private.js index 22c76b5f..0c99a971 100644 --- a/src/private.js +++ b/src/private.js @@ -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) } @@ -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)) { @@ -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)) diff --git a/src/rpc/handlers/find-node.js b/src/rpc/handlers/find-node.js index e10f85d6..a0829a37 100644 --- a/src/rpc/handlers/find-node.js +++ b/src/rpc/handlers/find-node.js @@ -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) diff --git a/src/rpc/handlers/get-providers.js b/src/rpc/handlers/get-providers.js index c998f42b..fbb590f7 100644 --- a/src/rpc/handlers/get-providers.js +++ b/src/rpc/handlers/get-providers.js @@ -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) diff --git a/src/rpc/handlers/get-value.js b/src/rpc/handlers/get-value.js index a7d58bc0..e4e5e355 100644 --- a/src/rpc/handlers/get-value.js +++ b/src/rpc/handlers/get-value.js @@ -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) } } diff --git a/test/kad-dht.spec.js b/test/kad-dht.spec.js index 9a3d3929..d741c4f4 100644 --- a/test/kad-dht.spec.js +++ b/test/kad-dht.spec.js @@ -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() }) }) diff --git a/test/rpc/handlers/add-provider.spec.js b/test/rpc/handlers/add-provider.spec.js index 082623e8..0360a46e 100644 --- a/test/rpc/handlers/add-provider.spec.js +++ b/test/rpc/handlers/add-provider.spec.js @@ -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 @@ -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/ }] diff --git a/test/rpc/handlers/find-node.spec.js b/test/rpc/handlers/find-node.spec.js index aecaa395..16448dda 100644 --- a/test/rpc/handlers/find-node.spec.js +++ b/test/rpc/handlers/find-node.spec.js @@ -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 @@ -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([ @@ -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() diff --git a/test/rpc/handlers/get-providers.spec.js b/test/rpc/handlers/get-providers.spec.js index 768e9ef6..89805383 100644 --- a/test/rpc/handlers/get-providers.spec.js +++ b/test/rpc/handlers/get-providers.spec.js @@ -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 @@ -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/) @@ -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() }) @@ -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() }) }) diff --git a/test/rpc/handlers/get-value.spec.js b/test/rpc/handlers/get-value.spec.js index 49718e4a..bfc61737 100644 --- a/test/rpc/handlers/get-value.spec.js +++ b/test/rpc/handlers/get-value.spec.js @@ -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 @@ -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/) @@ -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([ @@ -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] @@ -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) @@ -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() }) diff --git a/test/rpc/handlers/ping.spec.js b/test/rpc/handlers/ping.spec.js index bf49f311..b1558583 100644 --- a/test/rpc/handlers/ping.spec.js +++ b/test/rpc/handlers/ping.spec.js @@ -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 @@ -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() diff --git a/test/rpc/handlers/put-value.spec.js b/test/rpc/handlers/put-value.spec.js index 13856e7e..84c67c5f 100644 --- a/test/rpc/handlers/put-value.spec.js +++ b/test/rpc/handlers/put-value.spec.js @@ -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 @@ -38,7 +39,7 @@ 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() @@ -46,20 +47,24 @@ describe('rpc - handlers - PutValue', () => { }) 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(() => { diff --git a/test/rpc/index.spec.js b/test/rpc/index.spec.js index 5426d979..8e60d69b 100644 --- a/test/rpc/index.spec.js +++ b/test/rpc/index.spec.js @@ -7,16 +7,19 @@ const expect = chai.expect const pull = require('pull-stream') const lp = require('pull-length-prefixed') const Connection = require('interface-connection').Connection -const Libp2p = require('./../nodejs-bundle') +const PeerBook = require('peer-book') +const Swarm = require('libp2p-swarm') +const TCP = require('libp2p-tcp') +const Multiplex = require('libp2p-multiplex') const Message = require('../../src/message') -const Dht = require('../../src') +const KadDHT = require('../../src') const rpc = require('../../src/rpc') -const makePeers = require('../util').makePeers +const makePeers = require('../utils').makePeers describe('rpc', () => { - let infos + let peerInfos before((done) => { makePeers(2, (err, peers) => { @@ -24,20 +27,24 @@ describe('rpc', () => { return done(err) } - infos = peers + peerInfos = peers done() }) }) describe('protocolHandler', () => { it('calls back with the response', (done) => { - const libp2p = new Libp2p(infos[0]) - const dht = new Dht(libp2p) - dht.peerBook.put(infos[1]) + const swarm = new Swarm(peerInfos[0], new PeerBook()) + swarm.transport.add('tcp', new TCP()) + swarm.connection.addStreamMuxer(Multiplex) + swarm.connection.reuse() + const dht = new KadDHT(swarm, { kBucketSize: 5 }) + + dht.peerBook.put(peerInfos[1]) const msg = new Message(Message.TYPES.GET_VALUE, Buffer.from('hello'), 5) - const conn = makeConnection(msg, infos[1], (err, res) => { + const conn = makeConnection(msg, peerInfos[1], (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) const msg = Message.deserialize(res[0])