Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/peer-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class PeerRouting {
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
}

if (id.toB58String()== this._peerId.toB58String()) {
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
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)])
Expand Down
12 changes: 12 additions & 0 deletions test/peer-routing/peer-routing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ describe('peer-routing', () => {

return deferred.promise
})

it('should error when peer tries to find itself', async () =>{
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
Expand Down Expand Up @@ -187,6 +193,12 @@ describe('peer-routing', () => {
expect(mockApi.isDone()).to.equal(true)
})

it('should error when peer tries to find itself', async () =>{
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
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')
Expand Down