univo 0.3.3 → 0.4.1
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/dist/cli.mjs +2 -2
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +484 -416
- package/dist/proxy.d.mts +20 -0
- package/dist/proxy.d.ts +20 -0
- package/dist/proxy.mjs +90 -0
- package/dist/realtime.d.mts +4 -5
- package/dist/realtime.d.ts +4 -5
- package/dist/realtime.mjs +324 -106
- package/dist/rpc.d.mts +3 -326
- package/dist/rpc.d.ts +3 -326
- package/dist/shared/univo.BcZ0twA5.d.mts +359 -0
- package/dist/shared/univo.C0flyVee.mjs +21 -0
- package/dist/shared/univo.CNIJRI7c.d.ts +359 -0
- package/dist/shared/univo.DD1x3F1G.mjs +162 -0
- package/dist/transport.d.mts +1 -39
- package/dist/transport.d.ts +1 -39
- package/dist/transport.mjs +233 -4
- package/package.json +5 -1
- package/dist/shared/univo.BCKcG-WP.mjs +0 -95
- package/dist/shared/univo.DObJAnN6.mjs +0 -238
package/dist/index.mjs
CHANGED
|
@@ -1,100 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { c as createLogger,
|
|
1
|
+
import { hexToNumber, numberToHex, hexToBytes, keccak256, toRlp, hexToBigInt, formatTransaction, serializeTransaction, numberToBytes, isHex, concatBytes } from 'viem';
|
|
2
|
+
import { local } from './transport.mjs';
|
|
3
|
+
import { c as createLogger, r as retry, v as version, n as normalizeHex, d as decompress, i as isHexEqual, a as compress, b as iife, e as decoder } from './shared/univo.DD1x3F1G.mjs';
|
|
4
4
|
import { AdapterError } from './metadata/adapters.mjs';
|
|
5
|
+
import { c as createException, a as catchException } from './shared/univo.C0flyVee.mjs';
|
|
5
6
|
import 'ws';
|
|
6
7
|
import 'partysocket';
|
|
7
8
|
|
|
8
|
-
function createServer(opts) {
|
|
9
|
-
const log = createLogger({ quiet: opts.quiet, prefix: "[server]" });
|
|
10
|
-
const http = async (req) => {
|
|
11
|
-
let body_buffer;
|
|
12
|
-
try {
|
|
13
|
-
body_buffer = await req.arrayBuffer();
|
|
14
|
-
} catch {
|
|
15
|
-
return Response.json(
|
|
16
|
-
{ jsonrpc: "2.0", id: null, error: { code: 0, message: "Invalid request body" } },
|
|
17
|
-
//
|
|
18
|
-
{ status: 400 }
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
const authorization = req.headers.get("Authorization");
|
|
22
|
-
const authenticated = authorization === `Bearer ${opts.signingKey}`;
|
|
23
|
-
let body_string;
|
|
24
|
-
if (req.headers.get("Content-Encoding") === "gzip") {
|
|
25
|
-
if (!authorization) {
|
|
26
|
-
return Response.json(
|
|
27
|
-
{ jsonrpc: "2.0", id: null, error: { code: 0, message: "No bearer token provided" } },
|
|
28
|
-
//
|
|
29
|
-
{ status: 400 }
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
if (!authenticated) {
|
|
33
|
-
return Response.json(
|
|
34
|
-
{ jsonrpc: "2.0", id: null, error: { code: 0, message: "Invalid bearer token" } },
|
|
35
|
-
//
|
|
36
|
-
{ status: 400 }
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
body_string = await decompress(body_buffer);
|
|
40
|
-
} else {
|
|
41
|
-
body_string = decoder.decode(body_buffer);
|
|
42
|
-
}
|
|
43
|
-
let json;
|
|
44
|
-
try {
|
|
45
|
-
json = JSON.parse(body_string);
|
|
46
|
-
} catch {
|
|
47
|
-
return Response.json(
|
|
48
|
-
{ jsonrpc: "2.0", id: null, error: { code: 0, message: "Malformed JSON request body" } },
|
|
49
|
-
//
|
|
50
|
-
{ status: 400 }
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
if (json.method.startsWith("private_")) {
|
|
54
|
-
if (!authorization) {
|
|
55
|
-
return Response.json(
|
|
56
|
-
{ jsonrpc: "2.0", id: json.id, error: { code: 0, message: "No bearer token provided" } },
|
|
57
|
-
//
|
|
58
|
-
{ status: 400 }
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
if (authorization !== `Bearer ${opts.signingKey}`) {
|
|
62
|
-
return Response.json(
|
|
63
|
-
{ jsonrpc: "2.0", id: json.id, error: { code: 0, message: "Invalid bearer token" } },
|
|
64
|
-
//
|
|
65
|
-
{ status: 400 }
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
try {
|
|
70
|
-
const result = await opts.transport.request({ method: json.method, params: json.params });
|
|
71
|
-
return Response.json({ jsonrpc: "2.0", id: json.id, result });
|
|
72
|
-
} catch (error) {
|
|
73
|
-
const message = getException(error);
|
|
74
|
-
if (message) {
|
|
75
|
-
log.error(message);
|
|
76
|
-
return Response.json(
|
|
77
|
-
{ jsonrpc: "2.0", id: json.id, error: { code: 0, message } },
|
|
78
|
-
//
|
|
79
|
-
{ status: 400 }
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
if (error instanceof Error) {
|
|
83
|
-
log.error(error.message);
|
|
84
|
-
}
|
|
85
|
-
return Response.json(
|
|
86
|
-
{ jsonrpc: "2.0", id: json.id, error: { code: 0, message: "Internal server error" } },
|
|
87
|
-
//
|
|
88
|
-
{ status: 500 }
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
return {
|
|
93
|
-
http
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const SUPPORTED_TRANSACTION_TYPES = /* @__PURE__ */ new Set([0n, 1n, 2n, 3n, 4n]);
|
|
98
9
|
function matchFilter(block, filter) {
|
|
99
10
|
if (!chainValid(block, filter)) return false;
|
|
100
11
|
if (!toBlockValid(block, filter)) return false;
|
|
@@ -104,16 +15,16 @@ function matchFilter(block, filter) {
|
|
|
104
15
|
return true;
|
|
105
16
|
}
|
|
106
17
|
const chainValid = (block, filter) => {
|
|
107
|
-
if (
|
|
18
|
+
if (hexToNumber(block.eth_chainId) === filter.chain) return true;
|
|
108
19
|
return false;
|
|
109
20
|
};
|
|
110
21
|
const fromBlockValid = (block, filter) => {
|
|
111
|
-
if (
|
|
22
|
+
if (hexToNumber(block.eth_getBlockByNumber.number) >= filter.fromBlock) return true;
|
|
112
23
|
return false;
|
|
113
24
|
};
|
|
114
25
|
const toBlockValid = (block, filter) => {
|
|
115
26
|
if (filter.toBlock === void 0) return true;
|
|
116
|
-
if (
|
|
27
|
+
if (hexToNumber(block.eth_getBlockByNumber.number) <= filter.toBlock) return true;
|
|
117
28
|
return false;
|
|
118
29
|
};
|
|
119
30
|
const includesLogAddress = (block, filter) => {
|
|
@@ -134,18 +45,82 @@ const includesLogEvent = (block, filter) => {
|
|
|
134
45
|
}
|
|
135
46
|
return false;
|
|
136
47
|
};
|
|
48
|
+
function defineEvent(event) {
|
|
49
|
+
return event;
|
|
50
|
+
}
|
|
51
|
+
function defineAction(action) {
|
|
52
|
+
return action;
|
|
53
|
+
}
|
|
137
54
|
function defineIndexer(opts) {
|
|
138
|
-
const log = createLogger({ quiet: opts.quiet
|
|
139
|
-
const allEvents = [];
|
|
55
|
+
const log = createLogger({ quiet: opts.quiet, prefix: "[indexer]" });
|
|
56
|
+
const allEvents = [...opts.events];
|
|
140
57
|
const eventsGroupedByStorageMap = /* @__PURE__ */ new Map();
|
|
141
|
-
const
|
|
58
|
+
for (const event of allEvents) {
|
|
59
|
+
if (!/^[A-Za-z0-9_-]+$/.test(event.id)) {
|
|
60
|
+
throw new Error(`Invalid event id \`${event.id}\`. Only characters A-Z, a-z, 0-9, underscores, and hyphens are permitted.`);
|
|
61
|
+
}
|
|
62
|
+
const group = eventsGroupedByStorageMap.get(event.storage) ?? [];
|
|
63
|
+
group.push(event);
|
|
64
|
+
eventsGroupedByStorageMap.set(event.storage, group);
|
|
65
|
+
}
|
|
66
|
+
const allActions = [...opts.actions];
|
|
67
|
+
const actionIds = /* @__PURE__ */ new Set();
|
|
68
|
+
for (const action of allActions) {
|
|
69
|
+
if (!/^[A-Za-z0-9_-]+$/.test(action.id)) {
|
|
70
|
+
throw new Error(`Invalid action id \`${action.id}\`. Only characters A-Z, a-z, 0-9, underscores, and hyphens are permitted.`);
|
|
71
|
+
}
|
|
72
|
+
if (actionIds.has(action.id)) {
|
|
73
|
+
throw new Error(`Duplicate action id \`${action.id}\`.`);
|
|
74
|
+
}
|
|
75
|
+
actionIds.add(action.id);
|
|
76
|
+
}
|
|
77
|
+
const public_getMetadata = async () => {
|
|
78
|
+
return {
|
|
79
|
+
chains: Array.from(
|
|
80
|
+
new Set(
|
|
81
|
+
allEvents.flatMap((event) => {
|
|
82
|
+
return event.filters.flatMap((filter) => {
|
|
83
|
+
return numberToHex(filter.chain);
|
|
84
|
+
});
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
const public_getFinalizedHeight = async (chain) => {
|
|
91
|
+
const manifest = await getOrInitManifest(chain);
|
|
92
|
+
return manifest.finalized_block_height;
|
|
93
|
+
};
|
|
94
|
+
async function getOrInitManifest(chain) {
|
|
95
|
+
const path = `manifest/v1/${normalizeHex(chain)}`;
|
|
96
|
+
const manifestRes = await opts.metadataStorage.adapter.get(path);
|
|
97
|
+
let manifest;
|
|
98
|
+
if (manifestRes === null) {
|
|
99
|
+
const block = await getBlockFromChain({ chain, number: "finalized" });
|
|
100
|
+
const chainFinalizedHeight = hexToNumber(block.eth_getBlockByNumber.number);
|
|
101
|
+
const newManifest = {
|
|
102
|
+
finalized_block_height: chainFinalizedHeight,
|
|
103
|
+
finalized_block_hash: block.eth_getBlockByNumber.hash,
|
|
104
|
+
next_finalized_height: chainFinalizedHeight,
|
|
105
|
+
updated_at: Date.now()
|
|
106
|
+
};
|
|
107
|
+
await opts.metadataStorage.adapter.put(path, JSON.stringify(newManifest), { ifNoneMatch: "*" });
|
|
108
|
+
manifest = newManifest;
|
|
109
|
+
} else {
|
|
110
|
+
manifest = JSON.parse(decoder.decode(manifestRes.body));
|
|
111
|
+
}
|
|
112
|
+
return manifest;
|
|
113
|
+
}
|
|
142
114
|
async function getBlockFromChain(head) {
|
|
143
115
|
return await retry(() => retry_getBlockFromChain(head), 2);
|
|
144
116
|
}
|
|
145
117
|
async function retry_getBlockFromChain(head) {
|
|
146
118
|
const block = await opts.getBlock({ chain: head.chain, number: head.number });
|
|
147
|
-
if (block === null) {
|
|
148
|
-
throw new Error("
|
|
119
|
+
if (block.eth_getBlockByNumber === null) {
|
|
120
|
+
throw new Error("eth_getBlockByNumber returned null response");
|
|
121
|
+
}
|
|
122
|
+
if (block.eth_getBlockReceipts === null) {
|
|
123
|
+
throw new Error("eth_getBlockReceipts returned null response");
|
|
149
124
|
}
|
|
150
125
|
if (typeof head.hash === "string") {
|
|
151
126
|
if (!isHexEqual(head.hash, block.eth_getBlockByNumber.hash)) {
|
|
@@ -157,6 +132,7 @@ function defineIndexer(opts) {
|
|
|
157
132
|
throw new Error("Block returned unexpected parent hash");
|
|
158
133
|
}
|
|
159
134
|
}
|
|
135
|
+
verifyBlockHash(block);
|
|
160
136
|
verifyBlockHashes(block);
|
|
161
137
|
verifyLogIndicies(block);
|
|
162
138
|
verifyReceiptsRoot(block);
|
|
@@ -165,6 +141,48 @@ function defineIndexer(opts) {
|
|
|
165
141
|
verifyTransactionGasUsage(block);
|
|
166
142
|
return block;
|
|
167
143
|
}
|
|
144
|
+
function verifyBlockHash(block) {
|
|
145
|
+
const header = block.eth_getBlockByNumber;
|
|
146
|
+
const fields = [
|
|
147
|
+
hexToBytes(header.parentHash),
|
|
148
|
+
hexToBytes(header.sha3Uncles),
|
|
149
|
+
hexToBytes(header.miner),
|
|
150
|
+
hexToBytes(header.stateRoot),
|
|
151
|
+
hexToBytes(header.transactionsRoot),
|
|
152
|
+
hexToBytes(header.receiptsRoot),
|
|
153
|
+
hexToBytes(header.logsBloom),
|
|
154
|
+
quantityToBytes(header.difficulty),
|
|
155
|
+
quantityToBytes(header.number),
|
|
156
|
+
quantityToBytes(header.gasLimit),
|
|
157
|
+
quantityToBytes(header.gasUsed),
|
|
158
|
+
quantityToBytes(header.timestamp),
|
|
159
|
+
hexToBytes(header.extraData),
|
|
160
|
+
hexToBytes(header.mixHash),
|
|
161
|
+
hexToBytes(header.nonce)
|
|
162
|
+
];
|
|
163
|
+
const extensions = [
|
|
164
|
+
[header.baseFeePerGas, quantityToBytes],
|
|
165
|
+
[header.withdrawalsRoot, hexToBytes],
|
|
166
|
+
[header.blobGasUsed, quantityToBytes],
|
|
167
|
+
[header.excessBlobGas, quantityToBytes],
|
|
168
|
+
[header.parentBeaconBlockRoot, hexToBytes],
|
|
169
|
+
[header.requestsHash, hexToBytes]
|
|
170
|
+
];
|
|
171
|
+
let missingField = false;
|
|
172
|
+
for (const [value, encode] of extensions) {
|
|
173
|
+
if (value == null) {
|
|
174
|
+
missingField = true;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (missingField) {
|
|
178
|
+
throw new Error("Block returned incomplete block header");
|
|
179
|
+
}
|
|
180
|
+
fields.push(encode(value));
|
|
181
|
+
}
|
|
182
|
+
if (!isHexEqual(header.hash, keccak256(toRlp(fields, "bytes")))) {
|
|
183
|
+
throw new Error("Block returned header that does not match the block hash");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
168
186
|
function verifyBlockHashes(block) {
|
|
169
187
|
const blockHash = block.eth_getBlockByNumber.hash;
|
|
170
188
|
const transactions = block.eth_getBlockByNumber.transactions;
|
|
@@ -177,7 +195,7 @@ function defineIndexer(opts) {
|
|
|
177
195
|
if (!isHexEqual(block.eth_getBlockByNumber.hash, receipt.blockHash)) {
|
|
178
196
|
throw new Error("Block returned receipt with unexpected block hash");
|
|
179
197
|
}
|
|
180
|
-
const transactionIndex =
|
|
198
|
+
const transactionIndex = hexToNumber(receipt.transactionIndex);
|
|
181
199
|
const transaction = transactions[transactionIndex];
|
|
182
200
|
if (transaction === void 0 || !isHexEqual(transaction.hash, receipt.transactionHash)) {
|
|
183
201
|
throw new Error("Block returned receipt with unexpected transaction hash");
|
|
@@ -196,7 +214,7 @@ function defineIndexer(opts) {
|
|
|
196
214
|
let expectedLogIndex = 0n;
|
|
197
215
|
for (const receipt of block.eth_getBlockReceipts) {
|
|
198
216
|
for (const entry of receipt.logs) {
|
|
199
|
-
if (
|
|
217
|
+
if (hexToBigInt(entry.logIndex) !== expectedLogIndex) {
|
|
200
218
|
throw new Error("Block returned non-contiguous log indices");
|
|
201
219
|
}
|
|
202
220
|
expectedLogIndex++;
|
|
@@ -211,17 +229,17 @@ function defineIndexer(opts) {
|
|
|
211
229
|
}
|
|
212
230
|
function calculateTrieRoot(values) {
|
|
213
231
|
if (values.length === 0) {
|
|
214
|
-
return
|
|
232
|
+
return keccak256(toRlp(new Uint8Array(), "bytes"));
|
|
215
233
|
}
|
|
216
234
|
const entries = values.map((value, index) => ({
|
|
217
|
-
key: bytesToNibbles(
|
|
235
|
+
key: bytesToNibbles(toRlp(quantityToBytes(BigInt(index)), "bytes")),
|
|
218
236
|
value
|
|
219
237
|
}));
|
|
220
|
-
return
|
|
238
|
+
return keccak256(toRlp(encodeTrieNode(entries), "bytes"));
|
|
221
239
|
}
|
|
222
240
|
function trieNodeReference(node) {
|
|
223
|
-
const encoded =
|
|
224
|
-
return encoded.length < 32 ? node :
|
|
241
|
+
const encoded = toRlp(node, "bytes");
|
|
242
|
+
return encoded.length < 32 ? node : hexToBytes(keccak256(encoded));
|
|
225
243
|
}
|
|
226
244
|
function encodeTrieNode(entries, depth = 0) {
|
|
227
245
|
if (entries.length === 1) {
|
|
@@ -259,30 +277,31 @@ function defineIndexer(opts) {
|
|
|
259
277
|
return bytes;
|
|
260
278
|
}
|
|
261
279
|
function quantityToBytes(value) {
|
|
262
|
-
const number = typeof value === "bigint" ? value :
|
|
263
|
-
return number === 0n ? new Uint8Array() :
|
|
280
|
+
const number = typeof value === "bigint" ? value : hexToBigInt(value);
|
|
281
|
+
return number === 0n ? new Uint8Array() : numberToBytes(number);
|
|
264
282
|
}
|
|
265
283
|
function bytesToNibbles(value) {
|
|
266
284
|
return Array.from(value).flatMap((byte) => [byte >> 4, byte & 15]);
|
|
267
285
|
}
|
|
268
286
|
function serializeReceipt(receipt) {
|
|
269
|
-
const outcome = receipt.status === void 0 ?
|
|
287
|
+
const outcome = receipt.status === void 0 ? hexToBytes(receipt.root) : quantityToBytes(receipt.status);
|
|
270
288
|
const fields = [
|
|
271
289
|
outcome,
|
|
272
290
|
quantityToBytes(receipt.cumulativeGasUsed),
|
|
273
|
-
|
|
274
|
-
receipt.logs.map((log2) => [
|
|
291
|
+
hexToBytes(receipt.logsBloom),
|
|
292
|
+
receipt.logs.map((log2) => [hexToBytes(log2.address), log2.topics.map(hexToBytes), hexToBytes(log2.data)])
|
|
275
293
|
];
|
|
276
|
-
const encoded =
|
|
277
|
-
if (!
|
|
294
|
+
const encoded = toRlp(fields, "bytes");
|
|
295
|
+
if (!isHex(receipt.type)) {
|
|
278
296
|
throw new Error(`Unsupported receipt type ${receipt.type}`);
|
|
279
297
|
}
|
|
280
|
-
const type =
|
|
281
|
-
return type === 0n ? encoded :
|
|
298
|
+
const type = hexToBigInt(receipt.type);
|
|
299
|
+
return type === 0n ? encoded : concatBytes([Uint8Array.of(Number(type)), encoded]);
|
|
282
300
|
}
|
|
301
|
+
const SUPPORTED_TRANSACTION_TYPES = /* @__PURE__ */ new Set([0n, 1n, 2n, 3n, 4n]);
|
|
283
302
|
function verifyTransactionsRoot(block) {
|
|
284
303
|
const unsupportedTxs = block.eth_getBlockByNumber.transactions.filter((tx) => {
|
|
285
|
-
return !SUPPORTED_TRANSACTION_TYPES.has(
|
|
304
|
+
return !SUPPORTED_TRANSACTION_TYPES.has(hexToBigInt(tx.type));
|
|
286
305
|
});
|
|
287
306
|
for (const tx of unsupportedTxs) {
|
|
288
307
|
log.warn(`Encountered unsupported tx type for ${tx.hash} on ${block.eth_chainId}`);
|
|
@@ -291,20 +310,20 @@ function defineIndexer(opts) {
|
|
|
291
310
|
return log.warn("Unable to verify block `transactionsRoot` because of unsupported tx type");
|
|
292
311
|
}
|
|
293
312
|
const transactions = block.eth_getBlockByNumber.transactions.map((tx) => {
|
|
294
|
-
const { input, ...formatted } =
|
|
313
|
+
const { input, ...formatted } = formatTransaction(tx);
|
|
295
314
|
if (formatted.type === "legacy" && (formatted.v === 27n || formatted.v === 28n)) {
|
|
296
315
|
formatted.chainId = void 0;
|
|
297
316
|
}
|
|
298
317
|
const signature = formatted.type === "legacy" ? { r: formatted.r, s: formatted.s, v: formatted.v } : { r: formatted.r, s: formatted.s, yParity: formatted.yParity };
|
|
299
|
-
const serialized =
|
|
318
|
+
const serialized = serializeTransaction(
|
|
300
319
|
{ ...formatted, data: input },
|
|
301
320
|
//
|
|
302
321
|
signature
|
|
303
322
|
);
|
|
304
|
-
if (!isHexEqual(tx.hash,
|
|
323
|
+
if (!isHexEqual(tx.hash, keccak256(serialized))) {
|
|
305
324
|
throw new Error(`Transaction ${tx.hash} failed serialization check`);
|
|
306
325
|
}
|
|
307
|
-
return
|
|
326
|
+
return hexToBytes(serialized);
|
|
308
327
|
});
|
|
309
328
|
const transactionsRoot = calculateTrieRoot(transactions);
|
|
310
329
|
if (!isHexEqual(block.eth_getBlockByNumber.transactionsRoot, transactionsRoot)) {
|
|
@@ -319,29 +338,29 @@ function defineIndexer(opts) {
|
|
|
319
338
|
}
|
|
320
339
|
for (let index = 0; index < transactions.length; index++) {
|
|
321
340
|
const expectedIndex = BigInt(index);
|
|
322
|
-
if (
|
|
341
|
+
if (hexToBigInt(transactions[index].transactionIndex) !== expectedIndex) {
|
|
323
342
|
throw new Error("Block returned non-contiguous transaction indices");
|
|
324
343
|
}
|
|
325
|
-
if (
|
|
344
|
+
if (hexToBigInt(receipts[index].transactionIndex) !== expectedIndex) {
|
|
326
345
|
throw new Error("Block returned non-contiguous transaction indices");
|
|
327
346
|
}
|
|
328
347
|
for (const entry of receipts[index].logs) {
|
|
329
|
-
if (
|
|
348
|
+
if (hexToBigInt(entry.transactionIndex) !== expectedIndex) {
|
|
330
349
|
throw new Error("Block returned log with unexpected transaction index");
|
|
331
350
|
}
|
|
332
351
|
}
|
|
333
352
|
}
|
|
334
353
|
}
|
|
335
354
|
function verifyTransactionGasUsage(block) {
|
|
336
|
-
const gasLimit =
|
|
337
|
-
const blockGasUsed =
|
|
355
|
+
const gasLimit = hexToBigInt(block.eth_getBlockByNumber.gasLimit);
|
|
356
|
+
const blockGasUsed = hexToBigInt(block.eth_getBlockByNumber.gasUsed);
|
|
338
357
|
if (blockGasUsed > gasLimit) {
|
|
339
358
|
throw new Error("Block returned gas used greater than the block gas limit");
|
|
340
359
|
}
|
|
341
360
|
let cumulativeGasUsed = 0n;
|
|
342
361
|
for (const receipt of block.eth_getBlockReceipts) {
|
|
343
|
-
cumulativeGasUsed +=
|
|
344
|
-
if (
|
|
362
|
+
cumulativeGasUsed += hexToBigInt(receipt.gasUsed);
|
|
363
|
+
if (hexToBigInt(receipt.cumulativeGasUsed) !== cumulativeGasUsed) {
|
|
345
364
|
throw new Error("Block returned inconsistent cumulative gas used");
|
|
346
365
|
}
|
|
347
366
|
if (cumulativeGasUsed > gasLimit) {
|
|
@@ -352,59 +371,70 @@ function defineIndexer(opts) {
|
|
|
352
371
|
throw new Error("Block returned inconsistent gas used");
|
|
353
372
|
}
|
|
354
373
|
}
|
|
355
|
-
async
|
|
356
|
-
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
};
|
|
368
|
-
await opts.metadataStorage.adapter.put(path, JSON.stringify(newManifest), { ifNoneMatch: "*" });
|
|
369
|
-
manifest = newManifest;
|
|
370
|
-
} else {
|
|
371
|
-
manifest = JSON.parse(decoder.decode(manifestRes.body));
|
|
374
|
+
const public_writeUnfinalizedHead = async (head) => {
|
|
375
|
+
log.debug("Received unfinalized head...");
|
|
376
|
+
const blocksStart = Date.now();
|
|
377
|
+
const [block, manifest] = await Promise.all([
|
|
378
|
+
getBlockFromChain(head),
|
|
379
|
+
//
|
|
380
|
+
getOrInitManifest(head.chain)
|
|
381
|
+
]);
|
|
382
|
+
log.debug(`Loaded block in ${Date.now() - blocksStart}ms`);
|
|
383
|
+
const indexerFinalizedHeight = manifest.finalized_block_height;
|
|
384
|
+
if (hexToNumber(head.number) <= indexerFinalizedHeight) {
|
|
385
|
+
return log.debug("Receiving finalized head, ignoring...");
|
|
372
386
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const
|
|
377
|
-
|
|
387
|
+
const metadata_start = Date.now();
|
|
388
|
+
const chain = normalizeHex(block.eth_chainId);
|
|
389
|
+
const hash = normalizeHex(block.eth_getBlockByNumber.hash);
|
|
390
|
+
const number = normalizeHex(block.eth_getBlockByNumber.number, 16);
|
|
391
|
+
const parentHash = normalizeHex(block.eth_getBlockByNumber.parentHash);
|
|
392
|
+
const blocksKey = `blocks/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
393
|
+
const blocksValue = await compress(JSON.stringify(block));
|
|
394
|
+
const conditional = { ifNoneMatch: "*" };
|
|
395
|
+
const result = await opts.metadataStorage.adapter.put(blocksKey, blocksValue, conditional).catch((error) => {
|
|
396
|
+
if (error instanceof AdapterError) {
|
|
397
|
+
if (error.tag === "PreconditionFailed") {
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
throw error;
|
|
402
|
+
});
|
|
403
|
+
if (result === null) {
|
|
404
|
+
return log.debug("Block already persisted to wal, ignoring...");
|
|
405
|
+
}
|
|
406
|
+
log.debug(`Persisted block to wal in ${Date.now() - metadata_start}ms`);
|
|
407
|
+
await writeUnfinalizedBlock(block);
|
|
378
408
|
};
|
|
379
409
|
async function writeUnfinalizedBlock(block) {
|
|
380
410
|
const events_start = Date.now();
|
|
381
411
|
const events = eventsGroupedByStorageMap.entries().map(async ([storage, grouped_events]) => {
|
|
382
412
|
const batch = [];
|
|
383
|
-
for (const
|
|
413
|
+
for (const event of grouped_events) {
|
|
384
414
|
try {
|
|
385
|
-
if (!
|
|
386
|
-
log.debug(`Block matches no filters for event ${
|
|
415
|
+
if (!event.filters.some((filter) => matchFilter(block, filter))) {
|
|
416
|
+
log.debug(`Block matches no filters for event ${event.id}`);
|
|
387
417
|
continue;
|
|
388
418
|
}
|
|
389
|
-
const events2 =
|
|
390
|
-
for (const
|
|
391
|
-
batch.push(
|
|
419
|
+
const events2 = event.handler(block);
|
|
420
|
+
for (const event2 of events2) {
|
|
421
|
+
batch.push(event2);
|
|
392
422
|
}
|
|
393
423
|
} catch (error) {
|
|
394
|
-
log.error(`Failed to run your 'handler' for event ${
|
|
424
|
+
log.error(`Failed to run your 'handler' for event ${event.id}`);
|
|
395
425
|
throw error;
|
|
396
426
|
}
|
|
397
427
|
}
|
|
398
428
|
if (batch.length > 0) {
|
|
399
429
|
const start = Date.now();
|
|
400
430
|
await retry(() => storage.upsert(batch), 2).catch((error) => {
|
|
401
|
-
for (const
|
|
402
|
-
log.error(`Failed to run your 'upsert' handler for event ${
|
|
431
|
+
for (const event of grouped_events) {
|
|
432
|
+
log.error(`Failed to run your 'upsert' handler for event ${event.id}`);
|
|
403
433
|
}
|
|
404
434
|
throw error;
|
|
405
435
|
});
|
|
406
|
-
for (const
|
|
407
|
-
log.debug(`Recorded ${batch.length} ${
|
|
436
|
+
for (const event of grouped_events) {
|
|
437
|
+
log.debug(`Recorded ${batch.length} ${event.id} in ${Date.now() - start}ms`);
|
|
408
438
|
}
|
|
409
439
|
}
|
|
410
440
|
});
|
|
@@ -418,84 +448,71 @@ function defineIndexer(opts) {
|
|
|
418
448
|
const commitsValue = JSON.stringify({ hello: "world" });
|
|
419
449
|
await opts.metadataStorage.adapter.put(commitsKey, commitsValue);
|
|
420
450
|
}
|
|
421
|
-
const
|
|
422
|
-
log.debug(
|
|
423
|
-
const
|
|
424
|
-
const
|
|
425
|
-
|
|
451
|
+
const public_deleteReorganisedHead = async (head) => {
|
|
452
|
+
log.debug(`Received reorganised head ${hexToNumber(head.number)}`);
|
|
453
|
+
const chain = normalizeHex(head.chain);
|
|
454
|
+
const number = normalizeHex(head.number, 16);
|
|
455
|
+
const hash = normalizeHex(head.hash);
|
|
456
|
+
const parentHash = normalizeHex(head.parent_hash);
|
|
457
|
+
const blocksKey = `blocks/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
458
|
+
const [blocksRes, canonicalBlock] = await Promise.all([
|
|
459
|
+
opts.metadataStorage.adapter.get(blocksKey),
|
|
426
460
|
//
|
|
427
|
-
|
|
461
|
+
getBlockFromChain({ chain: head.chain, number: head.number })
|
|
428
462
|
]);
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
if (viem.hexToNumber(head.number) <= indexerFinalizedHeight) {
|
|
432
|
-
return log.debug("Receiving finalized head, ignoring...");
|
|
463
|
+
if (blocksRes === null) {
|
|
464
|
+
return log.debug("Reorganised block never/already processed");
|
|
433
465
|
}
|
|
434
|
-
const
|
|
435
|
-
const
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
const parentHash = normalizeHex(block.eth_getBlockByNumber.parentHash);
|
|
439
|
-
const blocksKey = `blocks/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
440
|
-
const blocksValue = await compress(JSON.stringify(block));
|
|
441
|
-
const conditional = { ifNoneMatch: "*" };
|
|
442
|
-
const result = await opts.metadataStorage.adapter.put(blocksKey, blocksValue, conditional).catch((error) => {
|
|
443
|
-
if (error instanceof AdapterError) {
|
|
444
|
-
if (error.tag === "PreconditionFailed") {
|
|
445
|
-
return null;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
throw error;
|
|
449
|
-
});
|
|
450
|
-
if (result === null) {
|
|
451
|
-
return log.debug("Block already persisted to wal, ignoring...");
|
|
466
|
+
const decompressedBlock = await decompress(blocksRes.body);
|
|
467
|
+
const storedBlock = JSON.parse(decompressedBlock);
|
|
468
|
+
if (isHexEqual(head.hash, canonicalBlock.eth_getBlockByNumber.hash)) {
|
|
469
|
+
throw new Error("Attempted to delete canonical block");
|
|
452
470
|
}
|
|
453
|
-
|
|
454
|
-
await writeUnfinalizedBlock(block);
|
|
471
|
+
await deleteReorganisedBlocksAndWriteCanonicalBlock([storedBlock], canonicalBlock);
|
|
455
472
|
};
|
|
456
473
|
async function deleteReorganisedBlocksAndWriteCanonicalBlock(reorganised, canonical) {
|
|
457
|
-
const deletes = allEvents.map(async (
|
|
474
|
+
const deletes = allEvents.map(async (event) => {
|
|
458
475
|
const batch = [];
|
|
459
476
|
try {
|
|
460
477
|
for (const block of reorganised) {
|
|
461
|
-
const events =
|
|
462
|
-
for (const
|
|
463
|
-
batch.push(
|
|
478
|
+
const events = event.handler(block);
|
|
479
|
+
for (const event2 of events) {
|
|
480
|
+
batch.push(event2);
|
|
464
481
|
}
|
|
465
482
|
}
|
|
466
483
|
} catch (error) {
|
|
467
|
-
log.error(`Failed to run your 'handler' for event ${
|
|
484
|
+
log.error(`Failed to run your 'handler' for event ${event.id}`);
|
|
468
485
|
throw error;
|
|
469
486
|
}
|
|
470
487
|
if (batch.length === 0) {
|
|
471
488
|
return;
|
|
472
489
|
}
|
|
473
|
-
await retry(() =>
|
|
474
|
-
log.error(`Failed to run your 'delete' handler for event ${
|
|
490
|
+
await retry(() => event.storage.delete(batch), 2).catch((error) => {
|
|
491
|
+
log.error(`Failed to run your 'delete' handler for event ${event.id}`);
|
|
475
492
|
throw error;
|
|
476
493
|
});
|
|
477
494
|
});
|
|
478
495
|
await Promise.all(deletes);
|
|
479
496
|
const upserts = eventsGroupedByStorageMap.entries().map(async ([storage, grouped_events]) => {
|
|
480
497
|
const batch = [];
|
|
481
|
-
for (const
|
|
498
|
+
for (const event of grouped_events) {
|
|
482
499
|
try {
|
|
483
|
-
if (!
|
|
500
|
+
if (!event.filters.some((filter) => matchFilter(canonical, filter))) {
|
|
484
501
|
continue;
|
|
485
502
|
}
|
|
486
|
-
const events =
|
|
487
|
-
for (const
|
|
488
|
-
batch.push(
|
|
503
|
+
const events = event.handler(canonical);
|
|
504
|
+
for (const event2 of events) {
|
|
505
|
+
batch.push(event2);
|
|
489
506
|
}
|
|
490
507
|
} catch (error) {
|
|
491
|
-
log.error(`Failed to run your 'handler' for event ${
|
|
508
|
+
log.error(`Failed to run your 'handler' for event ${event.id}`);
|
|
492
509
|
throw error;
|
|
493
510
|
}
|
|
494
511
|
}
|
|
495
512
|
if (batch.length > 0) {
|
|
496
513
|
await retry(() => storage.upsert(batch), 2).catch((error) => {
|
|
497
|
-
for (const
|
|
498
|
-
log.error(`Failed to run your 'upsert' handler for event ${
|
|
514
|
+
for (const event of grouped_events) {
|
|
515
|
+
log.error(`Failed to run your 'upsert' handler for event ${event.id}`);
|
|
499
516
|
}
|
|
500
517
|
throw error;
|
|
501
518
|
});
|
|
@@ -503,55 +520,38 @@ function defineIndexer(opts) {
|
|
|
503
520
|
});
|
|
504
521
|
await Promise.all(upserts);
|
|
505
522
|
}
|
|
506
|
-
const
|
|
507
|
-
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
const parentHash = normalizeHex(head.parent_hash);
|
|
512
|
-
const blocksKey = `blocks/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
513
|
-
const [blocksRes, canonicalBlock] = await Promise.all([
|
|
514
|
-
opts.metadataStorage.adapter.get(blocksKey),
|
|
523
|
+
const public_writeFinalizedHeight = async (chain) => {
|
|
524
|
+
const path = `cache/v1/${normalizeHex(chain)}`;
|
|
525
|
+
const [cached, block] = await Promise.all([
|
|
526
|
+
getChainCache(chain),
|
|
527
|
+
getBlockFromChain({ chain, number: "finalized" })
|
|
515
528
|
//
|
|
516
|
-
getBlockFromChain({ chain: head.chain, number: head.number })
|
|
517
529
|
]);
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
if (
|
|
524
|
-
|
|
530
|
+
const cache = {
|
|
531
|
+
chain_finalized_height: hexToNumber(block.eth_getBlockByNumber.number),
|
|
532
|
+
chain_finalized_block_hash: block.eth_getBlockByNumber.hash,
|
|
533
|
+
chain_finalized_parent_hash: block.eth_getBlockByNumber.parentHash
|
|
534
|
+
};
|
|
535
|
+
if (cached.chain_finalized_height >= cache.chain_finalized_height) {
|
|
536
|
+
return;
|
|
525
537
|
}
|
|
526
|
-
await
|
|
538
|
+
await opts.metadataStorage.adapter.put(path, JSON.stringify(cache));
|
|
527
539
|
};
|
|
528
|
-
async function
|
|
529
|
-
|
|
530
|
-
|
|
540
|
+
async function getChainCache(chain) {
|
|
541
|
+
const path = `cache/v1/${normalizeHex(chain)}`;
|
|
542
|
+
const cacheRes = await opts.metadataStorage.adapter.get(path);
|
|
543
|
+
if (cacheRes === null) {
|
|
544
|
+
const block = await getBlockFromChain({ chain, number: "finalized" });
|
|
545
|
+
const cache2 = {
|
|
546
|
+
chain_finalized_height: hexToNumber(block.eth_getBlockByNumber.number),
|
|
547
|
+
chain_finalized_block_hash: block.eth_getBlockByNumber.hash,
|
|
548
|
+
chain_finalized_parent_hash: block.eth_getBlockByNumber.parentHash
|
|
549
|
+
};
|
|
550
|
+
await opts.metadataStorage.adapter.put(path, JSON.stringify(cache2));
|
|
551
|
+
return cache2;
|
|
531
552
|
}
|
|
532
|
-
const
|
|
533
|
-
|
|
534
|
-
const hash = normalizeHex(block.eth_getBlockByNumber.hash);
|
|
535
|
-
const parentHash = normalizeHex(block.eth_getBlockByNumber.parentHash);
|
|
536
|
-
const prefix = `commits/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
537
|
-
const promises = actions.map(async (action2) => {
|
|
538
|
-
const events = action2.event.filters.some((filter) => matchFilter(block, filter)) ? action2.event.handler(block) : [];
|
|
539
|
-
const promises2 = events.map(async (event2) => {
|
|
540
|
-
await retry(() => action2.handler(event2), 2).catch((error) => {
|
|
541
|
-
log.error(`Failed to execute action ${action2.id}`);
|
|
542
|
-
throw error;
|
|
543
|
-
});
|
|
544
|
-
});
|
|
545
|
-
const results = await Promise.allSettled(promises2);
|
|
546
|
-
const failure = results.find((result) => result.status === "rejected");
|
|
547
|
-
if (failure !== void 0) {
|
|
548
|
-
throw failure.reason;
|
|
549
|
-
}
|
|
550
|
-
const commitsKey = `${prefix}/action/${action2.id}`;
|
|
551
|
-
const commitsValue = JSON.stringify({ hello: "world" });
|
|
552
|
-
await opts.metadataStorage.adapter.put(commitsKey, commitsValue);
|
|
553
|
-
});
|
|
554
|
-
await Promise.all(promises);
|
|
553
|
+
const cache = JSON.parse(decoder.decode(cacheRes.body));
|
|
554
|
+
return cache;
|
|
555
555
|
}
|
|
556
556
|
const public_writeFinalizedHead = async (head) => {
|
|
557
557
|
log.debug("Received finalized head...");
|
|
@@ -559,24 +559,59 @@ function defineIndexer(opts) {
|
|
|
559
559
|
return;
|
|
560
560
|
}
|
|
561
561
|
const blocksStart = Date.now();
|
|
562
|
-
const [
|
|
563
|
-
|
|
562
|
+
const [cache, blockFromMetadata, manifest, heads] = await Promise.all([
|
|
563
|
+
getChainCache(head.chain),
|
|
564
|
+
//
|
|
565
|
+
getBlockFromMetadata(head),
|
|
564
566
|
getOrInitManifest(head.chain),
|
|
565
|
-
|
|
567
|
+
listUnfinalizedHeads(head.chain)
|
|
566
568
|
]);
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
+
const receivedHeight = hexToNumber(head.number);
|
|
570
|
+
const chainFinalizedHeight = cache.chain_finalized_height;
|
|
569
571
|
const indexerFinalizedHeight = manifest.finalized_block_height;
|
|
570
|
-
const chainFinalizedHeight = viem.hexToNumber(chainFinalizedBlock.eth_getBlockByNumber.number);
|
|
571
572
|
if (receivedHeight <= indexerFinalizedHeight) {
|
|
572
573
|
return log.debug(`Received finalized head (${receivedHeight}) below indexer height (${indexerFinalizedHeight})`);
|
|
573
574
|
}
|
|
574
575
|
if (receivedHeight > chainFinalizedHeight) {
|
|
575
|
-
return log.error(`Received finalized head (${receivedHeight}) that has not finalized (${chainFinalizedHeight})`);
|
|
576
|
+
return log.error(`Received finalized head (${receivedHeight}) that has not actually finalized (${chainFinalizedHeight})`);
|
|
576
577
|
}
|
|
578
|
+
log.debug(`Loaded block in ${Date.now() - blocksStart}ms`);
|
|
579
|
+
const canonical = iife(() => {
|
|
580
|
+
let matchesHead = false;
|
|
581
|
+
let canonicalHead = {
|
|
582
|
+
chain: head.chain,
|
|
583
|
+
hash: cache.chain_finalized_block_hash,
|
|
584
|
+
parent_hash: cache.chain_finalized_parent_hash,
|
|
585
|
+
number: numberToHex(cache.chain_finalized_height)
|
|
586
|
+
};
|
|
587
|
+
const receivedHeight2 = hexToNumber(head.number);
|
|
588
|
+
for (let number2 = cache.chain_finalized_height; number2 > manifest.finalized_block_height; number2--) {
|
|
589
|
+
if (number2 === receivedHeight2) {
|
|
590
|
+
matchesHead = isHexEqual(head.hash, canonicalHead.hash) && isHexEqual(head.parent_hash, canonicalHead.parent_hash);
|
|
591
|
+
if (!matchesHead) {
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (number2 === manifest.finalized_block_height + 1) {
|
|
596
|
+
return matchesHead && isHexEqual(canonicalHead.parent_hash, manifest.finalized_block_hash);
|
|
597
|
+
}
|
|
598
|
+
const parent = heads.get(`${number2 - 1}/${normalizeHex(canonicalHead.parent_hash)}`);
|
|
599
|
+
if (parent === void 0) {
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
canonicalHead = parent;
|
|
603
|
+
}
|
|
604
|
+
return false;
|
|
605
|
+
});
|
|
606
|
+
const block = await iife(async () => {
|
|
607
|
+
if (canonical === false || blockFromMetadata === null) {
|
|
608
|
+
return await getBlockFromChain(head);
|
|
609
|
+
}
|
|
610
|
+
return blockFromMetadata;
|
|
611
|
+
});
|
|
577
612
|
const chain = normalizeHex(block.eth_chainId);
|
|
578
|
-
const number = normalizeHex(block.eth_getBlockByNumber.number, 16);
|
|
579
613
|
const hash = normalizeHex(block.eth_getBlockByNumber.hash);
|
|
614
|
+
const number = normalizeHex(block.eth_getBlockByNumber.number, 16);
|
|
580
615
|
const parentHash = normalizeHex(block.eth_getBlockByNumber.parentHash);
|
|
581
616
|
const finalizedKey = `finalized/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
582
617
|
const finalizedValue = JSON.stringify({ hello: "world" });
|
|
@@ -592,68 +627,62 @@ function defineIndexer(opts) {
|
|
|
592
627
|
}
|
|
593
628
|
await writeFinalizedBlock(block, allActions);
|
|
594
629
|
};
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
async function getBlocksProcessedAndGarbageCollect(chain, finalizedHeight) {
|
|
598
|
-
while (true) {
|
|
599
|
-
const blocksKey = `blocks/v1/${normalizeHex(chain)}/`;
|
|
600
|
-
const blocks = await opts.metadataStorage.adapter.list({ prefix: blocksKey, limit: 1e3 });
|
|
601
|
-
if (blocks.keys.length === 0) {
|
|
602
|
-
return [];
|
|
603
|
-
}
|
|
604
|
-
const garbageCollectionKeys = blocks.keys.filter((key) => {
|
|
605
|
-
const [_, __, ___, number] = key.split("/");
|
|
606
|
-
return viem.hexToNumber(number) <= finalizedHeight;
|
|
607
|
-
});
|
|
608
|
-
if (garbageCollectionKeys.length === 0) {
|
|
609
|
-
return blocks.keys.map((key) => {
|
|
610
|
-
const [_, __, ___, number, hash, parent_hash] = key.split("/");
|
|
611
|
-
return { chain, number, hash, parent_hash };
|
|
612
|
-
});
|
|
613
|
-
}
|
|
614
|
-
const garbageCollectionPromises = garbageCollectionKeys.map(async (key) => {
|
|
615
|
-
await opts.metadataStorage.adapter.delete(key);
|
|
616
|
-
});
|
|
617
|
-
await Promise.all(garbageCollectionPromises);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
async function getCommitsAndGarbageCollect(chain, finalizedHeight) {
|
|
621
|
-
while (true) {
|
|
622
|
-
const commitsKey = `commits/v1/${normalizeHex(chain)}/`;
|
|
623
|
-
const commits = await opts.metadataStorage.adapter.list({ prefix: commitsKey, limit: 1e3 });
|
|
624
|
-
if (commits.keys.length === 0) {
|
|
625
|
-
return [];
|
|
626
|
-
}
|
|
627
|
-
const garbageCollectionKeys = commits.keys.filter((key) => {
|
|
628
|
-
const [_, __, ___, number] = key.split("/");
|
|
629
|
-
return viem.hexToNumber(number) <= finalizedHeight;
|
|
630
|
-
});
|
|
631
|
-
if (garbageCollectionKeys.length === 0) {
|
|
632
|
-
return commits.keys.map((key) => {
|
|
633
|
-
const [_, __, ___, number, hash, parent_hash, type, id] = key.split("/");
|
|
634
|
-
return { chain, number, hash, parent_hash, type, id };
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
const garbageCollectionPromises = garbageCollectionKeys.map(async (key) => {
|
|
638
|
-
await opts.metadataStorage.adapter.delete(key);
|
|
639
|
-
});
|
|
640
|
-
await Promise.all(garbageCollectionPromises);
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
async function getBlockFromMetadataOrChain(head) {
|
|
630
|
+
async function getBlockFromMetadata(head) {
|
|
631
|
+
const hash = normalizeHex(head.hash);
|
|
644
632
|
const chain = normalizeHex(head.chain);
|
|
645
633
|
const number = normalizeHex(head.number, 16);
|
|
646
|
-
const hash = normalizeHex(head.hash);
|
|
647
634
|
const parentHash = normalizeHex(head.parent_hash);
|
|
648
635
|
const prefix = `blocks/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
649
636
|
const object = await opts.metadataStorage.adapter.get(prefix);
|
|
650
|
-
if (object
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
637
|
+
if (object === null) {
|
|
638
|
+
return null;
|
|
639
|
+
}
|
|
640
|
+
const block = await decompress(object.body);
|
|
641
|
+
const parsed = JSON.parse(block);
|
|
642
|
+
return parsed;
|
|
643
|
+
}
|
|
644
|
+
async function listUnfinalizedHeads(chain) {
|
|
645
|
+
const blocks = await opts.metadataStorage.adapter.list({
|
|
646
|
+
prefix: `blocks/v1/${normalizeHex(chain)}/`,
|
|
647
|
+
limit: 1e3
|
|
648
|
+
});
|
|
649
|
+
const heads = /* @__PURE__ */ new Map();
|
|
650
|
+
for (const key of blocks.keys) {
|
|
651
|
+
const [_, __, chain2, number, hash, parent_hash] = key.split("/");
|
|
652
|
+
heads.set(`${hexToNumber(number)}/${normalizeHex(hash)}`, { chain: chain2, number, hash, parent_hash });
|
|
654
653
|
}
|
|
655
|
-
return
|
|
654
|
+
return heads;
|
|
655
|
+
}
|
|
656
|
+
async function writeFinalizedBlock(block, actions) {
|
|
657
|
+
if (actions.length === 0) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
const chain = normalizeHex(block.eth_chainId);
|
|
661
|
+
const number = normalizeHex(block.eth_getBlockByNumber.number, 16);
|
|
662
|
+
const hash = normalizeHex(block.eth_getBlockByNumber.hash);
|
|
663
|
+
const parentHash = normalizeHex(block.eth_getBlockByNumber.parentHash);
|
|
664
|
+
const prefix = `commits/v1/${chain}/${number}/${hash}/${parentHash}`;
|
|
665
|
+
const promises = actions.map(async (action) => {
|
|
666
|
+
const events = action.event.filters.some((filter) => matchFilter(block, filter)) ? action.event.handler(block) : [];
|
|
667
|
+
const promises2 = events.map(async (event) => {
|
|
668
|
+
await retry(() => action.handler(event), 2).catch((error) => {
|
|
669
|
+
log.error(`Failed to execute action ${action.id}`);
|
|
670
|
+
throw error;
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
const results = await Promise.allSettled(promises2);
|
|
674
|
+
const failure = results.find((result) => result.status === "rejected");
|
|
675
|
+
if (failure !== void 0) {
|
|
676
|
+
throw failure.reason;
|
|
677
|
+
}
|
|
678
|
+
const commitsKey = `${prefix}/action/${action.id}`;
|
|
679
|
+
const commitsValue = JSON.stringify({ hello: "world" });
|
|
680
|
+
await opts.metadataStorage.adapter.put(commitsKey, commitsValue);
|
|
681
|
+
});
|
|
682
|
+
await Promise.all(promises);
|
|
656
683
|
}
|
|
684
|
+
const FINALIZATION_BATCH_SIZE = 32;
|
|
685
|
+
const LEASE_DURATION_MS = 60 * 1e3;
|
|
657
686
|
const public_finalize = async (chain) => {
|
|
658
687
|
const manifestKey = `manifest/v1/${normalizeHex(chain)}`;
|
|
659
688
|
const [chainFinalizedBlock, manifestGetRes] = await Promise.all([
|
|
@@ -661,7 +690,7 @@ function defineIndexer(opts) {
|
|
|
661
690
|
opts.metadataStorage.adapter.get(manifestKey)
|
|
662
691
|
//
|
|
663
692
|
]);
|
|
664
|
-
const chainFinalizedHeight =
|
|
693
|
+
const chainFinalizedHeight = hexToNumber(chainFinalizedBlock.eth_getBlockByNumber.number);
|
|
665
694
|
if (manifestGetRes === null) {
|
|
666
695
|
log.debug("No manifest file found");
|
|
667
696
|
const manifest2 = {
|
|
@@ -708,7 +737,7 @@ function defineIndexer(opts) {
|
|
|
708
737
|
const nextFinalizedHeight = Math.min(chainFinalizedHeight, indexerFinalizedBlockHeight + FINALIZATION_BATCH_SIZE);
|
|
709
738
|
const finalizationBatchSize = nextFinalizedHeight - indexerFinalizedBlockHeight;
|
|
710
739
|
const [nextFinalizedBlock, blocksProcessed] = await Promise.all([
|
|
711
|
-
getBlockFromChain({ chain, number:
|
|
740
|
+
getBlockFromChain({ chain, number: numberToHex(nextFinalizedHeight) }),
|
|
712
741
|
getBlocksProcessedAndGarbageCollect(chain, indexerFinalizedBlockHeight)
|
|
713
742
|
]);
|
|
714
743
|
const nextFinalizedHead = {
|
|
@@ -718,7 +747,7 @@ function defineIndexer(opts) {
|
|
|
718
747
|
parent_hash: nextFinalizedBlock.eth_getBlockByNumber.parentHash
|
|
719
748
|
};
|
|
720
749
|
const nextFinalizedBlockProcessed = blocksProcessed.some((block) => {
|
|
721
|
-
return isHexEqual(block.hash, nextFinalizedHead.hash) && isHexEqual(block.parent_hash, nextFinalizedHead.parent_hash) &&
|
|
750
|
+
return isHexEqual(block.hash, nextFinalizedHead.hash) && isHexEqual(block.parent_hash, nextFinalizedHead.parent_hash) && hexToNumber(block.number) === hexToNumber(nextFinalizedHead.number);
|
|
722
751
|
});
|
|
723
752
|
if (nextFinalizedBlockProcessed === false) {
|
|
724
753
|
await Promise.all([
|
|
@@ -733,7 +762,7 @@ function defineIndexer(opts) {
|
|
|
733
762
|
for (let index = 1; index < finalizationBatchSize; index++) {
|
|
734
763
|
const number = nextFinalizedHeight - index;
|
|
735
764
|
const blocksProcessedForHeight = blocksProcessed.filter((block) => {
|
|
736
|
-
return
|
|
765
|
+
return hexToNumber(block.number) === number;
|
|
737
766
|
});
|
|
738
767
|
const canonicalHead = blocksProcessedForHeight.find((block) => {
|
|
739
768
|
return isHexEqual(block.hash, parentHash);
|
|
@@ -744,7 +773,7 @@ function defineIndexer(opts) {
|
|
|
744
773
|
continue;
|
|
745
774
|
}
|
|
746
775
|
log.debug("Canonical head never processed, loading from chain...");
|
|
747
|
-
const canonicalBlock = await getBlockFromChain({ chain, number:
|
|
776
|
+
const canonicalBlock = await getBlockFromChain({ chain, number: numberToHex(number) });
|
|
748
777
|
const head = {
|
|
749
778
|
chain,
|
|
750
779
|
hash: canonicalBlock.eth_getBlockByNumber.hash,
|
|
@@ -771,17 +800,17 @@ function defineIndexer(opts) {
|
|
|
771
800
|
const commits = await getCommitsAndGarbageCollect(chain, indexerFinalizedBlockHeight);
|
|
772
801
|
for (const canonicalHead of canonicalHeads) {
|
|
773
802
|
const blocksProcessedForHeight = blocksProcessed.filter((head) => {
|
|
774
|
-
return
|
|
803
|
+
return hexToNumber(canonicalHead.number) === hexToNumber(head.number);
|
|
775
804
|
});
|
|
776
805
|
const processedOnlyCanonicalBlock = blocksProcessedForHeight.every((head) => {
|
|
777
806
|
return isHexEqual(canonicalHead.hash, head.hash) && isHexEqual(canonicalHead.parent_hash, head.parent_hash);
|
|
778
807
|
});
|
|
779
808
|
const eventsCommittedForHeight = commits.some((commit) => {
|
|
780
|
-
return commit.type === void 0 && isHexEqual(canonicalHead.hash, commit.hash) && isHexEqual(canonicalHead.parent_hash, commit.parent_hash) &&
|
|
809
|
+
return commit.type === void 0 && isHexEqual(canonicalHead.hash, commit.hash) && isHexEqual(canonicalHead.parent_hash, commit.parent_hash) && hexToNumber(canonicalHead.number) === hexToNumber(commit.number);
|
|
781
810
|
});
|
|
782
|
-
const actionsWithoutCommit = allActions.filter((
|
|
811
|
+
const actionsWithoutCommit = allActions.filter((action) => {
|
|
783
812
|
const commitExists = commits.some((commit) => {
|
|
784
|
-
return commit.id ===
|
|
813
|
+
return commit.id === action.id && commit.type === "action" && isHexEqual(canonicalHead.hash, commit.hash) && isHexEqual(canonicalHead.parent_hash, commit.parent_hash) && hexToNumber(canonicalHead.number) === hexToNumber(commit.number);
|
|
785
814
|
});
|
|
786
815
|
return !commitExists;
|
|
787
816
|
});
|
|
@@ -837,26 +866,77 @@ function defineIndexer(opts) {
|
|
|
837
866
|
}
|
|
838
867
|
log.debug("Indexer finalized, returning...");
|
|
839
868
|
};
|
|
869
|
+
async function getBlocksProcessedAndGarbageCollect(chain, finalizedHeight) {
|
|
870
|
+
while (true) {
|
|
871
|
+
const blocksKey = `blocks/v1/${normalizeHex(chain)}/`;
|
|
872
|
+
const blocks = await opts.metadataStorage.adapter.list({ prefix: blocksKey, limit: 1e3 });
|
|
873
|
+
if (blocks.keys.length === 0) {
|
|
874
|
+
return [];
|
|
875
|
+
}
|
|
876
|
+
const garbageCollectionKeys = blocks.keys.filter((key) => {
|
|
877
|
+
const [_, __, ___, number] = key.split("/");
|
|
878
|
+
return hexToNumber(number) <= finalizedHeight;
|
|
879
|
+
});
|
|
880
|
+
if (garbageCollectionKeys.length === 0) {
|
|
881
|
+
return blocks.keys.map((key) => {
|
|
882
|
+
const [_, __, ___, number, hash, parent_hash] = key.split("/");
|
|
883
|
+
return { chain, number, hash, parent_hash };
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
const garbageCollectionPromises = garbageCollectionKeys.map(async (key) => {
|
|
887
|
+
await opts.metadataStorage.adapter.delete(key);
|
|
888
|
+
});
|
|
889
|
+
await Promise.all(garbageCollectionPromises);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
async function getCommitsAndGarbageCollect(chain, finalizedHeight) {
|
|
893
|
+
while (true) {
|
|
894
|
+
const commitsKey = `commits/v1/${normalizeHex(chain)}/`;
|
|
895
|
+
const commits = await opts.metadataStorage.adapter.list({ prefix: commitsKey, limit: 1e3 });
|
|
896
|
+
if (commits.keys.length === 0) {
|
|
897
|
+
return [];
|
|
898
|
+
}
|
|
899
|
+
const garbageCollectionKeys = commits.keys.filter((key) => {
|
|
900
|
+
const [_, __, ___, number] = key.split("/");
|
|
901
|
+
return hexToNumber(number) <= finalizedHeight;
|
|
902
|
+
});
|
|
903
|
+
if (garbageCollectionKeys.length === 0) {
|
|
904
|
+
return commits.keys.map((key) => {
|
|
905
|
+
const [_, __, ___, number, hash, parent_hash, type, id] = key.split("/");
|
|
906
|
+
return { chain, number, hash, parent_hash, type, id };
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
const garbageCollectionPromises = garbageCollectionKeys.map(async (key) => {
|
|
910
|
+
await opts.metadataStorage.adapter.delete(key);
|
|
911
|
+
});
|
|
912
|
+
await Promise.all(garbageCollectionPromises);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
async function getBlockFromMetadataOrChain(head) {
|
|
916
|
+
const metadata = await getBlockFromMetadata(head);
|
|
917
|
+
if (metadata === null) {
|
|
918
|
+
return await getBlockFromChain(head);
|
|
919
|
+
}
|
|
920
|
+
return metadata;
|
|
921
|
+
}
|
|
840
922
|
const private_getMetadata = async () => {
|
|
841
923
|
return {
|
|
842
924
|
version,
|
|
843
|
-
language: "javascript"
|
|
925
|
+
language: "javascript",
|
|
926
|
+
events: allEvents.map((event) => {
|
|
927
|
+
const filters = event.filters.map((filter) => {
|
|
928
|
+
return {
|
|
929
|
+
chain: filter.chain,
|
|
930
|
+
event: filter.event,
|
|
931
|
+
address: filter.address,
|
|
932
|
+
to_block: filter.toBlock,
|
|
933
|
+
from_block: filter.fromBlock
|
|
934
|
+
};
|
|
935
|
+
});
|
|
936
|
+
return { id: event.id, filters };
|
|
937
|
+
})
|
|
844
938
|
};
|
|
845
939
|
};
|
|
846
|
-
const private_getEvents = async () => {
|
|
847
|
-
return allEvents.map((event2) => {
|
|
848
|
-
const filters = event2.filters.map((filter) => {
|
|
849
|
-
return {
|
|
850
|
-
chain: filter.chain,
|
|
851
|
-
event: filter.event,
|
|
852
|
-
address: filter.address,
|
|
853
|
-
to_block: filter.toBlock,
|
|
854
|
-
from_block: filter.fromBlock
|
|
855
|
-
};
|
|
856
|
-
});
|
|
857
|
-
return { id: event2.id, filters };
|
|
858
|
-
});
|
|
859
|
-
};
|
|
860
940
|
const ignored = [
|
|
861
941
|
"toJSON",
|
|
862
942
|
"toString",
|
|
@@ -900,26 +980,40 @@ function defineIndexer(opts) {
|
|
|
900
980
|
};
|
|
901
981
|
const IncompleteBlockError = createException("Received block with missing required property");
|
|
902
982
|
const private_writeEvents = async (params) => {
|
|
903
|
-
if (allEvents.length === 0)
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
983
|
+
if (allEvents.length === 0) {
|
|
984
|
+
return { failures: [] };
|
|
985
|
+
}
|
|
986
|
+
const relevantEvents = allEvents.filter((event) => params.events.includes(event.id));
|
|
987
|
+
if (relevantEvents.length === 0) {
|
|
988
|
+
return { failures: [] };
|
|
989
|
+
}
|
|
990
|
+
if (params.events.length === 0) {
|
|
991
|
+
return { failures: [] };
|
|
992
|
+
}
|
|
907
993
|
const failures = {};
|
|
908
|
-
let
|
|
909
|
-
const
|
|
910
|
-
|
|
994
|
+
let accessedUndefinedKey = false;
|
|
995
|
+
const proxiedBlocks = getWriteEventsProxy(params.blocks, () => {
|
|
996
|
+
accessedUndefinedKey = true;
|
|
911
997
|
throw new Error(IncompleteBlockError);
|
|
912
998
|
});
|
|
913
999
|
const promises = eventsGroupedByStorageMap.entries().map(async ([storage, grouped_events]) => {
|
|
914
1000
|
const batch = [];
|
|
915
|
-
for (const
|
|
916
|
-
if (!params.events.includes(
|
|
917
|
-
|
|
1001
|
+
for (const event of grouped_events) {
|
|
1002
|
+
if (!params.events.includes(event.id)) {
|
|
1003
|
+
continue;
|
|
1004
|
+
}
|
|
1005
|
+
for (const block of proxiedBlocks) {
|
|
918
1006
|
try {
|
|
919
|
-
if (!
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
1007
|
+
if (!event.filters.some((filter) => matchFilter(block, filter))) {
|
|
1008
|
+
continue;
|
|
1009
|
+
}
|
|
1010
|
+
const events = event.handler(block);
|
|
1011
|
+
if (accessedUndefinedKey) {
|
|
1012
|
+
throw new Error(IncompleteBlockError);
|
|
1013
|
+
}
|
|
1014
|
+
for (const event2 of events) {
|
|
1015
|
+
batch.push(event2);
|
|
1016
|
+
}
|
|
923
1017
|
} catch (error) {
|
|
924
1018
|
const status = catchException(error, IncompleteBlockError) ? "incomplete_error" : "handler_error";
|
|
925
1019
|
if (status === "handler_error") {
|
|
@@ -927,9 +1021,9 @@ function defineIndexer(opts) {
|
|
|
927
1021
|
log.error(error.message);
|
|
928
1022
|
}
|
|
929
1023
|
}
|
|
930
|
-
failures[
|
|
1024
|
+
failures[event.id + block.eth_getBlockByNumber.number] ??= {
|
|
931
1025
|
status,
|
|
932
|
-
event_id:
|
|
1026
|
+
event_id: event.id,
|
|
933
1027
|
chain: block.eth_chainId,
|
|
934
1028
|
hash: block.eth_getBlockByNumber.hash,
|
|
935
1029
|
number: block.eth_getBlockByNumber.number,
|
|
@@ -939,11 +1033,15 @@ function defineIndexer(opts) {
|
|
|
939
1033
|
}
|
|
940
1034
|
}
|
|
941
1035
|
}
|
|
942
|
-
if (batch.length === 0)
|
|
1036
|
+
if (batch.length === 0) {
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
943
1039
|
const start = Date.now();
|
|
944
1040
|
try {
|
|
945
1041
|
await retry(() => storage.upsert(batch), 2);
|
|
946
|
-
if (
|
|
1042
|
+
if (accessedUndefinedKey) {
|
|
1043
|
+
throw new Error(IncompleteBlockError);
|
|
1044
|
+
}
|
|
947
1045
|
} catch (error) {
|
|
948
1046
|
const status = catchException(error, IncompleteBlockError) ? "incomplete_error" : "upsert_error";
|
|
949
1047
|
if (status === "upsert_error") {
|
|
@@ -951,12 +1049,14 @@ function defineIndexer(opts) {
|
|
|
951
1049
|
log.error(error.message);
|
|
952
1050
|
}
|
|
953
1051
|
}
|
|
954
|
-
for (const
|
|
955
|
-
for (const block of
|
|
956
|
-
if (!
|
|
957
|
-
|
|
1052
|
+
for (const event of relevantEvents) {
|
|
1053
|
+
for (const block of proxiedBlocks) {
|
|
1054
|
+
if (!event.filters.some((filter) => matchFilter(block, filter))) {
|
|
1055
|
+
continue;
|
|
1056
|
+
}
|
|
1057
|
+
failures[event.id + block.eth_getBlockByNumber.number] ??= {
|
|
958
1058
|
status,
|
|
959
|
-
event_id:
|
|
1059
|
+
event_id: event.id,
|
|
960
1060
|
chain: block.eth_chainId,
|
|
961
1061
|
hash: block.eth_getBlockByNumber.hash,
|
|
962
1062
|
number: block.eth_getBlockByNumber.number,
|
|
@@ -967,13 +1067,13 @@ function defineIndexer(opts) {
|
|
|
967
1067
|
}
|
|
968
1068
|
}
|
|
969
1069
|
const stop = Date.now() - start;
|
|
970
|
-
for (const
|
|
971
|
-
log.debug(`Recorded ${batch.length} ${
|
|
1070
|
+
for (const event of relevantEvents) {
|
|
1071
|
+
log.debug(`Recorded ${batch.length} ${event.id} in ${stop}ms`);
|
|
972
1072
|
}
|
|
973
1073
|
});
|
|
974
1074
|
await Promise.all(promises);
|
|
975
|
-
const
|
|
976
|
-
return { failures:
|
|
1075
|
+
const failuresArray = Object.values(failures);
|
|
1076
|
+
return { failures: failuresArray };
|
|
977
1077
|
};
|
|
978
1078
|
const private_writeEventsAndGetKeys = async (params) => {
|
|
979
1079
|
if (allEvents.length === 0) {
|
|
@@ -982,7 +1082,7 @@ function defineIndexer(opts) {
|
|
|
982
1082
|
if (params.events.length === 0) {
|
|
983
1083
|
return { results: [], keys: [] };
|
|
984
1084
|
}
|
|
985
|
-
const relevant_events = allEvents.filter((
|
|
1085
|
+
const relevant_events = allEvents.filter((event) => params.events.includes(event.id));
|
|
986
1086
|
if (relevant_events.length === 0) {
|
|
987
1087
|
return { results: [], keys: [] };
|
|
988
1088
|
}
|
|
@@ -1012,20 +1112,20 @@ function defineIndexer(opts) {
|
|
|
1012
1112
|
}
|
|
1013
1113
|
const proxy = new Proxy(block, createProxyHandler(""));
|
|
1014
1114
|
await Promise.all(
|
|
1015
|
-
relevant_events.map(async (
|
|
1016
|
-
if (!
|
|
1115
|
+
relevant_events.map(async (event) => {
|
|
1116
|
+
if (!event.filters.some((filter) => matchFilter(proxy, filter))) {
|
|
1017
1117
|
return;
|
|
1018
1118
|
}
|
|
1019
1119
|
let events = [];
|
|
1020
1120
|
try {
|
|
1021
|
-
events =
|
|
1121
|
+
events = event.handler(proxy);
|
|
1022
1122
|
} catch (error) {
|
|
1023
1123
|
if (error instanceof Error) {
|
|
1024
1124
|
log.error(error.message);
|
|
1025
1125
|
}
|
|
1026
|
-
results[
|
|
1126
|
+
results[event.id + proxy.eth_getBlockByNumber.number] = {
|
|
1027
1127
|
status: "handler_error",
|
|
1028
|
-
event_id:
|
|
1128
|
+
event_id: event.id,
|
|
1029
1129
|
chain: proxy.eth_chainId,
|
|
1030
1130
|
hash: proxy.eth_getBlockByNumber.hash,
|
|
1031
1131
|
number: proxy.eth_getBlockByNumber.number,
|
|
@@ -1035,9 +1135,9 @@ function defineIndexer(opts) {
|
|
|
1035
1135
|
return;
|
|
1036
1136
|
}
|
|
1037
1137
|
if (events.length === 0) {
|
|
1038
|
-
results[
|
|
1138
|
+
results[event.id + proxy.eth_getBlockByNumber.number] = {
|
|
1039
1139
|
status: "ok",
|
|
1040
|
-
event_id:
|
|
1140
|
+
event_id: event.id,
|
|
1041
1141
|
chain: proxy.eth_chainId,
|
|
1042
1142
|
hash: proxy.eth_getBlockByNumber.hash,
|
|
1043
1143
|
number: proxy.eth_getBlockByNumber.number,
|
|
@@ -1048,15 +1148,15 @@ function defineIndexer(opts) {
|
|
|
1048
1148
|
}
|
|
1049
1149
|
try {
|
|
1050
1150
|
const start = Date.now();
|
|
1051
|
-
await
|
|
1052
|
-
log.debug(`Recorded ${events.length} ${
|
|
1151
|
+
await event.storage.upsert(events);
|
|
1152
|
+
log.debug(`Recorded ${events.length} ${event.id} in ${Date.now() - start}ms`);
|
|
1053
1153
|
} catch (error) {
|
|
1054
1154
|
if (error instanceof Error) {
|
|
1055
1155
|
log.error(error.message);
|
|
1056
1156
|
}
|
|
1057
|
-
results[
|
|
1157
|
+
results[event.id + proxy.eth_getBlockByNumber.number] = {
|
|
1058
1158
|
status: "upsert_error",
|
|
1059
|
-
event_id:
|
|
1159
|
+
event_id: event.id,
|
|
1060
1160
|
chain: proxy.eth_chainId,
|
|
1061
1161
|
hash: proxy.eth_getBlockByNumber.hash,
|
|
1062
1162
|
number: proxy.eth_getBlockByNumber.number,
|
|
@@ -1065,9 +1165,9 @@ function defineIndexer(opts) {
|
|
|
1065
1165
|
};
|
|
1066
1166
|
return;
|
|
1067
1167
|
}
|
|
1068
|
-
results[
|
|
1168
|
+
results[event.id + proxy.eth_getBlockByNumber.number] = {
|
|
1069
1169
|
status: "ok",
|
|
1070
|
-
event_id:
|
|
1170
|
+
event_id: event.id,
|
|
1071
1171
|
chain: proxy.eth_chainId,
|
|
1072
1172
|
hash: proxy.eth_getBlockByNumber.hash,
|
|
1073
1173
|
number: proxy.eth_getBlockByNumber.number,
|
|
@@ -1085,52 +1185,20 @@ function defineIndexer(opts) {
|
|
|
1085
1185
|
});
|
|
1086
1186
|
return { results: results_array, keys: filtered_keys };
|
|
1087
1187
|
};
|
|
1088
|
-
const event = (event2) => {
|
|
1089
|
-
if (!/^[A-Za-z0-9_-]+$/.test(event2.id)) {
|
|
1090
|
-
throw new Error(`Invalid event id \`${event2.id}\`. Only characters A-Z, a-z, 0-9, underscores, and hyphens are permitted.`);
|
|
1091
|
-
}
|
|
1092
|
-
allEvents.push(event2);
|
|
1093
|
-
const group = eventsGroupedByStorageMap.get(event2.storage) ?? [];
|
|
1094
|
-
group.push(event2);
|
|
1095
|
-
eventsGroupedByStorageMap.set(event2.storage, group);
|
|
1096
|
-
return event2;
|
|
1097
|
-
};
|
|
1098
|
-
const action = (action2) => {
|
|
1099
|
-
if (!/^[A-Za-z0-9_-]+$/.test(action2.id)) {
|
|
1100
|
-
throw new Error(`Invalid action id \`${action2.id}\`. Only characters A-Z, a-z, 0-9, underscores, and hyphens are permitted.`);
|
|
1101
|
-
}
|
|
1102
|
-
if (allActions.some((existing) => existing.id === action2.id)) {
|
|
1103
|
-
throw new Error(`Duplicate action id \`${action2.id}\`.`);
|
|
1104
|
-
}
|
|
1105
|
-
allActions.push(action2);
|
|
1106
|
-
return action2;
|
|
1107
|
-
};
|
|
1108
1188
|
const rpc = {
|
|
1109
1189
|
request: {
|
|
1110
1190
|
public_finalize,
|
|
1191
|
+
public_getMetadata,
|
|
1111
1192
|
public_getFinalizedHeight,
|
|
1112
1193
|
public_writeFinalizedHead,
|
|
1194
|
+
public_writeFinalizedHeight,
|
|
1113
1195
|
public_writeUnfinalizedHead,
|
|
1114
1196
|
public_deleteReorganisedHead,
|
|
1115
|
-
private_getEvents,
|
|
1116
1197
|
private_getMetadata,
|
|
1117
1198
|
private_writeEvents,
|
|
1118
1199
|
private_writeEventsAndGetKeys
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
};
|
|
1122
|
-
const server = createServer({
|
|
1123
|
-
transport: local(rpc),
|
|
1124
|
-
quiet: opts.quiet ?? false,
|
|
1125
|
-
signingKey: opts.signingKey
|
|
1126
|
-
});
|
|
1127
|
-
const indexer = {
|
|
1128
|
-
...rpc,
|
|
1129
|
-
event,
|
|
1130
|
-
action,
|
|
1131
|
-
fetch: server.http
|
|
1132
|
-
};
|
|
1133
|
-
return indexer;
|
|
1200
|
+
}};
|
|
1201
|
+
return local(rpc);
|
|
1134
1202
|
}
|
|
1135
1203
|
|
|
1136
|
-
export { defineIndexer };
|
|
1204
|
+
export { defineAction, defineEvent, defineIndexer };
|