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: yield only final peers from dht getClosestPeers #1380

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -115,16 +115,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 @@ -673,26 +672,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