envio 3.0.0-alpha.21 → 3.0.0-alpha.23
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/README.md +3 -3
- package/bin.mjs +2 -48
- package/evm.schema.json +67 -0
- package/fuel.schema.json +67 -0
- package/index.d.ts +822 -38
- package/index.js +5 -3
- package/package.json +10 -8
- package/rescript.json +5 -9
- package/src/Address.res +4 -5
- package/src/Address.res.mjs +9 -12
- package/src/Api.res +15 -0
- package/src/Api.res.mjs +20 -0
- package/src/Batch.res +32 -34
- package/src/Batch.res.mjs +172 -187
- package/src/Bin.res +89 -0
- package/src/Bin.res.mjs +97 -0
- package/src/ChainFetcher.res +33 -57
- package/src/ChainFetcher.res.mjs +197 -227
- package/src/ChainManager.res +6 -14
- package/src/ChainManager.res.mjs +74 -85
- package/src/ChainMap.res +14 -16
- package/src/ChainMap.res.mjs +38 -38
- package/src/Config.res +193 -135
- package/src/Config.res.mjs +566 -592
- package/src/Core.res +182 -0
- package/src/Core.res.mjs +207 -0
- package/src/Ecosystem.res +25 -4
- package/src/Ecosystem.res.mjs +12 -13
- package/src/Env.res +20 -13
- package/src/Env.res.mjs +124 -113
- package/src/EnvSafe.res +269 -0
- package/src/EnvSafe.res.mjs +296 -0
- package/src/EnvSafe.resi +18 -0
- package/src/Envio.res +37 -26
- package/src/Envio.res.mjs +59 -60
- package/src/ErrorHandling.res +2 -2
- package/src/ErrorHandling.res.mjs +15 -15
- package/src/EventConfigBuilder.res +219 -81
- package/src/EventConfigBuilder.res.mjs +259 -202
- package/src/EventProcessing.res +27 -38
- package/src/EventProcessing.res.mjs +165 -183
- package/src/EventUtils.res +11 -11
- package/src/EventUtils.res.mjs +21 -22
- package/src/EvmTypes.res +0 -1
- package/src/EvmTypes.res.mjs +5 -5
- package/src/FetchState.res +360 -256
- package/src/FetchState.res.mjs +958 -914
- package/src/GlobalState.res +365 -351
- package/src/GlobalState.res.mjs +958 -992
- package/src/GlobalStateManager.res +1 -2
- package/src/GlobalStateManager.res.mjs +36 -44
- package/src/HandlerLoader.res +107 -23
- package/src/HandlerLoader.res.mjs +128 -38
- package/src/HandlerRegister.res +127 -103
- package/src/HandlerRegister.res.mjs +164 -164
- package/src/HandlerRegister.resi +12 -4
- package/src/Hasura.res +35 -22
- package/src/Hasura.res.mjs +158 -167
- package/src/InMemoryStore.res +20 -27
- package/src/InMemoryStore.res.mjs +64 -80
- package/src/InMemoryTable.res +34 -39
- package/src/InMemoryTable.res.mjs +165 -170
- package/src/Internal.res +52 -33
- package/src/Internal.res.mjs +84 -81
- package/src/LazyLoader.res.mjs +55 -61
- package/src/LoadLayer.res +77 -78
- package/src/LoadLayer.res.mjs +160 -189
- package/src/LoadManager.res +16 -21
- package/src/LoadManager.res.mjs +79 -84
- package/src/LogSelection.res +236 -68
- package/src/LogSelection.res.mjs +211 -141
- package/src/Logging.res +13 -9
- package/src/Logging.res.mjs +130 -143
- package/src/Main.res +430 -51
- package/src/Main.res.mjs +530 -271
- package/src/Persistence.res +80 -84
- package/src/Persistence.res.mjs +131 -132
- package/src/PgStorage.res +294 -167
- package/src/PgStorage.res.mjs +799 -817
- package/src/Prometheus.res +50 -58
- package/src/Prometheus.res.mjs +345 -373
- package/src/ReorgDetection.res +22 -24
- package/src/ReorgDetection.res.mjs +100 -106
- package/src/SafeCheckpointTracking.res +7 -7
- package/src/SafeCheckpointTracking.res.mjs +40 -43
- package/src/SimulateItems.res +41 -49
- package/src/SimulateItems.res.mjs +257 -272
- package/src/Sink.res +2 -2
- package/src/Sink.res.mjs +22 -26
- package/src/TableIndices.res +1 -2
- package/src/TableIndices.res.mjs +42 -48
- package/src/TestIndexer.res +196 -189
- package/src/TestIndexer.res.mjs +536 -536
- package/src/TestIndexerProxyStorage.res +16 -16
- package/src/TestIndexerProxyStorage.res.mjs +99 -122
- package/src/TestIndexerWorker.res +4 -0
- package/src/TestIndexerWorker.res.mjs +7 -0
- package/src/Throttler.res +3 -3
- package/src/Throttler.res.mjs +23 -24
- package/src/Time.res +1 -1
- package/src/Time.res.mjs +18 -21
- package/src/TopicFilter.res +3 -3
- package/src/TopicFilter.res.mjs +29 -30
- package/src/UserContext.res +93 -54
- package/src/UserContext.res.mjs +197 -182
- package/src/Utils.res +141 -86
- package/src/Utils.res.mjs +334 -295
- package/src/bindings/BigDecimal.res +0 -2
- package/src/bindings/BigDecimal.res.mjs +19 -23
- package/src/bindings/ClickHouse.res +28 -27
- package/src/bindings/ClickHouse.res.mjs +243 -240
- package/src/bindings/DateFns.res +11 -11
- package/src/bindings/DateFns.res.mjs +7 -7
- package/src/bindings/EventSource.res.mjs +2 -2
- package/src/bindings/Express.res +2 -5
- package/src/bindings/Hrtime.res +2 -2
- package/src/bindings/Hrtime.res.mjs +30 -32
- package/src/bindings/Lodash.res.mjs +1 -1
- package/src/bindings/NodeJs.res +14 -9
- package/src/bindings/NodeJs.res.mjs +20 -20
- package/src/bindings/Pino.res +8 -10
- package/src/bindings/Pino.res.mjs +40 -43
- package/src/bindings/Postgres.res +7 -5
- package/src/bindings/Postgres.res.mjs +9 -9
- package/src/bindings/PromClient.res +17 -2
- package/src/bindings/PromClient.res.mjs +30 -7
- package/src/bindings/SDSL.res.mjs +2 -2
- package/src/bindings/Viem.res +4 -4
- package/src/bindings/Viem.res.mjs +20 -22
- package/src/bindings/Vitest.res +1 -1
- package/src/bindings/Vitest.res.mjs +2 -2
- package/src/bindings/WebSocket.res +1 -1
- package/src/db/EntityHistory.res +9 -3
- package/src/db/EntityHistory.res.mjs +84 -59
- package/src/db/InternalTable.res +62 -60
- package/src/db/InternalTable.res.mjs +271 -203
- package/src/db/Schema.res +1 -2
- package/src/db/Schema.res.mjs +28 -32
- package/src/db/Table.res +28 -27
- package/src/db/Table.res.mjs +276 -292
- package/src/sources/EventRouter.res +21 -16
- package/src/sources/EventRouter.res.mjs +55 -57
- package/src/sources/Evm.res +17 -1
- package/src/sources/Evm.res.mjs +16 -8
- package/src/sources/EvmChain.res +15 -17
- package/src/sources/EvmChain.res.mjs +40 -42
- package/src/sources/Fuel.res +14 -1
- package/src/sources/Fuel.res.mjs +16 -8
- package/src/sources/FuelSDK.res +1 -1
- package/src/sources/FuelSDK.res.mjs +6 -8
- package/src/sources/HyperFuel.res +8 -10
- package/src/sources/HyperFuel.res.mjs +113 -123
- package/src/sources/HyperFuelClient.res.mjs +6 -7
- package/src/sources/HyperFuelSource.res +19 -20
- package/src/sources/HyperFuelSource.res.mjs +339 -356
- package/src/sources/HyperSync.res +11 -13
- package/src/sources/HyperSync.res.mjs +206 -220
- package/src/sources/HyperSyncClient.res +5 -7
- package/src/sources/HyperSyncClient.res.mjs +70 -75
- package/src/sources/HyperSyncHeightStream.res +8 -9
- package/src/sources/HyperSyncHeightStream.res.mjs +78 -86
- package/src/sources/HyperSyncJsonApi.res +18 -15
- package/src/sources/HyperSyncJsonApi.res.mjs +201 -231
- package/src/sources/HyperSyncSource.res +17 -21
- package/src/sources/HyperSyncSource.res.mjs +268 -290
- package/src/sources/Rpc.res +5 -5
- package/src/sources/Rpc.res.mjs +168 -192
- package/src/sources/RpcSource.res +166 -167
- package/src/sources/RpcSource.res.mjs +972 -1046
- package/src/sources/RpcWebSocketHeightStream.res +10 -11
- package/src/sources/RpcWebSocketHeightStream.res.mjs +131 -145
- package/src/sources/SimulateSource.res +1 -1
- package/src/sources/SimulateSource.res.mjs +35 -38
- package/src/sources/Source.res +1 -1
- package/src/sources/Source.res.mjs +3 -3
- package/src/sources/SourceManager.res +39 -20
- package/src/sources/SourceManager.res.mjs +340 -371
- package/src/sources/SourceManager.resi +2 -1
- package/src/sources/Svm.res +12 -5
- package/src/sources/Svm.res.mjs +44 -41
- package/src/tui/Tui.res +23 -12
- package/src/tui/Tui.res.mjs +292 -290
- package/src/tui/bindings/Ink.res +2 -4
- package/src/tui/bindings/Ink.res.mjs +35 -41
- package/src/tui/components/BufferedProgressBar.res +7 -7
- package/src/tui/components/BufferedProgressBar.res.mjs +46 -46
- package/src/tui/components/CustomHooks.res +1 -2
- package/src/tui/components/CustomHooks.res.mjs +102 -122
- package/src/tui/components/Messages.res +1 -2
- package/src/tui/components/Messages.res.mjs +38 -42
- package/src/tui/components/SyncETA.res +10 -11
- package/src/tui/components/SyncETA.res.mjs +178 -196
- package/src/tui/components/TuiData.res +1 -1
- package/src/tui/components/TuiData.res.mjs +7 -6
- package/src/vendored/Rest.res +52 -66
- package/src/vendored/Rest.res.mjs +324 -364
- package/svm.schema.json +67 -0
- package/src/Address.gen.ts +0 -8
- package/src/Config.gen.ts +0 -19
- package/src/Envio.gen.ts +0 -55
- package/src/EvmTypes.gen.ts +0 -6
- package/src/InMemoryStore.gen.ts +0 -6
- package/src/Internal.gen.ts +0 -64
- package/src/PgStorage.gen.ts +0 -10
- package/src/PgStorage.res.d.mts +0 -5
- package/src/Types.ts +0 -56
- package/src/bindings/BigDecimal.gen.ts +0 -14
- package/src/bindings/BigDecimal.res.d.mts +0 -5
- package/src/bindings/BigInt.gen.ts +0 -10
- package/src/bindings/BigInt.res +0 -70
- package/src/bindings/BigInt.res.d.mts +0 -5
- package/src/bindings/BigInt.res.mjs +0 -154
- package/src/bindings/Ethers.res.d.mts +0 -5
- package/src/bindings/Pino.gen.ts +0 -17
- package/src/bindings/Postgres.gen.ts +0 -8
- package/src/bindings/Postgres.res.d.mts +0 -5
- package/src/bindings/Promise.res +0 -67
- package/src/bindings/Promise.res.mjs +0 -26
- package/src/db/InternalTable.gen.ts +0 -36
- package/src/sources/HyperSyncClient.gen.ts +0 -19
package/src/Persistence.res
CHANGED
|
@@ -21,8 +21,8 @@ type initialChainState = {
|
|
|
21
21
|
progressBlockNumber: int,
|
|
22
22
|
numEventsProcessed: float,
|
|
23
23
|
firstEventBlockNumber: option<int>,
|
|
24
|
-
timestampCaughtUpToHeadOrEndblock: option<
|
|
25
|
-
|
|
24
|
+
timestampCaughtUpToHeadOrEndblock: option<Date.t>,
|
|
25
|
+
indexingAddresses: array<Internal.indexingAddress>,
|
|
26
26
|
sourceBlockNumber: int,
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -49,6 +49,8 @@ type updatedEntity = {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
type storage = {
|
|
52
|
+
// Identifier used as the `storage` label on Prometheus metrics.
|
|
53
|
+
name: string,
|
|
52
54
|
// Should return true if we already have persisted data
|
|
53
55
|
// and we can skip initialization
|
|
54
56
|
isInitialized: unit => promise<bool>,
|
|
@@ -119,6 +121,9 @@ type storage = {
|
|
|
119
121
|
~updatedEffectsCache: array<updatedEffectCache>,
|
|
120
122
|
~updatedEntities: array<updatedEntity>,
|
|
121
123
|
) => promise<unit>,
|
|
124
|
+
// Release any long-lived resources (e.g. the postgres connection pool) so
|
|
125
|
+
// short-lived CLI commands like `db-migrate setup` can exit cleanly.
|
|
126
|
+
close: unit => promise<unit>,
|
|
122
127
|
}
|
|
123
128
|
|
|
124
129
|
type storageStatus =
|
|
@@ -142,10 +147,9 @@ let make = (
|
|
|
142
147
|
~allEnums,
|
|
143
148
|
~storage,
|
|
144
149
|
) => {
|
|
145
|
-
let allEntities =
|
|
146
|
-
userEntities->Js.Array2.concat([InternalTable.DynamicContractRegistry.entityConfig])
|
|
150
|
+
let allEntities = userEntities->Array.concat([InternalTable.EnvioAddresses.entityConfig])
|
|
147
151
|
let allEnums =
|
|
148
|
-
allEnums->
|
|
152
|
+
allEnums->Array.concat([EntityHistory.RowAction.config->Table.fromGenericEnumConfig])
|
|
149
153
|
{
|
|
150
154
|
userEntities,
|
|
151
155
|
allEntities,
|
|
@@ -192,8 +196,8 @@ let init = {
|
|
|
192
196
|
Logging.info(`Found existing indexer storage. Resuming indexing state...`)
|
|
193
197
|
let initialState = await persistence.storage.resumeInitialState()
|
|
194
198
|
persistence.storageStatus = Ready(initialState)
|
|
195
|
-
let progress =
|
|
196
|
-
initialState.chains->
|
|
199
|
+
let progress = Dict.make()
|
|
200
|
+
initialState.chains->Array.forEach(c => {
|
|
197
201
|
progress->Utils.Dict.setByInt(c.id, c.progressBlockNumber)
|
|
198
202
|
})
|
|
199
203
|
Logging.info({
|
|
@@ -213,7 +217,7 @@ let getInitializedStorageOrThrow = persistence => {
|
|
|
213
217
|
switch persistence.storageStatus {
|
|
214
218
|
| Unknown
|
|
215
219
|
| Initializing(_) =>
|
|
216
|
-
|
|
220
|
+
JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`)
|
|
217
221
|
| Ready(_) => persistence.storage
|
|
218
222
|
}
|
|
219
223
|
}
|
|
@@ -222,7 +226,7 @@ let getInitializedState = persistence => {
|
|
|
222
226
|
switch persistence.storageStatus {
|
|
223
227
|
| Unknown
|
|
224
228
|
| Initializing(_) =>
|
|
225
|
-
|
|
229
|
+
JsError.throwWithMessage(`Failed to access the initial state. The Persistence layer is not initialized.`)
|
|
226
230
|
| Ready(initialState) => initialState
|
|
227
231
|
}
|
|
228
232
|
}
|
|
@@ -237,9 +241,9 @@ let writeBatch = (
|
|
|
237
241
|
switch persistence.storageStatus {
|
|
238
242
|
| Unknown
|
|
239
243
|
| Initializing(_) =>
|
|
240
|
-
|
|
244
|
+
JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`)
|
|
241
245
|
| Ready({cache}) =>
|
|
242
|
-
let updatedEntities = persistence.allEntities->Belt.Array.
|
|
246
|
+
let updatedEntities = persistence.allEntities->Belt.Array.keepMap(entityConfig => {
|
|
243
247
|
let updates =
|
|
244
248
|
inMemoryStore
|
|
245
249
|
->InMemoryStore.getInMemTable(~entityConfig)
|
|
@@ -259,47 +263,40 @@ let writeBatch = (
|
|
|
259
263
|
~allEntities=persistence.allEntities,
|
|
260
264
|
~updatedEntities,
|
|
261
265
|
~updatedEffectsCache={
|
|
262
|
-
|
|
263
|
-
->
|
|
264
|
-
->Belt.Array.keepMapU(effectName => {
|
|
265
|
-
let inMemTable = inMemoryStore.effects->Js.Dict.unsafeGet(effectName)
|
|
266
|
+
let acc = []
|
|
267
|
+
inMemoryStore.effects->Utils.Dict.forEach(inMemTable => {
|
|
266
268
|
let {idsToStore, dict, effect, invalidationsCount} = inMemTable
|
|
267
269
|
switch idsToStore {
|
|
268
|
-
| [] =>
|
|
269
|
-
| ids =>
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
let c = {effectName, count: 0}
|
|
290
|
-
cache->Js.Dict.set(effectName, c)
|
|
291
|
-
c
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
let shouldInitialize = effectCacheRecord.count === 0
|
|
295
|
-
effectCacheRecord.count =
|
|
296
|
-
effectCacheRecord.count + items->Js.Array2.length - invalidationsCount
|
|
297
|
-
Prometheus.EffectCacheCount.set(~count=effectCacheRecord.count, ~effectName)
|
|
298
|
-
{effect, items, shouldInitialize}
|
|
299
|
-
})
|
|
270
|
+
| [] => ()
|
|
271
|
+
| ids =>
|
|
272
|
+
let items = Belt.Array.makeUninitializedUnsafe(ids->Belt.Array.length)
|
|
273
|
+
ids->Belt.Array.forEachWithIndex((index, id) => {
|
|
274
|
+
items->Array.setUnsafe(
|
|
275
|
+
index,
|
|
276
|
+
(
|
|
277
|
+
{
|
|
278
|
+
id,
|
|
279
|
+
output: dict->Dict.getUnsafe(id),
|
|
280
|
+
}: Internal.effectCacheItem
|
|
281
|
+
),
|
|
282
|
+
)
|
|
283
|
+
})
|
|
284
|
+
let effectName = effect.name
|
|
285
|
+
let effectCacheRecord = switch cache->Utils.Dict.dangerouslyGetNonOption(effectName) {
|
|
286
|
+
| Some(c) => c
|
|
287
|
+
| None =>
|
|
288
|
+
let c = {effectName, count: 0}
|
|
289
|
+
cache->Dict.set(effectName, c)
|
|
290
|
+
c
|
|
300
291
|
}
|
|
292
|
+
let shouldInitialize = effectCacheRecord.count === 0
|
|
293
|
+
effectCacheRecord.count =
|
|
294
|
+
effectCacheRecord.count + items->Array.length - invalidationsCount
|
|
295
|
+
Prometheus.EffectCacheCount.set(~count=effectCacheRecord.count, ~effectName)
|
|
296
|
+
acc->Array.push({effect, items, shouldInitialize})->ignore
|
|
301
297
|
}
|
|
302
298
|
})
|
|
299
|
+
acc
|
|
303
300
|
},
|
|
304
301
|
)
|
|
305
302
|
}
|
|
@@ -314,49 +311,48 @@ let prepareRollbackDiff = async (
|
|
|
314
311
|
~rollbackTargetCheckpointId,
|
|
315
312
|
)
|
|
316
313
|
|
|
317
|
-
let deletedEntities =
|
|
318
|
-
let setEntities =
|
|
314
|
+
let deletedEntities = Dict.make()
|
|
315
|
+
let setEntities = Dict.make()
|
|
319
316
|
|
|
320
|
-
let _ =
|
|
321
|
-
|
|
322
|
-
->
|
|
323
|
-
let entityTable = inMemStore->InMemoryStore.getInMemTable(~entityConfig)
|
|
317
|
+
let _ = await persistence.allEntities
|
|
318
|
+
->Belt.Array.map(async entityConfig => {
|
|
319
|
+
let entityTable = inMemStore->InMemoryStore.getInMemTable(~entityConfig)
|
|
324
320
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
321
|
+
let (removedIdsResult, restoredEntitiesResult) = await persistence.storage.getRollbackData(
|
|
322
|
+
~entityConfig,
|
|
323
|
+
~rollbackTargetCheckpointId,
|
|
324
|
+
)
|
|
329
325
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
326
|
+
// Process removed IDs
|
|
327
|
+
removedIdsResult->Array.forEach(data => {
|
|
328
|
+
deletedEntities->Utils.Dict.push(entityConfig.name, data["id"])
|
|
329
|
+
entityTable->InMemoryTable.Entity.set(
|
|
330
|
+
Delete({
|
|
331
|
+
entityId: data["id"],
|
|
332
|
+
checkpointId: rollbackDiffCheckpointId,
|
|
333
|
+
}),
|
|
334
|
+
~shouldSaveHistory=false,
|
|
335
|
+
~containsRollbackDiffChange=true,
|
|
336
|
+
)
|
|
337
|
+
})
|
|
342
338
|
|
|
343
|
-
|
|
339
|
+
let restoredEntities = restoredEntitiesResult->S.parseOrThrow(entityConfig.rowsSchema)
|
|
344
340
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
})
|
|
341
|
+
// Process restored entities
|
|
342
|
+
restoredEntities->Belt.Array.forEach((entity: Internal.entity) => {
|
|
343
|
+
setEntities->Utils.Dict.push(entityConfig.name, entity.id)
|
|
344
|
+
entityTable->InMemoryTable.Entity.set(
|
|
345
|
+
Set({
|
|
346
|
+
entityId: entity.id,
|
|
347
|
+
checkpointId: rollbackDiffCheckpointId,
|
|
348
|
+
entity,
|
|
349
|
+
}),
|
|
350
|
+
~shouldSaveHistory=false,
|
|
351
|
+
~containsRollbackDiffChange=true,
|
|
352
|
+
)
|
|
358
353
|
})
|
|
359
|
-
|
|
354
|
+
})
|
|
355
|
+
->Promise.all
|
|
360
356
|
|
|
361
357
|
{
|
|
362
358
|
"inMemStore": inMemStore,
|
package/src/Persistence.res.mjs
CHANGED
|
@@ -2,37 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
import * as Utils from "./Utils.res.mjs";
|
|
4
4
|
import * as Config from "./Config.res.mjs";
|
|
5
|
-
import * as Js_exn from "rescript/lib/es6/js_exn.js";
|
|
6
5
|
import * as Logging from "./Logging.res.mjs";
|
|
7
|
-
import * as Belt_Array from "rescript/lib/es6/
|
|
6
|
+
import * as Belt_Array from "@rescript/runtime/lib/es6/Belt_Array.js";
|
|
8
7
|
import * as Prometheus from "./Prometheus.res.mjs";
|
|
9
8
|
import * as EntityHistory from "./db/EntityHistory.res.mjs";
|
|
10
9
|
import * as ErrorHandling from "./ErrorHandling.res.mjs";
|
|
11
10
|
import * as InMemoryStore from "./InMemoryStore.res.mjs";
|
|
12
11
|
import * as InMemoryTable from "./InMemoryTable.res.mjs";
|
|
13
|
-
import * as
|
|
12
|
+
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
|
|
14
13
|
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
|
|
15
|
-
import * as
|
|
14
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
let StorageError = /* @__PURE__ */Primitive_exceptions.create("Persistence.StorageError");
|
|
18
17
|
|
|
19
18
|
function make(userEntities, allEnums, storage) {
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
let allEntities = userEntities.concat([Config.EnvioAddresses.entityConfig]);
|
|
20
|
+
let allEnums$1 = allEnums.concat([EntityHistory.RowAction.config]);
|
|
22
21
|
return {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
userEntities: userEntities,
|
|
23
|
+
allEntities: allEntities,
|
|
24
|
+
allEnums: allEnums$1,
|
|
25
|
+
storageStatus: "Unknown",
|
|
26
|
+
storage: storage
|
|
27
|
+
};
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
async function init(persistence, chainConfigs, resetOpt) {
|
|
32
|
-
|
|
31
|
+
let reset = resetOpt !== undefined ? resetOpt : false;
|
|
33
32
|
try {
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
let promise = persistence.storageStatus;
|
|
34
|
+
let shouldRun;
|
|
36
35
|
if (typeof promise !== "object") {
|
|
37
36
|
shouldRun = true;
|
|
38
37
|
} else if (promise.TAG === "Initializing") {
|
|
@@ -42,172 +41,172 @@ async function init(persistence, chainConfigs, resetOpt) {
|
|
|
42
41
|
shouldRun = reset;
|
|
43
42
|
}
|
|
44
43
|
if (!shouldRun) {
|
|
45
|
-
return
|
|
44
|
+
return;
|
|
46
45
|
}
|
|
47
|
-
|
|
46
|
+
let resolveRef = {
|
|
48
47
|
contents: null
|
|
49
48
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
let promise$1 = new Promise((resolve, param) => {
|
|
50
|
+
resolveRef.contents = resolve;
|
|
51
|
+
});
|
|
53
52
|
persistence.storageStatus = {
|
|
54
53
|
TAG: "Initializing",
|
|
55
54
|
_0: promise$1
|
|
56
55
|
};
|
|
57
56
|
if (reset || !await persistence.storage.isInitialized()) {
|
|
58
|
-
Logging.info(
|
|
59
|
-
|
|
60
|
-
Logging.info(
|
|
57
|
+
Logging.info(`Initializing the indexer storage...`);
|
|
58
|
+
let initialState = await persistence.storage.initialize(chainConfigs, persistence.allEntities, persistence.allEnums);
|
|
59
|
+
Logging.info(`The indexer storage is ready. Starting indexing!`);
|
|
61
60
|
persistence.storageStatus = {
|
|
62
61
|
TAG: "Ready",
|
|
63
62
|
_0: initialState
|
|
64
63
|
};
|
|
65
64
|
} else {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
tmp = typeof match !== "object"
|
|
65
|
+
let match = persistence.storageStatus;
|
|
66
|
+
let tmp;
|
|
67
|
+
tmp = typeof match !== "object" ? false : match.TAG === "Initializing";
|
|
69
68
|
if (tmp) {
|
|
70
|
-
Logging.info(
|
|
71
|
-
|
|
69
|
+
Logging.info(`Found existing indexer storage. Resuming indexing state...`);
|
|
70
|
+
let initialState$1 = await persistence.storage.resumeInitialState();
|
|
72
71
|
persistence.storageStatus = {
|
|
73
72
|
TAG: "Ready",
|
|
74
73
|
_0: initialState$1
|
|
75
74
|
};
|
|
76
|
-
|
|
77
|
-
initialState$1.chains.forEach(
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
let progress = {};
|
|
76
|
+
initialState$1.chains.forEach(c => {
|
|
77
|
+
progress[c.id] = c.progressBlockNumber;
|
|
78
|
+
});
|
|
80
79
|
Logging.info({
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
msg: `Successfully resumed indexing state! Continuing from the last checkpoint.`,
|
|
81
|
+
progress: progress
|
|
82
|
+
});
|
|
84
83
|
}
|
|
85
|
-
|
|
86
84
|
}
|
|
87
85
|
return resolveRef.contents();
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return ErrorHandling.mkLogAndRaise(undefined, "Failed to initialize the indexer storage.", exn);
|
|
86
|
+
} catch (raw_exn) {
|
|
87
|
+
let exn = Primitive_exceptions.internalToException(raw_exn);
|
|
88
|
+
return ErrorHandling.mkLogAndRaise(undefined, `Failed to initialize the indexer storage.`, exn);
|
|
92
89
|
}
|
|
93
90
|
}
|
|
94
91
|
|
|
95
92
|
function getInitializedStorageOrThrow(persistence) {
|
|
96
|
-
|
|
93
|
+
let match = persistence.storageStatus;
|
|
97
94
|
if (typeof match !== "object" || match.TAG === "Initializing") {
|
|
98
|
-
return
|
|
95
|
+
return Stdlib_JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`);
|
|
99
96
|
} else {
|
|
100
97
|
return persistence.storage;
|
|
101
98
|
}
|
|
102
99
|
}
|
|
103
100
|
|
|
104
101
|
function getInitializedState(persistence) {
|
|
105
|
-
|
|
102
|
+
let initialState = persistence.storageStatus;
|
|
106
103
|
if (typeof initialState !== "object" || initialState.TAG === "Initializing") {
|
|
107
|
-
return
|
|
104
|
+
return Stdlib_JsError.throwWithMessage(`Failed to access the initial state. The Persistence layer is not initialized.`);
|
|
108
105
|
} else {
|
|
109
106
|
return initialState._0;
|
|
110
107
|
}
|
|
111
108
|
}
|
|
112
109
|
|
|
113
110
|
function writeBatch(persistence, batch, config, inMemoryStore, isInReorgThreshold) {
|
|
114
|
-
|
|
111
|
+
let match = persistence.storageStatus;
|
|
115
112
|
if (typeof match !== "object") {
|
|
116
|
-
return
|
|
113
|
+
return Stdlib_JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`);
|
|
117
114
|
}
|
|
118
115
|
if (match.TAG === "Initializing") {
|
|
119
|
-
return
|
|
116
|
+
return Stdlib_JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`);
|
|
120
117
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
118
|
+
let cache = match._0.cache;
|
|
119
|
+
let updatedEntities = Belt_Array.keepMap(persistence.allEntities, entityConfig => {
|
|
120
|
+
let updates = InMemoryTable.Entity.updates(InMemoryStore.getInMemTable(inMemoryStore, entityConfig));
|
|
121
|
+
if (Utils.$$Array.isEmpty(updates)) {
|
|
122
|
+
return;
|
|
123
|
+
} else {
|
|
124
|
+
return {
|
|
125
|
+
entityConfig: entityConfig,
|
|
126
|
+
updates: updates
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
let acc = [];
|
|
131
|
+
return persistence.storage.writeBatch(batch, InMemoryTable.values(inMemoryStore.rawEvents), inMemoryStore.rollbackTargetCheckpointId, isInReorgThreshold, config, persistence.allEntities, (Utils.Dict.forEach(inMemoryStore.effects, inMemTable => {
|
|
132
|
+
let idsToStore = inMemTable.idsToStore;
|
|
133
|
+
let invalidationsCount = inMemTable.invalidationsCount;
|
|
134
|
+
let effect = inMemTable.effect;
|
|
135
|
+
let dict = inMemTable.dict;
|
|
136
|
+
if (idsToStore.length === 0) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
let items = new Array(idsToStore.length);
|
|
140
|
+
Belt_Array.forEachWithIndex(idsToStore, (index, id) => {
|
|
141
|
+
items[index] = {
|
|
142
|
+
id: id,
|
|
143
|
+
output: dict[id]
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
let effectName = effect.name;
|
|
147
|
+
let c = cache[effectName];
|
|
148
|
+
let effectCacheRecord;
|
|
149
|
+
if (c !== undefined) {
|
|
150
|
+
effectCacheRecord = c;
|
|
151
|
+
} else {
|
|
152
|
+
let c$1 = {
|
|
153
|
+
effectName: effectName,
|
|
154
|
+
count: 0
|
|
155
|
+
};
|
|
156
|
+
cache[effectName] = c$1;
|
|
157
|
+
effectCacheRecord = c$1;
|
|
158
|
+
}
|
|
159
|
+
let shouldInitialize = effectCacheRecord.count === 0;
|
|
160
|
+
effectCacheRecord.count = (effectCacheRecord.count + items.length | 0) - invalidationsCount | 0;
|
|
161
|
+
Prometheus.EffectCacheCount.set(effectCacheRecord.count, effectName);
|
|
162
|
+
acc.push({
|
|
163
|
+
effect: effect,
|
|
164
|
+
items: items,
|
|
165
|
+
shouldInitialize: shouldInitialize
|
|
166
|
+
});
|
|
167
|
+
}), acc), updatedEntities);
|
|
169
168
|
}
|
|
170
169
|
|
|
171
170
|
async function prepareRollbackDiff(persistence, rollbackTargetCheckpointId, rollbackDiffCheckpointId) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
await Promise.all(Belt_Array.map(persistence.allEntities,
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
171
|
+
let inMemStore = InMemoryStore.make(persistence.allEntities, rollbackTargetCheckpointId);
|
|
172
|
+
let deletedEntities = {};
|
|
173
|
+
let setEntities = {};
|
|
174
|
+
await Promise.all(Belt_Array.map(persistence.allEntities, async entityConfig => {
|
|
175
|
+
let entityTable = InMemoryStore.getInMemTable(inMemStore, entityConfig);
|
|
176
|
+
let match = await persistence.storage.getRollbackData(entityConfig, rollbackTargetCheckpointId);
|
|
177
|
+
match[0].forEach(data => {
|
|
178
|
+
Utils.Dict.push(deletedEntities, entityConfig.name, data.id);
|
|
179
|
+
InMemoryTable.Entity.set(entityTable, {
|
|
180
|
+
type: "DELETE",
|
|
181
|
+
entityId: data.id,
|
|
182
|
+
checkpointId: rollbackDiffCheckpointId
|
|
183
|
+
}, false, true);
|
|
184
|
+
});
|
|
185
|
+
let restoredEntities = S$RescriptSchema.parseOrThrow(match[1], entityConfig.rowsSchema);
|
|
186
|
+
return Belt_Array.forEach(restoredEntities, entity => {
|
|
187
|
+
Utils.Dict.push(setEntities, entityConfig.name, entity.id);
|
|
188
|
+
InMemoryTable.Entity.set(entityTable, {
|
|
189
|
+
type: "SET",
|
|
190
|
+
entityId: entity.id,
|
|
191
|
+
entity: entity,
|
|
192
|
+
checkpointId: rollbackDiffCheckpointId
|
|
193
|
+
}, false, true);
|
|
194
|
+
});
|
|
195
|
+
}));
|
|
197
196
|
return {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
197
|
+
inMemStore: inMemStore,
|
|
198
|
+
deletedEntities: deletedEntities,
|
|
199
|
+
setEntities: setEntities
|
|
200
|
+
};
|
|
202
201
|
}
|
|
203
202
|
|
|
204
203
|
export {
|
|
205
|
-
StorageError
|
|
206
|
-
make
|
|
207
|
-
init
|
|
208
|
-
getInitializedStorageOrThrow
|
|
209
|
-
getInitializedState
|
|
210
|
-
writeBatch
|
|
211
|
-
prepareRollbackDiff
|
|
204
|
+
StorageError,
|
|
205
|
+
make,
|
|
206
|
+
init,
|
|
207
|
+
getInitializedStorageOrThrow,
|
|
208
|
+
getInitializedState,
|
|
209
|
+
writeBatch,
|
|
210
|
+
prepareRollbackDiff,
|
|
212
211
|
}
|
|
213
212
|
/* Utils Not a pure module */
|