Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid deleting nodes from peerBook #76

Merged
merged 5 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +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 <[email protected]>",
Expand All @@ -54,4 +55,4 @@
"greenkeeperio-bot <[email protected]>",
"mayerwin <[email protected]>"
]
}
}
33 changes: 25 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,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) {
Expand All @@ -38,16 +39,15 @@ 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)
})

this.swarm.on('peer-mux-closed', (peerInfo) => {
this.emit('peer:disconnect', peerInfo)
this.peerBook.removeByB58String(peerInfo.id.toB58String())
})
}

Expand Down Expand Up @@ -91,7 +91,19 @@ class Node extends EventEmitter {
let transports = this.modules.transport

transports = Array.isArray(transports) ? transports : [transports]
const multiaddrs = this.peerInfo.multiaddrs

// so that we can have webrtc-star addrs without adding manually the id
const maOld = []
const maNew = []
this.peerInfo.multiaddrs.forEach((ma) => {
if (!mafmt.IPFS.matches(ma)) {
maOld.push(ma)
maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()))
}
})
this.peerInfo.multiaddrs.replace(maOld, maNew)

const multiaddrs = this.peerInfo.multiaddrs.toArray()

transports.forEach((transport) => {
if (transport.filter(multiaddrs).length > 0) {
Expand Down Expand Up @@ -152,13 +164,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)
Expand All @@ -172,7 +190,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)
}

Expand All @@ -198,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 {
Expand Down