@libp2p/kad-dht 12.0.15 → 12.0.16-1cd5aae11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +4 -4
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +3 -2
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/index.d.ts +34 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/network.d.ts +3 -0
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +33 -8
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-list/peer-distance-list.d.ts +13 -4
- package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -1
- package/dist/src/peer-list/peer-distance-list.js +29 -21
- package/dist/src/peer-list/peer-distance-list.js.map +1 -1
- package/dist/src/peer-routing/index.d.ts +5 -5
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +15 -24
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/query/manager.d.ts +3 -0
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/manager.js +14 -5
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query/query-path.d.ts +6 -6
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query/query-path.js +32 -20
- package/dist/src/query/query-path.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +11 -5
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +84 -42
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/k-bucket.d.ts +80 -115
- package/dist/src/routing-table/k-bucket.d.ts.map +1 -1
- package/dist/src/routing-table/k-bucket.js +165 -311
- package/dist/src/routing-table/k-bucket.js.map +1 -1
- package/dist/src/routing-table/refresh.d.ts.map +1 -1
- package/dist/src/routing-table/refresh.js +9 -4
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/package.json +14 -16
- package/src/constants.ts +1 -1
- package/src/content-routing/index.ts +3 -2
- package/src/index.ts +37 -1
- package/src/network.ts +38 -9
- package/src/peer-list/peer-distance-list.ts +36 -25
- package/src/peer-routing/index.ts +19 -28
- package/src/query/manager.ts +18 -5
- package/src/query/query-path.ts +46 -30
- package/src/routing-table/index.ts +100 -46
- package/src/routing-table/k-bucket.ts +214 -359
- package/src/routing-table/refresh.ts +10 -4
- package/dist/src/query/utils.d.ts +0 -6
- package/dist/src/query/utils.d.ts.map +0 -1
- package/dist/src/query/utils.js +0 -53
- package/dist/src/query/utils.js.map +0 -1
- package/dist/typedoc-urls.json +0 -55
- package/src/query/utils.ts +0 -64
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto';
|
|
2
|
+
import { setMaxListeners } from '@libp2p/interface';
|
|
2
3
|
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
3
4
|
import length from 'it-length';
|
|
4
5
|
import { sha256 } from 'multiformats/hashes/sha2';
|
|
@@ -104,7 +105,11 @@ export class RoutingTableRefresh {
|
|
|
104
105
|
// gen a key for the query to refresh the cpl
|
|
105
106
|
const peerId = await this._generateRandomPeerId(cpl);
|
|
106
107
|
this.log('starting refreshing cpl %s with key %p (routing table size was %s)', cpl, peerId, this.routingTable.size);
|
|
107
|
-
const
|
|
108
|
+
const signal = AbortSignal.timeout(this.refreshQueryTimeout);
|
|
109
|
+
setMaxListeners(Infinity, signal);
|
|
110
|
+
const peers = await length(this.peerRouting.getClosestPeers(peerId.toBytes(), {
|
|
111
|
+
signal
|
|
112
|
+
}));
|
|
108
113
|
this.log(`found ${peers} peers that were close to imaginary peer %p`, peerId);
|
|
109
114
|
this.log('finished refreshing cpl %s with key %p (routing table size is now %s)', cpl, peerId, this.routingTable.size);
|
|
110
115
|
}
|
|
@@ -125,7 +130,7 @@ export class RoutingTableRefresh {
|
|
|
125
130
|
}
|
|
126
131
|
const randomData = randomBytes(2);
|
|
127
132
|
const randomUint16 = (randomData[1] << 8) + randomData[0];
|
|
128
|
-
const key = await this._makePeerId(this.routingTable.kb.
|
|
133
|
+
const key = await this._makePeerId(this.routingTable.kb.localPeer.kadId, randomUint16, targetCommonPrefixLength);
|
|
129
134
|
return peerIdFromBytes(key);
|
|
130
135
|
}
|
|
131
136
|
async _makePeerId(localKadId, randomPrefix, targetCommonPrefixLength) {
|
|
@@ -185,8 +190,8 @@ export class RoutingTableRefresh {
|
|
|
185
190
|
if (this.routingTable.kb == null) {
|
|
186
191
|
return;
|
|
187
192
|
}
|
|
188
|
-
for (const {
|
|
189
|
-
const distance = uint8ArrayXor(this.routingTable.kb.
|
|
193
|
+
for (const { kadId } of this.routingTable.kb.toIterable()) {
|
|
194
|
+
const distance = uint8ArrayXor(this.routingTable.kb.localPeer.kadId, kadId);
|
|
190
195
|
let leadingZeros = 0;
|
|
191
196
|
for (const byte of distance) {
|
|
192
197
|
if (byte === 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refresh.js","sourceRoot":"","sources":["../../../src/routing-table/refresh.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,MAAM,MAAM,WAAW,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,sBAAsB,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAA;AACrF,OAAO,kBAAkB,MAAM,4BAA4B,CAAA;AAK3D;;GAEG;AACH,MAAM,wBAAwB,GAAG,EAAE,CAAA;AAcnC;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IACb,GA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AAY,GAAG,CAAC,CAAA;YAEpB,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;gBAC5B,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;oBACf,YAAY,EAAE,CAAA;gBAChB,CAAC;qBAAM,CAAC;oBACN,MAAK;gBACP,CAAC;YACH,CAAC;YAED,MAAM,YAAY,CAAA;QACpB,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.16-1cd5aae11",
|
|
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": "
|
|
61
|
-
"@libp2p/interface": "
|
|
62
|
-
"@libp2p/interface-internal": "
|
|
63
|
-
"@libp2p/peer-collections": "
|
|
64
|
-
"@libp2p/peer-id": "
|
|
65
|
-
"@libp2p/record": "
|
|
66
|
-
"@libp2p/utils": "
|
|
60
|
+
"@libp2p/crypto": "4.1.1-1cd5aae11",
|
|
61
|
+
"@libp2p/interface": "1.3.1-1cd5aae11",
|
|
62
|
+
"@libp2p/interface-internal": "1.2.1-1cd5aae11",
|
|
63
|
+
"@libp2p/peer-collections": "5.2.1-1cd5aae11",
|
|
64
|
+
"@libp2p/peer-id": "4.1.1-1cd5aae11",
|
|
65
|
+
"@libp2p/record": "4.0.1-1cd5aae11",
|
|
66
|
+
"@libp2p/utils": "5.4.1-1cd5aae11",
|
|
67
67
|
"@multiformats/multiaddr": "^12.2.1",
|
|
68
68
|
"any-signal": "^4.1.1",
|
|
69
69
|
"hashlru": "^2.3.0",
|
|
@@ -76,7 +76,6 @@
|
|
|
76
76
|
"it-parallel": "^3.0.6",
|
|
77
77
|
"it-pipe": "^3.0.1",
|
|
78
78
|
"it-protobuf-stream": "^1.1.2",
|
|
79
|
-
"it-pushable": "^3.2.3",
|
|
80
79
|
"it-take": "^3.0.4",
|
|
81
80
|
"multiformats": "^13.1.0",
|
|
82
81
|
"p-defer": "^4.0.1",
|
|
@@ -87,13 +86,13 @@
|
|
|
87
86
|
"race-signal": "^1.0.2",
|
|
88
87
|
"uint8-varint": "^2.0.4",
|
|
89
88
|
"uint8arraylist": "^2.4.8",
|
|
90
|
-
"uint8arrays": "^5.0
|
|
89
|
+
"uint8arrays": "^5.1.0"
|
|
91
90
|
},
|
|
92
91
|
"devDependencies": {
|
|
93
|
-
"@libp2p/interface-compliance-tests": "
|
|
94
|
-
"@libp2p/logger": "
|
|
95
|
-
"@libp2p/peer-id-factory": "
|
|
96
|
-
"@libp2p/peer-store": "
|
|
92
|
+
"@libp2p/interface-compliance-tests": "5.4.4-1cd5aae11",
|
|
93
|
+
"@libp2p/logger": "4.0.12-1cd5aae11",
|
|
94
|
+
"@libp2p/peer-id-factory": "4.1.1-1cd5aae11",
|
|
95
|
+
"@libp2p/peer-store": "10.0.18-1cd5aae11",
|
|
97
96
|
"@types/lodash.random": "^3.2.9",
|
|
98
97
|
"@types/lodash.range": "^3.2.9",
|
|
99
98
|
"@types/sinon": "^17.0.3",
|
|
@@ -102,7 +101,7 @@
|
|
|
102
101
|
"datastore-core": "^9.2.9",
|
|
103
102
|
"datastore-level": "^10.1.7",
|
|
104
103
|
"delay": "^6.0.0",
|
|
105
|
-
"execa": "^
|
|
104
|
+
"execa": "^9.0.2",
|
|
106
105
|
"it-all": "^3.0.4",
|
|
107
106
|
"it-filter": "^3.0.4",
|
|
108
107
|
"it-last": "^3.0.4",
|
|
@@ -115,7 +114,6 @@
|
|
|
115
114
|
"protons": "^7.5.0",
|
|
116
115
|
"sinon": "^17.0.1",
|
|
117
116
|
"sinon-ts": "^2.0.0",
|
|
118
|
-
"wherearewe": "^2.0.1",
|
|
119
117
|
"which": "^4.0.0"
|
|
120
118
|
},
|
|
121
119
|
"browser": {
|
package/src/constants.ts
CHANGED
|
@@ -50,4 +50,4 @@ export const TABLE_REFRESH_INTERVAL = 5 * minute
|
|
|
50
50
|
export const TABLE_REFRESH_QUERY_TIMEOUT = 30 * second
|
|
51
51
|
|
|
52
52
|
// When a timeout is not specified, run a query for this long
|
|
53
|
-
export const DEFAULT_QUERY_TIMEOUT =
|
|
53
|
+
export const DEFAULT_QUERY_TIMEOUT = 180 * second
|
|
@@ -58,13 +58,14 @@ export class ContentRouting {
|
|
|
58
58
|
*/
|
|
59
59
|
async * provide (key: CID, multiaddrs: Multiaddr[], options: RoutingOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
|
|
60
60
|
this.log('provide %s', key)
|
|
61
|
+
const target = key.multihash.bytes
|
|
61
62
|
|
|
62
63
|
// Add peer as provider
|
|
63
64
|
await this.providers.addProvider(key, this.components.peerId)
|
|
64
65
|
|
|
65
66
|
const msg: Partial<Message> = {
|
|
66
67
|
type: MessageType.ADD_PROVIDER,
|
|
67
|
-
key:
|
|
68
|
+
key: target,
|
|
68
69
|
providers: [
|
|
69
70
|
toPbPeerInfo({
|
|
70
71
|
id: this.components.peerId,
|
|
@@ -107,7 +108,7 @@ export class ContentRouting {
|
|
|
107
108
|
|
|
108
109
|
// Notify closest peers
|
|
109
110
|
yield * pipe(
|
|
110
|
-
this.peerRouting.getClosestPeers(
|
|
111
|
+
this.peerRouting.getClosestPeers(target, options),
|
|
111
112
|
(source) => map(source, (event) => maybeNotifyPeer(event)),
|
|
112
113
|
(source) => parallel(source, {
|
|
113
114
|
ordered: false,
|
package/src/index.ts
CHANGED
|
@@ -86,6 +86,7 @@ import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughM
|
|
|
86
86
|
import type { ProvidersInit } from './providers.js'
|
|
87
87
|
import type { Libp2pEvents, ComponentLogger, TypedEventTarget, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
|
|
88
88
|
import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
89
|
+
import type { AdaptiveTimeoutInit } from '@libp2p/utils/src/adaptive-timeout.js'
|
|
89
90
|
import type { Datastore } from 'interface-datastore'
|
|
90
91
|
import type { CID } from 'multiformats/cid'
|
|
91
92
|
import type { ProgressEvent } from 'progress-events'
|
|
@@ -300,12 +301,42 @@ export type Validators = Record<string, ValidateFn>
|
|
|
300
301
|
|
|
301
302
|
export interface KadDHTInit {
|
|
302
303
|
/**
|
|
303
|
-
* How many peers to store in each kBucket
|
|
304
|
+
* How many peers to store in each kBucket. Once there are more than this
|
|
305
|
+
* number of peers for a given prefix in a kBucket, the node will start to
|
|
306
|
+
* ping existing peers to see if they are still online - if they are offline
|
|
307
|
+
* they will be evicted and the new peer added.
|
|
304
308
|
*
|
|
305
309
|
* @default 20
|
|
306
310
|
*/
|
|
307
311
|
kBucketSize?: number
|
|
308
312
|
|
|
313
|
+
/**
|
|
314
|
+
* The threshold at which a kBucket will be split into two smaller kBuckets.
|
|
315
|
+
*
|
|
316
|
+
* KBuckets will not be split once the maximum trie depth is reached
|
|
317
|
+
* (controlled by the `prefixLength` option) so one can replicate go-libp2p's
|
|
318
|
+
* accelerated DHT client by (for example) setting `kBucketSize` to `Infinity`
|
|
319
|
+
* and `kBucketSplitThreshold` to 20.
|
|
320
|
+
*
|
|
321
|
+
* @default kBucketSize
|
|
322
|
+
*/
|
|
323
|
+
kBucketSplitThreshold?: number
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* How many bits of the KAD-ID of peers to use when creating the routing
|
|
327
|
+
* table.
|
|
328
|
+
*
|
|
329
|
+
* The routing table is a binary trie with peers stored in the leaf nodes. The
|
|
330
|
+
* larger this number gets, the taller the trie can grow and the more peers
|
|
331
|
+
* can be stored.
|
|
332
|
+
*
|
|
333
|
+
* Storing more peers means fewer lookups (and network operations) are needed
|
|
334
|
+
* to locate a certain peer, but also that more memory is consumed.
|
|
335
|
+
*
|
|
336
|
+
* @default 32
|
|
337
|
+
*/
|
|
338
|
+
prefixLength?: number
|
|
339
|
+
|
|
309
340
|
/**
|
|
310
341
|
* If true, only ever be a DHT client. If false, be a DHT client until told
|
|
311
342
|
* to be a DHT server via `setMode`.
|
|
@@ -405,6 +436,11 @@ export interface KadDHTInit {
|
|
|
405
436
|
* with this filter.
|
|
406
437
|
*/
|
|
407
438
|
peerInfoMapper?(peer: PeerInfo): PeerInfo
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Dynamic network timeout settings for sending messages to peers
|
|
442
|
+
*/
|
|
443
|
+
networkDialTimeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
|
|
408
444
|
}
|
|
409
445
|
|
|
410
446
|
export interface KadDHTComponents {
|
package/src/network.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TypedEventEmitter } from '@libp2p/interface'
|
|
2
2
|
import { Libp2pRecord } from '@libp2p/record'
|
|
3
|
+
import { AdaptiveTimeout, type AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'
|
|
3
4
|
import { pbStream } from 'it-protobuf-stream'
|
|
4
5
|
import { CodeError } from 'protons-runtime'
|
|
5
6
|
import { Message } from './message/dht.js'
|
|
@@ -16,6 +17,7 @@ import type { AbortOptions, Logger, Stream, PeerId, PeerInfo, Startable, Routing
|
|
|
16
17
|
export interface NetworkInit {
|
|
17
18
|
protocol: string
|
|
18
19
|
logPrefix: string
|
|
20
|
+
timeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
interface NetworkEvents {
|
|
@@ -30,6 +32,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
30
32
|
private readonly protocol: string
|
|
31
33
|
private running: boolean
|
|
32
34
|
private readonly components: KadDHTComponents
|
|
35
|
+
private readonly timeout: AdaptiveTimeout
|
|
33
36
|
|
|
34
37
|
/**
|
|
35
38
|
* Create a new network
|
|
@@ -42,6 +45,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
42
45
|
this.log = components.logger.forComponent(`${init.logPrefix}:network`)
|
|
43
46
|
this.running = false
|
|
44
47
|
this.protocol = protocol
|
|
48
|
+
this.timeout = new AdaptiveTimeout({
|
|
49
|
+
...(init.timeout ?? {}),
|
|
50
|
+
metrics: components.metrics,
|
|
51
|
+
metricName: `${init.logPrefix.replaceAll(':', '_')}_network_message_send_times_milliseconds`
|
|
52
|
+
})
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
/**
|
|
@@ -88,13 +96,24 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
88
96
|
yield sendQueryEvent({ to, type }, options)
|
|
89
97
|
|
|
90
98
|
let stream: Stream | undefined
|
|
99
|
+
const signal = this.timeout.getTimeoutSignal(options)
|
|
100
|
+
|
|
101
|
+
options = {
|
|
102
|
+
...options,
|
|
103
|
+
signal
|
|
104
|
+
}
|
|
91
105
|
|
|
92
106
|
try {
|
|
93
107
|
const connection = await this.components.connectionManager.openConnection(to, options)
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
stream = await connection.newStream(this.protocol, options)
|
|
96
109
|
const response = await this._writeReadMessage(stream, msg, options)
|
|
97
110
|
|
|
111
|
+
stream.close(options)
|
|
112
|
+
.catch(err => {
|
|
113
|
+
this.log.error('error closing stream to %p', to, err)
|
|
114
|
+
stream?.abort(err)
|
|
115
|
+
})
|
|
116
|
+
|
|
98
117
|
yield peerResponseEvent({
|
|
99
118
|
from: to,
|
|
100
119
|
messageType: response.type,
|
|
@@ -103,12 +122,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
103
122
|
record: response.record == null ? undefined : Libp2pRecord.deserialize(response.record)
|
|
104
123
|
}, options)
|
|
105
124
|
} catch (err: any) {
|
|
125
|
+
stream?.abort(err)
|
|
106
126
|
this.log.error('could not send %s to %p', msg.type, to, err)
|
|
107
127
|
yield queryErrorEvent({ from: to, error: err }, options)
|
|
108
128
|
} finally {
|
|
109
|
-
|
|
110
|
-
await stream.close()
|
|
111
|
-
}
|
|
129
|
+
this.timeout.cleanUp(signal)
|
|
112
130
|
}
|
|
113
131
|
}
|
|
114
132
|
|
|
@@ -131,20 +149,31 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
131
149
|
yield sendQueryEvent({ to, type }, options)
|
|
132
150
|
|
|
133
151
|
let stream: Stream | undefined
|
|
152
|
+
const signal = this.timeout.getTimeoutSignal(options)
|
|
153
|
+
|
|
154
|
+
options = {
|
|
155
|
+
...options,
|
|
156
|
+
signal
|
|
157
|
+
}
|
|
134
158
|
|
|
135
159
|
try {
|
|
136
160
|
const connection = await this.components.connectionManager.openConnection(to, options)
|
|
137
|
-
|
|
161
|
+
stream = await connection.newStream(this.protocol, options)
|
|
138
162
|
|
|
139
163
|
await this._writeMessage(stream, msg, options)
|
|
140
164
|
|
|
165
|
+
stream.close(options)
|
|
166
|
+
.catch(err => {
|
|
167
|
+
this.log.error('error closing stream to %p', to, err)
|
|
168
|
+
stream?.abort(err)
|
|
169
|
+
})
|
|
170
|
+
|
|
141
171
|
yield peerResponseEvent({ from: to, messageType: type }, options)
|
|
142
172
|
} catch (err: any) {
|
|
173
|
+
stream?.abort(err)
|
|
143
174
|
yield queryErrorEvent({ from: to, error: err }, options)
|
|
144
175
|
} finally {
|
|
145
|
-
|
|
146
|
-
await stream.close()
|
|
147
|
-
}
|
|
176
|
+
this.timeout.cleanUp(signal)
|
|
148
177
|
}
|
|
149
178
|
}
|
|
150
179
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
|
|
2
1
|
import { xor as uint8ArrayXor } from 'uint8arrays/xor'
|
|
3
|
-
import
|
|
4
|
-
import
|
|
2
|
+
import { xorCompare as uint8ArrayXorCompare } from 'uint8arrays/xor-compare'
|
|
3
|
+
import { convertPeerId } from '../utils.js'
|
|
4
|
+
import type { PeerId, PeerInfo } from '@libp2p/interface'
|
|
5
5
|
|
|
6
6
|
interface PeerDistance {
|
|
7
|
-
|
|
7
|
+
peer: PeerInfo
|
|
8
8
|
distance: Uint8Array
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -38,28 +38,36 @@ export class PeerDistanceList {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* The
|
|
41
|
+
* The peers in the list, in order of distance from the origin key
|
|
42
42
|
*/
|
|
43
|
-
get peers ():
|
|
44
|
-
return this.peerDistances.map(pd => pd.
|
|
43
|
+
get peers (): PeerInfo[] {
|
|
44
|
+
return this.peerDistances.map(pd => pd.peer)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Add a peerId to the list.
|
|
49
49
|
*/
|
|
50
|
-
async add (
|
|
51
|
-
|
|
50
|
+
async add (peer: PeerInfo): Promise<void> {
|
|
51
|
+
const dhtKey = await convertPeerId(peer.id)
|
|
52
|
+
|
|
53
|
+
this.addWitKadId(peer, dhtKey)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Add a peerId to the list.
|
|
58
|
+
*/
|
|
59
|
+
addWitKadId (peer: PeerInfo, kadId: Uint8Array): void {
|
|
60
|
+
if (this.peerDistances.find(pd => pd.peer.id.equals(peer.id)) != null) {
|
|
52
61
|
return
|
|
53
62
|
}
|
|
54
63
|
|
|
55
|
-
const dhtKey = await utils.convertPeerId(peerId)
|
|
56
64
|
const el = {
|
|
57
|
-
|
|
58
|
-
distance: uint8ArrayXor(this.originDhtKey,
|
|
65
|
+
peer,
|
|
66
|
+
distance: uint8ArrayXor(this.originDhtKey, kadId)
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
this.peerDistances.push(el)
|
|
62
|
-
this.peerDistances.sort((a, b) =>
|
|
70
|
+
this.peerDistances.sort((a, b) => uint8ArrayXorCompare(a.distance, b.distance))
|
|
63
71
|
this.peerDistances = this.peerDistances.slice(0, this.capacity)
|
|
64
72
|
}
|
|
65
73
|
|
|
@@ -67,26 +75,29 @@ export class PeerDistanceList {
|
|
|
67
75
|
* Indicates whether any of the peerIds passed as a parameter are closer
|
|
68
76
|
* to the origin key than the furthest peerId in the PeerDistanceList.
|
|
69
77
|
*/
|
|
70
|
-
async
|
|
71
|
-
if (peerIds.length === 0) {
|
|
72
|
-
return false
|
|
73
|
-
}
|
|
74
|
-
|
|
78
|
+
async isCloser (peerId: PeerId): Promise<boolean> {
|
|
75
79
|
if (this.length === 0) {
|
|
76
80
|
return true
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
const
|
|
83
|
+
const dhtKey = await convertPeerId(peerId)
|
|
84
|
+
const dhtKeyXor = uint8ArrayXor(dhtKey, this.originDhtKey)
|
|
80
85
|
const furthestDistance = this.peerDistances[this.peerDistances.length - 1].distance
|
|
81
86
|
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
return uint8ArrayXorCompare(dhtKeyXor, furthestDistance) === -1
|
|
88
|
+
}
|
|
84
89
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Indicates whether any of the peerIds passed as a parameter are closer
|
|
92
|
+
* to the origin key than the furthest peerId in the PeerDistanceList.
|
|
93
|
+
*/
|
|
94
|
+
async anyCloser (peerIds: PeerId[]): Promise<boolean> {
|
|
95
|
+
if (peerIds.length === 0) {
|
|
96
|
+
return false
|
|
88
97
|
}
|
|
89
98
|
|
|
90
|
-
return
|
|
99
|
+
return Promise.any(
|
|
100
|
+
peerIds.map(async peerId => this.isCloser(peerId))
|
|
101
|
+
)
|
|
91
102
|
}
|
|
92
103
|
}
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
} from '../query/events.js'
|
|
13
13
|
import { verifyRecord } from '../record/validators.js'
|
|
14
14
|
import * as utils from '../utils.js'
|
|
15
|
-
import type { KadDHTComponents, DHTRecord,
|
|
15
|
+
import type { KadDHTComponents, DHTRecord, FinalPeerEvent, QueryEvent, Validators } from '../index.js'
|
|
16
16
|
import type { Message } from '../message/dht.js'
|
|
17
17
|
import type { Network } from '../network.js'
|
|
18
18
|
import type { QueryManager, QueryOptions } from '../query/manager.js'
|
|
@@ -195,17 +195,17 @@ export class PeerRouting {
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
/**
|
|
198
|
-
* Kademlia '
|
|
199
|
-
*
|
|
198
|
+
* Kademlia 'FIND_NODE' operation on a key, which could be the bytes from
|
|
199
|
+
* a multihash or a peer ID
|
|
200
200
|
*/
|
|
201
|
-
async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<
|
|
201
|
+
async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
|
|
202
202
|
this.log('getClosestPeers to %b', key)
|
|
203
|
-
const
|
|
204
|
-
const tablePeers = this.routingTable.closestPeers(
|
|
203
|
+
const kadId = await utils.convertBuffer(key)
|
|
204
|
+
const tablePeers = this.routingTable.closestPeers(kadId)
|
|
205
205
|
const self = this // eslint-disable-line @typescript-eslint/no-this-alias
|
|
206
206
|
|
|
207
|
-
const peers = new PeerDistanceList(
|
|
208
|
-
await Promise.all(tablePeers.map(async peer => { await peers.add(peer) }))
|
|
207
|
+
const peers = new PeerDistanceList(kadId, this.routingTable.kBucketSize)
|
|
208
|
+
await Promise.all(tablePeers.map(async peer => { await peers.add({ id: peer, multiaddrs: [] }) }))
|
|
209
209
|
|
|
210
210
|
const getCloserPeersQuery: QueryFunc = async function * ({ peer, signal }) {
|
|
211
211
|
self.log('closerPeersSingle %s from %p', uint8ArrayToString(key, 'base32'), peer)
|
|
@@ -221,31 +221,22 @@ export class PeerRouting {
|
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
for await (const event of this.queryManager.run(key, getCloserPeersQuery, options)) {
|
|
224
|
-
yield event
|
|
225
|
-
|
|
226
224
|
if (event.name === 'PEER_RESPONSE') {
|
|
227
|
-
await Promise.all(event.closer.map(async peerData => {
|
|
225
|
+
await Promise.all(event.closer.map(async peerData => {
|
|
226
|
+
await peers.add(peerData)
|
|
227
|
+
}))
|
|
228
228
|
}
|
|
229
|
+
|
|
230
|
+
yield event
|
|
229
231
|
}
|
|
230
232
|
|
|
231
233
|
this.log('found %d peers close to %b', peers.length, key)
|
|
232
234
|
|
|
233
|
-
for (const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
from: this.peerId,
|
|
239
|
-
peer: {
|
|
240
|
-
id: peerId,
|
|
241
|
-
multiaddrs: peer.addresses.map(({ multiaddr }) => multiaddr)
|
|
242
|
-
}
|
|
243
|
-
}, options)
|
|
244
|
-
} catch (err: any) {
|
|
245
|
-
if (err.code !== 'ERR_NOT_FOUND') {
|
|
246
|
-
throw err
|
|
247
|
-
}
|
|
248
|
-
}
|
|
235
|
+
for (const peer of peers.peers) {
|
|
236
|
+
yield finalPeerEvent({
|
|
237
|
+
from: this.peerId,
|
|
238
|
+
peer
|
|
239
|
+
}, options)
|
|
249
240
|
}
|
|
250
241
|
}
|
|
251
242
|
|
|
@@ -255,7 +246,7 @@ export class PeerRouting {
|
|
|
255
246
|
*
|
|
256
247
|
* Note: The peerStore is updated with new addresses found for the given peer.
|
|
257
248
|
*/
|
|
258
|
-
async * getValueOrPeers (peer: PeerId, key: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<
|
|
249
|
+
async * getValueOrPeers (peer: PeerId, key: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<QueryEvent> {
|
|
259
250
|
for await (const event of this._getValueSingle(peer, key, options)) {
|
|
260
251
|
if (event.name === 'PEER_RESPONSE') {
|
|
261
252
|
if (event.record != null) {
|
package/src/query/manager.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { setMaxListeners } from '@libp2p/interface'
|
|
2
2
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
3
3
|
import { anySignal } from 'any-signal'
|
|
4
4
|
import merge from 'it-merge'
|
|
@@ -13,6 +13,7 @@ import type { QueryFunc } from './types.js'
|
|
|
13
13
|
import type { QueryEvent } from '../index.js'
|
|
14
14
|
import type { RoutingTable } from '../routing-table/index.js'
|
|
15
15
|
import type { ComponentLogger, Metric, Metrics, PeerId, RoutingOptions, Startable } from '@libp2p/interface'
|
|
16
|
+
import type { ConnectionManager } from '@libp2p/interface-internal'
|
|
16
17
|
import type { DeferredPromise } from 'p-defer'
|
|
17
18
|
|
|
18
19
|
export interface CleanUpEvents {
|
|
@@ -31,6 +32,7 @@ export interface QueryManagerComponents {
|
|
|
31
32
|
peerId: PeerId
|
|
32
33
|
metrics?: Metrics
|
|
33
34
|
logger: ComponentLogger
|
|
35
|
+
connectionManager: ConnectionManager
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
export interface QueryOptions extends RoutingOptions {
|
|
@@ -53,6 +55,7 @@ export class QueryManager implements Startable {
|
|
|
53
55
|
private queries: number
|
|
54
56
|
private readonly logger: ComponentLogger
|
|
55
57
|
private readonly peerId: PeerId
|
|
58
|
+
private readonly connectionManager: ConnectionManager
|
|
56
59
|
private readonly routingTable: RoutingTable
|
|
57
60
|
private initialQuerySelfHasRun?: DeferredPromise<void>
|
|
58
61
|
private readonly logPrefix: string
|
|
@@ -73,6 +76,7 @@ export class QueryManager implements Startable {
|
|
|
73
76
|
this.routingTable = init.routingTable
|
|
74
77
|
this.logger = components.logger
|
|
75
78
|
this.peerId = components.peerId
|
|
79
|
+
this.connectionManager = components.connectionManager
|
|
76
80
|
|
|
77
81
|
if (components.metrics != null) {
|
|
78
82
|
this.metrics = {
|
|
@@ -151,7 +155,6 @@ export class QueryManager implements Startable {
|
|
|
151
155
|
|
|
152
156
|
// query a subset of peers up to `kBucketSize / 2` in length
|
|
153
157
|
const startTime = Date.now()
|
|
154
|
-
const cleanUp = new TypedEventEmitter<CleanUpEvents>()
|
|
155
158
|
let queryFinished = false
|
|
156
159
|
|
|
157
160
|
try {
|
|
@@ -190,11 +193,11 @@ export class QueryManager implements Startable {
|
|
|
190
193
|
pathIndex: index,
|
|
191
194
|
numPaths: peersToQuery.length,
|
|
192
195
|
alpha: this.alpha,
|
|
193
|
-
cleanUp,
|
|
194
196
|
queryFuncTimeout: options.queryFuncTimeout,
|
|
195
197
|
log,
|
|
196
198
|
peersSeen,
|
|
197
|
-
onProgress: options.onProgress
|
|
199
|
+
onProgress: options.onProgress,
|
|
200
|
+
connectionManager: this.connectionManager
|
|
198
201
|
})
|
|
199
202
|
})
|
|
200
203
|
|
|
@@ -204,6 +207,17 @@ export class QueryManager implements Startable {
|
|
|
204
207
|
log.error('query error', event.error)
|
|
205
208
|
}
|
|
206
209
|
|
|
210
|
+
if (event.name === 'PEER_RESPONSE') {
|
|
211
|
+
for (const peer of [...event.closer, ...event.providers]) {
|
|
212
|
+
// eslint-disable-next-line max-depth
|
|
213
|
+
if (!(await this.connectionManager.isDialable(peer.multiaddrs))) {
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
await this.routingTable.add(peer.id)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
207
221
|
yield event
|
|
208
222
|
}
|
|
209
223
|
|
|
@@ -229,7 +243,6 @@ export class QueryManager implements Startable {
|
|
|
229
243
|
stopQueryTimer()
|
|
230
244
|
}
|
|
231
245
|
|
|
232
|
-
cleanUp.dispatchEvent(new CustomEvent('cleanup'))
|
|
233
246
|
log('query:done in %dms', Date.now() - startTime)
|
|
234
247
|
}
|
|
235
248
|
}
|