mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-29 08:51:34 +00:00
refactor: add core modules to libp2p (#400)
* refactor: add js-libp2p-connection-manager to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> * test(conn-mgr): only run in node * refactor: add js-libp2p-identify to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Yusef Napora <yusef@protocol.ai> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-pnet to repo Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> * refactor: add libp2p-ping to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: João Antunes <j.goncalo.antunes@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-circuit to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: Victor Bjelkholm <victorbjelkholm@gmail.com> Co-authored-by: Yusef Napora <yusef@napora.org> Co-authored-by: dirkmc <dirk@mccormick.cx> * test(switch): avoid using instanceof * chore(switch): update bignumber dep * refactor(circuit): clean up tests * refactor(switch): consolidate get peer utils * test(identify): do deep checks of addresses * test(identify): bump timeout for identify test * test(switch): tidy up limit dialer test * refactor(switch): remove redundant circuit tests * chore: add coverage script * refactor(circuit): consolidate get peer info * docs: reference original repositories in each sub readme * docs: fix comment * refactor: clean up sub package.json files and readmes
This commit is contained in:
@ -1,366 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const sinon = require('sinon')
|
||||
const once = require('once')
|
||||
const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const multiplex = require('pull-mplex')
|
||||
const PeerBook = require('peer-book')
|
||||
const getPorts = require('portfinder').getPorts
|
||||
|
||||
const utils = require('./utils')
|
||||
const createInfos = utils.createInfos
|
||||
const Swarm = require('libp2p-switch')
|
||||
const switchOptions = {
|
||||
denyTTL: 0 // nullifies denylisting
|
||||
}
|
||||
|
||||
describe(`circuit`, function () {
|
||||
describe('basic', () => {
|
||||
let swarmA // TCP and WS
|
||||
let swarmB // WS
|
||||
let swarmC // no transports
|
||||
let dialSpyA
|
||||
|
||||
before((done) => createInfos(3, (err, infos) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
const peerA = infos[0]
|
||||
const peerB = infos[1]
|
||||
const peerC = infos[2]
|
||||
|
||||
peerA.multiaddrs.add('/ip4/0.0.0.0/tcp/9001')
|
||||
peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9002/ws')
|
||||
|
||||
swarmA = new Swarm(peerA, new PeerBook(), switchOptions)
|
||||
swarmB = new Swarm(peerB, new PeerBook())
|
||||
swarmC = new Swarm(peerC, new PeerBook())
|
||||
|
||||
swarmA.transport.add('tcp', new TCP())
|
||||
swarmA.transport.add('ws', new WS())
|
||||
swarmB.transport.add('ws', new WS())
|
||||
|
||||
dialSpyA = sinon.spy(swarmA.transport, 'dial')
|
||||
|
||||
done()
|
||||
}))
|
||||
|
||||
after((done) => {
|
||||
parallel([
|
||||
(cb) => swarmA.stop(cb),
|
||||
(cb) => swarmB.stop(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('circuit not enabled and all transports failed', (done) => {
|
||||
swarmA.dial(swarmC._peerInfo, (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(err).to.match(/Circuit not enabled and all transports failed to dial peer/)
|
||||
expect(conn).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.enableCircuitRelay', () => {
|
||||
swarmA.connection.enableCircuitRelay({ enabled: true })
|
||||
expect(Object.keys(swarmA.transports).length).to.equal(3)
|
||||
|
||||
swarmB.connection.enableCircuitRelay({ enabled: true })
|
||||
expect(Object.keys(swarmB.transports).length).to.equal(2)
|
||||
})
|
||||
|
||||
it('listed on the transports map', () => {
|
||||
expect(swarmA.transports.Circuit).to.exist()
|
||||
expect(swarmB.transports.Circuit).to.exist()
|
||||
})
|
||||
|
||||
it('add /p2p-circuit addrs on start', (done) => {
|
||||
parallel([
|
||||
(cb) => swarmA.start(cb),
|
||||
(cb) => swarmB.start(cb)
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(swarmA._peerInfo.multiaddrs.toArray().filter((a) => a.toString()
|
||||
.includes(`/p2p-circuit`)).length).to.be.at.least(3)
|
||||
// ensure swarmA has had 0.0.0.0 replaced in the addresses
|
||||
expect(swarmA._peerInfo.multiaddrs.toArray().filter((a) => a.toString()
|
||||
.includes(`/0.0.0.0`)).length).to.equal(0)
|
||||
expect(swarmB._peerInfo.multiaddrs.toArray().filter((a) => a.toString()
|
||||
.includes(`/p2p-circuit`)).length).to.be.at.least(2)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('dial circuit only once', (done) => {
|
||||
swarmA._peerInfo.multiaddrs.clear()
|
||||
swarmA._peerInfo.multiaddrs
|
||||
.add(`/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star`)
|
||||
|
||||
swarmA.dial(swarmC._peerInfo, (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(err).to.match(/No available transports to dial peer/)
|
||||
expect(conn).to.not.exist()
|
||||
expect(dialSpyA.callCount).to.be.eql(1)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('dial circuit last', (done) => {
|
||||
const peerC = swarmC._peerInfo
|
||||
peerC.multiaddrs.clear()
|
||||
peerC.multiaddrs.add(`/p2p-circuit/ipfs/ABCD`)
|
||||
peerC.multiaddrs.add(`/ip4/127.0.0.1/tcp/9998/ipfs/ABCD`)
|
||||
peerC.multiaddrs.add(`/ip4/127.0.0.1/tcp/9999/ws/ipfs/ABCD`)
|
||||
|
||||
swarmA.dial(peerC, (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(conn).to.not.exist()
|
||||
expect(dialSpyA.lastCall.args[0]).to.be.eql('Circuit')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not try circuit if no transports enabled', (done) => {
|
||||
swarmC.dial(swarmA._peerInfo, (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(conn).to.not.exist()
|
||||
|
||||
expect(err).to.match(/No transports registered, dial not possible/)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not dial circuit if other transport succeed', (done) => {
|
||||
swarmA.dial(swarmB._peerInfo, (err) => {
|
||||
expect(err).not.to.exist()
|
||||
expect(dialSpyA.lastCall.args[0]).to.not.be.eql('Circuit')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('in a basic network', () => {
|
||||
// Create 5 nodes
|
||||
// Make node 1 act as a Bootstrap node and relay (speak tcp and ws)
|
||||
// Make nodes 2 & 3 speak tcp only
|
||||
// Make nodes 4 & 5 speak WS only
|
||||
// Have all nodes dial node 1
|
||||
// Each node should get the peers of node 1
|
||||
// Attempt to dial to each peer
|
||||
let bootstrapSwitch
|
||||
let tcpSwitch1
|
||||
let tcpSwitch2
|
||||
let wsSwitch1
|
||||
let wsSwitch2
|
||||
let bootstrapPeer
|
||||
let tcpPeer1
|
||||
let tcpPeer2
|
||||
let wsPeer1
|
||||
let wsPeer2
|
||||
|
||||
before((done) => createInfos(5, (err, infos) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
getPorts(6, (err, ports) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
bootstrapPeer = infos[0]
|
||||
tcpPeer1 = infos[1]
|
||||
tcpPeer2 = infos[2]
|
||||
wsPeer1 = infos[3]
|
||||
wsPeer2 = infos[4]
|
||||
|
||||
// Setup the addresses of our nodes
|
||||
bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
|
||||
bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
|
||||
tcpPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
|
||||
tcpPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
|
||||
wsPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
|
||||
wsPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
|
||||
|
||||
// Setup the bootstrap node with the minimum needed for being a relay
|
||||
bootstrapSwitch = new Swarm(bootstrapPeer, new PeerBook())
|
||||
bootstrapSwitch.connection.addStreamMuxer(multiplex)
|
||||
bootstrapSwitch.connection.reuse()
|
||||
bootstrapSwitch.connection.enableCircuitRelay({
|
||||
enabled: true,
|
||||
// The relay needs to allow hopping
|
||||
hop: {
|
||||
enabled: true
|
||||
}
|
||||
})
|
||||
|
||||
// Setup the tcp1 node with the minimum needed for dialing via a relay
|
||||
tcpSwitch1 = new Swarm(tcpPeer1, new PeerBook())
|
||||
tcpSwitch1.connection.addStreamMuxer(multiplex)
|
||||
tcpSwitch1.connection.reuse()
|
||||
tcpSwitch1.connection.enableCircuitRelay({
|
||||
enabled: true
|
||||
})
|
||||
|
||||
// Setup tcp2 node to not be able to dial/listen over relay
|
||||
tcpSwitch2 = new Swarm(tcpPeer2, new PeerBook())
|
||||
tcpSwitch2.connection.reuse()
|
||||
tcpSwitch2.connection.addStreamMuxer(multiplex)
|
||||
|
||||
// Setup the ws1 node with the minimum needed for dialing via a relay
|
||||
wsSwitch1 = new Swarm(wsPeer1, new PeerBook())
|
||||
wsSwitch1.connection.addStreamMuxer(multiplex)
|
||||
wsSwitch1.connection.reuse()
|
||||
wsSwitch1.connection.enableCircuitRelay({
|
||||
enabled: true
|
||||
})
|
||||
|
||||
// Setup the ws2 node with the minimum needed for dialing via a relay
|
||||
wsSwitch2 = new Swarm(wsPeer2, new PeerBook())
|
||||
wsSwitch2.connection.addStreamMuxer(multiplex)
|
||||
wsSwitch2.connection.reuse()
|
||||
wsSwitch2.connection.enableCircuitRelay({
|
||||
enabled: true
|
||||
})
|
||||
|
||||
bootstrapSwitch.transport.add('tcp', new TCP())
|
||||
bootstrapSwitch.transport.add('ws', new WS())
|
||||
tcpSwitch1.transport.add('tcp', new TCP())
|
||||
tcpSwitch2.transport.add('tcp', new TCP())
|
||||
wsSwitch1.transport.add('ws', new WS())
|
||||
wsSwitch2.transport.add('ws', new WS())
|
||||
|
||||
series([
|
||||
// start the nodes
|
||||
(cb) => {
|
||||
parallel([
|
||||
(cb) => bootstrapSwitch.start(cb),
|
||||
(cb) => tcpSwitch1.start(cb),
|
||||
(cb) => tcpSwitch2.start(cb),
|
||||
(cb) => wsSwitch1.start(cb),
|
||||
(cb) => wsSwitch2.start(cb)
|
||||
], cb)
|
||||
},
|
||||
// dial to the bootstrap node
|
||||
(cb) => {
|
||||
parallel([
|
||||
(cb) => tcpSwitch1.dial(bootstrapPeer, cb),
|
||||
(cb) => tcpSwitch2.dial(bootstrapPeer, cb),
|
||||
(cb) => wsSwitch1.dial(bootstrapPeer, cb),
|
||||
(cb) => wsSwitch2.dial(bootstrapPeer, cb)
|
||||
], cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) return done(err)
|
||||
|
||||
if (bootstrapSwitch._peerBook.getAllArray().length === 4) {
|
||||
return done()
|
||||
}
|
||||
|
||||
done = once(done)
|
||||
// Wait for everyone to connect, before we try relaying
|
||||
bootstrapSwitch.on('peer-mux-established', () => {
|
||||
if (bootstrapSwitch._peerBook.getAllArray().length === 4) {
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}))
|
||||
|
||||
before('wait so hop status can be negotiated', function (done) {
|
||||
setTimeout(done, 1000)
|
||||
})
|
||||
|
||||
after(function (done) {
|
||||
parallel([
|
||||
(cb) => bootstrapSwitch.stop(cb),
|
||||
(cb) => tcpSwitch1.stop(cb),
|
||||
(cb) => tcpSwitch2.stop(cb),
|
||||
(cb) => wsSwitch1.stop(cb),
|
||||
(cb) => wsSwitch2.stop(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('should be able to dial tcp -> tcp', (done) => {
|
||||
tcpSwitch2.on('peer-mux-established', (peerInfo) => {
|
||||
if (peerInfo.id.toB58String() === tcpPeer1.id.toB58String()) {
|
||||
tcpSwitch2.removeAllListeners('peer-mux-established')
|
||||
done()
|
||||
}
|
||||
})
|
||||
tcpSwitch1.dial(tcpPeer2, (err, connection) => {
|
||||
expect(err).to.not.exist()
|
||||
// We're not dialing a protocol, so we won't get a connection back
|
||||
expect(connection).to.be.undefined()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dial tcp -> ws over relay', (done) => {
|
||||
wsSwitch1.on('peer-mux-established', (peerInfo) => {
|
||||
if (peerInfo.id.toB58String() === tcpPeer1.id.toB58String()) {
|
||||
wsSwitch1.removeAllListeners('peer-mux-established')
|
||||
done()
|
||||
}
|
||||
})
|
||||
|
||||
tcpSwitch1.dial(wsPeer1, (err, connection) => {
|
||||
expect(err).to.not.exist()
|
||||
// We're not dialing a protocol, so we won't get a connection back
|
||||
expect(connection).to.be.undefined()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dial ws -> ws', (done) => {
|
||||
wsSwitch2.on('peer-mux-established', (peerInfo) => {
|
||||
if (peerInfo.id.toB58String() === wsPeer1.id.toB58String()) {
|
||||
wsSwitch2.removeAllListeners('peer-mux-established')
|
||||
done()
|
||||
}
|
||||
})
|
||||
wsSwitch1.dial(wsPeer2, (err, connection) => {
|
||||
expect(err).to.not.exist()
|
||||
// We're not dialing a protocol, so we won't get a connection back
|
||||
expect(connection).to.be.undefined()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dial ws -> tcp over relay', (done) => {
|
||||
tcpSwitch1.on('peer-mux-established', (peerInfo) => {
|
||||
if (peerInfo.id.toB58String() === wsPeer2.id.toB58String()) {
|
||||
tcpSwitch1.removeAllListeners('peer-mux-established')
|
||||
expect(Object.keys(tcpSwitch1._peerBook.getAll())).to.include(wsPeer2.id.toB58String())
|
||||
done()
|
||||
}
|
||||
})
|
||||
|
||||
wsSwitch2.dial(tcpPeer1, (err, connection) => {
|
||||
expect(err).to.not.exist()
|
||||
// We're not dialing a protocol, so we won't get a connection back
|
||||
expect(connection).to.be.undefined()
|
||||
})
|
||||
})
|
||||
|
||||
it('shouldnt be able to dial to a non relay node', (done) => {
|
||||
// tcpPeer2 doesnt have relay enabled
|
||||
wsSwitch1.dial(tcpPeer2, (err, connection) => {
|
||||
expect(err).to.exist()
|
||||
expect(connection).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('shouldnt be able to dial from a non relay node', (done) => {
|
||||
// tcpSwitch2 doesnt have relay enabled
|
||||
tcpSwitch2.dial(wsPeer1, (err, connection) => {
|
||||
expect(err).to.exist()
|
||||
expect(connection).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@ -13,7 +13,6 @@ const secio = require('libp2p-secio')
|
||||
const pull = require('pull-stream')
|
||||
const multiplex = require('pull-mplex')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const Connection = require('interface-connection').Connection
|
||||
const Protector = require('libp2p-pnet')
|
||||
const generatePSK = Protector.generate
|
||||
|
||||
@ -104,7 +103,7 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
done()
|
||||
})
|
||||
|
||||
@ -118,7 +117,7 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
expect(() => connection.close(new Error('shutting down'))).to.not.throw()
|
||||
done()
|
||||
})
|
||||
@ -171,11 +170,11 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
done()
|
||||
})
|
||||
|
||||
@ -192,7 +191,7 @@ describe('ConnectionFSM', () => {
|
||||
.callsArgWith(3, new Error('fail encrypt'))
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('close', () => {
|
||||
@ -213,11 +212,11 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.upgrade()
|
||||
})
|
||||
connection.once('muxed', (conn) => {
|
||||
@ -235,11 +234,11 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.upgrade()
|
||||
})
|
||||
connection.once('error:upgrade_failed', (err) => {
|
||||
@ -261,11 +260,11 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.upgrade()
|
||||
})
|
||||
connection.once('muxed', (conn) => {
|
||||
@ -273,7 +272,7 @@ describe('ConnectionFSM', () => {
|
||||
|
||||
connection.shake('/muxed-conn-test/1.0.0', (err, protocolConn) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(protocolConn).to.be.an.instanceof(Connection)
|
||||
expect(protocolConn).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
@ -292,11 +291,11 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.upgrade()
|
||||
})
|
||||
connection.once('muxed', (conn) => {
|
||||
@ -334,22 +333,22 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.upgrade()
|
||||
})
|
||||
connection.once('muxed', () => {
|
||||
throw new Error('connection shouldnt be muxed')
|
||||
})
|
||||
connection.once('unmuxed', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
|
||||
connection.shake('/unmuxed-conn-test/1.0.0', (err, protocolConn) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(protocolConn).to.be.an.instanceof(Connection)
|
||||
expect(protocolConn).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
@ -386,12 +385,12 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('private', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
done()
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.protect()
|
||||
})
|
||||
|
||||
@ -420,7 +419,7 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection).mark()
|
||||
expect(conn).to.exist().mark()
|
||||
connection.protect()
|
||||
})
|
||||
|
||||
@ -434,15 +433,15 @@ describe('ConnectionFSM', () => {
|
||||
})
|
||||
|
||||
connection.once('connected', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.protect()
|
||||
})
|
||||
connection.once('private', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
connection.encrypt()
|
||||
})
|
||||
connection.once('encrypted', (conn) => {
|
||||
expect(conn).to.be.an.instanceof(Connection)
|
||||
expect(conn).to.exist()
|
||||
done()
|
||||
})
|
||||
|
||||
|
@ -1,102 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const PeerBook = require('peer-book')
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const MultiAddr = require('multiaddr')
|
||||
const TestPeerInfos = require('./test-data/ids.json').infos
|
||||
|
||||
const getPeerInfo = require('libp2p-switch/get-peer-info')
|
||||
|
||||
describe('Get peer info', () => {
|
||||
let peerBook
|
||||
let peerInfoA
|
||||
let multiaddrA
|
||||
let peerIdA
|
||||
|
||||
before((done) => {
|
||||
peerBook = new PeerBook()
|
||||
PeerId.createFromJSON(TestPeerInfos[0].id, (err, id) => {
|
||||
peerIdA = id
|
||||
peerInfoA = new PeerInfo(peerIdA)
|
||||
multiaddrA = MultiAddr('/ipfs/QmdWYwTywvXBeLKWthrVNjkq9SafEDn1PbAZdz4xZW7Jd9')
|
||||
peerInfoA.multiaddrs.add(multiaddrA)
|
||||
peerBook.put(peerInfoA)
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able get peer info from multiaddr', () => {
|
||||
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
|
||||
expect(peerBook.has(_peerInfo)).to.equal(true)
|
||||
expect(peerInfoA).to.deep.equal(_peerInfo)
|
||||
})
|
||||
|
||||
it('should return a new PeerInfo with a multiAddr not in the PeerBook', () => {
|
||||
const wrongMultiAddr = MultiAddr('/ipfs/QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
|
||||
const _peerInfo = getPeerInfo(wrongMultiAddr, peerBook)
|
||||
expect(PeerInfo.isPeerInfo(_peerInfo)).to.equal(true)
|
||||
expect(peerBook.has(_peerInfo)).to.equal(false)
|
||||
})
|
||||
|
||||
it('should be able get peer info from peer id', () => {
|
||||
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
|
||||
expect(peerBook.has(_peerInfo)).to.equal(true)
|
||||
expect(peerInfoA).to.deep.equal(_peerInfo)
|
||||
})
|
||||
|
||||
it('should not be able to get the peer info for a wrong peer id', (done) => {
|
||||
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
|
||||
const func = () => {
|
||||
getPeerInfo(id, peerBook)
|
||||
}
|
||||
|
||||
expect(func).to.throw('Couldnt get PeerInfo')
|
||||
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('should add a peerInfo to the book', (done) => {
|
||||
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
|
||||
const peerInfo = new PeerInfo(id)
|
||||
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(false)
|
||||
|
||||
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
|
||||
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(true)
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('should return the most up to date version of the peer', (done) => {
|
||||
const ma1 = MultiAddr('/ip4/0.0.0.0/tcp/8080')
|
||||
const ma2 = MultiAddr('/ip6/::/tcp/8080')
|
||||
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
|
||||
const peerInfo = new PeerInfo(id)
|
||||
peerInfo.multiaddrs.add(ma1)
|
||||
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
|
||||
|
||||
const peerInfo2 = new PeerInfo(id)
|
||||
peerInfo2.multiaddrs.add(ma2)
|
||||
const returnedPeerInfo = getPeerInfo(peerInfo2, peerBook)
|
||||
expect(returnedPeerInfo.multiaddrs.toArray()).to.contain.members([
|
||||
ma1, ma2
|
||||
])
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('an invalid peer type should throw an error', () => {
|
||||
const func = () => {
|
||||
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)
|
||||
}
|
||||
|
||||
expect(func).to.throw('peer type not recognized')
|
||||
})
|
||||
})
|
@ -7,7 +7,7 @@ chai.use(require('chai-checkmark'))
|
||||
const expect = chai.expect
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
const setImmediate = require('async/setImmediate')
|
||||
const nextTick = require('async/nextTick')
|
||||
|
||||
const LimitDialer = require('libp2p-switch/limit-dialer')
|
||||
const utils = require('./utils')
|
||||
@ -37,14 +37,14 @@ describe('LimitDialer', () => {
|
||||
// mock transport
|
||||
const t1 = {
|
||||
dial (addr, cb) {
|
||||
setTimeout(() => cb(error), 1)
|
||||
nextTick(cb, error)
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
dialer.dialMany(peers[0].id, t1, peers[0].multiaddrs.toArray(), (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(err).to.eql([error, error, error])
|
||||
expect(err).to.include.members([error, error, error])
|
||||
expect(conn).to.not.exist()
|
||||
done()
|
||||
})
|
||||
@ -58,16 +58,16 @@ describe('LimitDialer', () => {
|
||||
dial (addr, cb) {
|
||||
const as = addr.toString()
|
||||
if (as.match(/191/)) {
|
||||
setImmediate(() => cb(new Error('fail')))
|
||||
nextTick(cb, new Error('fail'))
|
||||
return null
|
||||
} else if (as.match(/192/)) {
|
||||
setTimeout(cb, 2)
|
||||
nextTick(cb)
|
||||
return {
|
||||
source: pull.values([1]),
|
||||
sink: pull.drain()
|
||||
}
|
||||
} else if (as.match(/193/)) {
|
||||
setTimeout(cb, 8)
|
||||
nextTick(cb)
|
||||
return {
|
||||
source: pull.values([2]),
|
||||
sink: pull.drain()
|
||||
|
@ -8,7 +8,6 @@ require('./stream-muxers.node')
|
||||
require('./secio.node')
|
||||
require('./swarm-no-muxing.node')
|
||||
require('./swarm-muxing.node')
|
||||
require('./circuit-relay.node')
|
||||
require('./identify.node')
|
||||
require('./limit-dialer.node')
|
||||
require('./stats.node')
|
||||
|
Reference in New Issue
Block a user