Skip to content

Commit

Permalink
fix: reorg causing events to be skipped (#1403)
Browse files Browse the repository at this point in the history
* fix: reorg causing events to be skipped

* chore: changeset
  • Loading branch information
kyscott18 authored Jan 6, 2025
1 parent 03ab020 commit 95aa810
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-hornets-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ponder": patch
---

Fixed a bug where a reorg sometimes caused events to be skipped. This does not affect the rpc cache.
4 changes: 2 additions & 2 deletions packages/core/src/bin/utils/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function run({
case "block": {
// Events must be run block-by-block, so that `database.complete` can accurately
// update the temporary `checkpoint` value set in the trigger.
for (const events of splitEvents(event.events)) {
for (const { checkpoint, events } of splitEvents(event.events)) {
const result = await handleEvents(
decodeEvents(common, indexingBuild.sources, events),
event.checkpoint,
Expand All @@ -96,7 +96,7 @@ export async function run({
if (result.status === "error") onReloadableError(result.error);

// Set reorg table `checkpoint` column for newly inserted rows.
await database.complete({ checkpoint: event.checkpoint });
await database.complete({ checkpoint });
}

await metadataStore.setStatus(event.status);
Expand Down
18 changes: 14 additions & 4 deletions packages/core/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,27 @@ const min = (...checkpoints: (string | undefined)[]) => {
})!;
};

export const splitEvents = (events: RawEvent[]): RawEvent[][] => {
export const splitEvents = (
events: RawEvent[],
): { checkpoint: string; events: RawEvent[] }[] => {
let prevHash: Hash | undefined;
const result: RawEvent[][] = [];
const result: { checkpoint: string; events: RawEvent[] }[] = [];

for (const event of events) {
if (prevHash === undefined || prevHash !== event.block.hash) {
result.push([]);
result.push({
checkpoint: encodeCheckpoint({
...maxCheckpoint,
blockTimestamp: Number(event.block.timestamp),
chainId: BigInt(event.chainId),
blockNumber: event.block.number,
}),
events: [],
});
prevHash = event.block.hash;
}

result[result.length - 1]!.push(event);
result[result.length - 1]!.events.push(event);
}

return result;
Expand Down

0 comments on commit 95aa810

Please sign in to comment.