Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
feat: add KadDHT (#100)
Browse files Browse the repository at this point in the history
* feat: add KadDHT
  • Loading branch information
daviddias authored Apr 7, 2017
1 parent b43b7a6 commit d7bb4c9
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 14 deletions.
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "The libp2p build (module) used by js-ipfs on Node.js",
"main": "src/index.js",
"scripts": {
"test": "npm run test:muxer:spdy && npm run test:muxer:multiplex",
"test": "npm run test:muxer:both",
"test:muxer:spdy": "LIBP2P_MUXER=spdy aegir-test node",
"test:muxer:multiplex": "LIBP2P_MUXER=multiplex aegir-test node",
"test:muxer:both": "LIBP2P_MUXER=spdy, multiplex aegir-test node",
"test:muxer:both": "LIBP2P_MUXER=\"spdy, multiplex\" aegir-test node",
"lint": "aegir-lint",
"release": "aegir-release node",
"release-minor": "aegir-release node --type minor",
Expand Down Expand Up @@ -38,27 +38,30 @@
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-nodejs#readme",
"devDependencies": {
"aegir": "^11.0.1",
"async": "^2.2.0",
"async": "^2.3.0",
"chai": "^3.5.0",
"cids": "^0.5.0",
"dirty-chai": "^1.2.2",
"lodash.times": "^4.3.2",
"pre-commit": "^1.2.2",
"pull-stream": "^3.5.0"
},
"dependencies": {
"libp2p": "~0.8.0",
"libp2p": "~0.9.0",
"libp2p-kad-dht": "~0.1.0",
"libp2p-mdns": "~0.7.0",
"libp2p-multiplex": "~0.4.3",
"libp2p-railing": "~0.5.0",
"libp2p-secio": "~0.6.8",
"libp2p-spdy": "~0.10.6",
"libp2p-swarm": "~0.29.0",
"libp2p-swarm": "~0.29.1",
"libp2p-tcp": "~0.10.0",
"libp2p-webrtc-star": "~0.9.0",
"libp2p-websockets": "~0.10.0",
"mafmt": "^2.1.8",
"multiaddr": "^2.3.0",
"peer-book": "~0.4.0",
"peer-id": "~0.8.6",
"peer-id": "~0.8.7",
"peer-info": "~0.9.2"
},
"contributors": [
Expand All @@ -73,4 +76,4 @@
"kumavis <kumavis@users.noreply.github.com>",
"varunagarwal315 <varunagarwal315@gmail.com>"
]
}
}
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Railing = require('libp2p-railing')
const spdy = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const multiplex = require('libp2p-multiplex')
const secio = require('libp2p-secio')
const libp2p = require('libp2p')
Expand Down Expand Up @@ -49,11 +50,10 @@ class Node extends libp2p {
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [
secio
]
crypto: [ secio ]
},
discovery: []
discovery: [],
DHT: KadDHT
}

if (options.webRTCStar) {
Expand Down
85 changes: 85 additions & 0 deletions test/content-routing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const utils = require('./utils')
const createNode = utils.createNode
const _times = require('lodash.times')
const CID = require('cids')

describe('.contentRouting', () => {
let nodeA
let nodeB
let nodeC
let nodeD
let nodeE

before((done) => {
const tasks = _times(5, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', { mdns: false }, (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))
})
})

parallel(tasks, (err, nodes) => {
expect(err).to.not.exist()
nodeA = nodes[0]
nodeB = nodes[1]
nodeC = nodes[2]
nodeD = nodes[3]
nodeE = nodes[4]

parallel([
(cb) => nodeA.dial(nodeB.peerInfo, cb),
(cb) => nodeB.dial(nodeC.peerInfo, cb),
(cb) => nodeC.dial(nodeD.peerInfo, cb),
(cb) => nodeD.dial(nodeE.peerInfo, cb),
(cb) => nodeE.dial(nodeA.peerInfo, cb)
], done)
})
})

after((done) => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb),
(cb) => nodeC.stop(cb),
(cb) => nodeD.stop(cb),
(cb) => nodeE.stop(cb)
], done)
})

describe('le ring', () => {
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')

it('let kbucket get filled', (done) => {
setTimeout(() => done(), 50)
})

it('nodeA.contentRouting.provide', (done) => {
nodeA.contentRouting.provide(cid, done)
})

it('nodeE.contentRouting.findProviders for existing record', (done) => {
nodeE.contentRouting.findProviders(cid, 5000, (err, providers) => {
expect(err).to.not.exist()
expect(providers).to.have.length.above(0)
done()
})
})

it('nodeC.contentRouting.findProviders for non existing record (timeout)', (done) => {
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSnnnn')

nodeE.contentRouting.findProviders(cid, 5000, (err, providers) => {
expect(err).to.not.exist()
expect(providers).to.have.length(0)
done()
})
})
})
})
89 changes: 89 additions & 0 deletions test/peer-routing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const utils = require('./utils')
const createNode = utils.createNode
const _times = require('lodash.times')

describe('.peerRouting', () => {
let nodeA
let nodeB
let nodeC
let nodeD
let nodeE

before((done) => {
const tasks = _times(5, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', { mdns: false }, (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))
})
})

parallel(tasks, (err, nodes) => {
expect(err).to.not.exist()
nodeA = nodes[0]
nodeB = nodes[1]
nodeC = nodes[2]
nodeD = nodes[3]
nodeE = nodes[4]

parallel([
(cb) => nodeA.dial(nodeB.peerInfo, cb),
(cb) => nodeB.dial(nodeC.peerInfo, cb),
(cb) => nodeC.dial(nodeD.peerInfo, cb),
(cb) => nodeD.dial(nodeE.peerInfo, cb),
(cb) => nodeE.dial(nodeA.peerInfo, cb)
], done)
})
})

after((done) => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb),
(cb) => nodeC.stop(cb),
(cb) => nodeD.stop(cb),
(cb) => nodeE.stop(cb)
], done)
})

describe('el ring', () => {
it('let kbucket get filled', (done) => {
setTimeout(() => done(), 50)
})

it('nodeA.dial by Id to node C', (done) => {
nodeA.dial(nodeC.peerInfo.id, (err) => {
expect(err).to.not.exist()
done()
})
})

it('nodeB.dial by Id to node D', (done) => {
nodeB.dial(nodeD.peerInfo.id, (err) => {
expect(err).to.not.exist()
done()
})
})

it('nodeC.dial by Id to node E', (done) => {
nodeC.dial(nodeE.peerInfo.id, (err) => {
expect(err).to.not.exist()
done()
})
})

it('nodeB.peerRouting.findPeer(nodeE.peerInfo.id)', (done) => {
nodeB.peerRouting.findPeer(nodeE.peerInfo.id, (err, peerInfo) => {
expect(err).to.not.exist()
expect(nodeE.peerInfo.id.toB58String()).to.equal(peerInfo.id.toB58String())
done()
})
})
})
})
3 changes: 0 additions & 3 deletions test/tcp+websockets+webrtc-star.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
// const series = require('async/series')
// const multiaddr = require('multiaddr')
// const pull = require('pull-stream')
const signalling = require('libp2p-webrtc-star/src/sig-server')
const utils = require('./utils')
const createNode = utils.createNode
Expand Down

0 comments on commit d7bb4c9

Please sign in to comment.