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

Commit

Permalink
chore: add option for allowing to disable random walk
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 27, 2018
1 parent 45dfb33 commit f0bd76c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"lodash": "^4.17.10",
"lodash.random": "^3.2.0",
"lodash.range": "^3.2.0",
"peer-book": "~0.8.0"
"peer-book": "~0.8.0",
"sinon": "^6.3.4"
},
"contributors": [
"David Dias <[email protected]>",
Expand Down
22 changes: 19 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ class KadDHT {
/**
* Create a new KadDHT.
*
* @param {Switch} sw
* @param {object} options // {kBucketSize=20, datastore=MemoryDatastore}
* @param {Switch} sw libp2p-switch instance
* @param {object} options DHT options
* @param {number} options.kBucketSize k-bucket size (default 20)
* @param {Datastore} options.datastore datastore (default MemoryDatastore)
* @param {boolean} options.enabledDiscovery enable dht discovery (default true)
*/
constructor (sw, options) {
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
Expand Down Expand Up @@ -96,6 +99,11 @@ class KadDHT {
* @type {RandomWalk}
*/
this.randomWalk = new RandomWalk(this)

/**
* Random walk state, default true
*/
this.randomWalkEnabled = !options.hasOwnProperty('enabledDiscovery') ? true : Boolean(options.enabledDiscovery)
}

/**
Expand All @@ -115,7 +123,15 @@ class KadDHT {
*/
start (callback) {
this._running = true
this.network.start(callback)
this.network.start((err) => {
if (err) {
return callback(err)
}

// Start random walk if enabled
this.randomWalkEnabled && this.randomWalk.start()
callback()
})
}

/**
Expand Down
66 changes: 65 additions & 1 deletion test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const sinon = require('sinon')
const series = require('async/series')
const times = require('async/times')
const parallel = require('async/parallel')
Expand Down Expand Up @@ -135,6 +136,68 @@ describe('KadDHT', () => {
expect(dht).to.have.property('routingTable')
})

it('should be able to start and stop', function (done) {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()
const dht = new KadDHT(sw)

sinon.spy(dht.network, 'start')
sinon.spy(dht.randomWalk, 'start')

sinon.spy(dht.network, 'stop')
sinon.spy(dht.randomWalk, 'stop')

series([
(cb) => dht.start(cb),
(cb) => {
expect(dht.network.start.calledOnce).to.equal(true)
expect(dht.randomWalk.start.calledOnce).to.equal(true)

cb()
},
(cb) => dht.stop(cb)
], (err) => {
expect(err).to.not.exist()
expect(dht.network.stop.calledOnce).to.equal(true)
expect(dht.randomWalk.stop.calledOnce).to.equal(true)

done()
})
})

it('should be able to start with random-walk disabled', function (done) {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()
const dht = new KadDHT(sw, { enabledDiscovery: false })

sinon.spy(dht.network, 'start')
sinon.spy(dht.randomWalk, 'start')

sinon.spy(dht.network, 'stop')
sinon.spy(dht.randomWalk, 'stop')

series([
(cb) => dht.start(cb),
(cb) => {
expect(dht.network.start.calledOnce).to.equal(true)
expect(dht.randomWalk.start.calledOnce).to.equal(false)

cb()
},
(cb) => dht.stop(cb)
], (err) => {
expect(err).to.not.exist()
expect(dht.network.stop.calledOnce).to.equal(true)
expect(dht.randomWalk.stop.calledOnce).to.equal(true) // Should be always disabled, as it can be started using the instance

done()
})
})

it('put - get', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()
Expand Down Expand Up @@ -206,7 +269,8 @@ describe('KadDHT', () => {
const nDHTs = 20
const tdht = new TestDHT()

tdht.spawn(nDHTs, (err, dhts) => {
// random walk disabled for a manual usage
tdht.spawn(nDHTs, { enabledDiscovery: false }, (err, dhts) => {
expect(err).to.not.exist()

series([
Expand Down
18 changes: 14 additions & 4 deletions test/utils/test-dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ class TestDHT {
this.nodes = []
}

spawn (n, callback) {
times(n, (i, cb) => this._spawnOne(cb), (err, dhts) => {
spawn (n, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

times(n, (i, cb) => this._spawnOne(options, cb), (err, dhts) => {
if (err) { return callback(err) }
callback(null, dhts)
})
}

_spawnOne (callback) {
_spawnOne (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

createPeerInfo(1, (err, peers) => {
if (err) { return callback(err) }

Expand All @@ -37,7 +47,7 @@ class TestDHT {
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

const dht = new KadDHT(sw)
const dht = new KadDHT(sw, options)

dht.validators.v = {
func (key, publicKey, callback) {
Expand Down

0 comments on commit f0bd76c

Please sign in to comment.