mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-09 10:56:02 +00:00
Merge pull request #76 from libp2p/feat/advanced-peer-book
fix: avoid deleting nodes from peerBook
This commit is contained in:
commit
c6bdd869be
@ -41,11 +41,12 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"libp2p-ping": "~0.3.2",
|
"libp2p-ping": "~0.3.2",
|
||||||
"libp2p-swarm": "~0.28.0",
|
"libp2p-swarm": "~0.29.0",
|
||||||
|
"mafmt": "^2.1.8",
|
||||||
"multiaddr": "^2.3.0",
|
"multiaddr": "^2.3.0",
|
||||||
"peer-book": "~0.3.2",
|
"peer-book": "~0.4.0",
|
||||||
"peer-id": "~0.8.5",
|
"peer-id": "~0.8.6",
|
||||||
"peer-info": "~0.8.5"
|
"peer-info": "~0.9.2"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
33
src/index.js
33
src/index.js
@ -3,6 +3,7 @@
|
|||||||
const Swarm = require('libp2p-swarm')
|
const Swarm = require('libp2p-swarm')
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
const PeerInfo = require('peer-info')
|
const PeerInfo = require('peer-info')
|
||||||
|
const mafmt = require('mafmt')
|
||||||
const PeerBook = require('peer-book')
|
const PeerBook = require('peer-book')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
@ -25,7 +26,7 @@ class Node extends EventEmitter {
|
|||||||
this.peerBook = _peerBook || new PeerBook()
|
this.peerBook = _peerBook || new PeerBook()
|
||||||
this.isOnline = false
|
this.isOnline = false
|
||||||
|
|
||||||
this.swarm = new Swarm(this.peerInfo)
|
this.swarm = new Swarm(this.peerInfo, this.peerBook)
|
||||||
|
|
||||||
// Attach stream multiplexers
|
// Attach stream multiplexers
|
||||||
if (this.modules.connection.muxer) {
|
if (this.modules.connection.muxer) {
|
||||||
@ -38,8 +39,8 @@ class Node extends EventEmitter {
|
|||||||
// If muxer exists, we can use Identify
|
// If muxer exists, we can use Identify
|
||||||
this.swarm.connection.reuse()
|
this.swarm.connection.reuse()
|
||||||
|
|
||||||
// Received incommind dial and muxer upgrade happened, reuse this
|
// Received incommind dial and muxer upgrade happened,
|
||||||
// muxed connection
|
// reuse this muxed connection
|
||||||
this.swarm.on('peer-mux-established', (peerInfo) => {
|
this.swarm.on('peer-mux-established', (peerInfo) => {
|
||||||
this.emit('peer:connect', peerInfo)
|
this.emit('peer:connect', peerInfo)
|
||||||
this.peerBook.put(peerInfo)
|
this.peerBook.put(peerInfo)
|
||||||
@ -47,7 +48,6 @@ class Node extends EventEmitter {
|
|||||||
|
|
||||||
this.swarm.on('peer-mux-closed', (peerInfo) => {
|
this.swarm.on('peer-mux-closed', (peerInfo) => {
|
||||||
this.emit('peer:disconnect', peerInfo)
|
this.emit('peer:disconnect', peerInfo)
|
||||||
this.peerBook.removeByB58String(peerInfo.id.toB58String())
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,19 @@ class Node extends EventEmitter {
|
|||||||
let transports = this.modules.transport
|
let transports = this.modules.transport
|
||||||
|
|
||||||
transports = Array.isArray(transports) ? transports : [transports]
|
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) => {
|
transports.forEach((transport) => {
|
||||||
if (transport.filter(multiaddrs).length > 0) {
|
if (transport.filter(multiaddrs).length > 0) {
|
||||||
@ -152,13 +164,19 @@ class Node extends EventEmitter {
|
|||||||
|
|
||||||
dial (peer, protocol, callback) {
|
dial (peer, protocol, callback) {
|
||||||
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
||||||
const peerInfo = this._getPeerInfo(peer)
|
|
||||||
|
|
||||||
if (typeof protocol === 'function') {
|
if (typeof protocol === 'function') {
|
||||||
callback = protocol
|
callback = protocol
|
||||||
protocol = undefined
|
protocol = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let peerInfo
|
||||||
|
try {
|
||||||
|
peerInfo = this._getPeerInfo(peer)
|
||||||
|
} catch (err) {
|
||||||
|
return callback(err)
|
||||||
|
}
|
||||||
|
|
||||||
this.swarm.dial(peerInfo, protocol, (err, conn) => {
|
this.swarm.dial(peerInfo, protocol, (err, conn) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err)
|
return callback(err)
|
||||||
@ -172,7 +190,6 @@ class Node extends EventEmitter {
|
|||||||
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
||||||
const peerInfo = this._getPeerInfo(peer)
|
const peerInfo = this._getPeerInfo(peer)
|
||||||
|
|
||||||
this.peerBook.removeByB58String(peerInfo.id.toB58String())
|
|
||||||
this.swarm.hangUp(peerInfo, callback)
|
this.swarm.hangUp(peerInfo, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,7 +215,7 @@ class Node extends EventEmitter {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
||||||
}
|
}
|
||||||
p.multiaddr.add(peer)
|
p.multiaddrs.add(peer)
|
||||||
} else if (PeerId.isPeerId(peer)) {
|
} else if (PeerId.isPeerId(peer)) {
|
||||||
const peerIdB58Str = peer.toB58String()
|
const peerIdB58Str = peer.toB58String()
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user