Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit e7ccf13

Browse files
authored
fix: ping peers with correct protocol (#344)
Fixes a bug in the routing table peer eviction routine whereby the wrong protocol was being used.
1 parent 52601f8 commit e7ccf13

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export const hour = 60 * minute
1111

1212
export const MAX_RECORD_AGE = 36 * hour
1313

14+
export const LAN_PREFIX = '/lan'
15+
16+
export const PROTOCOL_PREFIX = '/ipfs'
17+
1418
export const PROTOCOL_DHT = '/kad/1.0.0'
1519

1620
export const RECORD_KEY_PREFIX = '/dht/record'

src/kad-dht.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { KadDHTInit } from './index.js'
2525
import { validators as recordValidators } from '@libp2p/record/validators'
2626
import { selectors as recordSelectors } from '@libp2p/record/selectors'
2727
import { symbol } from '@libp2p/interfaces/peer-discovery'
28+
import { PROTOCOL_DHT, PROTOCOL_PREFIX, LAN_PREFIX } from './constants.js'
2829

2930
export interface SingleKadDHTInit extends KadDHTInit {
3031
/**
@@ -81,14 +82,15 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
8182
this.running = false
8283
this.lan = Boolean(lan)
8384
this.log = logger(`libp2p:kad-dht:${lan === true ? 'lan' : 'wan'}`)
84-
this.protocol = `${protocolPrefix ?? '/ipfs'}${lan === true ? '/lan' : ''}/kad/1.0.0`
85+
this.protocol = `${protocolPrefix ?? PROTOCOL_PREFIX}${lan === true ? LAN_PREFIX : ''}${PROTOCOL_DHT}`
8586
this.kBucketSize = kBucketSize ?? 20
8687
this.clientMode = clientMode ?? true
8788
this.routingTable = new RoutingTable({
8889
kBucketSize,
8990
lan: this.lan,
9091
pingTimeout,
91-
pingConcurrency
92+
pingConcurrency,
93+
protocol: this.protocol
9294
})
9395

9496
this.providers = new Providers()

src/routing-table/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import KBuck from 'k-bucket'
33
import * as utils from '../utils.js'
44
import Queue from 'p-queue'
5-
import { PROTOCOL_DHT } from '../constants.js'
65
import { TimeoutController } from 'timeout-abort-controller'
76
import { logger } from '@libp2p/logger'
87
import type { PeerId } from '@libp2p/interfaces/peer-id'
@@ -42,6 +41,7 @@ const METRIC_PING_RUNNING = 'ping-running'
4241

4342
export interface RoutingTableInit {
4443
lan: boolean
44+
protocol: string
4545
kBucketSize?: number
4646
pingTimeout?: number
4747
pingConcurrency?: number
@@ -62,16 +62,18 @@ export class RoutingTable implements Startable, Initializable {
6262
private readonly pingTimeout: number
6363
private readonly pingConcurrency: number
6464
private running: boolean
65+
private readonly protocol: string
6566

6667
constructor (init: RoutingTableInit) {
67-
const { kBucketSize, pingTimeout, lan, pingConcurrency } = init
68+
const { kBucketSize, pingTimeout, lan, pingConcurrency, protocol } = init
6869

6970
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:routing-table`)
7071
this.kBucketSize = kBucketSize ?? 20
7172
this.pingTimeout = pingTimeout ?? 10000
7273
this.pingConcurrency = pingConcurrency ?? 10
7374
this.lan = lan
7475
this.running = false
76+
this.protocol = protocol
7577

7678
const updatePingQueueSizeMetric = () => {
7779
this.components.getMetrics()?.updateComponentMetric({
@@ -156,7 +158,7 @@ export class RoutingTable implements Startable, Initializable {
156158

157159
this.log('pinging old contact %p', oldContact.peer)
158160
const connection = await this.components.getConnectionManager().openConnection(oldContact.peer, options)
159-
const { stream } = await connection.newStream(PROTOCOL_DHT, options)
161+
const { stream } = await connection.newStream(this.protocol, options)
160162
stream.close()
161163
responded++
162164
} catch (err: any) {

test/generate-peers/generate-peers.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ describe.skip('generate peers', function () {
5858
})
5959
const table = new RoutingTable({
6060
kBucketSize: 20,
61-
lan: false
61+
lan: false,
62+
protocol: '/ipfs/kad/1.0.0'
6263
})
6364
table.init(components)
6465
refresh = new RoutingTableRefresh({

test/routing-table.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ describe('Routing Table', () => {
2424
})
2525

2626
table = new RoutingTable({
27-
lan: false
27+
lan: false,
28+
protocol: PROTOCOL_DHT
2829
})
2930
table.init(components)
3031
await table.start()

0 commit comments

Comments
 (0)