@sentio/runtime 2.61.0-rc.8 → 2.61.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/src/plugin.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  DataBinding,
3
3
  HandlerType,
4
- InitResponse,
5
4
  PreparedData,
6
5
  PreprocessResult,
7
6
  ProcessConfigResponse,
8
7
  ProcessResult,
9
8
  ProcessStreamResponse_Partitions,
10
9
  ProcessStreamResponse_Partitions_Partition_SysValue,
11
- StartRequest
10
+ StartRequest,
11
+ UpdateTemplatesRequest
12
12
  } from '@sentio/protos'
13
13
  import { IStoreContext, StoreContext } from './db-context.js'
14
14
  import { AsyncLocalStorage } from 'node:async_hooks'
@@ -60,12 +60,6 @@ export abstract class Plugin {
60
60
  * method used by action server only
61
61
  */
62
62
  shutdownServer() {}
63
-
64
- /**
65
- * Initialize the plugin, for service v3.
66
- * @param config
67
- */
68
- async init(config: InitResponse): Promise<void> {}
69
63
  }
70
64
 
71
65
  export class PluginManager {
@@ -90,8 +84,10 @@ export class PluginManager {
90
84
  }
91
85
  }
92
86
 
93
- async configure(config: ProcessConfigResponse, forChainId?: string): Promise<void> {
94
- await Promise.all(this.plugins.map((plugin) => plugin.configure(config, forChainId)))
87
+ async configure(config: ProcessConfigResponse): Promise<void> {
88
+ for (const plugin of this.plugins) {
89
+ await plugin.configure(config)
90
+ }
95
91
  }
96
92
 
97
93
  start(start: StartRequest, actionServerPort?: number) {
@@ -149,7 +145,11 @@ export class PluginManager {
149
145
  })
150
146
  }
151
147
 
152
- async init(resp: InitResponse) {
153
- return Promise.all(this.plugins.map((plugin) => plugin.init(resp)))
148
+ async updateTemplates(request: UpdateTemplatesRequest) {
149
+ for (const plugin of this.plugins) {
150
+ await plugin.start({
151
+ templateInstances: request.templateInstances
152
+ })
153
+ }
154
154
  }
155
155
  }
package/src/service-v3.ts CHANGED
@@ -1,20 +1,19 @@
1
1
  import {
2
- ConfigureHandlersRequest,
3
- ConfigureHandlersResponse,
4
2
  DataBinding,
5
3
  DeepPartial,
6
4
  Empty,
7
5
  HandlerType,
8
- InitResponse,
6
+ ProcessConfigRequest,
9
7
  ProcessConfigResponse,
10
8
  ProcessorV3ServiceImplementation,
11
9
  ProcessResult,
12
10
  ProcessStreamRequest,
13
11
  ProcessStreamResponse,
14
- ProcessStreamResponseV2,
15
- StartRequest
12
+ ProcessStreamResponseV3,
13
+ StartRequest,
14
+ UpdateTemplatesRequest
16
15
  } from '@sentio/protos'
17
- import { CallContext } from 'nice-grpc'
16
+ import { CallContext, ServerError, Status } from 'nice-grpc'
18
17
  import { AsyncIterable } from 'ix'
19
18
  import { PluginManager } from './plugin.js'
20
19
  import { Subject } from 'rxjs'
@@ -26,6 +25,7 @@ import { processMetrics } from './metrics.js'
26
25
  import { recordRuntimeInfo } from './service.js'
27
26
  import { DataBindingContext } from './db-context.js'
28
27
  import { TemplateInstanceState } from './state.js'
28
+ import { freezeGlobalConfig } from './global-config.js'
29
29
 
30
30
  const { process_binding_count, process_binding_time, process_binding_error } = processMetrics
31
31
 
@@ -33,6 +33,7 @@ export class ProcessorServiceImplV3 implements ProcessorV3ServiceImplementation
33
33
  readonly enablePartition: boolean
34
34
  private readonly loader: () => Promise<any>
35
35
  private readonly shutdownHandler?: () => void
36
+ private started = false
36
37
 
37
38
  constructor(loader: () => Promise<any>, options?: any, shutdownHandler?: () => void) {
38
39
  this.loader = loader
@@ -41,18 +42,37 @@ export class ProcessorServiceImplV3 implements ProcessorV3ServiceImplementation
41
42
  this.enablePartition = options?.['enable-partition'] == true
42
43
  }
43
44
 
44
- async init(request: Empty, context: CallContext): Promise<DeepPartial<InitResponse>> {
45
- await this.loader()
46
- const resp = InitResponse.fromPartial({
47
- chainIds: []
48
- })
49
- await PluginManager.INSTANCE.init(resp)
50
- resp.chainIds = Array.from(new Set(resp.chainIds))
51
- return resp
45
+ async start(request: StartRequest, context: CallContext): Promise<Empty> {
46
+ if (this.started) {
47
+ return {}
48
+ }
49
+
50
+ freezeGlobalConfig()
51
+
52
+ try {
53
+ await this.loader()
54
+ } catch (e) {
55
+ throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))
56
+ }
57
+
58
+ await PluginManager.INSTANCE.start(request)
59
+
60
+ this.started = true
61
+ return {}
62
+ }
63
+
64
+ async getConfig(request: ProcessConfigRequest, context: CallContext): Promise<ProcessConfigResponse> {
65
+ if (!this.started) {
66
+ throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
67
+ }
68
+
69
+ const newConfig = ProcessConfigResponse.fromPartial({})
70
+ await PluginManager.INSTANCE.configure(newConfig)
71
+ return newConfig
52
72
  }
53
73
 
54
74
  async *processBindingsStream(requests: AsyncIterable<ProcessStreamRequest>, context: CallContext) {
55
- const subject = new Subject<DeepPartial<ProcessStreamResponseV2>>()
75
+ const subject = new Subject<DeepPartial<ProcessStreamResponseV3>>()
56
76
  this.handleRequests(requests, subject)
57
77
  .then(() => {
58
78
  subject.complete()
@@ -88,7 +108,7 @@ export class ProcessorServiceImplV3 implements ProcessorV3ServiceImplementation
88
108
  async handleRequest(
89
109
  request: ProcessStreamRequest,
90
110
  lastBinding: DataBinding | undefined,
91
- subject: Subject<DeepPartial<ProcessStreamResponseV2>>
111
+ subject: Subject<DeepPartial<ProcessStreamResponseV3>>
92
112
  ) {
93
113
  if (request.binding) {
94
114
  process_binding_count.add(1)
@@ -141,7 +161,7 @@ export class ProcessorServiceImplV3 implements ProcessorV3ServiceImplementation
141
161
  private startProcess(
142
162
  processId: number,
143
163
  binding: DataBinding,
144
- subject: Subject<DeepPartial<ProcessStreamResponseV2>>
164
+ subject: Subject<DeepPartial<ProcessStreamResponseV3>>
145
165
  ) {
146
166
  const context = this.contexts.new(processId, subject)
147
167
  const start = Date.now()
@@ -194,22 +214,9 @@ export class ProcessorServiceImplV3 implements ProcessorV3ServiceImplementation
194
214
  })
195
215
  }
196
216
 
197
- async configureHandlers(
198
- request: ConfigureHandlersRequest,
199
- context: CallContext
200
- ): Promise<DeepPartial<ConfigureHandlersResponse>> {
201
- await PluginManager.INSTANCE.start(
202
- StartRequest.fromPartial({
203
- templateInstances: request.templateInstances
204
- })
205
- )
206
-
207
- const newConfig = ProcessConfigResponse.fromPartial({})
208
- await PluginManager.INSTANCE.configure(newConfig, request.chainId)
209
- return {
210
- accountConfigs: newConfig.accountConfigs,
211
- contractConfigs: newConfig.contractConfigs
212
- }
217
+ async updateTemplates(request: UpdateTemplatesRequest, context: CallContext): Promise<DeepPartial<Empty>> {
218
+ await PluginManager.INSTANCE.updateTemplates(request)
219
+ return {}
213
220
  }
214
221
  }
215
222
 
@@ -220,7 +227,7 @@ class Contexts {
220
227
  return this.contexts.get(processId)
221
228
  }
222
229
 
223
- new(processId: number, subject: Subject<DeepPartial<ProcessStreamResponseV2>>) {
230
+ new(processId: number, subject: Subject<DeepPartial<ProcessStreamResponseV3>>) {
224
231
  const context = new DataBindingContext(processId, subject)
225
232
  this.contexts.set(processId, context)
226
233
  return context