envio 3.0.0-alpha.21 → 3.0.0-alpha.22
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/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 +428 -51
- package/src/Main.res.mjs +528 -271
- package/src/Persistence.res +77 -84
- package/src/Persistence.res.mjs +131 -132
- package/src/PgStorage.res +291 -167
- package/src/PgStorage.res.mjs +797 -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 +15 -16
- package/src/TestIndexerProxyStorage.res.mjs +98 -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 +2 -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/Batch.res.mjs
CHANGED
|
@@ -1,38 +1,34 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
3
|
import * as Utils from "./Utils.res.mjs";
|
|
4
|
-
import * as $$BigInt from "./bindings/BigInt.res.mjs";
|
|
5
4
|
import * as ChainMap from "./ChainMap.res.mjs";
|
|
6
|
-
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
|
|
7
5
|
import * as FetchState from "./FetchState.res.mjs";
|
|
8
6
|
import * as ReorgDetection from "./ReorgDetection.res.mjs";
|
|
9
7
|
|
|
10
8
|
function getOrderedNextChain(fetchStates, batchSizePerChain) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for(
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
let earliestChain;
|
|
10
|
+
let earliestChainTimestamp = 0;
|
|
11
|
+
let chainKeys = ChainMap.keys(fetchStates);
|
|
12
|
+
for (let idx = 0, idx_finish = chainKeys.length - 1; idx <= idx_finish; ++idx) {
|
|
13
|
+
let chain = chainKeys[idx];
|
|
14
|
+
let fetchState = ChainMap.get(fetchStates, chain);
|
|
17
15
|
if (FetchState.isActivelyIndexing(fetchState)) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
let batchSize = batchSizePerChain[chain];
|
|
17
|
+
let timestamp = FetchState.getTimestampAt(fetchState, batchSize !== undefined ? batchSize : 0);
|
|
18
|
+
let earliestChain$1 = earliestChain;
|
|
21
19
|
if (!(earliestChain$1 !== undefined && (timestamp > earliestChainTimestamp || timestamp === earliestChainTimestamp && chain > earliestChain$1.chainId))) {
|
|
22
20
|
earliestChain = fetchState;
|
|
23
21
|
earliestChainTimestamp = timestamp;
|
|
24
22
|
}
|
|
25
|
-
|
|
26
23
|
}
|
|
27
|
-
|
|
28
24
|
}
|
|
29
25
|
return earliestChain;
|
|
30
26
|
}
|
|
31
27
|
|
|
32
|
-
|
|
28
|
+
let immutableEmptyBatchSizePerChain = {};
|
|
33
29
|
|
|
34
30
|
function hasOrderedReadyItem(fetchStates) {
|
|
35
|
-
|
|
31
|
+
let fetchState = getOrderedNextChain(fetchStates, immutableEmptyBatchSizePerChain);
|
|
36
32
|
if (fetchState !== undefined) {
|
|
37
33
|
return FetchState.hasReadyItem(fetchState);
|
|
38
34
|
} else {
|
|
@@ -41,13 +37,13 @@ function hasOrderedReadyItem(fetchStates) {
|
|
|
41
37
|
}
|
|
42
38
|
|
|
43
39
|
function hasUnorderedReadyItem(fetchStates) {
|
|
44
|
-
return ChainMap.values(fetchStates).some(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
return ChainMap.values(fetchStates).some(fetchState => {
|
|
41
|
+
if (FetchState.isActivelyIndexing(fetchState)) {
|
|
42
|
+
return FetchState.hasReadyItem(fetchState);
|
|
43
|
+
} else {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
function hasMultichainReadyItem(fetchStates, multichain) {
|
|
@@ -61,37 +57,35 @@ function hasMultichainReadyItem(fetchStates, multichain) {
|
|
|
61
57
|
function getChainAfterBatchIfProgressed(chainBeforeBatch, progressBlockNumberAfterBatch, fetchStateAfterBatch, batchSize) {
|
|
62
58
|
if (chainBeforeBatch.progressBlockNumber < progressBlockNumberAfterBatch) {
|
|
63
59
|
return {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
60
|
+
batchSize: batchSize,
|
|
61
|
+
progressBlockNumber: progressBlockNumberAfterBatch,
|
|
62
|
+
sourceBlockNumber: chainBeforeBatch.sourceBlockNumber,
|
|
63
|
+
totalEventsProcessed: chainBeforeBatch.totalEventsProcessed + batchSize,
|
|
64
|
+
fetchState: fetchStateAfterBatch,
|
|
65
|
+
isProgressAtHeadWhenBatchCreated: progressBlockNumberAfterBatch >= chainBeforeBatch.sourceBlockNumber - chainBeforeBatch.chainConfig.blockLag
|
|
66
|
+
};
|
|
71
67
|
}
|
|
72
|
-
|
|
73
68
|
}
|
|
74
69
|
|
|
75
70
|
function getProgressedChainsById(chainsBeforeBatch, batchSizePerChain, progressBlockNumberPerChain) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}));
|
|
71
|
+
let progressedChainsById = {};
|
|
72
|
+
ChainMap.values(chainsBeforeBatch).forEach(chainBeforeBatch => {
|
|
73
|
+
let fetchState = chainBeforeBatch.fetchState;
|
|
74
|
+
let progressBlockNumber = progressBlockNumberPerChain[fetchState.chainId.toString()];
|
|
75
|
+
let progressBlockNumberAfterBatch = progressBlockNumber !== undefined ? progressBlockNumber : chainBeforeBatch.progressBlockNumber;
|
|
76
|
+
let batchSize = batchSizePerChain[fetchState.chainId.toString()];
|
|
77
|
+
let progressedChain;
|
|
78
|
+
if (batchSize !== undefined) {
|
|
79
|
+
let leftItems = fetchState.buffer.slice(batchSize);
|
|
80
|
+
progressedChain = getChainAfterBatchIfProgressed(chainBeforeBatch, progressBlockNumberAfterBatch, FetchState.updateInternal(fetchState, undefined, undefined, leftItems, undefined, undefined), batchSize);
|
|
81
|
+
} else {
|
|
82
|
+
progressedChain = getChainAfterBatchIfProgressed(chainBeforeBatch, progressBlockNumberAfterBatch, chainBeforeBatch.fetchState, 0);
|
|
83
|
+
}
|
|
84
|
+
if (progressedChain !== undefined) {
|
|
85
|
+
progressedChainsById[chainBeforeBatch.fetchState.chainId] = progressedChain;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
95
89
|
return progressedChainsById;
|
|
96
90
|
}
|
|
97
91
|
|
|
@@ -99,11 +93,11 @@ function addReorgCheckpoints(prevCheckpointId, reorgDetection, fromBlockExclusiv
|
|
|
99
93
|
if (!(reorgDetection.shouldRollbackOnReorg && !Utils.Dict.isEmpty(reorgDetection.dataByBlockNumber))) {
|
|
100
94
|
return prevCheckpointId;
|
|
101
95
|
}
|
|
102
|
-
|
|
103
|
-
for(
|
|
104
|
-
|
|
96
|
+
let prevCheckpointId$1 = prevCheckpointId;
|
|
97
|
+
for (let blockNumber = fromBlockExclusive + 1, blockNumber_finish = toBlockExclusive - 1; blockNumber <= blockNumber_finish; ++blockNumber) {
|
|
98
|
+
let hash = ReorgDetection.getHashByBlockNumber(reorgDetection, blockNumber);
|
|
105
99
|
if (hash !== null) {
|
|
106
|
-
|
|
100
|
+
let checkpointId = prevCheckpointId$1 + 1n;
|
|
107
101
|
prevCheckpointId$1 = checkpointId;
|
|
108
102
|
mutCheckpointIds.push(checkpointId);
|
|
109
103
|
mutCheckpointChainIds.push(chainId);
|
|
@@ -111,48 +105,45 @@ function addReorgCheckpoints(prevCheckpointId, reorgDetection, fromBlockExclusiv
|
|
|
111
105
|
mutCheckpointBlockHashes.push(hash);
|
|
112
106
|
mutCheckpointEventsProcessed.push(0);
|
|
113
107
|
}
|
|
114
|
-
|
|
115
108
|
}
|
|
116
109
|
return prevCheckpointId$1;
|
|
117
110
|
}
|
|
118
111
|
|
|
119
112
|
function prepareOrderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSizeTarget) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
while(totalBatchSize < batchSizeTarget && !isFinished) {
|
|
135
|
-
var fetchState = getOrderedNextChain(fetchStates, mutBatchSizePerChain);
|
|
113
|
+
let totalBatchSize = 0;
|
|
114
|
+
let isFinished = false;
|
|
115
|
+
let prevCheckpointId = checkpointIdBeforeBatch;
|
|
116
|
+
let mutBatchSizePerChain = {};
|
|
117
|
+
let mutProgressBlockNumberPerChain = {};
|
|
118
|
+
let fetchStates = ChainMap.map(chainsBeforeBatch, chainBeforeBatch => chainBeforeBatch.fetchState);
|
|
119
|
+
let items = [];
|
|
120
|
+
let checkpointIds = [];
|
|
121
|
+
let checkpointChainIds = [];
|
|
122
|
+
let checkpointBlockNumbers = [];
|
|
123
|
+
let checkpointBlockHashes = [];
|
|
124
|
+
let checkpointEventsProcessed = [];
|
|
125
|
+
while (totalBatchSize < batchSizeTarget && !isFinished) {
|
|
126
|
+
let fetchState = getOrderedNextChain(fetchStates, mutBatchSizePerChain);
|
|
136
127
|
if (fetchState !== undefined) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
128
|
+
let chainBeforeBatch = ChainMap.get(chainsBeforeBatch, ChainMap.Chain.makeUnsafe(fetchState.chainId));
|
|
129
|
+
let batchSize = mutBatchSizePerChain[fetchState.chainId];
|
|
130
|
+
let itemsCountBefore = batchSize !== undefined ? batchSize : 0;
|
|
131
|
+
let progressBlockNumber = mutProgressBlockNumberPerChain[fetchState.chainId];
|
|
132
|
+
let prevBlockNumber = progressBlockNumber !== undefined ? progressBlockNumber : chainBeforeBatch.progressBlockNumber;
|
|
133
|
+
let newItemsCount = FetchState.getReadyItemsCount(fetchState, 1, itemsCountBefore);
|
|
143
134
|
if (newItemsCount > 0) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
135
|
+
let item0 = fetchState.buffer[itemsCountBefore];
|
|
136
|
+
let blockNumber = item0.blockNumber;
|
|
137
|
+
let chainId = fetchState.chainId;
|
|
138
|
+
let reorgDetection = chainBeforeBatch.reorgDetection;
|
|
139
|
+
let prevCheckpointId$1 = prevCheckpointId;
|
|
140
|
+
let tmp;
|
|
150
141
|
if (reorgDetection.shouldRollbackOnReorg && !Utils.Dict.isEmpty(reorgDetection.dataByBlockNumber)) {
|
|
151
|
-
|
|
152
|
-
for(
|
|
153
|
-
|
|
142
|
+
let prevCheckpointId$2 = prevCheckpointId$1;
|
|
143
|
+
for (let blockNumber$1 = prevBlockNumber + 1, blockNumber_finish = blockNumber - 1; blockNumber$1 <= blockNumber_finish; ++blockNumber$1) {
|
|
144
|
+
let hash = ReorgDetection.getHashByBlockNumber(reorgDetection, blockNumber$1);
|
|
154
145
|
if (hash !== null) {
|
|
155
|
-
|
|
146
|
+
let checkpointId = prevCheckpointId$2 + 1n;
|
|
156
147
|
prevCheckpointId$2 = checkpointId;
|
|
157
148
|
checkpointIds.push(checkpointId);
|
|
158
149
|
checkpointChainIds.push(chainId);
|
|
@@ -160,16 +151,15 @@ function prepareOrderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSi
|
|
|
160
151
|
checkpointBlockHashes.push(hash);
|
|
161
152
|
checkpointEventsProcessed.push(0);
|
|
162
153
|
}
|
|
163
|
-
|
|
164
154
|
}
|
|
165
155
|
tmp = prevCheckpointId$2;
|
|
166
156
|
} else {
|
|
167
157
|
tmp = prevCheckpointId$1;
|
|
168
158
|
}
|
|
169
159
|
prevCheckpointId = tmp;
|
|
170
|
-
|
|
160
|
+
let checkpointId$1 = prevCheckpointId + 1n;
|
|
171
161
|
items.push(item0);
|
|
172
|
-
for(
|
|
162
|
+
for (let idx = 1, idx_finish = newItemsCount - 1; idx <= idx_finish; ++idx) {
|
|
173
163
|
items.push(fetchState.buffer[itemsCountBefore + idx]);
|
|
174
164
|
}
|
|
175
165
|
checkpointIds.push(checkpointId$1);
|
|
@@ -182,18 +172,18 @@ function prepareOrderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSi
|
|
|
182
172
|
mutBatchSizePerChain[fetchState.chainId] = itemsCountBefore + newItemsCount;
|
|
183
173
|
mutProgressBlockNumberPerChain[fetchState.chainId] = blockNumber;
|
|
184
174
|
} else {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
175
|
+
let blockNumberAfterBatch = FetchState.bufferBlockNumber(fetchState);
|
|
176
|
+
let chainId$1 = fetchState.chainId;
|
|
177
|
+
let toBlockExclusive = blockNumberAfterBatch + 1;
|
|
178
|
+
let reorgDetection$1 = chainBeforeBatch.reorgDetection;
|
|
179
|
+
let prevCheckpointId$3 = prevCheckpointId;
|
|
180
|
+
let tmp$1;
|
|
191
181
|
if (reorgDetection$1.shouldRollbackOnReorg && !Utils.Dict.isEmpty(reorgDetection$1.dataByBlockNumber)) {
|
|
192
|
-
|
|
193
|
-
for(
|
|
194
|
-
|
|
182
|
+
let prevCheckpointId$4 = prevCheckpointId$3;
|
|
183
|
+
for (let blockNumber$2 = prevBlockNumber + 1, blockNumber_finish$1 = toBlockExclusive - 1; blockNumber$2 <= blockNumber_finish$1; ++blockNumber$2) {
|
|
184
|
+
let hash$1 = ReorgDetection.getHashByBlockNumber(reorgDetection$1, blockNumber$2);
|
|
195
185
|
if (hash$1 !== null) {
|
|
196
|
-
|
|
186
|
+
let checkpointId$2 = prevCheckpointId$4 + 1n;
|
|
197
187
|
prevCheckpointId$4 = checkpointId$2;
|
|
198
188
|
checkpointIds.push(checkpointId$2);
|
|
199
189
|
checkpointChainIds.push(chainId$1);
|
|
@@ -201,7 +191,6 @@ function prepareOrderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSi
|
|
|
201
191
|
checkpointBlockHashes.push(hash$1);
|
|
202
192
|
checkpointEventsProcessed.push(0);
|
|
203
193
|
}
|
|
204
|
-
|
|
205
194
|
}
|
|
206
195
|
tmp$1 = prevCheckpointId$4;
|
|
207
196
|
} else {
|
|
@@ -216,54 +205,52 @@ function prepareOrderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSi
|
|
|
216
205
|
}
|
|
217
206
|
};
|
|
218
207
|
return {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
208
|
+
totalBatchSize: totalBatchSize,
|
|
209
|
+
items: items,
|
|
210
|
+
progressedChainsById: getProgressedChainsById(chainsBeforeBatch, mutBatchSizePerChain, mutProgressBlockNumberPerChain),
|
|
211
|
+
checkpointIds: checkpointIds,
|
|
212
|
+
checkpointChainIds: checkpointChainIds,
|
|
213
|
+
checkpointBlockNumbers: checkpointBlockNumbers,
|
|
214
|
+
checkpointBlockHashes: checkpointBlockHashes,
|
|
215
|
+
checkpointEventsProcessed: checkpointEventsProcessed
|
|
216
|
+
};
|
|
228
217
|
}
|
|
229
218
|
|
|
230
219
|
function prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSizeTarget) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
var chainBeforeBatch = ChainMap.get(chainsBeforeBatch, ChainMap.Chain.makeUnsafe(fetchState.chainId));
|
|
250
|
-
var prevBlockNumber = chainBeforeBatch.progressBlockNumber;
|
|
220
|
+
let preparedFetchStates = FetchState.sortForUnorderedBatch(ChainMap.values(chainsBeforeBatch).map(chainBeforeBatch => chainBeforeBatch.fetchState), batchSizeTarget);
|
|
221
|
+
let chainIdx = 0;
|
|
222
|
+
let preparedNumber = preparedFetchStates.length;
|
|
223
|
+
let totalBatchSize = 0;
|
|
224
|
+
let prevCheckpointId = checkpointIdBeforeBatch;
|
|
225
|
+
let mutBatchSizePerChain = {};
|
|
226
|
+
let mutProgressBlockNumberPerChain = {};
|
|
227
|
+
let items = [];
|
|
228
|
+
let checkpointIds = [];
|
|
229
|
+
let checkpointChainIds = [];
|
|
230
|
+
let checkpointBlockNumbers = [];
|
|
231
|
+
let checkpointBlockHashes = [];
|
|
232
|
+
let checkpointEventsProcessed = [];
|
|
233
|
+
while (totalBatchSize < batchSizeTarget && chainIdx < preparedNumber) {
|
|
234
|
+
let fetchState = preparedFetchStates[chainIdx];
|
|
235
|
+
let chainBatchSize = FetchState.getReadyItemsCount(fetchState, batchSizeTarget - totalBatchSize, 0);
|
|
236
|
+
let chainBeforeBatch = ChainMap.get(chainsBeforeBatch, ChainMap.Chain.makeUnsafe(fetchState.chainId));
|
|
237
|
+
let prevBlockNumber = chainBeforeBatch.progressBlockNumber;
|
|
251
238
|
if (chainBatchSize > 0) {
|
|
252
|
-
for(
|
|
253
|
-
|
|
254
|
-
|
|
239
|
+
for (let idx = 0, idx_finish = chainBatchSize - 1; idx <= idx_finish; ++idx) {
|
|
240
|
+
let item = fetchState.buffer[idx];
|
|
241
|
+
let blockNumber = item.blockNumber;
|
|
255
242
|
if (blockNumber !== prevBlockNumber) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
243
|
+
let chainId = fetchState.chainId;
|
|
244
|
+
let fromBlockExclusive = prevBlockNumber;
|
|
245
|
+
let reorgDetection = chainBeforeBatch.reorgDetection;
|
|
246
|
+
let prevCheckpointId$1 = prevCheckpointId;
|
|
247
|
+
let tmp;
|
|
261
248
|
if (reorgDetection.shouldRollbackOnReorg && !Utils.Dict.isEmpty(reorgDetection.dataByBlockNumber)) {
|
|
262
|
-
|
|
263
|
-
for(
|
|
264
|
-
|
|
249
|
+
let prevCheckpointId$2 = prevCheckpointId$1;
|
|
250
|
+
for (let blockNumber$1 = fromBlockExclusive + 1, blockNumber_finish = blockNumber - 1; blockNumber$1 <= blockNumber_finish; ++blockNumber$1) {
|
|
251
|
+
let hash = ReorgDetection.getHashByBlockNumber(reorgDetection, blockNumber$1);
|
|
265
252
|
if (hash !== null) {
|
|
266
|
-
|
|
253
|
+
let checkpointId = prevCheckpointId$2 + 1n;
|
|
267
254
|
prevCheckpointId$2 = checkpointId;
|
|
268
255
|
checkpointIds.push(checkpointId);
|
|
269
256
|
checkpointChainIds.push(chainId);
|
|
@@ -271,14 +258,13 @@ function prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batch
|
|
|
271
258
|
checkpointBlockHashes.push(hash);
|
|
272
259
|
checkpointEventsProcessed.push(0);
|
|
273
260
|
}
|
|
274
|
-
|
|
275
261
|
}
|
|
276
262
|
tmp = prevCheckpointId$2;
|
|
277
263
|
} else {
|
|
278
264
|
tmp = prevCheckpointId$1;
|
|
279
265
|
}
|
|
280
266
|
prevCheckpointId = tmp;
|
|
281
|
-
|
|
267
|
+
let checkpointId$1 = prevCheckpointId + 1n;
|
|
282
268
|
checkpointIds.push(checkpointId$1);
|
|
283
269
|
checkpointChainIds.push(fetchState.chainId);
|
|
284
270
|
checkpointBlockNumbers.push(blockNumber);
|
|
@@ -287,7 +273,7 @@ function prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batch
|
|
|
287
273
|
prevBlockNumber = blockNumber;
|
|
288
274
|
prevCheckpointId = checkpointId$1;
|
|
289
275
|
} else {
|
|
290
|
-
|
|
276
|
+
let lastIndex = checkpointEventsProcessed.length - 1;
|
|
291
277
|
checkpointEventsProcessed[lastIndex] = checkpointEventsProcessed[lastIndex] + 1;
|
|
292
278
|
}
|
|
293
279
|
items.push(item);
|
|
@@ -295,19 +281,19 @@ function prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batch
|
|
|
295
281
|
totalBatchSize = totalBatchSize + chainBatchSize;
|
|
296
282
|
mutBatchSizePerChain[fetchState.chainId] = chainBatchSize;
|
|
297
283
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
284
|
+
let progressBlockNumberAfterBatch = FetchState.getUnorderedMultichainProgressBlockNumberAt(fetchState, chainBatchSize);
|
|
285
|
+
let chainId$1 = fetchState.chainId;
|
|
286
|
+
let toBlockExclusive = progressBlockNumberAfterBatch + 1;
|
|
287
|
+
let fromBlockExclusive$1 = prevBlockNumber;
|
|
288
|
+
let reorgDetection$1 = chainBeforeBatch.reorgDetection;
|
|
289
|
+
let prevCheckpointId$3 = prevCheckpointId;
|
|
290
|
+
let tmp$1;
|
|
305
291
|
if (reorgDetection$1.shouldRollbackOnReorg && !Utils.Dict.isEmpty(reorgDetection$1.dataByBlockNumber)) {
|
|
306
|
-
|
|
307
|
-
for(
|
|
308
|
-
|
|
292
|
+
let prevCheckpointId$4 = prevCheckpointId$3;
|
|
293
|
+
for (let blockNumber$2 = fromBlockExclusive$1 + 1, blockNumber_finish$1 = toBlockExclusive - 1; blockNumber$2 <= blockNumber_finish$1; ++blockNumber$2) {
|
|
294
|
+
let hash$1 = ReorgDetection.getHashByBlockNumber(reorgDetection$1, blockNumber$2);
|
|
309
295
|
if (hash$1 !== null) {
|
|
310
|
-
|
|
296
|
+
let checkpointId$2 = prevCheckpointId$4 + 1n;
|
|
311
297
|
prevCheckpointId$4 = checkpointId$2;
|
|
312
298
|
checkpointIds.push(checkpointId$2);
|
|
313
299
|
checkpointChainIds.push(chainId$1);
|
|
@@ -315,7 +301,6 @@ function prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batch
|
|
|
315
301
|
checkpointBlockHashes.push(hash$1);
|
|
316
302
|
checkpointEventsProcessed.push(0);
|
|
317
303
|
}
|
|
318
|
-
|
|
319
304
|
}
|
|
320
305
|
tmp$1 = prevCheckpointId$4;
|
|
321
306
|
} else {
|
|
@@ -326,19 +311,19 @@ function prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batch
|
|
|
326
311
|
chainIdx = chainIdx + 1;
|
|
327
312
|
};
|
|
328
313
|
return {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
314
|
+
totalBatchSize: totalBatchSize,
|
|
315
|
+
items: items,
|
|
316
|
+
progressedChainsById: getProgressedChainsById(chainsBeforeBatch, mutBatchSizePerChain, mutProgressBlockNumberPerChain),
|
|
317
|
+
checkpointIds: checkpointIds,
|
|
318
|
+
checkpointChainIds: checkpointChainIds,
|
|
319
|
+
checkpointBlockNumbers: checkpointBlockNumbers,
|
|
320
|
+
checkpointBlockHashes: checkpointBlockHashes,
|
|
321
|
+
checkpointEventsProcessed: checkpointEventsProcessed
|
|
322
|
+
};
|
|
338
323
|
}
|
|
339
324
|
|
|
340
325
|
function make(checkpointIdBeforeBatch, chainsBeforeBatch, multichain, batchSizeTarget) {
|
|
341
|
-
|
|
326
|
+
let tmp;
|
|
342
327
|
tmp = multichain === "ordered" ? ChainMap.size(chainsBeforeBatch) === 1 : true;
|
|
343
328
|
if (tmp) {
|
|
344
329
|
return prepareUnorderedBatch(checkpointIdBeforeBatch, chainsBeforeBatch, batchSizeTarget);
|
|
@@ -348,11 +333,11 @@ function make(checkpointIdBeforeBatch, chainsBeforeBatch, multichain, batchSizeT
|
|
|
348
333
|
}
|
|
349
334
|
|
|
350
335
|
function findFirstEventBlockNumber(batch, chainId) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
while(idx < checkpointsLength && result === undefined) {
|
|
355
|
-
|
|
336
|
+
let idx = 0;
|
|
337
|
+
let result;
|
|
338
|
+
let checkpointsLength = batch.checkpointIds.length;
|
|
339
|
+
while (idx < checkpointsLength && result === undefined) {
|
|
340
|
+
let checkpointChainId = batch.checkpointChainIds[idx];
|
|
356
341
|
if (checkpointChainId === chainId && batch.checkpointEventsProcessed[idx] > 0) {
|
|
357
342
|
result = batch.checkpointBlockNumbers[idx];
|
|
358
343
|
} else {
|
|
@@ -363,10 +348,10 @@ function findFirstEventBlockNumber(batch, chainId) {
|
|
|
363
348
|
}
|
|
364
349
|
|
|
365
350
|
function findLastEventItem(batch, chainId) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
while(idx >= 0 && result === undefined) {
|
|
369
|
-
|
|
351
|
+
let idx = batch.items.length - 1;
|
|
352
|
+
let result;
|
|
353
|
+
while (idx >= 0 && result === undefined) {
|
|
354
|
+
let item = batch.items[idx];
|
|
370
355
|
if (item.kind === 0 && item.chain === chainId) {
|
|
371
356
|
result = item;
|
|
372
357
|
} else {
|
|
@@ -377,17 +362,17 @@ function findLastEventItem(batch, chainId) {
|
|
|
377
362
|
}
|
|
378
363
|
|
|
379
364
|
export {
|
|
380
|
-
getOrderedNextChain
|
|
381
|
-
immutableEmptyBatchSizePerChain
|
|
382
|
-
hasOrderedReadyItem
|
|
383
|
-
hasUnorderedReadyItem
|
|
384
|
-
hasMultichainReadyItem
|
|
385
|
-
getProgressedChainsById
|
|
386
|
-
addReorgCheckpoints
|
|
387
|
-
prepareOrderedBatch
|
|
388
|
-
prepareUnorderedBatch
|
|
389
|
-
make
|
|
390
|
-
findFirstEventBlockNumber
|
|
391
|
-
findLastEventItem
|
|
365
|
+
getOrderedNextChain,
|
|
366
|
+
immutableEmptyBatchSizePerChain,
|
|
367
|
+
hasOrderedReadyItem,
|
|
368
|
+
hasUnorderedReadyItem,
|
|
369
|
+
hasMultichainReadyItem,
|
|
370
|
+
getProgressedChainsById,
|
|
371
|
+
addReorgCheckpoints,
|
|
372
|
+
prepareOrderedBatch,
|
|
373
|
+
prepareUnorderedBatch,
|
|
374
|
+
make,
|
|
375
|
+
findFirstEventBlockNumber,
|
|
376
|
+
findLastEventItem,
|
|
392
377
|
}
|
|
393
378
|
/* Utils Not a pure module */
|
package/src/Bin.res
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
@val external processChdir: string => unit = "process.chdir"
|
|
2
|
+
let setEnvVar: (string, string) => unit = %raw(`(k, v) => { process.env[k] = v; }`)
|
|
3
|
+
|
|
4
|
+
// Crash on unhandled promise rejections with a readable error.
|
|
5
|
+
// ReScript exceptions compile to plain objects, not Error instances, so Node.js prints "#<Object>".
|
|
6
|
+
NodeJs.globalProcess->NodeJs.onUnhandledRejection(reason => {
|
|
7
|
+
Logging.errorWithExn(reason->Utils.prettifyExn, "Unhandled promise rejection")
|
|
8
|
+
NodeJs.process->NodeJs.exitWithCode(Failure)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
// Wire format mirrors the Rust `executor::Command` enum — a tagged JSON
|
|
12
|
+
// object with a `kind` discriminator.
|
|
13
|
+
// `migrate` is `Null.t`, not `option`: Rust serde encodes `None` as JSON
|
|
14
|
+
// `null`, but ReScript's `option` expects `undefined`, so a `null` value
|
|
15
|
+
// wrongly passes `Option.map`'s `!== undefined` check. Callers convert via
|
|
16
|
+
// `Null.toOption` at use time.
|
|
17
|
+
type startCmd = {
|
|
18
|
+
migrate: Null.t<Main.migrateOpts>,
|
|
19
|
+
cwd: string,
|
|
20
|
+
env: dict<JSON.t>,
|
|
21
|
+
config: JSON.t,
|
|
22
|
+
}
|
|
23
|
+
type migrateCmd = {reset: bool, persistedState: JSON.t, config: JSON.t}
|
|
24
|
+
type dropSchemaCmd = {config: JSON.t}
|
|
25
|
+
|
|
26
|
+
type command =
|
|
27
|
+
| Start(startCmd)
|
|
28
|
+
| Migrate(migrateCmd)
|
|
29
|
+
| DropSchema(dropSchemaCmd)
|
|
30
|
+
|
|
31
|
+
let decodeCommand = (json: JSON.t): command => {
|
|
32
|
+
let obj = switch json->JSON.Decode.object {
|
|
33
|
+
| Some(o) => o
|
|
34
|
+
| None => JsError.throwWithMessage("Invalid command payload: not an object")
|
|
35
|
+
}
|
|
36
|
+
let kind = switch obj->Dict.get("kind")->Option.flatMap(JSON.Decode.string) {
|
|
37
|
+
| Some(k) => k
|
|
38
|
+
| None => JsError.throwWithMessage("Invalid command payload: missing kind")
|
|
39
|
+
}
|
|
40
|
+
switch kind {
|
|
41
|
+
| "start" => Start(json->(Utils.magic: JSON.t => startCmd))
|
|
42
|
+
| "migrate" => Migrate(json->(Utils.magic: JSON.t => migrateCmd))
|
|
43
|
+
| "drop-schema" => DropSchema(json->(Utils.magic: JSON.t => dropSchemaCmd))
|
|
44
|
+
| other => JsError.throwWithMessage(`Unknown command kind: ${other}`)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let applyEnv = (env: dict<JSON.t>) =>
|
|
49
|
+
env->Dict.forEachWithKey((value, key) => {
|
|
50
|
+
switch value->JSON.Decode.string {
|
|
51
|
+
| Some(v) => setEnvVar(key, v)
|
|
52
|
+
| None => ()
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
let run = async args => {
|
|
57
|
+
try {
|
|
58
|
+
switch (await Core.runCli(args))->Null.toOption {
|
|
59
|
+
// Rust-only command (codegen / init / stop / docker / help / version /
|
|
60
|
+
// scripts) — nothing for JS to do, exit cleanly.
|
|
61
|
+
| None => ()
|
|
62
|
+
| Some(json) =>
|
|
63
|
+
switch decodeCommand(json->JSON.parseOrThrow) {
|
|
64
|
+
| Start({migrate, cwd, env, config}) =>
|
|
65
|
+
Config.prime(config)
|
|
66
|
+
processChdir(cwd)
|
|
67
|
+
applyEnv(env)
|
|
68
|
+
await Main.start(~migrate=?migrate->Null.toOption)
|
|
69
|
+
| Migrate({reset, persistedState, config}) =>
|
|
70
|
+
Config.prime(config)
|
|
71
|
+
await Main.migrate(~reset, ~persistedState)
|
|
72
|
+
| DropSchema({config}) =>
|
|
73
|
+
Config.prime(config)
|
|
74
|
+
await Main.dropSchema()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
| exn =>
|
|
79
|
+
// Log just the exception's own message — wrapping it in "Failed at
|
|
80
|
+
// initialization" and pino's err serializer buries the real cause under
|
|
81
|
+
// a nested `err: { type, message, stack, ... }` block.
|
|
82
|
+
let message = switch exn->JsExn.anyToExnInternal {
|
|
83
|
+
| JsExn(e) => e->JsExn.message->Option.getOr("Failed at initialization")
|
|
84
|
+
| _ => "Failed at initialization"
|
|
85
|
+
}
|
|
86
|
+
Logging.error(message)
|
|
87
|
+
NodeJs.process->NodeJs.exitWithCode(Failure)
|
|
88
|
+
}
|
|
89
|
+
}
|