@libp2p/kad-dht 15.1.2-b32bc8406 → 15.1.2-bb4ad31db
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 +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +9 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/kad-dht.d.ts +2 -1
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +3 -6
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/network.d.ts +1 -1
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +2 -1
- package/dist/src/network.js.map +1 -1
- package/dist/src/providers.d.ts +0 -3
- package/dist/src/providers.d.ts.map +1 -1
- package/dist/src/providers.js +9 -29
- package/dist/src/providers.js.map +1 -1
- package/dist/src/query/manager.js +1 -1
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query-self.js +1 -1
- package/dist/src/query-self.js.map +1 -1
- package/dist/src/reprovider.d.ts +1 -4
- package/dist/src/reprovider.d.ts.map +1 -1
- package/dist/src/reprovider.js +3 -7
- package/dist/src/reprovider.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +2 -1
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +53 -28
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/k-bucket.d.ts +3 -0
- package/dist/src/routing-table/k-bucket.d.ts.map +1 -1
- package/dist/src/routing-table/k-bucket.js +10 -0
- package/dist/src/routing-table/k-bucket.js.map +1 -1
- package/dist/src/routing-table/refresh.js +1 -1
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/dist/src/rpc/index.d.ts +2 -1
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +50 -19
- package/dist/src/rpc/index.js.map +1 -1
- package/dist/src/topology-listener.d.ts +1 -1
- package/dist/src/topology-listener.d.ts.map +1 -1
- package/dist/src/topology-listener.js +1 -1
- package/dist/src/topology-listener.js.map +1 -1
- package/package.json +14 -14
- package/src/index.ts +10 -1
- package/src/kad-dht.ts +3 -7
- package/src/network.ts +2 -1
- package/src/providers.ts +9 -31
- package/src/query/manager.ts +1 -1
- package/src/query-self.ts +1 -1
- package/src/reprovider.ts +3 -10
- package/src/routing-table/index.ts +54 -28
- package/src/routing-table/k-bucket.ts +13 -0
- package/src/routing-table/refresh.ts +1 -1
- package/src/rpc/index.ts +54 -23
- package/src/topology-listener.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "15.1.2-
|
|
3
|
+
"version": "15.1.2-bb4ad31db",
|
|
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",
|
|
@@ -47,21 +47,20 @@
|
|
|
47
47
|
"doc-check": "aegir doc-check"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@libp2p/crypto": "5.1.4-
|
|
51
|
-
"@libp2p/interface": "2.10.2-
|
|
52
|
-
"@libp2p/interface-internal": "2.3.14-
|
|
53
|
-
"@libp2p/peer-collections": "6.0.30-
|
|
54
|
-
"@libp2p/peer-id": "5.1.5-
|
|
55
|
-
"@libp2p/ping": "2.0.32-
|
|
56
|
-
"@libp2p/record": "4.0.6-
|
|
57
|
-
"@libp2p/utils": "6.6.5-
|
|
50
|
+
"@libp2p/crypto": "5.1.4-bb4ad31db",
|
|
51
|
+
"@libp2p/interface": "2.10.2-bb4ad31db",
|
|
52
|
+
"@libp2p/interface-internal": "2.3.14-bb4ad31db",
|
|
53
|
+
"@libp2p/peer-collections": "6.0.30-bb4ad31db",
|
|
54
|
+
"@libp2p/peer-id": "5.1.5-bb4ad31db",
|
|
55
|
+
"@libp2p/ping": "2.0.32-bb4ad31db",
|
|
56
|
+
"@libp2p/record": "4.0.6-bb4ad31db",
|
|
57
|
+
"@libp2p/utils": "6.6.5-bb4ad31db",
|
|
58
58
|
"@multiformats/multiaddr": "^12.4.0",
|
|
59
59
|
"any-signal": "^4.1.1",
|
|
60
60
|
"interface-datastore": "^8.3.1",
|
|
61
61
|
"it-all": "^3.0.8",
|
|
62
62
|
"it-drain": "^3.0.9",
|
|
63
63
|
"it-length": "^3.0.8",
|
|
64
|
-
"it-length-prefixed": "^10.0.1",
|
|
65
64
|
"it-map": "^3.1.3",
|
|
66
65
|
"it-merge": "^3.0.11",
|
|
67
66
|
"it-parallel": "^3.0.11",
|
|
@@ -69,7 +68,7 @@
|
|
|
69
68
|
"it-protobuf-stream": "^2.0.2",
|
|
70
69
|
"it-pushable": "^3.2.3",
|
|
71
70
|
"it-take": "^3.0.8",
|
|
72
|
-
"
|
|
71
|
+
"main-event": "^1.0.1",
|
|
73
72
|
"multiformats": "^13.3.6",
|
|
74
73
|
"p-defer": "^4.0.1",
|
|
75
74
|
"p-event": "^6.0.1",
|
|
@@ -81,9 +80,9 @@
|
|
|
81
80
|
"uint8arrays": "^5.1.0"
|
|
82
81
|
},
|
|
83
82
|
"devDependencies": {
|
|
84
|
-
"@libp2p/interface-compliance-tests": "6.4.8-
|
|
85
|
-
"@libp2p/logger": "5.1.18-
|
|
86
|
-
"@libp2p/peer-store": "11.2.2-
|
|
83
|
+
"@libp2p/interface-compliance-tests": "6.4.8-bb4ad31db",
|
|
84
|
+
"@libp2p/logger": "5.1.18-bb4ad31db",
|
|
85
|
+
"@libp2p/peer-store": "11.2.2-bb4ad31db",
|
|
87
86
|
"@types/lodash.random": "^3.2.9",
|
|
88
87
|
"@types/lodash.range": "^3.2.9",
|
|
89
88
|
"@types/sinon": "^17.0.4",
|
|
@@ -95,6 +94,7 @@
|
|
|
95
94
|
"execa": "^9.5.3",
|
|
96
95
|
"it-filter": "^3.1.3",
|
|
97
96
|
"it-last": "^3.0.8",
|
|
97
|
+
"it-length-prefixed": "^10.0.1",
|
|
98
98
|
"it-pair": "^2.0.6",
|
|
99
99
|
"it-stream-types": "^2.0.2",
|
|
100
100
|
"lodash.random": "^3.2.0",
|
package/src/index.ts
CHANGED
|
@@ -135,11 +135,12 @@
|
|
|
135
135
|
import { KadDHT as KadDHTClass } from './kad-dht.js'
|
|
136
136
|
import { MessageType } from './message/dht.js'
|
|
137
137
|
import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughMapper } from './utils.js'
|
|
138
|
-
import type { Libp2pEvents, ComponentLogger,
|
|
138
|
+
import type { Libp2pEvents, ComponentLogger, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions, PrivateKey, AbortOptions } from '@libp2p/interface'
|
|
139
139
|
import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
140
140
|
import type { Ping } from '@libp2p/ping'
|
|
141
141
|
import type { AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'
|
|
142
142
|
import type { Datastore } from 'interface-datastore'
|
|
143
|
+
import type { TypedEventTarget } from 'main-event'
|
|
143
144
|
import type { CID } from 'multiformats/cid'
|
|
144
145
|
import type { ProgressEvent } from 'progress-events'
|
|
145
146
|
|
|
@@ -693,6 +694,14 @@ export interface KadDHTInit {
|
|
|
693
694
|
* @default 10_000
|
|
694
695
|
*/
|
|
695
696
|
onPeerConnectTimeout?: number
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* When acting as a DHT server, all incoming RPC requests must complete within
|
|
700
|
+
* this timeout in ms otherwise the stream will be closed.
|
|
701
|
+
*
|
|
702
|
+
* @default 10_000
|
|
703
|
+
*/
|
|
704
|
+
incomingMessageTimeout?: number
|
|
696
705
|
}
|
|
697
706
|
|
|
698
707
|
export interface KadDHTComponents {
|
package/src/kad-dht.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { NotFoundError,
|
|
1
|
+
import { NotFoundError, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol, serviceCapabilities, serviceDependencies, start, stop } from '@libp2p/interface'
|
|
2
2
|
import drain from 'it-drain'
|
|
3
|
-
import
|
|
3
|
+
import { setMaxListeners, TypedEventEmitter } from 'main-event'
|
|
4
4
|
import pDefer from 'p-defer'
|
|
5
5
|
import { ALPHA, ON_PEER_CONNECT_TIMEOUT, PROTOCOL } from './constants.js'
|
|
6
6
|
import { ContentFetching } from './content-fetching/index.js'
|
|
@@ -175,13 +175,10 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
|
|
|
175
175
|
this.peerInfoMapper = init.peerInfoMapper ?? removePrivateAddressesMapper
|
|
176
176
|
this.onPeerConnectTimeout = init.onPeerConnectTimeout ?? ON_PEER_CONNECT_TIMEOUT
|
|
177
177
|
|
|
178
|
-
const providerLock = createMortice()
|
|
179
|
-
|
|
180
178
|
this.providers = new Providers(components, {
|
|
181
179
|
...init.providers,
|
|
182
180
|
logPrefix,
|
|
183
|
-
datastorePrefix
|
|
184
|
-
lock: providerLock
|
|
181
|
+
datastorePrefix
|
|
185
182
|
})
|
|
186
183
|
|
|
187
184
|
this.validators = {
|
|
@@ -292,7 +289,6 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
|
|
|
292
289
|
metricsPrefix,
|
|
293
290
|
datastorePrefix,
|
|
294
291
|
contentRouting: this.contentRouting,
|
|
295
|
-
lock: providerLock,
|
|
296
292
|
operationMetrics
|
|
297
293
|
})
|
|
298
294
|
|
package/src/network.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { InvalidParametersError
|
|
1
|
+
import { InvalidParametersError } from '@libp2p/interface'
|
|
2
2
|
import { Libp2pRecord } from '@libp2p/record'
|
|
3
3
|
import { AdaptiveTimeout } from '@libp2p/utils/adaptive-timeout'
|
|
4
4
|
import { pbStream } from 'it-protobuf-stream'
|
|
5
|
+
import { TypedEventEmitter } from 'main-event'
|
|
5
6
|
import { Message } from './message/dht.js'
|
|
6
7
|
import { fromPbPeerInfo } from './message/utils.js'
|
|
7
8
|
import {
|
package/src/providers.ts
CHANGED
|
@@ -3,13 +3,11 @@ import * as varint from 'uint8-varint'
|
|
|
3
3
|
import { parseProviderKey, readProviderTime, toProviderKey } from './utils.js'
|
|
4
4
|
import type { AbortOptions, ComponentLogger, Logger, Metrics, PeerId } from '@libp2p/interface'
|
|
5
5
|
import type { Datastore } from 'interface-datastore'
|
|
6
|
-
import type { Mortice } from 'mortice'
|
|
7
6
|
import type { CID } from 'multiformats'
|
|
8
7
|
|
|
9
8
|
export interface ProvidersInit {
|
|
10
9
|
logPrefix: string
|
|
11
10
|
datastorePrefix: string
|
|
12
|
-
lock: Mortice
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
export interface ProvidersComponents {
|
|
@@ -29,59 +27,39 @@ export class Providers {
|
|
|
29
27
|
private readonly log: Logger
|
|
30
28
|
private readonly datastore: Datastore
|
|
31
29
|
private readonly datastorePrefix: string
|
|
32
|
-
private readonly lock: Mortice
|
|
33
30
|
|
|
34
31
|
constructor (components: ProvidersComponents, init: ProvidersInit) {
|
|
35
32
|
this.log = components.logger.forComponent(`${init.logPrefix}:providers`)
|
|
36
33
|
this.datastorePrefix = `${init.datastorePrefix}/provider`
|
|
37
34
|
this.datastore = components.datastore
|
|
38
|
-
this.lock = init.lock
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
/**
|
|
42
38
|
* Add a new provider for the given CID
|
|
43
39
|
*/
|
|
44
40
|
async addProvider (cid: CID, provider: PeerId, options?: AbortOptions): Promise<void> {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
this.log.trace('%p provides %s', provider, cid)
|
|
49
|
-
await this.writeProviderEntry(cid, provider, options)
|
|
50
|
-
} finally {
|
|
51
|
-
release()
|
|
52
|
-
}
|
|
41
|
+
this.log.trace('%p provides %s', provider, cid)
|
|
42
|
+
await this.writeProviderEntry(cid, provider, options)
|
|
53
43
|
}
|
|
54
44
|
|
|
55
45
|
/**
|
|
56
46
|
* Remove a provider for the given CID
|
|
57
47
|
*/
|
|
58
48
|
async removeProvider (cid: CID, provider: PeerId, options?: AbortOptions): Promise<void> {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const key = toProviderKey(this.datastorePrefix, cid, provider)
|
|
63
|
-
this.log.trace('%p no longer provides %s', provider, cid)
|
|
64
|
-
await this.datastore.delete(key)
|
|
65
|
-
} finally {
|
|
66
|
-
release()
|
|
67
|
-
}
|
|
49
|
+
const key = toProviderKey(this.datastorePrefix, cid, provider)
|
|
50
|
+
this.log.trace('%p no longer provides %s', provider, cid)
|
|
51
|
+
await this.datastore.delete(key, options)
|
|
68
52
|
}
|
|
69
53
|
|
|
70
54
|
/**
|
|
71
55
|
* Get a list of providers for the given CID
|
|
72
56
|
*/
|
|
73
57
|
async getProviders (cid: CID, options?: AbortOptions): Promise<PeerId[]> {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.log.trace('get providers for %c', cid)
|
|
78
|
-
const provs = await this.loadProviders(cid, options)
|
|
79
|
-
this.log.trace('got %d providers for %c', provs.size, cid)
|
|
58
|
+
this.log.trace('get providers for %c', cid)
|
|
59
|
+
const provs = await this.loadProviders(cid, options)
|
|
60
|
+
this.log.trace('got %d providers for %c', provs.size, cid)
|
|
80
61
|
|
|
81
|
-
|
|
82
|
-
} finally {
|
|
83
|
-
release()
|
|
84
|
-
}
|
|
62
|
+
return [...provs.keys()]
|
|
85
63
|
}
|
|
86
64
|
|
|
87
65
|
/**
|
package/src/query/manager.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { setMaxListeners } from '@libp2p/interface'
|
|
2
1
|
import { createScalableCuckooFilter } from '@libp2p/utils/filters'
|
|
3
2
|
import { anySignal } from 'any-signal'
|
|
4
3
|
import merge from 'it-merge'
|
|
4
|
+
import { setMaxListeners } from 'main-event'
|
|
5
5
|
import { pEvent } from 'p-event'
|
|
6
6
|
import { raceSignal } from 'race-signal'
|
|
7
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
package/src/query-self.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { setMaxListeners } from '@libp2p/interface'
|
|
2
1
|
import { anySignal } from 'any-signal'
|
|
3
2
|
import length from 'it-length'
|
|
4
3
|
import { pipe } from 'it-pipe'
|
|
5
4
|
import take from 'it-take'
|
|
5
|
+
import { setMaxListeners } from 'main-event'
|
|
6
6
|
import pDefer from 'p-defer'
|
|
7
7
|
import { QUERY_SELF_INTERVAL, QUERY_SELF_TIMEOUT, K, QUERY_SELF_INITIAL_INTERVAL } from './constants.js'
|
|
8
8
|
import { timeOperationMethod } from './utils.js'
|
package/src/reprovider.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface'
|
|
2
1
|
import { AdaptiveTimeout } from '@libp2p/utils/adaptive-timeout'
|
|
3
2
|
import { Queue } from '@libp2p/utils/queue'
|
|
4
3
|
import drain from 'it-drain'
|
|
4
|
+
import { TypedEventEmitter, setMaxListeners } from 'main-event'
|
|
5
5
|
import { PROVIDERS_VALIDITY, REPROVIDE_CONCURRENCY, REPROVIDE_INTERVAL, REPROVIDE_MAX_QUEUE_SIZE, REPROVIDE_THRESHOLD, REPROVIDE_TIMEOUT } from './constants.js'
|
|
6
6
|
import { parseProviderKey, readProviderTime, timeOperationMethod } from './utils.js'
|
|
7
7
|
import type { ContentRouting } from './content-routing/index.js'
|
|
@@ -10,7 +10,6 @@ import type { AbortOptions, ComponentLogger, Logger, Metrics, PeerId } from '@li
|
|
|
10
10
|
import type { AddressManager } from '@libp2p/interface-internal'
|
|
11
11
|
import type { AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'
|
|
12
12
|
import type { Datastore } from 'interface-datastore'
|
|
13
|
-
import type { Mortice } from 'mortice'
|
|
14
13
|
import type { CID } from 'multiformats/cid'
|
|
15
14
|
|
|
16
15
|
export interface ReproviderComponents {
|
|
@@ -26,7 +25,6 @@ export interface ReproviderInit {
|
|
|
26
25
|
metricsPrefix: string
|
|
27
26
|
datastorePrefix: string
|
|
28
27
|
contentRouting: ContentRouting
|
|
29
|
-
lock: Mortice
|
|
30
28
|
operationMetrics: OperationMetrics
|
|
31
29
|
concurrency?: number
|
|
32
30
|
maxQueueSize?: number
|
|
@@ -60,7 +58,6 @@ export class Reprovider extends TypedEventEmitter<ReprovideEvents> {
|
|
|
60
58
|
private readonly addressManager: AddressManager
|
|
61
59
|
private readonly validity: number
|
|
62
60
|
private readonly interval: number
|
|
63
|
-
private readonly lock: Mortice
|
|
64
61
|
private readonly peerId: PeerId
|
|
65
62
|
|
|
66
63
|
constructor (components: ReproviderComponents, init: ReproviderInit) {
|
|
@@ -86,7 +83,6 @@ export class Reprovider extends TypedEventEmitter<ReprovideEvents> {
|
|
|
86
83
|
this.validity = init.validity ?? PROVIDERS_VALIDITY
|
|
87
84
|
this.interval = init.interval ?? REPROVIDE_INTERVAL
|
|
88
85
|
this.contentRouting = init.contentRouting
|
|
89
|
-
this.lock = init.lock
|
|
90
86
|
this.running = false
|
|
91
87
|
|
|
92
88
|
this.reprovide = timeOperationMethod(this.reprovide.bind(this), init.operationMetrics, 'PROVIDE')
|
|
@@ -123,15 +119,13 @@ export class Reprovider extends TypedEventEmitter<ReprovideEvents> {
|
|
|
123
119
|
* them if the provider is us and the expiry is within the reprovide window.
|
|
124
120
|
*/
|
|
125
121
|
private async cleanUp (options?: AbortOptions): Promise<void> {
|
|
126
|
-
const release = await this.lock.writeLock(options)
|
|
127
|
-
|
|
128
122
|
try {
|
|
129
123
|
this.safeDispatchEvent('reprovide:start')
|
|
130
124
|
|
|
131
125
|
// Get all provider entries from the datastore
|
|
132
126
|
for await (const entry of this.datastore.query({
|
|
133
127
|
prefix: this.datastorePrefix
|
|
134
|
-
})) {
|
|
128
|
+
}, options)) {
|
|
135
129
|
try {
|
|
136
130
|
// Add a delete to the batch for each expired entry
|
|
137
131
|
const { cid, peerId } = parseProviderKey(entry.key)
|
|
@@ -144,7 +138,7 @@ export class Reprovider extends TypedEventEmitter<ReprovideEvents> {
|
|
|
144
138
|
|
|
145
139
|
// delete the record if it has expired
|
|
146
140
|
if (expired) {
|
|
147
|
-
await this.datastore.delete(entry.key)
|
|
141
|
+
await this.datastore.delete(entry.key, options)
|
|
148
142
|
}
|
|
149
143
|
|
|
150
144
|
// if the provider is us and we are within the reprovide threshold,
|
|
@@ -162,7 +156,6 @@ export class Reprovider extends TypedEventEmitter<ReprovideEvents> {
|
|
|
162
156
|
|
|
163
157
|
this.log('reprovide/cleanup successful')
|
|
164
158
|
} finally {
|
|
165
|
-
release()
|
|
166
159
|
this.safeDispatchEvent('reprovide:end')
|
|
167
160
|
|
|
168
161
|
if (this.running) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { start, stop } from '@libp2p/interface'
|
|
2
2
|
import { AdaptiveTimeout } from '@libp2p/utils/adaptive-timeout'
|
|
3
3
|
import { PeerQueue } from '@libp2p/utils/peer-queue'
|
|
4
4
|
import { anySignal } from 'any-signal'
|
|
5
5
|
import parallel from 'it-parallel'
|
|
6
|
+
import { TypedEventEmitter, setMaxListeners } from 'main-event'
|
|
6
7
|
import * as utils from '../utils.js'
|
|
7
8
|
import { ClosestPeers } from './closest-peers.js'
|
|
8
9
|
import { KBucket, isLeafBucket } from './k-bucket.js'
|
|
@@ -97,6 +98,8 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
97
98
|
kadBucketEvents: CounterGroup<'ping_old_contact' | 'ping_old_contact_error' | 'ping_new_contact' | 'ping_new_contact_error' | 'peer_added' | 'peer_removed'>
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
private shutdownController: AbortController
|
|
102
|
+
|
|
100
103
|
constructor (components: RoutingTableComponents, init: RoutingTableInit) {
|
|
101
104
|
super()
|
|
102
105
|
|
|
@@ -114,6 +117,7 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
114
117
|
this.peerRemoved = this.peerRemoved.bind(this)
|
|
115
118
|
this.populateFromDatastoreOnStart = init.populateFromDatastoreOnStart ?? POPULATE_FROM_DATASTORE_ON_START
|
|
116
119
|
this.populateFromDatastoreLimit = init.populateFromDatastoreLimit ?? POPULATE_FROM_DATASTORE_LIMIT
|
|
120
|
+
this.shutdownController = new AbortController()
|
|
117
121
|
|
|
118
122
|
this.pingOldContactQueue = new PeerQueue({
|
|
119
123
|
concurrency: init.pingOldContactConcurrency ?? PING_OLD_CONTACT_CONCURRENCY,
|
|
@@ -185,11 +189,13 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
185
189
|
|
|
186
190
|
this.running = true
|
|
187
191
|
|
|
192
|
+
this.shutdownController = new AbortController()
|
|
188
193
|
await start(this.closestPeerTagger, this.kb)
|
|
189
|
-
await this.kb.addSelfPeer(this.components.peerId)
|
|
190
194
|
}
|
|
191
195
|
|
|
192
196
|
async afterStart (): Promise<void> {
|
|
197
|
+
let peerStorePeers = 0
|
|
198
|
+
|
|
193
199
|
// do this async to not block startup but iterate serially to not overwhelm
|
|
194
200
|
// the ping queue
|
|
195
201
|
Promise.resolve().then(async () => {
|
|
@@ -197,37 +203,48 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
197
203
|
return
|
|
198
204
|
}
|
|
199
205
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
206
|
+
const signal = anySignal([
|
|
207
|
+
this.shutdownController.signal,
|
|
208
|
+
AbortSignal.timeout(20_000)
|
|
209
|
+
])
|
|
210
|
+
setMaxListeners(Infinity, signal)
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
// add existing peers from the peer store to routing table
|
|
214
|
+
for (const peer of await this.components.peerStore.all({
|
|
215
|
+
filters: [(peer) => {
|
|
216
|
+
return peer.protocols.includes(this.protocol) && peer.tags.has(KAD_PEER_TAG_NAME)
|
|
217
|
+
}],
|
|
218
|
+
limit: this.populateFromDatastoreLimit,
|
|
219
|
+
signal
|
|
220
|
+
})) {
|
|
221
|
+
if (!this.running) {
|
|
222
|
+
// bail if we've been shut down
|
|
223
|
+
return
|
|
224
|
+
}
|
|
213
225
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
tags
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
226
|
+
try {
|
|
227
|
+
await this.add(peer.id, {
|
|
228
|
+
signal
|
|
229
|
+
})
|
|
230
|
+
peerStorePeers++
|
|
231
|
+
} catch (err) {
|
|
232
|
+
this.log('failed to add peer %p to routing table, removing kad-dht peer tags - %e')
|
|
233
|
+
await this.components.peerStore.merge(peer.id, {
|
|
234
|
+
tags: {
|
|
235
|
+
[this.peerTagName]: undefined
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
}
|
|
224
239
|
}
|
|
240
|
+
} finally {
|
|
241
|
+
signal.clear()
|
|
225
242
|
}
|
|
226
243
|
|
|
227
244
|
this.log('added %d peer store peers to the routing table', peerStorePeers)
|
|
228
245
|
})
|
|
229
246
|
.catch(err => {
|
|
230
|
-
this.log.error('error adding peer store peers to the routing table %e', err)
|
|
247
|
+
this.log.error('error adding %d, peer store peers to the routing table - %e', peerStorePeers, err)
|
|
231
248
|
})
|
|
232
249
|
}
|
|
233
250
|
|
|
@@ -236,6 +253,7 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
236
253
|
await stop(this.closestPeerTagger, this.kb)
|
|
237
254
|
this.pingOldContactQueue.abort()
|
|
238
255
|
this.pingNewContactQueue.abort()
|
|
256
|
+
this.shutdownController.abort()
|
|
239
257
|
}
|
|
240
258
|
|
|
241
259
|
private async peerAdded (peer: Peer, bucket: LeafBucket, options?: AbortOptions): Promise<void> {
|
|
@@ -310,7 +328,11 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
310
328
|
|
|
311
329
|
const result = await this.pingOldContactQueue.add(async (options) => {
|
|
312
330
|
const signal = this.pingOldContactTimeout.getTimeoutSignal()
|
|
313
|
-
const signals = anySignal([
|
|
331
|
+
const signals = anySignal([
|
|
332
|
+
signal,
|
|
333
|
+
this.shutdownController.signal,
|
|
334
|
+
options?.signal
|
|
335
|
+
])
|
|
314
336
|
setMaxListeners(Infinity, signal, signals)
|
|
315
337
|
|
|
316
338
|
try {
|
|
@@ -342,7 +364,11 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
|
|
|
342
364
|
|
|
343
365
|
async verifyNewContact (contact: Peer, options?: AbortOptions): Promise<boolean> {
|
|
344
366
|
const signal = this.pingNewContactTimeout.getTimeoutSignal()
|
|
345
|
-
const signals = anySignal([
|
|
367
|
+
const signals = anySignal([
|
|
368
|
+
signal,
|
|
369
|
+
this.shutdownController.signal,
|
|
370
|
+
options?.signal
|
|
371
|
+
])
|
|
346
372
|
setMaxListeners(Infinity, signal, signals)
|
|
347
373
|
|
|
348
374
|
try {
|
|
@@ -46,6 +46,7 @@ export interface OnMoveCallback {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export interface KBucketComponents {
|
|
49
|
+
peerId: PeerId
|
|
49
50
|
metrics?: Metrics
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -134,6 +135,7 @@ export function isLeafBucket (obj: any): obj is LeafBucket {
|
|
|
134
135
|
* configurable prefix length, bucket split threshold and size.
|
|
135
136
|
*/
|
|
136
137
|
export class KBucket {
|
|
138
|
+
private readonly peerId: PeerId
|
|
137
139
|
public root: Bucket
|
|
138
140
|
public localPeer?: Peer
|
|
139
141
|
private readonly prefixLength: number
|
|
@@ -149,6 +151,7 @@ export class KBucket {
|
|
|
149
151
|
private readonly addingPeerMap: PeerMap<Promise<void>>
|
|
150
152
|
|
|
151
153
|
constructor (components: KBucketComponents, options: KBucketOptions) {
|
|
154
|
+
this.peerId = components.peerId
|
|
152
155
|
this.prefixLength = options.prefixLength ?? PREFIX_LENGTH
|
|
153
156
|
this.kBucketSize = options.kBucketSize ?? KBUCKET_SIZE
|
|
154
157
|
this.splitThreshold = options.splitThreshold ?? this.kBucketSize
|
|
@@ -170,8 +173,18 @@ export class KBucket {
|
|
|
170
173
|
}
|
|
171
174
|
}
|
|
172
175
|
|
|
176
|
+
async start (): Promise<void> {
|
|
177
|
+
await this.addSelfPeer(this.peerId)
|
|
178
|
+
}
|
|
179
|
+
|
|
173
180
|
stop (): void {
|
|
174
181
|
this.addingPeerMap.clear()
|
|
182
|
+
|
|
183
|
+
this.root = {
|
|
184
|
+
prefix: '',
|
|
185
|
+
depth: 0,
|
|
186
|
+
peers: []
|
|
187
|
+
}
|
|
175
188
|
}
|
|
176
189
|
|
|
177
190
|
async addSelfPeer (peerId: PeerId, options?: AbortOptions): Promise<void> {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto'
|
|
2
|
-
import { setMaxListeners } from '@libp2p/interface'
|
|
3
2
|
import { peerIdFromMultihash } from '@libp2p/peer-id'
|
|
4
3
|
import { anySignal } from 'any-signal'
|
|
5
4
|
import length from 'it-length'
|
|
5
|
+
import { setMaxListeners } from 'main-event'
|
|
6
6
|
import * as Digest from 'multiformats/hashes/digest'
|
|
7
7
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
8
8
|
import { xor as uint8ArrayXor } from 'uint8arrays/xor'
|
package/src/rpc/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { TimeoutError } from '@libp2p/interface'
|
|
2
|
+
import { pbStream } from 'it-protobuf-stream'
|
|
3
3
|
import { Message, MessageType } from '../message/dht.js'
|
|
4
4
|
import { AddProviderHandler } from './handlers/add-provider.js'
|
|
5
5
|
import { FindNodeHandler } from './handlers/find-node.js'
|
|
@@ -15,7 +15,7 @@ import type { GetProvidersHandlerComponents } from './handlers/get-providers.js'
|
|
|
15
15
|
import type { GetValueHandlerComponents } from './handlers/get-value.js'
|
|
16
16
|
import type { PutValueHandlerComponents } from './handlers/put-value.js'
|
|
17
17
|
import type { RoutingTable } from '../routing-table/index.js'
|
|
18
|
-
import type { CounterGroup, Logger, Metrics, PeerId, IncomingStreamData } from '@libp2p/interface'
|
|
18
|
+
import type { CounterGroup, Logger, Metrics, PeerId, IncomingStreamData, MetricGroup } from '@libp2p/interface'
|
|
19
19
|
|
|
20
20
|
export interface DHTMessageHandler {
|
|
21
21
|
handle(peerId: PeerId, msg: Message): Promise<Message | undefined>
|
|
@@ -30,6 +30,7 @@ export interface RPCInit {
|
|
|
30
30
|
metricsPrefix: string
|
|
31
31
|
datastorePrefix: string
|
|
32
32
|
peerInfoMapper: PeerInfoMapper
|
|
33
|
+
incomingMessageTimeout?: number
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export interface RPCComponents extends GetValueHandlerComponents, PutValueHandlerComponents, FindNodeHandlerComponents, GetProvidersHandlerComponents {
|
|
@@ -38,21 +39,24 @@ export interface RPCComponents extends GetValueHandlerComponents, PutValueHandle
|
|
|
38
39
|
|
|
39
40
|
export class RPC {
|
|
40
41
|
private readonly handlers: Record<string, DHTMessageHandler>
|
|
41
|
-
private readonly routingTable: RoutingTable
|
|
42
42
|
private readonly log: Logger
|
|
43
43
|
private readonly metrics: {
|
|
44
44
|
operations?: CounterGroup
|
|
45
45
|
errors?: CounterGroup
|
|
46
|
+
rpcTime?: MetricGroup
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
private readonly incomingMessageTimeout: number
|
|
50
|
+
|
|
48
51
|
constructor (components: RPCComponents, init: RPCInit) {
|
|
49
52
|
this.metrics = {
|
|
50
53
|
operations: components.metrics?.registerCounterGroup(`${init.metricsPrefix}_inbound_rpc_requests_total`),
|
|
51
|
-
errors: components.metrics?.registerCounterGroup(`${init.metricsPrefix}_inbound_rpc_errors_total`)
|
|
54
|
+
errors: components.metrics?.registerCounterGroup(`${init.metricsPrefix}_inbound_rpc_errors_total`),
|
|
55
|
+
rpcTime: components.metrics?.registerMetricGroup(`${init.metricsPrefix}_inbound_rpc_time_seconds`, { label: 'operation' })
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
this.log = components.logger.forComponent(`${init.logPrefix}:rpc`)
|
|
55
|
-
this.
|
|
59
|
+
this.incomingMessageTimeout = init.incomingMessageTimeout ?? 10_000
|
|
56
60
|
this.handlers = {
|
|
57
61
|
[MessageType.GET_VALUE.toString()]: new GetValueHandler(components, init),
|
|
58
62
|
[MessageType.PUT_VALUE.toString()]: new PutValueHandler(components, init),
|
|
@@ -92,34 +96,61 @@ export class RPC {
|
|
|
92
96
|
* Handle incoming streams on the dht protocol
|
|
93
97
|
*/
|
|
94
98
|
onIncomingStream (data: IncomingStreamData): void {
|
|
95
|
-
|
|
99
|
+
const message = 'unknown'
|
|
96
100
|
|
|
97
101
|
Promise.resolve().then(async () => {
|
|
98
102
|
const { stream, connection } = data
|
|
99
|
-
const peerId = connection.remotePeer
|
|
100
103
|
|
|
101
|
-
const
|
|
104
|
+
const abortListener = (): void => {
|
|
105
|
+
stream.abort(new TimeoutError())
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let signal = AbortSignal.timeout(this.incomingMessageTimeout)
|
|
109
|
+
signal.addEventListener('abort', abortListener)
|
|
110
|
+
|
|
111
|
+
const messages = pbStream(stream).pb(Message)
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
while (true) {
|
|
115
|
+
const message = await messages.read({
|
|
116
|
+
signal
|
|
117
|
+
})
|
|
102
118
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
119
|
+
const stopSuccessTimer = this.metrics?.rpcTime?.timer(message.type.toString())
|
|
120
|
+
const stopErrorTimer = this.metrics?.rpcTime?.timer(message.type.toString())
|
|
121
|
+
let errored = false
|
|
122
|
+
|
|
123
|
+
try {
|
|
108
124
|
// handle the message
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
self.log('incoming %s from %p', desMessage.type, peerId)
|
|
112
|
-
const res = await self.handleMessage(peerId, desMessage)
|
|
125
|
+
this.log('incoming %s from %p', message.type, connection.remotePeer)
|
|
126
|
+
const res = await this.handleMessage(connection.remotePeer, message)
|
|
113
127
|
|
|
114
128
|
// Not all handlers will return a response
|
|
115
129
|
if (res != null) {
|
|
116
|
-
|
|
130
|
+
await messages.write(res, {
|
|
131
|
+
signal
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
} catch (err) {
|
|
135
|
+
errored = true
|
|
136
|
+
stopErrorTimer?.()
|
|
137
|
+
|
|
138
|
+
throw err
|
|
139
|
+
} finally {
|
|
140
|
+
if (!errored) {
|
|
141
|
+
stopSuccessTimer?.()
|
|
117
142
|
}
|
|
118
143
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
144
|
+
|
|
145
|
+
// we have received a message so reset the timeout controller to
|
|
146
|
+
// allow the remote to send another
|
|
147
|
+
signal.removeEventListener('abort', abortListener)
|
|
148
|
+
signal = AbortSignal.timeout(this.incomingMessageTimeout)
|
|
149
|
+
signal.addEventListener('abort', abortListener)
|
|
150
|
+
}
|
|
151
|
+
} catch (err: any) {
|
|
152
|
+
stream.abort(err)
|
|
153
|
+
}
|
|
123
154
|
})
|
|
124
155
|
.catch(err => {
|
|
125
156
|
this.log.error('error handling %s RPC message from %p - %e', message, data.connection.remotePeer, err)
|
package/src/topology-listener.ts
CHANGED