Skip to content

Commit

Permalink
fix: do not add peers to routing table during RPC handling (libp2p#2866)
Browse files Browse the repository at this point in the history
This can cause a race condition where two peers try to bootstrap to each other, then fail to add the other to the routing table because they wait for the other to respond to a ping first.

It also fixes a smaller bug whereby we'd try to add a peer to the routing table after they send us an RPC message but they are a DHT client - they'd not respond to the ping we send before adding them so it's a waste of resources.
  • Loading branch information
dozyio authored and acul71 committed Dec 1, 2024
1 parent 0a86f81 commit ae340f8
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 8 deletions.
6 changes: 0 additions & 6 deletions packages/kad-dht/src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ export class RPC {
const { stream, connection } = data
const peerId = connection.remotePeer

try {
await this.routingTable.add(peerId)
} catch (err: any) {
this.log.error(err)
}

const self = this // eslint-disable-line @typescript-eslint/no-this-alias

await pipe(
Expand Down
2 changes: 0 additions & 2 deletions packages/kad-dht/test/rpc/handlers/ping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { defaultLogger } from '@libp2p/logger'
import { expect } from 'aegir/chai'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { type Message, MessageType } from '../../../src/message/dht.js'
import { PingHandler } from '../../../src/rpc/handlers/ping.js'
import { createPeerId } from '../../utils/create-peer-id.js'
Expand Down Expand Up @@ -30,7 +29,6 @@ describe('rpc - handlers - Ping', () => {
it('replies with the same message', async () => {
const msg: Message = {
type: T,
key: uint8ArrayFromString('hello'),
closer: [],
providers: []
}
Expand Down

0 comments on commit ae340f8

Please sign in to comment.