@libp2p/kad-dht 4.0.2 → 5.0.0
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/README.md +10 -12
- package/dist/src/content-fetching/index.d.ts +4 -5
- package/dist/src/content-fetching/index.d.ts.map +1 -1
- package/dist/src/content-fetching/index.js +8 -12
- package/dist/src/content-fetching/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +4 -5
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +7 -11
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/dual-kad-dht.d.ts +6 -7
- package/dist/src/dual-kad-dht.d.ts.map +1 -1
- package/dist/src/dual-kad-dht.js +3 -9
- package/dist/src/dual-kad-dht.js.map +1 -1
- package/dist/src/index.d.ts +16 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +7 -4
- package/dist/src/index.js.map +1 -1
- package/dist/src/kad-dht.d.ts +4 -6
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +16 -31
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/network.d.ts +4 -5
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +4 -8
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-routing/index.d.ts +4 -5
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +12 -16
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/providers.d.ts +7 -5
- package/dist/src/providers.d.ts.map +1 -1
- package/dist/src/providers.js +6 -10
- package/dist/src/providers.js.map +1 -1
- package/dist/src/query/manager.d.ts +8 -5
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/manager.js +5 -9
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query-self.d.ts +4 -5
- package/dist/src/query-self.d.ts.map +1 -1
- package/dist/src/query-self.js +3 -7
- package/dist/src/query-self.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +12 -5
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +11 -15
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/refresh.d.ts +1 -3
- package/dist/src/routing-table/refresh.d.ts.map +1 -1
- package/dist/src/routing-table/refresh.js +0 -2
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/dist/src/rpc/handlers/add-provider.d.ts +1 -3
- package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
- package/dist/src/rpc/handlers/add-provider.js +0 -2
- package/dist/src/rpc/handlers/add-provider.js.map +1 -1
- package/dist/src/rpc/handlers/find-node.d.ts +8 -6
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
- package/dist/src/rpc/handlers/find-node.js +5 -9
- package/dist/src/rpc/handlers/find-node.js.map +1 -1
- package/dist/src/rpc/handlers/get-providers.d.ts +7 -5
- package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-providers.js +3 -7
- package/dist/src/rpc/handlers/get-providers.js.map +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts +9 -5
- package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-value.js +5 -9
- package/dist/src/rpc/handlers/get-value.js.map +1 -1
- package/dist/src/rpc/handlers/ping.d.ts +1 -3
- package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
- package/dist/src/rpc/handlers/ping.js +0 -2
- package/dist/src/rpc/handlers/ping.js.map +1 -1
- package/dist/src/rpc/handlers/put-value.d.ts +7 -5
- package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/put-value.js +3 -7
- package/dist/src/rpc/handlers/put-value.js.map +1 -1
- package/dist/src/rpc/index.d.ts +9 -5
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +5 -10
- package/dist/src/rpc/index.js.map +1 -1
- package/dist/src/topology-listener.d.ts +4 -5
- package/dist/src/topology-listener.d.ts.map +1 -1
- package/dist/src/topology-listener.js +4 -8
- package/dist/src/topology-listener.js.map +1 -1
- package/package.json +8 -8
- package/src/content-fetching/index.ts +12 -14
- package/src/content-routing/index.ts +10 -13
- package/src/dual-kad-dht.ts +8 -13
- package/src/index.ts +25 -4
- package/src/kad-dht.ts +19 -35
- package/src/network.ts +7 -10
- package/src/peer-routing/index.ts +15 -18
- package/src/providers.ts +12 -12
- package/src/query/manager.ts +14 -11
- package/src/query-self.ts +6 -9
- package/src/routing-table/index.ts +23 -17
- package/src/routing-table/refresh.ts +1 -6
- package/src/rpc/handlers/add-provider.ts +1 -6
- package/src/rpc/handlers/find-node.ts +14 -12
- package/src/rpc/handlers/get-providers.ts +10 -9
- package/src/rpc/handlers/get-value.ts +14 -11
- package/src/rpc/handlers/ping.ts +1 -6
- package/src/rpc/handlers/put-value.ts +10 -9
- package/src/rpc/index.ts +15 -19
- package/src/topology-listener.ts +7 -10
package/src/providers.ts
CHANGED
|
@@ -15,7 +15,6 @@ import type { Datastore } from 'interface-datastore'
|
|
|
15
15
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
16
16
|
import type { CID } from 'multiformats'
|
|
17
17
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
18
|
-
import { Components, Initializable } from '@libp2p/components'
|
|
19
18
|
|
|
20
19
|
const log = logger('libp2p:kad-dht:providers')
|
|
21
20
|
|
|
@@ -31,6 +30,10 @@ export interface ProvidersInit {
|
|
|
31
30
|
provideValidity?: number
|
|
32
31
|
}
|
|
33
32
|
|
|
33
|
+
export interface ProvidersComponents {
|
|
34
|
+
datastore: Datastore
|
|
35
|
+
}
|
|
36
|
+
|
|
34
37
|
/**
|
|
35
38
|
* This class manages known providers.
|
|
36
39
|
* A provider is a peer that we know to have the content for a given CID.
|
|
@@ -43,8 +46,8 @@ export interface ProvidersInit {
|
|
|
43
46
|
* providers are stored in the datastore, but to ensure
|
|
44
47
|
* access is fast there is an LRU cache in front of that.
|
|
45
48
|
*/
|
|
46
|
-
export class Providers implements Startable
|
|
47
|
-
private components:
|
|
49
|
+
export class Providers implements Startable {
|
|
50
|
+
private readonly components: ProvidersComponents
|
|
48
51
|
private readonly cache: ReturnType<typeof cache>
|
|
49
52
|
private readonly cleanupInterval: number
|
|
50
53
|
private readonly provideValidity: number
|
|
@@ -52,9 +55,10 @@ export class Providers implements Startable, Initializable {
|
|
|
52
55
|
private started: boolean
|
|
53
56
|
private cleaner?: NodeJS.Timer
|
|
54
57
|
|
|
55
|
-
constructor (init: ProvidersInit = {}) {
|
|
58
|
+
constructor (components: ProvidersComponents, init: ProvidersInit = {}) {
|
|
56
59
|
const { cacheSize, cleanupInterval, provideValidity } = init
|
|
57
60
|
|
|
61
|
+
this.components = components
|
|
58
62
|
this.cleanupInterval = cleanupInterval ?? PROVIDERS_CLEANUP_INTERVAL
|
|
59
63
|
this.provideValidity = provideValidity ?? PROVIDERS_VALIDITY
|
|
60
64
|
this.cache = cache(cacheSize ?? PROVIDERS_LRU_CACHE_SIZE)
|
|
@@ -62,10 +66,6 @@ export class Providers implements Startable, Initializable {
|
|
|
62
66
|
this.started = false
|
|
63
67
|
}
|
|
64
68
|
|
|
65
|
-
init (components: Components): void {
|
|
66
|
-
this.components = components
|
|
67
|
-
}
|
|
68
|
-
|
|
69
69
|
isStarted () {
|
|
70
70
|
return this.started
|
|
71
71
|
}
|
|
@@ -112,10 +112,10 @@ export class Providers implements Startable, Initializable {
|
|
|
112
112
|
let count = 0
|
|
113
113
|
let deleteCount = 0
|
|
114
114
|
const deleted = new Map<string, Set<string>>()
|
|
115
|
-
const batch = this.components.
|
|
115
|
+
const batch = this.components.datastore.batch()
|
|
116
116
|
|
|
117
117
|
// Get all provider entries from the datastore
|
|
118
|
-
const query = this.components.
|
|
118
|
+
const query = this.components.datastore.query({ prefix: PROVIDER_KEY_PREFIX })
|
|
119
119
|
|
|
120
120
|
for await (const entry of query) {
|
|
121
121
|
try {
|
|
@@ -179,7 +179,7 @@ export class Providers implements Startable, Initializable {
|
|
|
179
179
|
let provs: Map<string, Date> = this.cache.get(cacheKey)
|
|
180
180
|
|
|
181
181
|
if (provs == null) {
|
|
182
|
-
provs = await loadProviders(this.components.
|
|
182
|
+
provs = await loadProviders(this.components.datastore, cid)
|
|
183
183
|
this.cache.set(cacheKey, provs)
|
|
184
184
|
}
|
|
185
185
|
|
|
@@ -201,7 +201,7 @@ export class Providers implements Startable, Initializable {
|
|
|
201
201
|
const dsKey = makeProviderKey(cid)
|
|
202
202
|
this.cache.set(dsKey, provs)
|
|
203
203
|
|
|
204
|
-
await writeProviderEntry(this.components.
|
|
204
|
+
await writeProviderEntry(this.components.datastore, cid, provider, now)
|
|
205
205
|
})
|
|
206
206
|
}
|
|
207
207
|
|
package/src/query/manager.ts
CHANGED
|
@@ -13,8 +13,8 @@ import type { PeerId } from '@libp2p/interface-peer-id'
|
|
|
13
13
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
14
14
|
import type { QueryFunc } from './types.js'
|
|
15
15
|
import type { QueryOptions } from '@libp2p/interface-dht'
|
|
16
|
-
import { Components, Initializable } from '@libp2p/components'
|
|
17
16
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
17
|
+
import type { Metrics } from '@libp2p/interface-metrics'
|
|
18
18
|
|
|
19
19
|
const METRIC_RUNNING_QUERIES = 'running-queries'
|
|
20
20
|
|
|
@@ -28,11 +28,16 @@ export interface QueryManagerInit {
|
|
|
28
28
|
alpha?: number
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
export interface QueryManagerComponents {
|
|
32
|
+
peerId: PeerId
|
|
33
|
+
metrics?: Metrics
|
|
34
|
+
}
|
|
35
|
+
|
|
31
36
|
/**
|
|
32
37
|
* Keeps track of all running queries
|
|
33
38
|
*/
|
|
34
|
-
export class QueryManager implements Startable
|
|
35
|
-
private components:
|
|
39
|
+
export class QueryManager implements Startable {
|
|
40
|
+
private readonly components: QueryManagerComponents
|
|
36
41
|
private readonly lan: boolean
|
|
37
42
|
public disjointPaths: number
|
|
38
43
|
private readonly alpha: number
|
|
@@ -40,8 +45,10 @@ export class QueryManager implements Startable, Initializable {
|
|
|
40
45
|
private running: boolean
|
|
41
46
|
private queries: number
|
|
42
47
|
|
|
43
|
-
constructor (init: QueryManagerInit) {
|
|
48
|
+
constructor (components: QueryManagerComponents, init: QueryManagerInit) {
|
|
44
49
|
const { lan = false, disjointPaths = K, alpha = ALPHA } = init
|
|
50
|
+
|
|
51
|
+
this.components = components
|
|
45
52
|
this.disjointPaths = disjointPaths ?? K
|
|
46
53
|
this.controllers = new Set()
|
|
47
54
|
this.running = false
|
|
@@ -50,10 +57,6 @@ export class QueryManager implements Startable, Initializable {
|
|
|
50
57
|
this.queries = 0
|
|
51
58
|
}
|
|
52
59
|
|
|
53
|
-
init (components: Components): void {
|
|
54
|
-
this.components = components
|
|
55
|
-
}
|
|
56
|
-
|
|
57
60
|
isStarted () {
|
|
58
61
|
return this.running
|
|
59
62
|
}
|
|
@@ -128,7 +131,7 @@ export class QueryManager implements Startable, Initializable {
|
|
|
128
131
|
try {
|
|
129
132
|
log('query:start')
|
|
130
133
|
this.queries++
|
|
131
|
-
this.components.
|
|
134
|
+
this.components.metrics?.updateComponentMetric({
|
|
132
135
|
system: 'libp2p',
|
|
133
136
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
134
137
|
metric: METRIC_RUNNING_QUERIES,
|
|
@@ -148,7 +151,7 @@ export class QueryManager implements Startable, Initializable {
|
|
|
148
151
|
return queryPath({
|
|
149
152
|
key,
|
|
150
153
|
startingPeer: peer,
|
|
151
|
-
ourPeerId: this.components.
|
|
154
|
+
ourPeerId: this.components.peerId,
|
|
152
155
|
signal,
|
|
153
156
|
query: queryFunc,
|
|
154
157
|
pathIndex: index,
|
|
@@ -183,7 +186,7 @@ export class QueryManager implements Startable, Initializable {
|
|
|
183
186
|
}
|
|
184
187
|
|
|
185
188
|
this.queries--
|
|
186
|
-
this.components.
|
|
189
|
+
this.components.metrics?.updateComponentMetric({
|
|
187
190
|
system: 'libp2p',
|
|
188
191
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
189
192
|
metric: METRIC_RUNNING_QUERIES,
|
package/src/query-self.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { logger, Logger } from '@libp2p/logger'
|
|
|
8
8
|
import type { PeerRouting } from './peer-routing/index.js'
|
|
9
9
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
10
10
|
import { pipe } from 'it-pipe'
|
|
11
|
-
import {
|
|
11
|
+
import type { KadDHTComponents } from './index.js'
|
|
12
12
|
|
|
13
13
|
export interface QuerySelfInit {
|
|
14
14
|
lan: boolean
|
|
@@ -21,9 +21,9 @@ export interface QuerySelfInit {
|
|
|
21
21
|
/**
|
|
22
22
|
* Receives notifications of new peers joining the network that support the DHT protocol
|
|
23
23
|
*/
|
|
24
|
-
export class QuerySelf implements Startable
|
|
24
|
+
export class QuerySelf implements Startable {
|
|
25
25
|
private readonly log: Logger
|
|
26
|
-
private components:
|
|
26
|
+
private readonly components: KadDHTComponents
|
|
27
27
|
private readonly peerRouting: PeerRouting
|
|
28
28
|
private readonly count: number
|
|
29
29
|
private readonly interval: number
|
|
@@ -32,9 +32,10 @@ export class QuerySelf implements Startable, Initializable {
|
|
|
32
32
|
private timeoutId?: NodeJS.Timer
|
|
33
33
|
private controller?: AbortController
|
|
34
34
|
|
|
35
|
-
constructor (init: QuerySelfInit) {
|
|
35
|
+
constructor (components: KadDHTComponents, init: QuerySelfInit) {
|
|
36
36
|
const { peerRouting, lan, count, interval, queryTimeout } = init
|
|
37
37
|
|
|
38
|
+
this.components = components
|
|
38
39
|
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:query-self`)
|
|
39
40
|
this.running = false
|
|
40
41
|
this.peerRouting = peerRouting
|
|
@@ -43,10 +44,6 @@ export class QuerySelf implements Startable, Initializable {
|
|
|
43
44
|
this.queryTimeout = queryTimeout ?? QUERY_SELF_TIMEOUT
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
init (components: Components): void {
|
|
47
|
-
this.components = components
|
|
48
|
-
}
|
|
49
|
-
|
|
50
47
|
isStarted () {
|
|
51
48
|
return this.running
|
|
52
49
|
}
|
|
@@ -86,7 +83,7 @@ export class QuerySelf implements Startable, Initializable {
|
|
|
86
83
|
}
|
|
87
84
|
} catch {} // fails on node < 15.4
|
|
88
85
|
const found = await pipe(
|
|
89
|
-
this.peerRouting.getClosestPeers(this.components.
|
|
86
|
+
this.peerRouting.getClosestPeers(this.components.peerId.toBytes(), {
|
|
90
87
|
signal
|
|
91
88
|
}),
|
|
92
89
|
(source) => take(source, this.count),
|
|
@@ -7,8 +7,10 @@ import { logger } from '@libp2p/logger'
|
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
8
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
9
|
import type { Logger } from '@libp2p/logger'
|
|
10
|
-
import { Components, Initializable } from '@libp2p/components'
|
|
11
10
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
11
|
+
import type { Metrics } from '@libp2p/interface-metrics'
|
|
12
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
13
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
12
14
|
|
|
13
15
|
export const KAD_CLOSE_TAG_NAME = 'kad-close'
|
|
14
16
|
export const KAD_CLOSE_TAG_VALUE = 50
|
|
@@ -66,17 +68,24 @@ export interface RoutingTableInit {
|
|
|
66
68
|
tagValue?: number
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
export interface RoutingTableComponents {
|
|
72
|
+
peerId: PeerId
|
|
73
|
+
peerStore: PeerStore
|
|
74
|
+
connectionManager: ConnectionManager
|
|
75
|
+
metrics?: Metrics
|
|
76
|
+
}
|
|
77
|
+
|
|
69
78
|
/**
|
|
70
79
|
* A wrapper around `k-bucket`, to provide easy store and
|
|
71
80
|
* retrieval for peers.
|
|
72
81
|
*/
|
|
73
|
-
export class RoutingTable implements Startable
|
|
82
|
+
export class RoutingTable implements Startable {
|
|
74
83
|
public kBucketSize: number
|
|
75
84
|
public kb?: KBucketTree
|
|
76
85
|
public pingQueue: Queue
|
|
77
86
|
|
|
78
87
|
private readonly log: Logger
|
|
79
|
-
private components:
|
|
88
|
+
private readonly components: RoutingTableComponents
|
|
80
89
|
private readonly lan: boolean
|
|
81
90
|
private readonly pingTimeout: number
|
|
82
91
|
private readonly pingConcurrency: number
|
|
@@ -85,9 +94,10 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
85
94
|
private readonly tagName: string
|
|
86
95
|
private readonly tagValue: number
|
|
87
96
|
|
|
88
|
-
constructor (init: RoutingTableInit) {
|
|
97
|
+
constructor (components: RoutingTableComponents, init: RoutingTableInit) {
|
|
89
98
|
const { kBucketSize, pingTimeout, lan, pingConcurrency, protocol, tagName, tagValue } = init
|
|
90
99
|
|
|
100
|
+
this.components = components
|
|
91
101
|
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:routing-table`)
|
|
92
102
|
this.kBucketSize = kBucketSize ?? KBUCKET_SIZE
|
|
93
103
|
this.pingTimeout = pingTimeout ?? PING_TIMEOUT
|
|
@@ -99,13 +109,13 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
99
109
|
this.tagValue = tagValue ?? KAD_CLOSE_TAG_VALUE
|
|
100
110
|
|
|
101
111
|
const updatePingQueueSizeMetric = () => {
|
|
102
|
-
this.components.
|
|
112
|
+
this.components.metrics?.updateComponentMetric({
|
|
103
113
|
system: 'libp2p',
|
|
104
114
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
105
115
|
metric: METRIC_PING_QUEUE_SIZE,
|
|
106
116
|
value: this.pingQueue.size
|
|
107
117
|
})
|
|
108
|
-
this.components.
|
|
118
|
+
this.components.metrics?.updateComponentMetric({
|
|
109
119
|
system: 'libp2p',
|
|
110
120
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
111
121
|
metric: METRIC_PING_RUNNING,
|
|
@@ -120,10 +130,6 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
120
130
|
this._onPing = this._onPing.bind(this)
|
|
121
131
|
}
|
|
122
132
|
|
|
123
|
-
init (components: Components): void {
|
|
124
|
-
this.components = components
|
|
125
|
-
}
|
|
126
|
-
|
|
127
133
|
isStarted () {
|
|
128
134
|
return this.running
|
|
129
135
|
}
|
|
@@ -132,7 +138,7 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
132
138
|
this.running = true
|
|
133
139
|
|
|
134
140
|
const kBuck: KBucketTree = new KBuck({
|
|
135
|
-
localNodeId: await utils.convertPeerId(this.components.
|
|
141
|
+
localNodeId: await utils.convertPeerId(this.components.peerId),
|
|
136
142
|
numberOfNodesPerKBucket: this.kBucketSize,
|
|
137
143
|
numberOfNodesToPing: 1
|
|
138
144
|
})
|
|
@@ -169,13 +175,13 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
169
175
|
Promise.resolve()
|
|
170
176
|
.then(async () => {
|
|
171
177
|
for (const peer of addedPeers) {
|
|
172
|
-
await this.components.
|
|
178
|
+
await this.components.peerStore.tagPeer(peer, this.tagName, {
|
|
173
179
|
value: this.tagValue
|
|
174
180
|
})
|
|
175
181
|
}
|
|
176
182
|
|
|
177
183
|
for (const peer of removedPeers) {
|
|
178
|
-
await this.components.
|
|
184
|
+
await this.components.peerStore.unTagPeer(peer, this.tagName)
|
|
179
185
|
}
|
|
180
186
|
})
|
|
181
187
|
.catch(err => {
|
|
@@ -227,7 +233,7 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
227
233
|
}
|
|
228
234
|
|
|
229
235
|
this.log('pinging old contact %p', oldContact.peer)
|
|
230
|
-
const connection = await this.components.
|
|
236
|
+
const connection = await this.components.connectionManager.openConnection(oldContact.peer, options)
|
|
231
237
|
const stream = await connection.newStream(this.protocol, options)
|
|
232
238
|
stream.close()
|
|
233
239
|
responded++
|
|
@@ -244,7 +250,7 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
244
250
|
timeoutController.clear()
|
|
245
251
|
}
|
|
246
252
|
|
|
247
|
-
this.components.
|
|
253
|
+
this.components.metrics?.updateComponentMetric({
|
|
248
254
|
system: 'libp2p',
|
|
249
255
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
250
256
|
metric: METRIC_ROUTING_TABLE_SIZE,
|
|
@@ -334,7 +340,7 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
334
340
|
|
|
335
341
|
this.log('added %p with kad id %b', peer, id)
|
|
336
342
|
|
|
337
|
-
this.components.
|
|
343
|
+
this.components.metrics?.updateComponentMetric({
|
|
338
344
|
system: 'libp2p',
|
|
339
345
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
340
346
|
metric: METRIC_ROUTING_TABLE_SIZE,
|
|
@@ -354,7 +360,7 @@ export class RoutingTable implements Startable, Initializable {
|
|
|
354
360
|
|
|
355
361
|
this.kb.remove(id)
|
|
356
362
|
|
|
357
|
-
this.components.
|
|
363
|
+
this.components.metrics?.updateComponentMetric({
|
|
358
364
|
system: 'libp2p',
|
|
359
365
|
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
|
|
360
366
|
metric: METRIC_ROUTING_TABLE_SIZE,
|
|
@@ -10,7 +10,6 @@ import { TABLE_REFRESH_INTERVAL, TABLE_REFRESH_QUERY_TIMEOUT } from '../constant
|
|
|
10
10
|
import type { RoutingTable } from './index.js'
|
|
11
11
|
import type { Logger } from '@libp2p/logger'
|
|
12
12
|
import type { PeerRouting } from '../peer-routing/index.js'
|
|
13
|
-
import type { Components, Initializable } from '@libp2p/components'
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* Cannot generate random KadIds longer than this + 1
|
|
@@ -29,7 +28,7 @@ export interface RoutingTableRefreshInit {
|
|
|
29
28
|
* A wrapper around `k-bucket`, to provide easy store and
|
|
30
29
|
* retrieval for peers.
|
|
31
30
|
*/
|
|
32
|
-
export class RoutingTableRefresh
|
|
31
|
+
export class RoutingTableRefresh {
|
|
33
32
|
private readonly log: Logger
|
|
34
33
|
private readonly peerRouting: PeerRouting
|
|
35
34
|
private readonly routingTable: RoutingTable
|
|
@@ -50,10 +49,6 @@ export class RoutingTableRefresh implements Initializable {
|
|
|
50
49
|
this.refreshTable = this.refreshTable.bind(this)
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
init (components: Components): void {
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
52
|
async start () {
|
|
58
53
|
this.log(`refreshing routing table every ${this.refreshInterval}ms`)
|
|
59
54
|
this.refreshTable(true)
|
|
@@ -5,7 +5,6 @@ import type { Providers } from '../../providers'
|
|
|
5
5
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
6
6
|
import type { DHTMessageHandler } from '../index.js'
|
|
7
7
|
import type { Message } from '../../message/index.js'
|
|
8
|
-
import type { Initializable } from '@libp2p/components'
|
|
9
8
|
|
|
10
9
|
const log = logger('libp2p:kad-dht:rpc:handlers:add-provider')
|
|
11
10
|
|
|
@@ -13,7 +12,7 @@ export interface AddProviderHandlerInit {
|
|
|
13
12
|
providers: Providers
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
export class AddProviderHandler implements DHTMessageHandler
|
|
15
|
+
export class AddProviderHandler implements DHTMessageHandler {
|
|
17
16
|
private readonly providers: Providers
|
|
18
17
|
|
|
19
18
|
constructor (init: AddProviderHandlerInit) {
|
|
@@ -21,10 +20,6 @@ export class AddProviderHandler implements DHTMessageHandler, Initializable {
|
|
|
21
20
|
this.providers = providers
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
init (): void {
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
23
|
async handle (peerId: PeerId, msg: Message) {
|
|
29
24
|
log('start')
|
|
30
25
|
|
|
@@ -5,13 +5,12 @@ import {
|
|
|
5
5
|
removePublicAddresses
|
|
6
6
|
} from '../../utils.js'
|
|
7
7
|
import { equals as uint8ArrayEquals } from 'uint8arrays'
|
|
8
|
-
import { Components } from '@libp2p/components'
|
|
9
8
|
import { protocols } from '@multiformats/multiaddr'
|
|
10
|
-
import type { Initializable } from '@libp2p/components'
|
|
11
9
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
12
10
|
import type { DHTMessageHandler } from '../index.js'
|
|
13
11
|
import type { PeerRouting } from '../../peer-routing/index.js'
|
|
14
12
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
13
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
15
14
|
|
|
16
15
|
const log = logger('libp2p:kad-dht:rpc:handlers:find-node')
|
|
17
16
|
|
|
@@ -20,19 +19,22 @@ export interface FindNodeHandlerInit {
|
|
|
20
19
|
lan: boolean
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
export
|
|
22
|
+
export interface FindNodeHandlerComponents {
|
|
23
|
+
peerId: PeerId
|
|
24
|
+
addressManager: AddressManager
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class FindNodeHandler implements DHTMessageHandler {
|
|
24
28
|
private readonly peerRouting: PeerRouting
|
|
25
29
|
private readonly lan: boolean
|
|
26
|
-
private components
|
|
30
|
+
private readonly components: FindNodeHandlerComponents
|
|
27
31
|
|
|
28
|
-
constructor (init: FindNodeHandlerInit) {
|
|
32
|
+
constructor (components: FindNodeHandlerComponents, init: FindNodeHandlerInit) {
|
|
29
33
|
const { peerRouting, lan } = init
|
|
30
|
-
this.peerRouting = peerRouting
|
|
31
|
-
this.lan = Boolean(lan)
|
|
32
|
-
}
|
|
33
34
|
|
|
34
|
-
init (components: Components): void {
|
|
35
35
|
this.components = components
|
|
36
|
+
this.peerRouting = peerRouting
|
|
37
|
+
this.lan = Boolean(lan)
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
/**
|
|
@@ -43,10 +45,10 @@ export class FindNodeHandler implements DHTMessageHandler, Initializable {
|
|
|
43
45
|
|
|
44
46
|
let closer: PeerInfo[] = []
|
|
45
47
|
|
|
46
|
-
if (uint8ArrayEquals(this.components.
|
|
48
|
+
if (uint8ArrayEquals(this.components.peerId.toBytes(), msg.key)) {
|
|
47
49
|
closer = [{
|
|
48
|
-
id: this.components.
|
|
49
|
-
multiaddrs: this.components.
|
|
50
|
+
id: this.components.peerId,
|
|
51
|
+
multiaddrs: this.components.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code)),
|
|
50
52
|
protocols: []
|
|
51
53
|
}]
|
|
52
54
|
} else {
|
|
@@ -11,7 +11,7 @@ import type { Providers } from '../../providers.js'
|
|
|
11
11
|
import type { PeerRouting } from '../../peer-routing/index.js'
|
|
12
12
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
13
13
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
14
|
-
import {
|
|
14
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
15
15
|
|
|
16
16
|
const log = logger('libp2p:kad-dht:rpc:handlers:get-providers')
|
|
17
17
|
|
|
@@ -21,24 +21,25 @@ export interface GetProvidersHandlerInit {
|
|
|
21
21
|
lan: boolean
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export
|
|
25
|
-
|
|
24
|
+
export interface GetProvidersHandlerComponents {
|
|
25
|
+
peerStore: PeerStore
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class GetProvidersHandler implements DHTMessageHandler {
|
|
29
|
+
private readonly components: GetProvidersHandlerComponents
|
|
26
30
|
private readonly peerRouting: PeerRouting
|
|
27
31
|
private readonly providers: Providers
|
|
28
32
|
private readonly lan: boolean
|
|
29
33
|
|
|
30
|
-
constructor (init: GetProvidersHandlerInit) {
|
|
34
|
+
constructor (components: GetProvidersHandlerComponents, init: GetProvidersHandlerInit) {
|
|
31
35
|
const { peerRouting, providers, lan } = init
|
|
32
36
|
|
|
37
|
+
this.components = components
|
|
33
38
|
this.peerRouting = peerRouting
|
|
34
39
|
this.providers = providers
|
|
35
40
|
this.lan = Boolean(lan)
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
init (components: Components): void {
|
|
39
|
-
this.components = components
|
|
40
|
-
}
|
|
41
|
-
|
|
42
43
|
async handle (peerId: PeerId, msg: Message) {
|
|
43
44
|
let cid
|
|
44
45
|
try {
|
|
@@ -71,7 +72,7 @@ export class GetProvidersHandler implements DHTMessageHandler, Initializable {
|
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
async _getAddresses (peerId: PeerId) {
|
|
74
|
-
const addrs = await this.components.
|
|
75
|
+
const addrs = await this.components.peerStore.addressBook.get(peerId)
|
|
75
76
|
|
|
76
77
|
return addrs.map(address => address.multiaddr)
|
|
77
78
|
}
|
|
@@ -9,7 +9,8 @@ import { logger } from '@libp2p/logger'
|
|
|
9
9
|
import type { DHTMessageHandler } from '../index.js'
|
|
10
10
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
11
11
|
import type { PeerRouting } from '../../peer-routing/index.js'
|
|
12
|
-
import {
|
|
12
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
13
|
+
import type { Datastore } from 'interface-datastore'
|
|
13
14
|
|
|
14
15
|
const log = logger('libp2p:kad-dht:rpc:handlers:get-value')
|
|
15
16
|
|
|
@@ -17,18 +18,20 @@ export interface GetValueHandlerInit {
|
|
|
17
18
|
peerRouting: PeerRouting
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
export
|
|
21
|
-
|
|
21
|
+
export interface GetValueHandlerComponents {
|
|
22
|
+
peerStore: PeerStore
|
|
23
|
+
datastore: Datastore
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class GetValueHandler implements DHTMessageHandler {
|
|
27
|
+
private readonly components: GetValueHandlerComponents
|
|
22
28
|
private readonly peerRouting: PeerRouting
|
|
23
29
|
|
|
24
|
-
constructor (init: GetValueHandlerInit) {
|
|
30
|
+
constructor (components: GetValueHandlerComponents, init: GetValueHandlerInit) {
|
|
25
31
|
const { peerRouting } = init
|
|
26
32
|
|
|
27
|
-
this.peerRouting = peerRouting
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
init (components: Components): void {
|
|
31
33
|
this.components = components
|
|
34
|
+
this.peerRouting = peerRouting
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
async handle (peerId: PeerId, msg: Message) {
|
|
@@ -48,7 +51,7 @@ export class GetValueHandler implements DHTMessageHandler, Initializable {
|
|
|
48
51
|
let pubKey: Uint8Array | undefined
|
|
49
52
|
|
|
50
53
|
try {
|
|
51
|
-
const key = await this.components.
|
|
54
|
+
const key = await this.components.peerStore.keyBook.get(idFromKey)
|
|
52
55
|
|
|
53
56
|
if (key == null) {
|
|
54
57
|
throw errcode(new Error('No public key found in key book'), 'ERR_NOT_FOUND')
|
|
@@ -99,7 +102,7 @@ export class GetValueHandler implements DHTMessageHandler, Initializable {
|
|
|
99
102
|
// Fetch value from ds
|
|
100
103
|
let rawRecord
|
|
101
104
|
try {
|
|
102
|
-
rawRecord = await this.components.
|
|
105
|
+
rawRecord = await this.components.datastore.get(dsKey)
|
|
103
106
|
} catch (err: any) {
|
|
104
107
|
if (err.code === 'ERR_NOT_FOUND') {
|
|
105
108
|
return undefined
|
|
@@ -118,7 +121,7 @@ export class GetValueHandler implements DHTMessageHandler, Initializable {
|
|
|
118
121
|
if (record.timeReceived == null ||
|
|
119
122
|
Date.now() - record.timeReceived.getTime() > MAX_RECORD_AGE) {
|
|
120
123
|
// If record is bad delete it and return
|
|
121
|
-
await this.components.
|
|
124
|
+
await this.components.datastore.delete(dsKey)
|
|
122
125
|
return undefined
|
|
123
126
|
}
|
|
124
127
|
|
package/src/rpc/handlers/ping.ts
CHANGED
|
@@ -2,17 +2,12 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import type { Message } from '../../message/index.js'
|
|
3
3
|
import type { DHTMessageHandler } from '../index.js'
|
|
4
4
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
5
|
-
import type { Initializable } from '@libp2p/components'
|
|
6
5
|
|
|
7
6
|
const log = logger('libp2p:kad-dht:rpc:handlers:ping')
|
|
8
7
|
|
|
9
|
-
export class PingHandler implements DHTMessageHandler
|
|
8
|
+
export class PingHandler implements DHTMessageHandler {
|
|
10
9
|
async handle (peerId: PeerId, msg: Message) {
|
|
11
10
|
log('ping from %p', peerId)
|
|
12
11
|
return msg
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
init (): void {
|
|
16
|
-
|
|
17
|
-
}
|
|
18
13
|
}
|
|
@@ -6,28 +6,29 @@ import type { DHTMessageHandler } from '../index.js'
|
|
|
6
6
|
import type { Validators } from '@libp2p/interface-dht'
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
8
|
import type { Message } from '../../message/index.js'
|
|
9
|
-
import {
|
|
9
|
+
import type { Datastore } from 'interface-datastore'
|
|
10
10
|
|
|
11
11
|
export interface PutValueHandlerInit {
|
|
12
12
|
validators: Validators
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export
|
|
15
|
+
export interface PutValueHandlerComponents {
|
|
16
|
+
datastore: Datastore
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class PutValueHandler implements DHTMessageHandler {
|
|
16
20
|
private readonly log: Logger
|
|
17
|
-
private components:
|
|
21
|
+
private readonly components: PutValueHandlerComponents
|
|
18
22
|
private readonly validators: Validators
|
|
19
23
|
|
|
20
|
-
constructor (init: PutValueHandlerInit) {
|
|
24
|
+
constructor (components: PutValueHandlerComponents, init: PutValueHandlerInit) {
|
|
21
25
|
const { validators } = init
|
|
22
26
|
|
|
27
|
+
this.components = components
|
|
23
28
|
this.log = logger('libp2p:kad-dht:rpc:handlers:put-value')
|
|
24
29
|
this.validators = validators
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
init (components: Components): void {
|
|
28
|
-
this.components = components
|
|
29
|
-
}
|
|
30
|
-
|
|
31
32
|
async handle (peerId: PeerId, msg: Message) {
|
|
32
33
|
const key = msg.key
|
|
33
34
|
this.log('%p asked us to store value for key %b', peerId, key)
|
|
@@ -46,7 +47,7 @@ export class PutValueHandler implements DHTMessageHandler, Initializable {
|
|
|
46
47
|
|
|
47
48
|
record.timeReceived = new Date()
|
|
48
49
|
const recordKey = bufferToRecordKey(record.key)
|
|
49
|
-
await this.components.
|
|
50
|
+
await this.components.datastore.put(recordKey, record.serialize().subarray())
|
|
50
51
|
this.log('put record for %b into datastore under key %k', key, recordKey)
|
|
51
52
|
} catch (err: any) {
|
|
52
53
|
this.log('did not put record for key %b into datastore %o', key, err)
|