@secondlayer/subgraphs 4.1.1 → 4.1.2

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.
@@ -2075,6 +2075,8 @@ function indexEventTypesForFilterTypes(filterTypes) {
2075
2075
  return [...types];
2076
2076
  }
2077
2077
  function referencedIndexEventTypes(subgraph) {
2078
+ if (!subgraph.sources)
2079
+ return [];
2078
2080
  return indexEventTypesForFilterTypes(sourceFilters(subgraph).map((f) => f.type));
2079
2081
  }
2080
2082
  function isStreamsIndexEligible(subgraph) {
@@ -3192,6 +3194,63 @@ function startCatchUpLeader(opts = {}) {
3192
3194
  });
3193
3195
  }
3194
3196
 
3197
+ // src/runtime/decoder-bound.ts
3198
+ import { decodeStreamsCursor, isEmptyRangeCursor } from "@secondlayer/shared";
3199
+ import { getSourceDb as getSourceDb3 } from "@secondlayer/shared/db";
3200
+ function decoderNameForEventType(indexEventType) {
3201
+ return `decode.${indexEventType}.v1`;
3202
+ }
3203
+ function decoderNamesForIndexEventTypes(eventTypes) {
3204
+ return eventTypes.map(decoderNameForEventType);
3205
+ }
3206
+ function decoderNamesForSubgraph(subgraph) {
3207
+ return decoderNamesForIndexEventTypes(referencedIndexEventTypes(subgraph));
3208
+ }
3209
+ function committedHeight(cursor) {
3210
+ if (!cursor)
3211
+ return null;
3212
+ try {
3213
+ const decoded = decodeStreamsCursor(cursor);
3214
+ if (isEmptyRangeCursor(decoded))
3215
+ return decoded.block_height;
3216
+ return Math.max(0, decoded.block_height - 1);
3217
+ } catch {
3218
+ return null;
3219
+ }
3220
+ }
3221
+ async function decoderBoundTip(decoderNames, opts) {
3222
+ if (decoderNames.length === 0)
3223
+ return { kind: "unbounded" };
3224
+ const sourceDb = opts?.sourceDb ?? getSourceDb3();
3225
+ const rows = await sourceDb.selectFrom("decoder_checkpoints").select(["decoder_name", "last_cursor"]).where("decoder_name", "in", decoderNames).execute();
3226
+ const byName = new Map(rows.map((r) => [r.decoder_name, r.last_cursor]));
3227
+ const missing = [];
3228
+ const heights = [];
3229
+ for (const name of decoderNames) {
3230
+ const height = committedHeight(byName.get(name));
3231
+ if (height === null)
3232
+ missing.push(name);
3233
+ else
3234
+ heights.push(height);
3235
+ }
3236
+ if (missing.length > 0)
3237
+ return { kind: "stall", missing };
3238
+ return { kind: "height", height: Math.min(...heights) };
3239
+ }
3240
+ async function boundSourceTip(rawTip, decoderNames, opts) {
3241
+ const bound = await decoderBoundTip(decoderNames, opts);
3242
+ if (bound.kind === "stall")
3243
+ return { ok: false, missing: bound.missing };
3244
+ if (bound.kind === "unbounded") {
3245
+ return { ok: true, tip: rawTip, floor: null };
3246
+ }
3247
+ return {
3248
+ ok: true,
3249
+ tip: Math.min(rawTip, bound.height),
3250
+ floor: bound.height
3251
+ };
3252
+ }
3253
+
3195
3254
  // src/runtime/catchup.ts
3196
3255
  var LOG_INTERVAL = 1000;
3197
3256
  var STANDARD_CATCHUP_BATCH_CONFIG = {
@@ -3302,7 +3361,28 @@ async function catchUpSubgraph(subgraph, subgraphName) {
3302
3361
  if (!subgraphRow)
3303
3362
  return 0;
3304
3363
  const lastProcessedBlock = Number(subgraphRow.last_processed_block);
3305
- const chainTip = await source.getTip();
3364
+ const rawTip = await source.getTip();
3365
+ if (rawTip <= 0)
3366
+ return 0;
3367
+ const bound = await boundSourceTip(rawTip, decoderNamesForSubgraph(subgraph));
3368
+ if (!bound.ok) {
3369
+ logger8.warn("Subgraph catch-up stalled: missing decoder checkpoint", {
3370
+ event: "subgraph_catchup_decoder_stall",
3371
+ subgraph: subgraphName,
3372
+ missing: bound.missing
3373
+ });
3374
+ return 0;
3375
+ }
3376
+ if (bound.floor !== null && bound.floor < rawTip) {
3377
+ logger8.debug("Subgraph catch-up tip bounded by decoder progress", {
3378
+ event: "subgraph_catchup_decoder_floor",
3379
+ subgraph: subgraphName,
3380
+ rawTip,
3381
+ floor: bound.floor,
3382
+ tip: bound.tip
3383
+ });
3384
+ }
3385
+ const chainTip = bound.tip;
3306
3386
  if (chainTip <= 0 || lastProcessedBlock >= chainTip)
3307
3387
  return 0;
3308
3388
  const subgraphStart = Number(subgraphRow.start_block) || 1;
@@ -3765,7 +3845,26 @@ async function processBlockRange(def, opts) {
3765
3845
  async function resolveBlockRange(source, def, opts) {
3766
3846
  const definitionStart = def.startBlock ?? 1;
3767
3847
  const fromBlock = opts?.startBlockFloor != null ? Math.max(definitionStart, opts.startBlockFloor) : definitionStart;
3768
- return { fromBlock, toBlock: await source.getTip() };
3848
+ const rawTip = await source.getTip();
3849
+ const bound = await boundSourceTip(rawTip, decoderNamesForSubgraph(def));
3850
+ if (!bound.ok) {
3851
+ logger10.warn("Reindex stalled: missing decoder checkpoint", {
3852
+ event: "subgraph_reindex_decoder_stall",
3853
+ subgraph: def.name,
3854
+ missing: bound.missing
3855
+ });
3856
+ throw new Error(`Reindex stalled: missing decoder checkpoint (${bound.missing.join(", ")})`);
3857
+ }
3858
+ if (bound.floor !== null && bound.floor < rawTip) {
3859
+ logger10.debug("Reindex tip bounded by decoder progress", {
3860
+ event: "subgraph_reindex_decoder_floor",
3861
+ subgraph: def.name,
3862
+ rawTip,
3863
+ floor: bound.floor,
3864
+ tip: bound.tip
3865
+ });
3866
+ }
3867
+ return { fromBlock, toBlock: bound.tip };
3769
3868
  }
3770
3869
  async function clearReindexMetadata(db, subgraphName) {
3771
3870
  await db.updateTable("subgraphs").set({ reindex_from_block: null, reindex_to_block: null }).where("name", "=", subgraphName).execute();
@@ -3992,5 +4091,5 @@ export {
3992
4091
  backfillSubgraph
3993
4092
  };
3994
4093
 
3995
- //# debugId=AB9A50BBDBE89C8764756E2164756E21
4094
+ //# debugId=5CCD9AC9CA5B91DC64756E2164756E21
3996
4095
  //# sourceMappingURL=reindex.js.map