@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.
- 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/processor.js +281 -147
- package/dist/src/runtime/processor.js.map +8 -6
- 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 +286 -152
- package/dist/src/service.js.map +8 -6
- 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
|
@@ -119,7 +119,12 @@ type TraitScope = {
|
|
|
119
119
|
*
|
|
120
120
|
* ```ts
|
|
121
121
|
* sources: {
|
|
122
|
-
* registry: {
|
|
122
|
+
* registry: {
|
|
123
|
+
* type: "print_event",
|
|
124
|
+
* contractId: REGISTRY,
|
|
125
|
+
* topic: "pool-created",
|
|
126
|
+
* prints: { "pool-created": { pool: "principal" } },
|
|
127
|
+
* },
|
|
123
128
|
* swaps: {
|
|
124
129
|
* type: "print_event",
|
|
125
130
|
* topic: "swap",
|
|
@@ -203,6 +208,9 @@ interface ContractCallFilter extends TraitScope, FactoryScope {
|
|
|
203
208
|
* decoded function arguments. Dev-provided; serialized into the deployed
|
|
204
209
|
* definition and used at runtime to decode args by name. Omit to keep
|
|
205
210
|
* `event.args` as a positional `unknown[]`.
|
|
211
|
+
*
|
|
212
|
+
* Deploy validation requires `abi` whenever `functionName` is set
|
|
213
|
+
* (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
|
|
206
214
|
*/
|
|
207
215
|
abi?: AbiContract2;
|
|
208
216
|
}
|
|
@@ -233,29 +241,70 @@ type PrintField = ColumnType | {
|
|
|
233
241
|
} | {
|
|
234
242
|
list: PrintField
|
|
235
243
|
};
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
244
|
+
/**
|
|
245
|
+
* Per-topic field schema for a print_event source. When declared, the handler's
|
|
246
|
+
* `event` is a discriminated union keyed by `topic` and `event.data` is typed
|
|
247
|
+
* per topic. Nested tuples, lists, and optional fields — see {@link PrintField}.
|
|
248
|
+
*
|
|
249
|
+
* Also opts the source into RUNTIME validation: a payload that does not match
|
|
250
|
+
* is skipped and logged rather than written as nulls.
|
|
251
|
+
*/
|
|
252
|
+
type PrintEventPrints = Record<string, Record<string, PrintField>>;
|
|
253
|
+
/**
|
|
254
|
+
* One column in a static identity map. Literals only — the bundler never
|
|
255
|
+
* executes user code, so `{ from: "tokenX" }` is valid and `from("tokenX")`
|
|
256
|
+
* is not.
|
|
257
|
+
*/
|
|
258
|
+
type MaterializeColumn = {
|
|
259
|
+
from: string
|
|
260
|
+
} | {
|
|
261
|
+
fromTx: "sender" | "txId"
|
|
262
|
+
} | {
|
|
263
|
+
fromBlock: "height" | "hash" | "timestamp"
|
|
264
|
+
};
|
|
265
|
+
/** Static insert from a source event — replaces a 1:1 handler. */
|
|
266
|
+
interface MaterializeSpec {
|
|
267
|
+
table: string;
|
|
268
|
+
/** Keys = schema column names (snake). */
|
|
269
|
+
columns: Record<string, MaterializeColumn>;
|
|
258
270
|
}
|
|
271
|
+
type PrintEventFields = FactoryScope & {
|
|
272
|
+
type: "print_event"
|
|
273
|
+
topic?: string
|
|
274
|
+
/** Identity map: insert without a handler function. XOR with handlers[name]. */
|
|
275
|
+
materialize?: MaterializeSpec
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Pinned to one or more contract ids with no trait — `prints` is required so
|
|
279
|
+
* `event.data` is typed and empty-table handlers cannot typecheck.
|
|
280
|
+
*/
|
|
281
|
+
type PinnedPrintEventFilter = PrintEventFields & {
|
|
282
|
+
contractId: string | readonly string[]
|
|
283
|
+
trait?: undefined
|
|
284
|
+
prints: PrintEventPrints
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* Trait-scoped print source (optionally narrowed by contractId). No single
|
|
288
|
+
* schema — `prints` stays optional.
|
|
289
|
+
*/
|
|
290
|
+
type TraitPrintEventFilter = PrintEventFields & {
|
|
291
|
+
contractId?: string | readonly string[]
|
|
292
|
+
trait: string
|
|
293
|
+
prints?: PrintEventPrints
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* Unpinned / factory-only print source (no contractId). `prints` optional.
|
|
297
|
+
*/
|
|
298
|
+
type UnpinnedPrintEventFilter = PrintEventFields & {
|
|
299
|
+
contractId?: undefined
|
|
300
|
+
trait?: string
|
|
301
|
+
prints?: PrintEventPrints
|
|
302
|
+
};
|
|
303
|
+
/**
|
|
304
|
+
* Print-event filter. Pinned sources (contractId set, no trait) require
|
|
305
|
+
* `prints`; trait and unpinned sources may omit it.
|
|
306
|
+
*/
|
|
307
|
+
type PrintEventFilter = PinnedPrintEventFilter | TraitPrintEventFilter | UnpinnedPrintEventFilter;
|
|
259
308
|
/** All subgraph filter types — discriminated on `type` */
|
|
260
309
|
type SubgraphFilter = StxTransferFilter | StxMintFilter | StxBurnFilter | StxLockFilter | FtTransferFilter | FtMintFilter | FtBurnFilter | NftTransferFilter | NftMintFilter | NftBurnFilter | ContractCallFilter | ContractDeployFilter | PrintEventFilter;
|
|
261
310
|
/** Transaction metadata available in handlers */
|
|
@@ -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;
|
|
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
|
-
|
|
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
|
+
});
|
|
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
|
-
|
|
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;
|
|
@@ -3442,5 +3575,5 @@ export {
|
|
|
3442
3575
|
BLOCK_RETRY_DELAYS_MS
|
|
3443
3576
|
};
|
|
3444
3577
|
|
|
3445
|
-
//# debugId=
|
|
3578
|
+
//# debugId=224EDC1D6107B84664756E2164756E21
|
|
3446
3579
|
//# sourceMappingURL=block-processor.js.map
|