Skip to content

Commit

Permalink
fix: yield only final peers from dht getClosestPeers (#1380)
Browse files Browse the repository at this point in the history
* fix: yield final peers from dht getClosestPeers

`PEER_RESPONSE` is an intermediate event, we should only yield from `FINAL_PEER` events as we'll only get `K` of those.

* chore: fix test
  • Loading branch information
achingbrain authored Sep 23, 2022
1 parent 6219841 commit 3f57eda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/dht/dht-peer-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class DHTPeerRouting implements PeerRouting {

async * getClosestPeers (key: Uint8Array, options: AbortOptions = {}) {
for await (const event of this.dht.getClosestPeers(key, options)) {
if (event.name === 'PEER_RESPONSE') {
yield * event.closer
if (event.name === 'FINAL_PEER') {
yield event.peer
}
}
}
Expand Down
27 changes: 10 additions & 17 deletions test/peer-routing/peer-routing.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,15 @@ describe('peer-routing', () => {
const dhtGetClosestPeersStub = sinon.stub(nodes[0].dht, 'getClosestPeers').callsFake(async function * () {
yield {
from: nodes[2].peerId,
type: EventTypes.PEER_RESPONSE,
name: 'PEER_RESPONSE',
type: EventTypes.FINAL_PEER,
name: 'FINAL_PEER',
messageName: 'FIND_NODE',
messageType: MessageType.FIND_NODE,
closer: [{
peer: {
id: nodes[1].peerId,
multiaddrs: [],
protocols: []
}],
providers: []
}
}
})

Expand Down Expand Up @@ -589,26 +588,20 @@ describe('peer-routing', () => {
const peerStoreAddressBookAddStub = sinon.spy(node.peerStore.addressBook, 'add')
const dhtGetClosestPeersStub = sinon.stub(node.dht, 'getClosestPeers').callsFake(async function * () {
yield {
name: 'PEER_RESPONSE',
type: EventTypes.PEER_RESPONSE,
name: 'FINAL_PEER',
type: EventTypes.FINAL_PEER,
messageName: 'FIND_NODE',
messageType: MessageType.FIND_NODE,
from: peerIds[0],
closer: [
results[0]
],
providers: []
peer: results[0]
}
yield {
name: 'PEER_RESPONSE',
type: EventTypes.PEER_RESPONSE,
name: 'FINAL_PEER',
type: EventTypes.FINAL_PEER,
messageName: 'FIND_NODE',
messageType: MessageType.FIND_NODE,
from: peerIds[0],
closer: [
results[1]
],
providers: []
peer: results[1]
}
})

Expand Down

0 comments on commit 3f57eda

Please sign in to comment.