diff --git a/src/peer-routing.js b/src/peer-routing.js index 0ff6f7b8..aaf06745 100644 --- a/src/peer-routing.js +++ b/src/peer-routing.js @@ -104,6 +104,10 @@ class PeerRouting { throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE') } + if (id.toB58String() === this._peerId.toB58String()) { + throw errCode(new Error('Should not try to find self'), 'ERR_FIND_SELF') + } + const output = await pipe( merge( ...this._routers.map(router => [router.findPeer(id, options)]) diff --git a/test/peer-routing/peer-routing.node.js b/test/peer-routing/peer-routing.node.js index 5413c3f1..5149c7d4 100644 --- a/test/peer-routing/peer-routing.node.js +++ b/test/peer-routing/peer-routing.node.js @@ -100,6 +100,12 @@ describe('peer-routing', () => { return deferred.promise }) + + it('should error when peer tries to find itself', async () => { + await expect(nodes[0].peerRouting.findPeer(nodes[0].peerId)) + .to.eventually.be.rejected() + .and.to.have.property('code', 'ERR_FIND_SELF') + }) }) describe('via delegate router', () => { @@ -187,6 +193,12 @@ describe('peer-routing', () => { expect(mockApi.isDone()).to.equal(true) }) + it('should error when peer tries to find itself', async () => { + await expect(node.peerRouting.findPeer(node.peerId)) + .to.eventually.be.rejected() + .and.to.have.property('code', 'ERR_FIND_SELF') + }) + it('should error when a peer cannot be found', async () => { const peerKey = 'key of a peer not on the network' const mockApi = nock('http://0.0.0.0:60197')