From b914ec73e50f4bdabbb92c04aa4ebca7fa2d69a8 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 08:17:12 +0100 Subject: [PATCH 1/6] feat: connectedMultiaddr --- package.json | 2 +- src/index.js | 135 +++++++++++++++++++++++---------------------- test/index.spec.js | 6 +- 3 files changed, 72 insertions(+), 71 deletions(-) diff --git a/package.json b/package.json index a2d4b02..8c2e0f3 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "pre-commit": "^1.2.2" }, "dependencies": { - "multiaddr": "^2.2.3", + "multiaddr": "^2.3.0", "lodash.uniqby": "^4.7.0", "peer-id": "^0.8.5" }, diff --git a/src/index.js b/src/index.js index df63b72..7c8c2d5 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,7 @@ const Id = require('peer-id') const multiaddr = require('multiaddr') const uniqBy = require('lodash.uniqby') - -exports = module.exports = PeerInfo +const assert = require('assert') function ensureMultiaddr (addr) { if (multiaddr.isMultiaddr(addr)) { @@ -15,85 +14,85 @@ function ensureMultiaddr (addr) { } // Peer represents a peer on the IPFS network -function PeerInfo (peerId) { - if (!(this instanceof PeerInfo)) { - return new PeerInfo(peerId) - } - - if (!peerId) { - throw new Error('Missing peerId. Use Peer.create(cb) to create one') - } - - this.id = peerId - - this.multiaddrs = [] - const observedMultiaddrs = [] - - this.multiaddr = {} - - this.multiaddr.add = (addr) => { - addr = ensureMultiaddr(addr) +class PeerInfo { + constructor (peerId) { + assert(peerId, 'Missing peerId. Use Peer.create(cb) to create one') + + this.id = peerId + this.multiaddrs = [] + this._observedMultiaddrs = [] + this._connectedMultiaddr = undefined + + this.multiaddr = {} + + this.multiaddr.add = (addr) => { + addr = ensureMultiaddr(addr) + + var exists = false + this.multiaddrs.some((m, i) => { + if (m.equals(addr)) { + exists = true + return true + } + }) + if (!exists) { + this.multiaddrs.push(addr) + } + } - var exists = false - this.multiaddrs.some((m, i) => { - if (m.equals(addr)) { - exists = true - return true + // to prevent multiaddr explosion due to Identify + this.multiaddr.addSafe = (addr) => { + addr = ensureMultiaddr(addr) + + let check = false + this._observedMultiaddrs.some((m, i) => { + if (m.equals(addr)) { + this.multiaddr.add(addr) + this._observedMultiaddrs.splice(i, 1) + check = true + } + }) + if (!check) { + this._observedMultiaddrs.push(addr) } - }) - if (!exists) { - this.multiaddrs.push(addr) } - } - // to prevent multiaddr explosion - this.multiaddr.addSafe = (addr) => { - addr = ensureMultiaddr(addr) + this.multiaddr.rm = (addr) => { + addr = ensureMultiaddr(addr) + + this.multiaddrs.some((m, i) => { + if (m.equals(addr)) { + this.multiaddrs.splice(i, 1) + return true + } + }) + } - var check = false - observedMultiaddrs.some((m, i) => { - if (m.equals(addr)) { - this.multiaddr.add(addr) - observedMultiaddrs.splice(i, 1) - check = true + this.multiaddr.replace = (existing, fresh) => { + if (!Array.isArray(existing)) { + existing = [existing] } - }) - if (!check) { - observedMultiaddrs.push(addr) + if (!Array.isArray(fresh)) { + fresh = [fresh] + } + existing.forEach((m) => this.multiaddr.rm(m)) + fresh.forEach((m) => this.multiaddr.add(m)) } } - this.multiaddr.rm = (addr) => { - addr = ensureMultiaddr(addr) - - this.multiaddrs.some((m, i) => { - if (m.equals(addr)) { - this.multiaddrs.splice(i, 1) - return true - } + distinctMultiaddr () { + return uniqBy(this.multiaddrs, (item) => { + return [item.toOptions().port, item.toOptions().transport].join() }) } - this.multiaddr.replace = (existing, fresh) => { - if (!Array.isArray(existing)) { - existing = [existing] - } - if (!Array.isArray(fresh)) { - fresh = [fresh] - } - existing.forEach((m) => { - this.multiaddr.rm(m) - }) - fresh.forEach((m) => { - this.multiaddr.add(m) - }) + setConnectedMultiaddr (ma) { + ma = ensureMultiaddr(ma) + this._connectedMultiaddr = ma } - this.distinctMultiaddr = () => { - var result = uniqBy(this.multiaddrs, function (item) { - return [item.toOptions().port, item.toOptions().transport].join() - }) - return result + isConnected () { + return Boolean(this._connectedMultiaddr) } } @@ -120,3 +119,5 @@ PeerInfo.isPeerInfo = (peerInfo) => { peerInfo.id && peerInfo.multiaddrs) } + +module.exports = PeerInfo diff --git a/test/index.spec.js b/test/index.spec.js index 127d450..ad33224 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -26,12 +26,12 @@ describe('peer-info', () => { PeerId.create((err, id) => { expect(err).to.not.exist() const pi = new PeerInfo(id) - const pi2 = PeerInfo(id) + const pi2 = new PeerInfo(id) expect(pi.id).to.exist() - expect(pi.id).to.deep.equal(id) + expect(pi.id).to.eql(id) expect(pi2).to.exist() expect(pi2.id).to.exist() - expect(pi2.id).to.deep.equal(id) + expect(pi2.id).to.eql(id) done() }) }) From 7bd5c984abe0ed08505a7475a1922564905b9638 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 09:29:00 +0100 Subject: [PATCH 2/6] new api proposal --- src/index.js | 98 +++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/src/index.js b/src/index.js index 7c8c2d5..2cc2e66 100644 --- a/src/index.js +++ b/src/index.js @@ -20,77 +20,81 @@ class PeerInfo { this.id = peerId this.multiaddrs = [] + this.protocols = [] this._observedMultiaddrs = [] this._connectedMultiaddr = undefined + } - this.multiaddr = {} - - this.multiaddr.add = (addr) => { - addr = ensureMultiaddr(addr) + multiaddrAdd (addr) { + addr = ensureMultiaddr(addr) - var exists = false - this.multiaddrs.some((m, i) => { - if (m.equals(addr)) { - exists = true - return true - } - }) - if (!exists) { - this.multiaddrs.push(addr) + var exists = false + this.multiaddrs.some((m, i) => { + if (m.equals(addr)) { + exists = true + return true } + }) + if (!exists) { + this.multiaddrs.push(addr) } + } + + // to prevent multiaddr explosion due to Identify + multiaddrAddSafe (addr) { + addr = ensureMultiaddr(addr) - // to prevent multiaddr explosion due to Identify - this.multiaddr.addSafe = (addr) => { - addr = ensureMultiaddr(addr) - - let check = false - this._observedMultiaddrs.some((m, i) => { - if (m.equals(addr)) { - this.multiaddr.add(addr) - this._observedMultiaddrs.splice(i, 1) - check = true - } - }) - if (!check) { - this._observedMultiaddrs.push(addr) + let check = false + this._observedMultiaddrs.some((m, i) => { + if (m.equals(addr)) { + this.multiaddr.add(addr) + this._observedMultiaddrs.splice(i, 1) + check = true } + }) + if (!check) { + this._observedMultiaddrs.push(addr) } + } - this.multiaddr.rm = (addr) => { - addr = ensureMultiaddr(addr) - - this.multiaddrs.some((m, i) => { - if (m.equals(addr)) { - this.multiaddrs.splice(i, 1) - return true - } - }) - } + multiaddrRemove (addr) { + addr = ensureMultiaddr(addr) - this.multiaddr.replace = (existing, fresh) => { - if (!Array.isArray(existing)) { - existing = [existing] + this.multiaddrs.some((m, i) => { + if (m.equals(addr)) { + this.multiaddrs.splice(i, 1) + return true } - if (!Array.isArray(fresh)) { - fresh = [fresh] - } - existing.forEach((m) => this.multiaddr.rm(m)) - fresh.forEach((m) => this.multiaddr.add(m)) + }) + } + + multiaddrReplace (existing, fresh) { + if (!Array.isArray(existing)) { + existing = [existing] } + if (!Array.isArray(fresh)) { + fresh = [fresh] + } + existing.forEach((m) => this.multiaddr.rm(m)) + fresh.forEach((m) => this.multiaddr.add(m)) } - distinctMultiaddr () { + multiaddrUniq () { return uniqBy(this.multiaddrs, (item) => { return [item.toOptions().port, item.toOptions().transport].join() }) } - setConnectedMultiaddr (ma) { + // only stores the current multiaddr being used + connect (ma) { ma = ensureMultiaddr(ma) this._connectedMultiaddr = ma } + disconnect () { + this._connectedMultiaddr = undefined + } + isConnected () { return Boolean(this._connectedMultiaddr) } From 002d40834146091e2a5a9c660839a13d28db2f25 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 10:48:18 +0100 Subject: [PATCH 3/6] for friedel with <3 --- src/index.js | 77 +++-------------------------------------- src/multiaddr-set.js | 81 ++++++++++++++++++++++++++++++++++++++++++++ src/utils.js | 15 ++++++++ 3 files changed, 100 insertions(+), 73 deletions(-) create mode 100644 src/multiaddr-set.js create mode 100644 src/utils.js diff --git a/src/index.js b/src/index.js index 2cc2e66..570874d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,90 +1,21 @@ 'use strict' const Id = require('peer-id') -const multiaddr = require('multiaddr') -const uniqBy = require('lodash.uniqby') +const ensureMultiaddr = require('./utils').ensureMultiaddr +const MultiaddrSet = require('./multiaddr-set') const assert = require('assert') -function ensureMultiaddr (addr) { - if (multiaddr.isMultiaddr(addr)) { - return addr - } - - return multiaddr(addr) -} - // Peer represents a peer on the IPFS network class PeerInfo { constructor (peerId) { assert(peerId, 'Missing peerId. Use Peer.create(cb) to create one') this.id = peerId - this.multiaddrs = [] - this.protocols = [] - this._observedMultiaddrs = [] + this.multiaddrs = new MultiaddrSet() + this.protocols = new Set() this._connectedMultiaddr = undefined } - multiaddrAdd (addr) { - addr = ensureMultiaddr(addr) - - var exists = false - this.multiaddrs.some((m, i) => { - if (m.equals(addr)) { - exists = true - return true - } - }) - if (!exists) { - this.multiaddrs.push(addr) - } - } - - // to prevent multiaddr explosion due to Identify - multiaddrAddSafe (addr) { - addr = ensureMultiaddr(addr) - - let check = false - this._observedMultiaddrs.some((m, i) => { - if (m.equals(addr)) { - this.multiaddr.add(addr) - this._observedMultiaddrs.splice(i, 1) - check = true - } - }) - if (!check) { - this._observedMultiaddrs.push(addr) - } - } - - multiaddrRemove (addr) { - addr = ensureMultiaddr(addr) - - this.multiaddrs.some((m, i) => { - if (m.equals(addr)) { - this.multiaddrs.splice(i, 1) - return true - } - }) - } - - multiaddrReplace (existing, fresh) { - if (!Array.isArray(existing)) { - existing = [existing] - } - if (!Array.isArray(fresh)) { - fresh = [fresh] - } - existing.forEach((m) => this.multiaddr.rm(m)) - fresh.forEach((m) => this.multiaddr.add(m)) - } - - multiaddrUniq () { - return uniqBy(this.multiaddrs, (item) => { - return [item.toOptions().port, item.toOptions().transport].join() - }) - } - // only stores the current multiaddr being used connect (ma) { ma = ensureMultiaddr(ma) diff --git a/src/multiaddr-set.js b/src/multiaddr-set.js new file mode 100644 index 0000000..c591cf2 --- /dev/null +++ b/src/multiaddr-set.js @@ -0,0 +1,81 @@ +'use strict' + +const ensureMultiaddr = require('./utils').ensureMultiaddr + +// Because JavaScript doesn't let you overload the compare in Set().. +class MultiaddrSet { + constructor (multiaddrs) { + this._multiaddrs = multiaddrs || [] + this._observedMultiaddrs = [] + } + + add (ma) { + ma = ensureMultiaddr(ma) + + if (!this.multiaddrHas(ma)) { + this.multiaddrs.push(ma) + } + } + + // to prevent multiaddr explosion due to identify + addSafe (ma) { + ma = ensureMultiaddr(ma) + + let check = false + this._observedMultiaddrs.some((m, i) => { + if (m.equals(ma)) { + this.multiaddr.add(ma) + this._observedMultiaddrs.splice(i, 1) + check = true + } + }) + if (!check) { + this._observedMultiaddrs.push(ma) + } + } + + toArray () { + return this._multiaddrs + } + + get length () { + return this.multiaddrs.length + } + + forEach (fn) { + return this._multiaddrs.forEach(fn) + } + + has (ma) { + return this._multiaddrs.some((m) => m.equals(ma)) + } + + delete (ma) { + ma = ensureMultiaddr(ma) + + this._multiaddrs.some((m, i) => { + if (m.equals(ma)) { + this.multiaddrs.splice(i, 1) + return true + } + }) + } + + // replaces selected existing multiaddrs with new ones + replace (existing, fresh) { + if (!Array.isArray(existing)) { + existing = [existing] + } + if (!Array.isArray(fresh)) { + fresh = [fresh] + } + existing.forEach((m) => this.multiaddr.rm(m)) + fresh.forEach((m) => this.multiaddr.add(m)) + } + + clear () { + this._multiaddrs = [] + } +} + +module.exports = MultiaddrSet diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..bf099af --- /dev/null +++ b/src/utils.js @@ -0,0 +1,15 @@ +'use strict' + +const multiaddr = require('multiaddr') + +function ensureMultiaddr (ma) { + if (multiaddr.isMultiaddr(ma)) { + return ma + } + + return multiaddr(ma) +} + +module.exports = { + ensureMultiaddr: ensureMultiaddr +} From 5840f9d63e2c3380ca0c9e63b594fbfa8fea554a Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 11:02:50 +0100 Subject: [PATCH 4/6] cr --- src/multiaddr-set.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/multiaddr-set.js b/src/multiaddr-set.js index c591cf2..f4b36aa 100644 --- a/src/multiaddr-set.js +++ b/src/multiaddr-set.js @@ -12,7 +12,7 @@ class MultiaddrSet { add (ma) { ma = ensureMultiaddr(ma) - if (!this.multiaddrHas(ma)) { + if (!this.has(ma)) { this.multiaddrs.push(ma) } } @@ -35,10 +35,10 @@ class MultiaddrSet { } toArray () { - return this._multiaddrs + return this._multiaddrs.slice() } - get length () { + get size () { return this.multiaddrs.length } From fbaa55bca53b09717396de19b4a69d0ca78fe019 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 11:35:19 +0100 Subject: [PATCH 5/6] last tests, cr, docs --- README.md | 26 +++-- package.json | 2 +- src/multiaddr-set.js | 36 +++++-- test/index.spec.js | 228 ++++++++++++++++++++++--------------------- 4 files changed, 162 insertions(+), 130 deletions(-) diff --git a/README.md b/README.md index 544d8f1..273ee32 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,19 @@ - [API](#api) - [`PeerInfo.create([id, ] callback)`](#peerinfocreateid-callback) - [`new PeerInfo(id)`](#new-peerinfoid) - - [`multiaddrs`](#multiaddrs) - - [`multiaddr.add(addr)`](#multiaddraddaddr) - - [`multiaddr.addSafe(addr)`](#multiaddraddsafeaddr) - - [`multiaddr.rm(addr)`](#multiaddrrmaddr) - - [`multiaddr.replace(existing, fresh)`](#multiaddrreplaceexisting-fresh) + - [`.connect(ma)`](#connectma) + - [`.disconnect()`](#connectma) + - [`.isConnected()`](#connectma) + - [`.multiaddrs`](#multiaddrs) + - [`.multiaddrs.add(addr)`](#multiaddraddaddr) + - [`.multiaddrs.addSafe(addr)`](#multiaddraddsafeaddr) + - [`.multiaddrs.forEach(fn)`](#multiaddrforeachfn) + - [`.multaiddrs.size()`]((#multiaddrsize) + - [`.multiaddrs.has()`]((#multiaddrhas) + - [`.multiaddrs.delete(addr)`](#multiaddrdeleteaddr) + - [`.multiaddrs.toArray()`](#multiaddrtoarray) + - [`.multiaddrs.replace(existing, fresh)`](#multiaddrreplaceexisting-fresh) + - [`.multiaddrs.distinct()`](#distinct) - [Contribute](#contribute) - [License](#license) @@ -103,14 +111,14 @@ Creates a new PeerInfo instance from an existing PeerID. A list of multiaddresses instances that `peer` can be reached at. -### `multiaddr.add(addr)` +### `.multiaddrs.add(addr)` - `addr: Multiaddr` Adds a new multiaddress that `peer` can be reached at. `addr` is an instance of a [multiaddr](https://github.com/libp2p/js-multiaddr). -### `multiaddr.addSafe(addr)` +### `.multiaddr.addSafe(addr)` - `addr: Multiaddr` @@ -123,13 +131,13 @@ peers which will not provide a useful multiaddr to be shared to the rest of the network (e.g. a multiaddr referring to a peer inside a LAN being shared to the outside world). -### `multiaddr.rm(addr)` +### `.multiaddr.delete(addr)` - `addr: Multiaddr` Removes a multiaddress instance `addr` from `peer`. -### `multiaddr.replace(existing, fresh)` +### `.multiaddr.replace(existing, fresh)` - `existing: Multiaddr` - `fresh: Multiaddr` diff --git a/package.json b/package.json index 8c2e0f3..0c2663c 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "dependencies": { "multiaddr": "^2.3.0", "lodash.uniqby": "^4.7.0", - "peer-id": "^0.8.5" + "peer-id": "^0.8.6" }, "contributors": [ "David Dias ", diff --git a/src/multiaddr-set.js b/src/multiaddr-set.js index f4b36aa..88607a9 100644 --- a/src/multiaddr-set.js +++ b/src/multiaddr-set.js @@ -1,6 +1,7 @@ 'use strict' const ensureMultiaddr = require('./utils').ensureMultiaddr +const uniqBy = require('lodash.uniqby') // Because JavaScript doesn't let you overload the compare in Set().. class MultiaddrSet { @@ -13,20 +14,25 @@ class MultiaddrSet { ma = ensureMultiaddr(ma) if (!this.has(ma)) { - this.multiaddrs.push(ma) + this._multiaddrs.push(ma) } } - // to prevent multiaddr explosion due to identify + // addSafe - prevent multiaddr explosion™ + // Multiaddr explosion is when you dial to a bunch of nodes and every node + // gives you a different observed address and you start storing them all to + // share with other peers. This seems like a good idea until you realize that + // most of those addresses are unique to the subnet that peer is in and so, + // they are completely worthless for all the other peers. This method is + // exclusively used by identify. addSafe (ma) { ma = ensureMultiaddr(ma) - let check = false - this._observedMultiaddrs.some((m, i) => { + const check = this._observedMultiaddrs.some((m, i) => { if (m.equals(ma)) { - this.multiaddr.add(ma) + this.add(ma) this._observedMultiaddrs.splice(i, 1) - check = true + return true } }) if (!check) { @@ -39,7 +45,7 @@ class MultiaddrSet { } get size () { - return this.multiaddrs.length + return this._multiaddrs.length } forEach (fn) { @@ -55,7 +61,7 @@ class MultiaddrSet { this._multiaddrs.some((m, i) => { if (m.equals(ma)) { - this.multiaddrs.splice(i, 1) + this._multiaddrs.splice(i, 1) return true } }) @@ -69,13 +75,23 @@ class MultiaddrSet { if (!Array.isArray(fresh)) { fresh = [fresh] } - existing.forEach((m) => this.multiaddr.rm(m)) - fresh.forEach((m) => this.multiaddr.add(m)) + existing.forEach((m) => this.delete(m)) + fresh.forEach((m) => this.add(m)) } clear () { this._multiaddrs = [] } + + // this only really helps make ip6 and ip4 multiaddrs distinct if they are + // different + // TODO this is not an ideal solution, probably this code should just be + // in libp2p-tcp + distinct () { + return uniqBy(this._multiaddrs, (ma) => { + return [ma.toOptions().port, ma.toOptions().transport].join() + }) + } } module.exports = MultiaddrSet diff --git a/test/index.spec.js b/test/index.spec.js index ad33224..196ec38 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -37,9 +37,7 @@ describe('peer-info', () => { }) it('throws when not passing an Id', () => { - expect( - () => new PeerInfo() - ).to.throw() + expect(() => new PeerInfo()).to.throw() }) it('isPeerInfo', () => { @@ -56,185 +54,195 @@ describe('peer-info', () => { }) }) - it('PeerInfo.create with exist()ing id', (done) => { + it('PeerInfo.create with existing id', (done) => { PeerId.create((err, id) => { expect(err).to.not.exist() PeerInfo.create(id, (err, pi) => { expect(err).to.not.exist() expect(pi.id).to.exist() - expect(pi.id).to.deep.equal(id) + expect(pi.id.isEqual(id)).to.equal(true) done() }) }) }) it('add multiaddr', () => { - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - pi.multiaddr.add(mh) - expect(pi.multiaddrs.length).to.equal(1) + const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.add(ma) + expect(pi.multiaddrs.size).to.equal(1) }) it('add multiaddr that are buffers', () => { - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - pi.multiaddr.add(mh.buffer) - expect(pi.multiaddrs[0] instanceof Multiaddr).to.equal(true) + const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.add(ma.buffer) + expect(pi.multiaddrs.has(ma)).to.equal(true) }) it('add repeated multiaddr', () => { - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - pi.multiaddr.add(mh) - expect(pi.multiaddrs.length).to.equal(1) - pi.multiaddr.add(mh) - expect(pi.multiaddrs.length).to.equal(1) + const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.add(ma) + expect(pi.multiaddrs.size).to.equal(1) + pi.multiaddrs.add(ma) + expect(pi.multiaddrs.size).to.equal(1) }) - it('rm multiaddr', () => { - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - pi.multiaddr.add(mh) - expect(pi.multiaddrs.length).to.equal(1) - pi.multiaddr.rm(mh) - expect(pi.multiaddrs.length).to.equal(0) + it('delete multiaddr', () => { + const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.add(ma) + expect(pi.multiaddrs.size).to.equal(1) + pi.multiaddrs.delete(ma) + expect(pi.multiaddrs.size).to.equal(0) }) it('addSafe - avoid multiaddr explosion', () => { - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/9002') - const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/9009') - pi.multiaddr.addSafe(mh) - expect(pi.multiaddrs.length).to.equal(0) - pi.multiaddr.addSafe(mh) - expect(pi.multiaddrs.length).to.equal(1) - pi.multiaddr.addSafe(mh2) - pi.multiaddr.addSafe(mh3) - expect(pi.multiaddrs.length).to.equal(1) + const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/tcp/9002') + const ma3 = Multiaddr('/ip4/127.0.0.1/tcp/9009') + pi.multiaddrs.addSafe(ma) + expect(pi.multiaddrs.size).to.equal(0) + pi.multiaddrs.addSafe(ma) + expect(pi.multiaddrs.size).to.equal(1) + pi.multiaddrs.addSafe(ma2) + pi.multiaddrs.addSafe(ma3) + expect(pi.multiaddrs.size).to.equal(1) }) it('addSafe - multiaddr that are buffers', () => { - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - pi.multiaddr.addSafe(mh.buffer) - pi.multiaddr.addSafe(mh.buffer) - expect(pi.multiaddrs[0] instanceof Multiaddr).to.equal(true) + const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.addSafe(ma.buffer) + pi.multiaddrs.addSafe(ma.buffer) + expect(pi.multiaddrs.has(ma)).to.equal(true) }) it('replace multiaddr', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') - const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') - const mh4 = Multiaddr('/ip4/127.0.0.1/tcp/5004') - const mh5 = Multiaddr('/ip4/127.0.0.1/tcp/5005') - const mh6 = Multiaddr('/ip4/127.0.0.1/tcp/5006') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') + const ma3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') + const ma4 = Multiaddr('/ip4/127.0.0.1/tcp/5004') + const ma5 = Multiaddr('/ip4/127.0.0.1/tcp/5005') + const ma6 = Multiaddr('/ip4/127.0.0.1/tcp/5006') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) - pi.multiaddr.add(mh3) - pi.multiaddr.add(mh4) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) + pi.multiaddrs.add(ma3) + pi.multiaddrs.add(ma4) - expect(pi.multiaddrs.length).to.equal(4) + expect(pi.multiaddrs.size).to.equal(4) - const old = [mh2, mh4] - const fresh = [mh5, mh6] + const old = [ma2, ma4] + const fresh = [ma5, ma6] - pi.multiaddr.replace(old, fresh) + pi.multiaddrs.replace(old, fresh) - expect(pi.multiaddrs.length).to.equal(4) + expect(pi.multiaddrs.size).to.equal(4) }) it('replace multiaddr (no arrays)', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') - const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') - const mh4 = Multiaddr('/ip4/127.0.0.1/tcp/5004') - const mh5 = Multiaddr('/ip4/127.0.0.1/tcp/5005') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') + const ma3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') + const ma4 = Multiaddr('/ip4/127.0.0.1/tcp/5004') + const ma5 = Multiaddr('/ip4/127.0.0.1/tcp/5005') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) - pi.multiaddr.add(mh3) - pi.multiaddr.add(mh4) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) + pi.multiaddrs.add(ma3) + pi.multiaddrs.add(ma4) - expect(pi.multiaddrs.length).to.equal(4) + expect(pi.multiaddrs.size).to.equal(4) - const old = mh2 - const fresh = mh5 + const old = ma2 + const fresh = ma5 - pi.multiaddr.replace(old, fresh) + pi.multiaddrs.replace(old, fresh) - expect(pi.multiaddrs.length).to.equal(4) + expect(pi.multiaddrs.size).to.equal(4) }) it('get distinct multiaddr same transport multiple different ports', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') - const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') - const mh4 = Multiaddr('/ip4/127.0.0.1/tcp/5004') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') + const ma3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') + const ma4 = Multiaddr('/ip4/127.0.0.1/tcp/5004') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) - pi.multiaddr.add(mh3) - pi.multiaddr.add(mh4) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) + pi.multiaddrs.add(ma3) + pi.multiaddrs.add(ma4) - var distinctMultiaddr = pi.distinctMultiaddr() + var distinctMultiaddr = pi.multiaddrs.distinct() expect(distinctMultiaddr.length).to.equal(4) }) it('get distinct multiaddr same transport different port', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) - var distinctMultiaddr = pi.distinctMultiaddr() - expect(distinctMultiaddr.length).to.equal(2) + var multiaddrDistinct = pi.multiaddrs.distinct() + expect(multiaddrDistinct.length).to.equal(2) }) it('get distinct multiaddr same transport same port', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) - var distinctMultiaddr = pi.distinctMultiaddr() - expect(distinctMultiaddr.length).to.equal(1) + var multiaddrDistinct = pi.multiaddrs.distinct() + expect(multiaddrDistinct.length).to.equal(1) }) it('get distinct multiaddr different transport same port', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip4/127.0.0.1/udp/5001') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip4/127.0.0.1/udp/5001') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) - var distinctMultiaddr = pi.distinctMultiaddr() - expect(distinctMultiaddr.length).to.equal(2) + var multiaddrDistinct = pi.multiaddrs.distinct() + expect(multiaddrDistinct.length).to.equal(2) }) it('get distinct multiaddr different family same port same transport', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip6/::/tcp/5001') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip6/::/tcp/5001') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) - var distinctMultiaddr = pi.distinctMultiaddr() - expect(distinctMultiaddr.length).to.equal(1) + const multiaddrDistinct = pi.multiaddrs.distinct() + expect(multiaddrDistinct.length).to.equal(1) }) it('get distinct multiaddr different family same port multiple transports', () => { - const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const mh2 = Multiaddr('/ip6/::/tcp/5001') - const mh3 = Multiaddr('/ip6/::/udp/5002') - const mh4 = Multiaddr('/ip4/127.0.0.1/udp/5002') + const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') + const ma2 = Multiaddr('/ip6/::/tcp/5001') + const ma3 = Multiaddr('/ip6/::/udp/5002') + const ma4 = Multiaddr('/ip4/127.0.0.1/udp/5002') - pi.multiaddr.add(mh1) - pi.multiaddr.add(mh2) - pi.multiaddr.add(mh3) - pi.multiaddr.add(mh4) + pi.multiaddrs.add(ma1) + pi.multiaddrs.add(ma2) + pi.multiaddrs.add(ma3) + pi.multiaddrs.add(ma4) - var distinctMultiaddr = pi.distinctMultiaddr() - expect(distinctMultiaddr.length).to.equal(2) + const multiaddrDistinct = pi.multiaddrs.distinct() + expect(multiaddrDistinct.length).to.equal(2) - expect(distinctMultiaddr[0].toOptions().family).to.equal('ipv4') - expect(distinctMultiaddr[1].toOptions().family).to.equal('ipv6') + expect(multiaddrDistinct[0].toOptions().family).to.equal('ipv4') + expect(multiaddrDistinct[1].toOptions().family).to.equal('ipv6') + }) + + it('.connect', () => { + pi.connect('/ip4/127.0.0.1') + expect(pi.isConnected()).to.equal(true) + }) + + it('.disconnect', () => { + pi.disconnect('/ip4/127.0.0.1') + expect(pi.isConnected()).to.equal(false) }) }) From 5f3407e3d2b3f927ae142458716a86b85de2a3a1 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 12:19:37 +0100 Subject: [PATCH 6/6] moar tests --- README.md | 2 +- src/index.js | 3 +++ src/multiaddr-set.js | 1 + test/index.spec.js | 34 +++++++++++++++++++++++++++++----- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 273ee32..92780bb 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ - [`.multiaddrs.add(addr)`](#multiaddraddaddr) - [`.multiaddrs.addSafe(addr)`](#multiaddraddsafeaddr) - [`.multiaddrs.forEach(fn)`](#multiaddrforeachfn) - - [`.multaiddrs.size()`]((#multiaddrsize) + - [`.multaiddrs.size`]((#multiaddrsize) - [`.multiaddrs.has()`]((#multiaddrhas) - [`.multiaddrs.delete(addr)`](#multiaddrdeleteaddr) - [`.multiaddrs.toArray()`](#multiaddrtoarray) diff --git a/src/index.js b/src/index.js index 570874d..55ff55e 100644 --- a/src/index.js +++ b/src/index.js @@ -19,6 +19,9 @@ class PeerInfo { // only stores the current multiaddr being used connect (ma) { ma = ensureMultiaddr(ma) + if (!this.multiaddrs.has(ma)) { + throw new Error('can\'t be connected to missing multiaddr from set') + } this._connectedMultiaddr = ma } diff --git a/src/multiaddr-set.js b/src/multiaddr-set.js index 88607a9..a8f9bdf 100644 --- a/src/multiaddr-set.js +++ b/src/multiaddr-set.js @@ -53,6 +53,7 @@ class MultiaddrSet { } has (ma) { + ma = ensureMultiaddr(ma) return this._multiaddrs.some((m) => m.equals(ma)) } diff --git a/test/index.spec.js b/test/index.spec.js index 196ec38..1ae895c 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -236,13 +236,37 @@ describe('peer-info', () => { expect(multiaddrDistinct[1].toOptions().family).to.equal('ipv6') }) - it('.connect', () => { - pi.connect('/ip4/127.0.0.1') - expect(pi.isConnected()).to.equal(true) + it('multiaddrs.has', () => { + pi.multiaddrs.add('/ip4/127.0.0.1/tcp/5001') + expect(pi.multiaddrs.has('/ip4/127.0.0.1/tcp/5001')).to.equal(true) + expect(pi.multiaddrs.has('/ip4/127.0.0.1/tcp/5001/ws')).to.equal(false) + }) + + it('multiaddrs.forEach', () => { + pi.multiaddrs.add('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.forEach((ma) => { + expect(pi.multiaddrs.has(ma)).to.equal(true) + }) }) - it('.disconnect', () => { - pi.disconnect('/ip4/127.0.0.1') + it('multiaddrs.toArray', () => { + pi.multiaddrs.add('/ip4/127.0.0.1/tcp/5001') + pi.multiaddrs.toArray().forEach((ma) => { + expect(pi.multiaddrs.has(ma)).to.equal(true) + }) + }) + + it('.connect .disconnect', () => { + pi.multiaddrs.add('/ip4/127.0.0.1/tcp/5001') + pi.connect('/ip4/127.0.0.1/tcp/5001') + expect(pi.isConnected()).to.equal(true) + pi.disconnect() expect(pi.isConnected()).to.equal(false) + expect(() => pi.connect('/ip4/127.0.0.1/tcp/5001/ws')).to.throw() + }) + + it('multiaddrs.clear', () => { + pi.multiaddrs.clear() + expect(pi.multiaddrs.size).to.equal(0) }) })