@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.
package/dist/src/index.js CHANGED
@@ -1842,9 +1842,11 @@ import { resolveTraitContractIds } from "@secondlayer/shared/db/queries/contract
1842
1842
  import { advanceOperationCursor } from "@secondlayer/shared/db/queries/subgraph-operations";
1843
1843
  import {
1844
1844
  isByoSubgraph,
1845
+ recordLiveError,
1845
1846
  recordLiveProgress,
1846
1847
  recordSubgraphProcessed,
1847
1848
  resolveSubgraphDb,
1849
+ rewindLiveProgress,
1848
1850
  updateSubgraphStatus
1849
1851
  } from "@secondlayer/shared/db/queries/subgraphs";
1850
1852
  import { logger as logger6 } from "@secondlayer/shared/logger";
@@ -2552,10 +2554,20 @@ class CursorRaceLostError extends Error {
2552
2554
  this.name = "CursorRaceLostError";
2553
2555
  }
2554
2556
  }
2557
+
2558
+ class LiveCursorRaceLostError extends Error {
2559
+ constructor(subgraphName, height) {
2560
+ super(`live cursor race lost for ${subgraphName} at block ${height}`);
2561
+ this.name = "LiveCursorRaceLostError";
2562
+ }
2563
+ }
2555
2564
  function opCursorMode(opts) {
2556
2565
  const ap = opts?.atomicProgress;
2557
2566
  return ap && "operationId" in ap ? ap : undefined;
2558
2567
  }
2568
+ function isReorgRewind(opts) {
2569
+ return opts?.reorgRewind === true;
2570
+ }
2559
2571
  function statusMode(opts) {
2560
2572
  const ap = opts?.atomicProgress;
2561
2573
  return ap && "status" in ap ? ap : undefined;
@@ -2625,7 +2637,11 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2625
2637
  result.matched = matched.length;
2626
2638
  if (matched.length === 0) {
2627
2639
  if (!opts?.skipProgressUpdate) {
2628
- await recordLiveProgress(targetDb, subgraphName, blockHeight);
2640
+ if (isReorgRewind(opts)) {
2641
+ await rewindLiveProgress(targetDb, subgraphName, blockHeight);
2642
+ } else {
2643
+ await recordLiveProgress(targetDb, subgraphName, blockHeight);
2644
+ }
2629
2645
  }
2630
2646
  return result;
2631
2647
  }
@@ -2644,13 +2660,23 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2644
2660
  };
2645
2661
  let handlerMs = 0;
2646
2662
  let flushMs = 0;
2647
- const applyProgress = async (tx, rr) => {
2663
+ const applyProgress = async (tx, rr, flushedWrites) => {
2648
2664
  if (opts?.skipProgressUpdate)
2649
2665
  return;
2666
+ const rewind = isReorgRewind(opts);
2650
2667
  if (rr.errors > 0 && rr.processed === 0) {
2651
- await updateSubgraphStatus(tx, subgraphName, "error", blockHeight);
2668
+ if (rewind) {
2669
+ await updateSubgraphStatus(tx, subgraphName, "error", blockHeight);
2670
+ } else {
2671
+ await recordLiveError(tx, subgraphName, blockHeight);
2672
+ }
2673
+ } else if (rewind) {
2674
+ await rewindLiveProgress(tx, subgraphName, blockHeight);
2652
2675
  } else {
2653
- await recordLiveProgress(tx, subgraphName, blockHeight);
2676
+ const advanced = await recordLiveProgress(tx, subgraphName, blockHeight);
2677
+ if (!advanced && flushedWrites && !route.byo) {
2678
+ throw new LiveCursorRaceLostError(subgraphName, blockHeight);
2679
+ }
2654
2680
  }
2655
2681
  if (rr.processed > 0 || rr.errors > 0) {
2656
2682
  const lastError = rr.errors > 0 ? `${rr.errors} error(s) at block ${blockHeight}` : undefined;
@@ -2699,13 +2725,15 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2699
2725
  } else if (byoOm && manifest && manifest.count > 0) {
2700
2726
  await advanceOperationCursor(tx, byoOm.operationId, blockHeight);
2701
2727
  }
2702
- await applyProgress(tx, runResult);
2728
+ await applyProgress(tx, runResult, manifest ? manifest.count > 0 : false);
2703
2729
  });
2704
2730
  } else {
2705
2731
  try {
2706
2732
  await targetDb.transaction().execute(async (tx) => {
2707
2733
  const opMode = opCursorMode(opts);
2708
- if (statusMode(opts)) {
2734
+ const sm = statusMode(opts);
2735
+ const rewind = isReorgRewind(opts);
2736
+ if (sm) {
2709
2737
  const row = await tx.selectFrom("subgraphs").select("last_processed_block").where("name", "=", subgraphName).executeTakeFirst();
2710
2738
  if (row && Number(row.last_processed_block) >= blockHeight) {
2711
2739
  result.skipped = true;
@@ -2717,6 +2745,12 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2717
2745
  result.skipped = true;
2718
2746
  return;
2719
2747
  }
2748
+ } else if (!rewind) {
2749
+ const row = await tx.selectFrom("subgraphs").select("last_processed_block").where("name", "=", subgraphName).executeTakeFirst();
2750
+ if (row && Number(row.last_processed_block) >= blockHeight) {
2751
+ result.skipped = true;
2752
+ return;
2753
+ }
2720
2754
  }
2721
2755
  const ctx = new SubgraphContext(tx, schemaName, subgraph.schema, blockMeta, initialTx, false, journalEnabled(opts));
2722
2756
  const handlerStart = performance.now();
@@ -2734,7 +2768,6 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2734
2768
  }
2735
2769
  flushMs = performance.now() - flushStart;
2736
2770
  }
2737
- const sm = statusMode(opts);
2738
2771
  if (sm && flushedWrites) {
2739
2772
  await updateSubgraphStatus(tx, subgraphName, sm.status, blockHeight);
2740
2773
  } else if (opMode && flushedWrites) {
@@ -2743,10 +2776,10 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2743
2776
  throw new CursorRaceLostError(opMode.operationId, blockHeight);
2744
2777
  }
2745
2778
  }
2746
- await applyProgress(tx, runResult);
2779
+ await applyProgress(tx, runResult, flushedWrites);
2747
2780
  });
2748
2781
  } catch (err) {
2749
- if (err instanceof CursorRaceLostError) {
2782
+ if (err instanceof CursorRaceLostError || err instanceof LiveCursorRaceLostError) {
2750
2783
  logger6.warn("cursor race lost — block already covered", {
2751
2784
  subgraph: subgraphName,
2752
2785
  blockHeight,
@@ -2759,11 +2792,13 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2759
2792
  }
2760
2793
  }
2761
2794
  const totalMs = performance.now() - blockStart;
2762
- result.timing = {
2763
- totalMs: Math.round(totalMs),
2764
- handlerMs: Math.round(handlerMs),
2765
- flushMs: Math.round(flushMs)
2766
- };
2795
+ if (!result.skipped) {
2796
+ result.timing = {
2797
+ totalMs: Math.round(totalMs),
2798
+ handlerMs: Math.round(handlerMs),
2799
+ flushMs: Math.round(flushMs)
2800
+ };
2801
+ }
2767
2802
  if (blockHeight % 1000 === 0) {
2768
2803
  try {
2769
2804
  const tables = Object.keys(subgraph.schema);
@@ -4701,5 +4736,5 @@ export {
4701
4736
  ByoBreakingChangeError
4702
4737
  };
4703
4738
 
4704
- //# debugId=EA579C34B6DF559E64756E2164756E21
4739
+ //# debugId=0DAD1DFA856B057164756E2164756E21
4705
4740
  //# sourceMappingURL=index.js.map