@libp2p/kad-dht 13.1.2-32c176fd5 → 13.1.2-661d6586a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. package/dist/index.min.js +2 -2
  2. package/dist/src/index.d.ts +26 -2
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/index.js.map +1 -1
  5. package/dist/src/kad-dht.d.ts.map +1 -1
  6. package/dist/src/kad-dht.js +25 -13
  7. package/dist/src/kad-dht.js.map +1 -1
  8. package/dist/src/network.d.ts +1 -1
  9. package/dist/src/network.d.ts.map +1 -1
  10. package/dist/src/network.js +1 -3
  11. package/dist/src/network.js.map +1 -1
  12. package/dist/src/query-self.d.ts.map +1 -1
  13. package/dist/src/query-self.js +13 -6
  14. package/dist/src/query-self.js.map +1 -1
  15. package/dist/src/routing-table/index.d.ts +53 -30
  16. package/dist/src/routing-table/index.d.ts.map +1 -1
  17. package/dist/src/routing-table/index.js +279 -167
  18. package/dist/src/routing-table/index.js.map +1 -1
  19. package/dist/src/routing-table/k-bucket.d.ts +67 -21
  20. package/dist/src/routing-table/k-bucket.d.ts.map +1 -1
  21. package/dist/src/routing-table/k-bucket.js +128 -60
  22. package/dist/src/routing-table/k-bucket.js.map +1 -1
  23. package/dist/src/routing-table/refresh.d.ts.map +1 -1
  24. package/dist/src/routing-table/refresh.js +4 -1
  25. package/dist/src/routing-table/refresh.js.map +1 -1
  26. package/dist/src/rpc/index.d.ts.map +1 -1
  27. package/dist/src/rpc/index.js +3 -7
  28. package/dist/src/rpc/index.js.map +1 -1
  29. package/package.json +11 -12
  30. package/src/index.ts +30 -2
  31. package/src/kad-dht.ts +29 -15
  32. package/src/network.ts +1 -4
  33. package/src/query-self.ts +14 -6
  34. package/src/routing-table/index.ts +318 -199
  35. package/src/routing-table/k-bucket.ts +203 -74
  36. package/src/routing-table/refresh.ts +5 -1
  37. package/src/rpc/index.ts +4 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/kad-dht",
3
- "version": "13.1.2-32c176fd5",
3
+ "version": "13.1.2-661d6586a",
4
4
  "description": "JavaScript implementation of the Kad-DHT for libp2p",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht#readme",
@@ -57,13 +57,13 @@
57
57
  "doc-check": "aegir doc-check"
58
58
  },
59
59
  "dependencies": {
60
- "@libp2p/crypto": "5.0.4-32c176fd5",
61
- "@libp2p/interface": "2.1.2-32c176fd5",
62
- "@libp2p/interface-internal": "2.0.6-32c176fd5",
63
- "@libp2p/peer-collections": "6.0.6-32c176fd5",
64
- "@libp2p/peer-id": "5.0.4-32c176fd5",
65
- "@libp2p/record": "4.0.4-32c176fd5",
66
- "@libp2p/utils": "6.0.6-32c176fd5",
60
+ "@libp2p/crypto": "5.0.4-661d6586a",
61
+ "@libp2p/interface": "2.1.2-661d6586a",
62
+ "@libp2p/interface-internal": "2.0.6-661d6586a",
63
+ "@libp2p/peer-collections": "6.0.6-661d6586a",
64
+ "@libp2p/peer-id": "5.0.4-661d6586a",
65
+ "@libp2p/record": "4.0.4-661d6586a",
66
+ "@libp2p/utils": "6.0.6-661d6586a",
67
67
  "@multiformats/multiaddr": "^12.2.3",
68
68
  "any-signal": "^4.1.1",
69
69
  "hashlru": "^2.3.0",
@@ -89,9 +89,9 @@
89
89
  "uint8arrays": "^5.1.0"
90
90
  },
91
91
  "devDependencies": {
92
- "@libp2p/interface-compliance-tests": "6.1.4-32c176fd5",
93
- "@libp2p/logger": "5.1.0-32c176fd5",
94
- "@libp2p/peer-store": "11.0.6-32c176fd5",
92
+ "@libp2p/interface-compliance-tests": "6.1.4-661d6586a",
93
+ "@libp2p/logger": "5.1.0-661d6586a",
94
+ "@libp2p/peer-store": "11.0.6-661d6586a",
95
95
  "@types/lodash.random": "^3.2.9",
96
96
  "@types/lodash.range": "^3.2.9",
97
97
  "@types/sinon": "^17.0.3",
@@ -109,7 +109,6 @@
109
109
  "lodash.random": "^3.2.0",
110
110
  "lodash.range": "^3.2.0",
111
111
  "p-retry": "^6.2.0",
112
- "p-wait-for": "^5.0.2",
113
112
  "protons": "^7.5.0",
114
113
  "sinon": "^18.0.0",
115
114
  "sinon-ts": "^2.0.0",
package/src/index.ts CHANGED
@@ -400,7 +400,7 @@ export interface KadDHTInit {
400
400
  * Settings for how long to wait in ms when pinging DHT peers to decide if
401
401
  * they should be evicted from the routing table or not.
402
402
  */
403
- pingTimeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
403
+ pingOldContactTimeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
404
404
 
405
405
  /**
406
406
  * How many peers to ping in parallel when deciding if they should
@@ -408,7 +408,35 @@ export interface KadDHTInit {
408
408
  *
409
409
  * @default 10
410
410
  */
411
- pingConcurrency?: number
411
+ pingOldContactConcurrency?: number
412
+
413
+ /**
414
+ * How long the queue to ping peers is allowed to grow
415
+ *
416
+ * @default 100
417
+ */
418
+ pingOldContactMaxQueueSize?: number
419
+
420
+ /**
421
+ * Settings for how long to wait in ms when pinging DHT peers to decide if
422
+ * they should be added to the routing table or not.
423
+ */
424
+ pingNewContactTimeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
425
+
426
+ /**
427
+ * How many peers to ping in parallel when deciding if they should be added to
428
+ * the routing table or not
429
+ *
430
+ * @default 10
431
+ */
432
+ pingNewContactConcurrency?: number
433
+
434
+ /**
435
+ * How long the queue to ping peers is allowed to grow
436
+ *
437
+ * @default 100
438
+ */
439
+ pingNewContactMaxQueueSize?: number
412
440
 
413
441
  /**
414
442
  * How many parallel incoming streams to allow on the DHT protocol per
package/src/kad-dht.ts CHANGED
@@ -138,8 +138,6 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
138
138
  querySelfInterval,
139
139
  protocol,
140
140
  logPrefix,
141
- pingTimeout,
142
- pingConcurrency,
143
141
  maxInboundStreams,
144
142
  maxOutboundStreams,
145
143
  providers: providersInit
@@ -156,15 +154,6 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
156
154
  this.maxInboundStreams = maxInboundStreams ?? DEFAULT_MAX_INBOUND_STREAMS
157
155
  this.maxOutboundStreams = maxOutboundStreams ?? DEFAULT_MAX_OUTBOUND_STREAMS
158
156
  this.peerInfoMapper = init.peerInfoMapper ?? removePrivateAddressesMapper
159
- this.routingTable = new RoutingTable(components, {
160
- kBucketSize,
161
- pingTimeout,
162
- pingConcurrency,
163
- protocol: this.protocol,
164
- logPrefix: loggingPrefix,
165
- prefixLength: init.prefixLength,
166
- splitThreshold: init.kBucketSplitThreshold
167
- })
168
157
 
169
158
  this.providers = new Providers(components, providersInit ?? {})
170
159
 
@@ -181,6 +170,21 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
181
170
  logPrefix: loggingPrefix
182
171
  })
183
172
 
173
+ this.routingTable = new RoutingTable(components, {
174
+ kBucketSize,
175
+ pingOldContactTimeout: init.pingOldContactTimeout,
176
+ pingOldContactConcurrency: init.pingOldContactConcurrency,
177
+ pingOldContactMaxQueueSize: init.pingOldContactMaxQueueSize,
178
+ pingNewContactTimeout: init.pingNewContactTimeout,
179
+ pingNewContactConcurrency: init.pingNewContactConcurrency,
180
+ pingNewContactMaxQueueSize: init.pingNewContactMaxQueueSize,
181
+ protocol: this.protocol,
182
+ logPrefix: loggingPrefix,
183
+ prefixLength: init.prefixLength,
184
+ splitThreshold: init.kBucketSplitThreshold,
185
+ network: this.network
186
+ })
187
+
184
188
  // all queries should wait for the initial query-self query to run so we have
185
189
  // some peers and don't force consumers to use arbitrary timeouts
186
190
  const initialQuerySelfHasRun = pDefer<any>()
@@ -376,11 +380,17 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
376
380
 
377
381
  await this.components.registrar.unhandle(this.protocol)
378
382
 
383
+ // check again after async work
384
+ if (mode === this.getMode() && !force) {
385
+ this.log('already in %s mode', mode)
386
+ return
387
+ }
388
+
379
389
  if (mode === 'client') {
380
- this.log('enabling client mode')
390
+ this.log('enabling client mode while in %s mode', this.getMode())
381
391
  this.clientMode = true
382
392
  } else {
383
- this.log('enabling server mode')
393
+ this.log('enabling server mode while in %s mode', this.getMode())
384
394
  this.clientMode = false
385
395
  await this.components.registrar.handle(this.protocol, this.rpc.onIncomingStream.bind(this.rpc), {
386
396
  maxInboundStreams: this.maxInboundStreams,
@@ -399,14 +409,18 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
399
409
  await this.setMode(this.clientMode ? 'client' : 'server', true)
400
410
 
401
411
  await start(
402
- this.querySelf,
412
+ this.routingTable,
403
413
  this.providers,
404
414
  this.queryManager,
405
415
  this.network,
406
- this.routingTable,
407
416
  this.topologyListener,
408
417
  this.routingTableRefresh
409
418
  )
419
+
420
+ // Query self after other components are configured
421
+ await start(
422
+ this.querySelf
423
+ )
410
424
  }
411
425
 
412
426
  /**
package/src/network.ts CHANGED
@@ -85,7 +85,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
85
85
  }
86
86
 
87
87
  /**
88
- * Send a request and record RTT for latency measurements
88
+ * Send a request and read a response
89
89
  */
90
90
  async * sendRequest (to: PeerId, msg: Partial<Message>, options: RoutingOptions = {}): AsyncGenerator<QueryEvent> {
91
91
  if (!this.running) {
@@ -204,7 +204,6 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
204
204
  async _writeMessage (stream: Stream, msg: Partial<Message>, options: AbortOptions): Promise<void> {
205
205
  const pb = pbStream(stream)
206
206
  await pb.write(msg, Message, options)
207
- await pb.unwrap().close(options)
208
207
  }
209
208
 
210
209
  /**
@@ -219,8 +218,6 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
219
218
 
220
219
  const message = await pb.read(Message, options)
221
220
 
222
- await pb.unwrap().close(options)
223
-
224
221
  // tell any listeners about new peers we've seen
225
222
  message.closer.forEach(peerData => {
226
223
  this.safeDispatchEvent<PeerInfo>('peer', {
package/src/query-self.ts CHANGED
@@ -106,19 +106,27 @@ export class QuerySelf implements Startable {
106
106
 
107
107
  if (this.started) {
108
108
  this.controller = new AbortController()
109
- const timeoutSignal = AbortSignal.timeout(this.queryTimeout)
110
- const signal = anySignal([this.controller.signal, timeoutSignal])
109
+ const signals = [this.controller.signal]
111
110
 
112
- // this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
113
- setMaxListeners(Infinity, signal, this.controller.signal, timeoutSignal)
111
+ // add a shorter timeout if we've already run our initial self query
112
+ if (this.initialQuerySelfHasRun == null) {
113
+ const timeoutSignal = AbortSignal.timeout(this.queryTimeout)
114
+ setMaxListeners(Infinity, timeoutSignal)
115
+ signals.push(timeoutSignal)
116
+ }
117
+
118
+ const signal = anySignal(signals)
119
+ setMaxListeners(Infinity, signal, this.controller.signal)
114
120
 
115
121
  try {
116
122
  if (this.routingTable.size === 0) {
117
123
  this.log('routing table was empty, waiting for some peers before running query')
118
- // wait to discover at least one DHT peer
124
+ // wait to discover at least one DHT peer that isn't us
119
125
  await pEvent(this.routingTable, 'peer:add', {
120
- signal
126
+ signal,
127
+ filter: (event) => !this.peerId.equals(event.detail)
121
128
  })
129
+ this.log('routing table has peers, continuing with query')
122
130
  }
123
131
 
124
132
  this.log('run self-query, look for %d peers timing out after %dms', this.count, this.queryTimeout)