envio 3.7.0 → 3.9.0-resolvers.0
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 +2 -2
- package/evm.schema.json +7 -0
- package/fuel.schema.json +7 -0
- package/index.d.ts +674 -161
- package/index.js +10 -0
- package/package.json +6 -7
- package/src/AddressRows.res +121 -0
- package/src/AddressRows.res.mjs +143 -0
- package/src/Batch.res +11 -0
- package/src/Batch.res.mjs +2 -1
- package/src/Bin.res +47 -0
- package/src/Bin.res.mjs +34 -1
- package/src/ChainState.res +108 -98
- package/src/ChainState.res.mjs +86 -67
- package/src/ChainState.resi +28 -15
- package/src/Config.res +61 -131
- package/src/Config.res.mjs +47 -79
- package/src/ContractMapping.res +66 -0
- package/src/ContractMapping.res.mjs +73 -0
- package/src/Core.res +24 -0
- package/src/Core.res.mjs +4 -0
- package/src/Ecosystem.res +7 -2
- package/src/Env.res +128 -1
- package/src/Env.res.mjs +123 -1
- package/src/Envio.res +54 -76
- package/src/EventConfigBuilder.res +316 -46
- package/src/EventConfigBuilder.res.mjs +231 -28
- package/src/FetchState.res +59 -67
- package/src/FetchState.res.mjs +34 -44
- package/src/HandlerRegister.res +26 -15
- package/src/HandlerRegister.res.mjs +27 -7
- package/src/Hasura.res +30 -22
- package/src/Hasura.res.mjs +11 -5
- package/src/InMemoryStore.res +23 -38
- package/src/InMemoryStore.res.mjs +13 -27
- package/src/IndexerState.res +40 -3
- package/src/IndexerState.res.mjs +30 -4
- package/src/IndexerState.resi +4 -1
- package/src/Internal.res +49 -30
- package/src/Internal.res.mjs +12 -16
- package/src/Main.res +27 -20
- package/src/Main.res.mjs +19 -14
- package/src/Metrics.res +15 -1
- package/src/Metrics.res.mjs +5 -1
- package/src/Persistence.res +46 -21
- package/src/Persistence.res.mjs +8 -5
- package/src/PgStorage.res +238 -79
- package/src/PgStorage.res.mjs +115 -73
- package/src/PruneStaleHistory.res +1 -1
- package/src/PruneStaleHistory.res.mjs +1 -1
- package/src/Rollback.res +62 -41
- package/src/Rollback.res.mjs +61 -19
- package/src/RollbackCommit.res +7 -9
- package/src/RollbackCommit.res.mjs +5 -6
- package/src/SimulateItems.res +254 -9
- package/src/SimulateItems.res.mjs +214 -47
- package/src/Sink.res +35 -26
- package/src/Sink.res.mjs +20 -16
- package/src/TestIndexer.res +121 -123
- package/src/TestIndexer.res.mjs +97 -102
- package/src/Utils.res +38 -0
- package/src/Utils.res.mjs +39 -2
- package/src/Writing.res +4 -2
- package/src/Writing.res.mjs +3 -2
- package/src/bindings/ClickHouse.res +311 -656
- package/src/bindings/ClickHouse.res.mjs +294 -395
- package/src/bindings/ClickHouseSink.res +303 -0
- package/src/bindings/ClickHouseSink.res.mjs +216 -0
- package/src/bindings/NodeJs.res +9 -0
- package/src/bindings/NodeJs.res.mjs +8 -1
- package/src/bindings/Postgres.res +9 -0
- package/src/bindings/Postgres.res.mjs +3 -0
- package/src/db/EntityHistory.res +29 -24
- package/src/db/EntityHistory.res.mjs +11 -19
- package/src/db/InternalTable.res +258 -83
- package/src/db/InternalTable.res.mjs +223 -85
- package/src/db/RollbackScope.res +29 -0
- package/src/db/RollbackScope.res.mjs +31 -0
- package/src/db/Table.res +28 -1
- package/src/db/Table.res.mjs +26 -2
- package/src/resolvers/ResolverProcess.res +809 -0
- package/src/resolvers/ResolverProcess.res.mjs +667 -0
- package/src/resolvers/ResolverQuery.res +317 -0
- package/src/resolvers/ResolverQuery.res.mjs +220 -0
- package/src/resolvers/collisions.js +108 -0
- package/src/resolvers/db.js +363 -0
- package/src/resolvers/dispatch.js +165 -0
- package/src/resolvers/errors.js +30 -0
- package/src/resolvers/graphqlSelection.js +292 -0
- package/src/resolvers/hasuraAction.js +84 -0
- package/src/resolvers/hasuraApply.js +323 -0
- package/src/resolvers/hasuraMetadata.js +119 -0
- package/src/resolvers/index.js +95 -0
- package/src/resolvers/manifest.js +370 -0
- package/src/resolvers/server.js +270 -0
- package/src/sources/AddressSet.res +0 -22
- package/src/sources/AddressStore.res +48 -88
- package/src/sources/AddressStore.res.mjs +11 -22
- package/src/sources/Evm.res +2 -0
- package/src/sources/Evm.res.mjs +2 -0
- package/src/sources/EvmHyperSyncSource.res +3 -2
- package/src/sources/Fuel.res +2 -0
- package/src/sources/Fuel.res.mjs +2 -0
- package/src/sources/SimulateSource.res +8 -4
- package/src/sources/SimulateSource.res.mjs +7 -3
- package/src/sources/Svm.res +36 -7
- package/src/sources/Svm.res.mjs +34 -4
- package/src/sources/SvmHyperSyncClient.res +20 -116
- package/src/sources/SvmHyperSyncClient.res.mjs +5 -7
- package/src/sources/SvmHyperSyncSource.res +89 -37
- package/src/sources/SvmHyperSyncSource.res.mjs +71 -39
- package/src/sources/TransactionStore.res +38 -0
- package/src/sources/TransactionStore.res.mjs +5 -0
- package/src/tui/Tui.res +1 -1
- package/src/tui/Tui.res.mjs +1 -1
- package/svm.schema.json +44 -156
- package/src/MemoryStorage.res +0 -724
- package/src/MemoryStorage.res.mjs +0 -601
|
@@ -2,17 +2,154 @@
|
|
|
2
2
|
|
|
3
3
|
import * as Table from "./Table.res.mjs";
|
|
4
4
|
import * as Utils from "../Utils.res.mjs";
|
|
5
|
-
import * as Config from "../Config.res.mjs";
|
|
6
5
|
import * as Address from "../Address.res.mjs";
|
|
7
6
|
import * as ChainId from "../ChainId.res.mjs";
|
|
7
|
+
import * as Postgres from "../bindings/Postgres.res.mjs";
|
|
8
|
+
import * as AddressRows from "../AddressRows.res.mjs";
|
|
8
9
|
import * as Stdlib_Null from "@rescript/runtime/lib/es6/Stdlib_Null.js";
|
|
10
|
+
import * as RollbackScope from "./RollbackScope.res.mjs";
|
|
9
11
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
10
12
|
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
|
|
11
13
|
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
|
|
12
14
|
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
13
15
|
|
|
16
|
+
let undefinedTableSqlState = "42P01";
|
|
17
|
+
|
|
18
|
+
function isUndefinedTable(exn) {
|
|
19
|
+
let e = Primitive_exceptions.internalToException(exn);
|
|
20
|
+
if (e.RE_EXN_ID === "JsExn") {
|
|
21
|
+
return e._1.code === undefinedTableSqlState;
|
|
22
|
+
} else {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function chainIdArrayType(pgSchema, chainIdMode) {
|
|
28
|
+
return Table.getPgFieldType("ChainId", pgSchema, true, false, false, chainIdMode);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let table = Table.mkTable("envio_contracts", undefined, [
|
|
32
|
+
Table.mkField("id", "SmallInt", S$RescriptSchema.int, undefined, undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
33
|
+
Table.mkField("name", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)
|
|
34
|
+
], undefined);
|
|
35
|
+
|
|
36
|
+
function makeInsertQuery(pgSchema) {
|
|
37
|
+
return `INSERT INTO "` + pgSchema + `"."` + table.tableName + `" ("id", "name")
|
|
38
|
+
SELECT * FROM unnest($1::` + "SMALLINT" + `[],$2::` + "TEXT" + `[]);`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function insert(sql, pgSchema, contractNames) {
|
|
42
|
+
return sql.unsafe(makeInsertQuery(pgSchema), [
|
|
43
|
+
contractNames.map((param, idx) => idx),
|
|
44
|
+
contractNames
|
|
45
|
+
], {prepare: true});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function read(sql, pgSchema) {
|
|
49
|
+
try {
|
|
50
|
+
let rows = await sql.unsafe(`SELECT "name" FROM "` + pgSchema + `"."` + table.tableName + `" ORDER BY "id";`);
|
|
51
|
+
return rows.map(row => row.name);
|
|
52
|
+
} catch (raw_exn) {
|
|
53
|
+
let exn = Primitive_exceptions.internalToException(raw_exn);
|
|
54
|
+
if (isUndefinedTable(exn)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
throw exn;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let EnvioContracts = {
|
|
62
|
+
table: table,
|
|
63
|
+
makeInsertQuery: makeInsertQuery,
|
|
64
|
+
insert: insert,
|
|
65
|
+
read: read
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
let name = "envio_addresses";
|
|
69
|
+
|
|
70
|
+
let table$1 = Table.mkTable(name, undefined, [
|
|
71
|
+
Table.mkField("chain_id", "ChainId", ChainId.schema, undefined, undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
72
|
+
Table.mkField("address", "Bytea", S$RescriptSchema.string, undefined, undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
73
|
+
Table.mkField("contract_id", "SmallInt", S$RescriptSchema.int, undefined, undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
74
|
+
Table.mkField("registration_block", "Int32", S$RescriptSchema.int, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)
|
|
75
|
+
], undefined);
|
|
76
|
+
|
|
77
|
+
function makeInsertQuery$1(pgSchema, chainIdModeOpt) {
|
|
78
|
+
let chainIdMode = chainIdModeOpt !== undefined ? chainIdModeOpt : "int32";
|
|
79
|
+
let chainIdArrayType$1 = chainIdArrayType(pgSchema, chainIdMode);
|
|
80
|
+
return `INSERT INTO "` + pgSchema + `"."` + table$1.tableName + `" ("chain_id", "address", "contract_id", "registration_block")
|
|
81
|
+
SELECT * FROM unnest($1::` + chainIdArrayType$1 + `,$2::` + "BYTEA" + `[],$3::` + "SMALLINT" + `[],$4::` + "INTEGER" + `[])
|
|
82
|
+
ON CONFLICT ("chain_id", "address", "contract_id") DO NOTHING;`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function insert$1(sql, pgSchema, rows, chainIdModeOpt) {
|
|
86
|
+
let chainIdMode = chainIdModeOpt !== undefined ? chainIdModeOpt : "int32";
|
|
87
|
+
let chainIds = [];
|
|
88
|
+
let addresses = [];
|
|
89
|
+
let contractIds = [];
|
|
90
|
+
let registrationBlocks = [];
|
|
91
|
+
rows.forEach(row => {
|
|
92
|
+
chainIds.push(row.chainId);
|
|
93
|
+
addresses.push(row.address);
|
|
94
|
+
contractIds.push(row.contractId);
|
|
95
|
+
registrationBlocks.push(row.registrationBlock);
|
|
96
|
+
});
|
|
97
|
+
return sql.unsafe(makeInsertQuery$1(pgSchema, chainIdMode), [
|
|
98
|
+
chainIds,
|
|
99
|
+
sql.typed(addresses, Postgres.byteaArrayOid),
|
|
100
|
+
contractIds,
|
|
101
|
+
registrationBlocks
|
|
102
|
+
], {prepare: true});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function makeDeleteQuery(pgSchema, chainIdModeOpt) {
|
|
106
|
+
let chainIdMode = chainIdModeOpt !== undefined ? chainIdModeOpt : "int32";
|
|
107
|
+
let chainIdArrayType$1 = chainIdArrayType(pgSchema, chainIdMode);
|
|
108
|
+
return `DELETE FROM "` + pgSchema + `"."` + table$1.tableName + `"
|
|
109
|
+
USING unnest($1::` + chainIdArrayType$1 + `,$2::` + "BYTEA" + `[],$3::` + "SMALLINT" + `[]) AS dead(chain_id, address, contract_id)
|
|
110
|
+
WHERE "` + table$1.tableName + `"."chain_id" = dead.chain_id
|
|
111
|
+
AND "` + table$1.tableName + `"."address" = dead.address
|
|
112
|
+
AND "` + table$1.tableName + `"."contract_id" = dead.contract_id;`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function $$delete(sql, pgSchema, keys, chainIdModeOpt) {
|
|
116
|
+
let chainIdMode = chainIdModeOpt !== undefined ? chainIdModeOpt : "int32";
|
|
117
|
+
let chainIds = [];
|
|
118
|
+
let addresses = [];
|
|
119
|
+
let contractIds = [];
|
|
120
|
+
keys.forEach(key => {
|
|
121
|
+
chainIds.push(key.chainId);
|
|
122
|
+
addresses.push(key.address);
|
|
123
|
+
contractIds.push(key.contractId);
|
|
124
|
+
});
|
|
125
|
+
return sql.unsafe(makeDeleteQuery(pgSchema, chainIdMode), [
|
|
126
|
+
chainIds,
|
|
127
|
+
sql.typed(addresses, Postgres.byteaArrayOid),
|
|
128
|
+
contractIds
|
|
129
|
+
], {prepare: true});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function makeGetRowsQuery(pgSchema) {
|
|
133
|
+
return `SELECT "chain_id" as "chainId",
|
|
134
|
+
"address" as "address",
|
|
135
|
+
"contract_id" as "contractId",
|
|
136
|
+
"registration_block" as "registrationBlock"
|
|
137
|
+
FROM "` + pgSchema + `"."` + table$1.tableName + `";`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let EnvioAddresses = {
|
|
141
|
+
name: name,
|
|
142
|
+
table: table$1,
|
|
143
|
+
makeInsertQuery: makeInsertQuery$1,
|
|
144
|
+
insert: insert$1,
|
|
145
|
+
makeDeleteQuery: makeDeleteQuery,
|
|
146
|
+
$$delete: $$delete,
|
|
147
|
+
makeGetRowsQuery: makeGetRowsQuery
|
|
148
|
+
};
|
|
149
|
+
|
|
14
150
|
let fields = [
|
|
15
151
|
"id",
|
|
152
|
+
"ecosystem",
|
|
16
153
|
"start_block",
|
|
17
154
|
"end_block",
|
|
18
155
|
"max_reorg_depth",
|
|
@@ -25,8 +162,9 @@ let fields = [
|
|
|
25
162
|
"_is_hyper_sync"
|
|
26
163
|
];
|
|
27
164
|
|
|
28
|
-
let table = Table.mkTable("envio_chains", undefined, [
|
|
165
|
+
let table$2 = Table.mkTable("envio_chains", undefined, [
|
|
29
166
|
Table.mkField("id", "ChainId", ChainId.schema, undefined, undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
167
|
+
Table.mkField("ecosystem", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
30
168
|
Table.mkField("start_block", "Int32", S$RescriptSchema.int, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
31
169
|
Table.mkField("end_block", "Int32", S$RescriptSchema.$$null(S$RescriptSchema.int), undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
32
170
|
Table.mkField("max_reorg_depth", "Int32", S$RescriptSchema.int, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
@@ -42,6 +180,7 @@ let table = Table.mkTable("envio_chains", undefined, [
|
|
|
42
180
|
function initialFromConfig(chainConfig) {
|
|
43
181
|
return {
|
|
44
182
|
id: chainConfig.id,
|
|
183
|
+
ecosystem: chainConfig.ecosystem,
|
|
45
184
|
start_block: chainConfig.startBlock,
|
|
46
185
|
end_block: Stdlib_Null.fromOption(chainConfig.endBlock),
|
|
47
186
|
max_reorg_depth: chainConfig.maxReorgDepth,
|
|
@@ -73,6 +212,8 @@ function makeInitialValuesQuery(pgSchema, chainConfigs) {
|
|
|
73
212
|
} else {
|
|
74
213
|
return "false";
|
|
75
214
|
}
|
|
215
|
+
} else if (match === "string") {
|
|
216
|
+
return `'` + value + `'`;
|
|
76
217
|
} else if (match === "object") {
|
|
77
218
|
return "NULL";
|
|
78
219
|
} else {
|
|
@@ -81,7 +222,7 @@ function makeInitialValuesQuery(pgSchema, chainConfigs) {
|
|
|
81
222
|
});
|
|
82
223
|
return `(` + values.join(", ") + `)`;
|
|
83
224
|
});
|
|
84
|
-
return `INSERT INTO "` + pgSchema + `"."` + table.tableName + `" (` + columnNames.join(", ") + `)
|
|
225
|
+
return `INSERT INTO "` + pgSchema + `"."` + table$2.tableName + `" (` + columnNames.join(", ") + `)
|
|
85
226
|
VALUES ` + valuesRows.join(",\n ") + `;`;
|
|
86
227
|
}
|
|
87
228
|
|
|
@@ -97,13 +238,13 @@ function makeMetaFieldsUpdateQuery(pgSchema) {
|
|
|
97
238
|
let paramIndex = index + 2 | 0;
|
|
98
239
|
return `"` + field + `" = $` + paramIndex.toString();
|
|
99
240
|
});
|
|
100
|
-
return `UPDATE "` + pgSchema + `"."` + table.tableName + `"
|
|
241
|
+
return `UPDATE "` + pgSchema + `"."` + table$2.tableName + `"
|
|
101
242
|
SET ` + setClauses.join(",\n ") + `
|
|
102
243
|
WHERE "` + "id" + `" = $1;`;
|
|
103
244
|
}
|
|
104
245
|
|
|
105
246
|
function makeSetReadyAtQuery(pgSchema) {
|
|
106
|
-
return `UPDATE "` + pgSchema + `"."` + table.tableName + `"
|
|
247
|
+
return `UPDATE "` + pgSchema + `"."` + table$2.tableName + `"
|
|
107
248
|
SET "` + "ready_at" + `" = $1
|
|
108
249
|
WHERE "` + "id" + `" = $2
|
|
109
250
|
AND "` + "ready_at" + `" IS NULL;`;
|
|
@@ -119,40 +260,15 @@ function makeGetInitialStateQuery(pgSchema) {
|
|
|
119
260
|
"` + "events_processed" + `"::float8 as "numEventsProcessed",
|
|
120
261
|
"` + "progress_block" + `" as "progressBlockNumber",
|
|
121
262
|
"` + "source_block" + `" as "sourceBlockNumber"
|
|
122
|
-
FROM "` + pgSchema + `"."` + table.tableName + `";`;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function makeGetIndexingAddressesQuery(pgSchema) {
|
|
126
|
-
return `SELECT "chain_id" as "chainId",
|
|
127
|
-
SUBSTRING("id" FROM POSITION('-' IN "id") + 1) as "address",
|
|
128
|
-
"contract_name" as "contractName",
|
|
129
|
-
"registration_block" as "registrationBlock"
|
|
130
|
-
FROM "` + pgSchema + `"."` + Config.EnvioAddresses.table.tableName + `";`;
|
|
263
|
+
FROM "` + pgSchema + `"."` + table$2.tableName + `";`;
|
|
131
264
|
}
|
|
132
265
|
|
|
133
266
|
async function getInitialState(sql, pgSchema) {
|
|
134
267
|
let match = await Promise.all([
|
|
135
268
|
sql.unsafe(makeGetInitialStateQuery(pgSchema)),
|
|
136
|
-
sql.unsafe(
|
|
269
|
+
sql.unsafe(makeGetRowsQuery(pgSchema))
|
|
137
270
|
]);
|
|
138
|
-
let
|
|
139
|
-
match[1].forEach(row => {
|
|
140
|
-
let key = ChainId.toString(ChainId.normalizeOrThrow(row.chainId));
|
|
141
|
-
let addresses = indexingAddressesByChainId[key];
|
|
142
|
-
let addresses$1;
|
|
143
|
-
if (addresses !== undefined) {
|
|
144
|
-
addresses$1 = addresses;
|
|
145
|
-
} else {
|
|
146
|
-
let addresses$2 = [];
|
|
147
|
-
indexingAddressesByChainId[key] = addresses$2;
|
|
148
|
-
addresses$1 = addresses$2;
|
|
149
|
-
}
|
|
150
|
-
addresses$1.push({
|
|
151
|
-
address: row.address,
|
|
152
|
-
contractName: row.contractName,
|
|
153
|
-
registrationBlock: row.registrationBlock
|
|
154
|
-
});
|
|
155
|
-
});
|
|
271
|
+
let addressRowsByChainId = AddressRows.group(match[1]);
|
|
156
272
|
return match[0].map(rawInitialState => {
|
|
157
273
|
let id = ChainId.normalizeOrThrow(rawInitialState.id);
|
|
158
274
|
return {
|
|
@@ -164,7 +280,7 @@ async function getInitialState(sql, pgSchema) {
|
|
|
164
280
|
timestampCaughtUpToHeadOrEndblock: rawInitialState.timestampCaughtUpToHeadOrEndblock,
|
|
165
281
|
numEventsProcessed: rawInitialState.numEventsProcessed,
|
|
166
282
|
progressBlockNumber: rawInitialState.progressBlockNumber,
|
|
167
|
-
|
|
283
|
+
addressRows: Stdlib_Option.getOr(addressRowsByChainId[ChainId.toString(id)], AddressRows.emptySeedRows()),
|
|
168
284
|
sourceBlockNumber: rawInitialState.sourceBlockNumber
|
|
169
285
|
};
|
|
170
286
|
});
|
|
@@ -181,7 +297,7 @@ function makeProgressFieldsUpdateQuery(pgSchema) {
|
|
|
181
297
|
let paramIndex = index + 2 | 0;
|
|
182
298
|
return `"` + field + `" = $` + paramIndex.toString();
|
|
183
299
|
});
|
|
184
|
-
return `UPDATE "` + pgSchema + `"."` + table.tableName + `"
|
|
300
|
+
return `UPDATE "` + pgSchema + `"."` + table$2.tableName + `"
|
|
185
301
|
SET ` + setClauses.join(",\n ") + `
|
|
186
302
|
WHERE "id" = $1;`;
|
|
187
303
|
}
|
|
@@ -219,14 +335,13 @@ function setProgressedChains(sql, pgSchema, progressedChains) {
|
|
|
219
335
|
|
|
220
336
|
let Chains = {
|
|
221
337
|
fields: fields,
|
|
222
|
-
table: table,
|
|
338
|
+
table: table$2,
|
|
223
339
|
initialFromConfig: initialFromConfig,
|
|
224
340
|
makeInitialValuesQuery: makeInitialValuesQuery,
|
|
225
341
|
metaFields: metaFields,
|
|
226
342
|
makeMetaFieldsUpdateQuery: makeMetaFieldsUpdateQuery,
|
|
227
343
|
makeSetReadyAtQuery: makeSetReadyAtQuery,
|
|
228
344
|
makeGetInitialStateQuery: makeGetInitialStateQuery,
|
|
229
|
-
makeGetIndexingAddressesQuery: makeGetIndexingAddressesQuery,
|
|
230
345
|
getInitialState: getInitialState,
|
|
231
346
|
progressFields: progressFields,
|
|
232
347
|
makeProgressFieldsUpdateQuery: makeProgressFieldsUpdateQuery,
|
|
@@ -234,26 +349,19 @@ let Chains = {
|
|
|
234
349
|
setProgressedChains: setProgressedChains
|
|
235
350
|
};
|
|
236
351
|
|
|
237
|
-
let table$
|
|
352
|
+
let table$3 = Table.mkTable("envio_info", undefined, [
|
|
238
353
|
Table.mkField("id", "Int32", S$RescriptSchema.int, "1", undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
239
354
|
Table.mkField("config", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)
|
|
240
355
|
], undefined);
|
|
241
356
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
async function read(sql, pgSchema) {
|
|
357
|
+
async function read$1(sql, pgSchema) {
|
|
245
358
|
let rows;
|
|
246
359
|
try {
|
|
247
|
-
rows = await sql.unsafe(`SELECT "config" FROM "` + pgSchema + `"."` + table$
|
|
360
|
+
rows = await sql.unsafe(`SELECT "config" FROM "` + pgSchema + `"."` + table$3.tableName + `" LIMIT 1;`);
|
|
248
361
|
} catch (raw_exn) {
|
|
249
362
|
let exn = Primitive_exceptions.internalToException(raw_exn);
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (e._1.code === undefinedTableSqlState) {
|
|
253
|
-
rows = [];
|
|
254
|
-
} else {
|
|
255
|
-
throw exn;
|
|
256
|
-
}
|
|
363
|
+
if (isUndefinedTable(exn)) {
|
|
364
|
+
rows = [];
|
|
257
365
|
} else {
|
|
258
366
|
throw exn;
|
|
259
367
|
}
|
|
@@ -262,13 +370,12 @@ async function read(sql, pgSchema) {
|
|
|
262
370
|
}
|
|
263
371
|
|
|
264
372
|
function write(sql, pgSchema, envioInfo) {
|
|
265
|
-
return sql.unsafe(`INSERT INTO "` + pgSchema + `"."` + table$
|
|
373
|
+
return sql.unsafe(`INSERT INTO "` + pgSchema + `"."` + table$3.tableName + `" ("id", "config") VALUES (1, $1) ON CONFLICT ("id") DO UPDATE SET "config" = EXCLUDED."config";`, [JSON.stringify(envioInfo)], {prepare: true});
|
|
266
374
|
}
|
|
267
375
|
|
|
268
376
|
let EnvioInfo = {
|
|
269
|
-
table: table$
|
|
270
|
-
|
|
271
|
-
read: read,
|
|
377
|
+
table: table$3,
|
|
378
|
+
read: read$1,
|
|
272
379
|
write: write
|
|
273
380
|
};
|
|
274
381
|
|
|
@@ -283,20 +390,42 @@ let dbSchema = S$RescriptSchema.object(s => ({
|
|
|
283
390
|
events_processed: s.f("events_processed", S$RescriptSchema.int)
|
|
284
391
|
}));
|
|
285
392
|
|
|
286
|
-
let
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
393
|
+
let columns = [
|
|
394
|
+
{
|
|
395
|
+
field: Table.mkField("id", "UInt64", S$RescriptSchema.bigint, undefined, undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
396
|
+
clickHouseFieldType: "UInt64",
|
|
397
|
+
valuesOf: batch => batch.checkpointIds
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
field: Table.mkField("chain_id", "ChainId", ChainId.schema, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
401
|
+
clickHouseFieldType: "ChainId",
|
|
402
|
+
valuesOf: batch => batch.checkpointChainIds
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
field: Table.mkField("block_number", "Int32", S$RescriptSchema.int, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
406
|
+
clickHouseFieldType: "Int32",
|
|
407
|
+
valuesOf: batch => batch.checkpointBlockNumbers
|
|
408
|
+
},
|
|
409
|
+
{
|
|
410
|
+
field: Table.mkField("block_hash", "String", S$RescriptSchema.$$null(S$RescriptSchema.string), undefined, undefined, true, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
411
|
+
clickHouseFieldType: "String",
|
|
412
|
+
valuesOf: batch => batch.checkpointBlockHashes
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
field: Table.mkField("events_processed", "Int32", S$RescriptSchema.int, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
416
|
+
clickHouseFieldType: "UInt64",
|
|
417
|
+
valuesOf: batch => batch.checkpointEventsProcessed
|
|
418
|
+
}
|
|
419
|
+
];
|
|
420
|
+
|
|
421
|
+
let table$4 = Table.mkTable("envio_checkpoints", undefined, columns.map(param => param.field), undefined);
|
|
293
422
|
|
|
294
423
|
function makeGetReorgCheckpointsQuery(pgSchema) {
|
|
295
424
|
return `WITH reorg_chains AS (
|
|
296
425
|
SELECT
|
|
297
426
|
"` + "id" + `" as id,
|
|
298
427
|
"` + "source_block" + `" - "` + "max_reorg_depth" + `" AS safe_block
|
|
299
|
-
FROM "` + pgSchema + `"."` + table.tableName + `"
|
|
428
|
+
FROM "` + pgSchema + `"."` + table$2.tableName + `"
|
|
300
429
|
WHERE "` + "max_reorg_depth" + `" > 0
|
|
301
430
|
AND "` + "progress_block" + `" > "` + "source_block" + `" - "` + "max_reorg_depth" + `"
|
|
302
431
|
)
|
|
@@ -305,25 +434,26 @@ SELECT
|
|
|
305
434
|
cp."` + "chain_id" + `",
|
|
306
435
|
cp."` + "block_number" + `",
|
|
307
436
|
cp."` + "block_hash" + `"
|
|
308
|
-
FROM "` + pgSchema + `"."` + table$
|
|
437
|
+
FROM "` + pgSchema + `"."` + table$4.tableName + `" cp
|
|
309
438
|
INNER JOIN reorg_chains rc
|
|
310
439
|
ON cp."` + "chain_id" + `" = rc.id
|
|
311
440
|
WHERE cp."` + "block_hash" + `" IS NOT NULL
|
|
312
|
-
AND cp."` + "block_number" + `" >= rc.safe_block
|
|
441
|
+
AND cp."` + "block_number" + `" >= rc.safe_block
|
|
442
|
+
ORDER BY cp."` + "id" + `";`;
|
|
313
443
|
}
|
|
314
444
|
|
|
315
445
|
function makeCommitedCheckpointIdQuery(pgSchema) {
|
|
316
|
-
return `SELECT COALESCE(MAX(` + "id" + `), ` + (0n).toString() + `) AS id FROM "` + pgSchema + `"."` + table$
|
|
446
|
+
return `SELECT COALESCE(MAX(` + "id" + `), ` + (0n).toString() + `) AS id FROM "` + pgSchema + `"."` + table$4.tableName + `";`;
|
|
317
447
|
}
|
|
318
448
|
|
|
319
449
|
function makeInsertCheckpointQuery(pgSchema, chainIdModeOpt) {
|
|
320
450
|
let chainIdMode = chainIdModeOpt !== undefined ? chainIdModeOpt : "int32";
|
|
321
|
-
let chainIdArrayType =
|
|
322
|
-
return `INSERT INTO "` + pgSchema + `"."` + table$
|
|
323
|
-
SELECT * FROM unnest($1::` + "BIGINT" + `[],$2::` + chainIdArrayType + `,$3::` + "INTEGER" + `[],$4::` + "TEXT" + `[],$5::` + "INTEGER" + `[]);`;
|
|
451
|
+
let chainIdArrayType$1 = chainIdArrayType(pgSchema, chainIdMode);
|
|
452
|
+
return `INSERT INTO "` + pgSchema + `"."` + table$4.tableName + `" ("` + "id" + `", "` + "chain_id" + `", "` + "block_number" + `", "` + "block_hash" + `", "` + "events_processed" + `")
|
|
453
|
+
SELECT * FROM unnest($1::` + "BIGINT" + `[],$2::` + chainIdArrayType$1 + `,$3::` + "INTEGER" + `[],$4::` + "TEXT" + `[],$5::` + "INTEGER" + `[]);`;
|
|
324
454
|
}
|
|
325
455
|
|
|
326
|
-
function insert(sql, pgSchema, checkpointIds, checkpointChainIds, checkpointBlockNumbers, checkpointBlockHashes, checkpointEventsProcessed, chainIdModeOpt) {
|
|
456
|
+
function insert$2(sql, pgSchema, checkpointIds, checkpointChainIds, checkpointBlockNumbers, checkpointBlockHashes, checkpointEventsProcessed, chainIdModeOpt) {
|
|
327
457
|
let chainIdMode = chainIdModeOpt !== undefined ? chainIdModeOpt : "int32";
|
|
328
458
|
let query = makeInsertCheckpointQuery(pgSchema, chainIdMode);
|
|
329
459
|
let checkpointIdStrings = Utils.$$BigInt.arrayToStringArray(checkpointIds);
|
|
@@ -336,12 +466,14 @@ function insert(sql, pgSchema, checkpointIds, checkpointChainIds, checkpointBloc
|
|
|
336
466
|
], {prepare: true});
|
|
337
467
|
}
|
|
338
468
|
|
|
339
|
-
|
|
340
|
-
|
|
469
|
+
let chainIdColumn = "chain_id";
|
|
470
|
+
|
|
471
|
+
function rollback(sql, pgSchema, scope, rollbackTargetCheckpointId) {
|
|
472
|
+
return sql.unsafe(`DELETE FROM "` + pgSchema + `"."` + table$4.tableName + `" WHERE "` + "id" + `" > $1` + RollbackScope.predicate(scope, chainIdColumn) + `;`, RollbackScope.params(scope, rollbackTargetCheckpointId), {prepare: true});
|
|
341
473
|
}
|
|
342
474
|
|
|
343
475
|
function makePruneStaleCheckpointsQuery(pgSchema) {
|
|
344
|
-
return `DELETE FROM "` + pgSchema + `"."` + table$
|
|
476
|
+
return `DELETE FROM "` + pgSchema + `"."` + table$4.tableName + `" WHERE "` + "id" + `" < $1;`;
|
|
345
477
|
}
|
|
346
478
|
|
|
347
479
|
function pruneStaleCheckpoints(sql, pgSchema, safeCheckpointId) {
|
|
@@ -349,7 +481,7 @@ function pruneStaleCheckpoints(sql, pgSchema, safeCheckpointId) {
|
|
|
349
481
|
}
|
|
350
482
|
|
|
351
483
|
function makeGetRollbackTargetCheckpointQuery(pgSchema) {
|
|
352
|
-
return `SELECT "` + "id" + `" FROM "` + pgSchema + `"."` + table$
|
|
484
|
+
return `SELECT "` + "id" + `" FROM "` + pgSchema + `"."` + table$4.tableName + `"
|
|
353
485
|
WHERE
|
|
354
486
|
"` + "chain_id" + `" = $1 AND
|
|
355
487
|
"` + "block_number" + `" <= $2
|
|
@@ -365,28 +497,30 @@ function getRollbackTargetCheckpoint(sql, pgSchema, reorgChainId, lastKnownValid
|
|
|
365
497
|
return rawResult.then(rows => Stdlib_Option.map(rows[0], row => BigInt(row.id)));
|
|
366
498
|
}
|
|
367
499
|
|
|
368
|
-
function makeGetRollbackProgressDiffQuery(pgSchema) {
|
|
500
|
+
function makeGetRollbackProgressDiffQuery(pgSchema, scope) {
|
|
369
501
|
return `SELECT
|
|
370
502
|
"` + "chain_id" + `"::float8 as "` + "chain_id" + `",
|
|
371
503
|
SUM("` + "events_processed" + `") as events_processed_diff,
|
|
372
504
|
MIN("` + "block_number" + `") - 1 as new_progress_block_number
|
|
373
|
-
FROM "` + pgSchema + `"."` + table$
|
|
374
|
-
WHERE "` + "id" + `" > $1
|
|
505
|
+
FROM "` + pgSchema + `"."` + table$4.tableName + `"
|
|
506
|
+
WHERE "` + "id" + `" > $1` + RollbackScope.predicate(scope, chainIdColumn) + `
|
|
375
507
|
GROUP BY "` + "chain_id" + `";`;
|
|
376
508
|
}
|
|
377
509
|
|
|
378
|
-
function getRollbackProgressDiff(sql, pgSchema, rollbackTargetCheckpointId) {
|
|
379
|
-
return sql.unsafe(makeGetRollbackProgressDiffQuery(pgSchema),
|
|
510
|
+
function getRollbackProgressDiff(sql, pgSchema, scope, rollbackTargetCheckpointId) {
|
|
511
|
+
return sql.unsafe(makeGetRollbackProgressDiffQuery(pgSchema, scope), RollbackScope.params(scope, rollbackTargetCheckpointId), {prepare: true});
|
|
380
512
|
}
|
|
381
513
|
|
|
382
514
|
let Checkpoints = {
|
|
383
515
|
dbSchema: dbSchema,
|
|
384
516
|
initialCheckpointId: 0n,
|
|
385
|
-
|
|
517
|
+
columns: columns,
|
|
518
|
+
table: table$4,
|
|
386
519
|
makeGetReorgCheckpointsQuery: makeGetReorgCheckpointsQuery,
|
|
387
520
|
makeCommitedCheckpointIdQuery: makeCommitedCheckpointIdQuery,
|
|
388
521
|
makeInsertCheckpointQuery: makeInsertCheckpointQuery,
|
|
389
|
-
insert: insert,
|
|
522
|
+
insert: insert$2,
|
|
523
|
+
chainIdColumn: chainIdColumn,
|
|
390
524
|
rollback: rollback,
|
|
391
525
|
makePruneStaleCheckpointsQuery: makePruneStaleCheckpointsQuery,
|
|
392
526
|
pruneStaleCheckpoints: pruneStaleCheckpoints,
|
|
@@ -411,7 +545,7 @@ let schema = S$RescriptSchema.schema(s => ({
|
|
|
411
545
|
params: s.m(S$RescriptSchema.json(false))
|
|
412
546
|
}));
|
|
413
547
|
|
|
414
|
-
let table$
|
|
548
|
+
let table$5 = Table.mkTable("raw_events", undefined, [
|
|
415
549
|
Table.mkField("chain_id", "ChainId", ChainId.schema, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
416
550
|
Table.mkField("event_id", "UInt64", S$RescriptSchema.bigint, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
417
551
|
Table.mkField("event_name", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
@@ -429,7 +563,7 @@ let table$3 = Table.mkTable("raw_events", undefined, [
|
|
|
429
563
|
|
|
430
564
|
let RawEvents = {
|
|
431
565
|
schema: schema,
|
|
432
|
-
table: table$
|
|
566
|
+
table: table$5
|
|
433
567
|
};
|
|
434
568
|
|
|
435
569
|
let metaViewName = "_meta";
|
|
@@ -440,6 +574,7 @@ function makeMetaViewQuery(pgSchema) {
|
|
|
440
574
|
return `CREATE VIEW "` + pgSchema + `"."` + metaViewName + `" AS
|
|
441
575
|
SELECT
|
|
442
576
|
"` + "id" + `" AS "chainId",
|
|
577
|
+
"` + "ecosystem" + `" AS "ecosystem",
|
|
443
578
|
"` + "start_block" + `" AS "startBlock",
|
|
444
579
|
"` + "end_block" + `" AS "endBlock",
|
|
445
580
|
"` + "progress_block" + `" AS "progressBlock",
|
|
@@ -449,7 +584,7 @@ SELECT
|
|
|
449
584
|
"` + "source_block" + `" AS "sourceBlock",
|
|
450
585
|
"` + "ready_at" + `" AS "readyAt",
|
|
451
586
|
("` + "ready_at" + `" IS NOT NULL) AS "isReady"
|
|
452
|
-
FROM "` + pgSchema + `"."` + table.tableName + `"
|
|
587
|
+
FROM "` + pgSchema + `"."` + table$2.tableName + `"
|
|
453
588
|
ORDER BY "` + "id" + `";`;
|
|
454
589
|
}
|
|
455
590
|
|
|
@@ -458,6 +593,7 @@ function makeChainMetadataViewQuery(pgSchema) {
|
|
|
458
593
|
SELECT
|
|
459
594
|
"` + "source_block" + `" AS "block_height",
|
|
460
595
|
"` + "id" + `" AS "chain_id",
|
|
596
|
+
"` + "ecosystem" + `" AS "ecosystem",
|
|
461
597
|
"` + "end_block" + `" AS "end_block",
|
|
462
598
|
"` + "first_event_block" + `" AS "first_event_block_number",
|
|
463
599
|
"` + "_is_hyper_sync" + `" AS "is_hyper_sync",
|
|
@@ -467,7 +603,7 @@ SELECT
|
|
|
467
603
|
"` + "events_processed" + `"::float4 AS "num_events_processed",
|
|
468
604
|
"` + "start_block" + `" AS "start_block",
|
|
469
605
|
"` + "ready_at" + `" AS "timestamp_caught_up_to_head_or_endblock"
|
|
470
|
-
FROM "` + pgSchema + `"."` + table.tableName + `";`;
|
|
606
|
+
FROM "` + pgSchema + `"."` + table$2.tableName + `";`;
|
|
471
607
|
}
|
|
472
608
|
|
|
473
609
|
let Views = {
|
|
@@ -483,12 +619,14 @@ let isNullable = true;
|
|
|
483
619
|
|
|
484
620
|
let isIndex = true;
|
|
485
621
|
|
|
486
|
-
let EnvioAddresses;
|
|
487
|
-
|
|
488
622
|
export {
|
|
489
623
|
isPrimaryKey,
|
|
490
624
|
isNullable,
|
|
491
625
|
isIndex,
|
|
626
|
+
undefinedTableSqlState,
|
|
627
|
+
isUndefinedTable,
|
|
628
|
+
chainIdArrayType,
|
|
629
|
+
EnvioContracts,
|
|
492
630
|
EnvioAddresses,
|
|
493
631
|
Chains,
|
|
494
632
|
EnvioInfo,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// How far across chains a reorg rollback reaches. `Isolated` is only chosen for
|
|
2
|
+
// a schema with no cross-chain entity, where a reorg on one chain can't have
|
|
3
|
+
// changed a row another chain owns — every sibling then keeps its progress,
|
|
4
|
+
// entities, history and checkpoints.
|
|
5
|
+
type t =
|
|
6
|
+
| Global
|
|
7
|
+
| Isolated(ChainId.t)
|
|
8
|
+
|
|
9
|
+
// Appended to the WHERE of a rollback query, which binds its target checkpoint
|
|
10
|
+
// as $1. An isolated rollback binds its chain id as $2 so only the rows that
|
|
11
|
+
// chain owns match.
|
|
12
|
+
let predicate = (scope, ~chainIdColumn: option<string>) =>
|
|
13
|
+
switch (scope, chainIdColumn) {
|
|
14
|
+
| (Global, _) => ""
|
|
15
|
+
| (Isolated(_), Some(column)) => ` AND "${column}" = $2`
|
|
16
|
+
| (Isolated(_), None) =>
|
|
17
|
+
JsError.throwWithMessage(
|
|
18
|
+
"Internal error: a rollback isolated to one chain needs a chain-id column to narrow to.",
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// The parameters such a query binds, in the order `predicate` reads them.
|
|
23
|
+
let params = (scope, ~targetCheckpointId: Internal.checkpointId): unknown => {
|
|
24
|
+
let targetCheckpointId = targetCheckpointId->BigInt.toString->(Utils.magic: string => unknown)
|
|
25
|
+
switch scope {
|
|
26
|
+
| Global => [targetCheckpointId]
|
|
27
|
+
| Isolated(chainId) => [targetCheckpointId, chainId->(Utils.magic: ChainId.t => unknown)]
|
|
28
|
+
}->(Utils.magic: array<unknown> => unknown)
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
|
|
4
|
+
|
|
5
|
+
function predicate(scope, chainIdColumn) {
|
|
6
|
+
if (typeof scope !== "object") {
|
|
7
|
+
return "";
|
|
8
|
+
} else if (chainIdColumn !== undefined) {
|
|
9
|
+
return ` AND "` + chainIdColumn + `" = $2`;
|
|
10
|
+
} else {
|
|
11
|
+
return Stdlib_JsError.throwWithMessage("Internal error: a rollback isolated to one chain needs a chain-id column to narrow to.");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function params(scope, targetCheckpointId) {
|
|
16
|
+
let targetCheckpointId$1 = targetCheckpointId.toString();
|
|
17
|
+
if (typeof scope !== "object") {
|
|
18
|
+
return [targetCheckpointId$1];
|
|
19
|
+
} else {
|
|
20
|
+
return [
|
|
21
|
+
targetCheckpointId$1,
|
|
22
|
+
scope._0
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
predicate,
|
|
29
|
+
params,
|
|
30
|
+
}
|
|
31
|
+
/* No side effect */
|