fix: peerRouting.findPeer() trying to find self (#941)

* throw error if node attempts to find itself

Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
This commit is contained in:
zeim839 2021-05-12 18:46:20 +04:00 committed by GitHub
parent d372a68692
commit a79c6b50d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -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)])

View File

@ -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')