envio 3.6.1 → 3.7.0-subgraph
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/index.d.ts +197 -9
- package/package.json +6 -6
- package/src/Batch.res +64 -25
- package/src/Batch.res.mjs +69 -46
- package/src/ChainFetching.res +13 -17
- package/src/ChainFetching.res.mjs +9 -14
- package/src/ChainState.res +160 -119
- package/src/ChainState.res.mjs +75 -46
- package/src/ChainState.resi +15 -12
- package/src/Config.res +18 -2
- package/src/Config.res.mjs +9 -6
- package/src/Core.res +20 -0
- package/src/Core.res.mjs +11 -0
- package/src/Ecosystem.res +0 -1
- package/src/Envio.res +16 -3
- package/src/EventConfigBuilder.res +155 -40
- package/src/EventConfigBuilder.res.mjs +76 -24
- package/src/EventProcessing.res +10 -14
- package/src/EventProcessing.res.mjs +12 -10
- package/src/EventUtils.res +3 -47
- package/src/FetchState.res +124 -104
- package/src/FetchState.res.mjs +116 -100
- package/src/HandlerLoader.res +20 -8
- package/src/HandlerLoader.res.mjs +11 -2
- package/src/HandlerRegister.res +108 -2
- package/src/HandlerRegister.res.mjs +59 -10
- package/src/HandlerRegister.resi +4 -0
- package/src/InMemoryStore.res +1 -6
- package/src/InMemoryStore.res.mjs +1 -3
- package/src/Internal.res +84 -24
- package/src/Internal.res.mjs +33 -2
- package/src/LazyLoader.res +12 -11
- package/src/LazyLoader.res.mjs +13 -7
- package/src/Main.res +22 -9
- package/src/Main.res.mjs +18 -5
- package/src/MemoryStorage.res +756 -0
- package/src/MemoryStorage.res.mjs +623 -0
- package/src/Persistence.res +4 -2
- package/src/PgStorage.res +24 -8
- package/src/PgStorage.res.mjs +15 -12
- package/src/ReorgDetection.res +0 -222
- package/src/ReorgDetection.res.mjs +0 -163
- package/src/Rollback.res +14 -15
- package/src/Rollback.res.mjs +4 -5
- package/src/SimulateItems.res +2 -2
- package/src/UserContext.res +358 -51
- package/src/UserContext.res.mjs +271 -35
- package/src/Utils.res +3 -0
- package/src/bindings/ClickHouse.res +16 -11
- package/src/bindings/ClickHouse.res.mjs +10 -10
- package/src/bindings/Vitest.res +1 -1
- package/src/sources/BlockStore.res +115 -5
- package/src/sources/BlockStore.res.mjs +25 -0
- package/src/sources/Evm.res +0 -1
- package/src/sources/Evm.res.mjs +0 -1
- package/src/sources/EvmHyperSyncSource.res +19 -84
- package/src/sources/EvmHyperSyncSource.res.mjs +24 -60
- package/src/sources/Fuel.res +24 -4
- package/src/sources/Fuel.res.mjs +23 -3
- package/src/sources/FuelHyperSync.res +8 -3
- package/src/sources/FuelHyperSync.res.mjs +5 -4
- package/src/sources/FuelHyperSync.resi +3 -1
- package/src/sources/FuelHyperSyncClient.res +7 -10
- package/src/sources/FuelHyperSyncSource.res +18 -45
- package/src/sources/FuelHyperSyncSource.res.mjs +18 -35
- package/src/sources/HyperSync.res +49 -229
- package/src/sources/HyperSync.res.mjs +74 -191
- package/src/sources/HyperSync.resi +11 -35
- package/src/sources/HyperSyncClient.res +9 -79
- package/src/sources/HyperSyncClient.res.mjs +2 -6
- package/src/sources/RequestStat.res +5 -0
- package/src/sources/RequestStat.res.mjs +2 -0
- package/src/sources/RpcSource.res +149 -43
- package/src/sources/RpcSource.res.mjs +104 -41
- package/src/sources/SimulateSource.res +8 -5
- package/src/sources/SimulateSource.res.mjs +6 -6
- package/src/sources/Source.res +89 -16
- package/src/sources/Source.res.mjs +50 -1
- package/src/sources/SourceManager.res +255 -50
- package/src/sources/SourceManager.res.mjs +149 -55
- package/src/sources/SourceManager.resi +2 -6
- package/src/sources/Svm.res +0 -7
- package/src/sources/Svm.res.mjs +0 -8
- package/src/sources/SvmHyperSyncClient.res +21 -16
- package/src/sources/SvmHyperSyncClient.res.mjs +3 -4
- package/src/sources/SvmHyperSyncSource.res +25 -117
- package/src/sources/SvmHyperSyncSource.res.mjs +8 -121
- package/src/subgraph/blocks.ts +176 -0
- package/src/subgraph/calls.ts +217 -0
- package/src/subgraph/conformance.ts +99 -0
- package/src/subgraph/division.ts +110 -0
- package/src/subgraph/errors.ts +90 -0
- package/src/subgraph/graph-ts-types/VERSION +2 -0
- package/src/subgraph/graph-ts-types/chain/arweave.d.ts +70 -0
- package/src/subgraph/graph-ts-types/chain/cosmos.d.ts +327 -0
- package/src/subgraph/graph-ts-types/chain/ethereum.d.ts +233 -0
- package/src/subgraph/graph-ts-types/chain/near.d.ts +253 -0
- package/src/subgraph/graph-ts-types/chain/starknet.d.ts +32 -0
- package/src/subgraph/graph-ts-types/common/collections.d.ts +136 -0
- package/src/subgraph/graph-ts-types/common/conversion.d.ts +11 -0
- package/src/subgraph/graph-ts-types/common/datasource.d.ts +30 -0
- package/src/subgraph/graph-ts-types/common/eager-offset.d.ts +0 -0
- package/src/subgraph/graph-ts-types/common/json.d.ts +17 -0
- package/src/subgraph/graph-ts-types/common/numbers.d.ts +120 -0
- package/src/subgraph/graph-ts-types/common/value.d.ts +120 -0
- package/src/subgraph/graph-ts-types/common/yaml.d.ts +90 -0
- package/src/subgraph/graph-ts-types/global/global.d.ts +194 -0
- package/src/subgraph/graph-ts-types/helper-functions.d.ts +22 -0
- package/src/subgraph/graph-ts-types/index.d.ts +102 -0
- package/src/subgraph/graph-ts.ts +1871 -0
- package/src/subgraph/hosts.ts +148 -0
- package/src/subgraph/runtime.ts +845 -0
- package/src/subgraph/scope.ts +63 -0
- package/svm.schema.json +3 -2
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Table from "./db/Table.res.mjs";
|
|
4
|
+
import * as Utils from "./Utils.res.mjs";
|
|
5
|
+
import * as Config from "./Config.res.mjs";
|
|
6
|
+
import * as ChainId from "./ChainId.res.mjs";
|
|
7
|
+
import * as EntityId from "./EntityId.res.mjs";
|
|
8
|
+
import * as Internal from "./Internal.res.mjs";
|
|
9
|
+
import * as Stdlib_Null from "@rescript/runtime/lib/es6/Stdlib_Null.js";
|
|
10
|
+
import * as EntityFilter from "./db/EntityFilter.res.mjs";
|
|
11
|
+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
12
|
+
import * as InternalTable from "./db/InternalTable.res.mjs";
|
|
13
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
14
|
+
import * as Primitive_float from "@rescript/runtime/lib/es6/Primitive_float.js";
|
|
15
|
+
import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
|
|
16
|
+
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
|
|
17
|
+
import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js";
|
|
18
|
+
|
|
19
|
+
function make() {
|
|
20
|
+
return {
|
|
21
|
+
entities: {},
|
|
22
|
+
entityConfigs: {},
|
|
23
|
+
history: {},
|
|
24
|
+
checkpoints: [],
|
|
25
|
+
chains: {},
|
|
26
|
+
cache: {},
|
|
27
|
+
effectCache: {},
|
|
28
|
+
envioInfo: undefined,
|
|
29
|
+
isInitialized: false
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function rowKey(scope, entityId) {
|
|
34
|
+
if (scope === "crossChain") {
|
|
35
|
+
return EntityId.toKey(entityId);
|
|
36
|
+
} else {
|
|
37
|
+
return ChainId.toString(scope) + `|` + EntityId.toKey(entityId);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let historyKey = rowKey;
|
|
42
|
+
|
|
43
|
+
function readChainId(entity, field) {
|
|
44
|
+
return entity[field.fieldName];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function copyEntity(entity) {
|
|
48
|
+
return Utils.Dict.shallowCopy(entity);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getEntityDict(state, name) {
|
|
52
|
+
let dict = state.entities[name];
|
|
53
|
+
if (dict !== undefined) {
|
|
54
|
+
return dict;
|
|
55
|
+
}
|
|
56
|
+
let dict$1 = {};
|
|
57
|
+
state.entities[name] = dict$1;
|
|
58
|
+
return dict$1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getHistory(state, name) {
|
|
62
|
+
let rows = state.history[name];
|
|
63
|
+
if (rows !== undefined) {
|
|
64
|
+
return rows;
|
|
65
|
+
}
|
|
66
|
+
let rows$1 = [];
|
|
67
|
+
state.history[name] = rows$1;
|
|
68
|
+
return rows$1;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function registerEntities(state, entities) {
|
|
72
|
+
entities.forEach(entityConfig => {
|
|
73
|
+
state.entityConfigs[entityConfig.name] = entityConfig;
|
|
74
|
+
state.entityConfigs[entityConfig.table.tableName] = entityConfig;
|
|
75
|
+
getEntityDict(state, entityConfig.name);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function seedIndexingAddresses(state, chainConfigs) {
|
|
80
|
+
let dict = getEntityDict(state, Config.EnvioAddresses.name);
|
|
81
|
+
chainConfigs.forEach(chainConfig => {
|
|
82
|
+
chainConfig.contracts.forEach(contract => {
|
|
83
|
+
contract.addresses.forEach(address => {
|
|
84
|
+
let entity_id = Config.EnvioAddresses.makeId(chainConfig.id, address);
|
|
85
|
+
let entity_chain_id = chainConfig.id;
|
|
86
|
+
let entity_contract_name = contract.name;
|
|
87
|
+
let entity = {
|
|
88
|
+
id: entity_id,
|
|
89
|
+
chain_id: entity_chain_id,
|
|
90
|
+
registration_block: -1,
|
|
91
|
+
registration_log_index: -1,
|
|
92
|
+
contract_name: entity_contract_name
|
|
93
|
+
};
|
|
94
|
+
dict[entity_id] = entity;
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function toIndexingAddress(dc) {
|
|
101
|
+
return {
|
|
102
|
+
address: Config.EnvioAddresses.getAddress(dc),
|
|
103
|
+
contractName: dc.contract_name,
|
|
104
|
+
registrationBlock: dc.registration_block
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getIndexingAddressesByChain(state) {
|
|
109
|
+
let byChain = {};
|
|
110
|
+
let dcDict = state.entities[Config.EnvioAddresses.name];
|
|
111
|
+
if (dcDict !== undefined) {
|
|
112
|
+
Object.values(dcDict).forEach(entity => {
|
|
113
|
+
let chainIdStr = ChainId.toString(entity.chain_id);
|
|
114
|
+
let arr = byChain[chainIdStr];
|
|
115
|
+
let contracts;
|
|
116
|
+
if (arr !== undefined) {
|
|
117
|
+
contracts = arr;
|
|
118
|
+
} else {
|
|
119
|
+
let arr$1 = [];
|
|
120
|
+
byChain[chainIdStr] = arr$1;
|
|
121
|
+
contracts = arr$1;
|
|
122
|
+
}
|
|
123
|
+
contracts.push(toIndexingAddress(entity));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return byChain;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function toInitialChainStates(state) {
|
|
130
|
+
let addressesByChain = getIndexingAddressesByChain(state);
|
|
131
|
+
return Object.values(state.chains).map(chain => ({
|
|
132
|
+
id: chain.id,
|
|
133
|
+
startBlock: chain.startBlock,
|
|
134
|
+
endBlock: chain.endBlock,
|
|
135
|
+
maxReorgDepth: chain.maxReorgDepth,
|
|
136
|
+
progressBlockNumber: chain.progressBlockNumber,
|
|
137
|
+
numEventsProcessed: chain.numEventsProcessed,
|
|
138
|
+
firstEventBlockNumber: chain.firstEventBlockNumber,
|
|
139
|
+
timestampCaughtUpToHeadOrEndblock: chain.timestampCaughtUpToHeadOrEndblock,
|
|
140
|
+
indexingAddresses: Stdlib_Option.getOr(addressesByChain[ChainId.toString(chain.id)], []),
|
|
141
|
+
sourceBlockNumber: chain.sourceBlockNumber
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function committedCheckpointId(state) {
|
|
146
|
+
return Stdlib_Array.reduce(state.checkpoints, InternalTable.Checkpoints.initialCheckpointId, (max, cp) => {
|
|
147
|
+
if (cp.id > max) {
|
|
148
|
+
return cp.id;
|
|
149
|
+
} else {
|
|
150
|
+
return max;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function reorgCheckpoints(state) {
|
|
156
|
+
return Stdlib_Array.filterMap(state.checkpoints, cp => {
|
|
157
|
+
let match = cp.blockHash;
|
|
158
|
+
let match$1 = state.chains[ChainId.toString(cp.chainId)];
|
|
159
|
+
if (match === undefined) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (match$1 === undefined) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
let safeBlock = match$1.sourceBlockNumber - match$1.maxReorgDepth | 0;
|
|
166
|
+
if (match$1.maxReorgDepth > 0 && match$1.progressBlockNumber > safeBlock && cp.blockNumber >= safeBlock) {
|
|
167
|
+
return {
|
|
168
|
+
id: cp.id,
|
|
169
|
+
chain_id: cp.chainId,
|
|
170
|
+
block_number: cp.blockNumber,
|
|
171
|
+
block_hash: match
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function toInitialState(state, cleanRun) {
|
|
178
|
+
return {
|
|
179
|
+
cleanRun: cleanRun,
|
|
180
|
+
cache: state.cache,
|
|
181
|
+
chains: toInitialChainStates(state),
|
|
182
|
+
checkpointId: committedCheckpointId(state),
|
|
183
|
+
reorgCheckpoints: reorgCheckpoints(state),
|
|
184
|
+
envioInfo: state.envioInfo
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function handleLoad(state, tableName, filter) {
|
|
189
|
+
let entityConfig = state.entityConfigs[tableName];
|
|
190
|
+
if (entityConfig !== undefined) {
|
|
191
|
+
let entityDict = Stdlib_Option.getOr(state.entities[entityConfig.name], {});
|
|
192
|
+
let matched = Object.values(entityDict).filter(entity => EntityFilter.matches(filter, entity));
|
|
193
|
+
let field = Table.getChainIdField(entityConfig.table);
|
|
194
|
+
if (field !== undefined) {
|
|
195
|
+
return matched.map(entity => {
|
|
196
|
+
let copy = Utils.Dict.shallowCopy(entity);
|
|
197
|
+
Utils.Dict.deleteInPlace(copy, field.fieldName);
|
|
198
|
+
return copy;
|
|
199
|
+
});
|
|
200
|
+
} else {
|
|
201
|
+
return matched;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
let cacheDict = state.effectCache[tableName];
|
|
205
|
+
if (cacheDict !== undefined) {
|
|
206
|
+
return Object.values(cacheDict).filter(item => EntityFilter.matches(filter, item));
|
|
207
|
+
} else {
|
|
208
|
+
return [];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function backfillHistory(state, entityConfig, scope, entityId, rows) {
|
|
213
|
+
let key = rowKey(scope, entityId);
|
|
214
|
+
if (rows.some(row => rowKey(row.scope, row.entityId) === key)) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
let entity = Stdlib_Option.flatMap(state.entities[entityConfig.name], dict => dict[rowKey(scope, entityId)]);
|
|
218
|
+
if (entity !== undefined) {
|
|
219
|
+
rows.push({
|
|
220
|
+
entityId: entityId,
|
|
221
|
+
scope: scope,
|
|
222
|
+
checkpointId: InternalTable.Checkpoints.initialCheckpointId,
|
|
223
|
+
action: "SET",
|
|
224
|
+
entity: entity
|
|
225
|
+
});
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function applyRollback(state, targetCheckpointId) {
|
|
231
|
+
state.checkpoints = state.checkpoints.filter(cp => cp.id <= targetCheckpointId);
|
|
232
|
+
Object.entries(state.history).forEach(param => {
|
|
233
|
+
state.history[param[0]] = param[1].filter(row => row.checkpointId <= targetCheckpointId);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function writeBatch(state, batch, rollback, isInReorgThreshold, config, updatedEntities, updatedEffectsCache, chainMetaData) {
|
|
238
|
+
let shouldSaveHistory = Config.shouldSaveHistory(config, isInReorgThreshold);
|
|
239
|
+
if (rollback !== undefined) {
|
|
240
|
+
applyRollback(state, rollback.targetCheckpointId);
|
|
241
|
+
}
|
|
242
|
+
let diffCheckpointId = Stdlib_Option.map(rollback, param => param.diffCheckpointId);
|
|
243
|
+
let isDiff = change => {
|
|
244
|
+
if (diffCheckpointId !== undefined) {
|
|
245
|
+
return change.checkpointId === diffCheckpointId;
|
|
246
|
+
} else {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
updatedEntities.forEach(param => {
|
|
251
|
+
let changes = param.changes;
|
|
252
|
+
let scope = param.scope;
|
|
253
|
+
let entityConfig = param.entityConfig;
|
|
254
|
+
let entityDict = getEntityDict(state, entityConfig.name);
|
|
255
|
+
let historyRows = getHistory(state, entityConfig.name);
|
|
256
|
+
let chainIdField = Table.getChainIdField(entityConfig.table);
|
|
257
|
+
let idsWithDiff = new Set();
|
|
258
|
+
changes.forEach(change => {
|
|
259
|
+
if (isDiff(change)) {
|
|
260
|
+
idsWithDiff.add(EntityId.toKey(change.entityId));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
changes.forEach(change => {
|
|
265
|
+
let entityId = change.entityId;
|
|
266
|
+
let shouldSaveChangeHistory = shouldSaveHistory && !isDiff(change);
|
|
267
|
+
if (shouldSaveHistory && !idsWithDiff.has(EntityId.toKey(entityId))) {
|
|
268
|
+
backfillHistory(state, entityConfig, scope, entityId, historyRows);
|
|
269
|
+
}
|
|
270
|
+
if (change.type === "SET") {
|
|
271
|
+
let entity = change.entity;
|
|
272
|
+
let match = Internal.chainScopeChainId(scope);
|
|
273
|
+
let storedEntity = chainIdField !== undefined && match !== undefined ? Internal.stampChainId(entity, chainIdField.fieldName, Primitive_option.valFromOption(match)) : entity;
|
|
274
|
+
entityDict[rowKey(scope, entityId)] = storedEntity;
|
|
275
|
+
if (shouldSaveChangeHistory) {
|
|
276
|
+
historyRows.push({
|
|
277
|
+
entityId: entityId,
|
|
278
|
+
scope: scope,
|
|
279
|
+
checkpointId: change.checkpointId,
|
|
280
|
+
action: "SET",
|
|
281
|
+
entity: storedEntity
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
} else {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
Utils.Dict.deleteInPlace(entityDict, rowKey(scope, entityId));
|
|
289
|
+
if (shouldSaveChangeHistory) {
|
|
290
|
+
historyRows.push({
|
|
291
|
+
entityId: entityId,
|
|
292
|
+
scope: scope,
|
|
293
|
+
checkpointId: change.checkpointId,
|
|
294
|
+
action: "DELETE",
|
|
295
|
+
entity: undefined
|
|
296
|
+
});
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
Object.values(batch.progressedChainsById).forEach(chainAfterBatch => {
|
|
302
|
+
let key = ChainId.toString(chainAfterBatch.fetchState.chainId);
|
|
303
|
+
let chain = state.chains[key];
|
|
304
|
+
if (chain !== undefined) {
|
|
305
|
+
chain.progressBlockNumber = chainAfterBatch.progressBlockNumber;
|
|
306
|
+
chain.sourceBlockNumber = chainAfterBatch.sourceBlockNumber;
|
|
307
|
+
chain.numEventsProcessed = chainAfterBatch.totalEventsProcessed;
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
if (chainMetaData !== undefined) {
|
|
312
|
+
Object.entries(chainMetaData).forEach(param => {
|
|
313
|
+
let meta = param[1];
|
|
314
|
+
let chain = state.chains[param[0]];
|
|
315
|
+
if (chain !== undefined) {
|
|
316
|
+
chain.firstEventBlockNumber = Primitive_option.fromNull(meta.first_event_block);
|
|
317
|
+
chain.latestFetchedBlockNumber = meta.buffer_block;
|
|
318
|
+
chain.timestampCaughtUpToHeadOrEndblock = Primitive_option.fromNull(meta.ready_at);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
if (shouldSaveHistory) {
|
|
324
|
+
for (let i = 0, i_finish = batch.checkpointIds.length; i < i_finish; ++i) {
|
|
325
|
+
state.checkpoints.push({
|
|
326
|
+
id: batch.checkpointIds[i],
|
|
327
|
+
chainId: batch.checkpointChainIds[i],
|
|
328
|
+
blockNumber: batch.checkpointBlockNumbers[i],
|
|
329
|
+
blockHash: Primitive_option.fromNull(batch.checkpointBlockHashes[i]),
|
|
330
|
+
eventsProcessed: batch.checkpointEventsProcessed[i]
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
updatedEffectsCache.forEach(param => {
|
|
335
|
+
let table = param.table;
|
|
336
|
+
let dict = state.effectCache[table.tableName];
|
|
337
|
+
let cacheDict;
|
|
338
|
+
if (dict !== undefined) {
|
|
339
|
+
cacheDict = dict;
|
|
340
|
+
} else {
|
|
341
|
+
let dict$1 = {};
|
|
342
|
+
state.effectCache[table.tableName] = dict$1;
|
|
343
|
+
cacheDict = dict$1;
|
|
344
|
+
}
|
|
345
|
+
param.items.forEach(item => {
|
|
346
|
+
cacheDict[item.id] = item;
|
|
347
|
+
});
|
|
348
|
+
let record = state.cache[table.tableName];
|
|
349
|
+
if (record !== undefined) {
|
|
350
|
+
record.count = Object.keys(cacheDict).length;
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function getRollbackData(state, entityConfig, rollbackTargetCheckpointId) {
|
|
357
|
+
let rows = Stdlib_Option.getOr(state.history[entityConfig.name], []);
|
|
358
|
+
let changedKeys = {};
|
|
359
|
+
rows.forEach(row => {
|
|
360
|
+
if (row.checkpointId > rollbackTargetCheckpointId) {
|
|
361
|
+
changedKeys[rowKey(row.scope, row.entityId)] = row;
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
let removals = [];
|
|
366
|
+
let restored = [];
|
|
367
|
+
Object.entries(changedKeys).forEach(param => {
|
|
368
|
+
let changedRow = param[1];
|
|
369
|
+
let key = param[0];
|
|
370
|
+
let preTarget = Stdlib_Array.reduce(rows, undefined, (latest, row) => {
|
|
371
|
+
if (rowKey(row.scope, row.entityId) === key && row.checkpointId <= rollbackTargetCheckpointId && !(latest !== undefined && latest.checkpointId >= row.checkpointId)) {
|
|
372
|
+
return row;
|
|
373
|
+
} else {
|
|
374
|
+
return latest;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
if (preTarget !== undefined) {
|
|
378
|
+
if (preTarget.action === "SET") {
|
|
379
|
+
let entity = preTarget.entity;
|
|
380
|
+
if (entity !== undefined) {
|
|
381
|
+
restored.push(entity);
|
|
382
|
+
return;
|
|
383
|
+
} else {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
removals.push({
|
|
388
|
+
entityId: preTarget.entityId,
|
|
389
|
+
scope: preTarget.scope
|
|
390
|
+
});
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
removals.push({
|
|
394
|
+
entityId: changedRow.entityId,
|
|
395
|
+
scope: changedRow.scope
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
return [
|
|
399
|
+
removals,
|
|
400
|
+
restored
|
|
401
|
+
];
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function getRollbackProgressDiff(state, rollbackTargetCheckpointId) {
|
|
405
|
+
let byChain = {};
|
|
406
|
+
state.checkpoints.forEach(cp => {
|
|
407
|
+
if (cp.id <= rollbackTargetCheckpointId) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
let key = ChainId.toString(cp.chainId);
|
|
411
|
+
let acc = byChain[key];
|
|
412
|
+
if (acc !== undefined) {
|
|
413
|
+
acc.eventsProcessed = acc.eventsProcessed + cp.eventsProcessed | 0;
|
|
414
|
+
acc.newProgressBlockNumber = Math.min(acc.newProgressBlockNumber, cp.blockNumber - 1 | 0);
|
|
415
|
+
} else {
|
|
416
|
+
byChain[key] = {
|
|
417
|
+
chainId: cp.chainId,
|
|
418
|
+
eventsProcessed: cp.eventsProcessed,
|
|
419
|
+
newProgressBlockNumber: cp.blockNumber - 1 | 0
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
return Object.values(byChain).map(acc => ({
|
|
424
|
+
chain_id: acc.chainId,
|
|
425
|
+
events_processed_diff: acc.eventsProcessed.toString(),
|
|
426
|
+
new_progress_block_number: acc.newProgressBlockNumber
|
|
427
|
+
}));
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function toStorage(state, config) {
|
|
431
|
+
return {
|
|
432
|
+
name: "memory",
|
|
433
|
+
isInitialized: async () => state.isInitialized,
|
|
434
|
+
initialize: async (chainConfigsOpt, entitiesOpt, $staropt$star, envioInfo) => {
|
|
435
|
+
let chainConfigs = chainConfigsOpt !== undefined ? chainConfigsOpt : [];
|
|
436
|
+
let entities = entitiesOpt !== undefined ? entitiesOpt : [];
|
|
437
|
+
$staropt$star !== undefined;
|
|
438
|
+
registerEntities(state, entities);
|
|
439
|
+
seedIndexingAddresses(state, chainConfigs);
|
|
440
|
+
chainConfigs.forEach(chainConfig => {
|
|
441
|
+
state.chains[ChainId.toString(chainConfig.id)] = {
|
|
442
|
+
id: chainConfig.id,
|
|
443
|
+
startBlock: chainConfig.startBlock,
|
|
444
|
+
endBlock: chainConfig.endBlock,
|
|
445
|
+
maxReorgDepth: chainConfig.maxReorgDepth,
|
|
446
|
+
progressBlockNumber: -1,
|
|
447
|
+
sourceBlockNumber: 0,
|
|
448
|
+
numEventsProcessed: 0,
|
|
449
|
+
firstEventBlockNumber: undefined,
|
|
450
|
+
latestFetchedBlockNumber: 0,
|
|
451
|
+
timestampCaughtUpToHeadOrEndblock: undefined
|
|
452
|
+
};
|
|
453
|
+
});
|
|
454
|
+
state.envioInfo = envioInfo;
|
|
455
|
+
state.isInitialized = true;
|
|
456
|
+
return toInitialState(state, true);
|
|
457
|
+
},
|
|
458
|
+
resumeInitialState: async () => toInitialState(state, false),
|
|
459
|
+
loadOrThrow: async (filter, table) => handleLoad(state, table.tableName, filter),
|
|
460
|
+
ensureQueryIndexes: async (param, param$1) => {},
|
|
461
|
+
ensureSchemaIndexes: async param => {},
|
|
462
|
+
finalizeBackfill: async (param, chainIds, readyAt) => {
|
|
463
|
+
chainIds.forEach(chainId => {
|
|
464
|
+
let chain = state.chains[ChainId.toString(chainId)];
|
|
465
|
+
if (chain !== undefined) {
|
|
466
|
+
chain.timestampCaughtUpToHeadOrEndblock = readyAt;
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
dumpEffectCache: async () => {},
|
|
472
|
+
reset: async () => {
|
|
473
|
+
let clear = dict => {
|
|
474
|
+
Object.keys(dict).forEach(key => Utils.Dict.deleteInPlace(dict, key));
|
|
475
|
+
};
|
|
476
|
+
clear(state.entities);
|
|
477
|
+
clear(state.history);
|
|
478
|
+
clear(state.chains);
|
|
479
|
+
clear(state.effectCache);
|
|
480
|
+
clear(state.cache);
|
|
481
|
+
state.checkpoints = [];
|
|
482
|
+
state.envioInfo = undefined;
|
|
483
|
+
state.isInitialized = false;
|
|
484
|
+
},
|
|
485
|
+
setChainMeta: async chainsData => {
|
|
486
|
+
Object.entries(chainsData).forEach(param => {
|
|
487
|
+
let meta = param[1];
|
|
488
|
+
let chain = state.chains[param[0]];
|
|
489
|
+
if (chain !== undefined) {
|
|
490
|
+
chain.firstEventBlockNumber = Primitive_option.fromNull(meta.first_event_block);
|
|
491
|
+
chain.latestFetchedBlockNumber = meta.buffer_block;
|
|
492
|
+
chain.timestampCaughtUpToHeadOrEndblock = Primitive_option.fromNull(meta.ready_at);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
return undefined;
|
|
497
|
+
},
|
|
498
|
+
pruneStaleCheckpoints: async safeCheckpointId => {
|
|
499
|
+
state.checkpoints = state.checkpoints.filter(cp => cp.id >= safeCheckpointId);
|
|
500
|
+
},
|
|
501
|
+
pruneStaleEntityHistory: async (entityName, param, param$1, safeCheckpointId) => {
|
|
502
|
+
let rows = state.history[entityName];
|
|
503
|
+
if (rows === undefined) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
let newestBelow = {};
|
|
507
|
+
rows.forEach(row => {
|
|
508
|
+
if (row.checkpointId >= safeCheckpointId) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
let key = rowKey(row.scope, row.entityId);
|
|
512
|
+
let current = newestBelow[key];
|
|
513
|
+
if (current !== undefined && current.checkpointId >= row.checkpointId) {
|
|
514
|
+
return;
|
|
515
|
+
} else {
|
|
516
|
+
newestBelow[key] = row;
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
state.history[entityName] = rows.filter(row => {
|
|
521
|
+
let key = rowKey(row.scope, row.entityId);
|
|
522
|
+
if (row.checkpointId >= safeCheckpointId) {
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
let kept = newestBelow[key];
|
|
526
|
+
if (kept !== undefined) {
|
|
527
|
+
return kept === row;
|
|
528
|
+
} else {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
},
|
|
533
|
+
getRollbackTargetCheckpoint: async (reorgChainId, lastKnownValidBlockNumber) => Stdlib_Array.reduce(state.checkpoints, undefined, (target, cp) => {
|
|
534
|
+
if (Primitive_object.equal(cp.chainId, reorgChainId) && cp.blockNumber <= lastKnownValidBlockNumber && !(target !== undefined && target >= cp.id)) {
|
|
535
|
+
return cp.id;
|
|
536
|
+
} else {
|
|
537
|
+
return target;
|
|
538
|
+
}
|
|
539
|
+
}),
|
|
540
|
+
getRollbackProgressDiff: async rollbackTargetCheckpointId => getRollbackProgressDiff(state, rollbackTargetCheckpointId),
|
|
541
|
+
getRollbackData: async (entityConfig, rollbackTargetCheckpointId) => getRollbackData(state, entityConfig, rollbackTargetCheckpointId),
|
|
542
|
+
writeBatch: async (batch, rollback, isInReorgThreshold, param, param$1, updatedEffectsCache, updatedEntities, chainMetaData, param$2) => writeBatch(state, batch, rollback, isInReorgThreshold, config, updatedEntities, updatedEffectsCache, chainMetaData),
|
|
543
|
+
close: async () => {}
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function currentRows(state, entityConfig) {
|
|
548
|
+
return Object.values(Stdlib_Option.getOr(state.entities[entityConfig.name], {}));
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function historyChanges(state, entityConfig) {
|
|
552
|
+
return Stdlib_Option.getOr(state.history[entityConfig.name], []).map(row => {
|
|
553
|
+
let match = row.action;
|
|
554
|
+
if (match === "SET") {
|
|
555
|
+
return {
|
|
556
|
+
type: "SET",
|
|
557
|
+
entityId: row.entityId,
|
|
558
|
+
entity: Stdlib_Option.getOr(row.entity, {}),
|
|
559
|
+
checkpointId: row.checkpointId
|
|
560
|
+
};
|
|
561
|
+
} else {
|
|
562
|
+
return {
|
|
563
|
+
type: "DELETE",
|
|
564
|
+
entityId: row.entityId,
|
|
565
|
+
checkpointId: row.checkpointId
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
}).toSorted((a, b) => {
|
|
569
|
+
let order = Primitive_string.compare(EntityId.toKey(a.entityId), EntityId.toKey(b.entityId));
|
|
570
|
+
if (order !== 0) {
|
|
571
|
+
return order;
|
|
572
|
+
} else {
|
|
573
|
+
return Primitive_float.compare(Number(a.checkpointId), Number(b.checkpointId));
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function checkpointRows(state) {
|
|
579
|
+
return state.checkpoints.map(cp => ({
|
|
580
|
+
id: cp.id,
|
|
581
|
+
chain_id: cp.chainId,
|
|
582
|
+
block_number: cp.blockNumber,
|
|
583
|
+
block_hash: Stdlib_Null.fromOption(cp.blockHash),
|
|
584
|
+
events_processed: cp.eventsProcessed
|
|
585
|
+
}));
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function effectCacheRows(state, tableName) {
|
|
589
|
+
return Object.values(Stdlib_Option.getOr(state.effectCache[tableName], {})).map(item => ({
|
|
590
|
+
id: item.id,
|
|
591
|
+
output: item.output
|
|
592
|
+
}));
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export {
|
|
596
|
+
make,
|
|
597
|
+
rowKey,
|
|
598
|
+
historyKey,
|
|
599
|
+
readChainId,
|
|
600
|
+
copyEntity,
|
|
601
|
+
getEntityDict,
|
|
602
|
+
getHistory,
|
|
603
|
+
registerEntities,
|
|
604
|
+
seedIndexingAddresses,
|
|
605
|
+
toIndexingAddress,
|
|
606
|
+
getIndexingAddressesByChain,
|
|
607
|
+
toInitialChainStates,
|
|
608
|
+
committedCheckpointId,
|
|
609
|
+
reorgCheckpoints,
|
|
610
|
+
toInitialState,
|
|
611
|
+
handleLoad,
|
|
612
|
+
backfillHistory,
|
|
613
|
+
applyRollback,
|
|
614
|
+
writeBatch,
|
|
615
|
+
getRollbackData,
|
|
616
|
+
getRollbackProgressDiff,
|
|
617
|
+
toStorage,
|
|
618
|
+
currentRows,
|
|
619
|
+
historyChanges,
|
|
620
|
+
checkpointRows,
|
|
621
|
+
effectCacheRows,
|
|
622
|
+
}
|
|
623
|
+
/* Table Not a pure module */
|
package/src/Persistence.res
CHANGED
|
@@ -147,11 +147,13 @@ type storage = {
|
|
|
147
147
|
"new_progress_block_number": int,
|
|
148
148
|
}>,
|
|
149
149
|
>,
|
|
150
|
-
//
|
|
150
|
+
// Rollback data for an entity, as decoded entities rather than storage rows:
|
|
151
|
+
// only the storage knows how it encoded them, so each one decodes its own
|
|
152
|
+
// before handing them back.
|
|
151
153
|
getRollbackData: (
|
|
152
154
|
~entityConfig: Internal.entityConfig,
|
|
153
155
|
~rollbackTargetCheckpointId: Internal.checkpointId,
|
|
154
|
-
) => promise<(array<rollbackRemoval>, array<
|
|
156
|
+
) => promise<(array<rollbackRemoval>, array<Internal.entity>)>,
|
|
155
157
|
// Write batch to storage
|
|
156
158
|
writeBatch: (
|
|
157
159
|
~batch: Batch.t,
|