-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathindex.ts
More file actions
311 lines (262 loc) · 8.84 KB
/
index.ts
File metadata and controls
311 lines (262 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { NotFoundError } from '@libp2p/interface'
import { Libp2pRecord } from '@libp2p/record'
import map from 'it-map'
import parallel from 'it-parallel'
import { pipe } from 'it-pipe'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import {
ALPHA
} from '../constants.js'
import { QueryError } from '../errors.js'
import { MessageType } from '../message/dht.js'
import {
valueEvent,
queryErrorEvent
} from '../query/events.js'
import { bestRecord } from '../record/selectors.js'
import { verifyRecord } from '../record/validators.js'
import { createPutRecord, bufferToRecordKey } from '../utils.js'
import type { KadDHTComponents, Validators, Selectors, ValueEvent, QueryEvent } from '../index.js'
import type { Message } from '../message/dht.js'
import type { Network, SendMessageOptions } from '../network.js'
import type { PeerRouting } from '../peer-routing/index.js'
import type { QueryManager } from '../query/manager.js'
import type { QueryFunc } from '../query/types.js'
import type { AbortOptions, Logger, RoutingOptions } from '@libp2p/interface'
export interface ContentFetchingInit {
validators: Validators
selectors: Selectors
peerRouting: PeerRouting
queryManager: QueryManager
network: Network
logPrefix: string
datastorePrefix: string
}
export class ContentFetching {
private readonly log: Logger
private readonly components: KadDHTComponents
private readonly validators: Validators
private readonly selectors: Selectors
private readonly peerRouting: PeerRouting
private readonly queryManager: QueryManager
private readonly network: Network
private readonly datastorePrefix: string
constructor (components: KadDHTComponents, init: ContentFetchingInit) {
const { validators, selectors, peerRouting, queryManager, network, logPrefix } = init
this.components = components
this.log = components.logger.forComponent(`${logPrefix}:content-fetching`)
this.datastorePrefix = `${init.datastorePrefix}/record`
this.validators = validators
this.selectors = selectors
this.peerRouting = peerRouting
this.queryManager = queryManager
this.network = network
this.get = components.metrics?.traceFunction('libp2p.kadDHT.get', this.get.bind(this), {
optionsIndex: 1
}) ?? this.get
this.put = components.metrics?.traceFunction('libp2p.kadDHT.put', this.put.bind(this), {
optionsIndex: 2
}) ?? this.put
}
/**
* Attempt to retrieve the value for the given key from
* the local datastore
*/
async getLocal (key: Uint8Array, options?: AbortOptions): Promise<Libp2pRecord> {
this.log('getLocal %b', key)
const dsKey = bufferToRecordKey(this.datastorePrefix, key)
this.log('fetching record for key %k', dsKey)
const raw = await this.components.datastore.get(dsKey, options)
this.log('found %k in local datastore', dsKey)
const rec = Libp2pRecord.deserialize(raw)
await verifyRecord(this.validators, rec, options)
return rec
}
/**
* Send the best record found to any peers that have an out of date record
*/
async * sendCorrectionRecord (key: Uint8Array, vals: ValueEvent[], best: Uint8Array, options: SendMessageOptions): AsyncGenerator<QueryEvent> {
this.log('sendCorrection for %b', key)
const fixupRec = createPutRecord(key, best)
for (const { value, from } of vals) {
// no need to do anything
if (uint8ArrayEquals(value, best)) {
this.log('record was ok')
continue
}
// correct ourself
if (this.components.peerId.equals(from)) {
try {
const dsKey = bufferToRecordKey(this.datastorePrefix, key)
this.log(`Storing corrected record for key ${dsKey.toString()}`)
await this.components.datastore.put(dsKey, fixupRec.subarray(), options)
} catch (err: any) {
this.log.error('failed error correcting self - %e', err)
}
continue
}
// send correction
let sentCorrection = false
const request: Partial<Message> = {
type: MessageType.PUT_VALUE,
key,
record: fixupRec
}
for await (const event of this.network.sendRequest(from, request, options)) {
if (event.name === 'PEER_RESPONSE' && (event.record != null) && uint8ArrayEquals(event.record.value, Libp2pRecord.deserialize(fixupRec).value)) {
sentCorrection = true
}
yield event
}
if (!sentCorrection) {
throw new QueryError('Could not send correction')
}
this.log.error('failed error correcting entry')
}
}
/**
* Store the given key/value pair in the DHT
*/
async * put (key: Uint8Array, value: Uint8Array, options: RoutingOptions): AsyncGenerator<unknown, void, undefined> {
this.log('put key %b value %b', key, value)
// create record in the dht format
const record = createPutRecord(key, value)
// store the record locally
const dsKey = bufferToRecordKey(this.datastorePrefix, key)
this.log(`storing record for key ${dsKey.toString()}`)
await this.components.datastore.put(dsKey, record.subarray(), options)
// put record to the closest peers
yield * pipe(
this.peerRouting.getClosestPeers(key, {
...options,
signal: options.signal
}),
(source) => map(source, (event) => {
return async () => {
if (event.name !== 'FINAL_PEER') {
return [event]
}
const events = []
const msg: Partial<Message> = {
type: MessageType.PUT_VALUE,
key,
record
}
this.log('send put to %p', event.peer.id)
for await (const putEvent of this.network.sendRequest(event.peer.id, msg, {
...options,
path: event.path
})) {
events.push(putEvent)
if (putEvent.name !== 'PEER_RESPONSE') {
continue
}
if (!(putEvent.record != null && uint8ArrayEquals(putEvent.record.value, Libp2pRecord.deserialize(record).value))) {
events.push(queryErrorEvent({
from: event.peer.id,
error: new QueryError('Value not put correctly'),
path: putEvent.path
}, options))
}
}
return events
}
}),
(source) => parallel(source, {
ordered: false,
concurrency: ALPHA
}),
async function * (source) {
for await (const events of source) {
yield * events
}
}
)
}
/**
* Get the value to the given key
*/
async * get (key: Uint8Array, options: RoutingOptions): AsyncGenerator<QueryEvent | ValueEvent> {
this.log('get %b', key)
const vals: ValueEvent[] = []
for await (const event of this.getMany(key, options)) {
if (event.name === 'VALUE') {
vals.push(event)
continue
}
yield event
}
if (vals.length === 0) {
return
}
const records = vals.map((v) => v.value)
let i = 0
try {
i = bestRecord(this.selectors, key, records)
} catch (err: any) {
// Assume the first record if no selector available
if (err.name !== 'InvalidParametersError') {
throw err
}
}
const best = records[i]
this.log('GetValue %b %b', key, best)
if (best == null) {
throw new NotFoundError('Best value was not found')
}
yield * this.sendCorrectionRecord(key, vals, best, {
...options,
path: {
index: -1,
queued: 0,
running: 0,
total: 0
}
})
yield vals[i]
}
/**
* Get the `n` values to the given key without sorting
*/
async * getMany (key: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<QueryEvent> {
this.log('getMany values for %b', key)
try {
const localRec = await this.getLocal(key, options)
yield valueEvent({
value: localRec.value,
from: this.components.peerId,
path: {
index: -1,
running: 0,
queued: 0,
total: 0
}
}, options)
} catch (err: any) {
this.log('error getting local value for %b', key, err)
}
const self = this
const getValueQuery: QueryFunc = async function * ({ peer, signal, path }) {
for await (const event of self.peerRouting.getValueOrPeers(peer.id, key, {
...options,
signal,
path
})) {
yield event
if (event.name === 'PEER_RESPONSE' && (event.record != null)) {
yield valueEvent({
from: peer.id,
value: event.record.value,
path
}, options)
}
}
}
// we have peers, lets send the actual query to them
// disable early termination so all close peers respond before we select the best record
yield * this.queryManager.run(key, getValueQuery, {
...options,
disableEarlyTermination: true
})
}
}