Socket
Socket
Sign inDemoInstall

libp2p-kad-dht

Package Overview
Dependencies
Maintainers
2
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libp2p-kad-dht - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

src/random-walk.js

10

CHANGELOG.md

@@ -0,1 +1,11 @@

<a name="0.9.0"></a>
# [0.9.0](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.8.0...v0.9.0) (2018-03-15)
### Features
* upgrade the discovery service to random-walk ([b8e0f72](https://github.com/libp2p/js-libp2p-kad-dht/commit/b8e0f72))
<a name="0.8.0"></a>

@@ -2,0 +12,0 @@ # [0.8.0](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.7.0...v0.8.0) (2018-02-07)

30

package.json
{
"name": "libp2p-kad-dht",
"version": "0.8.0",
"version": "0.9.0",
"description": "JavaScript implementation of the Kad-DHT for libp2p",

@@ -11,11 +11,8 @@ "main": "src/index.js",

"docs": "aegir docs",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
"release-major": "aegir release --type major --docs",
"release": "aegir release --docs -t node",
"release-minor": "aegir release --type minor --docs -t node",
"release-major": "aegir release --type major --docs -t node",
"coverage": "aegir coverage",
"coverage-publish": "aegir-coverage publish"
},
"browser": {
"./test/nodejs-bundle": "./test/browser-bundle"
},
"pre-push": [

@@ -51,12 +48,11 @@ "lint",

"k-bucket": "^3.3.1",
"libp2p-crypto": "~0.12.0",
"libp2p-crypto": "~0.12.1",
"libp2p-record": "~0.5.1",
"multihashing-async": "~0.4.7",
"peer-id": "~0.10.5",
"multihashing-async": "~0.4.8",
"peer-id": "~0.10.6",
"peer-info": "~0.11.6",
"priorityqueue": "^0.2.0",
"priorityqueue": "^0.2.1",
"protons": "^1.0.1",
"pull-length-prefixed": "^1.3.0",
"pull-stream": "^3.6.1",
"safe-buffer": "^5.1.1",
"pull-stream": "^3.6.2",
"varint": "^5.0.0",

@@ -66,3 +62,3 @@ "xor-distance": "^1.0.0"

"devDependencies": {
"aegir": "^12.4.0",
"aegir": "^13.0.5",
"chai": "^4.1.2",

@@ -72,5 +68,5 @@ "datastore-level": "~0.7.0",

"interface-connection": "~0.3.2",
"libp2p-multiplex": "~0.5.1",
"libp2p-switch": "~0.36.0",
"libp2p-tcp": "~0.11.5",
"libp2p-mplex": "~0.6.0",
"libp2p-switch": "~0.37.0",
"libp2p-tcp": "~0.11.6",
"lodash": "^4.17.5",

@@ -77,0 +73,0 @@ "lodash.random": "^3.2.0",

@@ -41,4 +41,20 @@ # js-libp2p-kad-dht

See https://libp2p.github.io/js-libp2p-kad-dht
See https://libp2p.github.io/js-libp2p-kad-dht for the auto generated docs.
The libp2p-kad-dht module offers 3 APIs: Peer Routing, Content Routing and Peer Discovery.
### Peer Routing
[![](https://raw.githubusercontent.com/libp2p/interface-peer-routing/master/img/badge.png)](https://github.com/libp2p/interface-peer-routing)
### Content Routing
[![](https://raw.githubusercontent.com/libp2p/interface-content-routing/master/img/badge.png)](https://github.com/libp2p/interface-content-routing)
### Peer Discovery
[![](https://github.com/libp2p/interface-peer-discovery/raw/master/img/badge.png)](https://github.com/libp2p/interface-peer-discovery)
`libp2p-kad-dht` provides a discovery service called `Random Walk` (random walks on the DHT to discover more nodes). It is accessible through `dht.randomWalk` and exposes the [Peer Discovery interface](https://github.com/libp2p/interface-peer-discovery).
## Contribute

@@ -45,0 +61,0 @@

@@ -21,2 +21,3 @@ 'use strict'

const Message = require('./message')
const RandomWalk = require('./random-walk')
const assert = require('assert')

@@ -92,2 +93,9 @@

Object.keys(pa).forEach((name) => { this[name] = pa[name] })
/**
* Provider management
*
* @type {RandomWalk}
*/
this.randomWalk = new RandomWalk(this)
}

@@ -124,3 +132,3 @@

this._running = false
this.bootstrapStop()
this.randomWalk.stop()
this.providers.stop()

@@ -144,46 +152,2 @@ this.network.stop(callback)

/**
* Kademlia 'node lookup' operation.
*
* @param {Buffer} key
* @param {function(Error, Array<PeerId>)} callback
* @returns {void}
*/
getClosestPeers (key, callback) {
this._log('getClosestPeers to %s', key.toString())
utils.convertBuffer(key, (err, id) => {
if (err) {
return callback(err)
}
const tablePeers = this.routingTable.closestPeers(id, c.ALPHA)
const q = new Query(this, key, (peer, callback) => {
waterfall([
(cb) => this._closerPeersSingle(key, peer, cb),
(closer, cb) => {
cb(null, {
closerPeers: closer
})
}
], callback)
})
q.run(tablePeers, (err, res) => {
if (err) {
return callback(err)
}
if (!res || !res.finalSet) {
return callback(null, [])
}
waterfall([
(cb) => utils.sortClosestPeers(Array.from(res.finalSet), id, cb),
(sorted, cb) => cb(null, sorted.slice(0, c.K))
], callback)
})
})
}
/**
* Store the given key/value pair in the DHT.

@@ -264,2 +228,3 @@ *

}
if (err == null) {

@@ -280,2 +245,3 @@ vals.push({

const rtp = this.routingTable.closestPeers(id, c.ALPHA)
this._log('peers in rt: %d', rtp.length)

@@ -297,5 +263,3 @@ if (rtp.length === 0) {

const res = {
closerPeers: peers
}
const res = { closerPeers: peers }

@@ -320,5 +284,3 @@ if ((rec && rec.value) ||

// run our query
timeout((cb) => {
query.run(rtp, cb)
}, maxTimeout)(cb)
timeout((cb) => query.run(rtp, cb), maxTimeout)(cb)
}

@@ -336,2 +298,46 @@ ], (err) => {

/**
* Kademlia 'node lookup' operation.
*
* @param {Buffer} key
* @param {function(Error, Array<PeerId>)} callback
* @returns {void}
*/
getClosestPeers (key, callback) {
this._log('getClosestPeers to %s', key.toString())
utils.convertBuffer(key, (err, id) => {
if (err) {
return callback(err)
}
const tablePeers = this.routingTable.closestPeers(id, c.ALPHA)
const q = new Query(this, key, (peer, callback) => {
waterfall([
(cb) => this._closerPeersSingle(key, peer, cb),
(closer, cb) => {
cb(null, {
closerPeers: closer
})
}
], callback)
})
q.run(tablePeers, (err, res) => {
if (err) {
return callback(err)
}
if (!res || !res.finalSet) {
return callback(null, [])
}
waterfall([
(cb) => utils.sortClosestPeers(Array.from(res.finalSet), id, cb),
(sorted, cb) => cb(null, sorted.slice(0, c.K))
], callback)
})
})
}
/**
* Get the public key for the given peer id.

@@ -383,2 +389,25 @@ *

/**
* Look if we are connected to a peer with the given id.
* Returns the `PeerInfo` for it, if found, otherwise `undefined`.
*
* @param {PeerId} peer
* @param {function(Error, PeerInfo)} callback
* @returns {void}
*/
findPeerLocal (peer, callback) {
this._log('findPeerLocal %s', peer.toB58String())
this.routingTable.find(peer, (err, p) => {
if (err) {
return callback(err)
}
if (!p || !this.peerBook.has(p)) {
return callback()
}
callback(null, this.peerBook.get(p))
})
}
// ----------- Content Routing
/**
* Announce to the network that a node can provide the given key.

@@ -423,2 +452,4 @@ * This is what Coral and MainlineDHT do to store large values

// ----------- Peer Routing
/**

@@ -507,67 +538,4 @@ * Search for a peer with the given ID.

}
/**
* Look if we are connected to a peer with the given id.
* Returns the `PeerInfo` for it, if found, otherwise `undefined`.
*
* @param {PeerId} peer
* @param {function(Error, PeerInfo)} callback
* @returns {void}
*/
findPeerLocal (peer, callback) {
this._log('findPeerLocal %s', peer.toB58String())
this.routingTable.find(peer, (err, p) => {
if (err) {
return callback(err)
}
if (!p || !this.peerBook.has(p)) {
return callback()
}
callback(null, this.peerBook.get(p))
})
}
/**
* Start the bootstrap process. This means running a number of queries every interval requesting random data.
* This is done to keep the dht healthy over time.
*
* @param {number} [queries=1] - how many queries to run per period
* @param {number} [period=300000] - how often to run the the bootstrap process, in milliseconds (5min)
* @param {number} [maxTimeout=10000] - how long to wait for the the bootstrap query to run, in milliseconds (10s)
* @returns {void}
*/
bootstrapStart (queries, period, maxTimeout) {
if (queries == null) {
queries = 1
}
if (period == null) {
period = 5 * c.minute
}
if (maxTimeout == null) {
maxTimeout = 10 * c.second
}
// Don't run twice
if (this._bootstrapRunning) {
return
}
this._bootstrapRunning = setInterval(
() => this._bootstrap(queries, maxTimeout),
period
)
}
/**
* Stop the bootstrap process.
*
* @returns {void}
*/
bootstrapStop () {
if (this._bootstrapRunning) {
clearInterval(this._bootstrapRunning)
}
}
}
module.exports = KadDHT

@@ -9,6 +9,3 @@ 'use strict'

const timeout = require('async/timeout')
const times = require('async/times')
const crypto = require('libp2p-crypto')
const PeerInfo = require('peer-info')
const multihashing = require('multihashing-async')

@@ -573,68 +570,3 @@ const utils = require('./utils')

dht.network.sendRequest(peer, msg, callback)
},
/**
* Do the bootstrap work.
*
* @param {number} queries
* @param {number} maxTimeout
* @returns {void}
*
* @private
*/
_bootstrap (queries, maxTimeout) {
dht._log('bootstrap:start')
times(queries, (i, cb) => {
waterfall([
(cb) => this._generateBootstrapId(cb),
(id, cb) => timeout((cb) => {
this._bootstrapQuery(id, cb)
}, maxTimeout)(cb)
], (err) => {
if (err) {
dht._log.error('bootstrap:error', err)
}
dht._log('bootstrap:done')
})
})
},
/**
* The query run during a bootstrap request.
*
* @param {PeerId} id
* @param {function(Error)} callback
* @returns {void}
*
* @private
*/
_bootstrapQuery (id, callback) {
dht._log('bootstrap:query:%s', id.toB58String())
this.findPeer(id, (err, peer) => {
if (err instanceof errors.NotFoundError) {
// expected case, we asked for random stuff after all
return callback()
}
if (err) {
return callback(err)
}
dht._log('bootstrap:query:found', err, peer)
// wait what, there was something found?
callback(new Error(`Bootstrap peer: ACTUALLY FOUND PEER: ${peer}, ${id.toB58String()}`))
})
},
/**
* Generate a random peer id for bootstrapping purposes.
*
* @param {function(Error, PeerId)} callback
* @returns {void}
*
* @private
*/
_generateBootstrapId (callback) {
multihashing(crypto.randomBytes(16), 'sha2-256', (err, digest) => {
if (err) {
return callback(err)
}
callback(null, new PeerId(digest))
})
}
})

@@ -7,3 +7,2 @@ 'use strict'

const utils = require('../../utils')
const Buffer = require('safe-buffer').Buffer

@@ -10,0 +9,0 @@ module.exports = (dht) => {

@@ -15,3 +15,2 @@ /* eslint-env mocha */

const random = require('lodash.random')
const Buffer = require('safe-buffer').Buffer
const _ = require('lodash')

@@ -22,3 +21,3 @@ const Record = require('libp2p-record').Record

const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')

@@ -29,7 +28,83 @@ const KadDHT = require('../src')

const utils = require('./utils')
const makePeers = utils.makePeers
const setupDHT = utils.setupDHT
const makeValues = utils.makeValues
const createPeerInfo = require('./utils/create-peer-info')
const createValues = require('./utils/create-values')
const TestDHT = require('./utils/test-dht')
// connect two dhts
function connectNoSync (a, b, callback) {
const target = _.cloneDeep(b.peerInfo)
target.id._pubKey = target.id.pubKey
target.id._privKey = null
a.switch.dial(target, callback)
}
function find (a, b, cb) {
retry({ times: 50, interval: 100 }, (cb) => {
a.routingTable.find(b.peerInfo.id, (err, match) => {
if (err) {
return cb(err)
}
if (!match) {
return cb(new Error('not found'))
}
try {
expect(a.peerBook.get(b.peerInfo).multiaddrs.toArray()[0].toString())
.to.eql(b.peerInfo.multiaddrs.toArray()[0].toString())
} catch (err) {
return cb(err)
}
cb()
})
}, cb)
}
// connect two dhts and wait for them to have each other
// in their routing table
function connect (a, b, callback) {
series([
(cb) => connectNoSync(a, b, cb),
(cb) => find(a, b, cb),
(cb) => find(b, a, cb)
], (err) => callback(err))
}
function bootstrap (dhts) {
dhts.forEach((dht) => {
dht.randomWalk._walk(3, 10000)
})
}
function waitForWellFormedTables (dhts, minPeers, avgPeers, maxTimeout, callback) {
timeout((cb) => {
retry({ times: 50, interval: 200 }, (cb) => {
let totalPeers = 0
const ready = dhts.map((dht) => {
const rtlen = dht.routingTable.size
totalPeers += rtlen
if (minPeers > 0 && rtlen < minPeers) {
return false
}
const actualAvgPeers = totalPeers / dhts.length
if (avgPeers > 0 && actualAvgPeers < avgPeers) {
return false
}
return true
})
const done = ready.every(Boolean)
cb(done ? null : new Error('not done yet'))
}, cb)
}, maxTimeout)(callback)
}
function countDiffPeers (a, b) {
const s = new Set()
a.forEach((p) => s.add(p.toB58String()))
return b.filter((p) => !s.has(p.toB58String())).length
}
describe('KadDHT', () => {

@@ -43,4 +118,4 @@ let peerInfos

parallel([
(cb) => makePeers(3, cb),
(cb) => makeValues(20, cb)
(cb) => createPeerInfo(3, cb),
(cb) => createValues(20, cb)
], (err, res) => {

@@ -54,13 +129,6 @@ expect(err).to.not.exist()

// Give the nodes some time to finish request
afterEach(function (done) {
this.timeout(10 * 1000)
utils.teardown(done)
})
it('create', () => {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -77,4 +145,5 @@ const dht = new KadDHT(sw, { kBucketSize: 5 })

this.timeout(10 * 1000)
const tdht = new TestDHT()
times(2, (i, cb) => setupDHT(cb), (err, dhts) => {
tdht.spawn(2, (err, dhts) => {
expect(err).to.not.exist()

@@ -92,3 +161,6 @@ const dhtA = dhts[0]

}
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -100,5 +172,10 @@ })

setupDHTs(4, (err, dhts, addrs, ids) => {
const tdht = new TestDHT()
tdht.spawn(4, (err, dhts) => {
expect(err).to.not.exist()
waterfall([
const addrs = dhts.map((d) => d.peerInfo.multiaddrs.toArray()[0])
const ids = dhts.map((d) => d.peerInfo.id)
series([
(cb) => connect(dhts[0], dhts[1], cb),

@@ -120,3 +197,3 @@ (cb) => connect(dhts[1], dhts[2], cb),

provs[0].multiaddrs.toArray()[0].toString()
).to.be.eql(
).to.equal(
addrs[3].toString()

@@ -128,15 +205,19 @@ )

}
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})
})
it('bootstrap', function (done) {
it('random-walk', function (done) {
this.timeout(40 * 1000)
const nDHTs = 20
const tdht = new TestDHT()
setupDHTs(nDHTs, (err, dhts) => {
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
waterfall([
series([
// ring connect

@@ -151,3 +232,6 @@ (cb) => times(nDHTs, (i, cb) => {

}
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -159,3 +243,6 @@ })

setupDHTs(4, (err, dhts) => {
const nDHTs = 4
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()

@@ -168,12 +255,15 @@

(cb) => dhts[3].put(
new Buffer('/v/hello'),
new Buffer('world'),
Buffer.from('/v/hello'),
Buffer.from('world'),
cb
),
(cb) => dhts[0].get(new Buffer('/v/hello'), 1000, cb),
(cb) => dhts[0].get(Buffer.from('/v/hello'), 1000, cb),
(res, cb) => {
expect(res).to.be.eql(new Buffer('world'))
expect(res).to.eql(Buffer.from('world'))
cb()
}
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -185,5 +275,10 @@ })

setupDHTs(4, (err, dhts, addrs, ids) => {
const nDHTs = 4
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const ids = dhts.map((d) => d.peerInfo.id)
waterfall([

@@ -198,3 +293,6 @@ (cb) => connect(dhts[0], dhts[1], cb),

}
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -206,6 +304,6 @@ })

parallel([
(cb) => setupDHT(cb),
(cb) => setupDHT(cb)
], (err, dhts) => {
const nDHTs = 2
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()

@@ -223,3 +321,6 @@ const dhtA = dhts[0]

(cb) => dhtB.switch.dial(peerA.id, cb)
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -232,5 +333,10 @@ })

setupDHTs(101, (err, dhts, addrs, ids) => {
const nDHTs = 101
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const ids = dhts.map((d) => d.peerInfo.id)
const guy = dhts[0]

@@ -285,3 +391,6 @@ const others = dhts.slice(1)

})
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -294,3 +403,5 @@ })

const nDHTs = 30
setupDHTs(nDHTs, (err, dhts) => {
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()

@@ -303,7 +414,7 @@

}, cb),
(cb) => dhts[1].getClosestPeers(new Buffer('foo'), cb)
(cb) => dhts[1].getClosestPeers(Buffer.from('foo'), cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res[1]).to.have.length(c.K)
done()
tdht.teardown(done)
})

@@ -317,9 +428,15 @@ })

setupDHTs(2, (err, dhts, addrs, ids) => {
const nDHTs = 2
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const ids = dhts.map((d) => d.peerInfo.id)
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].peerInfo.id.pubKey)
done()
expect(key).to.eql(dhts[1].peerInfo.id.pubKey)
tdht.teardown(done)
})

@@ -330,7 +447,12 @@ })

it('connected node', function (done) {
this.timeout(40 * 1000)
this.timeout(30 * 1000)
setupDHTs(2, (err, dhts, addrs, ids) => {
const nDHTs = 2
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const ids = dhts.map((d) => d.peerInfo.id)
waterfall([

@@ -349,3 +471,6 @@ (cb) => connect(dhts[0], dhts[1], cb),

}
], done)
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})

@@ -358,3 +483,3 @@ })

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -377,3 +502,3 @@ const dht = new KadDHT(sw)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -400,3 +525,3 @@ const dht = new KadDHT(sw)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -426,3 +551,3 @@ const dht = new KadDHT(sw)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -448,3 +573,3 @@ const dht = new KadDHT(sw)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -468,87 +593,1 @@ const dht = new KadDHT(sw)

})
function setupDHTs (n, callback) {
times(n, (i, cb) => setupDHT(cb), (err, dhts) => {
if (err) {
return callback(err)
}
callback(null, dhts, dhts.map((d) => d.peerInfo.multiaddrs.toArray()[0]), dhts.map((d) => d.peerInfo.id))
})
}
// connect two dhts
function connectNoSync (a, b, callback) {
const target = _.cloneDeep(b.peerInfo)
target.id._pubKey = target.id.pubKey
target.id._privKey = null
a.switch.dial(target, callback)
}
function find (a, b, cb) {
retry({ times: 50, interval: 100 }, (cb) => {
a.routingTable.find(b.peerInfo.id, (err, match) => {
if (err) {
return cb(err)
}
if (!match) {
return cb(new Error('not found'))
}
try {
expect(a.peerBook.get(b.peerInfo).multiaddrs.toArray()[0].toString())
.to.eql(b.peerInfo.multiaddrs.toArray()[0].toString())
} catch (err) {
return cb(err)
}
cb()
})
}, cb)
}
// connect two dhts and wait for them to have each other
// in their routing table
function connect (a, b, callback) {
series([
(cb) => connectNoSync(a, b, cb),
(cb) => find(a, b, cb),
(cb) => find(b, a, cb)
], (err) => callback(err))
}
function bootstrap (dhts) {
dhts.forEach((dht) => {
dht._bootstrap(3, 10000)
})
}
function waitForWellFormedTables (dhts, minPeers, avgPeers, maxTimeout, callback) {
timeout((cb) => {
retry({ times: 50, interval: 200 }, (cb) => {
let totalPeers = 0
const ready = dhts.map((dht) => {
const rtlen = dht.routingTable.size
totalPeers += rtlen
if (minPeers > 0 && rtlen < minPeers) {
return false
}
const actualAvgPeers = totalPeers / dhts.length
if (avgPeers > 0 && actualAvgPeers < avgPeers) {
return false
}
return true
})
const done = ready.every(Boolean)
cb(done ? null : new Error('not done yet'))
}, cb)
}, maxTimeout)(callback)
}
function countDiffPeers (a, b) {
const s = new Set()
a.forEach((p) => s.add(p.toB58String()))
return b.filter((p) => !s.has(p.toB58String())).length
}

@@ -10,3 +10,3 @@ /* eslint-env mocha */

const makePeers = require('./utils').makePeers
const createPeerInfo = require('./utils/create-peer-info')

@@ -19,3 +19,3 @@ describe('LimitedPeerList', () => {

makePeers(5, (err, p) => {
createPeerInfo(5, (err, p) => {
if (err) {

@@ -22,0 +22,0 @@ return done(err)

@@ -67,3 +67,3 @@ /* eslint-env mocha */

expect(dec.record.serialize()).to.be.eql(record.serialize())
expect(dec.record.key).to.be.eql(Buffer.from('hello'))
expect(dec.record.key).to.eql(Buffer.from('hello'))

@@ -70,0 +70,0 @@ expect(dec.closerPeers).to.have.length(5)

@@ -15,3 +15,3 @@ /* eslint-env mocha */

const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')

@@ -21,3 +21,3 @@ const KadDHT = require('../src')

const makePeers = require('./utils').makePeers
const createPeerInfo = require('./utils/create-peer-info')

@@ -30,3 +30,3 @@ describe('Network', () => {

this.timeout(10 * 1000)
makePeers(3, (err, result) => {
createPeerInfo(3, (err, result) => {
if (err) {

@@ -39,3 +39,3 @@ return done(err)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -42,0 +42,0 @@ dht = new KadDHT(sw)

@@ -10,3 +10,3 @@ /* eslint-env mocha */

const makePeers = require('./utils').makePeers
const createPeerInfo = require('./utils/create-peer-info')

@@ -17,3 +17,3 @@ describe('PeerList', () => {

before((done) => {
makePeers(3, (err, p) => {
createPeerInfo(3, (err, p) => {
if (err) {

@@ -20,0 +20,0 @@ return done(err)

@@ -20,7 +20,7 @@ /* eslint-env mocha */

const os = require('os')
const Buffer = require('safe-buffer').Buffer
const Providers = require('../src/providers')
const util = require('./utils')
const createPeerInfo = require('./utils/create-peer-info')
const createValues = require('./utils/create-values')

@@ -32,3 +32,3 @@ describe('Providers', () => {

this.timeout(10 * 1000)
util.makePeers(3, (err, peers) => {
createPeerInfo(3, (err, peers) => {
if (err) {

@@ -136,4 +136,4 @@ return done(err)

(cb) => parallel([
(cb) => util.makeValues(100, cb),
(cb) => util.makePeers(600, cb)
(cb) => createValues(100, cb),
(cb) => createPeerInfo(600, cb)
], cb),

@@ -140,0 +140,0 @@ (res, cb) => {

@@ -7,7 +7,6 @@ /* eslint-env mocha */

const expect = chai.expect
const Buffer = require('safe-buffer').Buffer
const PeerBook = require('peer-book')
const Switch = require('libp2p-switch')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')

@@ -17,3 +16,3 @@ const DHT = require('../src')

const makePeers = require('./utils').makePeers
const createPeerInfo = require('./utils/create-peer-info')

@@ -26,3 +25,3 @@ describe('Query', () => {

this.timeout(5 * 1000)
makePeers(3, (err, result) => {
createPeerInfo(3, (err, result) => {
if (err) {

@@ -35,3 +34,3 @@ return done(err)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -38,0 +37,0 @@ dht = new DHT(sw)

@@ -10,3 +10,3 @@ /* eslint-env mocha */

const each = require('async/each')
const waterfall = require('async/waterfall')
const series = require('async/series')
const range = require('lodash.range')

@@ -16,6 +16,6 @@ const random = require('lodash.random')

const RoutingTable = require('../src/routing')
const utils = require('../src/utils')
const kadUtils = require('../src/utils')
function createPeers (n, callback) {
map(range(n), (i, cb) => PeerId.create({bits: 1024}, cb), callback)
function createPeerId (n, callback) {
map(range(n), (i, cb) => PeerId.create({bits: 512}, cb), callback)
}

@@ -29,3 +29,3 @@

PeerId.create((err, id) => {
PeerId.create({ bits: 512 }, (err, id) => {
expect(err).to.not.exist()

@@ -40,13 +40,13 @@ table = new RoutingTable(id, 20)

createPeers(20, (err, peers) => {
createPeerId(20, (err, ids) => {
expect(err).to.not.exist()
waterfall([
series([
(cb) => each(range(1000), (n, cb) => {
table.add(peers[random(peers.length - 1)], cb)
table.add(ids[random(ids.length - 1)], cb)
}, cb),
(cb) => each(range(20), (n, cb) => {
const id = peers[random(peers.length - 1)]
const id = ids[random(ids.length - 1)]
utils.convertPeerId(id, (err, key) => {
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()

@@ -62,15 +62,14 @@ expect(table.closestPeers(key, 5).length)

// TODO fix a callback that is being called twice, making this test fail
it.skip('remove', function (done) {
it('remove', function (done) {
this.timeout(20 * 1000)
createPeers(10, (err, peers) => {
createPeerId(10, (err, peers) => {
expect(err).to.not.exist()
let k
waterfall([
series([
(cb) => each(peers, (peer, cbEach) => table.add(peer, cbEach), cb),
(cb) => {
const id = peers[2]
utils.convertPeerId(id, (err, key) => {
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()

@@ -95,9 +94,9 @@ k = key

createPeers(4, (err, peers) => {
createPeerId(4, (err, peers) => {
expect(err).to.not.exist()
waterfall([
series([
(cb) => each(peers, (peer, cb) => table.add(peer, cb), cb),
(cb) => {
const id = peers[2]
utils.convertPeerId(id, (err, key) => {
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()

@@ -112,13 +111,12 @@ expect(table.closestPeer(key)).to.eql(id)

// TODO fix a callback that is being called twice, making this test fail
it.skip('closestPeers', function (done) {
it('closestPeers', function (done) {
this.timeout(20 * 1000)
createPeers(18, (err, peers) => {
createPeerId(18, (err, peers) => {
expect(err).to.not.exist()
waterfall([
series([
(cb) => each(peers, (peer, cb) => table.add(peer, cb), cb),
(cb) => {
const id = peers[2]
utils.convertPeerId(id, (err, key) => {
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()

@@ -125,0 +123,0 @@ expect(table.closestPeers(key, 15)).to.have.length(15)

@@ -11,11 +11,14 @@ /* eslint-env mocha */

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('../../utils')
const createPeerInfo = require('../../utils/create-peer-info')
const createValues = require('../../utils/create-values')
const TestDHT = require('../../utils/test-dht')
describe('rpc - handlers - AddProvider', () => {
let peers
let values
let tdht
let dht

@@ -25,4 +28,4 @@

parallel([
(cb) => util.makePeers(3, cb),
(cb) => util.makeValues(2, cb)
(cb) => createPeerInfo(3, cb),
(cb) => createValues(2, cb)
], (err, res) => {

@@ -36,8 +39,8 @@ expect(err).to.not.exist()

afterEach((done) => util.teardown(done))
beforeEach((done) => {
tdht = new TestDHT()
beforeEach((done) => {
util.setupDHT((err, res) => {
tdht.spawn(1, (err, dhts) => {
expect(err).to.not.exist()
dht = res
dht = dhts[0]
done()

@@ -47,2 +50,6 @@ })

afterEach((done) => {
tdht.teardown(done)
})
describe('invalid messages', () => {

@@ -110,5 +117,3 @@ const tests = [{

expect(provs[0].id).to.eql(sender.id.id)
expect(
dht.peerBook.has(sender.id)
).to.be.eql(false)
expect(dht.peerBook.has(sender.id)).to.equal(false)
cb()

@@ -115,0 +120,0 @@ }

@@ -8,16 +8,19 @@ /* eslint-env mocha */

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('../../utils')
const T = Message.TYPES.FIND_NODE
const createPeerInfo = require('../../utils/create-peer-info')
// const createValues = require('../../utils/create-values')
const TestDHT = require('../../utils/test-dht')
describe('rpc - handlers - FindNode', () => {
let peers
let tdht
let dht
before((done) => {
util.makePeers(3, (err, res) => {
createPeerInfo(3, (err, res) => {
expect(err).to.not.exist()

@@ -29,8 +32,8 @@ peers = res

afterEach((done) => util.teardown(done))
beforeEach((done) => {
tdht = new TestDHT()
beforeEach((done) => {
util.setupDHT((err, res) => {
tdht.spawn(1, (err, dhts) => {
expect(err).to.not.exist()
dht = res
dht = dhts[0]
done()

@@ -40,2 +43,6 @@ })

afterEach((done) => {
tdht.teardown(done)
})
it('returns self, if asked for self', (done) => {

@@ -42,0 +49,0 @@ const msg = new Message(T, dht.peerInfo.id.id, 0)

@@ -9,3 +9,2 @@ /* eslint-env mocha */

const waterfall = require('async/waterfall')
const Buffer = require('safe-buffer').Buffer

@@ -15,9 +14,13 @@ const Message = require('../../../src/message')

const handler = require('../../../src/rpc/handlers/get-providers')
const util = require('../../utils')
const T = Message.TYPES.GET_PROVIDERS
const createPeerInfo = require('../../utils/create-peer-info')
const createValues = require('../../utils/create-values')
const TestDHT = require('../../utils/test-dht')
describe('rpc - handlers - GetProviders', () => {
let peers
let values
let tdht
let dht

@@ -27,4 +30,4 @@

parallel([
(cb) => util.makePeers(3, cb),
(cb) => util.makeValues(2, cb)
(cb) => createPeerInfo(3, cb),
(cb) => createValues(2, cb)
], (err, res) => {

@@ -38,8 +41,8 @@ expect(err).to.not.exist()

afterEach((done) => util.teardown(done))
beforeEach((done) => {
tdht = new TestDHT()
beforeEach((done) => {
util.setupDHT((err, res) => {
tdht.spawn(1, (err, dhts) => {
expect(err).to.not.exist()
dht = res
dht = dhts[0]
done()

@@ -49,2 +52,6 @@ })

afterEach((done) => {
tdht.teardown(done)
})
it('errors with an invalid key ', (done) => {

@@ -51,0 +58,0 @@ const msg = new Message(T, Buffer.from('hello'), 0)

@@ -8,16 +8,19 @@ /* eslint-env mocha */

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('../../utils')
const T = Message.TYPES.GET_VALUE
const createPeerInfo = require('../../utils/create-peer-info')
// const createValues = require('../../utils/create-values')
const TestDHT = require('../../utils/test-dht')
describe('rpc - handlers - GetValue', () => {
let peers
let tdht
let dht
before((done) => {
util.makePeers(2, (err, res) => {
createPeerInfo(2, (err, res) => {
expect(err).to.not.exist()

@@ -29,8 +32,8 @@ peers = res

afterEach((done) => util.teardown(done))
beforeEach((done) => {
tdht = new TestDHT()
beforeEach((done) => {
util.setupDHT((err, res) => {
tdht.spawn(1, (err, dhts) => {
expect(err).to.not.exist()
dht = res
dht = dhts[0]
done()

@@ -40,2 +43,6 @@ })

afterEach((done) => {
tdht.teardown(done)
})
it('errors when missing key', (done) => {

@@ -42,0 +49,0 @@ const msg = new Message(T, Buffer.alloc(0), 0)

@@ -7,16 +7,17 @@ /* eslint-env mocha */

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('../../utils')
const T = Message.TYPES.PING
const createPeerInfo = require('../../utils/create-peer-info')
const TestDHT = require('../../utils/test-dht')
describe('rpc - handlers - Ping', () => {
let peers
let tdht
let dht
before((done) => {
util.makePeers(2, (err, res) => {
createPeerInfo(2, (err, res) => {
expect(err).to.not.exist()

@@ -28,8 +29,8 @@ peers = res

afterEach((done) => util.teardown(done))
beforeEach((done) => {
tdht = new TestDHT()
beforeEach((done) => {
util.setupDHT((err, res) => {
tdht.spawn(1, (err, dhts) => {
expect(err).to.not.exist()
dht = res
dht = dhts[0]
done()

@@ -39,2 +40,6 @@ })

afterEach((done) => {
tdht.teardown(done)
})
it('replies with the same message', (done) => {

@@ -41,0 +46,0 @@ const msg = new Message(T, Buffer.from('hello'), 5)

@@ -9,3 +9,2 @@ /* eslint-env mocha */

const Record = require('libp2p-record').Record
const Buffer = require('safe-buffer').Buffer

@@ -16,3 +15,5 @@ const Message = require('../../../src/message')

const util = require('../../utils')
const createPeerInfo = require('../../utils/create-peer-info')
// const createValues = require('../../utils/create-values')
const TestDHT = require('../../utils/test-dht')

@@ -23,6 +24,7 @@ const T = Message.TYPES.PUT_VALUE

let peers
let tdht
let dht
before((done) => {
util.makePeers(2, (err, res) => {
createPeerInfo(2, (err, res) => {
expect(err).to.not.exist()

@@ -34,8 +36,8 @@ peers = res

afterEach((done) => util.teardown(done))
beforeEach((done) => {
tdht = new TestDHT()
beforeEach((done) => {
util.setupDHT((err, res) => {
tdht.spawn(1, (err, dhts) => {
expect(err).to.not.exist()
dht = res
dht = dhts[0]
done()

@@ -45,2 +47,6 @@ })

afterEach((done) => {
tdht.teardown(done)
})
it('errors on missing record', (done) => {

@@ -47,0 +53,0 @@ const msg = new Message(T, Buffer.from('hello'), 5)

@@ -13,3 +13,3 @@ /* eslint-env mocha */

const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')

@@ -20,3 +20,3 @@ const Message = require('../../src/message')

const makePeers = require('../utils').makePeers
const createPeerInfo = require('../utils/create-peer-info')

@@ -27,3 +27,3 @@ describe('rpc', () => {

before((done) => {
makePeers(2, (err, peers) => {
createPeerInfo(2, (err, peers) => {
if (err) {

@@ -42,3 +42,3 @@ return done(err)

sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()

@@ -45,0 +45,0 @@ const dht = new KadDHT(sw, { kBucketSize: 5 })

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc