@secondlayer/subgraphs 4.1.5 → 4.2.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/src/index.d.ts +87 -29
- package/dist/src/index.js +376 -128
- package/dist/src/index.js.map +9 -7
- package/dist/src/runtime/block-processor.d.ts +72 -23
- package/dist/src/runtime/block-processor.js +186 -53
- package/dist/src/runtime/block-processor.js.map +7 -5
- package/dist/src/runtime/catchup.d.ts +72 -23
- package/dist/src/runtime/catchup.js +201 -68
- package/dist/src/runtime/catchup.js.map +7 -5
- package/dist/src/runtime/emitter.js +24 -1
- package/dist/src/runtime/emitter.js.map +5 -4
- package/dist/src/runtime/processor.js +328 -166
- package/dist/src/runtime/processor.js.map +10 -7
- package/dist/src/runtime/reindex.d.ts +72 -23
- package/dist/src/runtime/reindex.js +238 -105
- package/dist/src/runtime/reindex.js.map +7 -5
- package/dist/src/runtime/reorg.d.ts +72 -23
- package/dist/src/runtime/reorg.js +215 -81
- package/dist/src/runtime/reorg.js.map +8 -6
- package/dist/src/runtime/runner.d.ts +72 -23
- package/dist/src/runtime/runner.js +156 -23
- package/dist/src/runtime/runner.js.map +8 -6
- package/dist/src/runtime/source-matcher.d.ts +72 -23
- package/dist/src/schema/index.d.ts +72 -23
- package/dist/src/schema/index.js +115 -1
- package/dist/src/schema/index.js.map +3 -3
- package/dist/src/service.js +348 -171
- package/dist/src/service.js.map +11 -8
- package/dist/src/testing/index.d.ts +195 -24
- package/dist/src/testing/index.js +448 -25
- package/dist/src/testing/index.js.map +9 -6
- package/dist/src/triggers/index.d.ts +83 -24
- package/dist/src/triggers/index.js +5 -2
- package/dist/src/triggers/index.js.map +3 -3
- package/dist/src/types.d.ts +73 -24
- package/dist/src/validate.d.ts +72 -23
- package/dist/src/validate.js +115 -1
- package/dist/src/validate.js.map +3 -3
- package/package.json +2 -2
|
@@ -1212,7 +1212,7 @@ function matchSources(sources, transactions, events, traitContracts = new Map, f
|
|
|
1212
1212
|
|
|
1213
1213
|
// src/runtime/runner.ts
|
|
1214
1214
|
import { getErrorMessage } from "@secondlayer/shared";
|
|
1215
|
-
import { logger as
|
|
1215
|
+
import { logger as logger4 } from "@secondlayer/shared/logger";
|
|
1216
1216
|
import {
|
|
1217
1217
|
clarityValueToJS,
|
|
1218
1218
|
deserializeCV as deserializeCV4,
|
|
@@ -1226,6 +1226,19 @@ import {
|
|
|
1226
1226
|
function camelizeDataKey(str) {
|
|
1227
1227
|
return str.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
1228
1228
|
}
|
|
1229
|
+
function camelizeKeys(obj) {
|
|
1230
|
+
if (obj === null || obj === undefined)
|
|
1231
|
+
return obj;
|
|
1232
|
+
if (typeof obj !== "object")
|
|
1233
|
+
return obj;
|
|
1234
|
+
if (Array.isArray(obj))
|
|
1235
|
+
return obj.map(camelizeKeys);
|
|
1236
|
+
const result = {};
|
|
1237
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
1238
|
+
result[camelizeDataKey(k)] = camelizeKeys(v);
|
|
1239
|
+
}
|
|
1240
|
+
return result;
|
|
1241
|
+
}
|
|
1229
1242
|
function cvToTree(cv) {
|
|
1230
1243
|
switch (cv.type) {
|
|
1231
1244
|
case "uint":
|
|
@@ -1550,6 +1563,58 @@ function inferPrintTopics(samples) {
|
|
|
1550
1563
|
return out.sort((a, b) => b.count - a.count);
|
|
1551
1564
|
}
|
|
1552
1565
|
|
|
1566
|
+
// src/runtime/materialize.ts
|
|
1567
|
+
function resolveColumn(col, event, tx, block) {
|
|
1568
|
+
if ("fromTx" in col) {
|
|
1569
|
+
return { ok: true, value: tx[col.fromTx] };
|
|
1570
|
+
}
|
|
1571
|
+
if ("fromBlock" in col) {
|
|
1572
|
+
return { ok: true, value: block[col.fromBlock] };
|
|
1573
|
+
}
|
|
1574
|
+
const data = event.data;
|
|
1575
|
+
const dataObj = data !== null && typeof data === "object" && !Array.isArray(data) ? data : undefined;
|
|
1576
|
+
if (dataObj && Object.prototype.hasOwnProperty.call(dataObj, col.from)) {
|
|
1577
|
+
return { ok: true, value: dataObj[col.from] };
|
|
1578
|
+
}
|
|
1579
|
+
if (col.from === "topic" && "topic" in event) {
|
|
1580
|
+
return { ok: true, value: event.topic };
|
|
1581
|
+
}
|
|
1582
|
+
if (Object.prototype.hasOwnProperty.call(event, col.from)) {
|
|
1583
|
+
return { ok: true, value: event[col.from] };
|
|
1584
|
+
}
|
|
1585
|
+
return { ok: false, missing: col.from };
|
|
1586
|
+
}
|
|
1587
|
+
function buildMaterializeRow(spec, event, ctx, tableColumns) {
|
|
1588
|
+
const row = {};
|
|
1589
|
+
for (const [colName, mapping] of Object.entries(spec.columns)) {
|
|
1590
|
+
const resolved = resolveColumn(mapping, event, ctx.tx, ctx.block);
|
|
1591
|
+
if (!resolved.ok) {
|
|
1592
|
+
const colDef = tableColumns[colName];
|
|
1593
|
+
if (colDef?.nullable) {
|
|
1594
|
+
row[colName] = null;
|
|
1595
|
+
continue;
|
|
1596
|
+
}
|
|
1597
|
+
return {
|
|
1598
|
+
ok: false,
|
|
1599
|
+
reason: `required materialize field "${resolved.missing}" missing for column "${colName}"`
|
|
1600
|
+
};
|
|
1601
|
+
}
|
|
1602
|
+
row[colName] = resolved.value ?? null;
|
|
1603
|
+
}
|
|
1604
|
+
return { ok: true, row };
|
|
1605
|
+
}
|
|
1606
|
+
function applyMaterializeInsert(spec, event, ctx, schema) {
|
|
1607
|
+
const table = schema[spec.table];
|
|
1608
|
+
if (!table) {
|
|
1609
|
+
return { ok: false, reason: `materialize table "${spec.table}" missing` };
|
|
1610
|
+
}
|
|
1611
|
+
const built = buildMaterializeRow(spec, event, ctx, table.columns);
|
|
1612
|
+
if (!built.ok)
|
|
1613
|
+
return built;
|
|
1614
|
+
ctx.insert(spec.table, built.row);
|
|
1615
|
+
return { ok: true };
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1553
1618
|
// src/runtime/print-validate.ts
|
|
1554
1619
|
var COLUMN_TYPES = new Set([
|
|
1555
1620
|
"text",
|
|
@@ -1629,21 +1694,62 @@ function validatePrintPayload(prints, topic, data) {
|
|
|
1629
1694
|
return matchesTopic(declared, data);
|
|
1630
1695
|
}
|
|
1631
1696
|
|
|
1632
|
-
// src/runtime/
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1697
|
+
// src/runtime/violations.ts
|
|
1698
|
+
import { getTargetDb } from "@secondlayer/shared/db";
|
|
1699
|
+
import { logger as logger3 } from "@secondlayer/shared/logger";
|
|
1700
|
+
import { sql as sql2 } from "kysely";
|
|
1701
|
+
var VIOLATIONS_CAP_PER_SUBGRAPH = 100;
|
|
1702
|
+
function jsonSafe2(value) {
|
|
1703
|
+
if (typeof value === "bigint")
|
|
1704
|
+
return value.toString();
|
|
1705
|
+
if (value === null || value === undefined)
|
|
1706
|
+
return value;
|
|
1707
|
+
if (Array.isArray(value))
|
|
1708
|
+
return value.map(jsonSafe2);
|
|
1709
|
+
if (typeof value === "object") {
|
|
1710
|
+
const out = {};
|
|
1711
|
+
for (const [k, v] of Object.entries(value)) {
|
|
1712
|
+
out[k] = jsonSafe2(v);
|
|
1713
|
+
}
|
|
1714
|
+
return out;
|
|
1644
1715
|
}
|
|
1645
|
-
return
|
|
1716
|
+
return value;
|
|
1717
|
+
}
|
|
1718
|
+
async function defaultRecordPrintViolation(input) {
|
|
1719
|
+
try {
|
|
1720
|
+
const db = getTargetDb();
|
|
1721
|
+
const sample = jsonSafe2(input.samplePayload);
|
|
1722
|
+
await db.insertInto("subgraph_violations").values({
|
|
1723
|
+
subgraph_name: input.subgraphName,
|
|
1724
|
+
source_name: input.sourceName,
|
|
1725
|
+
block_height: input.blockHeight,
|
|
1726
|
+
tx_id: input.txId,
|
|
1727
|
+
reason: input.reason,
|
|
1728
|
+
sample_payload: sample
|
|
1729
|
+
}).execute();
|
|
1730
|
+
await sql2`
|
|
1731
|
+
DELETE FROM subgraph_violations
|
|
1732
|
+
WHERE id IN (
|
|
1733
|
+
SELECT id FROM subgraph_violations
|
|
1734
|
+
WHERE subgraph_name = ${input.subgraphName}
|
|
1735
|
+
ORDER BY seen_at DESC
|
|
1736
|
+
OFFSET ${VIOLATIONS_CAP_PER_SUBGRAPH}
|
|
1737
|
+
)
|
|
1738
|
+
`.execute(db);
|
|
1739
|
+
} catch (err) {
|
|
1740
|
+
logger3.warn("Failed to record print violation", {
|
|
1741
|
+
subgraph: input.subgraphName,
|
|
1742
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
var recorder = defaultRecordPrintViolation;
|
|
1747
|
+
async function recordPrintViolation(input) {
|
|
1748
|
+
await recorder(input);
|
|
1646
1749
|
}
|
|
1750
|
+
|
|
1751
|
+
// src/runtime/runner.ts
|
|
1752
|
+
var DEFAULT_ERROR_THRESHOLD = 50;
|
|
1647
1753
|
function decodeFunctionArgs2(args) {
|
|
1648
1754
|
let parsed = args;
|
|
1649
1755
|
if (typeof parsed === "string") {
|
|
@@ -1891,16 +1997,18 @@ async function runHandlers(subgraph, matched, ctx, opts) {
|
|
|
1891
1997
|
units.sort((a, b) => (a.tx.tx_index ?? 0) - (b.tx.tx_index ?? 0) || (a.event?.event_index ?? -1) - (b.event?.event_index ?? -1));
|
|
1892
1998
|
for (const { tx, event, sourceName } of units) {
|
|
1893
1999
|
if (errors >= threshold) {
|
|
1894
|
-
|
|
2000
|
+
logger4.error("Subgraph error threshold reached, skipping remaining events", {
|
|
1895
2001
|
subgraph: subgraph.name,
|
|
1896
2002
|
errors,
|
|
1897
2003
|
threshold
|
|
1898
2004
|
});
|
|
1899
2005
|
return { processed, errors, ...skipped > 0 ? { skipped } : {} };
|
|
1900
2006
|
}
|
|
2007
|
+
const filter = filterLookup.get(sourceName);
|
|
2008
|
+
const materialize = filter && "materialize" in filter && filter.materialize !== undefined ? filter.materialize : undefined;
|
|
1901
2009
|
const handler = subgraph.handlers[sourceName] ?? subgraph.handlers["*"] ?? null;
|
|
1902
|
-
if (!handler) {
|
|
1903
|
-
|
|
2010
|
+
if (!handler && !materialize) {
|
|
2011
|
+
logger4.warn("No handler found for source", {
|
|
1904
2012
|
subgraph: subgraph.name,
|
|
1905
2013
|
sourceName,
|
|
1906
2014
|
txId: tx.tx_id
|
|
@@ -1915,7 +2023,6 @@ async function runHandlers(subgraph, matched, ctx, opts) {
|
|
|
1915
2023
|
contractId: tx.contract_id ?? null,
|
|
1916
2024
|
functionName: tx.function_name ?? null
|
|
1917
2025
|
});
|
|
1918
|
-
const filter = filterLookup.get(sourceName);
|
|
1919
2026
|
const checkpoint = ctx.opsCheckpoint();
|
|
1920
2027
|
try {
|
|
1921
2028
|
let payload;
|
|
@@ -1957,22 +2064,48 @@ async function runHandlers(subgraph, matched, ctx, opts) {
|
|
|
1957
2064
|
const verdict = validatePrintPayload(filter.prints, printPayload.topic ?? "", printPayload.data ?? {});
|
|
1958
2065
|
if (!verdict.ok) {
|
|
1959
2066
|
skipped++;
|
|
1960
|
-
|
|
2067
|
+
logger4.warn("Print payload does not match declared prints schema", {
|
|
1961
2068
|
subgraph: subgraph.name,
|
|
1962
2069
|
sourceName,
|
|
1963
2070
|
topic: printPayload.topic,
|
|
1964
2071
|
txId: tx.tx_id,
|
|
1965
2072
|
reason: verdict.reason
|
|
1966
2073
|
});
|
|
2074
|
+
recordPrintViolation({
|
|
2075
|
+
subgraphName: subgraph.name,
|
|
2076
|
+
sourceName,
|
|
2077
|
+
blockHeight: ctx.block.height,
|
|
2078
|
+
txId: tx.tx_id,
|
|
2079
|
+
reason: verdict.reason,
|
|
2080
|
+
samplePayload: {
|
|
2081
|
+
topic: printPayload.topic ?? "",
|
|
2082
|
+
data: printPayload.data ?? {}
|
|
2083
|
+
}
|
|
2084
|
+
});
|
|
1967
2085
|
continue;
|
|
1968
2086
|
}
|
|
1969
2087
|
}
|
|
1970
|
-
|
|
1971
|
-
|
|
2088
|
+
if (materialize) {
|
|
2089
|
+
const result = applyMaterializeInsert(materialize, payload, ctx, subgraph.schema);
|
|
2090
|
+
if (!result.ok) {
|
|
2091
|
+
skipped++;
|
|
2092
|
+
logger4.warn("Materialize skipped: required field missing", {
|
|
2093
|
+
subgraph: subgraph.name,
|
|
2094
|
+
sourceName,
|
|
2095
|
+
txId: tx.tx_id,
|
|
2096
|
+
reason: result.reason
|
|
2097
|
+
});
|
|
2098
|
+
continue;
|
|
2099
|
+
}
|
|
2100
|
+
processed++;
|
|
2101
|
+
} else if (handler) {
|
|
2102
|
+
await handler(payload, ctx);
|
|
2103
|
+
processed++;
|
|
2104
|
+
}
|
|
1972
2105
|
} catch (err) {
|
|
1973
2106
|
ctx.rollbackTo(checkpoint);
|
|
1974
2107
|
errors++;
|
|
1975
|
-
|
|
2108
|
+
logger4.error("Subgraph handler error", {
|
|
1976
2109
|
subgraph: subgraph.name,
|
|
1977
2110
|
sourceName,
|
|
1978
2111
|
txId: tx.tx_id,
|
|
@@ -1985,7 +2118,7 @@ async function runHandlers(subgraph, matched, ctx, opts) {
|
|
|
1985
2118
|
}
|
|
1986
2119
|
|
|
1987
2120
|
// src/runtime/block-processor.ts
|
|
1988
|
-
import { getTargetDb } from "@secondlayer/shared/db";
|
|
2121
|
+
import { getTargetDb as getTargetDb2 } from "@secondlayer/shared/db";
|
|
1989
2122
|
import { resolveTraitContractIds } from "@secondlayer/shared/db/queries/contracts";
|
|
1990
2123
|
import { recordGapBatch } from "@secondlayer/shared/db/queries/subgraph-gaps";
|
|
1991
2124
|
import { advanceOperationCursor } from "@secondlayer/shared/db/queries/subgraph-operations";
|
|
@@ -1996,15 +2129,15 @@ import {
|
|
|
1996
2129
|
rewindLiveProgress,
|
|
1997
2130
|
updateSubgraphStatus
|
|
1998
2131
|
} from "@secondlayer/shared/db/queries/subgraphs";
|
|
1999
|
-
import { logger as
|
|
2000
|
-
import { sql as
|
|
2132
|
+
import { logger as logger8 } from "@secondlayer/shared/logger";
|
|
2133
|
+
import { sql as sql4 } from "kysely";
|
|
2001
2134
|
|
|
2002
2135
|
// src/runtime/block-source.ts
|
|
2003
2136
|
import { getSourceDb as getSourceDb2 } from "@secondlayer/shared/db";
|
|
2004
2137
|
import {
|
|
2005
2138
|
createInternalIndexHttpClient
|
|
2006
2139
|
} from "@secondlayer/shared/index-http";
|
|
2007
|
-
import { logger as
|
|
2140
|
+
import { logger as logger5 } from "@secondlayer/shared/logger";
|
|
2008
2141
|
|
|
2009
2142
|
// src/runtime/batch-loader.ts
|
|
2010
2143
|
async function loadBlockRange(db, fromHeight, toHeight) {
|
|
@@ -2491,7 +2624,7 @@ class FallbackBlockSource {
|
|
|
2491
2624
|
try {
|
|
2492
2625
|
return await this.primary.getTip();
|
|
2493
2626
|
} catch (err) {
|
|
2494
|
-
|
|
2627
|
+
logger5.warn("block source primary getTip failed — using DB tap", {
|
|
2495
2628
|
error: err instanceof Error ? err.message : String(err)
|
|
2496
2629
|
});
|
|
2497
2630
|
return this.fallback.getTip();
|
|
@@ -2501,7 +2634,7 @@ class FallbackBlockSource {
|
|
|
2501
2634
|
try {
|
|
2502
2635
|
return await this.primary.loadBlockRange(fromHeight, toHeight);
|
|
2503
2636
|
} catch (err) {
|
|
2504
|
-
|
|
2637
|
+
logger5.warn("block source primary loadBlockRange failed — using DB tap", {
|
|
2505
2638
|
from: fromHeight,
|
|
2506
2639
|
to: toHeight,
|
|
2507
2640
|
error: err instanceof Error ? err.message : String(err)
|
|
@@ -2538,7 +2671,7 @@ function resolveBlockSource(subgraph) {
|
|
|
2538
2671
|
return new FallbackBlockSource(new PublicApiBlockSource(buildHttpClient(), referencedIndexEventTypes(subgraph), canSparseScan(subgraph) ? sparseProbeTargets(subgraph) : undefined, needsTransactionData(subgraph)), postgresBlockSource);
|
|
2539
2672
|
}
|
|
2540
2673
|
if (process.env.SUBGRAPH_SOURCE === "streams-index" && subgraph) {
|
|
2541
|
-
|
|
2674
|
+
logger5.debug("Subgraph not streams-index eligible, using DB tap", {
|
|
2542
2675
|
subgraph: subgraph.name
|
|
2543
2676
|
});
|
|
2544
2677
|
}
|
|
@@ -2547,7 +2680,7 @@ function resolveBlockSource(subgraph) {
|
|
|
2547
2680
|
|
|
2548
2681
|
// src/runtime/outbox-emit.ts
|
|
2549
2682
|
import { createHash as createHash2 } from "node:crypto";
|
|
2550
|
-
import { logger as
|
|
2683
|
+
import { logger as logger6 } from "@secondlayer/shared/logger";
|
|
2551
2684
|
var loggedKillSwitch = false;
|
|
2552
2685
|
var OP_VERB = {
|
|
2553
2686
|
insert: "created",
|
|
@@ -2571,7 +2704,7 @@ function stableStringify(obj) {
|
|
|
2571
2704
|
async function emitSubscriptionOutbox(tx, subgraphName, manifest, matcher, blockHeight) {
|
|
2572
2705
|
if (!isEmitOutboxEnabled()) {
|
|
2573
2706
|
if (!loggedKillSwitch) {
|
|
2574
|
-
|
|
2707
|
+
logger6.warn("SECONDLAYER_EMIT_OUTBOX=false — outbox emission bypassed");
|
|
2575
2708
|
loggedKillSwitch = true;
|
|
2576
2709
|
}
|
|
2577
2710
|
return 0;
|
|
@@ -2616,7 +2749,7 @@ function sandboxEnabled(subgraph) {
|
|
|
2616
2749
|
// src/runtime/sandbox/host.ts
|
|
2617
2750
|
import { tmpdir } from "node:os";
|
|
2618
2751
|
import { fileURLToPath } from "node:url";
|
|
2619
|
-
import { logger as
|
|
2752
|
+
import { logger as logger7 } from "@secondlayer/shared/logger";
|
|
2620
2753
|
|
|
2621
2754
|
// src/runtime/sandbox/bundle.ts
|
|
2622
2755
|
import esbuild from "esbuild";
|
|
@@ -2696,7 +2829,7 @@ function spawnSandboxProcess() {
|
|
|
2696
2829
|
break;
|
|
2697
2830
|
const text = decoder.decode(value, { stream: true });
|
|
2698
2831
|
if (text.trim()) {
|
|
2699
|
-
|
|
2832
|
+
logger7.warn("Sandbox subprocess stderr", { stderr: text.trim() });
|
|
2700
2833
|
}
|
|
2701
2834
|
}
|
|
2702
2835
|
} catch {}
|
|
@@ -2754,7 +2887,7 @@ async function ensureSandboxProcess(subgraphName, version, handlerCode) {
|
|
|
2754
2887
|
try {
|
|
2755
2888
|
await initSandboxProcess(existing.proc, subgraphName, version, handlerCode);
|
|
2756
2889
|
existing.version = version;
|
|
2757
|
-
|
|
2890
|
+
logger7.info("Sandbox subprocess handler reloaded", {
|
|
2758
2891
|
subgraph: subgraphName,
|
|
2759
2892
|
version
|
|
2760
2893
|
});
|
|
@@ -2762,7 +2895,7 @@ async function ensureSandboxProcess(subgraphName, version, handlerCode) {
|
|
|
2762
2895
|
} catch (err) {
|
|
2763
2896
|
existing.proc.kill();
|
|
2764
2897
|
pool.delete(subgraphName);
|
|
2765
|
-
|
|
2898
|
+
logger7.warn("Sandbox subprocess re-init failed; respawning", {
|
|
2766
2899
|
subgraph: subgraphName,
|
|
2767
2900
|
error: err instanceof Error ? err.message : String(err)
|
|
2768
2901
|
});
|
|
@@ -2845,7 +2978,7 @@ async function runBlockThroughProcess(entry, subgraphName, hostCtx, block, match
|
|
|
2845
2978
|
return result;
|
|
2846
2979
|
} catch (err) {
|
|
2847
2980
|
evictSandboxWorker(subgraphName);
|
|
2848
|
-
|
|
2981
|
+
logger7.warn("Sandbox subprocess run failed; subprocess evicted", {
|
|
2849
2982
|
subgraph: subgraphName,
|
|
2850
2983
|
blockHeight: block.height,
|
|
2851
2984
|
error: err instanceof Error ? err.message : String(err)
|
|
@@ -2916,7 +3049,7 @@ async function answerRead(hostCtx, msg) {
|
|
|
2916
3049
|
|
|
2917
3050
|
// src/runtime/subscription-state.ts
|
|
2918
3051
|
import { listSubscriptions } from "@secondlayer/shared/db/queries/subscriptions";
|
|
2919
|
-
import { sql as
|
|
3052
|
+
import { sql as sql3 } from "kysely";
|
|
2920
3053
|
|
|
2921
3054
|
// src/runtime/emitter-matcher.ts
|
|
2922
3055
|
function isPrimitive(v) {
|
|
@@ -3080,7 +3213,7 @@ class SubscriptionMatcher {
|
|
|
3080
3213
|
// src/runtime/subscription-state.ts
|
|
3081
3214
|
var matcher = new SubscriptionMatcher;
|
|
3082
3215
|
async function refreshMatcher(db) {
|
|
3083
|
-
const rows = await
|
|
3216
|
+
const rows = await sql3`
|
|
3084
3217
|
SELECT * FROM subscriptions WHERE status = 'active'
|
|
3085
3218
|
`.execute(db);
|
|
3086
3219
|
matcher.setAll(rows.rows);
|
|
@@ -3102,7 +3235,7 @@ async function resolveRoute(subgraphName, targetDb) {
|
|
|
3102
3235
|
version: row?.version ?? ""
|
|
3103
3236
|
};
|
|
3104
3237
|
routeCache.set(subgraphName, route);
|
|
3105
|
-
|
|
3238
|
+
logger8.info("subgraph handler dispatch path resolved", {
|
|
3106
3239
|
subgraph: subgraphName,
|
|
3107
3240
|
path: sandboxEnabled({ sandbox_workers: route.sandboxWorkers }) ? "sandbox" : "in-process"
|
|
3108
3241
|
});
|
|
@@ -3126,8 +3259,8 @@ async function resolveFactoryContracts(subgraph, blockHeight, schemaName, db, tx
|
|
|
3126
3259
|
for (const [discoveringSource, factory] of factories) {
|
|
3127
3260
|
const known = new Set;
|
|
3128
3261
|
try {
|
|
3129
|
-
const rows = await
|
|
3130
|
-
SELECT address FROM ${
|
|
3262
|
+
const rows = await sql4`
|
|
3263
|
+
SELECT address FROM ${sql4.raw(`"${schemaName}"."_factory_addresses"`)}
|
|
3131
3264
|
WHERE source_name = ${discoveringSource} AND block_height <= ${blockHeight}
|
|
3132
3265
|
`.execute(db);
|
|
3133
3266
|
for (const row of rows.rows)
|
|
@@ -3155,8 +3288,8 @@ async function persistFactoryDiscoveries(schemaName, db, blockHeight, discovered
|
|
|
3155
3288
|
if (discovered.length === 0)
|
|
3156
3289
|
return;
|
|
3157
3290
|
for (const { sourceName, address } of discovered) {
|
|
3158
|
-
await
|
|
3159
|
-
INSERT INTO ${
|
|
3291
|
+
await sql4`
|
|
3292
|
+
INSERT INTO ${sql4.raw(`"${schemaName}"."_factory_addresses"`)}
|
|
3160
3293
|
(source_name, address, block_height)
|
|
3161
3294
|
VALUES (${sourceName}, ${address}, ${blockHeight})
|
|
3162
3295
|
ON CONFLICT (source_name, address) DO NOTHING
|
|
@@ -3216,7 +3349,7 @@ async function processBlockWithRetry(subgraph, subgraphName, blockHeight, opts,
|
|
|
3216
3349
|
const delay = retryDelaysMs[attempt];
|
|
3217
3350
|
if (delay === undefined)
|
|
3218
3351
|
break;
|
|
3219
|
-
|
|
3352
|
+
logger8.warn("Block processing failed, retrying", {
|
|
3220
3353
|
subgraph: subgraphName,
|
|
3221
3354
|
blockHeight,
|
|
3222
3355
|
attempt: attempt + 1,
|
|
@@ -3229,7 +3362,7 @@ async function processBlockWithRetry(subgraph, subgraphName, blockHeight, opts,
|
|
|
3229
3362
|
throw lastError;
|
|
3230
3363
|
}
|
|
3231
3364
|
async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
3232
|
-
const targetDb =
|
|
3365
|
+
const targetDb = getTargetDb2();
|
|
3233
3366
|
const blockStart = performance.now();
|
|
3234
3367
|
const result = {
|
|
3235
3368
|
blockHeight,
|
|
@@ -3248,7 +3381,7 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
3248
3381
|
} else {
|
|
3249
3382
|
const data = (await resolveBlockSource(subgraph).loadBlockRange(blockHeight, blockHeight)).get(blockHeight);
|
|
3250
3383
|
if (!data) {
|
|
3251
|
-
|
|
3384
|
+
logger8.debug("Block not found or non-canonical for subgraph processing", {
|
|
3252
3385
|
subgraph: subgraphName,
|
|
3253
3386
|
blockHeight
|
|
3254
3387
|
});
|
|
@@ -3314,7 +3447,7 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
3314
3447
|
{ start: blockHeight, end: blockHeight, reason: "processing_error" }
|
|
3315
3448
|
]);
|
|
3316
3449
|
} else {
|
|
3317
|
-
|
|
3450
|
+
logger8.warn("Block errors not recorded as a gap — no subgraphId", {
|
|
3318
3451
|
subgraph: subgraphName,
|
|
3319
3452
|
blockHeight,
|
|
3320
3453
|
errors: rr.errors
|
|
@@ -3387,7 +3520,7 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
3387
3520
|
});
|
|
3388
3521
|
} catch (err) {
|
|
3389
3522
|
if (err instanceof CursorRaceLostError || err instanceof LiveCursorRaceLostError) {
|
|
3390
|
-
|
|
3523
|
+
logger8.warn("cursor race lost — block already covered", {
|
|
3391
3524
|
subgraph: subgraphName,
|
|
3392
3525
|
blockHeight,
|
|
3393
3526
|
error: err.message
|
|
@@ -3412,10 +3545,10 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
3412
3545
|
try {
|
|
3413
3546
|
const tables = Object.keys(subgraph.schema);
|
|
3414
3547
|
for (const table of tables) {
|
|
3415
|
-
const { rows } = await
|
|
3548
|
+
const { rows } = await sql4.raw(`SELECT n_live_tup AS count FROM pg_stat_user_tables WHERE schemaname = '${schemaName}' AND relname = '${table}'`).execute(route.dataDb);
|
|
3416
3549
|
const count = Number(rows[0]?.count ?? 0);
|
|
3417
3550
|
if (count >= 1e7) {
|
|
3418
|
-
|
|
3551
|
+
logger8.warn("Subgraph table exceeds 10M rows (estimate)", {
|
|
3419
3552
|
subgraph: subgraphName,
|
|
3420
3553
|
table,
|
|
3421
3554
|
count
|
|
@@ -3423,13 +3556,13 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
3423
3556
|
}
|
|
3424
3557
|
}
|
|
3425
3558
|
} catch (err) {
|
|
3426
|
-
|
|
3559
|
+
logger8.debug("Row count sample failed", {
|
|
3427
3560
|
subgraph: subgraphName,
|
|
3428
3561
|
error: err instanceof Error ? err.message : String(err)
|
|
3429
3562
|
});
|
|
3430
3563
|
}
|
|
3431
3564
|
if (journalEnabled(opts)) {
|
|
3432
|
-
await
|
|
3565
|
+
await sql4.raw(`DELETE FROM "${schemaName}"."_journal" WHERE "block_height" < ${blockHeight - JOURNAL_RETENTION_BLOCKS}`).execute(route.dataDb).catch(() => {});
|
|
3433
3566
|
}
|
|
3434
3567
|
}
|
|
3435
3568
|
return result;
|
|
@@ -3499,9 +3632,9 @@ class StatsAccumulator {
|
|
|
3499
3632
|
}
|
|
3500
3633
|
|
|
3501
3634
|
// src/runtime/catchup.ts
|
|
3502
|
-
import { getTargetDb as
|
|
3635
|
+
import { getTargetDb as getTargetDb3, sql as sql5 } from "@secondlayer/shared/db";
|
|
3503
3636
|
import { getSubgraph } from "@secondlayer/shared/db/queries/subgraphs";
|
|
3504
|
-
import { logger as
|
|
3637
|
+
import { logger as logger9 } from "@secondlayer/shared/logger";
|
|
3505
3638
|
|
|
3506
3639
|
// src/runtime/catchup-leader.ts
|
|
3507
3640
|
import {
|
|
@@ -3631,8 +3764,8 @@ async function withSubgraphBlockLock(name, fn) {
|
|
|
3631
3764
|
}
|
|
3632
3765
|
}
|
|
3633
3766
|
async function withSubgraphBlockAdvisoryLock(name, fn) {
|
|
3634
|
-
return
|
|
3635
|
-
await
|
|
3767
|
+
return getTargetDb3().transaction().execute(async (lockTx) => {
|
|
3768
|
+
await sql5`SELECT pg_advisory_xact_lock(hashtext(${`subgraph-block:${name}`}))`.execute(lockTx);
|
|
3636
3769
|
return fn();
|
|
3637
3770
|
});
|
|
3638
3771
|
}
|
|
@@ -3693,7 +3826,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3693
3826
|
catchingUp.add(subgraphName);
|
|
3694
3827
|
try {
|
|
3695
3828
|
const source = resolveBlockSource(subgraph);
|
|
3696
|
-
const targetDb =
|
|
3829
|
+
const targetDb = getTargetDb3();
|
|
3697
3830
|
const subgraphRow = await getSubgraph(targetDb, subgraphName);
|
|
3698
3831
|
if (!subgraphRow)
|
|
3699
3832
|
return 0;
|
|
@@ -3705,7 +3838,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3705
3838
|
if (usesDecodedIndexPlane(subgraph)) {
|
|
3706
3839
|
const bound = await boundSourceTip(rawTip, decoderNamesForSubgraph(subgraph));
|
|
3707
3840
|
if (!bound.ok) {
|
|
3708
|
-
|
|
3841
|
+
logger9.warn("Subgraph catch-up stalled: missing decoder checkpoint", {
|
|
3709
3842
|
event: "subgraph_catchup_decoder_stall",
|
|
3710
3843
|
subgraph: subgraphName,
|
|
3711
3844
|
missing: bound.missing
|
|
@@ -3713,7 +3846,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3713
3846
|
return 0;
|
|
3714
3847
|
}
|
|
3715
3848
|
if (bound.floor !== null && bound.floor < rawTip) {
|
|
3716
|
-
|
|
3849
|
+
logger9.debug("Subgraph catch-up tip bounded by decoder progress", {
|
|
3717
3850
|
event: "subgraph_catchup_decoder_floor",
|
|
3718
3851
|
subgraph: subgraphName,
|
|
3719
3852
|
rawTip,
|
|
@@ -3728,7 +3861,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3728
3861
|
const subgraphStart = Number(subgraphRow.start_block) || 1;
|
|
3729
3862
|
const startBlock = Math.max(lastProcessedBlock + 1, subgraphStart);
|
|
3730
3863
|
const totalBlocks = chainTip - lastProcessedBlock;
|
|
3731
|
-
|
|
3864
|
+
logger9.info("Subgraph catch-up starting", {
|
|
3732
3865
|
subgraph: subgraphName,
|
|
3733
3866
|
from: startBlock,
|
|
3734
3867
|
to: chainTip,
|
|
@@ -3744,7 +3877,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3744
3877
|
let nextBatchPromise = batchConfig.prefetch ? source.loadBlockRange(currentHeight, prefetchedBatchEnd) : undefined;
|
|
3745
3878
|
while (currentHeight <= chainTip) {
|
|
3746
3879
|
if (!isCatchUpLeader()) {
|
|
3747
|
-
|
|
3880
|
+
logger9.info("Lost catch-up leadership, stopping catch-up", {
|
|
3748
3881
|
subgraph: subgraphName,
|
|
3749
3882
|
processed
|
|
3750
3883
|
});
|
|
@@ -3752,7 +3885,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3752
3885
|
}
|
|
3753
3886
|
const currentRow = await getSubgraph(targetDb, subgraphName);
|
|
3754
3887
|
if (!currentRow || currentRow.status !== "active") {
|
|
3755
|
-
|
|
3888
|
+
logger9.info("Subgraph status changed, stopping catch-up", {
|
|
3756
3889
|
subgraph: subgraphName,
|
|
3757
3890
|
status: currentRow?.status ?? "deleted"
|
|
3758
3891
|
});
|
|
@@ -3781,7 +3914,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3781
3914
|
blockData = (await source.loadBlockRange(height, height)).get(height);
|
|
3782
3915
|
}
|
|
3783
3916
|
if (!blockData) {
|
|
3784
|
-
|
|
3917
|
+
logger9.warn("Block missing during catch-up, deferring to next tick", {
|
|
3785
3918
|
subgraph: subgraphName,
|
|
3786
3919
|
blockHeight: height
|
|
3787
3920
|
});
|
|
@@ -3802,12 +3935,12 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3802
3935
|
});
|
|
3803
3936
|
} catch (err) {
|
|
3804
3937
|
if (err instanceof ReorgEpochChangedError) {
|
|
3805
|
-
|
|
3938
|
+
logger9.info("Reorg detected mid catch-up, aborting walk without writing further progress", { subgraph: subgraphName, blockHeight: height });
|
|
3806
3939
|
stopCatchup = true;
|
|
3807
3940
|
break;
|
|
3808
3941
|
}
|
|
3809
3942
|
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
3810
|
-
|
|
3943
|
+
logger9.error("Block processing failed persistently during catch-up", {
|
|
3811
3944
|
subgraph: subgraphName,
|
|
3812
3945
|
blockHeight: height,
|
|
3813
3946
|
error: errorMsg
|
|
@@ -3826,7 +3959,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3826
3959
|
}
|
|
3827
3960
|
}
|
|
3828
3961
|
if (processed % LOG_INTERVAL === 0) {
|
|
3829
|
-
|
|
3962
|
+
logger9.info("Subgraph catch-up progress", {
|
|
3830
3963
|
subgraph: subgraphName,
|
|
3831
3964
|
processed,
|
|
3832
3965
|
total: totalBlocks,
|
|
@@ -3842,7 +3975,7 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
3842
3975
|
currentHeight = batchEnd + 1;
|
|
3843
3976
|
}
|
|
3844
3977
|
await stats.flush(targetDb);
|
|
3845
|
-
|
|
3978
|
+
logger9.info("Subgraph catch-up complete", {
|
|
3846
3979
|
subgraph: subgraphName,
|
|
3847
3980
|
processed
|
|
3848
3981
|
});
|
|
@@ -3860,5 +3993,5 @@ export {
|
|
|
3860
3993
|
bumpReorgEpoch
|
|
3861
3994
|
};
|
|
3862
3995
|
|
|
3863
|
-
//# debugId=
|
|
3996
|
+
//# debugId=EAA60BDA783421E364756E2164756E21
|
|
3864
3997
|
//# sourceMappingURL=catchup.js.map
|