envio 3.0.2 → 3.1.0-rc.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.
Files changed (91) hide show
  1. package/README.md +0 -1
  2. package/evm.schema.json +15 -8
  3. package/fuel.schema.json +19 -12
  4. package/index.d.ts +0 -2
  5. package/package.json +6 -7
  6. package/rescript.json +1 -1
  7. package/src/Batch.res +4 -214
  8. package/src/Batch.res.mjs +6 -165
  9. package/src/ChainFetcher.res +4 -5
  10. package/src/ChainFetcher.res.mjs +6 -7
  11. package/src/ChainManager.res +10 -9
  12. package/src/ChainManager.res.mjs +6 -10
  13. package/src/Config.res +9 -25
  14. package/src/Config.res.mjs +17 -27
  15. package/src/Core.res +7 -0
  16. package/src/Ctx.res +1 -0
  17. package/src/Env.res +0 -1
  18. package/src/Env.res.mjs +0 -3
  19. package/src/EventConfigBuilder.res +13 -123
  20. package/src/EventConfigBuilder.res.mjs +6 -73
  21. package/src/EventProcessing.res +5 -29
  22. package/src/EventProcessing.res.mjs +11 -20
  23. package/src/EventUtils.res +0 -27
  24. package/src/EventUtils.res.mjs +0 -24
  25. package/src/FetchState.res +1 -11
  26. package/src/FetchState.res.mjs +2 -16
  27. package/src/GlobalState.res +23 -37
  28. package/src/GlobalState.res.mjs +10 -38
  29. package/src/HandlerLoader.res +6 -5
  30. package/src/HandlerLoader.res.mjs +27 -9
  31. package/src/HandlerRegister.res +1 -12
  32. package/src/HandlerRegister.res.mjs +1 -6
  33. package/src/HandlerRegister.resi +1 -1
  34. package/src/Hasura.res +96 -32
  35. package/src/Hasura.res.mjs +93 -38
  36. package/src/InMemoryStore.res +181 -45
  37. package/src/InMemoryStore.res.mjs +143 -40
  38. package/src/InMemoryTable.res +147 -247
  39. package/src/InMemoryTable.res.mjs +131 -230
  40. package/src/Internal.res +10 -34
  41. package/src/Internal.res.mjs +9 -3
  42. package/src/LoadLayer.res +5 -5
  43. package/src/LoadLayer.res.mjs +5 -5
  44. package/src/Main.res +4 -6
  45. package/src/Main.res.mjs +26 -15
  46. package/src/Persistence.res +7 -132
  47. package/src/Persistence.res.mjs +1 -102
  48. package/src/PgStorage.res +57 -40
  49. package/src/PgStorage.res.mjs +60 -34
  50. package/src/ReorgDetection.res +35 -58
  51. package/src/ReorgDetection.res.mjs +21 -29
  52. package/src/SimulateItems.res.mjs +21 -3
  53. package/src/Sink.res +2 -2
  54. package/src/Sink.res.mjs +1 -1
  55. package/src/TableIndices.res +9 -2
  56. package/src/TableIndices.res.mjs +7 -1
  57. package/src/TestIndexer.res +53 -60
  58. package/src/TestIndexer.res.mjs +77 -63
  59. package/src/TestIndexerProxyStorage.res +4 -14
  60. package/src/TestIndexerProxyStorage.res.mjs +1 -5
  61. package/src/UserContext.res +2 -4
  62. package/src/UserContext.res.mjs +4 -5
  63. package/src/Utils.res +0 -2
  64. package/src/Utils.res.mjs +0 -3
  65. package/src/bindings/ClickHouse.res +45 -38
  66. package/src/bindings/ClickHouse.res.mjs +16 -17
  67. package/src/bindings/Vitest.res +3 -0
  68. package/src/db/InternalTable.res +59 -18
  69. package/src/db/InternalTable.res.mjs +82 -51
  70. package/src/db/Table.res +9 -2
  71. package/src/db/Table.res.mjs +10 -7
  72. package/src/sources/EvmChain.res +32 -9
  73. package/src/sources/EvmChain.res.mjs +31 -4
  74. package/src/sources/HyperFuelSource.res +14 -57
  75. package/src/sources/HyperFuelSource.res.mjs +18 -38
  76. package/src/sources/HyperSync.res +36 -101
  77. package/src/sources/HyperSync.res.mjs +42 -96
  78. package/src/sources/HyperSync.resi +4 -22
  79. package/src/sources/HyperSyncClient.res +67 -245
  80. package/src/sources/HyperSyncClient.res.mjs +47 -46
  81. package/src/sources/HyperSyncSource.res +76 -147
  82. package/src/sources/HyperSyncSource.res.mjs +61 -114
  83. package/src/sources/RpcSource.res +43 -22
  84. package/src/sources/RpcSource.res.mjs +50 -35
  85. package/src/sources/SimulateSource.res +1 -7
  86. package/src/sources/SimulateSource.res.mjs +1 -7
  87. package/src/sources/Source.res +8 -1
  88. package/src/sources/SourceManager.res +9 -0
  89. package/src/sources/SourceManager.res.mjs +10 -0
  90. package/src/sources/SourceManager.resi +2 -0
  91. package/svm.schema.json +11 -4
@@ -2,22 +2,20 @@
2
2
 
3
3
  import * as Utils from "./Utils.res.mjs";
4
4
  import * as Config from "./Config.res.mjs";
5
- import * as Belt_Array from "@rescript/runtime/lib/es6/Belt_Array.js";
6
- import * as EventUtils from "./EventUtils.res.mjs";
7
- import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
5
+ import * as Internal from "./Internal.res.mjs";
6
+ import * as Prometheus from "./Prometheus.res.mjs";
7
+ import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
8
8
  import * as ErrorHandling from "./ErrorHandling.res.mjs";
9
9
  import * as InMemoryTable from "./InMemoryTable.res.mjs";
10
+ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
11
+ import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
10
12
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
11
13
 
12
- function hashRawEventsKey(key) {
13
- return EventUtils.getEventIdKeyString(key.chainId, key.eventId);
14
- }
15
-
16
14
  let UndefinedEntity = /* @__PURE__ */Primitive_exceptions.create("InMemoryStore.EntityTables.UndefinedEntity");
17
15
 
18
16
  function make(entities) {
19
17
  let init = {};
20
- Belt_Array.forEach(entities, entityConfig => {
18
+ entities.forEach(entityConfig => {
21
19
  init[entityConfig.name] = InMemoryTable.Entity.make();
22
20
  });
23
21
  return init;
@@ -35,40 +33,21 @@ function get(self, entityName) {
35
33
  }
36
34
  }
37
35
 
38
- function clone(self) {
39
- return Object.fromEntries(Belt_Array.map(Object.entries(self), param => [
40
- param[0],
41
- InMemoryTable.Entity.clone(param[1])
42
- ]));
43
- }
44
-
45
36
  let EntityTables = {
46
37
  UndefinedEntity: UndefinedEntity,
47
38
  make: make,
48
- get: get,
49
- clone: clone
39
+ get: get
50
40
  };
51
41
 
52
- function make$1(entities, rollbackTargetCheckpointId) {
42
+ function make$1(entities, committedCheckpointIdOpt) {
43
+ let committedCheckpointId = committedCheckpointIdOpt !== undefined ? committedCheckpointIdOpt : Internal.initialCheckpointId;
53
44
  return {
54
- rawEvents: InMemoryTable.make(hashRawEventsKey),
45
+ allEntities: entities,
46
+ rawEvents: [],
55
47
  entities: make(entities),
56
48
  effects: {},
57
- rollbackTargetCheckpointId: rollbackTargetCheckpointId
58
- };
59
- }
60
-
61
- function clone$1(self) {
62
- return {
63
- rawEvents: InMemoryTable.clone(self.rawEvents),
64
- entities: clone(self.entities),
65
- effects: Stdlib_Dict.mapValues(self.effects, table => ({
66
- idsToStore: table.idsToStore.slice(),
67
- invalidationsCount: table.invalidationsCount,
68
- dict: Utils.Dict.shallowCopy(table.dict),
69
- effect: table.effect
70
- })),
71
- rollbackTargetCheckpointId: self.rollbackTargetCheckpointId
49
+ rollback: undefined,
50
+ committedCheckpointId: committedCheckpointId
72
51
  };
73
52
  }
74
53
 
@@ -93,10 +72,131 @@ function getInMemTable(inMemoryStore, entityConfig) {
93
72
  }
94
73
 
95
74
  function isRollingBack(inMemoryStore) {
96
- return inMemoryStore.rollbackTargetCheckpointId !== undefined;
75
+ return inMemoryStore.rollback !== undefined;
76
+ }
77
+
78
+ async function writeBatch(inMemoryStore, persistence, batch, config, isInReorgThreshold) {
79
+ let match = persistence.storageStatus;
80
+ if (typeof match !== "object") {
81
+ return Stdlib_JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`);
82
+ }
83
+ if (match.TAG === "Initializing") {
84
+ return Stdlib_JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`);
85
+ }
86
+ let cache = match._0.cache;
87
+ let committedCheckpointId = inMemoryStore.committedCheckpointId;
88
+ let totalChanges = {
89
+ contents: 0
90
+ };
91
+ persistence.allEntities.forEach(entityConfig => {
92
+ totalChanges.contents = totalChanges.contents + getInMemTable(inMemoryStore, entityConfig).changesCount;
93
+ });
94
+ let keepLatestChanges = totalChanges.contents < 50000;
95
+ let updatedEntities = Stdlib_Array.filterMap(persistence.allEntities, entityConfig => {
96
+ let table = getInMemTable(inMemoryStore, entityConfig);
97
+ table.changesCount = table.changesCount - table.prevEntityChanges.length;
98
+ let changes = table.prevEntityChanges;
99
+ Utils.Dict.forEach(table.latestEntityChangeById, change => {
100
+ if (change.checkpointId > committedCheckpointId) {
101
+ changes.push(change);
102
+ return;
103
+ }
104
+ });
105
+ if (Utils.$$Array.isEmpty(changes)) {
106
+ return;
107
+ } else {
108
+ return {
109
+ entityConfig: entityConfig,
110
+ changes: changes
111
+ };
112
+ }
113
+ });
114
+ let acc = [];
115
+ await persistence.storage.writeBatch(batch, inMemoryStore.rawEvents, inMemoryStore.rollback, isInReorgThreshold, config, persistence.allEntities, (Utils.Dict.forEach(inMemoryStore.effects, inMemTable => {
116
+ let idsToStore = inMemTable.idsToStore;
117
+ let invalidationsCount = inMemTable.invalidationsCount;
118
+ let effect = inMemTable.effect;
119
+ let dict = inMemTable.dict;
120
+ if (idsToStore.length === 0) {
121
+ return;
122
+ }
123
+ let items = idsToStore.map(id => ({
124
+ id: id,
125
+ output: dict[id]
126
+ }));
127
+ let effectName = effect.name;
128
+ let c = cache[effectName];
129
+ let effectCacheRecord;
130
+ if (c !== undefined) {
131
+ effectCacheRecord = c;
132
+ } else {
133
+ let c$1 = {
134
+ effectName: effectName,
135
+ count: 0
136
+ };
137
+ cache[effectName] = c$1;
138
+ effectCacheRecord = c$1;
139
+ }
140
+ let shouldInitialize = effectCacheRecord.count === 0;
141
+ effectCacheRecord.count = (effectCacheRecord.count + items.length | 0) - invalidationsCount | 0;
142
+ Prometheus.EffectCacheCount.set(effectCacheRecord.count, effectName);
143
+ acc.push({
144
+ effect: effect,
145
+ items: items,
146
+ shouldInitialize: shouldInitialize
147
+ });
148
+ }), acc), updatedEntities);
149
+ inMemoryStore.rawEvents = [];
150
+ inMemoryStore.effects = {};
151
+ inMemoryStore.rollback = undefined;
152
+ let checkpointId = Utils.$$Array.last(batch.checkpointIds);
153
+ inMemoryStore.committedCheckpointId = checkpointId !== undefined ? checkpointId : committedCheckpointId;
154
+ persistence.allEntities.forEach(entityConfig => {
155
+ let table = getInMemTable(inMemoryStore, entityConfig);
156
+ let resetTable = keepLatestChanges ? InMemoryTable.Entity.resetButKeepLatestChanges(table) : InMemoryTable.Entity.make();
157
+ inMemoryStore.entities[entityConfig.name] = resetTable;
158
+ });
97
159
  }
98
160
 
99
- function setBatchDcs(inMemoryStore, batch, shouldSaveHistory) {
161
+ async function prepareRollbackDiff(inMemoryStore, persistence, rollbackTargetCheckpointId, rollbackDiffCheckpointId) {
162
+ inMemoryStore.rawEvents = [];
163
+ inMemoryStore.entities = make(inMemoryStore.allEntities);
164
+ inMemoryStore.effects = {};
165
+ inMemoryStore.rollback = {
166
+ targetCheckpointId: rollbackTargetCheckpointId,
167
+ diffCheckpointId: rollbackDiffCheckpointId
168
+ };
169
+ let deletedEntities = {};
170
+ let setEntities = {};
171
+ await Promise.all(persistence.allEntities.map(async entityConfig => {
172
+ let entityTable = getInMemTable(inMemoryStore, entityConfig);
173
+ let match = await persistence.storage.getRollbackData(entityConfig, rollbackTargetCheckpointId);
174
+ match[0].forEach(data => {
175
+ Utils.Dict.push(deletedEntities, entityConfig.name, data.id);
176
+ InMemoryTable.Entity.set(entityTable, inMemoryStore.committedCheckpointId, {
177
+ type: "DELETE",
178
+ entityId: data.id,
179
+ checkpointId: rollbackDiffCheckpointId
180
+ });
181
+ });
182
+ let restoredEntities = S$RescriptSchema.parseOrThrow(match[1], entityConfig.rowsSchema);
183
+ restoredEntities.forEach(entity => {
184
+ Utils.Dict.push(setEntities, entityConfig.name, entity.id);
185
+ InMemoryTable.Entity.set(entityTable, inMemoryStore.committedCheckpointId, {
186
+ type: "SET",
187
+ entityId: entity.id,
188
+ entity: entity,
189
+ checkpointId: rollbackDiffCheckpointId
190
+ });
191
+ });
192
+ }));
193
+ return {
194
+ deletedEntities: deletedEntities,
195
+ setEntities: setEntities
196
+ };
197
+ }
198
+
199
+ function setBatchDcs(inMemoryStore, batch) {
100
200
  let inMemTable = getInMemTable(inMemoryStore, Config.EnvioAddresses.entityConfig);
101
201
  let itemIdx = 0;
102
202
  for (let checkpoint = 0, checkpoint_finish = batch.checkpointIds.length; checkpoint < checkpoint_finish; ++checkpoint) {
@@ -120,12 +220,12 @@ function setBatchDcs(inMemoryStore, batch, shouldSaveHistory) {
120
220
  registration_log_index: entity_registration_log_index,
121
221
  contract_name: entity_contract_name
122
222
  };
123
- InMemoryTable.Entity.set(inMemTable, {
223
+ InMemoryTable.Entity.set(inMemTable, inMemoryStore.committedCheckpointId, {
124
224
  type: "SET",
125
225
  entityId: entity_id,
126
226
  entity: entity,
127
227
  checkpointId: checkpointId
128
- }, shouldSaveHistory, undefined);
228
+ });
129
229
  }
130
230
  }
131
231
  }
@@ -133,14 +233,17 @@ function setBatchDcs(inMemoryStore, batch, shouldSaveHistory) {
133
233
  }
134
234
  }
135
235
 
236
+ let keepLatestChangesLimit = 50000;
237
+
136
238
  export {
137
- hashRawEventsKey,
138
239
  EntityTables,
139
240
  make$1 as make,
140
- clone$1 as clone,
241
+ keepLatestChangesLimit,
141
242
  getEffectInMemTable,
142
243
  getInMemTable,
143
244
  isRollingBack,
245
+ writeBatch,
246
+ prepareRollbackDiff,
144
247
  setBatchDcs,
145
248
  }
146
249
  /* Utils Not a pure module */