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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

View File

@ -259,6 +259,33 @@ describe('peer discovery', () => {
})
})
describe('discovery scenarios', () => {
setup({
config: {
dht: {
enabled: false
},
peerDiscovery: {
autoDial: false,
bootstrap: {
enabled: true,
list: []
}
}
}
})
it('should ignore self on discovery', function () {
const discoverySpy = sinon.spy()
nodeA.on('peer:discovery', discoverySpy)
nodeA._discovery[0].emit('peer', nodeA.peerInfo)
expect(discoverySpy.called).to.eql(false)
expect(nodeA.peerBook.getAllArray()).to.have.length(0)
expect()
})
})
describe('MulticastDNS', () => {
setup({
config: {