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/ReorgDetection.res
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
open Belt
|
|
2
|
-
|
|
3
1
|
type blockDataWithTimestamp = {
|
|
4
2
|
blockHash: string,
|
|
5
3
|
blockNumber: int,
|
|
@@ -58,7 +56,7 @@ let make = (
|
|
|
58
56
|
~maxReorgDepth,
|
|
59
57
|
~shouldRollbackOnReorg,
|
|
60
58
|
) => {
|
|
61
|
-
let dataByBlockNumber =
|
|
59
|
+
let dataByBlockNumber = Dict.make()
|
|
62
60
|
|
|
63
61
|
chainReorgCheckpoints->Belt.Array.forEach(block => {
|
|
64
62
|
dataByBlockNumber->Utils.Dict.setByInt(
|
|
@@ -79,17 +77,17 @@ let make = (
|
|
|
79
77
|
|
|
80
78
|
let getDataByBlockNumberCopyInThreshold = ({dataByBlockNumber, maxReorgDepth}: t, ~knownHeight) => {
|
|
81
79
|
// Js engine automatically orders numeric object keys
|
|
82
|
-
let ascBlockNumberKeys = dataByBlockNumber->
|
|
80
|
+
let ascBlockNumberKeys = dataByBlockNumber->Dict.keysToArray
|
|
83
81
|
let thresholdBlockNumber = knownHeight - maxReorgDepth
|
|
84
82
|
|
|
85
|
-
let copy =
|
|
83
|
+
let copy = Dict.make()
|
|
86
84
|
|
|
87
85
|
for idx in 0 to ascBlockNumberKeys->Array.length - 1 {
|
|
88
|
-
let blockNumberKey = ascBlockNumberKeys->
|
|
89
|
-
let scannedBlock = dataByBlockNumber->
|
|
86
|
+
let blockNumberKey = ascBlockNumberKeys->Array.getUnsafe(idx)
|
|
87
|
+
let scannedBlock = dataByBlockNumber->Dict.getUnsafe(blockNumberKey)
|
|
90
88
|
let isInReorgThreshold = scannedBlock.blockNumber >= thresholdBlockNumber
|
|
91
89
|
if isInReorgThreshold {
|
|
92
|
-
copy->
|
|
90
|
+
copy->Dict.set(blockNumberKey, scannedBlock)
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
93
|
|
|
@@ -143,14 +141,14 @@ let registerReorgGuard = (
|
|
|
143
141
|
ReorgDetected(reorgDetected),
|
|
144
142
|
)
|
|
145
143
|
| None => {
|
|
146
|
-
dataByBlockNumberCopyInThreshold->
|
|
144
|
+
dataByBlockNumberCopyInThreshold->Dict.set(
|
|
147
145
|
rangeLastBlock.blockNumber->Int.toString,
|
|
148
146
|
rangeLastBlock,
|
|
149
147
|
)
|
|
150
148
|
switch prevRangeLastBlock {
|
|
151
149
|
| None => ()
|
|
152
150
|
| Some(prevRangeLastBlock) =>
|
|
153
|
-
dataByBlockNumberCopyInThreshold->
|
|
151
|
+
dataByBlockNumberCopyInThreshold->Dict.set(
|
|
154
152
|
prevRangeLastBlock.blockNumber->Int.toString,
|
|
155
153
|
prevRangeLastBlock,
|
|
156
154
|
)
|
|
@@ -176,19 +174,19 @@ let getLatestValidScannedBlock = (
|
|
|
176
174
|
reorgDetection: t,
|
|
177
175
|
~blockNumbersAndHashes: array<blockDataWithTimestamp>,
|
|
178
176
|
) => {
|
|
179
|
-
let verifiedDataByBlockNumber =
|
|
177
|
+
let verifiedDataByBlockNumber = Dict.make()
|
|
180
178
|
for idx in 0 to blockNumbersAndHashes->Array.length - 1 {
|
|
181
179
|
let blockData = blockNumbersAndHashes->Array.getUnsafe(idx)
|
|
182
|
-
verifiedDataByBlockNumber->
|
|
180
|
+
verifiedDataByBlockNumber->Dict.set(blockData.blockNumber->Int.toString, blockData)
|
|
183
181
|
}
|
|
184
182
|
// Js engine automatically orders numeric object keys
|
|
185
|
-
let ascBlockNumberKeys = verifiedDataByBlockNumber->
|
|
183
|
+
let ascBlockNumberKeys = verifiedDataByBlockNumber->Dict.keysToArray
|
|
186
184
|
|
|
187
185
|
let getPrevScannedBlockNumber = idx =>
|
|
188
186
|
ascBlockNumberKeys
|
|
189
187
|
->Belt.Array.get(idx - 1)
|
|
190
188
|
->Option.map(key => {
|
|
191
|
-
(verifiedDataByBlockNumber->
|
|
189
|
+
(verifiedDataByBlockNumber->Dict.getUnsafe(key)).blockNumber
|
|
192
190
|
})
|
|
193
191
|
|
|
194
192
|
let rec loop = idx => {
|
|
@@ -196,7 +194,7 @@ let getLatestValidScannedBlock = (
|
|
|
196
194
|
| Some(blockNumberKey) =>
|
|
197
195
|
switch reorgDetection.dataByBlockNumber->Utils.Dict.dangerouslyGetNonOption(blockNumberKey) {
|
|
198
196
|
| Some(scannedBlock)
|
|
199
|
-
if (verifiedDataByBlockNumber->
|
|
197
|
+
if (verifiedDataByBlockNumber->Dict.getUnsafe(blockNumberKey)).blockHash ===
|
|
200
198
|
scannedBlock.blockHash =>
|
|
201
199
|
loop(idx + 1)
|
|
202
200
|
| _ => getPrevScannedBlockNumber(idx)
|
|
@@ -216,17 +214,17 @@ let rollbackToValidBlockNumber = (
|
|
|
216
214
|
~blockNumber: int,
|
|
217
215
|
) => {
|
|
218
216
|
// Js engine automatically orders numeric object keys
|
|
219
|
-
let ascBlockNumberKeys = dataByBlockNumber->
|
|
217
|
+
let ascBlockNumberKeys = dataByBlockNumber->Dict.keysToArray
|
|
220
218
|
|
|
221
|
-
let newDataByBlockNumber =
|
|
219
|
+
let newDataByBlockNumber = Dict.make()
|
|
222
220
|
|
|
223
221
|
let rec loop = idx => {
|
|
224
222
|
switch ascBlockNumberKeys->Belt.Array.get(idx) {
|
|
225
223
|
| Some(blockNumberKey) => {
|
|
226
|
-
let scannedBlock = dataByBlockNumber->
|
|
224
|
+
let scannedBlock = dataByBlockNumber->Dict.getUnsafe(blockNumberKey)
|
|
227
225
|
let shouldKeep = scannedBlock.blockNumber <= blockNumber
|
|
228
226
|
if shouldKeep {
|
|
229
|
-
newDataByBlockNumber->
|
|
227
|
+
newDataByBlockNumber->Dict.set(blockNumberKey, scannedBlock)
|
|
230
228
|
loop(idx + 1)
|
|
231
229
|
} else {
|
|
232
230
|
()
|
|
@@ -248,12 +246,12 @@ let getThresholdBlockNumbersBelowBlock = (self: t, ~blockNumber: int, ~knownHeig
|
|
|
248
246
|
let arr = []
|
|
249
247
|
|
|
250
248
|
// Js engine automatically orders numeric object keys
|
|
251
|
-
let ascBlockNumberKeys = self.dataByBlockNumber->
|
|
249
|
+
let ascBlockNumberKeys = self.dataByBlockNumber->Dict.keysToArray
|
|
252
250
|
let thresholdBlockNumber = knownHeight - self.maxReorgDepth
|
|
253
251
|
|
|
254
252
|
for idx in 0 to ascBlockNumberKeys->Array.length - 1 {
|
|
255
|
-
let blockNumberKey = ascBlockNumberKeys->
|
|
256
|
-
let scannedBlock = self.dataByBlockNumber->
|
|
253
|
+
let blockNumberKey = ascBlockNumberKeys->Array.getUnsafe(idx)
|
|
254
|
+
let scannedBlock = self.dataByBlockNumber->Dict.getUnsafe(blockNumberKey)
|
|
257
255
|
let isInReorgThreshold = scannedBlock.blockNumber >= thresholdBlockNumber
|
|
258
256
|
if isInReorgThreshold && scannedBlock.blockNumber < blockNumber {
|
|
259
257
|
arr->Array.push(scannedBlock.blockNumber)
|
|
@@ -264,7 +262,7 @@ let getThresholdBlockNumbersBelowBlock = (self: t, ~blockNumber: int, ~knownHeig
|
|
|
264
262
|
|
|
265
263
|
let getHashByBlockNumber = (reorgDetection: t, ~blockNumber) => {
|
|
266
264
|
switch reorgDetection.dataByBlockNumber->Utils.Dict.dangerouslyGetByIntNonOption(blockNumber) {
|
|
267
|
-
| Some(v) =>
|
|
268
|
-
| None =>
|
|
265
|
+
| Some(v) => Null.Value(v.blockHash)
|
|
266
|
+
| None => Null.Null
|
|
269
267
|
}
|
|
270
268
|
}
|
|
@@ -1,61 +1,60 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
|
-
import * as Belt_Array from "rescript/lib/es6/
|
|
4
|
-
import * as
|
|
3
|
+
import * as Belt_Array from "@rescript/runtime/lib/es6/Belt_Array.js";
|
|
4
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
5
5
|
|
|
6
6
|
function reorgDetectedToLogParams(reorgDetected, shouldRollbackOnReorg) {
|
|
7
|
-
|
|
7
|
+
let scannedBlock = reorgDetected.scannedBlock;
|
|
8
8
|
return {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
msg: `Blockchain reorg detected. ` + (
|
|
10
|
+
shouldRollbackOnReorg ? "Initiating indexer rollback" : "NOT initiating indexer rollback due to configuration"
|
|
11
|
+
) + `.`,
|
|
12
|
+
blockNumber: scannedBlock.blockNumber,
|
|
13
|
+
indexedBlockHash: scannedBlock.blockHash,
|
|
14
|
+
receivedBlockHash: reorgDetected.receivedBlock.blockHash
|
|
15
|
+
};
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
function make(chainReorgCheckpoints, maxReorgDepth, shouldRollbackOnReorg) {
|
|
19
|
-
|
|
20
|
-
Belt_Array.forEach(chainReorgCheckpoints,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
let dataByBlockNumber = {};
|
|
20
|
+
Belt_Array.forEach(chainReorgCheckpoints, block => {
|
|
21
|
+
dataByBlockNumber[block.block_number] = {
|
|
22
|
+
blockHash: block.block_hash,
|
|
23
|
+
blockNumber: block.block_number
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
26
|
return {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
shouldRollbackOnReorg: shouldRollbackOnReorg,
|
|
28
|
+
maxReorgDepth: maxReorgDepth,
|
|
29
|
+
dataByBlockNumber: dataByBlockNumber
|
|
30
|
+
};
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function getDataByBlockNumberCopyInThreshold(param, knownHeight) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
for(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
let dataByBlockNumber = param.dataByBlockNumber;
|
|
35
|
+
let ascBlockNumberKeys = Object.keys(dataByBlockNumber);
|
|
36
|
+
let thresholdBlockNumber = knownHeight - param.maxReorgDepth | 0;
|
|
37
|
+
let copy = {};
|
|
38
|
+
for (let idx = 0, idx_finish = ascBlockNumberKeys.length; idx < idx_finish; ++idx) {
|
|
39
|
+
let blockNumberKey = ascBlockNumberKeys[idx];
|
|
40
|
+
let scannedBlock = dataByBlockNumber[blockNumberKey];
|
|
41
|
+
let isInReorgThreshold = scannedBlock.blockNumber >= thresholdBlockNumber;
|
|
42
42
|
if (isInReorgThreshold) {
|
|
43
43
|
copy[blockNumberKey] = scannedBlock;
|
|
44
44
|
}
|
|
45
|
-
|
|
46
45
|
}
|
|
47
46
|
return copy;
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
function registerReorgGuard(self, reorgGuard, knownHeight) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
let maxReorgDepth = self.maxReorgDepth;
|
|
51
|
+
let shouldRollbackOnReorg = self.shouldRollbackOnReorg;
|
|
52
|
+
let dataByBlockNumberCopyInThreshold = getDataByBlockNumberCopyInThreshold(self, knownHeight);
|
|
53
|
+
let prevRangeLastBlock = reorgGuard.prevRangeLastBlock;
|
|
54
|
+
let rangeLastBlock = reorgGuard.rangeLastBlock;
|
|
55
|
+
let scannedBlock = dataByBlockNumberCopyInThreshold[rangeLastBlock.blockNumber.toString()];
|
|
56
|
+
let maybeReorgDetected;
|
|
57
|
+
let exit = 0;
|
|
59
58
|
if (scannedBlock !== undefined && scannedBlock.blockHash !== rangeLastBlock.blockHash) {
|
|
60
59
|
maybeReorgDetected = {
|
|
61
60
|
scannedBlock: scannedBlock,
|
|
@@ -66,59 +65,55 @@ function registerReorgGuard(self, reorgGuard, knownHeight) {
|
|
|
66
65
|
}
|
|
67
66
|
if (exit === 1) {
|
|
68
67
|
if (prevRangeLastBlock !== undefined) {
|
|
69
|
-
|
|
68
|
+
let scannedBlock$1 = dataByBlockNumberCopyInThreshold[prevRangeLastBlock.blockNumber.toString()];
|
|
70
69
|
maybeReorgDetected = scannedBlock$1 !== undefined && scannedBlock$1.blockHash !== prevRangeLastBlock.blockHash ? ({
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
scannedBlock: scannedBlock$1,
|
|
71
|
+
receivedBlock: prevRangeLastBlock
|
|
72
|
+
}) : undefined;
|
|
74
73
|
} else {
|
|
75
74
|
maybeReorgDetected = undefined;
|
|
76
75
|
}
|
|
77
76
|
}
|
|
78
77
|
if (maybeReorgDetected !== undefined) {
|
|
79
78
|
return [
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
shouldRollbackOnReorg ? self : make([], maxReorgDepth, shouldRollbackOnReorg),
|
|
80
|
+
{
|
|
81
|
+
TAG: "ReorgDetected",
|
|
82
|
+
_0: maybeReorgDetected
|
|
83
|
+
}
|
|
84
|
+
];
|
|
86
85
|
} else {
|
|
87
|
-
dataByBlockNumberCopyInThreshold[
|
|
86
|
+
dataByBlockNumberCopyInThreshold[rangeLastBlock.blockNumber.toString()] = rangeLastBlock;
|
|
88
87
|
if (prevRangeLastBlock !== undefined) {
|
|
89
|
-
dataByBlockNumberCopyInThreshold[
|
|
88
|
+
dataByBlockNumberCopyInThreshold[prevRangeLastBlock.blockNumber.toString()] = prevRangeLastBlock;
|
|
90
89
|
}
|
|
91
90
|
return [
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
{
|
|
92
|
+
shouldRollbackOnReorg: shouldRollbackOnReorg,
|
|
93
|
+
maxReorgDepth: maxReorgDepth,
|
|
94
|
+
dataByBlockNumber: dataByBlockNumberCopyInThreshold
|
|
95
|
+
},
|
|
96
|
+
"NoReorg"
|
|
97
|
+
];
|
|
99
98
|
}
|
|
100
99
|
}
|
|
101
100
|
|
|
102
101
|
function getLatestValidScannedBlock(reorgDetection, blockNumbersAndHashes) {
|
|
103
|
-
|
|
104
|
-
for(
|
|
105
|
-
|
|
106
|
-
verifiedDataByBlockNumber[
|
|
102
|
+
let verifiedDataByBlockNumber = {};
|
|
103
|
+
for (let idx = 0, idx_finish = blockNumbersAndHashes.length; idx < idx_finish; ++idx) {
|
|
104
|
+
let blockData = blockNumbersAndHashes[idx];
|
|
105
|
+
verifiedDataByBlockNumber[blockData.blockNumber.toString()] = blockData;
|
|
107
106
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
var _idx = 0;
|
|
115
|
-
while(true) {
|
|
116
|
-
var idx$1 = _idx;
|
|
117
|
-
var blockNumberKey = Belt_Array.get(ascBlockNumberKeys, idx$1);
|
|
107
|
+
let ascBlockNumberKeys = Object.keys(verifiedDataByBlockNumber);
|
|
108
|
+
let getPrevScannedBlockNumber = idx => Stdlib_Option.map(Belt_Array.get(ascBlockNumberKeys, idx - 1 | 0), key => verifiedDataByBlockNumber[key].blockNumber);
|
|
109
|
+
let _idx = 0;
|
|
110
|
+
while (true) {
|
|
111
|
+
let idx$1 = _idx;
|
|
112
|
+
let blockNumberKey = Belt_Array.get(ascBlockNumberKeys, idx$1);
|
|
118
113
|
if (blockNumberKey === undefined) {
|
|
119
114
|
return getPrevScannedBlockNumber(idx$1);
|
|
120
115
|
}
|
|
121
|
-
|
|
116
|
+
let scannedBlock = reorgDetection.dataByBlockNumber[blockNumberKey];
|
|
122
117
|
if (scannedBlock === undefined) {
|
|
123
118
|
return getPrevScannedBlockNumber(idx$1);
|
|
124
119
|
}
|
|
@@ -126,57 +121,56 @@ function getLatestValidScannedBlock(reorgDetection, blockNumbersAndHashes) {
|
|
|
126
121
|
return getPrevScannedBlockNumber(idx$1);
|
|
127
122
|
}
|
|
128
123
|
_idx = idx$1 + 1 | 0;
|
|
129
|
-
continue
|
|
124
|
+
continue;
|
|
130
125
|
};
|
|
131
126
|
}
|
|
132
127
|
|
|
133
128
|
function rollbackToValidBlockNumber(param, blockNumber) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
while(true) {
|
|
139
|
-
|
|
140
|
-
|
|
129
|
+
let dataByBlockNumber = param.dataByBlockNumber;
|
|
130
|
+
let ascBlockNumberKeys = Object.keys(dataByBlockNumber);
|
|
131
|
+
let newDataByBlockNumber = {};
|
|
132
|
+
let loop = _idx => {
|
|
133
|
+
while (true) {
|
|
134
|
+
let idx = _idx;
|
|
135
|
+
let blockNumberKey = Belt_Array.get(ascBlockNumberKeys, idx);
|
|
141
136
|
if (blockNumberKey === undefined) {
|
|
142
|
-
return
|
|
137
|
+
return;
|
|
143
138
|
}
|
|
144
|
-
|
|
145
|
-
|
|
139
|
+
let scannedBlock = dataByBlockNumber[blockNumberKey];
|
|
140
|
+
let shouldKeep = scannedBlock.blockNumber <= blockNumber;
|
|
146
141
|
if (!shouldKeep) {
|
|
147
|
-
return
|
|
142
|
+
return;
|
|
148
143
|
}
|
|
149
144
|
newDataByBlockNumber[blockNumberKey] = scannedBlock;
|
|
150
145
|
_idx = idx + 1 | 0;
|
|
151
|
-
continue
|
|
146
|
+
continue;
|
|
152
147
|
};
|
|
153
148
|
};
|
|
154
149
|
loop(0);
|
|
155
150
|
return {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
151
|
+
shouldRollbackOnReorg: param.shouldRollbackOnReorg,
|
|
152
|
+
maxReorgDepth: param.maxReorgDepth,
|
|
153
|
+
dataByBlockNumber: newDataByBlockNumber
|
|
154
|
+
};
|
|
160
155
|
}
|
|
161
156
|
|
|
162
157
|
function getThresholdBlockNumbersBelowBlock(self, blockNumber, knownHeight) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
for(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
158
|
+
let arr = [];
|
|
159
|
+
let ascBlockNumberKeys = Object.keys(self.dataByBlockNumber);
|
|
160
|
+
let thresholdBlockNumber = knownHeight - self.maxReorgDepth | 0;
|
|
161
|
+
for (let idx = 0, idx_finish = ascBlockNumberKeys.length; idx < idx_finish; ++idx) {
|
|
162
|
+
let blockNumberKey = ascBlockNumberKeys[idx];
|
|
163
|
+
let scannedBlock = self.dataByBlockNumber[blockNumberKey];
|
|
164
|
+
let isInReorgThreshold = scannedBlock.blockNumber >= thresholdBlockNumber;
|
|
170
165
|
if (isInReorgThreshold && scannedBlock.blockNumber < blockNumber) {
|
|
171
166
|
arr.push(scannedBlock.blockNumber);
|
|
172
167
|
}
|
|
173
|
-
|
|
174
168
|
}
|
|
175
169
|
return arr;
|
|
176
170
|
}
|
|
177
171
|
|
|
178
172
|
function getHashByBlockNumber(reorgDetection, blockNumber) {
|
|
179
|
-
|
|
173
|
+
let v = reorgDetection.dataByBlockNumber[blockNumber];
|
|
180
174
|
if (v !== undefined) {
|
|
181
175
|
return v.blockHash;
|
|
182
176
|
} else {
|
|
@@ -185,13 +179,13 @@ function getHashByBlockNumber(reorgDetection, blockNumber) {
|
|
|
185
179
|
}
|
|
186
180
|
|
|
187
181
|
export {
|
|
188
|
-
reorgDetectedToLogParams
|
|
189
|
-
make
|
|
190
|
-
getDataByBlockNumberCopyInThreshold
|
|
191
|
-
registerReorgGuard
|
|
192
|
-
getLatestValidScannedBlock
|
|
193
|
-
rollbackToValidBlockNumber
|
|
194
|
-
getThresholdBlockNumbersBelowBlock
|
|
195
|
-
getHashByBlockNumber
|
|
182
|
+
reorgDetectedToLogParams,
|
|
183
|
+
make,
|
|
184
|
+
getDataByBlockNumberCopyInThreshold,
|
|
185
|
+
registerReorgGuard,
|
|
186
|
+
getLatestValidScannedBlock,
|
|
187
|
+
rollbackToValidBlockNumber,
|
|
188
|
+
getThresholdBlockNumbersBelowBlock,
|
|
189
|
+
getHashByBlockNumber,
|
|
196
190
|
}
|
|
197
191
|
/* No side effect */
|
|
@@ -19,7 +19,7 @@ let make = (
|
|
|
19
19
|
let checkpointBlockNumbers = Belt.Array.makeUninitializedUnsafe(
|
|
20
20
|
chainReorgCheckpoints->Array.length,
|
|
21
21
|
)
|
|
22
|
-
chainReorgCheckpoints->
|
|
22
|
+
chainReorgCheckpoints->Array.forEachWithIndex((checkpoint, idx) => {
|
|
23
23
|
checkpointIds->Belt.Array.setUnsafe(idx, checkpoint.checkpointId)
|
|
24
24
|
checkpointBlockNumbers->Belt.Array.setUnsafe(idx, checkpoint.blockNumber)
|
|
25
25
|
})
|
|
@@ -84,9 +84,9 @@ let updateOnNewBatch = (
|
|
|
84
84
|
for idx in 0 to safeCheckpointTracking.checkpointIds->Array.length - 1 {
|
|
85
85
|
let checkpointId = safeCheckpointTracking.checkpointIds->Belt.Array.getUnsafe(idx)
|
|
86
86
|
if checkpointId >= safeCheckpointId {
|
|
87
|
-
mutCheckpointIds->
|
|
87
|
+
mutCheckpointIds->Array.push(checkpointId)->ignore
|
|
88
88
|
mutCheckpointBlockNumbers
|
|
89
|
-
->
|
|
89
|
+
->Array.push(safeCheckpointTracking.checkpointBlockNumbers->Belt.Array.getUnsafe(idx))
|
|
90
90
|
->ignore
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -94,9 +94,9 @@ let updateOnNewBatch = (
|
|
|
94
94
|
// Append new checkpoints
|
|
95
95
|
for idx in 0 to batchCheckpointIds->Array.length - 1 {
|
|
96
96
|
if batchCheckpointChainIds->Belt.Array.getUnsafe(idx) === chainId {
|
|
97
|
-
mutCheckpointIds->
|
|
97
|
+
mutCheckpointIds->Array.push(batchCheckpointIds->Belt.Array.getUnsafe(idx))->ignore
|
|
98
98
|
mutCheckpointBlockNumbers
|
|
99
|
-
->
|
|
99
|
+
->Array.push(batchCheckpointBlockNumbers->Belt.Array.getUnsafe(idx))
|
|
100
100
|
->ignore
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -116,10 +116,10 @@ let rollback = (safeCheckpointTracking: t, ~targetBlockNumber: int) => {
|
|
|
116
116
|
let blockNumber = safeCheckpointTracking.checkpointBlockNumbers->Belt.Array.getUnsafe(idx)
|
|
117
117
|
if blockNumber <= targetBlockNumber {
|
|
118
118
|
mutCheckpointIds
|
|
119
|
-
->
|
|
119
|
+
->Array.push(safeCheckpointTracking.checkpointIds->Belt.Array.getUnsafe(idx))
|
|
120
120
|
->ignore
|
|
121
121
|
mutCheckpointBlockNumbers
|
|
122
|
-
->
|
|
122
|
+
->Array.push(safeCheckpointTracking.checkpointBlockNumbers->Belt.Array.getUnsafe(idx))
|
|
123
123
|
->ignore
|
|
124
124
|
}
|
|
125
125
|
}
|
|
@@ -3,24 +3,24 @@
|
|
|
3
3
|
|
|
4
4
|
function make(maxReorgDepth, shouldRollbackOnReorg, chainReorgCheckpoints) {
|
|
5
5
|
if (!(maxReorgDepth > 0 && shouldRollbackOnReorg)) {
|
|
6
|
-
return
|
|
6
|
+
return;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
chainReorgCheckpoints.forEach(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
let checkpointIds = new Array(chainReorgCheckpoints.length);
|
|
9
|
+
let checkpointBlockNumbers = new Array(chainReorgCheckpoints.length);
|
|
10
|
+
chainReorgCheckpoints.forEach((checkpoint, idx) => {
|
|
11
|
+
checkpointIds[idx] = checkpoint.id;
|
|
12
|
+
checkpointBlockNumbers[idx] = checkpoint.block_number;
|
|
13
|
+
});
|
|
14
14
|
return {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
checkpointIds: checkpointIds,
|
|
16
|
+
checkpointBlockNumbers: checkpointBlockNumbers,
|
|
17
|
+
maxReorgDepth: maxReorgDepth
|
|
18
|
+
};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
function getSafeCheckpointId(safeCheckpointTracking, sourceBlockNumber) {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
let safeBlockNumber = sourceBlockNumber - safeCheckpointTracking.maxReorgDepth | 0;
|
|
23
|
+
let match = safeCheckpointTracking.checkpointIds;
|
|
24
24
|
if (match.length === 0) {
|
|
25
25
|
return 0n;
|
|
26
26
|
}
|
|
@@ -30,16 +30,16 @@ function getSafeCheckpointId(safeCheckpointTracking, sourceBlockNumber) {
|
|
|
30
30
|
if (match.length === 1) {
|
|
31
31
|
return match[0];
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
while(idx < trackingCheckpointsCount && result === undefined) {
|
|
33
|
+
let trackingCheckpointsCount = safeCheckpointTracking.checkpointIds.length;
|
|
34
|
+
let result;
|
|
35
|
+
let idx = 1;
|
|
36
|
+
while (idx < trackingCheckpointsCount && result === undefined) {
|
|
37
37
|
if (safeCheckpointTracking.checkpointBlockNumbers[idx] > safeBlockNumber) {
|
|
38
38
|
result = safeCheckpointTracking.checkpointIds[idx - 1 | 0];
|
|
39
39
|
}
|
|
40
40
|
idx = idx + 1 | 0;
|
|
41
41
|
};
|
|
42
|
-
|
|
42
|
+
let checkpointId = result;
|
|
43
43
|
if (checkpointId !== undefined) {
|
|
44
44
|
return checkpointId;
|
|
45
45
|
} else {
|
|
@@ -48,53 +48,50 @@ function getSafeCheckpointId(safeCheckpointTracking, sourceBlockNumber) {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
function updateOnNewBatch(safeCheckpointTracking, sourceBlockNumber, chainId, batchCheckpointIds, batchCheckpointBlockNumbers, batchCheckpointChainIds) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
for(
|
|
55
|
-
|
|
51
|
+
let safeCheckpointId = getSafeCheckpointId(safeCheckpointTracking, sourceBlockNumber);
|
|
52
|
+
let mutCheckpointIds = [];
|
|
53
|
+
let mutCheckpointBlockNumbers = [];
|
|
54
|
+
for (let idx = 0, idx_finish = safeCheckpointTracking.checkpointIds.length; idx < idx_finish; ++idx) {
|
|
55
|
+
let checkpointId = safeCheckpointTracking.checkpointIds[idx];
|
|
56
56
|
if (checkpointId >= safeCheckpointId) {
|
|
57
57
|
mutCheckpointIds.push(checkpointId);
|
|
58
58
|
mutCheckpointBlockNumbers.push(safeCheckpointTracking.checkpointBlockNumbers[idx]);
|
|
59
59
|
}
|
|
60
|
-
|
|
61
60
|
}
|
|
62
|
-
for(
|
|
61
|
+
for (let idx$1 = 0, idx_finish$1 = batchCheckpointIds.length; idx$1 < idx_finish$1; ++idx$1) {
|
|
63
62
|
if (batchCheckpointChainIds[idx$1] === chainId) {
|
|
64
63
|
mutCheckpointIds.push(batchCheckpointIds[idx$1]);
|
|
65
64
|
mutCheckpointBlockNumbers.push(batchCheckpointBlockNumbers[idx$1]);
|
|
66
65
|
}
|
|
67
|
-
|
|
68
66
|
}
|
|
69
67
|
return {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
68
|
+
checkpointIds: mutCheckpointIds,
|
|
69
|
+
checkpointBlockNumbers: mutCheckpointBlockNumbers,
|
|
70
|
+
maxReorgDepth: safeCheckpointTracking.maxReorgDepth
|
|
71
|
+
};
|
|
74
72
|
}
|
|
75
73
|
|
|
76
74
|
function rollback(safeCheckpointTracking, targetBlockNumber) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
for(
|
|
80
|
-
|
|
75
|
+
let mutCheckpointIds = [];
|
|
76
|
+
let mutCheckpointBlockNumbers = [];
|
|
77
|
+
for (let idx = 0, idx_finish = safeCheckpointTracking.checkpointIds.length; idx < idx_finish; ++idx) {
|
|
78
|
+
let blockNumber = safeCheckpointTracking.checkpointBlockNumbers[idx];
|
|
81
79
|
if (blockNumber <= targetBlockNumber) {
|
|
82
80
|
mutCheckpointIds.push(safeCheckpointTracking.checkpointIds[idx]);
|
|
83
81
|
mutCheckpointBlockNumbers.push(safeCheckpointTracking.checkpointBlockNumbers[idx]);
|
|
84
82
|
}
|
|
85
|
-
|
|
86
83
|
}
|
|
87
84
|
return {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
checkpointIds: mutCheckpointIds,
|
|
86
|
+
checkpointBlockNumbers: mutCheckpointBlockNumbers,
|
|
87
|
+
maxReorgDepth: safeCheckpointTracking.maxReorgDepth
|
|
88
|
+
};
|
|
92
89
|
}
|
|
93
90
|
|
|
94
91
|
export {
|
|
95
|
-
make
|
|
96
|
-
getSafeCheckpointId
|
|
97
|
-
updateOnNewBatch
|
|
98
|
-
rollback
|
|
92
|
+
make,
|
|
93
|
+
getSafeCheckpointId,
|
|
94
|
+
updateOnNewBatch,
|
|
95
|
+
rollback,
|
|
99
96
|
}
|
|
100
97
|
/* No side effect */
|