Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
feat: discovery fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Jan 6, 2019
1 parent c035e87 commit 42756b4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "libp2p-stardust",
"version": "0.0.1",
"description": "Basically ws-star but without the bugs",
"main": "index.js",
"main": "src/transport/index.js",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
Expand All @@ -29,6 +29,7 @@
"libp2p-websockets": "^0.12.0",
"multistream-select": "^0.14.3",
"peer-id": "^0.12.1",
"peer-info": "^0.15.0",
"protons": "^1.0.1"
},
"devDependencies": {
Expand Down
19 changes: 14 additions & 5 deletions src/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
const LP = require('../rpc/lp')
const pull = require('pull-stream/pull')
const handshake = require('pull-handshake')
const {JoinInit, JoinChallenge, JoinChallengeSolution, JoinVerify, Discovery, DialRequest, DialResponse, Error: E, ErrorTranslations} = require('../rpc/proto')
const {JoinInit, JoinChallenge, JoinChallengeSolution, JoinVerify, Discovery, DialRequest, DialResponse, ErrorTranslations} = require('../rpc/proto')

const prom = (f) => new Promise((resolve, reject) => f((err, res) => err ? reject(err) : resolve(res)))

const sha5 = (data) => crypto.createHash('sha512').update(data).digest()

const crypto = require('crypto')
const ID = require('peer-id')
const PeerInfo = require('peer-info')

const debug = require('debug')
const log = debug('libp2p:stardust:client')
Expand All @@ -28,10 +29,19 @@ class Connection {
async readDiscovery () {
const addrBase = this.address.decapsulate('p2p-websocket-star')
const {ids} = await this.rpc.readProto(Discovery)
log('reading discovery')
ids
.map(id => new ID(id).toB58String())
.filter(id => id !== this.client.id.toB58String())
.map(id => addrBase.encapsulate('/p2p-websocket-star/ipfs/' + id))
.map(id => {
const pi = new PeerInfo(new ID(id))
if (pi.id.toB58String() === this.client.id.toB58String()) return
pi.multiaddrs.add(addrBase.encapsulate('/p2p-websocket-star/ipfs/' + pi.id.toB58String()))

return pi
})
.filter(Boolean)
.forEach(pi => this.client.discovery.emit('peer', pi))

this.readDiscovery()
}

async connect (address) {
Expand Down Expand Up @@ -70,7 +80,6 @@ class Connection {
this.rpc = rpc
muxed.on('stream', this.handler)
this.readDiscovery()
setInterval(this.readDiscovery.bind(this), 10 * 1000)
}

async dial (addr) {
Expand Down
10 changes: 10 additions & 0 deletions src/transport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Connection = require('interface-connection').Connection
const once = require('once')
const debug = require('debug')
const log = debug('libp2p:stardust')
const EE = require('events').EventEmitter

function noop () {}

Expand All @@ -30,6 +31,15 @@ class Stardust {
this.switch = new MicroSwitch({ transports, addresses: [], muxers })
this.id = id

this.discovery = new EE()
this.discovery.tag = 'stardust'
this.discovery.start = (callback) => {
setImmediate(callback)
}
this.discovery.stop = (callback) => {
setImmediate(callback)
}

this.connections = {}
}

Expand Down
4 changes: 2 additions & 2 deletions test/connect.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Client = require('../src/client')
const Client = require('..')
const ID = require('peer-id')
const IDJSON = require('./id.json')
const multiaddr = require('multiaddr')
Expand All @@ -22,7 +22,7 @@ describe('connect', () => {

it('should be creatable', () => {
client = new Client({id})
conn = client.createConnection(() => {})
conn = client.createListener(() => {})
})

it('should connect to server', async () => conn.connect(SERVER_URL))
Expand Down
10 changes: 7 additions & 3 deletions test/dial.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Client = require('../src/client')
const Client = require('..')
const ID = require('peer-id')
const IDJSON = [require('./id.json'), require('./id2.json')]
const multiaddr = require('multiaddr')
Expand All @@ -21,7 +21,7 @@ describe('dial', () => {

it('connect all clients', async () => {
clients = ids.map(id => new Client({id}))
conns = clients.map(client => client.createConnection(conn => pull(conn, conn)))
conns = clients.map(client => client.createListener(conn => pull(conn, conn)))
await Promise.all(conns.map(conn => conn.connect(SERVER_URL)))
clients.forEach(c => (c.addr = multiaddr('/ipfs/' + c.id.toB58String())))
})
Expand All @@ -39,6 +39,10 @@ describe('dial', () => {
})

it('client1 should discover client2', function (done) { // TODO: fix this
this.timeout(10000)
this.timeout(15000)
clients[0].discovery.once('peer', (pi) => {
require('assert')(String(pi.multiaddrs.toArray()[0]) === String(SERVER_URL) + String(clients[1].addr))
done()
})
})
})

0 comments on commit 42756b4

Please sign in to comment.