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

feat: add KadDHT #100

Merged
merged 9 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 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 @@ -40,12 +40,15 @@
"aegir": "^11.0.1",
"async": "^2.2.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-kad-dht": "0.0.1",
"libp2p-mdns": "~0.7.0",
"libp2p-multiplex": "~0.4.3",
"libp2p-railing": "~0.5.0",
Expand Down Expand Up @@ -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
80 changes: 80 additions & 0 deletions test/content-routing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* 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.only('.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('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).length.to.be.above(0)
done()
})
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is yielding an empty array of providers immediately, meaning that it failing to find the peer and it is also not respective timeout.


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.exist()
done()
})
})
})
})
64 changes: 64 additions & 0 deletions test/peer-routing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* 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.only('.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('nodeA.dial by Id to node C', (done) => {
nodeA.dial(nodeC.peerInfo.id, done)
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dignifiedquire this simple test is failing with:

 1) .peerRouting ring nodeA.dial by Id to node C:

  Error
      at LookupFailureError (/Users/koruza/code/js-libp2p-kad-dht/src/errors.js:7:1)
      at waterfall (/Users/koruza/code/js-libp2p-kad-dht/src/index.js:454:23)
      at nextTask (/Users/koruza/code/js-libp2p-kad-dht/node_modules/async/waterfall.js:28:14)
      at /Users/koruza/code/js-libp2p-kad-dht/node_modules/async/waterfall.js:22:13
      at apply (/Users/koruza/code/js-libp2p-kad-dht/node_modules/lodash/_apply.js:15:25)
      at /Users/koruza/code/js-libp2p-kad-dht/node_modules/lodash/_overRest.js:32:12
      at /Users/koruza/code/js-libp2p-kad-dht/node_modules/async/internal/onlyOnce.js:12:16
      at setImmediate (/Users/koruza/code/js-libp2p-kad-dht/node_modules/multihashing-async/src/utils.js:8:7)
      at Immediate.<anonymous> (/Users/koruza/code/js-libp2p-kad-dht/node_modules/async/internal/setImmediate.js:26:16)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

Any clue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some delay in order to ensure that the network is stabilized (which should not be required) and now I'm getting a different error. The peerRouting.findPeer is returning undefined as the peer and not a error in case of peer not found, making swarm to fail.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#100 (comment) still persists


it.skip('nodeB.dial by Id to node D', (done) => {})
it.skip('nodeC.dial by Id to node E', (done) => {})
it.skip('nodeB.peerRouting.findPeers(nodeE.peerInfo.id)', (done) => {})
})
})