-
Notifications
You must be signed in to change notification settings - Fork 15
/
events.js
361 lines (319 loc) · 9.79 KB
/
events.js
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { Aggregator, Dealer } from '@web3-storage/filecoin-client'
import { Aggregate, Piece } from '@web3-storage/data-segment'
import { CBOR } from '@ucanto/core'
import {
getBufferedPieces,
aggregatePieces,
handleBufferReducingWithoutAggregate,
handleBufferReducingWithAggregate,
} from './buffer-reducing.js'
import {
StoreOperationFailed,
QueueOperationFailed,
UnexpectedState,
} from '../errors.js'
/**
* On piece queue messages, store piece.
*
* @param {import('./api.js').PieceMessageContext} context
* @param {import('./api.js').PieceMessage} message
*/
export const handlePieceMessage = async (context, message) => {
const { piece, group } = message
// Store piece into the store. Store events MAY be used to propagate piece over
const putRes = await context.pieceStore.put({
piece,
group,
status: 'offered',
insertedAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
if (putRes.error) {
return {
error: new StoreOperationFailed(putRes.error.message),
}
}
return { ok: {} }
}
/**
* On Piece store insert batch, buffer pieces together to resume buffer processing.
*
* @param {import('./api.js').PieceInsertEventContext} context
* @param {import('./api.js').PieceRecord[]} records
*/
export const handlePiecesInsert = async (context, records) => {
// TODO: separate buffers per group after MVP
const { group } = records[0]
/** @type {import('./api.js').Buffer} */
const buffer = {
pieces: records.map((p) => ({
piece: p.piece,
insertedAt: p.insertedAt,
// Set policy as insertion
policy: /** @type {import('./api.js').PiecePolicy} */ (0),
})),
group,
}
const block = await CBOR.write(buffer)
// Store block in buffer store
const bufferStorePut = await context.bufferStore.put({
buffer,
block: block.cid,
})
if (bufferStorePut.error) {
return bufferStorePut
}
// Propagate message
const bufferQueueAdd = await context.bufferQueue.add({
pieces: block.cid,
group,
})
if (bufferQueueAdd.error) {
return {
error: new QueueOperationFailed(bufferQueueAdd.error.message),
}
}
return { ok: {} }
}
/**
* On buffer queue messages, reduce received buffer records into a bigger buffer.
* - If new buffer does not have enough load to build an aggregate, it is stored
* and requeued for buffer reducing
* - If new buffer has enough load to build an aggregate, it is stored and queued
* into aggregateOfferQueue. Remaining of the new buffer (in case buffer bigger
* than maximum aggregate size) is re-queued into the buffer queue.
*
* @param {import('./api.js').BufferMessageContext} context
* @param {import('./api.js').BufferMessage[]} records
*/
export const handleBufferQueueMessage = async (context, records) => {
// Get reduced buffered pieces
const buffers = records.map((r) => r.pieces)
const { error: errorGetBufferedPieces, ok: okGetBufferedPieces } =
await getBufferedPieces(buffers, context.bufferStore)
if (errorGetBufferedPieces) {
return { error: errorGetBufferedPieces }
}
const { bufferedPieces, group } = okGetBufferedPieces
// Attempt to aggregate buffered pieces within the ranges.
// In case it is possible, included pieces and remaining pieces are returned
// so that they can be propagated to respective stores/queues.
const aggregateInfo = aggregatePieces(bufferedPieces, {
maxAggregateSize: context.config.maxAggregateSize,
minAggregateSize: context.config.minAggregateSize,
minUtilizationFactor: context.config.minUtilizationFactor,
prependBufferedPieces: context.config.prependBufferedPieces,
})
// Store buffered pieces if not enough to do aggregate and re-queue them
if (!aggregateInfo) {
const { error: errorHandleBufferReducingWithoutAggregate } =
await handleBufferReducingWithoutAggregate({
buffer: {
pieces: bufferedPieces,
group,
},
bufferStore: context.bufferStore,
bufferQueue: context.bufferQueue,
})
if (errorHandleBufferReducingWithoutAggregate) {
return { error: errorHandleBufferReducingWithoutAggregate }
}
// No pieces were aggregate
return {
ok: {
aggregatedPieces: 0,
},
}
}
// Store buffered pieces to do aggregate and re-queue remaining ones
const { error: errorHandleBufferReducingWithAggregate } =
await handleBufferReducingWithAggregate({
aggregateInfo,
bufferStore: context.bufferStore,
bufferQueue: context.bufferQueue,
aggregateOfferQueue: context.aggregateOfferQueue,
group,
})
if (errorHandleBufferReducingWithAggregate) {
return { error: errorHandleBufferReducingWithAggregate }
}
return {
ok: {
aggregatedPieces: aggregateInfo.addedBufferedPieces.length,
},
}
}
/**
* On aggregate offer queue message, store aggregate record in store.
*
* @param {import('./api.js').AggregateOfferMessageContext} context
* @param {import('./api.js').AggregateOfferMessage} message
*/
export const handleAggregateOfferMessage = async (context, message) => {
const { pieces, aggregate, buffer, group, minPieceInsertedAt } = message
// Store aggregate information into the store. Store events MAY be used to propagate aggregate over
const putRes = await context.aggregateStore.put({
pieces,
aggregate,
buffer,
group,
minPieceInsertedAt,
insertedAt: new Date().toISOString(),
})
// TODO: should we ignore error already there?
if (putRes.error) {
return putRes
}
return { ok: {}, error: undefined }
}
/**
* On Aggregate store insert, offer inserted aggregate for deal.
*
* @param {import('./api.js').AggregateInsertEventToPieceAcceptQueueContext} context
* @param {import('./api.js').AggregateRecord} record
*/
export const handleAggregateInsertToPieceAcceptQueue = async (
context,
record
) => {
const bufferStoreRes = await context.bufferStore.get(record.buffer)
if (bufferStoreRes.error) {
return bufferStoreRes
}
// Get pieces from buffer
const pieces = bufferStoreRes.ok.buffer.pieces.map((p) =>
Piece.fromLink(p.piece)
)
const aggregate = bufferStoreRes.ok.buffer.aggregate
const aggregateBuilder = Aggregate.build({
pieces,
size: Aggregate.Size.from(context.config.maxAggregateSize),
})
if (aggregate && !aggregateBuilder.link.equals(aggregate)) {
return {
error: new UnexpectedState(
`invalid aggregate computed for ${bufferStoreRes.ok.block.link}`
),
}
}
// TODO: Batch per a maximum to queue
for (const piece of pieces) {
const inclusionProof = aggregateBuilder.resolveProof(piece.link)
if (inclusionProof.error) {
return inclusionProof
}
const addMessage = await context.pieceAcceptQueue.add({
piece: piece.link,
aggregate: aggregateBuilder.link,
group: bufferStoreRes.ok.buffer.group,
inclusion: {
subtree: inclusionProof.ok[0],
index: inclusionProof.ok[1],
},
})
if (addMessage.error) {
return addMessage
}
}
return {
ok: {},
}
}
/**
* On piece accept queue message, store inclusion record in store.
*
* @param {import('./api.js').PieceAcceptMessageContext} context
* @param {import('./api.js').PieceAcceptMessage} message
*/
export const handlePieceAcceptMessage = async (context, message) => {
const { piece, aggregate, group, inclusion } = message
// Store inclusion information into the store. Store events MAY be used to propagate inclusion over
const putRes = await context.inclusionStore.put({
piece,
aggregate,
group,
inclusion,
insertedAt: new Date().toISOString(),
})
// TODO: should we ignore error already there?
if (putRes.error) {
return putRes
}
return { ok: {}, error: undefined }
}
/**
* On Inclusion store insert, piece table can be updated to reflect piece state.
*
* @param {import('./api.js').InclusionInsertEventToUpdateState} context
* @param {Pick<import('./api.js').InclusionRecord, 'piece' | 'group'>} record
*/
export const handleInclusionInsertToUpdateState = async (context, record) => {
const updateRes = await context.pieceStore.update(
{
piece: record.piece,
group: record.group,
},
{
status: 'accepted',
updatedAt: new Date().toISOString(),
}
)
if (updateRes.error) {
return updateRes
}
return { ok: {}, error: undefined }
}
/**
* @param {import('./api.js').InclusionInsertEventToIssuePieceAccept} context
* @param {Pick<import('./api.js').InclusionRecord, 'piece' | 'group'>} record
*/
export const handleInclusionInsertToIssuePieceAccept = async (
context,
record
) => {
// invoke piece/accept to issue receipt
const pieceAcceptInv = await Aggregator.pieceAccept(
context.aggregatorService.invocationConfig,
record.piece,
record.group,
{ connection: context.aggregatorService.connection }
)
if (pieceAcceptInv.out.error) {
return {
error: pieceAcceptInv.out.error,
}
}
return { ok: {} }
}
/**
* On Aggregate store insert, offer inserted aggregate for deal.
*
* @param {import('./api.js').AggregateInsertEventToAggregateOfferContext} context
* @param {import('./api.js').AggregateRecord} record
*/
export const handleAggregateInsertToAggregateOffer = async (
context,
record
) => {
const bufferStoreRes = await context.bufferStore.get(record.buffer)
if (bufferStoreRes.error) {
return {
error: bufferStoreRes.error,
}
}
// Get pieces from buffer
const pieces = bufferStoreRes.ok.buffer.pieces.map((p) => p.piece)
// invoke aggregate/offer
const aggregateOfferInv = await Dealer.aggregateOffer(
context.dealerService.invocationConfig,
record.aggregate,
pieces,
{ connection: context.dealerService.connection }
)
if (aggregateOfferInv.out.error) {
return {
error: aggregateOfferInv.out.error,
}
}
return { ok: {} }
}