From ad1e6ff705a7b98fc2b3c0c8dd45aaf68b5b1b7f Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 9 Apr 2017 16:51:35 -0700 Subject: [PATCH 01/29] feat: adding circuit relaying --- package.json | 12 + src/index.js | 20 + test/node.js | 1 + test/nodejs-bundle/circuit/circuit.js | 444 ++++++++++++++++++ test/nodejs-bundle/circuit/fixtures/nodes.js | 25 + .../circuit/helpers/test-node.js | 22 + test/nodejs-bundle/circuit/helpers/utils.js | 110 +++++ 7 files changed, 634 insertions(+) create mode 100644 test/nodejs-bundle/circuit/circuit.js create mode 100644 test/nodejs-bundle/circuit/fixtures/nodes.js create mode 100644 test/nodejs-bundle/circuit/helpers/test-node.js create mode 100644 test/nodejs-bundle/circuit/helpers/utils.js diff --git a/package.json b/package.json index ed179c5c4e..b89a88b339 100644 --- a/package.json +++ b/package.json @@ -66,9 +66,21 @@ "pull-serializer": "^0.3.2", "pull-stream": "^3.6.1", "safe-buffer": "^5.1.1", + "sinon": "^2.3.6", "electron-webrtc": "^0.3.0", "wrtc": "0.0.62" }, + "dependencies": { + "async": "^2.5.0", + "libp2p-ping": "~0.4.0", + "libp2p-swarm": "~0.29.1", + "libp2p-circuit": "~0.0.1", + "mafmt": "^2.1.8", + "multiaddr": "^2.3.0", + "peer-book": "~0.4.0", + "peer-id": "~0.8.7", + "peer-info": "~0.9.2" + }, "contributors": [ "Chris Bratlien ", "Daijiro Wachi ", diff --git a/src/index.js b/src/index.js index 8667e908b4..3dfb8d9cdb 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ const PeerInfo = require('peer-info') const PeerBook = require('peer-book') const mafmt = require('mafmt') const multiaddr = require('multiaddr') +const Circuit = require('libp2p-circuit') exports = module.exports @@ -31,6 +32,7 @@ class Node extends EventEmitter { _options = _options || {} this._isStarted = false + this.relayCircuit = null this.swarm = new Swarm(this.peerInfo, this.peerBook) @@ -43,6 +45,24 @@ class Node extends EventEmitter { // If muxer exists, we can use Identify this.swarm.connection.reuse() + // enable circuit relaying + // TODO: move defaults elsewhere + _options.Relay = Object.assign({ + Circuit: { + Enabled: false, + Active: false + }, + DialMode: 'onion' + }, _options.Relay) + + if (_options.Relay.Circuit.Enabled) { + this.relayCircuit = new Circuit.Relay(_options.Relay.Circuit) + this.relayCircuit.mount(this.swarm) + } + + // If muxer exists, we can use Relay for listening/dialing + this.swarm.connection.relay(_options.Relay) + // Received incommind dial and muxer upgrade happened, // reuse this muxed connection this.swarm.on('peer-mux-established', (peerInfo) => { diff --git a/test/node.js b/test/node.js index f66c017b50..3eddc12ea9 100644 --- a/test/node.js +++ b/test/node.js @@ -8,3 +8,4 @@ require('./nodejs-bundle/stream-muxing') require('./nodejs-bundle/discovery') require('./nodejs-bundle/peer-routing') require('./nodejs-bundle/content-routing') +require('./nodejs-bundle/circuit/circuit') diff --git a/test/nodejs-bundle/circuit/circuit.js b/test/nodejs-bundle/circuit/circuit.js new file mode 100644 index 0000000000..f94aea49ed --- /dev/null +++ b/test/nodejs-bundle/circuit/circuit.js @@ -0,0 +1,444 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const spdy = require('libp2p-spdy') +const multiplex = require('libp2p-multiplex') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const sinon = require('sinon') +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +const nodeKeys = require('./fixtures/nodes') + +describe('test relay', function () { + describe('test connecting over any relay', function () { + this.timeout(500000) + + let portBase = 9010 // TODO: randomize or mock sockets + let testNodes + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode: { + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true + } + } + } + }, + nodeA: { + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + done() + }) + } + + beforeEach(function (done) { + setUpNodes(spdy, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode = testNodes['relayNode'] + + waterfall([ + (cb) => nodeA.dial(relayNode.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, () => done()) + }) + + it('dial to a node over a relay and write values', function (done) { + utils.dialAndReverse( + testNodes.nodeA, + testNodes.nodeB, + ['hello', 'hello1', 'hello2', 'hello3'], + (err, result) => { + expect(err).to.be.null() + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) + expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) + expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) + done() + }) + }) + }) + + describe('test listening on relay address', function () { + this.timeout(500000) + + describe(`listen on an explicit relay addr`, function () { + let portBase = 9020 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + let active = false + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ip4/0.0.0.0/tcp/9023/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ip4/0.0.0.0/tcp/9020/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') + relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') + + done() + }) + } + + // no way to tell which relay is going to be used with multidialing + describe(`passive`, function () { + beforeEach(function (done) { + waterfall([ + (cb) => setUpNodes(multiplex, cb), + (cb) => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) + ], () => setTimeout(cb, 1000)) // WS needs some time to initialize + } + ], done) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy2.called).to.be.not.ok() + expect(relaySpy1.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.not.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + }) + + describe.skip(`active`, function () { + beforeEach(function (done) { + active = true + setUpNodes(multiplex, () => { + setTimeout(done, 1000) // give the nodes time to startup + }) + }) + + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy2.called).to.be.not.ok() + expect(relaySpy1.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.not.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + }) + }) + + describe(`listen on an explicit chained relay addr`, function () { + let portBase = 9030 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit` + + `/ip4/0.0.0.0/tcp/9031/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') + relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') + + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct chained relay addr', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['relayNode2'].peerInfo.id.toB58String()}`)) + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) + }) + + it('dial over the correct chained relay addr and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ip4/0.0.0.0/tcp/9031/ipfs/${testNodes['relayNode1'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) + }) + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/fixtures/nodes.js b/test/nodejs-bundle/circuit/fixtures/nodes.js new file mode 100644 index 0000000000..71a274d91d --- /dev/null +++ b/test/nodejs-bundle/circuit/fixtures/nodes.js @@ -0,0 +1,25 @@ +'use strict' + +exports.node1 = { + id: 'QmSswe1d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7KOXdGW2RcRLj7q5E5uXtnEoCillViVJtnRPSun7Gzkfm2Gn3ezQH0WZKVkA+mnpd5JgW2JsS69L6pEPnS0OWZT4b+3AFZgXL8vs2ucR2CJeLdxYdilHuPAoGBAPLCzBkAboXZZtvEWqzqtVNqdMrjLHihFrpg4TXSsk8+ZQZCVN+sRyTGTvBX8+Jvx4at6ClaSgT3eJ/412fEH6CHvrFXjUE9W9y6X0axxaT63y1OXmFiB/hU3vjLWZKZWSDGNS7St02fYri4tWmGtJDjYG1maLRhMSzcoj4fP1xz', + pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC6pg6LYWbY+49SOYdYap6RPqKqZxg80IXeo3hiTUbiGtTruxVYZpnz3UbernL9J9mwlXJGRUQJUKmXmi1yePTQiyclpH0KyPefaWLbpxJQdCBI1TPZpDWo2hutWSPqhKBU1QyH2FLKQPWLdxuIX1cNFPtIlSl5gCxN6oiDIwh7++kxNM1G+d5XgJX6iHLlLoNv3Wn6XYX+VtYdyZRFk8gYyT2BrISbxmsrSjSOodwUUzF8TYTjsqW6ksL2x0mrRm2cMM9evULktqwU+I8l9ulASDbFWBXUToXaZSL9M+Oq5JvZO0WIjPeYVpAgWladtayhdxg5dBv8aTbDaM5DZvyRAgMBAAE=' +} diff --git a/test/nodejs-bundle/circuit/helpers/test-node.js b/test/nodejs-bundle/circuit/helpers/test-node.js new file mode 100644 index 0000000000..f5a92e0124 --- /dev/null +++ b/test/nodejs-bundle/circuit/helpers/test-node.js @@ -0,0 +1,22 @@ +'use strict' + +const Libp2p = require('../../../../src') +const secio = require('libp2p-secio') + +class TestNode extends Libp2p { + constructor (peerInfo, transports, muxer, options) { + options = options || {} + + const modules = { + transport: transports, + connection: { + muxer: [muxer], + crypto: options.isCrypto ? [secio] : null + }, + discovery: [] + } + super(modules, peerInfo, null, options) + } +} + +module.exports = TestNode diff --git a/test/nodejs-bundle/circuit/helpers/utils.js b/test/nodejs-bundle/circuit/helpers/utils.js new file mode 100644 index 0000000000..de198ca90d --- /dev/null +++ b/test/nodejs-bundle/circuit/helpers/utils.js @@ -0,0 +1,110 @@ +'use strict' + +const TestNode = require('./test-node') +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') +const eachAsync = require('async/each') +const pull = require('pull-stream') + +exports.createNodes = function createNodes (configNodes, callback) { + const nodes = {} + eachAsync(Object.keys(configNodes), (key, cb1) => { + let config = configNodes[key] + + const setup = (err, peer) => { + if (err) { + callback(err) + } + + eachAsync(config.addrs, (addr, cb2) => { + peer.multiaddrs.add(addr) + cb2() + }, (err) => { + if (err) { + return callback(err) + } + + nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config) + cb1() + }) + } + + if (config.id) { + PeerId.createFromJSON(config.id, (err, peerId) => { + if (err) return callback(err) + PeerInfo.create(peerId, setup) + }) + } else { + PeerInfo.create(setup) + } + }, (err) => { + if (err) { + return callback(err) + } + + startNodes(nodes, (err) => { + if (err) { + callback(err) + } + + callback(null, nodes) + }) + }) +} + +function startNodes (nodes, callback) { + eachAsync(Object.keys(nodes), + (key, cb) => { + nodes[key].start(cb) + }, + (err) => { + if (err) { + return callback(err) + } + callback(null) + }) +} + +exports.stopNodes = function stopNodes (nodes, callback) { + eachAsync(Object.keys(nodes), + (key, cb) => { + nodes[key].stop(cb) + }, + (err) => { + if (err) { + return callback(err) + } + callback() + }) +} + +function reverse (protocol, conn) { + pull( + conn, + pull.map((data) => { + return data.toString().split('').reverse().join('') + }), + conn + ) +} + +exports.dialAndReverse = function dialAndRevers (srcNode, dstNode, vals, done) { + dstNode.handle('/ipfs/reverse/1.0.0', reverse) + + srcNode.dial(dstNode.peerInfo, '/ipfs/reverse/1.0.0', (err, conn) => { + if (err) return done(err) + + pull( + pull.values(vals), + conn, + pull.collect((err, data) => { + if (err) return done(err) + + let reversed = data.map((val, i) => { + return val.toString() + }) + + srcNode.hangUp(srcNode.peerInfo, () => done(null, reversed)) + })) + }) +} From 3cc96737ca0fad1426fe20701297b41bedc452b0 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Mar 2017 12:26:34 +0100 Subject: [PATCH 02/29] feat: new super simplified API --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 3dfb8d9cdb..df3869a61d 100644 --- a/src/index.js +++ b/src/index.js @@ -269,6 +269,7 @@ class Node extends EventEmitter { dial (peer, protocol, callback) { assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) + const peerInfo = this._getPeerInfo(peer) if (typeof protocol === 'function') { callback = protocol From 58e4f1a7a60e1de84277d0fd5005d378964489dd Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 29 Mar 2017 20:02:41 +0100 Subject: [PATCH 03/29] feat: append peer id to multiaddr if not there --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index df3869a61d..4d4f8b5290 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,7 @@ const Ping = require('libp2p-ping') const Swarm = require('libp2p-swarm') const PeerId = require('peer-id') const PeerInfo = require('peer-info') +const mafmt = require('mafmt') const PeerBook = require('peer-book') const mafmt = require('mafmt') const multiaddr = require('multiaddr') @@ -72,6 +73,8 @@ class Node extends EventEmitter { this.swarm.on('peer-mux-closed', (peerInfo) => { this.emit('peer:disconnect', peerInfo) + // TODO remove this line + this.peerBook.removeByB58String(peerInfo.id.toB58String()) }) } From 89c3f442c8f34390aba915d06bb9943b0eb76e97 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 6 Apr 2017 15:45:23 -0400 Subject: [PATCH 04/29] =?UTF-8?q?[WIP]=C2=A0Awesome=20DHT=20(#86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: integrate dht * better interfaces * docs: add documentation for peerRouting, contentRouting, dht * fix: take in passed datastore * fix: update usage of _getPeerInfo * fix: getPeerInfo * docs: update docs * moar feat: correctly handle p2p-circuit addrs when creating a peer info object refactor: rework config options --- src/index.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index 4d4f8b5290..ea5ee66aa5 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,6 @@ const Ping = require('libp2p-ping') const Swarm = require('libp2p-swarm') const PeerId = require('peer-id') const PeerInfo = require('peer-info') -const mafmt = require('mafmt') const PeerBook = require('peer-book') const mafmt = require('mafmt') const multiaddr = require('multiaddr') @@ -46,23 +45,13 @@ class Node extends EventEmitter { // If muxer exists, we can use Identify this.swarm.connection.reuse() - // enable circuit relaying - // TODO: move defaults elsewhere - _options.Relay = Object.assign({ - Circuit: { - Enabled: false, - Active: false - }, - DialMode: 'onion' - }, _options.Relay) - - if (_options.Relay.Circuit.Enabled) { - this.relayCircuit = new Circuit.Relay(_options.Relay.Circuit) + if (_options.relay && _options.relay.circuit) { + this.relayCircuit = new Circuit.Hop(_options.relay.circuit) this.relayCircuit.mount(this.swarm) } // If muxer exists, we can use Relay for listening/dialing - this.swarm.connection.relay(_options.Relay) + this.swarm.connection.enableRelayDialing(_options.relay) // Received incommind dial and muxer upgrade happened, // reuse this muxed connection @@ -73,8 +62,6 @@ class Node extends EventEmitter { this.swarm.on('peer-mux-closed', (peerInfo) => { this.emit('peer:disconnect', peerInfo) - // TODO remove this line - this.peerBook.removeByB58String(peerInfo.id.toB58String()) }) } @@ -272,7 +259,6 @@ class Node extends EventEmitter { dial (peer, protocol, callback) { assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) - const peerInfo = this._getPeerInfo(peer) if (typeof protocol === 'function') { callback = protocol @@ -322,16 +308,24 @@ class Node extends EventEmitter { // PeerInfo if (PeerInfo.isPeerInfo(peer)) { p = peer - // Multiaddr instance (not string) + // Multiaddr instance (not string) } else if (multiaddr.isMultiaddr(peer)) { - const peerIdB58Str = peer.getPeerId() + let peerId + if (peer.toString().includes('p2p-circuit')) { + // last segment is always the peer ipfs segment + peerId = multiaddr(peer.toString().split('p2p-circuit').pop()) + } else { + peerId = peer + } + + const peerIdB58Str = peerId.getPeerId() try { p = this.peerBook.get(peerIdB58Str) } catch (err) { p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) } p.multiaddrs.add(peer) - // PeerId + // PeerId } else if (PeerId.isPeerId(peer)) { const peerIdB58Str = peer.toB58String() try { From afd31be1403db90817f22fc96a71b8be19a6fbd0 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 23 Jul 2017 20:17:38 -0700 Subject: [PATCH 05/29] feat: rework circuit relay for protobufs --- src/index.js | 7 - test/nodejs-bundle/circuit/circuit.js | 143 +++++++++--------- .../circuit/helpers/test-node.js | 1 - 3 files changed, 69 insertions(+), 82 deletions(-) diff --git a/src/index.js b/src/index.js index ea5ee66aa5..db3509472e 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,6 @@ const PeerInfo = require('peer-info') const PeerBook = require('peer-book') const mafmt = require('mafmt') const multiaddr = require('multiaddr') -const Circuit = require('libp2p-circuit') exports = module.exports @@ -32,7 +31,6 @@ class Node extends EventEmitter { _options = _options || {} this._isStarted = false - this.relayCircuit = null this.swarm = new Swarm(this.peerInfo, this.peerBook) @@ -45,11 +43,6 @@ class Node extends EventEmitter { // If muxer exists, we can use Identify this.swarm.connection.reuse() - if (_options.relay && _options.relay.circuit) { - this.relayCircuit = new Circuit.Hop(_options.relay.circuit) - this.relayCircuit.mount(this.swarm) - } - // If muxer exists, we can use Relay for listening/dialing this.swarm.connection.enableRelayDialing(_options.relay) diff --git a/test/nodejs-bundle/circuit/circuit.js b/test/nodejs-bundle/circuit/circuit.js index f94aea49ed..58c8faaadb 100644 --- a/test/nodejs-bundle/circuit/circuit.js +++ b/test/nodejs-bundle/circuit/circuit.js @@ -3,7 +3,6 @@ const TCP = require('libp2p-tcp') const WS = require('libp2p-websockets') -const spdy = require('libp2p-spdy') const multiplex = require('libp2p-multiplex') const waterfall = require('async/waterfall') @@ -71,7 +70,7 @@ describe('test relay', function () { } beforeEach(function (done) { - setUpNodes(spdy, () => { + setUpNodes(multiplex, () => { let nodeA = testNodes['nodeA'] let nodeB = testNodes['nodeB'] let relayNode = testNodes['relayNode'] @@ -184,8 +183,8 @@ describe('test relay', function () { } testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') - relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') done() }) @@ -219,12 +218,10 @@ describe('test relay', function () { it('dial over the correct relay', function (done) { utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { expect(err).to.be.null() - expect(relaySpy2.called).to.be.not.ok() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + expect(relaySpy1.args.some((a) => { + return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() expect(result[0]).to.equal('hello'.split('').reverse('').join('')) @@ -235,12 +232,10 @@ describe('test relay', function () { it('dial over the correct relay and transport', function (done) { utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { expect(err).to.be.null() - expect(relaySpy1.called).to.be.not.ok() - expect(relaySpy2.called).to.be.ok() - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + expect(relaySpy2.args.some((a) => { + return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` + })).to.be.ok() expect(result[0]).to.equal('hello'.split('').reverse('').join('')) @@ -249,52 +244,52 @@ describe('test relay', function () { }) }) - describe.skip(`active`, function () { - beforeEach(function (done) { - active = true - setUpNodes(multiplex, () => { - setTimeout(done, 1000) // give the nodes time to startup - }) - }) - - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy2.called).to.be.not.ok() - expect(relaySpy1.called).to.be.ok() - - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.not.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - }) + // describe.skip(`active`, function () { + // beforeEach(function (done) { + // active = true + // setUpNodes(multiplex, () => { + // setTimeout(done, 1000) // give the nodes time to startup + // }) + // }) + // + // afterEach(function circuitTests (done) { + // relaySpy1.reset() + // relaySpy2.reset() + // utils.stopNodes(testNodes, done) + // }) + // + // it('dial over the correct relay', function (done) { + // utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + // expect(err).to.be.null() + // expect(relaySpy2.called).to.be.not.ok() + // expect(relaySpy1.called).to.be.ok() + // + // expect(relaySpy1.args[0][1].toString()) + // .to + // .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + // + // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + // + // done(err) + // }) + // }) + // + // it('dial over the correct relay and transport', function (done) { + // utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + // expect(err).to.be.null() + // expect(relaySpy1.called).to.be.not.ok() + // expect(relaySpy2.called).to.be.ok() + // + // expect(relaySpy2.args[0][1].toString()) + // .to + // .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + // + // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + // + // done(err) + // }) + // }) + // }) }) describe(`listen on an explicit chained relay addr`, function () { @@ -371,8 +366,8 @@ describe('test relay', function () { } testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') - relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') done() }) @@ -408,13 +403,13 @@ describe('test relay', function () { expect(relaySpy1.called).to.be.ok() expect(relaySpy2.called).to.be.ok() - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['relayNode2'].peerInfo.id.toB58String()}`)) + expect(relaySpy1.args.some((a) => { + return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['relayNode2'].peerInfo.id.toB58String()}` + })).to.be.ok() - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + expect(relaySpy2.args.some((a) => { + return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() expect(result[0]).to.equal('hello'.split('').reverse('').join('')) done(err) @@ -427,13 +422,13 @@ describe('test relay', function () { expect(relaySpy1.called).to.be.ok() expect(relaySpy2.called).to.be.ok() - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + expect(relaySpy1.args.some((a) => { + return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` + })).to.be.ok() - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ip4/0.0.0.0/tcp/9031/ipfs/${testNodes['relayNode1'].peerInfo.id.toB58String()}`)) + expect(relaySpy2.args.some((a) => { + return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ip4/0.0.0.0/tcp/9031/ipfs/${testNodes['relayNode1'].peerInfo.id.toB58String()}` + })).to.be.ok() expect(result[0]).to.equal('hello'.split('').reverse('').join('')) done(err) diff --git a/test/nodejs-bundle/circuit/helpers/test-node.js b/test/nodejs-bundle/circuit/helpers/test-node.js index f5a92e0124..6f29f201f3 100644 --- a/test/nodejs-bundle/circuit/helpers/test-node.js +++ b/test/nodejs-bundle/circuit/helpers/test-node.js @@ -6,7 +6,6 @@ const secio = require('libp2p-secio') class TestNode extends Libp2p { constructor (peerInfo, transports, muxer, options) { options = options || {} - const modules = { transport: transports, connection: { From 7acdc9669f76f40346035230d4829f7291bc0637 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 2 Aug 2017 10:33:24 -0700 Subject: [PATCH 06/29] feat: circuit loading and tests --- package.json | 39 +- test/node.js | 2 +- test/nodejs-bundle/circuit/circuit.js | 439 ------------------ .../circuit/dial-over-any-relay.js | 99 ++++ .../dial-over-specific-relay-transport.js | 141 ++++++ .../circuit/dial-over-specific-relay.js | 208 +++++++++ test/nodejs-bundle/circuit/index.js | 5 + 7 files changed, 468 insertions(+), 465 deletions(-) delete mode 100644 test/nodejs-bundle/circuit/circuit.js create mode 100644 test/nodejs-bundle/circuit/dial-over-any-relay.js create mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js create mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay.js create mode 100644 test/nodejs-bundle/circuit/index.js diff --git a/package.json b/package.json index b89a88b339..8552082c7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "libp2p", - "version": "0.12.4", + "version": "0.12.0", "description": "JavaScript base class for libp2p bundles", "main": "src/index.js", "scripts": { @@ -39,11 +39,11 @@ "dependencies": { "async": "^2.5.0", "libp2p-ping": "~0.6.0", - "libp2p-swarm": "~0.32.4", - "mafmt": "^3.0.1", - "multiaddr": "^3.0.1", - "peer-book": "~0.5.1", - "peer-id": "~0.10.1", + "libp2p-swarm": "~0.32.0", + "mafmt": "^3.0.0", + "multiaddr": "^3.0.0", + "peer-book": "~0.5.0", + "peer-id": "~0.10.0", "peer-info": "~0.11.0" }, "devDependencies": { @@ -51,39 +51,28 @@ "chai": "^4.1.2", "dirty-chai": "^2.0.1", "cids": "~0.5.1", - "libp2p-kad-dht": "~0.5.1", - "libp2p-mdns": "~0.9.1", + "libp2p-kad-dht": "~0.5.0", + "libp2p-mdns": "~0.9.0", "libp2p-multiplex": "~0.5.0", - "libp2p-railing": "~0.7.1", - "libp2p-secio": "~0.8.1", + "libp2p-railing": "~0.7.0", + "libp2p-circuit": "0.0.2", + "libp2p-secio": "~0.8.0", "libp2p-spdy": "~0.11.0", "libp2p-tcp": "~0.11.0", - "libp2p-webrtc-star": "~0.13.2", + "libp2p-webrtc-star": "~0.13.0", "libp2p-websockets": "~0.10.1", "lodash.times": "^4.3.2", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", "pull-serializer": "^0.3.2", - "pull-stream": "^3.6.1", + "pull-stream": "^3.6.0", "safe-buffer": "^5.1.1", "sinon": "^2.3.6", "electron-webrtc": "^0.3.0", "wrtc": "0.0.62" }, - "dependencies": { - "async": "^2.5.0", - "libp2p-ping": "~0.4.0", - "libp2p-swarm": "~0.29.1", - "libp2p-circuit": "~0.0.1", - "mafmt": "^2.1.8", - "multiaddr": "^2.3.0", - "peer-book": "~0.4.0", - "peer-id": "~0.8.7", - "peer-info": "~0.9.2" - }, "contributors": [ "Chris Bratlien ", - "Daijiro Wachi ", "David Dias ", "Elven ", "Friedel Ziegelmayer ", @@ -93,4 +82,4 @@ "greenkeeperio-bot ", "mayerwin " ] -} +} \ No newline at end of file diff --git a/test/node.js b/test/node.js index 3eddc12ea9..2c579ab89c 100644 --- a/test/node.js +++ b/test/node.js @@ -8,4 +8,4 @@ require('./nodejs-bundle/stream-muxing') require('./nodejs-bundle/discovery') require('./nodejs-bundle/peer-routing') require('./nodejs-bundle/content-routing') -require('./nodejs-bundle/circuit/circuit') +require('./nodejs-bundle/circuit') diff --git a/test/nodejs-bundle/circuit/circuit.js b/test/nodejs-bundle/circuit/circuit.js deleted file mode 100644 index 58c8faaadb..0000000000 --- a/test/nodejs-bundle/circuit/circuit.js +++ /dev/null @@ -1,439 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const sinon = require('sinon') -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -const nodeKeys = require('./fixtures/nodes') - -describe('test relay', function () { - describe('test connecting over any relay', function () { - this.timeout(500000) - - let portBase = 9010 // TODO: randomize or mock sockets - let testNodes - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode: { - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true - } - } - } - }, - nodeA: { - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode = testNodes['relayNode'] - - waterfall([ - (cb) => nodeA.dial(relayNode.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, () => done()) - }) - - it('dial to a node over a relay and write values', function (done) { - utils.dialAndReverse( - testNodes.nodeA, - testNodes.nodeB, - ['hello', 'hello1', 'hello2', 'hello3'], - (err, result) => { - expect(err).to.be.null() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) - expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) - expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) - done() - }) - }) - }) - - describe('test listening on relay address', function () { - this.timeout(500000) - - describe(`listen on an explicit relay addr`, function () { - let portBase = 9020 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - let active = false - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ip4/0.0.0.0/tcp/9023/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ip4/0.0.0.0/tcp/9020/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') - - done() - }) - } - - // no way to tell which relay is going to be used with multidialing - describe(`passive`, function () { - beforeEach(function (done) { - waterfall([ - (cb) => setUpNodes(multiplex, cb), - (cb) => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) - ], () => setTimeout(cb, 1000)) // WS needs some time to initialize - } - ], done) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - - expect(relaySpy1.args.some((a) => { - return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - - expect(relaySpy2.args.some((a) => { - return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - }) - - // describe.skip(`active`, function () { - // beforeEach(function (done) { - // active = true - // setUpNodes(multiplex, () => { - // setTimeout(done, 1000) // give the nodes time to startup - // }) - // }) - // - // afterEach(function circuitTests (done) { - // relaySpy1.reset() - // relaySpy2.reset() - // utils.stopNodes(testNodes, done) - // }) - // - // it('dial over the correct relay', function (done) { - // utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - // expect(err).to.be.null() - // expect(relaySpy2.called).to.be.not.ok() - // expect(relaySpy1.called).to.be.ok() - // - // expect(relaySpy1.args[0][1].toString()) - // .to - // .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - // - // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - // - // done(err) - // }) - // }) - // - // it('dial over the correct relay and transport', function (done) { - // utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - // expect(err).to.be.null() - // expect(relaySpy1.called).to.be.not.ok() - // expect(relaySpy2.called).to.be.ok() - // - // expect(relaySpy2.args[0][1].toString()) - // .to - // .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - // - // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - // - // done(err) - // }) - // }) - // }) - }) - - describe(`listen on an explicit chained relay addr`, function () { - let portBase = 9030 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit` + - `/ip4/0.0.0.0/tcp/9031/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports['Circuit'].listeners[0].hopHandler, 'handle') - - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct chained relay addr', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy1.args.some((a) => { - return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['relayNode2'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(relaySpy2.args.some((a) => { - return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) - }) - }) - - it('dial over the correct chained relay addr and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy1.args.some((a) => { - return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(relaySpy2.args.some((a) => { - return a[0].dstPeer && a[0].dstPeer.addrs[0].toString() === `/ip4/0.0.0.0/tcp/9031/ipfs/${testNodes['relayNode1'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) - }) - }) - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/dial-over-any-relay.js b/test/nodejs-bundle/circuit/dial-over-any-relay.js new file mode 100644 index 0000000000..d1f3c08db7 --- /dev/null +++ b/test/nodejs-bundle/circuit/dial-over-any-relay.js @@ -0,0 +1,99 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const multiplex = require('libp2p-multiplex') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +describe('test connecting over any relay', function () { + this.timeout(500000) + + let portBase = 9010 // TODO: randomize or mock sockets + let testNodes + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode: { + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true + } + } + } + }, + nodeA: { + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode = testNodes['relayNode'] + + waterfall([ + (cb) => nodeA.dial(relayNode.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, () => done()) + }) + + it('dial to a node over a relay and write values', function (done) { + utils.dialAndReverse( + testNodes.nodeB, + testNodes.nodeA, + ['hello', 'hello1', 'hello2', 'hello3'], + (err, result) => { + expect(err).to.be.null() + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) + expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) + expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) + done() + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js new file mode 100644 index 0000000000..2e91366c27 --- /dev/null +++ b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js @@ -0,0 +1,141 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const multiplex = require('libp2p-multiplex') +const multiaddr = require('multiaddr') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const sinon = require('sinon') +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +const nodeKeys = require('./fixtures/nodes') +const Circuit = require('libp2p-circuit') + +describe(`listen on an explicit chained relay addr`, function () { + this.timeout(500000) + + let portBase = 9030 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct chained relay addr', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy1.args.some((a) => { + return a[0] && + a[0].dstPeer && + multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay.js b/test/nodejs-bundle/circuit/dial-over-specific-relay.js new file mode 100644 index 0000000000..575d423f93 --- /dev/null +++ b/test/nodejs-bundle/circuit/dial-over-specific-relay.js @@ -0,0 +1,208 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const multiplex = require('libp2p-multiplex') +const multiaddr = require('multiaddr') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const sinon = require('sinon') +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +const nodeKeys = require('./fixtures/nodes') +const Circuit = require('libp2p-circuit') + +describe(`listen on an explicit relay addr`, function () { + this.timeout(500000) + + let portBase = 9020 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + let active = false + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + + done() + }) + } + + // no way to tell which relay is going to be used with multidialing + describe(`passive`, function () { + beforeEach(function (done) { + waterfall([ + (cb) => setUpNodes(multiplex, cb), + (cb) => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) + ], () => setTimeout(cb, 1000)) // WS needs some time to initialize + } + ], done) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + + expect(relaySpy1.args.some((a) => { + return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === + `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + + expect(relaySpy2.args.some((a) => { + return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === + `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` + })).to.be.ok() + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + }) + + // describe.skip(`active`, function () { + // beforeEach(function (done) { + // active = true + // setUpNodes(multiplex, () => { + // setTimeout(done, 1000) // give the nodes time to startup + // }) + // }) + // + // afterEach(function circuitTests (done) { + // relaySpy1.reset() + // relaySpy2.reset() + // utils.stopNodes(testNodes, done) + // }) + // + // it('dial over the correct relay', function (done) { + // utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + // expect(err).to.be.null() + // expect(relaySpy2.called).to.be.not.ok() + // expect(relaySpy1.called).to.be.ok() + // + // expect(relaySpy1.args[0][1].toString()) + // .to + // .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + // + // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + // + // done(err) + // }) + // }) + // + // it('dial over the correct relay and transport', function (done) { + // utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + // expect(err).to.be.null() + // expect(relaySpy1.called).to.be.not.ok() + // expect(relaySpy2.called).to.be.ok() + // + // expect(relaySpy2.args[0][1].toString()) + // .to + // .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + // + // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + // + // done(err) + // }) + // }) + // }) +}) diff --git a/test/nodejs-bundle/circuit/index.js b/test/nodejs-bundle/circuit/index.js new file mode 100644 index 0000000000..fcf515e667 --- /dev/null +++ b/test/nodejs-bundle/circuit/index.js @@ -0,0 +1,5 @@ +'use strict' + +require('./dial-over-any-relay') +require('./dial-over-specific-relay') +require('./dial-over-specific-relay-transport') From 485eebd49e6901d3c02c33d4f8dde1be3a5913a3 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 16 Aug 2017 17:10:04 -0600 Subject: [PATCH 07/29] fix: clean up _getPeerInfo to work with /p2p-circuit --- src/index.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index db3509472e..04c37031c5 100644 --- a/src/index.js +++ b/src/index.js @@ -171,7 +171,7 @@ class Node extends EventEmitter { this.swarm.transport.add( transport.tag || transport.constructor.name, transport) } else if (transport.constructor && - transport.constructor.name === 'WebSockets') { + transport.constructor.name === 'WebSockets') { // TODO find a cleaner way to signal that a transport is always // used for dialing, even if no listener ws = transport @@ -303,15 +303,7 @@ class Node extends EventEmitter { p = peer // Multiaddr instance (not string) } else if (multiaddr.isMultiaddr(peer)) { - let peerId - if (peer.toString().includes('p2p-circuit')) { - // last segment is always the peer ipfs segment - peerId = multiaddr(peer.toString().split('p2p-circuit').pop()) - } else { - peerId = peer - } - - const peerIdB58Str = peerId.getPeerId() + const peerIdB58Str = peer.getPeerId() try { p = this.peerBook.get(peerIdB58Str) } catch (err) { From 9477389b5fdc5bf532d0a71ce1eb906977580423 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 16 Aug 2017 17:10:25 -0600 Subject: [PATCH 08/29] wip: tests cleaup --- src/index.js | 2 +- .../circuit/dial-over-any-relay.js | 139 +++++---- .../dial-over-specific-relay-transport.js | 214 ++++++------- .../circuit/dial-over-specific-relay.js | 289 ++++++++---------- test/nodejs-bundle/circuit/helpers/utils.js | 37 +-- test/nodejs-bundle/nodejs-bundle.js | 10 +- test/nodejs-bundle/utils.js | 2 +- 7 files changed, 314 insertions(+), 379 deletions(-) diff --git a/src/index.js b/src/index.js index 04c37031c5..04e882e878 100644 --- a/src/index.js +++ b/src/index.js @@ -44,7 +44,7 @@ class Node extends EventEmitter { this.swarm.connection.reuse() // If muxer exists, we can use Relay for listening/dialing - this.swarm.connection.enableRelayDialing(_options.relay) + this.swarm.connection.enableCircuitRelay(_options.relay) // Received incommind dial and muxer upgrade happened, // reuse this muxed connection diff --git a/test/nodejs-bundle/circuit/dial-over-any-relay.js b/test/nodejs-bundle/circuit/dial-over-any-relay.js index d1f3c08db7..6ff969f05f 100644 --- a/test/nodejs-bundle/circuit/dial-over-any-relay.js +++ b/test/nodejs-bundle/circuit/dial-over-any-relay.js @@ -13,87 +13,86 @@ chai.use(require('dirty-chai')) const expect = chai.expect -describe('test connecting over any relay', function () { - this.timeout(500000) +describe(`dial over any relay`, function () { + describe('test connecting over any relay', function () { + this.timeout(500000) - let portBase = 9010 // TODO: randomize or mock sockets - let testNodes + let portBase = 9010 // TODO: randomize or mock sockets + let testNodes - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode: { - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode: { + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true + } } } + }, + nodeA: { + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ] } }, - nodeA: { - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } + (err, nodes) => { + if (err) { + return done(err) + } - testNodes = nodes - done() - }) - } + testNodes = nodes + done() + }) + } - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode = testNodes['relayNode'] + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode = testNodes['relayNode'] - waterfall([ - (cb) => nodeA.dial(relayNode.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize + waterfall([ + (cb) => nodeA.dial(relayNode.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) }) - }) - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, () => done()) - }) + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, () => done()) + }) - it('dial to a node over a relay and write values', function (done) { - utils.dialAndReverse( - testNodes.nodeB, - testNodes.nodeA, - ['hello', 'hello1', 'hello2', 'hello3'], - (err, result) => { - expect(err).to.be.null() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) - expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) - expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) - done() - }) + it('dial to a node over a relay and write values', function (done) { + utils.dialAndReverse( + testNodes.nodeB, + testNodes.nodeA, + ['hello'], + (err, result) => { + expect(err).to.be.null() + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done() + }) + }) }) }) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js index 2e91366c27..8d0be13f17 100644 --- a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js +++ b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js @@ -18,124 +18,126 @@ const expect = chai.expect const nodeKeys = require('./fixtures/nodes') const Circuit = require('libp2p-circuit') -describe(`listen on an explicit chained relay addr`, function () { - this.timeout(500000) - - let portBase = 9030 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true +describe(`dial over specific relay and transport`, function () { + describe(`listen on an explicit chained relay addr`, function () { + this.timeout(500000) + + let portBase = 9030 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } } } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } } } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] } }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - - done() + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize }) - }) - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) - it('dial over the correct chained relay addr', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() + it('dial over the correct chained relay addr', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() - expect(relaySpy1.args.some((a) => { - return a[0] && - a[0].dstPeer && - multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() + expect(relaySpy1.args.some((a) => { + return a[0] && + a[0].dstPeer && + multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) }) }) }) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay.js b/test/nodejs-bundle/circuit/dial-over-specific-relay.js index 575d423f93..5bc67d161c 100644 --- a/test/nodejs-bundle/circuit/dial-over-specific-relay.js +++ b/test/nodejs-bundle/circuit/dial-over-specific-relay.js @@ -18,191 +18,146 @@ const expect = chai.expect const nodeKeys = require('./fixtures/nodes') const Circuit = require('libp2p-circuit') -describe(`listen on an explicit relay addr`, function () { - this.timeout(500000) - - let portBase = 9020 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - let active = false - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active +describe(`dial over specific relay`, function () { + describe(`listen on an explicit relay addr`, function () { + this.timeout(500000) + + let portBase = 9020 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + let active = false + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } } } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } } } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] } }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - - done() + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + + done() + }) + } + + // no way to tell which relay is going to be used with multidialing + describe(`passive`, function () { + beforeEach(function (done) { + waterfall([ + (cb) => setUpNodes(multiplex, cb), + (cb) => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) + ], () => setTimeout(cb, 1000)) // WS needs some time to initialize + } + ], done) }) - } - - // no way to tell which relay is going to be used with multidialing - describe(`passive`, function () { - beforeEach(function (done) { - waterfall([ - (cb) => setUpNodes(multiplex, cb), - (cb) => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) - ], () => setTimeout(cb, 1000)) // WS needs some time to initialize - } - ], done) - }) - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, done) - }) + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, done) + }) - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() - expect(relaySpy1.args.some((a) => { - return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === - `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() + expect(relaySpy1.args.some((a) => { + return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === + `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) + done(err) + }) }) - }) - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() - expect(relaySpy2.args.some((a) => { - return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === - `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` - })).to.be.ok() + expect(relaySpy2.args.some((a) => { + return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === + `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` + })).to.be.ok() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) + done(err) + }) }) }) }) - - // describe.skip(`active`, function () { - // beforeEach(function (done) { - // active = true - // setUpNodes(multiplex, () => { - // setTimeout(done, 1000) // give the nodes time to startup - // }) - // }) - // - // afterEach(function circuitTests (done) { - // relaySpy1.reset() - // relaySpy2.reset() - // utils.stopNodes(testNodes, done) - // }) - // - // it('dial over the correct relay', function (done) { - // utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - // expect(err).to.be.null() - // expect(relaySpy2.called).to.be.not.ok() - // expect(relaySpy1.called).to.be.ok() - // - // expect(relaySpy1.args[0][1].toString()) - // .to - // .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - // - // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - // - // done(err) - // }) - // }) - // - // it('dial over the correct relay and transport', function (done) { - // utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - // expect(err).to.be.null() - // expect(relaySpy1.called).to.be.not.ok() - // expect(relaySpy2.called).to.be.ok() - // - // expect(relaySpy2.args[0][1].toString()) - // .to - // .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - // - // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - // - // done(err) - // }) - // }) - // }) }) diff --git a/test/nodejs-bundle/circuit/helpers/utils.js b/test/nodejs-bundle/circuit/helpers/utils.js index de198ca90d..14f87e5735 100644 --- a/test/nodejs-bundle/circuit/helpers/utils.js +++ b/test/nodejs-bundle/circuit/helpers/utils.js @@ -8,7 +8,7 @@ const pull = require('pull-stream') exports.createNodes = function createNodes (configNodes, callback) { const nodes = {} - eachAsync(Object.keys(configNodes), (key, cb1) => { + eachAsync(Object.keys(configNodes), (key, cb) => { let config = configNodes[key] const setup = (err, peer) => { @@ -16,17 +16,12 @@ exports.createNodes = function createNodes (configNodes, callback) { callback(err) } - eachAsync(config.addrs, (addr, cb2) => { + config.addrs.forEach((addr) => { peer.multiaddrs.add(addr) - cb2() - }, (err) => { - if (err) { - return callback(err) - } - - nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config) - cb1() }) + + nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config) + cb() } if (config.id) { @@ -53,29 +48,11 @@ exports.createNodes = function createNodes (configNodes, callback) { } function startNodes (nodes, callback) { - eachAsync(Object.keys(nodes), - (key, cb) => { - nodes[key].start(cb) - }, - (err) => { - if (err) { - return callback(err) - } - callback(null) - }) + eachAsync(Object.keys(nodes), (key, cb) => nodes[key].start(cb), callback) } exports.stopNodes = function stopNodes (nodes, callback) { - eachAsync(Object.keys(nodes), - (key, cb) => { - nodes[key].stop(cb) - }, - (err) => { - if (err) { - return callback(err) - } - callback() - }) + eachAsync(Object.keys(nodes), (key, cb) => nodes[key].stop(cb), callback) } function reverse (protocol, conn) { diff --git a/test/nodejs-bundle/nodejs-bundle.js b/test/nodejs-bundle/nodejs-bundle.js index 5d67075210..34d1d7ba21 100644 --- a/test/nodejs-bundle/nodejs-bundle.js +++ b/test/nodejs-bundle/nodejs-bundle.js @@ -16,8 +16,10 @@ function mapMuxers (list) { return pref } switch (pref.trim().toLowerCase()) { - case 'spdy': return spdy - case 'multiplex': return multiplex + case 'spdy': + return spdy + case 'multiplex': + return multiplex default: throw new Error(pref + ' muxer not available') } @@ -40,13 +42,13 @@ class Node extends libp2p { options = options || {} const modules = { - transport: [ + transport: options.transports || [ new TCP(), new WS() ], connection: { muxer: getMuxers(options.muxer), - crypto: [ secio ] + crypto: [secio] }, discovery: [] } diff --git a/test/nodejs-bundle/utils.js b/test/nodejs-bundle/utils.js index 4c26b66f9f..7245f284ef 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/nodejs-bundle/utils.js @@ -20,7 +20,7 @@ function createNode (multiaddrs, options, callback) { } waterfall([ - (cb) => PeerId.create({ bits: 1024 }, cb), + (cb) => options.id ? PeerId.createFromB58String(options.id) : PeerId.create({bits: 1024}, cb), (peerId, cb) => PeerInfo.create(peerId, cb), (peerInfo, cb) => { multiaddrs.map((ma) => peerInfo.multiaddrs.add(ma)) From cc28fd70a3a3a7bcd4b5ae941811e73344dcb353 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 22 Aug 2017 14:15:15 -0600 Subject: [PATCH 09/29] test: clean up --- package.json | 2 +- src/index.js | 2 +- test/nodejs-bundle/circuit.js | 195 ++++++++++++++++++ .../circuit/dial-over-any-relay.js | 98 --------- .../dial-over-specific-relay-transport.js | 143 ------------- .../circuit/dial-over-specific-relay.js | 163 --------------- test/nodejs-bundle/circuit/fixtures/nodes.js | 25 --- .../circuit/helpers/test-node.js | 21 -- test/nodejs-bundle/circuit/helpers/utils.js | 87 -------- test/nodejs-bundle/circuit/index.js | 5 - test/nodejs-bundle/nodejs-bundle.js | 10 +- test/nodejs-bundle/utils.js | 3 +- 12 files changed, 203 insertions(+), 551 deletions(-) create mode 100644 test/nodejs-bundle/circuit.js delete mode 100644 test/nodejs-bundle/circuit/dial-over-any-relay.js delete mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js delete mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay.js delete mode 100644 test/nodejs-bundle/circuit/fixtures/nodes.js delete mode 100644 test/nodejs-bundle/circuit/helpers/test-node.js delete mode 100644 test/nodejs-bundle/circuit/helpers/utils.js delete mode 100644 test/nodejs-bundle/circuit/index.js diff --git a/package.json b/package.json index 8552082c7d..b77e4cf13c 100644 --- a/package.json +++ b/package.json @@ -49,13 +49,13 @@ "devDependencies": { "aegir": "^11.0.2", "chai": "^4.1.2", - "dirty-chai": "^2.0.1", "cids": "~0.5.1", "libp2p-kad-dht": "~0.5.0", "libp2p-mdns": "~0.9.0", "libp2p-multiplex": "~0.5.0", "libp2p-railing": "~0.7.0", "libp2p-circuit": "0.0.2", + "libp2p-railing": "~0.6.1", "libp2p-secio": "~0.8.0", "libp2p-spdy": "~0.11.0", "libp2p-tcp": "~0.11.0", diff --git a/src/index.js b/src/index.js index 04e882e878..da5ebbe21b 100644 --- a/src/index.js +++ b/src/index.js @@ -171,7 +171,7 @@ class Node extends EventEmitter { this.swarm.transport.add( transport.tag || transport.constructor.name, transport) } else if (transport.constructor && - transport.constructor.name === 'WebSockets') { + transport.constructor.name === 'WebSockets') { // TODO find a cleaner way to signal that a transport is always // used for dialing, even if no listener ws = transport diff --git a/test/nodejs-bundle/circuit.js b/test/nodejs-bundle/circuit.js new file mode 100644 index 0000000000..3728a9c69e --- /dev/null +++ b/test/nodejs-bundle/circuit.js @@ -0,0 +1,195 @@ +/* eslint-env mocha */ +'use strict' + +const pull = require('pull-stream') +const waterfall = require('async/waterfall') +const parallel = require('async/parallel') +const utils = require('./utils') +const Circuit = require('libp2p-circuit') +const multiaddr = require('multiaddr') + +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect +const sinon = require('sinon') + +describe(`circuit`, function () { + let handlerSpies = [] + let relayNode1 + let relayNode2 + let nodeWS1 + let nodeWS2 + let nodeTCP1 + let nodeTCP2 + + function setupNode (addrs, options, cb) { + if (typeof options === 'function') { + cb = options + options = {} + } + + options = options || {} + + return utils.createNode(addrs, options, (err, node) => { + expect(err).to.not.exist() + + node.handle('/echo/1.0.0', utils.echo) + node.start((err) => { + expect(err).to.not.exist() + + handlerSpies.push(sinon.spy(node + .swarm + .transports[Circuit.tag] + .listeners[0] + .hopHandler, + 'handle')) + cb(node) + }) + }) + } + + before((done) => { + waterfall([ + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9010/ws`, + `/ip4/0.0.0.0/tcp/9011` + ], { + relay: { + circuit: { + enabled: true, + active: false // passive relay + } + } + }, (node) => { + relayNode1 = node + cb() + }), + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9110/ws`, + `/ip4/0.0.0.0/tcp/9111` + ], { + relay: { + circuit: { + enabled: true, + active: true // active relay + } + } + }, (node) => { + relayNode2 = node + cb() + }), + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9210/ws` + ], (node) => { + nodeWS1 = node + cb() + }), + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9410/ws` + ], (node) => { + nodeWS2 = node + cb() + }), + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9211`, + `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit` + ], (node) => { + nodeTCP1 = node + cb() + }), + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9311`, + `/ip4/0.0.0.0/tcp/9111/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit` + ], (node) => { + nodeTCP2 = node + cb() + }) + ], (err) => { + expect(err).to.not.exist() + + waterfall([ + (cb) => nodeWS1.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeWS1.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeTCP1.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeTCP2.dial(relayNode2.peerInfo, cb) + ], done) + }) + }) + + after((done) => { + parallel([ + (cb) => relayNode1.stop(cb), + (cb) => relayNode2.stop(cb), + (cb) => nodeWS1.stop(cb), + (cb) => nodeWS2.stop(cb), + (cb) => nodeTCP1.stop(cb), + (cb) => nodeTCP2.stop(cb) + ], done) + }) + + it('should dial from WS1 to TCP1 over any R', function (done) { + nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + expect(conn).to.exist() + + pull( + pull.values(['hello']), + conn, + pull.collect((e, result) => { + expect(e).to.not.exist() + expect(result[0].toString()).to.equal('hello') + done() + }) + ) + }) + }) + + it('should dial from WS1 to TCP1 over R1', function (done) { + nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + expect(conn).to.exist() + + pull( + pull.values(['hello']), + conn, + pull.collect((e, result) => { + expect(e).to.not.exist() + expect(result[0].toString()).to.equal('hello') + + const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString() + expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`) + done() + }) + ) + }) + }) + + it(`should dial from WS1 to TCP2 over R2`, function (done) { + nodeWS1.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + expect(conn).to.exist() + + pull( + pull.values(['hello']), + conn, + pull.collect((e, result) => { + expect(e).to.not.exist() + expect(result[0].toString()).to.equal('hello') + + const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString() + expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`) + done() + }) + ) + }) + }) + + it(`should not dial - no R from WS2 to TCP1`, function (done) { + nodeWS2.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.exist() + expect(conn).to.not.exist() + done() + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/dial-over-any-relay.js b/test/nodejs-bundle/circuit/dial-over-any-relay.js deleted file mode 100644 index 6ff969f05f..0000000000 --- a/test/nodejs-bundle/circuit/dial-over-any-relay.js +++ /dev/null @@ -1,98 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -describe(`dial over any relay`, function () { - describe('test connecting over any relay', function () { - this.timeout(500000) - - let portBase = 9010 // TODO: randomize or mock sockets - let testNodes - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode: { - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true - } - } - } - }, - nodeA: { - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode = testNodes['relayNode'] - - waterfall([ - (cb) => nodeA.dial(relayNode.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, () => done()) - }) - - it('dial to a node over a relay and write values', function (done) { - utils.dialAndReverse( - testNodes.nodeB, - testNodes.nodeA, - ['hello'], - (err, result) => { - expect(err).to.be.null() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done() - }) - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js deleted file mode 100644 index 8d0be13f17..0000000000 --- a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js +++ /dev/null @@ -1,143 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') -const multiaddr = require('multiaddr') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const sinon = require('sinon') -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -const nodeKeys = require('./fixtures/nodes') -const Circuit = require('libp2p-circuit') - -describe(`dial over specific relay and transport`, function () { - describe(`listen on an explicit chained relay addr`, function () { - this.timeout(500000) - - let portBase = 9030 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct chained relay addr', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy1.args.some((a) => { - return a[0] && - a[0].dstPeer && - multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) - }) - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay.js b/test/nodejs-bundle/circuit/dial-over-specific-relay.js deleted file mode 100644 index 5bc67d161c..0000000000 --- a/test/nodejs-bundle/circuit/dial-over-specific-relay.js +++ /dev/null @@ -1,163 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') -const multiaddr = require('multiaddr') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const sinon = require('sinon') -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -const nodeKeys = require('./fixtures/nodes') -const Circuit = require('libp2p-circuit') - -describe(`dial over specific relay`, function () { - describe(`listen on an explicit relay addr`, function () { - this.timeout(500000) - - let portBase = 9020 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - let active = false - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - - done() - }) - } - - // no way to tell which relay is going to be used with multidialing - describe(`passive`, function () { - beforeEach(function (done) { - waterfall([ - (cb) => setUpNodes(multiplex, cb), - (cb) => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) - ], () => setTimeout(cb, 1000)) // WS needs some time to initialize - } - ], done) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - - expect(relaySpy1.args.some((a) => { - return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === - `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - - expect(relaySpy2.args.some((a) => { - return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === - `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/fixtures/nodes.js b/test/nodejs-bundle/circuit/fixtures/nodes.js deleted file mode 100644 index 71a274d91d..0000000000 --- a/test/nodejs-bundle/circuit/fixtures/nodes.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' - -exports.node1 = { - id: 'QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE', - privKey: 'CAASpwkwggSjAgEAAoIBAQDJwzJPar4nylKY71Mm5q2BOED8uPf1ILvIi15VwVZWqter6flnlii/RKEcBypPbFqJHHa56MvybgQgrFmHKwDjnJvq4jyOZfR+o/D/99Ft1p2FAEBjImSXAgNpK4YsbyV5r0Q1+Avcj++aWWlLu6enUrL9WGzeUkf0U5L6XwXEPRUQdEojAIQi241P1hyqXX5gKAZVGqcPtKb6p1db3fcXodkS1G6JR90TopJHCqTCECp3SB9c6LlG7KXU92sIHJBlhOEEzGkEI1pM1SWnNnW5VLEypU7P56ifzzp4QxPNiJeC+cmE5SrgR3cXP44iKOuNVRJwBpCh5oNYqECzgqJ9AgMBAAECggEBAJpCdqXHrAmKJCqv2HiGqCODGhTfax1s4IYNIJwaTOPIjUrw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CBI1TPZpDWo2hutWSPqhKBU1QyH2FLKQPWLdxuIX1cNFPtIlSl5gCxN6oiDIwh7++kxNM1G+d5XgJX6iHLlLoNv3Wn6XYX+VtYdyZRFk8gYyT2BrISbxmsrSjSOodwUUzF8TYTjsqW6ksL2x0mrRm2cMM9evULktqwU+I8l9ulASDbFWBXUToXaZSL9M+Oq5JvZO0WIjPeYVpAgWladtayhdxg5dBv8aTbDaM5DZvyRAgMBAAE=' -} diff --git a/test/nodejs-bundle/circuit/helpers/test-node.js b/test/nodejs-bundle/circuit/helpers/test-node.js deleted file mode 100644 index 6f29f201f3..0000000000 --- a/test/nodejs-bundle/circuit/helpers/test-node.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -const Libp2p = require('../../../../src') -const secio = require('libp2p-secio') - -class TestNode extends Libp2p { - constructor (peerInfo, transports, muxer, options) { - options = options || {} - const modules = { - transport: transports, - connection: { - muxer: [muxer], - crypto: options.isCrypto ? [secio] : null - }, - discovery: [] - } - super(modules, peerInfo, null, options) - } -} - -module.exports = TestNode diff --git a/test/nodejs-bundle/circuit/helpers/utils.js b/test/nodejs-bundle/circuit/helpers/utils.js deleted file mode 100644 index 14f87e5735..0000000000 --- a/test/nodejs-bundle/circuit/helpers/utils.js +++ /dev/null @@ -1,87 +0,0 @@ -'use strict' - -const TestNode = require('./test-node') -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const eachAsync = require('async/each') -const pull = require('pull-stream') - -exports.createNodes = function createNodes (configNodes, callback) { - const nodes = {} - eachAsync(Object.keys(configNodes), (key, cb) => { - let config = configNodes[key] - - const setup = (err, peer) => { - if (err) { - callback(err) - } - - config.addrs.forEach((addr) => { - peer.multiaddrs.add(addr) - }) - - nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config) - cb() - } - - if (config.id) { - PeerId.createFromJSON(config.id, (err, peerId) => { - if (err) return callback(err) - PeerInfo.create(peerId, setup) - }) - } else { - PeerInfo.create(setup) - } - }, (err) => { - if (err) { - return callback(err) - } - - startNodes(nodes, (err) => { - if (err) { - callback(err) - } - - callback(null, nodes) - }) - }) -} - -function startNodes (nodes, callback) { - eachAsync(Object.keys(nodes), (key, cb) => nodes[key].start(cb), callback) -} - -exports.stopNodes = function stopNodes (nodes, callback) { - eachAsync(Object.keys(nodes), (key, cb) => nodes[key].stop(cb), callback) -} - -function reverse (protocol, conn) { - pull( - conn, - pull.map((data) => { - return data.toString().split('').reverse().join('') - }), - conn - ) -} - -exports.dialAndReverse = function dialAndRevers (srcNode, dstNode, vals, done) { - dstNode.handle('/ipfs/reverse/1.0.0', reverse) - - srcNode.dial(dstNode.peerInfo, '/ipfs/reverse/1.0.0', (err, conn) => { - if (err) return done(err) - - pull( - pull.values(vals), - conn, - pull.collect((err, data) => { - if (err) return done(err) - - let reversed = data.map((val, i) => { - return val.toString() - }) - - srcNode.hangUp(srcNode.peerInfo, () => done(null, reversed)) - })) - }) -} diff --git a/test/nodejs-bundle/circuit/index.js b/test/nodejs-bundle/circuit/index.js deleted file mode 100644 index fcf515e667..0000000000 --- a/test/nodejs-bundle/circuit/index.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict' - -require('./dial-over-any-relay') -require('./dial-over-specific-relay') -require('./dial-over-specific-relay-transport') diff --git a/test/nodejs-bundle/nodejs-bundle.js b/test/nodejs-bundle/nodejs-bundle.js index 34d1d7ba21..5d67075210 100644 --- a/test/nodejs-bundle/nodejs-bundle.js +++ b/test/nodejs-bundle/nodejs-bundle.js @@ -16,10 +16,8 @@ function mapMuxers (list) { return pref } switch (pref.trim().toLowerCase()) { - case 'spdy': - return spdy - case 'multiplex': - return multiplex + case 'spdy': return spdy + case 'multiplex': return multiplex default: throw new Error(pref + ' muxer not available') } @@ -42,13 +40,13 @@ class Node extends libp2p { options = options || {} const modules = { - transport: options.transports || [ + transport: [ new TCP(), new WS() ], connection: { muxer: getMuxers(options.muxer), - crypto: [secio] + crypto: [ secio ] }, discovery: [] } diff --git a/test/nodejs-bundle/utils.js b/test/nodejs-bundle/utils.js index 7245f284ef..3bce092930 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/nodejs-bundle/utils.js @@ -10,6 +10,7 @@ const waterfall = require('async/waterfall') const pull = require('pull-stream') function createNode (multiaddrs, options, callback) { + options = options || {} if (typeof options === 'function') { callback = options options = {} @@ -20,7 +21,7 @@ function createNode (multiaddrs, options, callback) { } waterfall([ - (cb) => options.id ? PeerId.createFromB58String(options.id) : PeerId.create({bits: 1024}, cb), + (cb) => options.id ? PeerId.createFromB58String(options.id) : PeerId.create({ bits: 1024 }, cb), (peerId, cb) => PeerInfo.create(peerId, cb), (peerInfo, cb) => { multiaddrs.map((ma) => peerInfo.multiaddrs.add(ma)) From 0c068298585838bc7d2fcca537df5e7c3f891b96 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 23 Oct 2017 10:41:20 -0700 Subject: [PATCH 10/29] wip --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index b77e4cf13c..3f99cae455 100644 --- a/package.json +++ b/package.json @@ -54,8 +54,7 @@ "libp2p-mdns": "~0.9.0", "libp2p-multiplex": "~0.5.0", "libp2p-railing": "~0.7.0", - "libp2p-circuit": "0.0.2", - "libp2p-railing": "~0.6.1", + "libp2p-circuit": "0.1.0", "libp2p-secio": "~0.8.0", "libp2p-spdy": "~0.11.0", "libp2p-tcp": "~0.11.0", From 973e00655c15c6216453c303011015a76de061e4 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 23 Oct 2017 11:48:08 -0700 Subject: [PATCH 11/29] fix: bringing back test reworks and new aegir --- gulpfile.js => .aegir.js | 20 ++++---- package.json | 36 ++++++------- test/nodejs-bundle/circuit.js | 96 +++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 65 deletions(-) rename gulpfile.js => .aegir.js (74%) diff --git a/gulpfile.js b/.aegir.js similarity index 74% rename from gulpfile.js rename to .aegir.js index 694c65b55c..e7fff53c0e 100644 --- a/gulpfile.js +++ b/.aegir.js @@ -1,6 +1,5 @@ 'use strict' -const gulp = require('gulp') const Node = require('./test/nodejs-bundle/nodejs-bundle.js') const PeerInfo = require('peer-info') const PeerId = require('peer-id') @@ -12,7 +11,7 @@ let server let node const rawPeer = require('./test/browser-bundle/peer.json') -gulp.task('libnode:start', (done) => { +const before = (done) => { let count = 0 const ready = () => ++count === 2 ? done() : null @@ -36,20 +35,21 @@ gulp.task('libnode:start', (done) => { node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) node.start(() => ready()) }) -}) +} -gulp.task('libnode:stop', (done) => { +const after = (done) => { setTimeout(() => node.stop((err) => { if (err) { return done(err) } server.stop(done) }), 2000) -}) +} -gulp.task('test:browser:before', ['libnode:start']) -gulp.task('test:node:before', ['libnode:start']) -gulp.task('test:browser:after', ['libnode:stop']) -gulp.task('test:node:after', ['libnode:stop']) +module.exports = { + hooks: { + pre: before, + post: after + } +} -require('aegir/gulp')(gulp) diff --git a/package.json b/package.json index 3f99cae455..e8711d70f5 100644 --- a/package.json +++ b/package.json @@ -4,16 +4,16 @@ "description": "JavaScript base class for libp2p bundles", "main": "src/index.js", "scripts": { - "test": "gulp test", - "test:node": "gulp test:node", - "test:browser": "gulp test:browser --dom", - "release": "gulp release --dom", - "release-minor": "gulp release --type minor --dom", - "release-major": "gulp release --type major --dom", - "build": "gulp build", - "lint": "aegir-lint", - "coverage": "aegir-coverage", - "coverage-publish": "aegir-coverage publish" + "lint": "aegir lint", + "build": "aegir build", + "test": "aegir test --target node --target browser --no-parallel --timeout 50000", + "test:node": "aegir test --target node --no-parallel", + "test:browser": "aegir test --target browser --no-parallel", + "release": "aegir test release --target node --target browser --no-parallel", + "release-minor": "aegir release --type minor --target node --target browser --no-parallel", + "release-major": "aegir release --type major --target node --target browser --no-parallel", + "coverage": "aegir coverage", + "coverage-publish": "aegir coverage --provider coveralls" }, "repository": { "type": "git", @@ -47,19 +47,22 @@ "peer-info": "~0.11.0" }, "devDependencies": { - "aegir": "^11.0.2", + "aegir": "^12.1.0", "chai": "^4.1.2", "cids": "~0.5.1", + "dirty-chai": "^2.0.1", + "electron-webrtc": "^0.3.0", + "libp2p-circuit": "^0.1.0", "libp2p-kad-dht": "~0.5.0", "libp2p-mdns": "~0.9.0", "libp2p-multiplex": "~0.5.0", "libp2p-railing": "~0.7.0", - "libp2p-circuit": "0.1.0", "libp2p-secio": "~0.8.0", "libp2p-spdy": "~0.11.0", - "libp2p-tcp": "~0.11.0", - "libp2p-webrtc-star": "~0.13.0", - "libp2p-websockets": "~0.10.1", + "libp2p-swarm": "^0.33.0", + "libp2p-tcp": "^0.11.0", + "libp2p-webrtc-star": "^0.13.2", + "libp2p-websockets": "^0.10.4", "lodash.times": "^4.3.2", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", @@ -67,7 +70,6 @@ "pull-stream": "^3.6.0", "safe-buffer": "^5.1.1", "sinon": "^2.3.6", - "electron-webrtc": "^0.3.0", "wrtc": "0.0.62" }, "contributors": [ @@ -81,4 +83,4 @@ "greenkeeperio-bot ", "mayerwin " ] -} \ No newline at end of file +} diff --git a/test/nodejs-bundle/circuit.js b/test/nodejs-bundle/circuit.js index 3728a9c69e..a21c5e9402 100644 --- a/test/nodejs-bundle/circuit.js +++ b/test/nodejs-bundle/circuit.js @@ -38,12 +38,15 @@ describe(`circuit`, function () { node.start((err) => { expect(err).to.not.exist() - handlerSpies.push(sinon.spy(node - .swarm - .transports[Circuit.tag] - .listeners[0] - .hopHandler, - 'handle')) + if (node.swarm.transports[Circuit.tag]) { + handlerSpies.push(sinon.spy(node + .swarm + .transports[Circuit.tag] + .listeners[0] + .hopHandler, + 'handle')) + } + cb(node) }) }) @@ -56,7 +59,8 @@ describe(`circuit`, function () { `/ip4/0.0.0.0/tcp/9011` ], { relay: { - circuit: { + enabled: true, + hop: { enabled: true, active: false // passive relay } @@ -70,7 +74,8 @@ describe(`circuit`, function () { `/ip4/0.0.0.0/tcp/9111` ], { relay: { - circuit: { + enabled: true, + hop: { enabled: true, active: true // active relay } @@ -79,42 +84,57 @@ describe(`circuit`, function () { relayNode2 = node cb() }), - (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9210/ws` - ], (node) => { - nodeWS1 = node - cb() - }), - (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9410/ws` - ], (node) => { - nodeWS2 = node - cb() - }), + (cb) => setupNode([`/ip4/0.0.0.0/tcp/9210/ws`], + { + relay: { + enabled: true + } + }, + (node) => { + nodeWS1 = node + cb() + }), + (cb) => setupNode([`/ip4/0.0.0.0/tcp/9410/ws`], + { + relay: { + enabled: true + } + }, + (node) => { + nodeWS2 = node + cb() + }), (cb) => setupNode([ `/ip4/0.0.0.0/tcp/9211`, - `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit` - ], (node) => { - nodeTCP1 = node - cb() - }), + `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit`], + { + relay: { + enabled: true + } + }, + (node) => { + nodeTCP1 = node + cb() + }), (cb) => setupNode([ `/ip4/0.0.0.0/tcp/9311`, `/ip4/0.0.0.0/tcp/9111/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit` - ], (node) => { - nodeTCP2 = node - cb() - }) - ], (err) => { - expect(err).to.not.exist() + ], + (node) => { + nodeTCP2 = node + cb() + })], + (err) => { + expect(err).to.not.exist() - waterfall([ - (cb) => nodeWS1.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeWS1.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeTCP1.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeTCP2.dial(relayNode2.peerInfo, cb) - ], done) - }) + waterfall([ + (cb) => nodeWS1.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeWS1.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeTCP1.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeTCP2.dial(relayNode2.peerInfo, cb) + ], done) + } + ) }) after((done) => { From 7da79d51d29ee8c86ff90c282385c299e111956f Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 22 Aug 2017 14:52:58 -0600 Subject: [PATCH 12/29] test: group tests --- test/nodejs-bundle/utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/nodejs-bundle/utils.js b/test/nodejs-bundle/utils.js index 3bce092930..90d8542180 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/nodejs-bundle/utils.js @@ -10,7 +10,6 @@ const waterfall = require('async/waterfall') const pull = require('pull-stream') function createNode (multiaddrs, options, callback) { - options = options || {} if (typeof options === 'function') { callback = options options = {} From b35dc9f84404ff638aad168065f5a271d76cfc9d Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 22 Aug 2017 14:15:15 -0600 Subject: [PATCH 13/29] test: clean up --- test/nodejs-bundle/utils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/nodejs-bundle/utils.js b/test/nodejs-bundle/utils.js index 90d8542180..3bce092930 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/nodejs-bundle/utils.js @@ -10,6 +10,7 @@ const waterfall = require('async/waterfall') const pull = require('pull-stream') function createNode (multiaddrs, options, callback) { + options = options || {} if (typeof options === 'function') { callback = options options = {} From 9323ec6747198dd52de62d13d4ef047b95a04de9 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 1 Sep 2017 17:31:34 -0600 Subject: [PATCH 14/29] test: adjust test --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index e8711d70f5..6c54b783be 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "devDependencies": { "aegir": "^12.1.0", "chai": "^4.1.2", + "dirty-chai": "^2.0.1", "cids": "~0.5.1", "dirty-chai": "^2.0.1", "electron-webrtc": "^0.3.0", From 44252de8dc93a16da4220b2d7455d997c8c34c13 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 3 Sep 2017 16:22:23 -0600 Subject: [PATCH 15/29] fix: use getPeerId to determine if the ipfs fragment is missing --- src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index da5ebbe21b..37b1351f29 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,6 @@ const Swarm = require('libp2p-swarm') const PeerId = require('peer-id') const PeerInfo = require('peer-info') const PeerBook = require('peer-book') -const mafmt = require('mafmt') const multiaddr = require('multiaddr') exports = module.exports @@ -158,7 +157,7 @@ class Node extends EventEmitter { const maOld = [] const maNew = [] this.peerInfo.multiaddrs.forEach((ma) => { - if (!mafmt.IPFS.matches(ma)) { + if (!ma.getPeerId()) { maOld.push(ma) maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String())) } From 722bf1fbbd33d5712f720fb180a15da3eac028fc Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 9 Apr 2017 16:51:35 -0700 Subject: [PATCH 16/29] feat: adding circuit relaying --- package.json | 11 + test/nodejs-bundle/circuit/circuit.js | 444 ++++++++++++++++++ test/nodejs-bundle/circuit/fixtures/nodes.js | 25 + .../circuit/helpers/test-node.js | 22 + test/nodejs-bundle/circuit/helpers/utils.js | 110 +++++ 5 files changed, 612 insertions(+) create mode 100644 test/nodejs-bundle/circuit/circuit.js create mode 100644 test/nodejs-bundle/circuit/fixtures/nodes.js create mode 100644 test/nodejs-bundle/circuit/helpers/test-node.js create mode 100644 test/nodejs-bundle/circuit/helpers/utils.js diff --git a/package.json b/package.json index 6c54b783be..59010ab39d 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,17 @@ "sinon": "^2.3.6", "wrtc": "0.0.62" }, + "dependencies": { + "async": "^2.5.0", + "libp2p-ping": "~0.4.0", + "libp2p-swarm": "~0.29.1", + "libp2p-circuit": "~0.0.1", + "mafmt": "^2.1.8", + "multiaddr": "^2.3.0", + "peer-book": "~0.4.0", + "peer-id": "~0.8.7", + "peer-info": "~0.9.2" + }, "contributors": [ "Chris Bratlien ", "David Dias ", diff --git a/test/nodejs-bundle/circuit/circuit.js b/test/nodejs-bundle/circuit/circuit.js new file mode 100644 index 0000000000..f94aea49ed --- /dev/null +++ b/test/nodejs-bundle/circuit/circuit.js @@ -0,0 +1,444 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const spdy = require('libp2p-spdy') +const multiplex = require('libp2p-multiplex') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const sinon = require('sinon') +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +const nodeKeys = require('./fixtures/nodes') + +describe('test relay', function () { + describe('test connecting over any relay', function () { + this.timeout(500000) + + let portBase = 9010 // TODO: randomize or mock sockets + let testNodes + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode: { + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true + } + } + } + }, + nodeA: { + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + done() + }) + } + + beforeEach(function (done) { + setUpNodes(spdy, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode = testNodes['relayNode'] + + waterfall([ + (cb) => nodeA.dial(relayNode.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, () => done()) + }) + + it('dial to a node over a relay and write values', function (done) { + utils.dialAndReverse( + testNodes.nodeA, + testNodes.nodeB, + ['hello', 'hello1', 'hello2', 'hello3'], + (err, result) => { + expect(err).to.be.null() + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) + expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) + expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) + done() + }) + }) + }) + + describe('test listening on relay address', function () { + this.timeout(500000) + + describe(`listen on an explicit relay addr`, function () { + let portBase = 9020 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + let active = false + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ip4/0.0.0.0/tcp/9023/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ip4/0.0.0.0/tcp/9020/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') + relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') + + done() + }) + } + + // no way to tell which relay is going to be used with multidialing + describe(`passive`, function () { + beforeEach(function (done) { + waterfall([ + (cb) => setUpNodes(multiplex, cb), + (cb) => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) + ], () => setTimeout(cb, 1000)) // WS needs some time to initialize + } + ], done) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy2.called).to.be.not.ok() + expect(relaySpy1.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.not.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + }) + + describe.skip(`active`, function () { + beforeEach(function (done) { + active = true + setUpNodes(multiplex, () => { + setTimeout(done, 1000) // give the nodes time to startup + }) + }) + + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy2.called).to.be.not.ok() + expect(relaySpy1.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.not.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + }) + }) + + describe(`listen on an explicit chained relay addr`, function () { + let portBase = 9030 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit` + + `/ip4/0.0.0.0/tcp/9031/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') + relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') + + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct chained relay addr', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['relayNode2'].peerInfo.id.toB58String()}`)) + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) + }) + + it('dial over the correct chained relay addr and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy1.args[0][1].toString()) + .to + .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + + expect(relaySpy2.args[0][1].toString()) + .to + .equal((`/ip4/0.0.0.0/tcp/9031/ipfs/${testNodes['relayNode1'].peerInfo.id.toB58String()}`)) + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) + }) + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/fixtures/nodes.js b/test/nodejs-bundle/circuit/fixtures/nodes.js new file mode 100644 index 0000000000..71a274d91d --- /dev/null +++ b/test/nodejs-bundle/circuit/fixtures/nodes.js @@ -0,0 +1,25 @@ +'use strict' + +exports.node1 = { + id: 'QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE', + privKey: 'CAASpwkwggSjAgEAAoIBAQDJwzJPar4nylKY71Mm5q2BOED8uPf1ILvIi15VwVZWqter6flnlii/RKEcBypPbFqJHHa56MvybgQgrFmHKwDjnJvq4jyOZfR+o/D/99Ft1p2FAEBjImSXAgNpK4YsbyV5r0Q1+Avcj++aWWlLu6enUrL9WGzeUkf0U5L6XwXEPRUQdEojAIQi241P1hyqXX5gKAZVGqcPtKb6p1db3fcXodkS1G6JR90TopJHCqTCECp3SB9c6LlG7KXU92sIHJBlhOEEzGkEI1pM1SWnNnW5VLEypU7P56ifzzp4QxPNiJeC+cmE5SrgR3cXP44iKOuNVRJwBpCh5oNYqECzgqJ9AgMBAAECggEBAJpCdqXHrAmKJCqv2HiGqCODGhTfax1s4IYNIJwaTOP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pxJQdCBI1TPZpDWo2hutWSPqhKBU1QyH2FLKQPWLdxuIX1cNFPtIlSl5gCxN6oiDIwh7++kxNM1G+d5XgJX6iHLlLoNv3Wn6XYX+VtYdyZRFk8gYyT2BrISbxmsrSjSOodwUUzF8TYTjsqW6ksL2x0mrRm2cMM9evULktqwU+I8l9ulASDbFWBXUToXaZSL9M+Oq5JvZO0WIjPeYVpAgWladtayhdxg5dBv8aTbDaM5DZvyRAgMBAAE=' +} diff --git a/test/nodejs-bundle/circuit/helpers/test-node.js b/test/nodejs-bundle/circuit/helpers/test-node.js new file mode 100644 index 0000000000..f5a92e0124 --- /dev/null +++ b/test/nodejs-bundle/circuit/helpers/test-node.js @@ -0,0 +1,22 @@ +'use strict' + +const Libp2p = require('../../../../src') +const secio = require('libp2p-secio') + +class TestNode extends Libp2p { + constructor (peerInfo, transports, muxer, options) { + options = options || {} + + const modules = { + transport: transports, + connection: { + muxer: [muxer], + crypto: options.isCrypto ? [secio] : null + }, + discovery: [] + } + super(modules, peerInfo, null, options) + } +} + +module.exports = TestNode diff --git a/test/nodejs-bundle/circuit/helpers/utils.js b/test/nodejs-bundle/circuit/helpers/utils.js new file mode 100644 index 0000000000..de198ca90d --- /dev/null +++ b/test/nodejs-bundle/circuit/helpers/utils.js @@ -0,0 +1,110 @@ +'use strict' + +const TestNode = require('./test-node') +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') +const eachAsync = require('async/each') +const pull = require('pull-stream') + +exports.createNodes = function createNodes (configNodes, callback) { + const nodes = {} + eachAsync(Object.keys(configNodes), (key, cb1) => { + let config = configNodes[key] + + const setup = (err, peer) => { + if (err) { + callback(err) + } + + eachAsync(config.addrs, (addr, cb2) => { + peer.multiaddrs.add(addr) + cb2() + }, (err) => { + if (err) { + return callback(err) + } + + nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config) + cb1() + }) + } + + if (config.id) { + PeerId.createFromJSON(config.id, (err, peerId) => { + if (err) return callback(err) + PeerInfo.create(peerId, setup) + }) + } else { + PeerInfo.create(setup) + } + }, (err) => { + if (err) { + return callback(err) + } + + startNodes(nodes, (err) => { + if (err) { + callback(err) + } + + callback(null, nodes) + }) + }) +} + +function startNodes (nodes, callback) { + eachAsync(Object.keys(nodes), + (key, cb) => { + nodes[key].start(cb) + }, + (err) => { + if (err) { + return callback(err) + } + callback(null) + }) +} + +exports.stopNodes = function stopNodes (nodes, callback) { + eachAsync(Object.keys(nodes), + (key, cb) => { + nodes[key].stop(cb) + }, + (err) => { + if (err) { + return callback(err) + } + callback() + }) +} + +function reverse (protocol, conn) { + pull( + conn, + pull.map((data) => { + return data.toString().split('').reverse().join('') + }), + conn + ) +} + +exports.dialAndReverse = function dialAndRevers (srcNode, dstNode, vals, done) { + dstNode.handle('/ipfs/reverse/1.0.0', reverse) + + srcNode.dial(dstNode.peerInfo, '/ipfs/reverse/1.0.0', (err, conn) => { + if (err) return done(err) + + pull( + pull.values(vals), + conn, + pull.collect((err, data) => { + if (err) return done(err) + + let reversed = data.map((val, i) => { + return val.toString() + }) + + srcNode.hangUp(srcNode.peerInfo, () => done(null, reversed)) + })) + }) +} From f03265253966b37c4313e9f0cd4c2ac5eb52b9c7 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 2 Aug 2017 10:33:24 -0700 Subject: [PATCH 17/29] feat: circuit loading and tests --- package.json | 11 - .../circuit/dial-over-any-relay.js | 99 +++++++++ .../dial-over-specific-relay-transport.js | 141 ++++++++++++ .../circuit/dial-over-specific-relay.js | 208 ++++++++++++++++++ test/nodejs-bundle/circuit/index.js | 5 + 5 files changed, 453 insertions(+), 11 deletions(-) create mode 100644 test/nodejs-bundle/circuit/dial-over-any-relay.js create mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js create mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay.js create mode 100644 test/nodejs-bundle/circuit/index.js diff --git a/package.json b/package.json index 59010ab39d..6c54b783be 100644 --- a/package.json +++ b/package.json @@ -73,17 +73,6 @@ "sinon": "^2.3.6", "wrtc": "0.0.62" }, - "dependencies": { - "async": "^2.5.0", - "libp2p-ping": "~0.4.0", - "libp2p-swarm": "~0.29.1", - "libp2p-circuit": "~0.0.1", - "mafmt": "^2.1.8", - "multiaddr": "^2.3.0", - "peer-book": "~0.4.0", - "peer-id": "~0.8.7", - "peer-info": "~0.9.2" - }, "contributors": [ "Chris Bratlien ", "David Dias ", diff --git a/test/nodejs-bundle/circuit/dial-over-any-relay.js b/test/nodejs-bundle/circuit/dial-over-any-relay.js new file mode 100644 index 0000000000..d1f3c08db7 --- /dev/null +++ b/test/nodejs-bundle/circuit/dial-over-any-relay.js @@ -0,0 +1,99 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const multiplex = require('libp2p-multiplex') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +describe('test connecting over any relay', function () { + this.timeout(500000) + + let portBase = 9010 // TODO: randomize or mock sockets + let testNodes + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode: { + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true + } + } + } + }, + nodeA: { + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode = testNodes['relayNode'] + + waterfall([ + (cb) => nodeA.dial(relayNode.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, () => done()) + }) + + it('dial to a node over a relay and write values', function (done) { + utils.dialAndReverse( + testNodes.nodeB, + testNodes.nodeA, + ['hello', 'hello1', 'hello2', 'hello3'], + (err, result) => { + expect(err).to.be.null() + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) + expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) + expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) + done() + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js new file mode 100644 index 0000000000..2e91366c27 --- /dev/null +++ b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js @@ -0,0 +1,141 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const multiplex = require('libp2p-multiplex') +const multiaddr = require('multiaddr') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const sinon = require('sinon') +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +const nodeKeys = require('./fixtures/nodes') +const Circuit = require('libp2p-circuit') + +describe(`listen on an explicit chained relay addr`, function () { + this.timeout(500000) + + let portBase = 9030 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: true + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + + done() + }) + } + + beforeEach(function (done) { + setUpNodes(multiplex, () => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), + (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) + ], () => setTimeout(done, 1000)) // WS needs some time to initialize + }) + }) + + afterEach(function circuitTests (done) { + relaySpy1.reset() + relaySpy2.reset() + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct chained relay addr', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + expect(relaySpy1.called).to.be.ok() + expect(relaySpy2.called).to.be.ok() + + expect(relaySpy1.args.some((a) => { + return a[0] && + a[0].dstPeer && + multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + done(err) + }) + }) +}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay.js b/test/nodejs-bundle/circuit/dial-over-specific-relay.js new file mode 100644 index 0000000000..575d423f93 --- /dev/null +++ b/test/nodejs-bundle/circuit/dial-over-specific-relay.js @@ -0,0 +1,208 @@ +/* eslint-env mocha */ +'use strict' + +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const multiplex = require('libp2p-multiplex') +const multiaddr = require('multiaddr') + +const waterfall = require('async/waterfall') +const utils = require('./helpers/utils') + +const sinon = require('sinon') +const chai = require('chai') +chai.use(require('dirty-chai')) + +const expect = chai.expect + +const nodeKeys = require('./fixtures/nodes') +const Circuit = require('libp2p-circuit') + +describe(`listen on an explicit relay addr`, function () { + this.timeout(500000) + + let portBase = 9020 // TODO: randomize or mock sockets + let testNodes + + let relaySpy1 + let relaySpy2 + let active = false + + function setUpNodes (muxer, done) { + utils.createNodes( + { + relayNode1: { + id: nodeKeys.node1, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + relayNode2: { + id: nodeKeys.node2, + transports: [new TCP(), new WS()], + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/${portBase++}/ws` + ], + isCrypto: true, + config: { + relay: { + circuit: { + enabled: true, + active: active + } + } + } + }, + nodeA: { + id: nodeKeys.node3, + transports: [new TCP()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}`, + `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node2.id}/p2p-circuit` + ] + }, + nodeB: { + id: nodeKeys.node4, + transports: [new WS()], + isCrypto: true, + muxer: muxer, + addrs: [ + `/ip4/0.0.0.0/tcp/${portBase++}/ws`, + `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, + `/ipfs/${nodeKeys.node1.id}/p2p-circuit` + ] + } + }, + (err, nodes) => { + if (err) { + return done(err) + } + + testNodes = nodes + relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') + + done() + }) + } + + // no way to tell which relay is going to be used with multidialing + describe(`passive`, function () { + beforeEach(function (done) { + waterfall([ + (cb) => setUpNodes(multiplex, cb), + (cb) => { + let nodeA = testNodes['nodeA'] + let nodeB = testNodes['nodeB'] + let relayNode1 = testNodes['relayNode1'] + let relayNode2 = testNodes['relayNode2'] + + waterfall([ + (cb) => nodeA.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), + (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) + ], () => setTimeout(cb, 1000)) // WS needs some time to initialize + } + ], done) + }) + + afterEach(function circuitTests (done) { + utils.stopNodes(testNodes, done) + }) + + it('dial over the correct relay', function (done) { + utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + expect(err).to.be.null() + + expect(relaySpy1.args.some((a) => { + return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === + `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` + })).to.be.ok() + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + + it('dial over the correct relay and transport', function (done) { + utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + expect(err).to.be.null() + + expect(relaySpy2.args.some((a) => { + return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === + `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` + })).to.be.ok() + + expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + + done(err) + }) + }) + }) + + // describe.skip(`active`, function () { + // beforeEach(function (done) { + // active = true + // setUpNodes(multiplex, () => { + // setTimeout(done, 1000) // give the nodes time to startup + // }) + // }) + // + // afterEach(function circuitTests (done) { + // relaySpy1.reset() + // relaySpy2.reset() + // utils.stopNodes(testNodes, done) + // }) + // + // it('dial over the correct relay', function (done) { + // utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { + // expect(err).to.be.null() + // expect(relaySpy2.called).to.be.not.ok() + // expect(relaySpy1.called).to.be.ok() + // + // expect(relaySpy1.args[0][1].toString()) + // .to + // .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) + // + // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + // + // done(err) + // }) + // }) + // + // it('dial over the correct relay and transport', function (done) { + // utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { + // expect(err).to.be.null() + // expect(relaySpy1.called).to.be.not.ok() + // expect(relaySpy2.called).to.be.ok() + // + // expect(relaySpy2.args[0][1].toString()) + // .to + // .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) + // + // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) + // + // done(err) + // }) + // }) + // }) +}) diff --git a/test/nodejs-bundle/circuit/index.js b/test/nodejs-bundle/circuit/index.js new file mode 100644 index 0000000000..fcf515e667 --- /dev/null +++ b/test/nodejs-bundle/circuit/index.js @@ -0,0 +1,5 @@ +'use strict' + +require('./dial-over-any-relay') +require('./dial-over-specific-relay') +require('./dial-over-specific-relay-transport') From acdd40bae48c0bcb5aebfa7422412610a7312c01 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 22 Aug 2017 14:15:15 -0600 Subject: [PATCH 18/29] test: clean up --- test/nodejs-bundle/circuit/fixtures/nodes.js | 25 -------------------- test/nodejs-bundle/circuit/index.js | 5 ---- 2 files changed, 30 deletions(-) delete mode 100644 test/nodejs-bundle/circuit/fixtures/nodes.js delete mode 100644 test/nodejs-bundle/circuit/index.js diff --git a/test/nodejs-bundle/circuit/fixtures/nodes.js b/test/nodejs-bundle/circuit/fixtures/nodes.js deleted file mode 100644 index 71a274d91d..0000000000 --- a/test/nodejs-bundle/circuit/fixtures/nodes.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' - -exports.node1 = { - id: 'QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE', - privKey: 'CAASpwkwggSjAgEAAoIBAQDJwzJPar4nylKY71Mm5q2BOED8uPf1ILvIi15VwVZWqter6flnlii/RKEcBypPbFqJHHa56MvybgQgrFmHKwDjnJvq4jyOZfR+o/D/99Ft1p2FAEBjImSXAgNpK4YsbyV5r0Q1+Avcj++aWWlLu6enUrL9WGzeUkf0U5L6XwXEPRUQdEojAIQi241P1hyqXX5gKAZVGqcPtKb6p1db3fcXodkS1G6JR90TopJHCqTCECp3SB9c6LlG7KXU92sIHJBlhOEEzGkEI1pM1SWnNnW5VLEypU7P56ifzzp4QxPNiJeC+cmE5SrgR3cXP44iKOuNVRJwBpCh5oNYqECzgqJ9AgMBAAECggEBAJpCdqXHrAmKJCqv2HiGqCODGhTfax1s4IYNIJwaTOPIjUrwgfKUGSVb2H4wcEX3RyVLsO6lMcFyIg/FFlJFK9HavE8SmFAbXZqxx6I9HE+JZjf5IEFrW1Mlg+wWDejNNe7adSF6O79wATaWo+32VNGWZilTQTGd4UvJ1jc9DZCh8zZeNhm4C6exXD45gMB0HI1t2ZNl47scsBEE4rV+s7F7y8Yk/tIsf0wSI/H8KSXS5I9aFxr3Z9c3HOfbVwhnIfNUDqcFTeU5Bn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DaM5DZvyRAgMBAAE=' -} diff --git a/test/nodejs-bundle/circuit/index.js b/test/nodejs-bundle/circuit/index.js deleted file mode 100644 index fcf515e667..0000000000 --- a/test/nodejs-bundle/circuit/index.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict' - -require('./dial-over-any-relay') -require('./dial-over-specific-relay') -require('./dial-over-specific-relay-transport') From 13ea93cf89711bfd314a3996ccb9b8da82262736 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 3 Sep 2017 16:53:29 -0600 Subject: [PATCH 19/29] wip --- test/nodejs-bundle/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nodejs-bundle/utils.js b/test/nodejs-bundle/utils.js index 3bce092930..d993fc6c9b 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/nodejs-bundle/utils.js @@ -21,7 +21,7 @@ function createNode (multiaddrs, options, callback) { } waterfall([ - (cb) => options.id ? PeerId.createFromB58String(options.id) : PeerId.create({ bits: 1024 }, cb), + (cb) => PeerId.create({ bits: 1024 }, cb), (peerId, cb) => PeerInfo.create(peerId, cb), (peerInfo, cb) => { multiaddrs.map((ma) => peerInfo.multiaddrs.add(ma)) From b68c6cf840984cdaece575ffd246d20a89064634 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 17 Oct 2017 16:10:28 -0700 Subject: [PATCH 20/29] feat: upgrade to latest aegir --- examples/protocol-and-stream-muxing/3.js | 8 ++++---- test/nodejs-bundle/discovery.js | 9 ++++++--- test/nodejs-bundle/stream-muxing.js | 6 +++--- test/nodejs-bundle/tcp+websockets+webrtc-star.js | 7 ++++--- test/nodejs-bundle/tcp.js | 12 ++++++------ test/nodejs-bundle/turbolence.js | 4 ++-- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/examples/protocol-and-stream-muxing/3.js b/examples/protocol-and-stream-muxing/3.js index b9d66c666d..86c6e686f9 100644 --- a/examples/protocol-and-stream-muxing/3.js +++ b/examples/protocol-and-stream-muxing/3.js @@ -74,10 +74,10 @@ parallel([ if (err) { throw err } console.log('Addresses by which both peers are connected') node1.peerBook - .getAllArray() - .forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString())) + .getAllArray() + .forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString())) node2.peerBook - .getAllArray() - .forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString())) + .getAllArray() + .forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString())) }) }) diff --git a/test/nodejs-bundle/discovery.js b/test/nodejs-bundle/discovery.js index 0e3dc83c09..9644af9719 100644 --- a/test/nodejs-bundle/discovery.js +++ b/test/nodejs-bundle/discovery.js @@ -60,7 +60,8 @@ describe('discovery', () => { describe('MulticastDNS', () => { setup({ mdns: true }) - it('find a peer', (done) => { + it('find a peer', function (done) { + this.timeout(20000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) @@ -73,7 +74,8 @@ describe('discovery', () => { describe.skip('WebRTCStar', () => { setup({ webRTCStar: true }) - it('find a peer', (done) => { + it('find a peer', function (done) { + this.timeout(20000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) @@ -88,7 +90,8 @@ describe('discovery', () => { mdns: true }) - it('find a peer', (done) => { + it('find a peer', function (done) { + this.timeout(20000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) diff --git a/test/nodejs-bundle/stream-muxing.js b/test/nodejs-bundle/stream-muxing.js index 72304245ed..31a2c96b97 100644 --- a/test/nodejs-bundle/stream-muxing.js +++ b/test/nodejs-bundle/stream-muxing.js @@ -16,11 +16,11 @@ function test (nodeA, nodeB, callback) { expect(err).to.not.exist() pull( - pull.values([new Buffer('hey')]), + pull.values([Buffer.from('hey')]), conn, pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([new Buffer('hey')]) + expect(data).to.be.eql([Buffer.from('hey')]) callback() }) ) @@ -34,7 +34,7 @@ function teardown (nodeA, nodeB, callback) { ], callback) } -describe('stream muxing', (done) => { +describe('stream muxing', () => { it('spdy only', (done) => { let nodeA let nodeB diff --git a/test/nodejs-bundle/tcp+websockets+webrtc-star.js b/test/nodejs-bundle/tcp+websockets+webrtc-star.js index 8d5939e28f..180704e5cf 100644 --- a/test/nodejs-bundle/tcp+websockets+webrtc-star.js +++ b/test/nodejs-bundle/tcp+websockets+webrtc-star.js @@ -30,7 +30,7 @@ describe('TCP + WebSockets + WebRTCStar', () => { }) }, (cb) => { - const wstar = new WStar({wrtc: wrtc}) + const wstar = new WStar({ wrtc: wrtc }) createNode([ '/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/25011/ws', @@ -65,7 +65,7 @@ describe('TCP + WebSockets + WebRTCStar', () => { }), (cb) => { - const wstar = new WStar({wrtc: wrtc}) + const wstar = new WStar({ wrtc: wrtc }) createNode([ '/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star' @@ -194,7 +194,8 @@ describe('TCP + WebSockets + WebRTCStar', () => { }) }) - it('nodeAll.dial nodeWStar using PeerInfo', (done) => { + it('nodeAll.dial nodeWStar using PeerInfo', function (done) { + this.timeout(10000) nodeAll.dial(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() diff --git a/test/nodejs-bundle/tcp.js b/test/nodejs-bundle/tcp.js index 36fe530a39..a8b4cb4aae 100644 --- a/test/nodejs-bundle/tcp.js +++ b/test/nodejs-bundle/tcp.js @@ -70,11 +70,11 @@ describe('TCP only', () => { expect(err).to.not.exist() pull( - pull.values([new Buffer('hey')]), + pull.values([Buffer.from('hey')]), conn, pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([new Buffer('hey')]) + expect(data).to.be.eql([Buffer.from('hey')]) done() }) ) @@ -130,11 +130,11 @@ describe('TCP only', () => { } ], () => { pull( - pull.values([new Buffer('hey')]), + pull.values([Buffer.from('hey')]), conn, pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([new Buffer('hey')]) + expect(data).to.be.eql([Buffer.from('hey')]) done() }) ) @@ -193,11 +193,11 @@ describe('TCP only', () => { } ], () => { pull( - pull.values([new Buffer('hey')]), + pull.values([Buffer.from('hey')]), conn, pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([new Buffer('hey')]) + expect(data).to.be.eql([Buffer.from('hey')]) done() }) ) diff --git a/test/nodejs-bundle/turbolence.js b/test/nodejs-bundle/turbolence.js index 4455322697..527e73ef47 100644 --- a/test/nodejs-bundle/turbolence.js +++ b/test/nodejs-bundle/turbolence.js @@ -57,11 +57,11 @@ describe('Turbolence tests', () => { expect(Object.keys(peers)).to.have.length(1) pull( - pull.values([new Buffer('hey')]), + pull.values([Buffer.from('hey')]), conn, pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.eql([new Buffer('hey')]) + expect(data).to.eql([Buffer.from('hey')]) done() }) ) From 6f8b324ee0757daebba7d3f174678bacb8180f04 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 17 Oct 2017 16:27:36 -0700 Subject: [PATCH 21/29] fix: removing unused tests --- test/nodejs-bundle/circuit/circuit.js | 444 ------------------ .../circuit/dial-over-any-relay.js | 99 ---- .../dial-over-specific-relay-transport.js | 141 ------ .../circuit/dial-over-specific-relay.js | 208 -------- .../circuit/helpers/test-node.js | 22 - test/nodejs-bundle/circuit/helpers/utils.js | 110 ----- 6 files changed, 1024 deletions(-) delete mode 100644 test/nodejs-bundle/circuit/circuit.js delete mode 100644 test/nodejs-bundle/circuit/dial-over-any-relay.js delete mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js delete mode 100644 test/nodejs-bundle/circuit/dial-over-specific-relay.js delete mode 100644 test/nodejs-bundle/circuit/helpers/test-node.js delete mode 100644 test/nodejs-bundle/circuit/helpers/utils.js diff --git a/test/nodejs-bundle/circuit/circuit.js b/test/nodejs-bundle/circuit/circuit.js deleted file mode 100644 index f94aea49ed..0000000000 --- a/test/nodejs-bundle/circuit/circuit.js +++ /dev/null @@ -1,444 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const spdy = require('libp2p-spdy') -const multiplex = require('libp2p-multiplex') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const sinon = require('sinon') -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -const nodeKeys = require('./fixtures/nodes') - -describe('test relay', function () { - describe('test connecting over any relay', function () { - this.timeout(500000) - - let portBase = 9010 // TODO: randomize or mock sockets - let testNodes - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode: { - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true - } - } - } - }, - nodeA: { - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - done() - }) - } - - beforeEach(function (done) { - setUpNodes(spdy, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode = testNodes['relayNode'] - - waterfall([ - (cb) => nodeA.dial(relayNode.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, () => done()) - }) - - it('dial to a node over a relay and write values', function (done) { - utils.dialAndReverse( - testNodes.nodeA, - testNodes.nodeB, - ['hello', 'hello1', 'hello2', 'hello3'], - (err, result) => { - expect(err).to.be.null() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) - expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) - expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) - done() - }) - }) - }) - - describe('test listening on relay address', function () { - this.timeout(500000) - - describe(`listen on an explicit relay addr`, function () { - let portBase = 9020 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - let active = false - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ip4/0.0.0.0/tcp/9023/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ip4/0.0.0.0/tcp/9020/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') - relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') - - done() - }) - } - - // no way to tell which relay is going to be used with multidialing - describe(`passive`, function () { - beforeEach(function (done) { - waterfall([ - (cb) => setUpNodes(multiplex, cb), - (cb) => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) - ], () => setTimeout(cb, 1000)) // WS needs some time to initialize - } - ], done) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy2.called).to.be.not.ok() - expect(relaySpy1.called).to.be.ok() - - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.not.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - }) - - describe.skip(`active`, function () { - beforeEach(function (done) { - active = true - setUpNodes(multiplex, () => { - setTimeout(done, 1000) // give the nodes time to startup - }) - }) - - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy2.called).to.be.not.ok() - expect(relaySpy1.called).to.be.ok() - - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.not.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - }) - }) - - describe(`listen on an explicit chained relay addr`, function () { - let portBase = 9030 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9033/ws/ipfs/${nodeKeys.node2.id}/p2p-circuit` + - `/ip4/0.0.0.0/tcp/9031/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].relayCircuit, '_circuit') - relaySpy2 = sinon.spy(testNodes['relayNode2'].relayCircuit, '_circuit') - - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct chained relay addr', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['relayNode2'].peerInfo.id.toB58String()}`)) - - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) - }) - }) - - it('dial over the correct chained relay addr and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy1.args[0][1].toString()) - .to - .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - - expect(relaySpy2.args[0][1].toString()) - .to - .equal((`/ip4/0.0.0.0/tcp/9031/ipfs/${testNodes['relayNode1'].peerInfo.id.toB58String()}`)) - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) - }) - }) - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/dial-over-any-relay.js b/test/nodejs-bundle/circuit/dial-over-any-relay.js deleted file mode 100644 index d1f3c08db7..0000000000 --- a/test/nodejs-bundle/circuit/dial-over-any-relay.js +++ /dev/null @@ -1,99 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -describe('test connecting over any relay', function () { - this.timeout(500000) - - let portBase = 9010 // TODO: randomize or mock sockets - let testNodes - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode: { - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true - } - } - } - }, - nodeA: { - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode = testNodes['relayNode'] - - waterfall([ - (cb) => nodeA.dial(relayNode.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, () => done()) - }) - - it('dial to a node over a relay and write values', function (done) { - utils.dialAndReverse( - testNodes.nodeB, - testNodes.nodeA, - ['hello', 'hello1', 'hello2', 'hello3'], - (err, result) => { - expect(err).to.be.null() - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - expect(result[1]).to.equal('hello1'.split('').reverse('').join('')) - expect(result[2]).to.equal('hello2'.split('').reverse('').join('')) - expect(result[3]).to.equal('hello3'.split('').reverse('').join('')) - done() - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js b/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js deleted file mode 100644 index 2e91366c27..0000000000 --- a/test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js +++ /dev/null @@ -1,141 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') -const multiaddr = require('multiaddr') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const sinon = require('sinon') -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -const nodeKeys = require('./fixtures/nodes') -const Circuit = require('libp2p-circuit') - -describe(`listen on an explicit chained relay addr`, function () { - this.timeout(500000) - - let portBase = 9030 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: true - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - - done() - }) - } - - beforeEach(function (done) { - setUpNodes(multiplex, () => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb), - (conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb) - ], () => setTimeout(done, 1000)) // WS needs some time to initialize - }) - }) - - afterEach(function circuitTests (done) { - relaySpy1.reset() - relaySpy2.reset() - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct chained relay addr', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - expect(relaySpy1.called).to.be.ok() - expect(relaySpy2.called).to.be.ok() - - expect(relaySpy1.args.some((a) => { - return a[0] && - a[0].dstPeer && - multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - done(err) - }) - }) -}) diff --git a/test/nodejs-bundle/circuit/dial-over-specific-relay.js b/test/nodejs-bundle/circuit/dial-over-specific-relay.js deleted file mode 100644 index 575d423f93..0000000000 --- a/test/nodejs-bundle/circuit/dial-over-specific-relay.js +++ /dev/null @@ -1,208 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const TCP = require('libp2p-tcp') -const WS = require('libp2p-websockets') -const multiplex = require('libp2p-multiplex') -const multiaddr = require('multiaddr') - -const waterfall = require('async/waterfall') -const utils = require('./helpers/utils') - -const sinon = require('sinon') -const chai = require('chai') -chai.use(require('dirty-chai')) - -const expect = chai.expect - -const nodeKeys = require('./fixtures/nodes') -const Circuit = require('libp2p-circuit') - -describe(`listen on an explicit relay addr`, function () { - this.timeout(500000) - - let portBase = 9020 // TODO: randomize or mock sockets - let testNodes - - let relaySpy1 - let relaySpy2 - let active = false - - function setUpNodes (muxer, done) { - utils.createNodes( - { - relayNode1: { - id: nodeKeys.node1, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - relayNode2: { - id: nodeKeys.node2, - transports: [new TCP(), new WS()], - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/${portBase++}/ws` - ], - isCrypto: true, - config: { - relay: { - circuit: { - enabled: true, - active: active - } - } - } - }, - nodeA: { - id: nodeKeys.node3, - transports: [new TCP()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}`, - `/ip4/0.0.0.0/tcp/9022/ipfs/${nodeKeys.node2.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node2.id}/p2p-circuit` - ] - }, - nodeB: { - id: nodeKeys.node4, - transports: [new WS()], - isCrypto: true, - muxer: muxer, - addrs: [ - `/ip4/0.0.0.0/tcp/${portBase++}/ws`, - `/ip4/0.0.0.0/tcp/9021/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`, - `/ipfs/${nodeKeys.node1.id}/p2p-circuit` - ] - } - }, - (err, nodes) => { - if (err) { - return done(err) - } - - testNodes = nodes - relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle') - - done() - }) - } - - // no way to tell which relay is going to be used with multidialing - describe(`passive`, function () { - beforeEach(function (done) { - waterfall([ - (cb) => setUpNodes(multiplex, cb), - (cb) => { - let nodeA = testNodes['nodeA'] - let nodeB = testNodes['nodeB'] - let relayNode1 = testNodes['relayNode1'] - let relayNode2 = testNodes['relayNode2'] - - waterfall([ - (cb) => nodeA.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeA.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeB.dial(relayNode2.peerInfo, cb) - ], () => setTimeout(cb, 1000)) // WS needs some time to initialize - } - ], done) - }) - - afterEach(function circuitTests (done) { - utils.stopNodes(testNodes, done) - }) - - it('dial over the correct relay', function (done) { - utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - expect(err).to.be.null() - - expect(relaySpy1.args.some((a) => { - return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === - `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - - it('dial over the correct relay and transport', function (done) { - utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - expect(err).to.be.null() - - expect(relaySpy2.args.some((a) => { - return a[0].dstPeer && multiaddr(a[0].dstPeer.addrs[0]).toString() === - `/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}` - })).to.be.ok() - - expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - - done(err) - }) - }) - }) - - // describe.skip(`active`, function () { - // beforeEach(function (done) { - // active = true - // setUpNodes(multiplex, () => { - // setTimeout(done, 1000) // give the nodes time to startup - // }) - // }) - // - // afterEach(function circuitTests (done) { - // relaySpy1.reset() - // relaySpy2.reset() - // utils.stopNodes(testNodes, done) - // }) - // - // it('dial over the correct relay', function (done) { - // utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => { - // expect(err).to.be.null() - // expect(relaySpy2.called).to.be.not.ok() - // expect(relaySpy1.called).to.be.ok() - // - // expect(relaySpy1.args[0][1].toString()) - // .to - // .equal((`/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`)) - // - // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - // - // done(err) - // }) - // }) - // - // it('dial over the correct relay and transport', function (done) { - // utils.dialAndReverse(testNodes['nodeB'], testNodes['nodeA'], ['hello'], (err, result) => { - // expect(err).to.be.null() - // expect(relaySpy1.called).to.be.not.ok() - // expect(relaySpy2.called).to.be.ok() - // - // expect(relaySpy2.args[0][1].toString()) - // .to - // .equal((`/ipfs/${testNodes['nodeA'].peerInfo.id.toB58String()}`)) - // - // expect(result[0]).to.equal('hello'.split('').reverse('').join('')) - // - // done(err) - // }) - // }) - // }) -}) diff --git a/test/nodejs-bundle/circuit/helpers/test-node.js b/test/nodejs-bundle/circuit/helpers/test-node.js deleted file mode 100644 index f5a92e0124..0000000000 --- a/test/nodejs-bundle/circuit/helpers/test-node.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict' - -const Libp2p = require('../../../../src') -const secio = require('libp2p-secio') - -class TestNode extends Libp2p { - constructor (peerInfo, transports, muxer, options) { - options = options || {} - - const modules = { - transport: transports, - connection: { - muxer: [muxer], - crypto: options.isCrypto ? [secio] : null - }, - discovery: [] - } - super(modules, peerInfo, null, options) - } -} - -module.exports = TestNode diff --git a/test/nodejs-bundle/circuit/helpers/utils.js b/test/nodejs-bundle/circuit/helpers/utils.js deleted file mode 100644 index de198ca90d..0000000000 --- a/test/nodejs-bundle/circuit/helpers/utils.js +++ /dev/null @@ -1,110 +0,0 @@ -'use strict' - -const TestNode = require('./test-node') -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const eachAsync = require('async/each') -const pull = require('pull-stream') - -exports.createNodes = function createNodes (configNodes, callback) { - const nodes = {} - eachAsync(Object.keys(configNodes), (key, cb1) => { - let config = configNodes[key] - - const setup = (err, peer) => { - if (err) { - callback(err) - } - - eachAsync(config.addrs, (addr, cb2) => { - peer.multiaddrs.add(addr) - cb2() - }, (err) => { - if (err) { - return callback(err) - } - - nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config) - cb1() - }) - } - - if (config.id) { - PeerId.createFromJSON(config.id, (err, peerId) => { - if (err) return callback(err) - PeerInfo.create(peerId, setup) - }) - } else { - PeerInfo.create(setup) - } - }, (err) => { - if (err) { - return callback(err) - } - - startNodes(nodes, (err) => { - if (err) { - callback(err) - } - - callback(null, nodes) - }) - }) -} - -function startNodes (nodes, callback) { - eachAsync(Object.keys(nodes), - (key, cb) => { - nodes[key].start(cb) - }, - (err) => { - if (err) { - return callback(err) - } - callback(null) - }) -} - -exports.stopNodes = function stopNodes (nodes, callback) { - eachAsync(Object.keys(nodes), - (key, cb) => { - nodes[key].stop(cb) - }, - (err) => { - if (err) { - return callback(err) - } - callback() - }) -} - -function reverse (protocol, conn) { - pull( - conn, - pull.map((data) => { - return data.toString().split('').reverse().join('') - }), - conn - ) -} - -exports.dialAndReverse = function dialAndRevers (srcNode, dstNode, vals, done) { - dstNode.handle('/ipfs/reverse/1.0.0', reverse) - - srcNode.dial(dstNode.peerInfo, '/ipfs/reverse/1.0.0', (err, conn) => { - if (err) return done(err) - - pull( - pull.values(vals), - conn, - pull.collect((err, data) => { - if (err) return done(err) - - let reversed = data.map((val, i) => { - return val.toString() - }) - - srcNode.hangUp(srcNode.peerInfo, () => done(null, reversed)) - })) - }) -} From e3be331c4087e6c64c83b8924457c134e69095f1 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 23 Oct 2017 11:55:57 -0700 Subject: [PATCH 22/29] feat: cleanup tests --- package.json | 1 - test/nodejs-bundle/circuit.js | 223 +++++++++++++++++----------------- 2 files changed, 113 insertions(+), 111 deletions(-) diff --git a/package.json b/package.json index 6c54b783be..9187f9226d 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "chai": "^4.1.2", "dirty-chai": "^2.0.1", "cids": "~0.5.1", - "dirty-chai": "^2.0.1", "electron-webrtc": "^0.3.0", "libp2p-circuit": "^0.1.0", "libp2p-kad-dht": "~0.5.0", diff --git a/test/nodejs-bundle/circuit.js b/test/nodejs-bundle/circuit.js index a21c5e9402..8afbf973c1 100644 --- a/test/nodejs-bundle/circuit.js +++ b/test/nodejs-bundle/circuit.js @@ -3,6 +3,7 @@ const pull = require('pull-stream') const waterfall = require('async/waterfall') +const series = require('async/series') const parallel = require('async/parallel') const utils = require('./utils') const Circuit = require('libp2p-circuit') @@ -38,15 +39,7 @@ describe(`circuit`, function () { node.start((err) => { expect(err).to.not.exist() - if (node.swarm.transports[Circuit.tag]) { - handlerSpies.push(sinon.spy(node - .swarm - .transports[Circuit.tag] - .listeners[0] - .hopHandler, - 'handle')) - } - + handlerSpies.push(sinon.spy(node.swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle')) cb(node) }) }) @@ -54,6 +47,7 @@ describe(`circuit`, function () { before((done) => { waterfall([ + // set up passive relay (cb) => setupNode([ `/ip4/0.0.0.0/tcp/9010/ws`, `/ip4/0.0.0.0/tcp/9011` @@ -69,6 +63,7 @@ describe(`circuit`, function () { relayNode1 = node cb() }), + // setup active relay (cb) => setupNode([ `/ip4/0.0.0.0/tcp/9110/ws`, `/ip4/0.0.0.0/tcp/9111` @@ -77,64 +72,68 @@ describe(`circuit`, function () { enabled: true, hop: { enabled: true, - active: true // active relay + active: false // passive relay } } }, (node) => { relayNode2 = node cb() }), - (cb) => setupNode([`/ip4/0.0.0.0/tcp/9210/ws`], - { - relay: { - enabled: true - } - }, - (node) => { - nodeWS1 = node - cb() - }), - (cb) => setupNode([`/ip4/0.0.0.0/tcp/9410/ws`], - { - relay: { - enabled: true - } - }, - (node) => { - nodeWS2 = node - cb() - }), + // setup node with WS + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9210/ws` + ], { + relay: { + enabled: true + } + }, (node) => { + nodeWS1 = node + cb() + }), + // setup node with WS + (cb) => setupNode([ + `/ip4/0.0.0.0/tcp/9410/ws` + ], { + relay: { + enabled: true + } + }, (node) => { + nodeWS2 = node + cb() + }), + // set up node with TCP and listening on relay1 (cb) => setupNode([ `/ip4/0.0.0.0/tcp/9211`, - `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit`], - { - relay: { - enabled: true - } - }, - (node) => { - nodeTCP1 = node - cb() - }), + `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit` + ], { + relay: { + enabled: true + } + }, (node) => { + nodeTCP1 = node + cb() + }), + // set up node with TCP and listening on relay2 over TCP transport (cb) => setupNode([ `/ip4/0.0.0.0/tcp/9311`, `/ip4/0.0.0.0/tcp/9111/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit` - ], - (node) => { - nodeTCP2 = node - cb() - })], - (err) => { - expect(err).to.not.exist() + ], { + relay: { + enabled: true + } + }, (node) => { + nodeTCP2 = node + cb() + }) + ], (err) => { + expect(err).to.not.exist() - waterfall([ - (cb) => nodeWS1.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeWS1.dial(relayNode2.peerInfo, cb), - (conn, cb) => nodeTCP1.dial(relayNode1.peerInfo, cb), - (conn, cb) => nodeTCP2.dial(relayNode2.peerInfo, cb) - ], done) - } - ) + series([ + (cb) => nodeWS1.dial(relayNode1.peerInfo, cb), + (cb) => nodeWS1.dial(relayNode2.peerInfo, cb), + (cb) => nodeTCP1.dial(relayNode1.peerInfo, cb) + ], done) + }) }) after((done) => { @@ -148,68 +147,72 @@ describe(`circuit`, function () { ], done) }) - it('should dial from WS1 to TCP1 over any R', function (done) { - nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - expect(conn).to.exist() - - pull( - pull.values(['hello']), - conn, - pull.collect((e, result) => { - expect(e).to.not.exist() - expect(result[0].toString()).to.equal('hello') - done() - }) - ) + describe(`any relay`, function () { + it('should dial from WS1 to TCP1 over any R', function (done) { + nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + expect(conn).to.exist() + + pull( + pull.values(['hello']), + conn, + pull.collect((e, result) => { + expect(e).to.not.exist() + expect(result[0].toString()).to.equal('hello') + done() + }) + ) + }) }) - }) - it('should dial from WS1 to TCP1 over R1', function (done) { - nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - expect(conn).to.exist() - - pull( - pull.values(['hello']), - conn, - pull.collect((e, result) => { - expect(e).to.not.exist() - expect(result[0].toString()).to.equal('hello') - - const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString() - expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`) - done() - }) - ) + it(`should not dial - no R from WS2 to TCP1`, function (done) { + nodeWS2.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.exist() + expect(conn).to.not.exist() + done() + }) }) }) - it(`should dial from WS1 to TCP2 over R2`, function (done) { - nodeWS1.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - expect(conn).to.exist() - - pull( - pull.values(['hello']), - conn, - pull.collect((e, result) => { - expect(e).to.not.exist() - expect(result[0].toString()).to.equal('hello') - - const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString() - expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`) - done() - }) - ) + describe(`explicit relay`, function () { + it('should dial from WS1 to TCP1 over R1', function (done) { + nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + expect(conn).to.exist() + + pull( + pull.values(['hello']), + conn, + pull.collect((e, result) => { + expect(e).to.not.exist() + expect(result[0].toString()).to.equal('hello') + + const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString() + expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`) + done() + }) + ) + }) }) - }) - it(`should not dial - no R from WS2 to TCP1`, function (done) { - nodeWS2.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { - expect(err).to.exist() - expect(conn).to.not.exist() - done() + it(`should dial from WS1 to TCP2 over R2`, function (done) { + nodeWS1.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + expect(conn).to.exist() + + pull( + pull.values(['hello']), + conn, + pull.collect((e, result) => { + expect(e).to.not.exist() + expect(result[0].toString()).to.equal('hello') + + const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString() + expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`) + done() + }) + ) + }) }) }) }) From decf7c297191ddbfa8aa6a963cc180f48584a7cb Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 23 Oct 2017 11:58:41 -0700 Subject: [PATCH 23/29] fix: create node defautl options --- test/nodejs-bundle/utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/nodejs-bundle/utils.js b/test/nodejs-bundle/utils.js index d993fc6c9b..6a89ed7861 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/nodejs-bundle/utils.js @@ -10,12 +10,13 @@ const waterfall = require('async/waterfall') const pull = require('pull-stream') function createNode (multiaddrs, options, callback) { - options = options || {} if (typeof options === 'function') { callback = options options = {} } + options = options || {} + if (!Array.isArray(multiaddrs)) { multiaddrs = [multiaddrs] } From 7e4b8506f195b2cfd1e5a603b797f30c363ccf28 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 23 Oct 2017 12:25:44 -0700 Subject: [PATCH 24/29] chore: upgrade swarm to latest version --- package.json | 3 +-- test/nodejs-bundle/circuit.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 9187f9226d..b2f2d09b43 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "dependencies": { "async": "^2.5.0", "libp2p-ping": "~0.6.0", - "libp2p-swarm": "~0.32.0", + "libp2p-swarm": "~0.33.0", "mafmt": "^3.0.0", "multiaddr": "^3.0.0", "peer-book": "~0.5.0", @@ -59,7 +59,6 @@ "libp2p-railing": "~0.7.0", "libp2p-secio": "~0.8.0", "libp2p-spdy": "~0.11.0", - "libp2p-swarm": "^0.33.0", "libp2p-tcp": "^0.11.0", "libp2p-webrtc-star": "^0.13.2", "libp2p-websockets": "^0.10.4", diff --git a/test/nodejs-bundle/circuit.js b/test/nodejs-bundle/circuit.js index 8afbf973c1..f1fbfa6ba8 100644 --- a/test/nodejs-bundle/circuit.js +++ b/test/nodejs-bundle/circuit.js @@ -49,8 +49,8 @@ describe(`circuit`, function () { waterfall([ // set up passive relay (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9010/ws`, - `/ip4/0.0.0.0/tcp/9011` + `/ip4/0.0.0.0/tcp/0/ws`, + `/ip4/0.0.0.0/tcp/0` ], { relay: { enabled: true, @@ -65,8 +65,8 @@ describe(`circuit`, function () { }), // setup active relay (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9110/ws`, - `/ip4/0.0.0.0/tcp/9111` + `/ip4/0.0.0.0/tcp/0/ws`, + `/ip4/0.0.0.0/tcp/0` ], { relay: { enabled: true, @@ -81,7 +81,7 @@ describe(`circuit`, function () { }), // setup node with WS (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9210/ws` + `/ip4/0.0.0.0/tcp/0/ws` ], { relay: { enabled: true @@ -92,7 +92,7 @@ describe(`circuit`, function () { }), // setup node with WS (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9410/ws` + `/ip4/0.0.0.0/tcp/0/ws` ], { relay: { enabled: true @@ -103,7 +103,7 @@ describe(`circuit`, function () { }), // set up node with TCP and listening on relay1 (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9211`, + `/ip4/0.0.0.0/tcp/0`, `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit` ], { relay: { @@ -115,8 +115,8 @@ describe(`circuit`, function () { }), // set up node with TCP and listening on relay2 over TCP transport (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/9311`, - `/ip4/0.0.0.0/tcp/9111/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit` + `/ip4/0.0.0.0/tcp/0`, + `/ip4/0.0.0.0/tcp/0/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit` ], { relay: { enabled: true From c7d7ab33e9c6c746add66b3491c6643b8f0e8999 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 23 Oct 2017 23:34:22 -0700 Subject: [PATCH 25/29] fix: updated aegir and adjust timeouts --- package.json | 4 ++-- test/nodejs-bundle/circuit.js | 9 ++++++--- test/nodejs-bundle/stream-muxing.js | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index b2f2d09b43..660fef3a7d 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "libp2p", - "version": "0.12.0", + "version": "0.12.4", "description": "JavaScript base class for libp2p bundles", "main": "src/index.js", "scripts": { "lint": "aegir lint", "build": "aegir build", - "test": "aegir test --target node --target browser --no-parallel --timeout 50000", + "test": "aegir test --target node --target browser --no-parallel", "test:node": "aegir test --target node --no-parallel", "test:browser": "aegir test --target browser --no-parallel", "release": "aegir test release --target node --target browser --no-parallel", diff --git a/test/nodejs-bundle/circuit.js b/test/nodejs-bundle/circuit.js index f1fbfa6ba8..4e6caadf67 100644 --- a/test/nodejs-bundle/circuit.js +++ b/test/nodejs-bundle/circuit.js @@ -45,7 +45,9 @@ describe(`circuit`, function () { }) } - before((done) => { + before(function (done) { + this.timeout(20000) + waterfall([ // set up passive relay (cb) => setupNode([ @@ -131,8 +133,9 @@ describe(`circuit`, function () { series([ (cb) => nodeWS1.dial(relayNode1.peerInfo, cb), (cb) => nodeWS1.dial(relayNode2.peerInfo, cb), - (cb) => nodeTCP1.dial(relayNode1.peerInfo, cb) - ], done) + (cb) => nodeTCP1.dial(relayNode1.peerInfo, cb), + (cb) => nodeTCP2.dial(relayNode2.peerInfo, cb) + ], () => setTimeout(done, 10000)) }) }) diff --git a/test/nodejs-bundle/stream-muxing.js b/test/nodejs-bundle/stream-muxing.js index 31a2c96b97..3b8295375b 100644 --- a/test/nodejs-bundle/stream-muxing.js +++ b/test/nodejs-bundle/stream-muxing.js @@ -35,7 +35,9 @@ function teardown (nodeA, nodeB, callback) { } describe('stream muxing', () => { - it('spdy only', (done) => { + it('spdy only', function (done) { + this.timeout(3000) + let nodeA let nodeB @@ -99,7 +101,9 @@ describe('stream muxing', () => { ], done) }) - it('spdy + multiplex', (done) => { + it('spdy + multiplex', function (done) { + this.timeout(3000) + let nodeA let nodeB @@ -131,7 +135,9 @@ describe('stream muxing', () => { ], done) }) - it('spdy + multiplex switched order', (done) => { + it('spdy + multiplex switched order', function (done) { + this.timeout(3000) + let nodeA let nodeB @@ -163,7 +169,9 @@ describe('stream muxing', () => { ], done) }) - it('one without the other fails to establish a muxedConn', (done) => { + it('one without the other fails to establish a muxedConn', function (done) { + this.timeout(5000) + let nodeA let nodeB From dd2df805e1fd8f643220fcad8a64b83f7dafa236 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 24 Oct 2017 00:13:13 -0700 Subject: [PATCH 26/29] feat: more timeouts --- src/index.js | 4 ++-- test/nodejs-bundle/discovery.js | 6 +++--- test/nodejs-bundle/stream-muxing.js | 2 +- test/nodejs-bundle/tcp+websockets+webrtc-star.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 37b1351f29..80c2471a80 100644 --- a/src/index.js +++ b/src/index.js @@ -300,7 +300,7 @@ class Node extends EventEmitter { // PeerInfo if (PeerInfo.isPeerInfo(peer)) { p = peer - // Multiaddr instance (not string) + // Multiaddr instance (not string) } else if (multiaddr.isMultiaddr(peer)) { const peerIdB58Str = peer.getPeerId() try { @@ -309,7 +309,7 @@ class Node extends EventEmitter { p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) } p.multiaddrs.add(peer) - // PeerId + // PeerId } else if (PeerId.isPeerId(peer)) { const peerIdB58Str = peer.toB58String() try { diff --git a/test/nodejs-bundle/discovery.js b/test/nodejs-bundle/discovery.js index 9644af9719..bdaf5ad858 100644 --- a/test/nodejs-bundle/discovery.js +++ b/test/nodejs-bundle/discovery.js @@ -61,7 +61,7 @@ describe('discovery', () => { setup({ mdns: true }) it('find a peer', function (done) { - this.timeout(20000) + this.timeout(15000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) @@ -75,7 +75,7 @@ describe('discovery', () => { setup({ webRTCStar: true }) it('find a peer', function (done) { - this.timeout(20000) + this.timeout(15000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) @@ -91,7 +91,7 @@ describe('discovery', () => { }) it('find a peer', function (done) { - this.timeout(20000) + this.timeout(15000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) diff --git a/test/nodejs-bundle/stream-muxing.js b/test/nodejs-bundle/stream-muxing.js index 3b8295375b..f6b0bcf9e0 100644 --- a/test/nodejs-bundle/stream-muxing.js +++ b/test/nodejs-bundle/stream-muxing.js @@ -36,7 +36,7 @@ function teardown (nodeA, nodeB, callback) { describe('stream muxing', () => { it('spdy only', function (done) { - this.timeout(3000) + this.timeout(5000) let nodeA let nodeB diff --git a/test/nodejs-bundle/tcp+websockets+webrtc-star.js b/test/nodejs-bundle/tcp+websockets+webrtc-star.js index 180704e5cf..6bb0758799 100644 --- a/test/nodejs-bundle/tcp+websockets+webrtc-star.js +++ b/test/nodejs-bundle/tcp+websockets+webrtc-star.js @@ -30,7 +30,7 @@ describe('TCP + WebSockets + WebRTCStar', () => { }) }, (cb) => { - const wstar = new WStar({ wrtc: wrtc }) + const wstar = new WStar({wrtc: wrtc}) createNode([ '/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/25011/ws', @@ -65,7 +65,7 @@ describe('TCP + WebSockets + WebRTCStar', () => { }), (cb) => { - const wstar = new WStar({ wrtc: wrtc }) + const wstar = new WStar({wrtc: wrtc}) createNode([ '/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star' From fa9578df3b6c969cad2c6bfa771598b046b95357 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 25 Oct 2017 12:43:49 -0700 Subject: [PATCH 27/29] chore: updating deps --- package.json | 6 +++--- test/nodejs-bundle/circuit.js | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 660fef3a7d..93f1a7e809 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "async": "^2.5.0", "libp2p-ping": "~0.6.0", "libp2p-swarm": "~0.33.0", - "mafmt": "^3.0.0", + "mafmt": "^3.0.2", "multiaddr": "^3.0.0", "peer-book": "~0.5.0", "peer-id": "~0.10.0", @@ -49,10 +49,10 @@ "devDependencies": { "aegir": "^12.1.0", "chai": "^4.1.2", - "dirty-chai": "^2.0.1", "cids": "~0.5.1", + "dirty-chai": "^2.0.1", "electron-webrtc": "^0.3.0", - "libp2p-circuit": "^0.1.0", + "libp2p-circuit": "^0.1.2", "libp2p-kad-dht": "~0.5.0", "libp2p-mdns": "~0.9.0", "libp2p-multiplex": "~0.5.0", diff --git a/test/nodejs-bundle/circuit.js b/test/nodejs-bundle/circuit.js index 4e6caadf67..c90ed91f36 100644 --- a/test/nodejs-bundle/circuit.js +++ b/test/nodejs-bundle/circuit.js @@ -135,7 +135,7 @@ describe(`circuit`, function () { (cb) => nodeWS1.dial(relayNode2.peerInfo, cb), (cb) => nodeTCP1.dial(relayNode1.peerInfo, cb), (cb) => nodeTCP2.dial(relayNode2.peerInfo, cb) - ], () => setTimeout(done, 10000)) + ], done) }) }) @@ -151,6 +151,7 @@ describe(`circuit`, function () { }) describe(`any relay`, function () { + this.timeout(20000) it('should dial from WS1 to TCP1 over any R', function (done) { nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() @@ -178,6 +179,7 @@ describe(`circuit`, function () { }) describe(`explicit relay`, function () { + this.timeout(20000) it('should dial from WS1 to TCP1 over R1', function (done) { nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() From 1830571f137b1ac06903e297f641e76caef28998 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 25 Oct 2017 12:52:59 -0700 Subject: [PATCH 28/29] fix: circle ci builds --- circle.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 56f7efbe2d..4e1698a60f 100644 --- a/circle.yml +++ b/circle.yml @@ -6,9 +6,13 @@ dependencies: pre: - google-chrome --version - curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb + - for v in $(curl http://archive.ubuntu.com/ubuntu/pool/main/n/nss/ | grep "href=" | grep "libnss3.*deb\"" -o | grep -o "libnss3.*deb" | grep "3.28" | grep "14.04"); do curl -L -o $v http://archive.ubuntu.com/ubuntu/pool/main/n/nss/$v; done && rm libnss3-tools*_i386.deb libnss3-dev*_i386.deb - sudo dpkg -i google-chrome.deb || true + - sudo dpkg -i libnss3*.deb || true - sudo apt-get update + - sudo apt-get install -f || true + - sudo dpkg -i libnss3*.deb - sudo apt-get install -f - sudo apt-get install --only-upgrade lsb-base - sudo dpkg -i google-chrome.deb - - google-chrome --version + - google-chrome --version \ No newline at end of file From e3b04a411e9378e32840cc8b4c12664f11ec0a9a Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 25 Oct 2017 12:56:30 -0700 Subject: [PATCH 29/29] test: timeouts --- test/nodejs-bundle/content-routing.js | 3 ++- test/nodejs-bundle/peer-routing.js | 3 ++- test/nodejs-bundle/stream-muxing.js | 4 ++-- test/nodejs-bundle/tcp+websockets+webrtc-star.js | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/nodejs-bundle/content-routing.js b/test/nodejs-bundle/content-routing.js index 6a46a2aaa1..bc47148e50 100644 --- a/test/nodejs-bundle/content-routing.js +++ b/test/nodejs-bundle/content-routing.js @@ -19,7 +19,8 @@ describe('.contentRouting', () => { let nodeD let nodeE - before((done) => { + before(function (done) { + this.timeout(5000) const tasks = _times(5, () => (cb) => { createNode('/ip4/0.0.0.0/tcp/0', { mdns: false, diff --git a/test/nodejs-bundle/peer-routing.js b/test/nodejs-bundle/peer-routing.js index 03243102e8..fb2e5611bd 100644 --- a/test/nodejs-bundle/peer-routing.js +++ b/test/nodejs-bundle/peer-routing.js @@ -18,7 +18,8 @@ describe('.peerRouting', () => { let nodeD let nodeE - before((done) => { + before(function (done) { + this.timeout(5000) const tasks = _times(5, () => (cb) => { createNode('/ip4/0.0.0.0/tcp/0', { mdns: false, diff --git a/test/nodejs-bundle/stream-muxing.js b/test/nodejs-bundle/stream-muxing.js index f6b0bcf9e0..bf9eb5aeb4 100644 --- a/test/nodejs-bundle/stream-muxing.js +++ b/test/nodejs-bundle/stream-muxing.js @@ -102,7 +102,7 @@ describe('stream muxing', () => { }) it('spdy + multiplex', function (done) { - this.timeout(3000) + this.timeout(5000) let nodeA let nodeB @@ -136,7 +136,7 @@ describe('stream muxing', () => { }) it('spdy + multiplex switched order', function (done) { - this.timeout(3000) + this.timeout(5000) let nodeA let nodeB diff --git a/test/nodejs-bundle/tcp+websockets+webrtc-star.js b/test/nodejs-bundle/tcp+websockets+webrtc-star.js index 6bb0758799..e4b1e183f5 100644 --- a/test/nodejs-bundle/tcp+websockets+webrtc-star.js +++ b/test/nodejs-bundle/tcp+websockets+webrtc-star.js @@ -20,7 +20,8 @@ describe('TCP + WebSockets + WebRTCStar', () => { let ss - before((done) => { + before(function (done) { + this.timeout(5000) parallel([ (cb) => { signalling.start({ port: 24642 }, (err, server) => {