@sentio/runtime 2.40.0-rc.1 → 2.40.0-rc.2

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/src/service.ts CHANGED
@@ -11,6 +11,8 @@ import {
11
11
  HandlerType,
12
12
  PreparedData,
13
13
  PreprocessResult,
14
+ PreprocessStreamRequest,
15
+ PreprocessStreamResponse,
14
16
  ProcessBindingResponse,
15
17
  ProcessBindingsRequest,
16
18
  ProcessConfigRequest,
@@ -124,7 +126,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
124
126
  }
125
127
 
126
128
  async processBindings(request: ProcessBindingsRequest, options?: CallContext): Promise<ProcessBindingResponse> {
127
- const ethCallResults = await this.preprocessBindings(request, options)
129
+ const ethCallResults = await this.preprocessBindings(request.bindings, undefined, options)
128
130
 
129
131
  const promises = []
130
132
  for (const binding of request.bindings) {
@@ -154,12 +156,14 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
154
156
  }
155
157
 
156
158
  async preprocessBindings(
157
- request: ProcessBindingsRequest,
159
+ bindings: DataBinding[],
160
+ dbContext?: StoreContext,
158
161
  options?: CallContext
159
162
  ): Promise<{ [calldata: string]: any[] }> {
163
+ console.log('preprocessBindings start')
160
164
  const promises = []
161
- for (const binding of request.bindings) {
162
- promises.push(this.preprocessBinding(binding, options))
165
+ for (const binding of bindings) {
166
+ promises.push(this.preprocessBinding(binding, dbContext, options))
163
167
  }
164
168
  let preprocessResults: PreprocessResult[]
165
169
  try {
@@ -167,6 +171,10 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
167
171
  } catch (e) {
168
172
  throw e
169
173
  }
174
+ console.log(
175
+ 'ethCallParams: ',
176
+ preprocessResults.map((r) => r.ethCallParams)
177
+ )
170
178
  const groupedRequests = new Map<string, EthCallParam[]>()
171
179
  const providers = new Map<string, Provider>()
172
180
  for (const result of preprocessResults) {
@@ -205,7 +213,11 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
205
213
  return results
206
214
  }
207
215
 
208
- async preprocessBinding(request: DataBinding, options?: CallContext): Promise<PreprocessResult> {
216
+ async preprocessBinding(
217
+ request: DataBinding,
218
+ dbContext?: StoreContext,
219
+ options?: CallContext
220
+ ): Promise<PreprocessResult> {
209
221
  if (!this.started) {
210
222
  throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
211
223
  }
@@ -221,7 +233,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
221
233
  ]
222
234
  )
223
235
  }
224
- return await PluginManager.INSTANCE.preprocessBinding(request)
236
+ return await PluginManager.INSTANCE.preprocessBinding(request, dbContext)
225
237
  }
226
238
 
227
239
  async processBinding(
@@ -266,6 +278,65 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
266
278
  yield* from(subject).pipe(withAbort(context.signal))
267
279
  }
268
280
 
281
+ async handlePreprocessRequests(
282
+ requests: AsyncIterable<PreprocessStreamRequest>,
283
+ subject: Subject<DeepPartial<PreprocessStreamResponse>>
284
+ ) {
285
+ const contexts = new Contexts()
286
+
287
+ for await (const request of requests) {
288
+ try {
289
+ console.debug('received request:', request)
290
+ if (request.bindings) {
291
+ const bindings = request.bindings.bindings
292
+ const dbContext = contexts.new(request.processId, subject)
293
+ const start = Date.now()
294
+ this.preprocessBindings(bindings, dbContext)
295
+ .then(() => {
296
+ subject.next({
297
+ processId: request.processId
298
+ })
299
+ })
300
+ .catch((e) => {
301
+ console.debug(e)
302
+ dbContext.error(request.processId, e)
303
+ process_binding_error.add(1)
304
+ })
305
+ .finally(() => {
306
+ const cost = Date.now() - start
307
+ console.debug('preprocessBinding', request.processId, ' took', cost, 'ms')
308
+ process_binding_time.add(cost)
309
+ contexts.delete(request.processId)
310
+ })
311
+ }
312
+ if (request.dbResult) {
313
+ const dbContext = contexts.get(request.processId)
314
+ dbContext?.result(request.dbResult)
315
+ }
316
+ } catch (e) {
317
+ // should not happen
318
+ console.error('unexpect error during handle loop', e)
319
+ }
320
+ }
321
+ }
322
+
323
+ async *preprocessBindingsStream(requests: AsyncIterable<PreprocessStreamRequest>, context: CallContext) {
324
+ if (!this.started) {
325
+ throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
326
+ }
327
+
328
+ const subject = new Subject<DeepPartial<PreprocessStreamResponse>>()
329
+ this.handlePreprocessRequests(requests, subject)
330
+ .then(() => {
331
+ subject.complete()
332
+ })
333
+ .catch((e) => {
334
+ console.error(e)
335
+ subject.error(e)
336
+ })
337
+ yield* from(subject).pipe(withAbort(context.signal))
338
+ }
339
+
269
340
  private async handleRequests(
270
341
  requests: AsyncIterable<ProcessStreamRequest>,
271
342
  subject: Subject<DeepPartial<ProcessStreamResponse>>