@secondlayer/subgraphs 3.23.2 → 3.24.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.
@@ -450,6 +450,19 @@ interface ProcessBlockOptions {
450
450
  } | {
451
451
  operationId: string
452
452
  };
453
+ /**
454
+ * Reorg rewind (f069): set exclusively by `reorg.ts`'s fork-block
455
+ * reprocess. Bypasses the live-path replay guard (a fork block's height
456
+ * is, by construction, at or below the current cursor — the guard would
457
+ * otherwise skip it) and writes the checkpoint via
458
+ * `rewindLiveProgress` (unconditional — a rewind's whole point is
459
+ * moving the cursor *backward*, which the live path's conditional
460
+ * advance would refuse). Safe only because the reorg path holds a
461
+ * transaction-scoped advisory lock across its whole
462
+ * delete+reprocess+rewind sequence, so nothing else can interleave a
463
+ * forward write. Never set this outside `reorg.ts`.
464
+ */
465
+ reorgRewind?: true;
453
466
  }
454
467
  /** Default per-block retry schedule before a failure counts as persistent. */
455
468
  declare const BLOCK_RETRY_DELAYS_MS: number[];
@@ -1627,9 +1627,11 @@ import { resolveTraitContractIds } from "@secondlayer/shared/db/queries/contract
1627
1627
  import { advanceOperationCursor } from "@secondlayer/shared/db/queries/subgraph-operations";
1628
1628
  import {
1629
1629
  isByoSubgraph,
1630
+ recordLiveError,
1630
1631
  recordLiveProgress,
1631
1632
  recordSubgraphProcessed,
1632
1633
  resolveSubgraphDb,
1634
+ rewindLiveProgress,
1633
1635
  updateSubgraphStatus
1634
1636
  } from "@secondlayer/shared/db/queries/subgraphs";
1635
1637
  import { logger as logger6 } from "@secondlayer/shared/logger";
@@ -2337,10 +2339,20 @@ class CursorRaceLostError extends Error {
2337
2339
  this.name = "CursorRaceLostError";
2338
2340
  }
2339
2341
  }
2342
+
2343
+ class LiveCursorRaceLostError extends Error {
2344
+ constructor(subgraphName, height) {
2345
+ super(`live cursor race lost for ${subgraphName} at block ${height}`);
2346
+ this.name = "LiveCursorRaceLostError";
2347
+ }
2348
+ }
2340
2349
  function opCursorMode(opts) {
2341
2350
  const ap = opts?.atomicProgress;
2342
2351
  return ap && "operationId" in ap ? ap : undefined;
2343
2352
  }
2353
+ function isReorgRewind(opts) {
2354
+ return opts?.reorgRewind === true;
2355
+ }
2344
2356
  function statusMode(opts) {
2345
2357
  const ap = opts?.atomicProgress;
2346
2358
  return ap && "status" in ap ? ap : undefined;
@@ -2410,7 +2422,11 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2410
2422
  result.matched = matched.length;
2411
2423
  if (matched.length === 0) {
2412
2424
  if (!opts?.skipProgressUpdate) {
2413
- await recordLiveProgress(targetDb, subgraphName, blockHeight);
2425
+ if (isReorgRewind(opts)) {
2426
+ await rewindLiveProgress(targetDb, subgraphName, blockHeight);
2427
+ } else {
2428
+ await recordLiveProgress(targetDb, subgraphName, blockHeight);
2429
+ }
2414
2430
  }
2415
2431
  return result;
2416
2432
  }
@@ -2429,13 +2445,23 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2429
2445
  };
2430
2446
  let handlerMs = 0;
2431
2447
  let flushMs = 0;
2432
- const applyProgress = async (tx, rr) => {
2448
+ const applyProgress = async (tx, rr, flushedWrites) => {
2433
2449
  if (opts?.skipProgressUpdate)
2434
2450
  return;
2451
+ const rewind = isReorgRewind(opts);
2435
2452
  if (rr.errors > 0 && rr.processed === 0) {
2436
- await updateSubgraphStatus(tx, subgraphName, "error", blockHeight);
2453
+ if (rewind) {
2454
+ await updateSubgraphStatus(tx, subgraphName, "error", blockHeight);
2455
+ } else {
2456
+ await recordLiveError(tx, subgraphName, blockHeight);
2457
+ }
2458
+ } else if (rewind) {
2459
+ await rewindLiveProgress(tx, subgraphName, blockHeight);
2437
2460
  } else {
2438
- await recordLiveProgress(tx, subgraphName, blockHeight);
2461
+ const advanced = await recordLiveProgress(tx, subgraphName, blockHeight);
2462
+ if (!advanced && flushedWrites && !route.byo) {
2463
+ throw new LiveCursorRaceLostError(subgraphName, blockHeight);
2464
+ }
2439
2465
  }
2440
2466
  if (rr.processed > 0 || rr.errors > 0) {
2441
2467
  const lastError = rr.errors > 0 ? `${rr.errors} error(s) at block ${blockHeight}` : undefined;
@@ -2484,13 +2510,15 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2484
2510
  } else if (byoOm && manifest && manifest.count > 0) {
2485
2511
  await advanceOperationCursor(tx, byoOm.operationId, blockHeight);
2486
2512
  }
2487
- await applyProgress(tx, runResult);
2513
+ await applyProgress(tx, runResult, manifest ? manifest.count > 0 : false);
2488
2514
  });
2489
2515
  } else {
2490
2516
  try {
2491
2517
  await targetDb.transaction().execute(async (tx) => {
2492
2518
  const opMode = opCursorMode(opts);
2493
- if (statusMode(opts)) {
2519
+ const sm = statusMode(opts);
2520
+ const rewind = isReorgRewind(opts);
2521
+ if (sm) {
2494
2522
  const row = await tx.selectFrom("subgraphs").select("last_processed_block").where("name", "=", subgraphName).executeTakeFirst();
2495
2523
  if (row && Number(row.last_processed_block) >= blockHeight) {
2496
2524
  result.skipped = true;
@@ -2502,6 +2530,12 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2502
2530
  result.skipped = true;
2503
2531
  return;
2504
2532
  }
2533
+ } else if (!rewind) {
2534
+ const row = await tx.selectFrom("subgraphs").select("last_processed_block").where("name", "=", subgraphName).executeTakeFirst();
2535
+ if (row && Number(row.last_processed_block) >= blockHeight) {
2536
+ result.skipped = true;
2537
+ return;
2538
+ }
2505
2539
  }
2506
2540
  const ctx = new SubgraphContext(tx, schemaName, subgraph.schema, blockMeta, initialTx, false, journalEnabled(opts));
2507
2541
  const handlerStart = performance.now();
@@ -2519,7 +2553,6 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2519
2553
  }
2520
2554
  flushMs = performance.now() - flushStart;
2521
2555
  }
2522
- const sm = statusMode(opts);
2523
2556
  if (sm && flushedWrites) {
2524
2557
  await updateSubgraphStatus(tx, subgraphName, sm.status, blockHeight);
2525
2558
  } else if (opMode && flushedWrites) {
@@ -2528,10 +2561,10 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2528
2561
  throw new CursorRaceLostError(opMode.operationId, blockHeight);
2529
2562
  }
2530
2563
  }
2531
- await applyProgress(tx, runResult);
2564
+ await applyProgress(tx, runResult, flushedWrites);
2532
2565
  });
2533
2566
  } catch (err) {
2534
- if (err instanceof CursorRaceLostError) {
2567
+ if (err instanceof CursorRaceLostError || err instanceof LiveCursorRaceLostError) {
2535
2568
  logger6.warn("cursor race lost — block already covered", {
2536
2569
  subgraph: subgraphName,
2537
2570
  blockHeight,
@@ -2544,11 +2577,13 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2544
2577
  }
2545
2578
  }
2546
2579
  const totalMs = performance.now() - blockStart;
2547
- result.timing = {
2548
- totalMs: Math.round(totalMs),
2549
- handlerMs: Math.round(handlerMs),
2550
- flushMs: Math.round(flushMs)
2551
- };
2580
+ if (!result.skipped) {
2581
+ result.timing = {
2582
+ totalMs: Math.round(totalMs),
2583
+ handlerMs: Math.round(handlerMs),
2584
+ flushMs: Math.round(flushMs)
2585
+ };
2586
+ }
2552
2587
  if (blockHeight % 1000 === 0) {
2553
2588
  try {
2554
2589
  const tables = Object.keys(subgraph.schema);
@@ -2583,5 +2618,5 @@ export {
2583
2618
  BLOCK_RETRY_DELAYS_MS
2584
2619
  };
2585
2620
 
2586
- //# debugId=ED4E61C22DF18FC964756E2164756E21
2621
+ //# debugId=0C53CE27E78CBEED64756E2164756E21
2587
2622
  //# sourceMappingURL=block-processor.js.map