envio 3.0.2 → 3.1.0-rc.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 (101) 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 +12 -28
  10. package/src/ChainFetcher.res.mjs +8 -17
  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 -8
  18. package/src/Env.res.mjs +0 -6
  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 +2 -15
  26. package/src/FetchState.res.mjs +3 -18
  27. package/src/GlobalState.res +26 -39
  28. package/src/GlobalState.res.mjs +12 -40
  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 +205 -45
  37. package/src/InMemoryStore.res.mjs +157 -40
  38. package/src/InMemoryTable.res +165 -249
  39. package/src/InMemoryTable.res.mjs +156 -227
  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/LogSelection.res +15 -19
  45. package/src/LogSelection.res.mjs +5 -6
  46. package/src/Main.res +4 -6
  47. package/src/Main.res.mjs +26 -15
  48. package/src/Persistence.res +7 -132
  49. package/src/Persistence.res.mjs +1 -102
  50. package/src/PgStorage.res +57 -40
  51. package/src/PgStorage.res.mjs +60 -34
  52. package/src/ReorgDetection.res +35 -58
  53. package/src/ReorgDetection.res.mjs +21 -29
  54. package/src/SimulateItems.res.mjs +21 -3
  55. package/src/Sink.res +2 -2
  56. package/src/Sink.res.mjs +1 -1
  57. package/src/TableIndices.res +9 -2
  58. package/src/TableIndices.res.mjs +7 -1
  59. package/src/TestIndexer.res +53 -60
  60. package/src/TestIndexer.res.mjs +77 -63
  61. package/src/TestIndexerProxyStorage.res +4 -14
  62. package/src/TestIndexerProxyStorage.res.mjs +1 -5
  63. package/src/UserContext.res +2 -4
  64. package/src/UserContext.res.mjs +4 -5
  65. package/src/Utils.res +0 -2
  66. package/src/Utils.res.mjs +0 -3
  67. package/src/bindings/ClickHouse.res +45 -38
  68. package/src/bindings/ClickHouse.res.mjs +16 -17
  69. package/src/bindings/Vitest.res +3 -0
  70. package/src/db/InternalTable.res +59 -18
  71. package/src/db/InternalTable.res.mjs +82 -51
  72. package/src/db/Table.res +9 -2
  73. package/src/db/Table.res.mjs +10 -7
  74. package/src/sources/EnvioApiClient.res +15 -0
  75. package/src/sources/EnvioApiClient.res.mjs +24 -0
  76. package/src/sources/EvmChain.res +32 -10
  77. package/src/sources/EvmChain.res.mjs +31 -5
  78. package/src/sources/HyperFuelSource.res +15 -58
  79. package/src/sources/HyperFuelSource.res.mjs +20 -39
  80. package/src/sources/HyperSync.res +54 -100
  81. package/src/sources/HyperSync.res.mjs +67 -96
  82. package/src/sources/HyperSync.resi +4 -22
  83. package/src/sources/HyperSyncClient.res +70 -247
  84. package/src/sources/HyperSyncClient.res.mjs +47 -46
  85. package/src/sources/HyperSyncSource.res +94 -166
  86. package/src/sources/HyperSyncSource.res.mjs +100 -127
  87. package/src/sources/RpcSource.res +43 -22
  88. package/src/sources/RpcSource.res.mjs +50 -35
  89. package/src/sources/SimulateSource.res +1 -7
  90. package/src/sources/SimulateSource.res.mjs +1 -7
  91. package/src/sources/Source.res +10 -1
  92. package/src/sources/Source.res.mjs +3 -0
  93. package/src/sources/SourceManager.res +177 -8
  94. package/src/sources/SourceManager.res.mjs +141 -3
  95. package/src/sources/SourceManager.resi +19 -0
  96. package/src/tui/Tui.res +44 -6
  97. package/src/tui/Tui.res.mjs +56 -8
  98. package/src/tui/components/TuiData.res +3 -0
  99. package/svm.schema.json +11 -4
  100. package/src/sources/HyperSyncJsonApi.res +0 -390
  101. package/src/sources/HyperSyncJsonApi.res.mjs +0 -237
@@ -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,145 @@ 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
+ if (keepLatestChanges) {
155
+ persistence.allEntities.forEach(entityConfig => {
156
+ let table = getInMemTable(inMemoryStore, entityConfig);
157
+ inMemoryStore.entities[entityConfig.name] = InMemoryTable.Entity.resetButKeepLatestChanges(table);
158
+ });
159
+ return;
160
+ }
161
+ let loadedFromDbCount = {
162
+ contents: 0
163
+ };
164
+ let resetTables = persistence.allEntities.map(entityConfig => {
165
+ let resetTable = InMemoryTable.Entity.resetButKeepLoadedFromDbChanges(getInMemTable(inMemoryStore, entityConfig));
166
+ loadedFromDbCount.contents = loadedFromDbCount.contents + resetTable.changesCount;
167
+ return resetTable;
168
+ });
169
+ let dropEverything = loadedFromDbCount.contents >= 50000;
170
+ persistence.allEntities.forEach((entityConfig, idx) => {
171
+ inMemoryStore.entities[entityConfig.name] = dropEverything ? InMemoryTable.Entity.make() : resetTables[idx];
172
+ });
97
173
  }
98
174
 
99
- function setBatchDcs(inMemoryStore, batch, shouldSaveHistory) {
175
+ async function prepareRollbackDiff(inMemoryStore, persistence, rollbackTargetCheckpointId, rollbackDiffCheckpointId) {
176
+ inMemoryStore.rawEvents = [];
177
+ inMemoryStore.entities = make(inMemoryStore.allEntities);
178
+ inMemoryStore.effects = {};
179
+ inMemoryStore.rollback = {
180
+ targetCheckpointId: rollbackTargetCheckpointId,
181
+ diffCheckpointId: rollbackDiffCheckpointId
182
+ };
183
+ let deletedEntities = {};
184
+ let setEntities = {};
185
+ await Promise.all(persistence.allEntities.map(async entityConfig => {
186
+ let entityTable = getInMemTable(inMemoryStore, entityConfig);
187
+ let match = await persistence.storage.getRollbackData(entityConfig, rollbackTargetCheckpointId);
188
+ match[0].forEach(data => {
189
+ Utils.Dict.push(deletedEntities, entityConfig.name, data.id);
190
+ InMemoryTable.Entity.set(entityTable, inMemoryStore.committedCheckpointId, {
191
+ type: "DELETE",
192
+ entityId: data.id,
193
+ checkpointId: rollbackDiffCheckpointId
194
+ });
195
+ });
196
+ let restoredEntities = S$RescriptSchema.parseOrThrow(match[1], entityConfig.rowsSchema);
197
+ restoredEntities.forEach(entity => {
198
+ Utils.Dict.push(setEntities, entityConfig.name, entity.id);
199
+ InMemoryTable.Entity.set(entityTable, inMemoryStore.committedCheckpointId, {
200
+ type: "SET",
201
+ entityId: entity.id,
202
+ entity: entity,
203
+ checkpointId: rollbackDiffCheckpointId
204
+ });
205
+ });
206
+ }));
207
+ return {
208
+ deletedEntities: deletedEntities,
209
+ setEntities: setEntities
210
+ };
211
+ }
212
+
213
+ function setBatchDcs(inMemoryStore, batch) {
100
214
  let inMemTable = getInMemTable(inMemoryStore, Config.EnvioAddresses.entityConfig);
101
215
  let itemIdx = 0;
102
216
  for (let checkpoint = 0, checkpoint_finish = batch.checkpointIds.length; checkpoint < checkpoint_finish; ++checkpoint) {
@@ -120,12 +234,12 @@ function setBatchDcs(inMemoryStore, batch, shouldSaveHistory) {
120
234
  registration_log_index: entity_registration_log_index,
121
235
  contract_name: entity_contract_name
122
236
  };
123
- InMemoryTable.Entity.set(inMemTable, {
237
+ InMemoryTable.Entity.set(inMemTable, inMemoryStore.committedCheckpointId, {
124
238
  type: "SET",
125
239
  entityId: entity_id,
126
240
  entity: entity,
127
241
  checkpointId: checkpointId
128
- }, shouldSaveHistory, undefined);
242
+ });
129
243
  }
130
244
  }
131
245
  }
@@ -133,14 +247,17 @@ function setBatchDcs(inMemoryStore, batch, shouldSaveHistory) {
133
247
  }
134
248
  }
135
249
 
250
+ let keepLatestChangesLimit = 50000;
251
+
136
252
  export {
137
- hashRawEventsKey,
138
253
  EntityTables,
139
254
  make$1 as make,
140
- clone$1 as clone,
255
+ keepLatestChangesLimit,
141
256
  getEffectInMemTable,
142
257
  getInMemTable,
143
258
  isRollingBack,
259
+ writeBatch,
260
+ prepareRollbackDiff,
144
261
  setBatchDcs,
145
262
  }
146
263
  /* Utils Not a pure module */