From 643ec86ca3b6685e807c5c9e019ff9fda31ee4ab Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Mon, 20 Nov 2017 17:01:04 +0000 Subject: [PATCH 01/11] removing multiaddresses for which we have no transport for from peerinfo. Fixes js-ipfs/issues/213 --- src/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/index.js b/src/index.js index 80c2471a80..7a818ba357 100644 --- a/src/index.js +++ b/src/index.js @@ -177,6 +177,13 @@ class Node extends EventEmitter { } }) + // detect which multiaddrs we don't have a transport for and remove them + multiaddrs.forEach((multiaddr) => { + if (!transports.find((transport) => transport.filter(multiaddr).length > 0)) { + this.peerInfo.multiaddrs.delete(multiaddr) + } + }) + series([ (cb) => this.swarm.listen(cb), (cb) => { From a01ce1aeac98eb16765bbe8c1bc2183d49a685f0 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Mon, 20 Nov 2017 17:40:50 +0000 Subject: [PATCH 02/11] keeping the circuit addresses --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7a818ba357..94a243d67b 100644 --- a/src/index.js +++ b/src/index.js @@ -179,7 +179,8 @@ class Node extends EventEmitter { // detect which multiaddrs we don't have a transport for and remove them multiaddrs.forEach((multiaddr) => { - if (!transports.find((transport) => transport.filter(multiaddr).length > 0)) { + if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) && + !transports.find((transport) => transport.filter(multiaddr).length > 0)) { this.peerInfo.multiaddrs.delete(multiaddr) } }) From 585f13001a23143998a827e79cdd98aa5b0d404e Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Tue, 21 Nov 2017 08:44:39 +0000 Subject: [PATCH 03/11] multiaddr trim test --- test/multiaddr-trim.js | 32 ++++++++++++++++++++++++++++ test/node.js | 1 + test/nodejs-bundle/multiaddr-trim.js | 32 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test/multiaddr-trim.js create mode 100644 test/nodejs-bundle/multiaddr-trim.js diff --git a/test/multiaddr-trim.js b/test/multiaddr-trim.js new file mode 100644 index 0000000000..8744fccdde --- /dev/null +++ b/test/multiaddr-trim.js @@ -0,0 +1,32 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const createNode = require('./utils/node').createNode + +describe('multiaddr trim', () => { + let node + it('can create a test node with an irrelevant multiaddr', (done) => { + createNode( + [ + '/ip4/0.0.0.0/tcp/0/p2p-webrtc-direct' + ], + (err, _node) => { + expect(err).to.not.exist() + node = _node + expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) + done() + }) + }) + + it('starts node', (done) => { + node.start(done) + }) + + it('irrelevant multiaddr got trimmed', (done) => { + expect(node.peerInfo.multiaddrs.toArray()).to.have.length(0) + done() + }) +}) diff --git a/test/node.js b/test/node.js index 6bde025de1..ede2d7f8c4 100644 --- a/test/node.js +++ b/test/node.js @@ -7,3 +7,4 @@ require('./peer-discovery.node') require('./peer-routing.node') require('./content-routing.node') require('./circuit-relay.node') +require('./multiaddr-trim') diff --git a/test/nodejs-bundle/multiaddr-trim.js b/test/nodejs-bundle/multiaddr-trim.js new file mode 100644 index 0000000000..2dbb0bbba0 --- /dev/null +++ b/test/nodejs-bundle/multiaddr-trim.js @@ -0,0 +1,32 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const createNode = require('./utils').createNode + +describe('multiaddr trim', () => { + let node + it('can create a test node with an irrelevant multiaddr', (done) => { + createNode( + [ + '/ip4/0.0.0.0/tcp/0/p2p-webrtc-direct' + ], + (err, _node) => { + expect(err).to.not.exist() + node = _node + expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) + done() + }) + }) + + it('starts node', (done) => { + node.start(done) + }) + + it('irrelevant multiaddr got trimmed', (done) => { + expect(node.peerInfo.multiaddrs.toArray()).to.have.length(0) + done() + }) +}) From 1ce506446d7d105c727b001762c2a801b2afa359 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Tue, 21 Nov 2017 10:13:32 +0000 Subject: [PATCH 04/11] using a protocol that IPFS actually uses --- test/nodejs-bundle/multiaddr-trim.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/nodejs-bundle/multiaddr-trim.js b/test/nodejs-bundle/multiaddr-trim.js index 2dbb0bbba0..46cd2d855e 100644 --- a/test/nodejs-bundle/multiaddr-trim.js +++ b/test/nodejs-bundle/multiaddr-trim.js @@ -6,12 +6,12 @@ chai.use(require('dirty-chai')) const expect = chai.expect const createNode = require('./utils').createNode -describe('multiaddr trim', () => { +describe.only('multiaddr trim', () => { let node it('can create a test node with an irrelevant multiaddr', (done) => { createNode( [ - '/ip4/0.0.0.0/tcp/0/p2p-webrtc-direct' + '/ip4/0.0.0.0/tcp/0/p2p-webrtc-star' ], (err, _node) => { expect(err).to.not.exist() From ebd8c45cebe827fd0671e075edebead500959942 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Mon, 27 Nov 2017 15:36:23 +0000 Subject: [PATCH 05/11] detecting innactive multiaddresses after starting --- src/index.js | 25 +++++++++++++++---------- test/nodejs-bundle/multiaddr-trim.js | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 94a243d67b..8c3d225625 100644 --- a/src/index.js +++ b/src/index.js @@ -156,15 +156,15 @@ class Node extends EventEmitter { // so that we can have webrtc-star addrs without adding manually the id const maOld = [] const maNew = [] - this.peerInfo.multiaddrs.forEach((ma) => { + this.peerInfo.multiaddrs.toArray().forEach((ma) => { if (!ma.getPeerId()) { maOld.push(ma) maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String())) } }) this.peerInfo.multiaddrs.replace(maOld, maNew) - const multiaddrs = this.peerInfo.multiaddrs.toArray() + const multiaddrs = this.peerInfo.multiaddrs.toArray() transports.forEach((transport) => { if (transport.filter(multiaddrs).length > 0) { this.swarm.transport.add( @@ -177,14 +177,6 @@ class Node extends EventEmitter { } }) - // detect which multiaddrs we don't have a transport for and remove them - multiaddrs.forEach((multiaddr) => { - if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) && - !transports.find((transport) => transport.filter(multiaddr).length > 0)) { - this.peerInfo.multiaddrs.delete(multiaddr) - } - }) - series([ (cb) => this.swarm.listen(cb), (cb) => { @@ -208,6 +200,19 @@ class Node extends EventEmitter { } cb() }, + (cb) => { + // detect which multiaddrs we don't have a transport for and remove them + const multiaddrs = this.peerInfo.multiaddrs.toArray() + transports.forEach((transport) => { + multiaddrs.forEach((multiaddr) => { + if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) && + !transports.find((transport) => transport.filter(multiaddr).length > 0)) { + this.peerInfo.multiaddrs.delete(multiaddr) + } + }) + }) + cb() + }, (cb) => { this.emit('start') cb() diff --git a/test/nodejs-bundle/multiaddr-trim.js b/test/nodejs-bundle/multiaddr-trim.js index 46cd2d855e..996982b3be 100644 --- a/test/nodejs-bundle/multiaddr-trim.js +++ b/test/nodejs-bundle/multiaddr-trim.js @@ -6,7 +6,7 @@ chai.use(require('dirty-chai')) const expect = chai.expect const createNode = require('./utils').createNode -describe.only('multiaddr trim', () => { +describe('multiaddr trim', () => { let node it('can create a test node with an irrelevant multiaddr', (done) => { createNode( From 17206dd3eabbbf29cfcde3761684aaf413e35aca Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Mon, 27 Nov 2017 15:47:52 +0000 Subject: [PATCH 06/11] making linter happy --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8c3d225625..2cd1121c7a 100644 --- a/src/index.js +++ b/src/index.js @@ -207,7 +207,7 @@ class Node extends EventEmitter { multiaddrs.forEach((multiaddr) => { if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) && !transports.find((transport) => transport.filter(multiaddr).length > 0)) { - this.peerInfo.multiaddrs.delete(multiaddr) + this.peerInfo.multiaddrs.delete(multiaddr) } }) }) From 42940bcc98263fa61343c33f0444da6833fe4db2 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 1 Dec 2017 10:20:48 +0000 Subject: [PATCH 07/11] tests: testing multiaddress trim leaves relevant addresses untouched --- test/multiaddr-trim.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/multiaddr-trim.js b/test/multiaddr-trim.js index 8744fccdde..8ad873455b 100644 --- a/test/multiaddr-trim.js +++ b/test/multiaddr-trim.js @@ -8,15 +8,25 @@ const createNode = require('./utils/node').createNode describe('multiaddr trim', () => { let node + + after((done) => { + if (node) { + node.stop(done) + } else { + done() + } + }) + it('can create a test node with an irrelevant multiaddr', (done) => { createNode( [ - '/ip4/0.0.0.0/tcp/0/p2p-webrtc-direct' + '/ip4/0.0.0.0/tcp/0/wss/p2p-webrtc-direct', + '/ip4/0.0.0.0/tcp/0' ], (err, _node) => { expect(err).to.not.exist() node = _node - expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) + expect(node.peerInfo.multiaddrs.toArray()).to.have.length(2) done() }) }) @@ -26,7 +36,8 @@ describe('multiaddr trim', () => { }) it('irrelevant multiaddr got trimmed', (done) => { - expect(node.peerInfo.multiaddrs.toArray()).to.have.length(0) + expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) + expect(node.peerInfo.multiaddrs.toArray()[0].toString()).to.match(/^\/ip4\/0.0.0.0\/tcp\/0\/ipfs\/\w+/) done() }) }) From 078fc42ae23f32a278316477f34f48ab31c99aa1 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 1 Dec 2017 11:01:35 +0000 Subject: [PATCH 08/11] tests fix: escaping resulting address match --- test/multiaddr-trim.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/multiaddr-trim.js b/test/multiaddr-trim.js index 8ad873455b..44ecb5ccd3 100644 --- a/test/multiaddr-trim.js +++ b/test/multiaddr-trim.js @@ -37,7 +37,7 @@ describe('multiaddr trim', () => { it('irrelevant multiaddr got trimmed', (done) => { expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) - expect(node.peerInfo.multiaddrs.toArray()[0].toString()).to.match(/^\/ip4\/0.0.0.0\/tcp\/0\/ipfs\/\w+/) + expect(node.peerInfo.multiaddrs.toArray()[0].toString()).to.match(/^\/ip4\/0\.0\.0\.0\/tcp\/0\/ipfs\/\w+/) done() }) }) From 5c5e8d42ce483432dbbc85c4ee2744341bdc24f4 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 1 Dec 2017 17:13:11 +0000 Subject: [PATCH 09/11] test fix: avoiding conflating addresses in testing multiaddr trimming --- test/multiaddr-trim.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/multiaddr-trim.js b/test/multiaddr-trim.js index 44ecb5ccd3..0869e2521a 100644 --- a/test/multiaddr-trim.js +++ b/test/multiaddr-trim.js @@ -20,8 +20,8 @@ describe('multiaddr trim', () => { it('can create a test node with an irrelevant multiaddr', (done) => { createNode( [ - '/ip4/0.0.0.0/tcp/0/wss/p2p-webrtc-direct', - '/ip4/0.0.0.0/tcp/0' + '/ip4/0.0.0.0/tcp/999/wss/p2p-webrtc-direct', + '/ip4/127.0.0.1/tcp/0' ], (err, _node) => { expect(err).to.not.exist() @@ -37,7 +37,7 @@ describe('multiaddr trim', () => { it('irrelevant multiaddr got trimmed', (done) => { expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) - expect(node.peerInfo.multiaddrs.toArray()[0].toString()).to.match(/^\/ip4\/0\.0\.0\.0\/tcp\/0\/ipfs\/\w+/) + expect(node.peerInfo.multiaddrs.toArray()[0].toString()).to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ipfs\/\w+$/) done() }) }) From 94160fe642e50e1d69012fb87d204f9c6dbe9207 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 14 Dec 2017 07:01:43 +0000 Subject: [PATCH 10/11] refactor test, add one more challenge --- test/multiaddr-trim.js | 49 ++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/test/multiaddr-trim.js b/test/multiaddr-trim.js index 0869e2521a..9dc93715b2 100644 --- a/test/multiaddr-trim.js +++ b/test/multiaddr-trim.js @@ -4,40 +4,37 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect +const series = require('async/series') const createNode = require('./utils/node').createNode describe('multiaddr trim', () => { - let node + it('non used multiaddrs get trimmed', (done) => { + let node - after((done) => { - if (node) { - node.stop(done) - } else { - done() - } - }) - - it('can create a test node with an irrelevant multiaddr', (done) => { - createNode( - [ + series([ + (cb) => createNode([ '/ip4/0.0.0.0/tcp/999/wss/p2p-webrtc-direct', - '/ip4/127.0.0.1/tcp/0' - ], - (err, _node) => { + '/ip4/127.0.0.1/tcp/55555/ws', + '/ip4/0.0.0.0/tcp/0/' + ], (err, _node) => { expect(err).to.not.exist() node = _node - expect(node.peerInfo.multiaddrs.toArray()).to.have.length(2) - done() - }) - }) + const multiaddrs = node.peerInfo.multiaddrs.toArray() + // multiaddrs.forEach((ma) => console.log(ma.toString())) + expect(multiaddrs).to.have.length(3) + cb() + }), + (cb) => node.start(cb) + ], (err) => { + expect(err).to.not.exist() - it('starts node', (done) => { - node.start(done) - }) + const multiaddrs = node.peerInfo.multiaddrs.toArray() + // console.log('--') + // multiaddrs.forEach((ma) => console.log(ma.toString())) - it('irrelevant multiaddr got trimmed', (done) => { - expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) - expect(node.peerInfo.multiaddrs.toArray()[0].toString()).to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ipfs\/\w+$/) - done() + expect(multiaddrs.length).to.at.least(2) + expect(multiaddrs[0].toString()).to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ws\/ipfs\/\w+$/) + node.stop(done) + }) }) }) From 1ff8370c080d2c107862caf9b5a986a782168fa0 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 14 Dec 2017 07:19:18 +0000 Subject: [PATCH 11/11] remove old file --- test/nodejs-bundle/multiaddr-trim.js | 32 ---------------------------- 1 file changed, 32 deletions(-) delete mode 100644 test/nodejs-bundle/multiaddr-trim.js diff --git a/test/nodejs-bundle/multiaddr-trim.js b/test/nodejs-bundle/multiaddr-trim.js deleted file mode 100644 index 996982b3be..0000000000 --- a/test/nodejs-bundle/multiaddr-trim.js +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const createNode = require('./utils').createNode - -describe('multiaddr trim', () => { - let node - it('can create a test node with an irrelevant multiaddr', (done) => { - createNode( - [ - '/ip4/0.0.0.0/tcp/0/p2p-webrtc-star' - ], - (err, _node) => { - expect(err).to.not.exist() - node = _node - expect(node.peerInfo.multiaddrs.toArray()).to.have.length(1) - done() - }) - }) - - it('starts node', (done) => { - node.start(done) - }) - - it('irrelevant multiaddr got trimmed', (done) => { - expect(node.peerInfo.multiaddrs.toArray()).to.have.length(0) - done() - }) -})