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: return closest peers from FIND_NODE #2499

Merged
merged 3 commits into from
Apr 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions packages/kad-dht/src/rpc/handlers/find-node.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { CodeError } from '@libp2p/interface'
import { protocols } from '@multiformats/multiaddr'
import { equals as uint8ArrayEquals } from 'uint8arrays'
import { MessageType } from '../../message/dht.js'
import type { PeerInfoMapper } from '../../index.js'
import type { Message } from '../../message/dht.js'
import type { PeerRouting } from '../../peer-routing/index.js'
import type { DHTMessageHandler } from '../index.js'
import type { ComponentLogger, Logger, PeerId, PeerInfo } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'

export interface FindNodeHandlerInit {
peerRouting: PeerRouting
Expand All @@ -17,23 +14,20 @@ export interface FindNodeHandlerInit {

export interface FindNodeHandlerComponents {
peerId: PeerId
addressManager: AddressManager
logger: ComponentLogger
}

export class FindNodeHandler implements DHTMessageHandler {
private readonly peerRouting: PeerRouting
private readonly peerInfoMapper: PeerInfoMapper
private readonly peerId: PeerId
private readonly addressManager: AddressManager
private readonly log: Logger

constructor (components: FindNodeHandlerComponents, init: FindNodeHandlerInit) {
const { peerRouting, logPrefix } = init

this.log = components.logger.forComponent(`${logPrefix}:rpc:handlers:find-node`)
this.peerId = components.peerId
this.addressManager = components.addressManager
this.peerRouting = peerRouting
this.peerInfoMapper = init.peerInfoMapper
}
Expand All @@ -44,27 +38,19 @@ export class FindNodeHandler implements DHTMessageHandler {
async handle (peerId: PeerId, msg: Message): Promise<Message> {
this.log('incoming request from %p for peers closer to %b', peerId, msg.key)

let closer: PeerInfo[] = []

if (msg.key == null) {
throw new CodeError('Invalid FIND_NODE message received - key was missing', 'ERR_INVALID_MESSAGE')
}

if (uint8ArrayEquals(this.peerId.toBytes(), msg.key)) {
closer = [{
id: this.peerId,
multiaddrs: this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
}]
} else {
closer = await this.peerRouting.getCloserPeersOffline(msg.key, peerId)
}
const closer: PeerInfo[] = await this.peerRouting.getCloserPeersOffline(msg.key, peerId)

const response: Message = {
type: MessageType.FIND_NODE,
clusterLevel: msg.clusterLevel,
closer: closer
.map(this.peerInfoMapper)
.filter(({ multiaddrs }) => multiaddrs.length)
.filter(({ id }) => !id.equals(this.peerId))
.map(peerInfo => ({
id: peerInfo.id.toBytes(),
multiaddrs: peerInfo.multiaddrs.map(ma => ma.bytes)
Expand Down
2 changes: 1 addition & 1 deletion packages/kad-dht/src/rpc/handlers/get-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

async handle (peerId: PeerId, msg: Message): Promise<Message> {
if (msg.key == null) {
throw new CodeError('Invalid FIND_NODE message received - key was missing', 'ERR_INVALID_MESSAGE')
throw new CodeError('Invalid GET_PROVIDERS message received - key was missing', 'ERR_INVALID_MESSAGE')

Check warning on line 43 in packages/kad-dht/src/rpc/handlers/get-providers.ts

View check run for this annotation

Codecov / codecov/patch

packages/kad-dht/src/rpc/handlers/get-providers.ts#L43

Added line #L43 was not covered by tests
}

let cid
Expand Down
31 changes: 31 additions & 0 deletions packages/kad-dht/test/kad-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,37 @@ describe('KadDHT', () => {

expect(res).to.not.be.empty()
})

it('should not include itself in getClosestPeers PEER_RESPONSE', async function () {
this.timeout(240 * 1000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why are we using a 240 second timeout?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No specific reason, it was copied from the preceding test.


const nDHTs = 30
const dhts = await Promise.all(
new Array(nDHTs).fill(0).map(async () => tdht.spawn())
)

const connected: Array<Promise<void>> = []

for (let i = 0; i < dhts.length - 1; i++) {
connected.push(tdht.connect(dhts[i], dhts[(i + 1) % dhts.length]))
}

await Promise.all(connected)

const res = await all(dhts[1].getClosestPeers(dhts[2].components.peerId.toBytes()))
expect(res).to.not.be.empty()

// no peer should include itself in the response, only other peers that it
// knows who are closer
for (const event of res) {
if (event.name !== 'PEER_RESPONSE') {
continue
}

expect(event.closer.map(peer => peer.id.toString()))
.to.not.include(event.from.toString())
}
})
})

describe('errors', () => {
Expand Down
37 changes: 21 additions & 16 deletions packages/kad-dht/test/rpc/handlers/find-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { peerIdFromBytes } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import Sinon, { type SinonStubbedInstance } from 'sinon'
import { stubInterface } from 'sinon-ts'
import { type Message, MessageType } from '../../../src/message/dht.js'
import { PeerRouting } from '../../../src/peer-routing/index.js'
import { FindNodeHandler } from '../../../src/rpc/handlers/find-node.js'
import { passthroughMapper, removePrivateAddressesMapper, removePublicAddressesMapper } from '../../../src/utils.js'
import { createPeerId } from '../../utils/create-peer-id.js'
import type { DHTMessageHandler } from '../../../src/rpc/index.js'
import type { PeerId } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { StubbedInstance } from 'sinon-ts'

const T = MessageType.FIND_NODE

Expand All @@ -24,18 +21,15 @@ describe('rpc - handlers - FindNode', () => {
let targetPeer: PeerId
let handler: DHTMessageHandler
let peerRouting: SinonStubbedInstance<PeerRouting>
let addressManager: StubbedInstance<AddressManager>

beforeEach(async () => {
peerId = await createPeerId()
sourcePeer = await createPeerId()
targetPeer = await createPeerId()
peerRouting = Sinon.createStubInstance(PeerRouting)
addressManager = stubInterface<AddressManager>()

handler = new FindNodeHandler({
peerId,
addressManager,
logger: defaultLogger()
}, {
peerRouting,
Expand All @@ -44,21 +38,33 @@ describe('rpc - handlers - FindNode', () => {
})
})

it('returns self, if asked for self', async () => {
it('returns nodes close to self but excludes self, if asked for self', async () => {
const msg: Message = {
type: T,
key: peerId.multihash.bytes,
closer: [],
providers: []
}

addressManager.getAddresses.returns([
multiaddr(`/ip4/127.0.0.1/tcp/4002/p2p/${peerId.toString()}`),
multiaddr(`/ip4/192.168.1.5/tcp/4002/p2p/${peerId.toString()}`),
multiaddr(`/ip4/221.4.67.0/tcp/4002/p2p/${peerId.toString()}`)
])
peerRouting.getCloserPeersOffline
.withArgs(peerId.multihash.bytes, peerId)
.resolves([{
id: targetPeer, // closer peer
multiaddrs: [
multiaddr('/ip4/127.0.0.1/tcp/4002'),
multiaddr('/ip4/192.168.1.5/tcp/4002'),
multiaddr('/ip4/221.4.67.0/tcp/4002')
]
}, {
id: peerId, // self peer
multiaddrs: [
multiaddr('/ip4/127.0.0.1/tcp/4002'),
multiaddr('/ip4/192.168.1.5/tcp/4002'),
multiaddr('/ip4/221.4.67.0/tcp/4002')
]
}])

const response = await handler.handle(sourcePeer, msg)
const response = await handler.handle(peerId, msg)

if (response == null) {
throw new Error('No response received from handler')
Expand All @@ -67,7 +73,8 @@ describe('rpc - handlers - FindNode', () => {
expect(response.closer).to.have.length(1)
const peer = response.closer[0]

expect(peerIdFromBytes(peer.id).toString()).to.equal(peerId.toString())
expect(peerIdFromBytes(peer.id).toString()).to.equal(targetPeer.toString())
expect(peer.multiaddrs).to.not.be.empty()
})

it('returns closer peers', async () => {
Expand Down Expand Up @@ -138,7 +145,6 @@ describe('rpc - handlers - FindNode', () => {

handler = new FindNodeHandler({
peerId,
addressManager,
logger: defaultLogger()
}, {
peerRouting,
Expand Down Expand Up @@ -181,7 +187,6 @@ describe('rpc - handlers - FindNode', () => {

handler = new FindNodeHandler({
peerId,
addressManager,
logger: defaultLogger()
}, {
peerRouting,
Expand Down
2 changes: 0 additions & 2 deletions packages/kad-dht/test/rpc/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { passthroughMapper } from '../../src/utils.js'
import { createPeerId } from '../utils/create-peer-id.js'
import type { Validators } from '../../src/index.js'
import type { Libp2pEvents, Connection, PeerId, PeerStore } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { Datastore } from 'interface-datastore'
import type { Duplex, Source } from 'it-stream-types'

Expand All @@ -45,7 +44,6 @@ describe('rpc', () => {
peerId,
datastore,
peerStore: stubInterface<PeerStore>(),
addressManager: stubInterface<AddressManager>(),
logger: defaultLogger()
}
components.peerStore = new PersistentPeerStore({
Expand Down
Loading