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

Commit

Permalink
feat: use libp2p-switch
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Feb 7, 2018
1 parent a2a5c2e commit 054e5e5
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 101 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"test": "aegir test --target node",
"test": "aegir test -t node",
"build": "aegir build",
"docs": "aegir docs",
"release": "aegir release --docs",
Expand All @@ -17,7 +17,7 @@
"browser": {
"./test/nodejs-bundle": "./test/browser-bundle"
},
"pre-commit": [
"pre-push": [
"lint",
"test"
],
Expand Down Expand Up @@ -67,9 +67,9 @@
"dirty-chai": "^2.0.1",
"interface-connection": "~0.3.2",
"libp2p-multiplex": "~0.5.1",
"libp2p-swarm": "~0.35.1",
"libp2p-tcp": "~0.11.2",
"lodash": "^4.17.4",
"libp2p-switch": "~0.36.0",
"libp2p-tcp": "~0.11.5",
"lodash": "^4.17.5",
"lodash.random": "^3.2.0",
"lodash.range": "^3.2.0",
"peer-book": "~0.5.4",
Expand Down
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ class KadDHT {
/**
* Create a new KadDHT.
*
* @param {Swarm} swarm
* @param {Switch} sw
* @param {object} options // {kBucketSize=20, datastore=MemoryDatastore}
*/
constructor (swarm, options) {
assert(swarm, 'libp2p-kad-dht requires a instance of swarmt a')
constructor (sw, options) {
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
options = options || {}

/**
* Local reference to libp2p-swarm.
* Local reference to the libp2p-switch instance
*
* @type {Swarm}
* @type {Switch}
*/
this.swarm = swarm
this.switch = sw

/**
* k-bucket size, defaults to 20.
* k-bucket size, defaults to 20
*
* @type {number}
*/
Expand Down Expand Up @@ -130,11 +130,11 @@ class KadDHT {
* @type {PeerInfo}
*/
get peerInfo () {
return this.swarm._peerInfo
return this.switch._peerInfo
}

get peerBook () {
return this.swarm._peerBook
return this.switch._peerBook
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ class Network {
return cb(new Error('Network is already running'))
}

// TODO add a way to check if swarm has started or not
// TODO add a way to check if switch has started or not
if (!this.dht.isStarted) {
return cb(new Error('Can not start network'))
}

this._running = true

// handle incoming connections
this.dht.swarm.handle(c.PROTOCOL_DHT, this._rpc)
this.dht.switch.handle(c.PROTOCOL_DHT, this._rpc)

// handle new connections
this.dht.swarm.on('peer-mux-established', this._onPeerConnected)
this.dht.switch.on('peer-mux-established', this._onPeerConnected)

cb()
}
Expand All @@ -70,9 +70,9 @@ class Network {
return cb(new Error('Network is already stopped'))
}
this._running = false
this.dht.swarm.removeListener('peer-mux-established', this._onPeerConnected)
this.dht.switch.removeListener('peer-mux-established', this._onPeerConnected)

this.dht.swarm.unhandle(c.PROTOCOL_DHT)
this.dht.switch.unhandle(c.PROTOCOL_DHT)
cb()
}

Expand All @@ -91,12 +91,12 @@ class Network {
* @type {bool}
*/
get isConnected () {
// TODO add a way to check if swarm has started or not
// TODO add a way to check if switch has started or not
return this.dht.isStarted && this.isStarted
}

/**
* Handle new connections in the swarm.
* Handle new connections in the switch.
*
* @param {PeerInfo} peer
* @returns {void}
Expand All @@ -107,7 +107,7 @@ class Network {
return this._log.error('Network is offline')
}

this.dht.swarm.dial(peer, c.PROTOCOL_DHT, (err, conn) => {
this.dht.switch.dial(peer, c.PROTOCOL_DHT, (err, conn) => {
if (err) {
return this._log('%s does not support protocol: %s', peer.id.toB58String(), c.PROTOCOL_DHT)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ class Network {
}

this._log('sending to: %s', to.toB58String())
this.dht.swarm.dial(to, c.PROTOCOL_DHT, (err, conn) => {
this.dht.switch.dial(to, c.PROTOCOL_DHT, (err, conn) => {
if (err) {
return callback(err)
}
Expand All @@ -164,7 +164,7 @@ class Network {

this._log('sending to: %s', to.toB58String())

this.dht.swarm.dial(to, c.PROTOCOL_DHT, (err, conn) => {
this.dht.switch.dial(to, c.PROTOCOL_DHT, (err, conn) => {
if (err) {
return callback(err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = (dht) => {
}

/**
* Handle incoming streams from the swarm, on the dht protocol.
* Handle incoming streams from the Switch, on the dht protocol.
*
* @param {string} protocol
* @param {Connection} conn
Expand Down
70 changes: 35 additions & 35 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Buffer = require('safe-buffer').Buffer
const _ = require('lodash')
const Record = require('libp2p-record').Record
const PeerBook = require('peer-book')
const Swarm = require('libp2p-swarm')
const Switch = require('libp2p-switch')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')

Expand Down Expand Up @@ -55,14 +55,14 @@ describe('KadDHT', () => {
})

it('create', () => {
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 })
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
const dht = new KadDHT(sw, { kBucketSize: 5 })

expect(dht).to.have.property('peerInfo').eql(peerInfos[0])
expect(dht).to.have.property('swarm').eql(swarm)
expect(dht).to.have.property('switch').eql(sw)
expect(dht).to.have.property('kBucketSize', 5)
expect(dht).to.have.property('routingTable')
})
Expand Down Expand Up @@ -202,8 +202,8 @@ describe('KadDHT', () => {
dhtB.peerBook.put(peerA)

parallel([
(cb) => dhtA.swarm.dial(peerB.id, cb),
(cb) => dhtB.swarm.dial(peerA.id, cb)
(cb) => dhtA.switch.dial(peerB.id, cb),
(cb) => dhtB.switch.dial(peerA.id, cb)
], done)
})
})
Expand Down Expand Up @@ -329,11 +329,11 @@ describe('KadDHT', () => {
})

it('_nearestPeersToQuery', (done) => {
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)
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
const dht = new KadDHT(sw)

dht.peerBook.put(peerInfos[1])
series([
Expand All @@ -347,11 +347,11 @@ describe('KadDHT', () => {
})

it('_betterPeersToQuery', (done) => {
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)
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
const dht = new KadDHT(sw)

dht.peerBook.put(peerInfos[1])
dht.peerBook.put(peerInfos[2])
Expand All @@ -369,11 +369,11 @@ describe('KadDHT', () => {

describe('_verifyRecordLocally', () => {
it('invalid record (missing public key)', (done) => {
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)
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
const dht = new KadDHT(sw)

// Not putting the peer info into the peerbook
// dht.peerBook.put(peerInfos[1])
Expand All @@ -394,11 +394,11 @@ describe('KadDHT', () => {
})

it('valid record - signed', (done) => {
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)
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
const dht = new KadDHT(sw)

dht.peerBook.put(peerInfos[1])

Expand All @@ -415,11 +415,11 @@ describe('KadDHT', () => {
})

it('valid record - not signed', (done) => {
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)
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
const dht = new KadDHT(sw)

dht.peerBook.put(peerInfos[1])

Expand Down Expand Up @@ -451,7 +451,7 @@ function connectNoSync (a, b, callback) {
const target = _.cloneDeep(b.peerInfo)
target.id._pubKey = target.id.pubKey
target.id._privKey = null
a.swarm.dial(target, callback)
a.switch.dial(target, callback)
}

function find (a, b, cb) {
Expand Down
20 changes: 10 additions & 10 deletions test/network.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const lp = require('pull-length-prefixed')
const series = require('async/series')
const Buffer = require('safe-buffer').Buffer
const PeerBook = require('peer-book')
const Swarm = require('libp2p-swarm')
const Switch = require('libp2p-switch')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')

Expand All @@ -31,14 +31,14 @@ describe('Network', () => {
}

peerInfos = result
const swarm = new Swarm(peerInfos[0], new PeerBook())
swarm.transport.add('tcp', new TCP())
swarm.connection.addStreamMuxer(Multiplex)
swarm.connection.reuse()
dht = new KadDHT(swarm)
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Multiplex)
sw.connection.reuse()
dht = new KadDHT(sw)

series([
(cb) => swarm.listen(cb),
(cb) => sw.start(cb),
(cb) => dht.start(cb)
], done)
})
Expand All @@ -48,7 +48,7 @@ describe('Network', () => {
this.timeout(10 * 1000)
series([
(cb) => dht.stop(cb),
(cb) => dht.swarm.close(cb)
(cb) => dht.switch.stop(cb)
], done)
})

Expand All @@ -64,7 +64,7 @@ describe('Network', () => {
const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0)

// mock it
dht.swarm.dial = (peer, protocol, callback) => {
dht.switch.dial = (peer, protocol, callback) => {
expect(protocol).to.eql('/ipfs/kad/1.0.0')
const msg = new Message(Message.TYPES.FIND_NODE, Buffer.from('world'), 0)

Expand Down Expand Up @@ -105,7 +105,7 @@ describe('Network', () => {
const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0)

// mock it
dht.swarm.dial = (peer, protocol, callback) => {
dht.switch.dial = (peer, protocol, callback) => {
expect(protocol).to.eql('/ipfs/kad/1.0.0')
const rawConn = {
// hanging
Expand Down
Loading

0 comments on commit 054e5e5

Please sign in to comment.