envio 3.2.1 → 3.3.0-alpha.1

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.
Files changed (106) hide show
  1. package/package.json +6 -7
  2. package/src/Batch.res +12 -15
  3. package/src/Batch.res.mjs +4 -5
  4. package/src/BatchProcessing.res +199 -0
  5. package/src/BatchProcessing.res.mjs +125 -0
  6. package/src/ChainFetching.res +373 -0
  7. package/src/ChainFetching.res.mjs +206 -0
  8. package/src/ChainMetadata.res +27 -0
  9. package/src/ChainMetadata.res.mjs +27 -0
  10. package/src/ChainState.res +643 -0
  11. package/src/ChainState.res.mjs +476 -0
  12. package/src/ChainState.resi +87 -0
  13. package/src/Config.res +13 -4
  14. package/src/Config.res.mjs +8 -3
  15. package/src/ContractRegisterContext.res +106 -0
  16. package/src/ContractRegisterContext.res.mjs +76 -0
  17. package/src/Core.res +3 -0
  18. package/src/CrossChainState.res +294 -0
  19. package/src/CrossChainState.res.mjs +221 -0
  20. package/src/CrossChainState.resi +39 -0
  21. package/src/Ecosystem.res +58 -0
  22. package/src/Ecosystem.res.mjs +43 -1
  23. package/src/Env.res +0 -2
  24. package/src/Env.res.mjs +0 -3
  25. package/src/EventConfigBuilder.res +2 -2
  26. package/src/EventProcessing.res +60 -46
  27. package/src/EventProcessing.res.mjs +33 -32
  28. package/src/EventUtils.res +0 -4
  29. package/src/EventUtils.res.mjs +0 -4
  30. package/src/ExitOnCaughtUp.res +10 -0
  31. package/src/ExitOnCaughtUp.res.mjs +22 -0
  32. package/src/FetchState.res +205 -73
  33. package/src/FetchState.res.mjs +242 -103
  34. package/src/InMemoryStore.res +36 -451
  35. package/src/InMemoryStore.res.mjs +20 -369
  36. package/src/IndexerLoop.res +43 -0
  37. package/src/IndexerLoop.res.mjs +46 -0
  38. package/src/IndexerState.res +536 -0
  39. package/src/IndexerState.res.mjs +543 -0
  40. package/src/IndexerState.resi +128 -0
  41. package/src/Internal.res +29 -8
  42. package/src/LoadLayer.res +20 -18
  43. package/src/LoadLayer.res.mjs +14 -12
  44. package/src/LoadLayer.resi +6 -3
  45. package/src/Logging.res +11 -44
  46. package/src/Logging.res.mjs +10 -42
  47. package/src/Main.res +54 -79
  48. package/src/Main.res.mjs +44 -56
  49. package/src/PgStorage.res +8 -71
  50. package/src/PgStorage.res.mjs +3 -47
  51. package/src/Prometheus.res +1 -13
  52. package/src/Prometheus.res.mjs +84 -96
  53. package/src/PruneStaleHistory.res +45 -0
  54. package/src/PruneStaleHistory.res.mjs +46 -0
  55. package/src/RawEvent.res +73 -0
  56. package/src/RawEvent.res.mjs +51 -0
  57. package/src/Rollback.res +204 -0
  58. package/src/Rollback.res.mjs +118 -0
  59. package/src/SimulateItems.res +12 -10
  60. package/src/SimulateItems.res.mjs +1 -1
  61. package/src/UserContext.res +26 -114
  62. package/src/UserContext.res.mjs +15 -78
  63. package/src/Writing.res +242 -0
  64. package/src/Writing.res.mjs +215 -0
  65. package/src/db/InternalTable.res +14 -27
  66. package/src/sources/Evm.res +40 -1
  67. package/src/sources/Evm.res.mjs +94 -84
  68. package/src/sources/Fuel.res +40 -1
  69. package/src/sources/Fuel.res.mjs +36 -26
  70. package/src/sources/HyperFuel.res +41 -120
  71. package/src/sources/HyperFuel.res.mjs +42 -80
  72. package/src/sources/HyperFuel.resi +1 -24
  73. package/src/sources/HyperFuelClient.res +16 -297
  74. package/src/sources/HyperFuelClient.res.mjs +5 -4
  75. package/src/sources/HyperFuelSource.res +33 -8
  76. package/src/sources/HyperFuelSource.res.mjs +56 -10
  77. package/src/sources/HyperSyncSource.res +5 -3
  78. package/src/sources/HyperSyncSource.res.mjs +1 -1
  79. package/src/sources/RpcSource.res +2 -2
  80. package/src/sources/RpcSource.res.mjs +1 -1
  81. package/src/sources/SourceManager.res +14 -23
  82. package/src/sources/SourceManager.res.mjs +27 -29
  83. package/src/sources/SourceManager.resi +4 -2
  84. package/src/sources/Svm.res +18 -1
  85. package/src/sources/Svm.res.mjs +30 -22
  86. package/src/sources/SvmHyperSyncSource.res +1 -1
  87. package/src/sources/SvmHyperSyncSource.res.mjs +1 -1
  88. package/src/tui/Tui.res +38 -36
  89. package/src/tui/Tui.res.mjs +51 -51
  90. package/src/ChainFetcher.res +0 -519
  91. package/src/ChainFetcher.res.mjs +0 -314
  92. package/src/ChainManager.res +0 -358
  93. package/src/ChainManager.res.mjs +0 -291
  94. package/src/Ctx.res +0 -6
  95. package/src/Ctx.res.mjs +0 -2
  96. package/src/GlobalState.res +0 -1056
  97. package/src/GlobalState.res.mjs +0 -1086
  98. package/src/GlobalStateManager.res +0 -57
  99. package/src/GlobalStateManager.res.mjs +0 -68
  100. package/src/GlobalStateManager.resi +0 -7
  101. package/src/Ports.res +0 -5
  102. package/src/Ports.res.mjs +0 -9
  103. package/src/adapters/MarkBatchProcessedAdapter.res +0 -5
  104. package/src/adapters/MarkBatchProcessedAdapter.res.mjs +0 -14
  105. package/src/sources/EnvioApiClient.res +0 -15
  106. package/src/sources/EnvioApiClient.res.mjs +0 -24
@@ -0,0 +1,543 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Env from "./Env.res.mjs";
4
+ import * as Utils from "./Utils.res.mjs";
5
+ import * as Config from "./Config.res.mjs";
6
+ import * as Logging from "./Logging.res.mjs";
7
+ import * as ChainMap from "./ChainMap.res.mjs";
8
+ import * as Internal from "./Internal.res.mjs";
9
+ import * as Throttler from "./Throttler.res.mjs";
10
+ import * as ChainState from "./ChainState.res.mjs";
11
+ import * as Prometheus from "./Prometheus.res.mjs";
12
+ import * as LoadManager from "./LoadManager.res.mjs";
13
+ import * as ErrorHandling from "./ErrorHandling.res.mjs";
14
+ import * as InMemoryTable from "./InMemoryTable.res.mjs";
15
+ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
16
+ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
17
+ import * as CrossChainState from "./CrossChainState.res.mjs";
18
+ import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
19
+ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
20
+ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
21
+
22
+ function make() {
23
+ let intervalMillis = Env.ThrottleWrites.pruneStaleDataIntervalMillis;
24
+ let logger = Logging.createChild({
25
+ context: "Throttler for pruning stale entity history data",
26
+ intervalMillis: intervalMillis
27
+ });
28
+ let pruneStaleEntityHistory = Throttler.make(intervalMillis, logger);
29
+ return {
30
+ pruneStaleEntityHistory: pruneStaleEntityHistory
31
+ };
32
+ }
33
+
34
+ let UndefinedEntity = /* @__PURE__ */Primitive_exceptions.create("IndexerState.EntityTables.UndefinedEntity");
35
+
36
+ function make$1(entities) {
37
+ let init = {};
38
+ entities.forEach(entityConfig => {
39
+ init[entityConfig.name] = InMemoryTable.Entity.make();
40
+ });
41
+ return init;
42
+ }
43
+
44
+ function get(self, entityName) {
45
+ let table = self[entityName];
46
+ if (table !== undefined) {
47
+ return table;
48
+ } else {
49
+ return ErrorHandling.mkLogAndRaise(undefined, "Unexpected, entity InMemoryTable is undefined", {
50
+ RE_EXN_ID: UndefinedEntity,
51
+ entityName: entityName
52
+ });
53
+ }
54
+ }
55
+
56
+ function make$2(config, persistence, chainStates, isInReorgThreshold, isRealtime, targetBufferSizeOpt, committedCheckpointIdOpt, isDevelopmentModeOpt, shouldUseTuiOpt, exitAfterFirstEventBlockOpt, onError) {
57
+ let targetBufferSize = targetBufferSizeOpt !== undefined ? targetBufferSizeOpt : CrossChainState.calculateTargetBufferSize();
58
+ let committedCheckpointId = committedCheckpointIdOpt !== undefined ? committedCheckpointIdOpt : Internal.initialCheckpointId;
59
+ let isDevelopmentMode = isDevelopmentModeOpt !== undefined ? isDevelopmentModeOpt : false;
60
+ let shouldUseTui = shouldUseTuiOpt !== undefined ? shouldUseTuiOpt : false;
61
+ let exitAfterFirstEventBlock = exitAfterFirstEventBlockOpt !== undefined ? exitAfterFirstEventBlockOpt : false;
62
+ let intervalMillis = Env.ThrottleWrites.chainMetadataIntervalMillis;
63
+ let chainMetaThrottler = Throttler.make(intervalMillis, Logging.createChild({
64
+ context: "Throttler for chain metadata writes",
65
+ intervalMillis: intervalMillis
66
+ }));
67
+ return {
68
+ config: config,
69
+ persistence: persistence,
70
+ allEntities: persistence.allEntities,
71
+ entities: make$1(persistence.allEntities),
72
+ effects: {},
73
+ rollback: undefined,
74
+ committedCheckpointId: committedCheckpointId,
75
+ processedCheckpointId: committedCheckpointId,
76
+ processedBatches: [],
77
+ processedBatchesCount: 0,
78
+ writeFiber: undefined,
79
+ hasFailedWrite: false,
80
+ commitWaiters: [],
81
+ chainMeta: {},
82
+ chainMetaDirty: false,
83
+ chainMetaThrottler: chainMetaThrottler,
84
+ isProcessing: false,
85
+ crossChainState: CrossChainState.make(chainStates, isInReorgThreshold, isRealtime, targetBufferSize),
86
+ rollbackState: "NoRollback",
87
+ indexerStartTime: new Date(),
88
+ writeThrottlers: make(),
89
+ loadManager: LoadManager.make(),
90
+ keepProcessAlive: isDevelopmentMode || shouldUseTui,
91
+ exitAfterFirstEventBlock: exitAfterFirstEventBlock,
92
+ onError: onError,
93
+ isStopped: false,
94
+ epoch: 0
95
+ };
96
+ }
97
+
98
+ function makeFromDbState(config, persistence, initialState, registrations, isDevelopmentModeOpt, shouldUseTuiOpt, exitAfterFirstEventBlockOpt, reducedPollingInterval, targetBufferSizeOpt, onError) {
99
+ let isDevelopmentMode = isDevelopmentModeOpt !== undefined ? isDevelopmentModeOpt : false;
100
+ let shouldUseTui = shouldUseTuiOpt !== undefined ? shouldUseTuiOpt : false;
101
+ let exitAfterFirstEventBlock = exitAfterFirstEventBlockOpt !== undefined ? exitAfterFirstEventBlockOpt : false;
102
+ let targetBufferSize = targetBufferSizeOpt !== undefined ? targetBufferSizeOpt : CrossChainState.calculateTargetBufferSize();
103
+ let isInReorgThreshold = initialState.cleanRun ? false : initialState.chains.some(chain => {
104
+ let progressBlockNumber = chain.progressBlockNumber;
105
+ let sourceBlockNumber = chain.sourceBlockNumber;
106
+ let maxReorgDepth = chain.maxReorgDepth;
107
+ if (maxReorgDepth > 0 && sourceBlockNumber > 0) {
108
+ return progressBlockNumber > (sourceBlockNumber - maxReorgDepth | 0);
109
+ } else {
110
+ return false;
111
+ }
112
+ });
113
+ Prometheus.ProcessingMaxBatchSize.set(config.batchSize);
114
+ Prometheus.ReorgThreshold.set(isInReorgThreshold);
115
+ Utils.Dict.forEach(initialState.cache, param => {
116
+ let count = param.count;
117
+ Prometheus.EffectCacheCount.set(count, param.effectName);
118
+ });
119
+ let isRealtime = !Env.updateSyncTimeOnRestart && initialState.chains.length !== 0 && initialState.chains.every(c => Stdlib_Option.isSome(c.timestampCaughtUpToHeadOrEndblock));
120
+ let chainStates = {};
121
+ initialState.chains.forEach(resumedChainState => {
122
+ let chain = Config.getChain(config, resumedChainState.id);
123
+ let chainConfig = ChainMap.get(config.chainMap, chain);
124
+ chainStates[resumedChainState.id] = ChainState.makeFromDbState(chainConfig, resumedChainState, initialState.reorgCheckpoints, isInReorgThreshold, isRealtime, config, registrations, reducedPollingInterval);
125
+ });
126
+ let allChainsReady = {
127
+ contents: initialState.chains.length !== 0
128
+ };
129
+ Utils.Dict.forEach(chainStates, cs => {
130
+ let chainId = ChainState.chainConfig(cs).id;
131
+ Prometheus.ProgressBlockNumber.set(ChainState.committedProgressBlockNumber(cs), chainId);
132
+ Prometheus.ProgressReady.init(chainId);
133
+ if (ChainState.isReady(cs)) {
134
+ return Prometheus.ProgressReady.set(chainId);
135
+ } else {
136
+ allChainsReady.contents = false;
137
+ return;
138
+ }
139
+ });
140
+ if (allChainsReady.contents) {
141
+ Prometheus.ProgressReady.setAllReady();
142
+ }
143
+ return make$2(config, persistence, chainStates, isInReorgThreshold, isRealtime, targetBufferSize, initialState.checkpointId, isDevelopmentMode, shouldUseTui, exitAfterFirstEventBlock, onError);
144
+ }
145
+
146
+ function isStale(state, stateId) {
147
+ if (state.isStopped) {
148
+ return true;
149
+ } else {
150
+ return stateId !== state.epoch;
151
+ }
152
+ }
153
+
154
+ function isResolvingReorg(state) {
155
+ let match = state.rollbackState;
156
+ if (typeof match !== "object") {
157
+ return match !== "NoRollback";
158
+ } else {
159
+ return match.TAG !== "RollbackReady";
160
+ }
161
+ }
162
+
163
+ function errorExit(state, errHandler) {
164
+ if (!state.isStopped) {
165
+ state.isStopped = true;
166
+ return state.onError(errHandler);
167
+ }
168
+ }
169
+
170
+ function stop(state) {
171
+ state.isStopped = true;
172
+ }
173
+
174
+ function getChainState(state, chain) {
175
+ let cs = CrossChainState.chainStates(state.crossChainState)[chain];
176
+ if (cs !== undefined) {
177
+ return Primitive_option.valFromOption(cs);
178
+ } else {
179
+ return Stdlib_JsError.throwWithMessage("No chain with id " + ChainMap.Chain.toString(chain) + " found in chain states");
180
+ }
181
+ }
182
+
183
+ function getSafeCheckpointId(state) {
184
+ return CrossChainState.getSafeCheckpointId(state.crossChainState);
185
+ }
186
+
187
+ function createBatch(state, processedCheckpointId, batchSizeTarget, isRollback) {
188
+ return CrossChainState.createBatch(state.crossChainState, processedCheckpointId, batchSizeTarget, isRollback);
189
+ }
190
+
191
+ function enterReorgThreshold(state) {
192
+ CrossChainState.enterReorgThreshold(state.crossChainState);
193
+ }
194
+
195
+ function beginReorg(state, chain, blockNumber) {
196
+ state.epoch = state.epoch + 1 | 0;
197
+ state.rollbackState = {
198
+ TAG: "ReorgDetected",
199
+ chain: chain,
200
+ blockNumber: blockNumber
201
+ };
202
+ }
203
+
204
+ function enterFindingReorgDepth(state) {
205
+ state.rollbackState = "FindingReorgDepth";
206
+ }
207
+
208
+ function foundReorgDepth(state, chain, rollbackTargetBlockNumber) {
209
+ state.rollbackState = {
210
+ TAG: "FoundReorgDepth",
211
+ chain: chain,
212
+ rollbackTargetBlockNumber: rollbackTargetBlockNumber
213
+ };
214
+ }
215
+
216
+ function completeRollback(state, eventsProcessedDiffByChain) {
217
+ state.rollbackState = {
218
+ TAG: "RollbackReady",
219
+ eventsProcessedDiffByChain: eventsProcessedDiffByChain
220
+ };
221
+ }
222
+
223
+ function clearRollback(state) {
224
+ state.rollbackState = "NoRollback";
225
+ }
226
+
227
+ function invalidateInflight(state) {
228
+ state.epoch = state.epoch + 1 | 0;
229
+ }
230
+
231
+ function applyBatchProgress(state, batch) {
232
+ CrossChainState.applyBatchProgress(state.crossChainState, batch);
233
+ }
234
+
235
+ function isProcessing(state) {
236
+ return state.isProcessing;
237
+ }
238
+
239
+ function beginProcessing(state) {
240
+ state.isProcessing = true;
241
+ }
242
+
243
+ function endProcessing(state) {
244
+ state.isProcessing = false;
245
+ }
246
+
247
+ function recordProcessedBatch(state) {
248
+ state.processedBatchesCount = state.processedBatchesCount + 1 | 0;
249
+ }
250
+
251
+ function config(state) {
252
+ return state.config;
253
+ }
254
+
255
+ function persistence(state) {
256
+ return state.persistence;
257
+ }
258
+
259
+ function allEntities(state) {
260
+ return state.allEntities;
261
+ }
262
+
263
+ function entities(state) {
264
+ return state.entities;
265
+ }
266
+
267
+ function effects(state) {
268
+ return state.effects;
269
+ }
270
+
271
+ function committedCheckpointId(state) {
272
+ return state.committedCheckpointId;
273
+ }
274
+
275
+ function processedCheckpointId(state) {
276
+ return state.processedCheckpointId;
277
+ }
278
+
279
+ function processedBatches(state) {
280
+ return state.processedBatches;
281
+ }
282
+
283
+ function processedBatchesCount(state) {
284
+ return state.processedBatchesCount;
285
+ }
286
+
287
+ function writeFiber(state) {
288
+ return state.writeFiber;
289
+ }
290
+
291
+ function hasFailedWrite(state) {
292
+ return state.hasFailedWrite;
293
+ }
294
+
295
+ function chainMetaDirty(state) {
296
+ return state.chainMetaDirty;
297
+ }
298
+
299
+ function chainMetaThrottler(state) {
300
+ return state.chainMetaThrottler;
301
+ }
302
+
303
+ function crossChainState(state) {
304
+ return state.crossChainState;
305
+ }
306
+
307
+ function chainStates(state) {
308
+ return CrossChainState.chainStates(state.crossChainState);
309
+ }
310
+
311
+ function isInReorgThreshold(state) {
312
+ return CrossChainState.isInReorgThreshold(state.crossChainState);
313
+ }
314
+
315
+ function isRealtime(state) {
316
+ return CrossChainState.isRealtime(state.crossChainState);
317
+ }
318
+
319
+ function rollbackState(state) {
320
+ return state.rollbackState;
321
+ }
322
+
323
+ function indexerStartTime(state) {
324
+ return state.indexerStartTime;
325
+ }
326
+
327
+ function loadManager(state) {
328
+ return state.loadManager;
329
+ }
330
+
331
+ function keepProcessAlive(state) {
332
+ return state.keepProcessAlive;
333
+ }
334
+
335
+ function exitAfterFirstEventBlock(state) {
336
+ return state.exitAfterFirstEventBlock;
337
+ }
338
+
339
+ function isStopped(state) {
340
+ return state.isStopped;
341
+ }
342
+
343
+ function epoch(state) {
344
+ return state.epoch;
345
+ }
346
+
347
+ function pruneStaleEntityHistoryThrottler(state) {
348
+ return state.writeThrottlers.pruneStaleEntityHistory;
349
+ }
350
+
351
+ function queueProcessedBatch(state, batch) {
352
+ state.processedBatches.push(batch);
353
+ let checkpointId = Utils.$$Array.last(batch.checkpointIds);
354
+ if (checkpointId !== undefined) {
355
+ state.processedCheckpointId = checkpointId;
356
+ return;
357
+ }
358
+ }
359
+
360
+ function drainBatchRun(state) {
361
+ let all = state.processedBatches;
362
+ let isInReorgThreshold = all[0].isInReorgThreshold;
363
+ let rest = [];
364
+ let progressedChainsById = {};
365
+ let totalBatchSize = {
366
+ contents: 0
367
+ };
368
+ let items = [];
369
+ let checkpointIds = [];
370
+ let checkpointChainIds = [];
371
+ let checkpointBlockNumbers = [];
372
+ let checkpointBlockHashes = [];
373
+ let checkpointEventsProcessed = [];
374
+ all.forEach(batch => {
375
+ if (Utils.$$Array.isEmpty(rest) && batch.isInReorgThreshold === isInReorgThreshold) {
376
+ Utils.Dict.forEachWithKey(batch.progressedChainsById, (chainAfterBatch, key) => {
377
+ progressedChainsById[key] = chainAfterBatch;
378
+ });
379
+ totalBatchSize.contents = totalBatchSize.contents + batch.totalBatchSize | 0;
380
+ items.push(...batch.items);
381
+ checkpointIds.push(...batch.checkpointIds);
382
+ checkpointChainIds.push(...batch.checkpointChainIds);
383
+ checkpointBlockNumbers.push(...batch.checkpointBlockNumbers);
384
+ checkpointBlockHashes.push(...batch.checkpointBlockHashes);
385
+ checkpointEventsProcessed.push(...batch.checkpointEventsProcessed);
386
+ } else {
387
+ rest.push(batch);
388
+ }
389
+ });
390
+ state.processedBatches = rest;
391
+ return {
392
+ totalBatchSize: totalBatchSize.contents,
393
+ items: items,
394
+ progressedChainsById: progressedChainsById,
395
+ isInReorgThreshold: isInReorgThreshold,
396
+ checkpointIds: checkpointIds,
397
+ checkpointChainIds: checkpointChainIds,
398
+ checkpointBlockNumbers: checkpointBlockNumbers,
399
+ checkpointBlockHashes: checkpointBlockHashes,
400
+ checkpointEventsProcessed: checkpointEventsProcessed
401
+ };
402
+ }
403
+
404
+ function takeRollback(state) {
405
+ let rollback = state.rollback;
406
+ state.rollback = undefined;
407
+ return rollback;
408
+ }
409
+
410
+ function markCommitted(state, upToCheckpointId) {
411
+ state.committedCheckpointId = upToCheckpointId;
412
+ }
413
+
414
+ function beginRollbackDiff(state, targetCheckpointId, diffCheckpointId, progressBlockNumberByChainId) {
415
+ state.entities = make$1(state.allEntities);
416
+ state.effects = {};
417
+ state.rollback = {
418
+ targetCheckpointId: targetCheckpointId,
419
+ diffCheckpointId: diffCheckpointId,
420
+ progressBlockNumberByChainId: progressBlockNumberByChainId
421
+ };
422
+ }
423
+
424
+ function recordWriteFailure(state, exn) {
425
+ state.hasFailedWrite = true;
426
+ state.onError(ErrorHandling.make(exn, undefined, "Failed writing batch to the database"));
427
+ }
428
+
429
+ function beginWriteFiber(state, fiber) {
430
+ state.writeFiber = fiber;
431
+ }
432
+
433
+ function endWriteFiber(state) {
434
+ state.writeFiber = undefined;
435
+ }
436
+
437
+ function wakeCommitWaiters(state) {
438
+ let waiters = state.commitWaiters;
439
+ state.commitWaiters = [];
440
+ waiters.forEach(resolve => resolve());
441
+ }
442
+
443
+ function addCommitWaiter(state, resolve) {
444
+ state.commitWaiters.push(resolve);
445
+ }
446
+
447
+ function metaFieldsEqual(a, b) {
448
+ if (Primitive_object.equal(a.first_event_block, b.first_event_block) && a.buffer_block === b.buffer_block && a._is_hyper_sync === b._is_hyper_sync) {
449
+ return Primitive_object.equal(Stdlib_Option.map(Primitive_option.fromNull(a.ready_at), prim => prim.getTime()), Stdlib_Option.map(Primitive_option.fromNull(b.ready_at), prim => prim.getTime()));
450
+ } else {
451
+ return false;
452
+ }
453
+ }
454
+
455
+ function stageChainMeta(state, chainsData) {
456
+ Utils.Dict.forEachWithKey(chainsData, (meta, chainId) => {
457
+ let prev = state.chainMeta[chainId];
458
+ let changed = prev !== undefined ? !metaFieldsEqual(meta, prev) : true;
459
+ if (changed) {
460
+ state.chainMeta[chainId] = meta;
461
+ state.chainMetaDirty = true;
462
+ return;
463
+ }
464
+ });
465
+ }
466
+
467
+ function takeChainMetaSnapshot(state) {
468
+ if (state.chainMetaDirty) {
469
+ state.chainMetaDirty = false;
470
+ return Utils.Dict.shallowCopy(state.chainMeta);
471
+ }
472
+ }
473
+
474
+ let EntityTables = {
475
+ make: make$1,
476
+ get: get
477
+ };
478
+
479
+ let unexpectedErrorMsg = "Indexer has failed with an unexpected error";
480
+
481
+ export {
482
+ EntityTables,
483
+ make$2 as make,
484
+ makeFromDbState,
485
+ unexpectedErrorMsg,
486
+ isStale,
487
+ isResolvingReorg,
488
+ errorExit,
489
+ stop,
490
+ getChainState,
491
+ enterReorgThreshold,
492
+ beginReorg,
493
+ enterFindingReorgDepth,
494
+ foundReorgDepth,
495
+ completeRollback,
496
+ clearRollback,
497
+ invalidateInflight,
498
+ applyBatchProgress,
499
+ isProcessing,
500
+ beginProcessing,
501
+ endProcessing,
502
+ recordProcessedBatch,
503
+ createBatch,
504
+ getSafeCheckpointId,
505
+ config,
506
+ persistence,
507
+ allEntities,
508
+ entities,
509
+ effects,
510
+ committedCheckpointId,
511
+ processedCheckpointId,
512
+ processedBatches,
513
+ processedBatchesCount,
514
+ writeFiber,
515
+ hasFailedWrite,
516
+ chainMetaDirty,
517
+ chainMetaThrottler,
518
+ crossChainState,
519
+ chainStates,
520
+ isInReorgThreshold,
521
+ isRealtime,
522
+ rollbackState,
523
+ indexerStartTime,
524
+ loadManager,
525
+ keepProcessAlive,
526
+ exitAfterFirstEventBlock,
527
+ isStopped,
528
+ epoch,
529
+ pruneStaleEntityHistoryThrottler,
530
+ queueProcessedBatch,
531
+ drainBatchRun,
532
+ takeRollback,
533
+ markCommitted,
534
+ beginRollbackDiff,
535
+ recordWriteFailure,
536
+ beginWriteFiber,
537
+ endWriteFiber,
538
+ wakeCommitWaiters,
539
+ addCommitWaiter,
540
+ stageChainMeta,
541
+ takeChainMetaSnapshot,
542
+ }
543
+ /* Env Not a pure module */
@@ -0,0 +1,128 @@
1
+ // The indexer state. `t` is opaque: other modules read it through the accessors
2
+ // and change it only through the transitions and setters exposed here.
3
+
4
+ type chain = ChainMap.Chain.t
5
+
6
+ type rollbackState =
7
+ | NoRollback
8
+ | ReorgDetected({chain: chain, blockNumber: int})
9
+ | FindingReorgDepth
10
+ | FoundReorgDepth({chain: chain, rollbackTargetBlockNumber: int})
11
+ | RollbackReady({eventsProcessedDiffByChain: dict<float>})
12
+
13
+ module EntityTables: {
14
+ type t = dict<InMemoryTable.Entity.t>
15
+ let make: array<Internal.entityConfig> => t
16
+ let get: (t, ~entityName: string) => InMemoryTable.Entity.t
17
+ }
18
+
19
+ type effectCacheInMemTable = {
20
+ mutable idsToStore: array<string>,
21
+ mutable invalidationsCount: int,
22
+ mutable dict: dict<Change.t<Internal.effectOutput>>,
23
+ mutable changesCount: float,
24
+ effect: Internal.effect,
25
+ }
26
+
27
+ type t
28
+
29
+ let make: (
30
+ ~config: Config.t,
31
+ ~persistence: Persistence.t,
32
+ ~chainStates: dict<ChainState.t>,
33
+ ~isInReorgThreshold: bool,
34
+ ~isRealtime: bool,
35
+ ~targetBufferSize: int=?,
36
+ ~committedCheckpointId: Internal.checkpointId=?,
37
+ ~isDevelopmentMode: bool=?,
38
+ ~shouldUseTui: bool=?,
39
+ ~exitAfterFirstEventBlock: bool=?,
40
+ ~onError: ErrorHandling.t => unit,
41
+ ) => t
42
+
43
+ let makeFromDbState: (
44
+ ~config: Config.t,
45
+ ~persistence: Persistence.t,
46
+ ~initialState: Persistence.initialState,
47
+ ~registrations: HandlerRegister.registrations,
48
+ ~isDevelopmentMode: bool=?,
49
+ ~shouldUseTui: bool=?,
50
+ ~exitAfterFirstEventBlock: bool=?,
51
+ ~reducedPollingInterval: int=?,
52
+ ~targetBufferSize: int=?,
53
+ ~onError: ErrorHandling.t => unit,
54
+ ) => t
55
+
56
+ let unexpectedErrorMsg: string
57
+
58
+ // Read guards / transitions.
59
+ let isStale: (t, ~stateId: int) => bool
60
+ let isResolvingReorg: t => bool
61
+ let errorExit: (t, ErrorHandling.t) => unit
62
+ let stop: t => unit
63
+ let getChainState: (t, ~chain: chain) => ChainState.t
64
+ let enterReorgThreshold: t => unit
65
+ let beginReorg: (t, ~chain: chain, ~blockNumber: int) => unit
66
+ let enterFindingReorgDepth: t => unit
67
+ let foundReorgDepth: (t, ~chain: chain, ~rollbackTargetBlockNumber: int) => unit
68
+ let completeRollback: (t, ~eventsProcessedDiffByChain: dict<float>) => unit
69
+ let clearRollback: t => unit
70
+ let invalidateInflight: t => unit
71
+ let applyBatchProgress: (t, ~batch: Batch.t) => unit
72
+ let isProcessing: t => bool
73
+ let beginProcessing: t => unit
74
+ let endProcessing: t => unit
75
+ let recordProcessedBatch: t => unit
76
+ let createBatch: (
77
+ t,
78
+ ~processedCheckpointId: Internal.checkpointId,
79
+ ~batchSizeTarget: int,
80
+ ~isRollback: bool,
81
+ ) => Batch.t
82
+ let getSafeCheckpointId: t => option<Internal.checkpointId>
83
+
84
+ // Accessors.
85
+ let config: t => Config.t
86
+ let persistence: t => Persistence.t
87
+ let allEntities: t => array<Internal.entityConfig>
88
+ let entities: t => EntityTables.t
89
+ let effects: t => dict<effectCacheInMemTable>
90
+ let committedCheckpointId: t => Internal.checkpointId
91
+ let processedCheckpointId: t => Internal.checkpointId
92
+ let processedBatches: t => array<Batch.t>
93
+ let processedBatchesCount: t => int
94
+ let writeFiber: t => option<promise<unit>>
95
+ let hasFailedWrite: t => bool
96
+ let chainMetaDirty: t => bool
97
+ let chainMetaThrottler: t => Throttler.t
98
+ let crossChainState: t => CrossChainState.t
99
+ let chainStates: t => dict<ChainState.t>
100
+ let isInReorgThreshold: t => bool
101
+ let isRealtime: t => bool
102
+ let rollbackState: t => rollbackState
103
+ let indexerStartTime: t => Date.t
104
+ let loadManager: t => LoadManager.t
105
+ let keepProcessAlive: t => bool
106
+ let exitAfterFirstEventBlock: t => bool
107
+ let isStopped: t => bool
108
+ let epoch: t => int
109
+ let pruneStaleEntityHistoryThrottler: t => Throttler.t
110
+
111
+ // Store domain operations.
112
+ let queueProcessedBatch: (t, ~batch: Batch.t) => unit
113
+ let drainBatchRun: t => Batch.t
114
+ let takeRollback: t => option<Persistence.rollback>
115
+ let markCommitted: (t, ~upToCheckpointId: Internal.checkpointId) => unit
116
+ let beginRollbackDiff: (
117
+ t,
118
+ ~targetCheckpointId: Internal.checkpointId,
119
+ ~diffCheckpointId: Internal.checkpointId,
120
+ ~progressBlockNumberByChainId: dict<int>,
121
+ ) => unit
122
+ let recordWriteFailure: (t, exn) => unit
123
+ let beginWriteFiber: (t, promise<unit>) => unit
124
+ let endWriteFiber: t => unit
125
+ let wakeCommitWaiters: t => unit
126
+ let addCommitWaiter: (t, unit => unit) => unit
127
+ let stageChainMeta: (t, dict<InternalTable.Chains.metaFields>) => unit
128
+ let takeChainMetaSnapshot: t => option<dict<InternalTable.Chains.metaFields>>