Skip to content

Commit 6408d01

Browse files
committed
Break everything
1 parent 759a36a commit 6408d01

File tree

18 files changed

+131
-312
lines changed

18 files changed

+131
-312
lines changed

codegenerator/cli/npm/envio/src/InternalConfig.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type chain = {
3030
id: int,
3131
startBlock: int,
3232
endBlock?: int,
33-
confirmedBlockThreshold: int,
33+
maxReorgDepth: int,
3434
contracts: array<contract>,
3535
sources: array<Source.t>,
3636
}

codegenerator/cli/npm/envio/src/PgStorage.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ let makeInitializeTransaction = (
6565
let generalTables = [
6666
InternalTable.Chains.table,
6767
InternalTable.PersistedState.table,
68-
InternalTable.Blocks.table,
68+
InternalTable.ReorgCheckpoints.table,
6969
InternalTable.RawEvents.table,
7070
]
7171

codegenerator/cli/npm/envio/src/ReorgDetection.res

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,10 @@ type validBlockResult = result<blockDataWithTimestamp, validBlockError>
4343
module LastBlockScannedHashes: {
4444
type t
4545
/**Instantiat t with existing data*/
46-
let makeWithData: (
47-
array<blockData>,
48-
~confirmedBlockThreshold: int,
49-
~detectedReorgBlock: blockData=?,
50-
) => t
46+
let makeWithData: (array<blockData>, ~maxReorgDepth: int, ~detectedReorgBlock: blockData=?) => t
5147

5248
/**Instantiat empty t with no block data*/
53-
let empty: (~confirmedBlockThreshold: int) => t
49+
let empty: (~maxReorgDepth: int) => t
5450

5551
/** Registers a new reorg guard, prunes unneeded data, and returns the updated state.
5652
* Resets internal state if shouldRollbackOnReorg is false (detect-only mode)
@@ -82,7 +78,7 @@ module LastBlockScannedHashes: {
8278
// as a threshold for reorgs. If for eg. this is 200,
8379
// it means we are accounting for reorgs up to 200 blocks
8480
// behind the head
85-
confirmedBlockThreshold: int,
81+
maxReorgDepth: int,
8682
// A hash map of recent blockdata by block number to make comparison checks
8783
// for reorgs.
8884
dataByBlockNumber: dict<blockData>,
@@ -93,33 +89,33 @@ module LastBlockScannedHashes: {
9389
detectedReorgBlock: option<blockData>,
9490
}
9591

96-
let makeWithData = (blocks, ~confirmedBlockThreshold, ~detectedReorgBlock=?) => {
92+
let makeWithData = (blocks, ~maxReorgDepth, ~detectedReorgBlock=?) => {
9793
let dataByBlockNumber = Js.Dict.empty()
9894

9995
blocks->Belt.Array.forEach(block => {
10096
dataByBlockNumber->Js.Dict.set(block.blockNumber->Js.Int.toString, block)
10197
})
10298

10399
{
104-
confirmedBlockThreshold,
100+
maxReorgDepth,
105101
dataByBlockNumber,
106102
detectedReorgBlock,
107103
}
108104
}
109105
//Instantiates empty LastBlockHashes
110-
let empty = (~confirmedBlockThreshold) => {
111-
confirmedBlockThreshold,
106+
let empty = (~maxReorgDepth) => {
107+
maxReorgDepth,
112108
dataByBlockNumber: Js.Dict.empty(),
113109
detectedReorgBlock: None,
114110
}
115111

116112
let getDataByBlockNumberCopyInThreshold = (
117-
{dataByBlockNumber, confirmedBlockThreshold}: t,
113+
{dataByBlockNumber, maxReorgDepth}: t,
118114
~currentBlockHeight,
119115
) => {
120116
// Js engine automatically orders numeric object keys
121117
let ascBlockNumberKeys = dataByBlockNumber->Js.Dict.keys
122-
let thresholdBlockNumber = currentBlockHeight - confirmedBlockThreshold
118+
let thresholdBlockNumber = currentBlockHeight - maxReorgDepth
123119

124120
let copy = Js.Dict.empty()
125121

@@ -136,7 +132,7 @@ module LastBlockScannedHashes: {
136132
}
137133

138134
let registerReorgGuard = (
139-
{confirmedBlockThreshold} as self: t,
135+
{maxReorgDepth} as self: t,
140136
~reorgGuard: reorgGuard,
141137
~currentBlockHeight,
142138
~shouldRollbackOnReorg,
@@ -180,7 +176,7 @@ module LastBlockScannedHashes: {
180176
...self,
181177
detectedReorgBlock: Some(reorgDetected.scannedBlock),
182178
}
183-
: empty(~confirmedBlockThreshold),
179+
: empty(~maxReorgDepth),
184180
ReorgDetected(reorgDetected),
185181
)
186182
| None => {
@@ -199,7 +195,7 @@ module LastBlockScannedHashes: {
199195

200196
(
201197
{
202-
confirmedBlockThreshold,
198+
maxReorgDepth,
203199
dataByBlockNumber: dataByBlockNumberCopyInThreshold,
204200
detectedReorgBlock: None,
205201
},
@@ -289,10 +285,7 @@ module LastBlockScannedHashes: {
289285
Return a BlockNumbersAndHashes.t rolled back to where blockData is less
290286
than the provided blockNumber
291287
*/
292-
let rollbackToValidBlockNumber = (
293-
{dataByBlockNumber, confirmedBlockThreshold}: t,
294-
~blockNumber: int,
295-
) => {
288+
let rollbackToValidBlockNumber = ({dataByBlockNumber, maxReorgDepth}: t, ~blockNumber: int) => {
296289
// Js engine automatically orders numeric object keys
297290
let ascBlockNumberKeys = dataByBlockNumber->Js.Dict.keys
298291

@@ -316,7 +309,7 @@ module LastBlockScannedHashes: {
316309
loop(0)
317310

318311
{
319-
confirmedBlockThreshold,
312+
maxReorgDepth,
320313
dataByBlockNumber: newDataByBlockNumber,
321314
detectedReorgBlock: None,
322315
}

codegenerator/cli/npm/envio/src/db/InternalTable.res

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Chains = {
1616
| #id
1717
| #start_block
1818
| #end_block
19+
| #max_reorg_depth
1920
| #source_block
2021
| #first_event_block
2122
| #buffer_block
@@ -28,6 +29,7 @@ module Chains = {
2829
#id,
2930
#start_block,
3031
#end_block,
32+
#max_reorg_depth,
3133
#source_block,
3234
#first_event_block,
3335
#buffer_block,
@@ -52,6 +54,7 @@ module Chains = {
5254
@as("id") id: int,
5355
@as("start_block") startBlock: int,
5456
@as("end_block") endBlock: Js.null<int>,
57+
@as("max_reorg_depth") maxReorgDepth: int,
5558
@as("progress_block") progressBlockNumber: int,
5659
@as("events_processed") numEventsProcessed: int,
5760
...metaFields,
@@ -64,6 +67,7 @@ module Chains = {
6467
// Values populated from config
6568
mkField((#start_block: field :> string), Integer, ~fieldSchema=S.int),
6669
mkField((#end_block: field :> string), Integer, ~fieldSchema=S.null(S.int), ~isNullable),
70+
mkField((#max_reorg_depth: field :> string), Integer, ~fieldSchema=S.int),
6771
// Block number of the latest block that was fetched from the source
6872
mkField((#buffer_block: field :> string), Integer, ~fieldSchema=S.int),
6973
// Block number of the currently active source
@@ -98,6 +102,7 @@ module Chains = {
98102
id: chainConfig.id,
99103
startBlock: chainConfig.startBlock,
100104
endBlock: chainConfig.endBlock->Js.Null.fromOption,
105+
maxReorgDepth: chainConfig.maxReorgDepth,
101106
blockHeight: 0,
102107
firstEventBlockNumber: Js.Null.empty,
103108
latestFetchedBlockNumber: -1,
@@ -254,22 +259,24 @@ module PersistedState = {
254259
)
255260
}
256261

257-
module Blocks = {
262+
module ReorgCheckpoints = {
258263
type t = {
259264
id: bigint,
260265
@as("chain_id")
261266
chainId: int,
262-
number: int,
263-
hash: string,
267+
@as("block_number")
268+
blockNumber: int,
269+
@as("block_hash")
270+
blockHash: string,
264271
}
265272

266273
let table = mkTable(
267-
"envio_blocks",
274+
"envio_reorg_checkpoints",
268275
~fields=[
269276
mkField("id", Numeric, ~fieldSchema=S.bigint, ~isPrimaryKey),
270277
mkField("chain_id", Integer, ~fieldSchema=S.int),
271-
mkField("number", Integer, ~fieldSchema=S.int),
272-
mkField("hash", Text, ~fieldSchema=S.string),
278+
mkField("block_number", Integer, ~fieldSchema=S.int),
279+
mkField("block_hash", Text, ~fieldSchema=S.string),
273280
],
274281
)
275282
}

codegenerator/cli/templates/dynamic/codegen/src/ConfigYAML.res.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type contract = {
2525
type configYaml = {
2626
syncSource,
2727
startBlock: int,
28-
confirmedBlockThreshold: int,
28+
maxReorgDepth: int,
2929
contracts: dict<contract>,
3030
lowercaseAddresses: bool,
3131
}
@@ -58,7 +58,7 @@ let publicConfig = ChainMap.fromArrayUnsafe([
5858
(
5959
chain,
6060
{
61-
confirmedBlockThreshold: {{chain_config.network_config.confirmed_block_threshold}},
61+
maxReorgDepth: {{chain_config.network_config.confirmed_block_threshold}},
6262
syncSource: {{chain_config.deprecated_sync_source_code}},
6363
startBlock: {{chain_config.network_config.start_block}},
6464
contracts,

codegenerator/cli/templates/dynamic/codegen/src/RegisterHandlers.res.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let registerContractHandlers = (
5656
]
5757
let chain = ChainMap.Chain.makeUnsafe(~chainId={{chain_config.network_config.id}})
5858
{
59-
InternalConfig.confirmedBlockThreshold: {{chain_config.network_config.confirmed_block_threshold}},
59+
InternalConfig.maxReorgDepth: {{chain_config.network_config.confirmed_block_threshold}},
6060
startBlock: {{chain_config.network_config.start_block}},
6161
{{#if chain_config.network_config.end_block}}
6262
endBlock: {{chain_config.network_config.end_block}},

codegenerator/cli/templates/static/codegen/src/IO.res

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,20 @@ let executeBatch = async (
224224
| Some(eventIdentifier) =>
225225
Some(
226226
sql =>
227-
Promise.all2((
227+
Promise.all([
228228
sql->DbFunctions.EntityHistory.deleteAllEntityHistoryAfterEventIdentifier(
229229
~isUnorderedMultichainMode=switch config.multichain {
230230
| Unordered => true
231231
| Ordered => false
232232
},
233233
~eventIdentifier,
234234
),
235-
sql->DbFunctions.EndOfBlockRangeScannedData.rollbackEndOfBlockRangeScannedDataForChain(
236-
~chainId=eventIdentifier.chainId,
237-
~knownBlockNumber=eventIdentifier.blockNumber,
238-
),
239-
)),
235+
// FIXME: Add rollback for blocks table
236+
// sql->DbFunctions.EndOfBlockRangeScannedData.rollbackEndOfBlockRangeScannedDataForChain(
237+
// ~chainId=eventIdentifier.chainId,
238+
// ~knownBlockNumber=eventIdentifier.blockNumber,
239+
// ),
240+
]),
240241
)
241242
| None => None
242243
}

codegenerator/cli/templates/static/codegen/src/db/DbFunctions.res

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,6 @@ module General = {
1313
}
1414
}
1515

16-
module EndOfBlockRangeScannedData = {
17-
type endOfBlockRangeScannedData = {
18-
@as("chain_id") chainId: int,
19-
@as("block_number") blockNumber: int,
20-
@as("block_hash") blockHash: string,
21-
}
22-
23-
@module("./DbFunctionsImplementation.js")
24-
external batchSet: (Postgres.sql, array<endOfBlockRangeScannedData>) => promise<unit> =
25-
"batchSetEndOfBlockRangeScannedData"
26-
27-
let setEndOfBlockRangeScannedData = (sql, endOfBlockRangeScannedData) =>
28-
batchSet(sql, [endOfBlockRangeScannedData])
29-
30-
@module("./DbFunctionsImplementation.js")
31-
external readEndOfBlockRangeScannedDataForChain: (
32-
Postgres.sql,
33-
~chainId: int,
34-
) => promise<array<endOfBlockRangeScannedData>> = "readEndOfBlockRangeScannedDataForChain"
35-
36-
@module("./DbFunctionsImplementation.js")
37-
external deleteStaleEndOfBlockRangeScannedDataForChain: (
38-
Postgres.sql,
39-
~chainId: int,
40-
//minimum blockNumber that should be kept in db
41-
~blockNumberThreshold: int,
42-
) => promise<unit> = "deleteStaleEndOfBlockRangeScannedDataForChain"
43-
44-
@module("./DbFunctionsImplementation.js")
45-
external rollbackEndOfBlockRangeScannedDataForChain: (
46-
Postgres.sql,
47-
~chainId: int,
48-
//The known block number we are rollbacking to
49-
~knownBlockNumber: int,
50-
) => promise<unit> = "rollbackEndOfBlockRangeScannedDataForChain"
51-
}
52-
5316
module DynamicContractRegistry = {
5417
@module("./DbFunctionsImplementation.js")
5518
external readAllDynamicContractsRaw: (Postgres.sql, ~chainId: chainId) => promise<Js.Json.t> =

codegenerator/cli/templates/static/codegen/src/db/DbFunctionsImplementation.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,6 @@ module.exports.batchDeleteItemsInTable = (table, sql, pkArray) => {
3131
}
3232
};
3333

34-
const batchSetEndOfBlockRangeScannedDataCore = (sql, rowDataArray) => {
35-
return sql`
36-
INSERT INTO ${sql(publicSchema)}."end_of_block_range_scanned_data"
37-
${sql(rowDataArray, "chain_id", "block_number", "block_hash")}
38-
ON CONFLICT(chain_id, block_number) DO UPDATE
39-
SET
40-
"chain_id" = EXCLUDED."chain_id",
41-
"block_number" = EXCLUDED."block_number",
42-
"block_hash" = EXCLUDED."block_hash";`;
43-
};
44-
45-
module.exports.batchSetEndOfBlockRangeScannedData = chunkBatchQuery(
46-
batchSetEndOfBlockRangeScannedDataCore
47-
);
48-
49-
module.exports.readEndOfBlockRangeScannedDataForChain = (sql, chainId) => {
50-
return sql`
51-
SELECT * FROM ${sql(publicSchema)}."end_of_block_range_scanned_data"
52-
WHERE
53-
chain_id = ${chainId}
54-
ORDER BY block_number ASC;`;
55-
};
56-
57-
module.exports.deleteStaleEndOfBlockRangeScannedDataForChain = (
58-
sql,
59-
chainId,
60-
blockNumberThreshold
61-
) => {
62-
return sql`
63-
DELETE
64-
FROM ${sql(publicSchema)}."end_of_block_range_scanned_data"
65-
WHERE chain_id = ${chainId}
66-
AND block_number < ${blockNumberThreshold};`;
67-
};
68-
69-
module.exports.rollbackEndOfBlockRangeScannedDataForChain = (
70-
sql,
71-
chainId,
72-
knownBlockNumber
73-
) => {
74-
return sql`
75-
DELETE
76-
FROM ${sql(publicSchema)}."end_of_block_range_scanned_data"
77-
WHERE chain_id = ${chainId}
78-
AND block_number > ${knownBlockNumber};`;
79-
};
80-
8134
module.exports.readAllDynamicContracts = (sql, chainId) => sql`
8235
SELECT *
8336
FROM ${sql(publicSchema)}."dynamic_contract_registry"

0 commit comments

Comments
 (0)