From 300936f2f3d43fd378473e540b6ef83f04918db1 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 29 Mar 2017 18:03:00 +0100 Subject: [PATCH 1/5] fix: avoid deleting nodes from peerBook --- src/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index e7a6beef78..224ce347d9 100644 --- a/src/index.js +++ b/src/index.js @@ -38,8 +38,8 @@ class Node extends EventEmitter { // If muxer exists, we can use Identify this.swarm.connection.reuse() - // Received incommind dial and muxer upgrade happened, reuse this - // muxed connection + // Received incommind dial and muxer upgrade happened, + // reuse this muxed connection this.swarm.on('peer-mux-established', (peerInfo) => { this.emit('peer:connect', peerInfo) this.peerBook.put(peerInfo) @@ -47,7 +47,6 @@ class Node extends EventEmitter { this.swarm.on('peer-mux-closed', (peerInfo) => { this.emit('peer:disconnect', peerInfo) - this.peerBook.removeByB58String(peerInfo.id.toB58String()) }) } From 59ea9c388fb56b3dc14ccf74343ccc29e053a4a9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 29 Mar 2017 20:02:41 +0100 Subject: [PATCH 2/5] feat: append peer id to multiaddr if not there --- package.json | 3 ++- src/index.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e5aed837a..6401e72210 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "dependencies": { "libp2p-ping": "~0.3.2", "libp2p-swarm": "~0.28.0", + "mafmt": "^2.1.8", "multiaddr": "^2.3.0", "peer-book": "~0.3.2", "peer-id": "~0.8.5", @@ -54,4 +55,4 @@ "greenkeeperio-bot ", "mayerwin " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 224ce347d9..7b85a7da2f 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ 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 multiaddr = require('multiaddr') const EventEmitter = require('events').EventEmitter @@ -47,6 +48,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()) }) } @@ -104,6 +107,13 @@ class Node extends EventEmitter { } }) + // so that we can have webrtc-star addrs without adding manually the id + this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => { + if (!mafmt.IPFS.matches(ma)) { + ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()) + } + }) + this.swarm.listen((err) => { if (err) { return callback(err) From 291e79fc993f28d3a25271896192c670ab64ba0f Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 29 Mar 2017 21:02:37 +0100 Subject: [PATCH 3/5] fix: addition of ipfs id appendix must come before transport filtering --- src/index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 7b85a7da2f..113d899899 100644 --- a/src/index.js +++ b/src/index.js @@ -93,6 +93,15 @@ class Node extends EventEmitter { let transports = this.modules.transport transports = Array.isArray(transports) ? transports : [transports] + + // so that we can have webrtc-star addrs without adding manually the id + this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => { + if (!mafmt.IPFS.matches(ma)) { + ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()) + } + return ma + }) + const multiaddrs = this.peerInfo.multiaddrs transports.forEach((transport) => { @@ -107,13 +116,6 @@ class Node extends EventEmitter { } }) - // so that we can have webrtc-star addrs without adding manually the id - this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => { - if (!mafmt.IPFS.matches(ma)) { - ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()) - } - }) - this.swarm.listen((err) => { if (err) { return callback(err) From a4b41b0f9a1f243913e11a3243a0a80148393f81 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 07:12:45 +0100 Subject: [PATCH 4/5] feat: not remove peer from peerBook on disconnect --- src/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 113d899899..51057a3762 100644 --- a/src/index.js +++ b/src/index.js @@ -48,8 +48,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()) }) } @@ -163,13 +161,19 @@ class Node extends EventEmitter { dial (peer, protocol, callback) { assert(this.isOn(), OFFLINE_ERROR_MESSAGE) - const peerInfo = this._getPeerInfo(peer) if (typeof protocol === 'function') { callback = protocol protocol = undefined } + let peerInfo + try { + peerInfo = this._getPeerInfo(peer) + } catch (err) { + return callback(err) + } + this.swarm.dial(peerInfo, protocol, (err, conn) => { if (err) { return callback(err) @@ -183,7 +187,6 @@ class Node extends EventEmitter { assert(this.isOn(), OFFLINE_ERROR_MESSAGE) const peerInfo = this._getPeerInfo(peer) - this.peerBook.removeByB58String(peerInfo.id.toB58String()) this.swarm.hangUp(peerInfo, callback) } From dee0340806e088e46643b81bd89ce9177241a2e4 Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 31 Mar 2017 15:52:20 +0100 Subject: [PATCH 5/5] chore: update to latest peer-info, peer-book and libp2p-swarm --- package.json | 8 ++++---- src/index.js | 15 +++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 6401e72210..c5a64b585b 100644 --- a/package.json +++ b/package.json @@ -41,12 +41,12 @@ }, "dependencies": { "libp2p-ping": "~0.3.2", - "libp2p-swarm": "~0.28.0", + "libp2p-swarm": "~0.29.0", "mafmt": "^2.1.8", "multiaddr": "^2.3.0", - "peer-book": "~0.3.2", - "peer-id": "~0.8.5", - "peer-info": "~0.8.5" + "peer-book": "~0.4.0", + "peer-id": "~0.8.6", + "peer-info": "~0.9.2" }, "contributors": [ "David Dias ", diff --git a/src/index.js b/src/index.js index 51057a3762..ece861179a 100644 --- a/src/index.js +++ b/src/index.js @@ -26,7 +26,7 @@ class Node extends EventEmitter { this.peerBook = _peerBook || new PeerBook() this.isOnline = false - this.swarm = new Swarm(this.peerInfo) + this.swarm = new Swarm(this.peerInfo, this.peerBook) // Attach stream multiplexers if (this.modules.connection.muxer) { @@ -93,14 +93,17 @@ class Node extends EventEmitter { transports = Array.isArray(transports) ? transports : [transports] // so that we can have webrtc-star addrs without adding manually the id - this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => { + const maOld = [] + const maNew = [] + this.peerInfo.multiaddrs.forEach((ma) => { if (!mafmt.IPFS.matches(ma)) { - ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()) + maOld.push(ma) + maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String())) } - return ma }) + this.peerInfo.multiaddrs.replace(maOld, maNew) - const multiaddrs = this.peerInfo.multiaddrs + const multiaddrs = this.peerInfo.multiaddrs.toArray() transports.forEach((transport) => { if (transport.filter(multiaddrs).length > 0) { @@ -212,7 +215,7 @@ class Node extends EventEmitter { } catch (err) { p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) } - p.multiaddr.add(peer) + p.multiaddrs.add(peer) } else if (PeerId.isPeerId(peer)) { const peerIdB58Str = peer.toB58String() try {