fix: bail when discovering self (#357)

This commit is contained in:
Jacob Heun
2019-04-16 12:05:22 +02:00
committed by GitHub
parent c049074cb5
commit f28dffb268
3 changed files with 36 additions and 2 deletions

View File

@ -7,5 +7,7 @@ exports.messages = {
exports.codes = {
DHT_DISABLED: 'ERR_DHT_DISABLED',
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED'
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF'
}

View File

@ -24,11 +24,12 @@ const dht = require('./dht')
const pubsub = require('./pubsub')
const getPeerInfo = require('./get-peer-info')
const validateConfig = require('./config').validate
const { codes } = require('./errors')
const notStarted = (action, state) => {
return errCode(
new Error(`libp2p cannot ${action} when not started; state is ${state}`),
'ERR_NODE_NOT_STARTED'
codes.ERR_NODE_NOT_STARTED
)
}
@ -476,6 +477,10 @@ class Node extends EventEmitter {
* @param {PeerInfo} peerInfo
*/
_peerDiscovered (peerInfo) {
if (peerInfo.id.toB58String() === this.peerInfo.id.toB58String()) {
log.error(new Error(codes.ERR_DISCOVERED_SELF))
return
}
peerInfo = this.peerBook.put(peerInfo)
if (!this.isStarted()) return