@secondlayer/subgraphs 4.1.5 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/src/index.d.ts +87 -29
  2. package/dist/src/index.js +376 -128
  3. package/dist/src/index.js.map +9 -7
  4. package/dist/src/runtime/block-processor.d.ts +72 -23
  5. package/dist/src/runtime/block-processor.js +186 -53
  6. package/dist/src/runtime/block-processor.js.map +7 -5
  7. package/dist/src/runtime/catchup.d.ts +72 -23
  8. package/dist/src/runtime/catchup.js +201 -68
  9. package/dist/src/runtime/catchup.js.map +7 -5
  10. package/dist/src/runtime/processor.js +281 -147
  11. package/dist/src/runtime/processor.js.map +8 -6
  12. package/dist/src/runtime/reindex.d.ts +72 -23
  13. package/dist/src/runtime/reindex.js +238 -105
  14. package/dist/src/runtime/reindex.js.map +7 -5
  15. package/dist/src/runtime/reorg.d.ts +72 -23
  16. package/dist/src/runtime/reorg.js +215 -81
  17. package/dist/src/runtime/reorg.js.map +8 -6
  18. package/dist/src/runtime/runner.d.ts +72 -23
  19. package/dist/src/runtime/runner.js +156 -23
  20. package/dist/src/runtime/runner.js.map +8 -6
  21. package/dist/src/runtime/source-matcher.d.ts +72 -23
  22. package/dist/src/schema/index.d.ts +72 -23
  23. package/dist/src/schema/index.js +115 -1
  24. package/dist/src/schema/index.js.map +3 -3
  25. package/dist/src/service.js +286 -152
  26. package/dist/src/service.js.map +8 -6
  27. package/dist/src/testing/index.d.ts +195 -24
  28. package/dist/src/testing/index.js +448 -25
  29. package/dist/src/testing/index.js.map +9 -6
  30. package/dist/src/triggers/index.d.ts +83 -24
  31. package/dist/src/triggers/index.js +5 -2
  32. package/dist/src/triggers/index.js.map +3 -3
  33. package/dist/src/types.d.ts +73 -24
  34. package/dist/src/validate.d.ts +72 -23
  35. package/dist/src/validate.js +115 -1
  36. package/dist/src/validate.js.map +3 -3
  37. 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 logger3 } from "@secondlayer/shared/logger";
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/runner.ts
1633
- var DEFAULT_ERROR_THRESHOLD = 50;
1634
- function camelizeKeys(obj) {
1635
- if (obj === null || obj === undefined)
1636
- return obj;
1637
- if (typeof obj !== "object")
1638
- return obj;
1639
- if (Array.isArray(obj))
1640
- return obj.map(camelizeKeys);
1641
- const result = {};
1642
- for (const [k, v] of Object.entries(obj)) {
1643
- result[camelizeDataKey(k)] = camelizeKeys(v);
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 result;
1716
+ return value;
1646
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);
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
- logger3.error("Subgraph error threshold reached, skipping remaining events", {
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
- logger3.warn("No handler found for source", {
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
- logger3.warn("Print payload does not match declared prints schema", {
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
+ });
2085
+ continue;
2086
+ }
2087
+ }
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
+ });
1967
2098
  continue;
1968
2099
  }
2100
+ processed++;
2101
+ } else if (handler) {
2102
+ await handler(payload, ctx);
2103
+ processed++;
1969
2104
  }
1970
- await handler(payload, ctx);
1971
- processed++;
1972
2105
  } catch (err) {
1973
2106
  ctx.rollbackTo(checkpoint);
1974
2107
  errors++;
1975
- logger3.error("Subgraph handler error", {
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 logger7 } from "@secondlayer/shared/logger";
2000
- import { sql as sql3 } from "kysely";
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 logger4 } from "@secondlayer/shared/logger";
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
- logger4.warn("block source primary getTip failed — using DB tap", {
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
- logger4.warn("block source primary loadBlockRange failed — using DB tap", {
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
- logger4.debug("Subgraph not streams-index eligible, using DB tap", {
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 logger5 } from "@secondlayer/shared/logger";
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
- logger5.warn("SECONDLAYER_EMIT_OUTBOX=false — outbox emission bypassed");
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 logger6 } from "@secondlayer/shared/logger";
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
- logger6.warn("Sandbox subprocess stderr", { stderr: text.trim() });
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
- logger6.info("Sandbox subprocess handler reloaded", {
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
- logger6.warn("Sandbox subprocess re-init failed; respawning", {
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
- logger6.warn("Sandbox subprocess run failed; subprocess evicted", {
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 sql2 } from "kysely";
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 sql2`
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
- logger7.info("subgraph handler dispatch path resolved", {
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 sql3`
3130
- SELECT address FROM ${sql3.raw(`"${schemaName}"."_factory_addresses"`)}
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 sql3`
3159
- INSERT INTO ${sql3.raw(`"${schemaName}"."_factory_addresses"`)}
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
- logger7.warn("Block processing failed, retrying", {
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 = getTargetDb();
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
- logger7.debug("Block not found or non-canonical for subgraph processing", {
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
- logger7.warn("Block errors not recorded as a gap — no subgraphId", {
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
- logger7.warn("cursor race lost — block already covered", {
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 sql3.raw(`SELECT n_live_tup AS count FROM pg_stat_user_tables WHERE schemaname = '${schemaName}' AND relname = '${table}'`).execute(route.dataDb);
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
- logger7.warn("Subgraph table exceeds 10M rows (estimate)", {
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
- logger7.debug("Row count sample failed", {
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 sql3.raw(`DELETE FROM "${schemaName}"."_journal" WHERE "block_height" < ${blockHeight - JOURNAL_RETENTION_BLOCKS}`).execute(route.dataDb).catch(() => {});
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 getTargetDb2, sql as sql4 } from "@secondlayer/shared/db";
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 logger8 } from "@secondlayer/shared/logger";
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 getTargetDb2().transaction().execute(async (lockTx) => {
3635
- await sql4`SELECT pg_advisory_xact_lock(hashtext(${`subgraph-block:${name}`}))`.execute(lockTx);
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 = getTargetDb2();
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
- logger8.warn("Subgraph catch-up stalled: missing decoder checkpoint", {
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
- logger8.debug("Subgraph catch-up tip bounded by decoder progress", {
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
- logger8.info("Subgraph catch-up starting", {
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
- logger8.info("Lost catch-up leadership, stopping catch-up", {
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
- logger8.info("Subgraph status changed, stopping catch-up", {
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
- logger8.warn("Block missing during catch-up, deferring to next tick", {
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
- logger8.info("Reorg detected mid catch-up, aborting walk without writing further progress", { subgraph: subgraphName, blockHeight: height });
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
- logger8.error("Block processing failed persistently during catch-up", {
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
- logger8.info("Subgraph catch-up progress", {
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
- logger8.info("Subgraph catch-up complete", {
3978
+ logger9.info("Subgraph catch-up complete", {
3846
3979
  subgraph: subgraphName,
3847
3980
  processed
3848
3981
  });
@@ -3854,16 +3987,16 @@ async function catchUpSubgraph(subgraph, subgraphName) {
3854
3987
 
3855
3988
  // src/runtime/reorg.ts
3856
3989
  import { getErrorMessage as getErrorMessage2 } from "@secondlayer/shared";
3857
- import { getRawClient, getTargetDb as getTargetDb3 } from "@secondlayer/shared/db";
3990
+ import { getRawClient, getTargetDb as getTargetDb4 } from "@secondlayer/shared/db";
3858
3991
  import { listSubgraphs } from "@secondlayer/shared/db/queries/subgraphs";
3859
- import { logger as logger9 } from "@secondlayer/shared/logger";
3860
- import { sql as sql5 } from "kysely";
3992
+ import { logger as logger10 } from "@secondlayer/shared/logger";
3993
+ import { sql as sql6 } from "kysely";
3861
3994
  async function handleSubgraphReorg(blockHeight, loadSubgraphDef) {
3862
- const targetDb = getTargetDb3();
3995
+ const targetDb = getTargetDb4();
3863
3996
  const activeSubgraphs = (await listSubgraphs(targetDb)).filter((v) => v.status === "active");
3864
3997
  if (activeSubgraphs.length === 0)
3865
3998
  return;
3866
- logger9.info("Propagating reorg to subgraphs", {
3999
+ logger10.info("Propagating reorg to subgraphs", {
3867
4000
  blockHeight,
3868
4001
  subgraphCount: activeSubgraphs.length
3869
4002
  });
@@ -3874,13 +4007,13 @@ async function handleSubgraphReorg(blockHeight, loadSubgraphDef) {
3874
4007
  continue;
3875
4008
  bumpReorgEpoch(sg.name);
3876
4009
  await withSubgraphBlockLock(sg.name, async () => {
3877
- await getTargetDb3().transaction().execute(async (lockTx) => {
3878
- await sql5`SELECT pg_advisory_xact_lock(hashtext(${`subgraph-reorg:${sg.name}`}))`.execute(lockTx);
4010
+ await getTargetDb4().transaction().execute(async (lockTx) => {
4011
+ await sql6`SELECT pg_advisory_xact_lock(hashtext(${`subgraph-reorg:${sg.name}`}))`.execute(lockTx);
3879
4012
  await reorgOneSubgraph(sg, schema, blockHeight, loadSubgraphDef);
3880
4013
  });
3881
4014
  });
3882
4015
  } catch (err) {
3883
- logger9.error("Subgraph reorg handling failed", {
4016
+ logger10.error("Subgraph reorg handling failed", {
3884
4017
  subgraph: sg.name,
3885
4018
  blockHeight,
3886
4019
  error: getErrorMessage2(err)
@@ -3889,7 +4022,7 @@ async function handleSubgraphReorg(blockHeight, loadSubgraphDef) {
3889
4022
  }
3890
4023
  }
3891
4024
  async function reorgOneSubgraph(sg, schema, blockHeight, loadSubgraphDef) {
3892
- const targetDb = getTargetDb3();
4025
+ const targetDb = getTargetDb4();
3893
4026
  const schemaName = sg.schema_name ?? pgSchemaName(sg.name);
3894
4027
  const client = getRawClient("target");
3895
4028
  const tableNames = Object.keys(schema);
@@ -3918,7 +4051,7 @@ async function reorgOneSubgraph(sg, schema, blockHeight, loadSubgraphDef) {
3918
4051
  await tx.unsafe(`DELETE FROM "${schemaName}"."_journal" WHERE block_height >= $1`, [blockHeight]);
3919
4052
  });
3920
4053
  } else {
3921
- logger9.warn("Subgraph has no revert journal — falling back to height delete (accumulator rows may lose history)", { subgraph: sg.name, blockHeight });
4054
+ logger10.warn("Subgraph has no revert journal — falling back to height delete (accumulator rows may lose history)", { subgraph: sg.name, blockHeight });
3922
4055
  for (const tableName of tableNames) {
3923
4056
  await client.unsafe(`DELETE FROM "${schemaName}"."${tableName}" WHERE "_block_height" >= $1`, [blockHeight]);
3924
4057
  }
@@ -3928,6 +4061,7 @@ async function reorgOneSubgraph(sg, schema, blockHeight, loadSubgraphDef) {
3928
4061
  DELETE FROM "${schemaName}"."_factory_addresses" WHERE block_height >= ${Number(blockHeight)};
3929
4062
  END IF;
3930
4063
  END $$`);
4064
+ await targetDb.deleteFrom("subgraph_violations").where("subgraph_name", "=", sg.name).where("block_height", ">=", blockHeight).execute();
3931
4065
  for (const [tableName, rows] of Object.entries(revertedByTable)) {
3932
4066
  if (rows.length === 0)
3933
4067
  continue;
@@ -3961,7 +4095,7 @@ async function reorgOneSubgraph(sg, schema, blockHeight, loadSubgraphDef) {
3961
4095
  }).as("row_pk"),
3962
4096
  eb2.val("pending").as("status")
3963
4097
  ]).where("subgraph_name", "=", sg.name).where("table_name", "=", tableName).where("status", "=", "active")).onConflict((oc) => oc.columns(["subscription_id", "dedup_key"]).doNothing()).execute().catch((err) => {
3964
- logger9.error("Failed to emit revert event for subscriptions", {
4098
+ logger10.error("Failed to emit revert event for subscriptions", {
3965
4099
  event: "reorg_revert_emit_dropped",
3966
4100
  subgraph: sg.name,
3967
4101
  table: tableName,
@@ -3970,14 +4104,14 @@ async function reorgOneSubgraph(sg, schema, blockHeight, loadSubgraphDef) {
3970
4104
  });
3971
4105
  });
3972
4106
  }
3973
- logger9.info("Subgraph reorg cleanup done", {
4107
+ logger10.info("Subgraph reorg cleanup done", {
3974
4108
  subgraph: sg.name,
3975
4109
  blockHeight,
3976
4110
  tablesAffected: Object.keys(revertedByTable).length
3977
4111
  });
3978
4112
  const def = await loadSubgraphDef(sg);
3979
4113
  await processBlock(def, sg.name, blockHeight, { reorgRewind: true });
3980
- logger9.info("Subgraph reorg reprocessed", {
4114
+ logger10.info("Subgraph reorg reprocessed", {
3981
4115
  subgraph: sg.name,
3982
4116
  blockHeight
3983
4117
  });
@@ -3986,5 +4120,5 @@ export {
3986
4120
  handleSubgraphReorg
3987
4121
  };
3988
4122
 
3989
- //# debugId=CCDDEB49D7B7E7B464756E2164756E21
4123
+ //# debugId=C83516F4FE20D8A864756E2164756E21
3990
4124
  //# sourceMappingURL=reorg.js.map