Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforce strict ordering on sync events #1451

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-spoons-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ponder": patch
---

Improved reorg handling resilience.
30 changes: 23 additions & 7 deletions packages/core/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { intervalUnion } from "@/utils/interval.js";
import { never } from "@/utils/never.js";
import { type RequestQueue, createRequestQueue } from "@/utils/requestQueue.js";
import { startClock } from "@/utils/timer.js";
import { type Queue, createQueue } from "@ponder/common";
import {
type Address,
type Hash,
Expand Down Expand Up @@ -236,6 +237,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
syncProgress: SyncProgress;
historicalSync: HistoricalSync;
realtimeSync: RealtimeSync;
realtimeQueue: Queue<void, RealtimeSyncEvent>;
unfinalizedBlocks: Omit<
Extract<RealtimeSyncEvent, { type: "block" }>,
"type"
Expand Down Expand Up @@ -290,13 +292,22 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
network,
onFatalError: args.onFatalError,
});

const realtimeQueue = createQueue({
initialStart: true,
browser: false,
concurrency: 1,
worker: async (event: RealtimeSyncEvent) =>
onRealtimeSyncEvent({ event, network }),
});

const realtimeSync = createRealtimeSync({
common: args.common,
sources,
requestQueue,
network,
onEvent: (event) =>
onRealtimeSyncEvent({ event, network }).catch((error) => {
realtimeQueue.add(event).catch((error) => {
args.common.logger.error({
service: "sync",
msg: `Fatal error: Unable to process ${event.type} event`,
Expand Down Expand Up @@ -343,6 +354,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
syncProgress,
historicalSync,
realtimeSync,
realtimeQueue,
unfinalizedBlocks: [],
});
status[network.name] = { block: null, ready: false };
Expand Down Expand Up @@ -717,11 +729,6 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
syncProgress.finalized = event.block;
const checkpoint = getOmnichainCheckpoint("finalized")!;

// Raise event to parent function (runtime)
if (checkpoint > prev) {
args.onRealtimeEvent({ type: "finalize", checkpoint });
}

if (
getChainCheckpoint({ syncProgress, network, tag: "finalized" })! >
getOmnichainCheckpoint("current")!
Expand Down Expand Up @@ -813,6 +820,11 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
});
}

// Raise event to parent function (runtime)
if (checkpoint > prev) {
args.onRealtimeEvent({ type: "finalize", checkpoint });
}

/**
* The realtime service can be killed if `endBlock` is
* defined has become finalized.
Expand Down Expand Up @@ -973,8 +985,12 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
isKilled = true;
const promises: Promise<void>[] = [];
for (const network of args.networks) {
const { historicalSync, realtimeSync } = perNetworkSync.get(network)!;
const { historicalSync, realtimeSync, realtimeQueue } =
perNetworkSync.get(network)!;
historicalSync.kill();
realtimeQueue.pause();
realtimeQueue.clear();
promises.push(realtimeQueue.onIdle());
promises.push(realtimeSync.kill());
}
await Promise.all(promises);
Expand Down
Loading