Skip to content

Commit

Permalink
fix cached from block (#1042)
Browse files Browse the repository at this point in the history
* fix cached from block

* cleanup

* chore: changeset

* Update silver-lamps-tan.md

---------

Co-authored-by: typedarray <[email protected]>
  • Loading branch information
kyscott18 and 0xOlias authored Aug 16, 2024
1 parent 0a4cc2c commit 2c21ffa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-lamps-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ponder/core": patch
---

Fixed a regression introduced in `0.5.9` that sometimes caused events to be skipped during indexing if any `startBlock` was set earlier than in a previous indexing run. This issue did not affect the database integrity, but affected apps should restart to ensure all events are indexed.
18 changes: 14 additions & 4 deletions packages/core/src/sync-historical/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
intervalDifference,
intervalIntersection,
intervalSum,
sortIntervals,
} from "@/utils/interval.js";
import { never } from "@/utils/never.js";
import type { RequestQueue } from "@/utils/requestQueue.js";
Expand Down Expand Up @@ -89,12 +90,21 @@ export const createHistoricalSync = async (
* some progress.
*/
const _latestCompletedBlocks = args.sources.map(({ filter }) => {
const completedIntervals = intervalIntersection(
[[filter.fromBlock, filter.toBlock ?? Number.POSITIVE_INFINITY]],
intervalsCache.get(filter)!,
const requiredInterval = [
filter.fromBlock,
filter.toBlock ?? Number.POSITIVE_INFINITY,
] satisfies Interval;
const cachedIntervals = intervalsCache.get(filter)!;

const completedIntervals = sortIntervals(
intervalIntersection([requiredInterval], cachedIntervals),
);

if (completedIntervals.length === 0) return undefined;
return completedIntervals[0]![1];

const earliestCompletedInterval = completedIntervals[0]!;
if (earliestCompletedInterval[0] !== filter.fromBlock) return undefined;
return earliestCompletedInterval[1];
});

if (_latestCompletedBlocks.every((block) => block !== undefined)) {
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/utils/interval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
intervalIntersection,
intervalSum,
intervalUnion,
sortIntervals,
} from "./interval.js";

test("intervalSum handles empty input", () => {
Expand Down Expand Up @@ -194,3 +195,23 @@ test("intervalDifference does not mutate inputs", () => {

expect(initial).toStrictEqual([6, 17]);
});

test("sortIntervals", () => {
let result = sortIntervals([
[1, 5],
[4, 7],
]);
expect(result).toStrictEqual([
[1, 5],
[4, 7],
]);

result = sortIntervals([
[4, 7],
[1, 5],
]);
expect(result).toStrictEqual([
[1, 5],
[4, 7],
]);
});
4 changes: 4 additions & 0 deletions packages/core/src/utils/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ export function intervalDifference(
return result;
}

export function sortIntervals(intervals: Interval[]) {
return intervals.sort((a, b) => (a[0] < b[0] ? -1 : 1));
}

export function getChunks({
interval,
maxChunkSize,
Expand Down

0 comments on commit 2c21ffa

Please sign in to comment.