@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 +50 -15
- package/dist/src/index.js.map +3 -3
- package/dist/src/runtime/block-processor.d.ts +13 -0
- package/dist/src/runtime/block-processor.js +50 -15
- package/dist/src/runtime/block-processor.js.map +3 -3
- package/dist/src/runtime/catchup.js +84 -15
- package/dist/src/runtime/catchup.js.map +6 -5
- package/dist/src/runtime/processor.js +182 -130
- package/dist/src/runtime/processor.js.map +7 -7
- package/dist/src/runtime/reindex.js +50 -15
- package/dist/src/runtime/reindex.js.map +3 -3
- package/dist/src/runtime/reorg.js +182 -105
- package/dist/src/runtime/reorg.js.map +7 -6
- package/dist/src/service.js +184 -132
- package/dist/src/service.js.map +7 -7
- package/package.json +3 -5
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
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);
|
|
@@ -2643,6 +2678,33 @@ class StatsAccumulator {
|
|
|
2643
2678
|
import { getTargetDb as getTargetDb2 } from "@secondlayer/shared/db";
|
|
2644
2679
|
import { getSubgraph } from "@secondlayer/shared/db/queries/subgraphs";
|
|
2645
2680
|
import { logger as logger7 } from "@secondlayer/shared/logger";
|
|
2681
|
+
|
|
2682
|
+
// src/runtime/catchup-leader.ts
|
|
2683
|
+
import {
|
|
2684
|
+
SUBGRAPH_CATCHUP_LOCK_KEY,
|
|
2685
|
+
createPostgresLeaderBackend,
|
|
2686
|
+
withLeaderLock
|
|
2687
|
+
} from "@secondlayer/shared/leader";
|
|
2688
|
+
import { targetListenerUrl } from "@secondlayer/shared/queue/listener";
|
|
2689
|
+
var catchUpLeader = false;
|
|
2690
|
+
function isCatchUpLeader() {
|
|
2691
|
+
return catchUpLeader;
|
|
2692
|
+
}
|
|
2693
|
+
function startCatchUpLeader(opts = {}) {
|
|
2694
|
+
return withLeaderLock(SUBGRAPH_CATCHUP_LOCK_KEY, async () => {
|
|
2695
|
+
catchUpLeader = true;
|
|
2696
|
+
await opts.onAcquire?.();
|
|
2697
|
+
return () => {
|
|
2698
|
+
catchUpLeader = false;
|
|
2699
|
+
};
|
|
2700
|
+
}, {
|
|
2701
|
+
pollMs: opts.pollMs,
|
|
2702
|
+
heartbeatMs: opts.heartbeatMs,
|
|
2703
|
+
createBackend: opts.createBackend ?? (() => createPostgresLeaderBackend(targetListenerUrl()))
|
|
2704
|
+
});
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
// src/runtime/catchup.ts
|
|
2646
2708
|
var LOG_INTERVAL = 1000;
|
|
2647
2709
|
var STANDARD_CATCHUP_BATCH_CONFIG = {
|
|
2648
2710
|
defaultBatchSize: 500,
|
|
@@ -2764,6 +2826,13 @@ async function catchUpSubgraph(subgraph, subgraphName) {
|
|
|
2764
2826
|
let prefetchedBatchEnd = Math.min(currentHeight + batchSize - 1, chainTip);
|
|
2765
2827
|
let nextBatchPromise = batchConfig.prefetch ? source.loadBlockRange(currentHeight, prefetchedBatchEnd) : undefined;
|
|
2766
2828
|
while (currentHeight <= chainTip) {
|
|
2829
|
+
if (!isCatchUpLeader()) {
|
|
2830
|
+
logger7.info("Lost catch-up leadership, stopping catch-up", {
|
|
2831
|
+
subgraph: subgraphName,
|
|
2832
|
+
processed
|
|
2833
|
+
});
|
|
2834
|
+
break;
|
|
2835
|
+
}
|
|
2767
2836
|
const currentRow = await getSubgraph(targetDb, subgraphName);
|
|
2768
2837
|
if (!currentRow || currentRow.status !== "active") {
|
|
2769
2838
|
logger7.info("Subgraph status changed, stopping catch-up", {
|
|
@@ -2871,5 +2940,5 @@ export {
|
|
|
2871
2940
|
bumpReorgEpoch
|
|
2872
2941
|
};
|
|
2873
2942
|
|
|
2874
|
-
//# debugId=
|
|
2943
|
+
//# debugId=05204354946D588C64756E2164756E21
|
|
2875
2944
|
//# sourceMappingURL=catchup.js.map
|