Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugins: fix issue caused by cursors with empty unique key",
"packageName": "@apibara/beaconchain",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions change/@apibara-evm-c96dd7a5-d16a-44aa-ac3a-947a9b904517.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugins: fix issue caused by cursors with empty unique key",
"packageName": "@apibara/evm",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugin-drizzle: fix issue caused by cursors with empty unique key",
"packageName": "@apibara/plugin-drizzle",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugins: fix issue caused by cursors with empty unique key",
"packageName": "@apibara/plugin-mongo",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugins: fix issue caused by cursors with empty unique key",
"packageName": "@apibara/plugin-sqlite",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugins: fix issue caused by cursors with empty unique key",
"packageName": "@apibara/protocol",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
"packages/evm",
"packages/indexer",
"packages/protocol",
"packages/starknet"
"packages/starknet",
"packages/plugin-drizzle",
"packages/plugin-mongo",
"packages/plugin-sqlite"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion packages/beaconchain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/beaconchain",
"version": "2.0.1-beta.31",
"version": "2.0.1-beta.39",
"type": "module",
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/evm",
"version": "2.0.1-beta.31",
"version": "2.0.1-beta.39",
"type": "module",
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/plugin-drizzle",
"version": "2.0.0-beta.38",
"version": "2.0.0-beta.39",
"type": "module",
"files": [
"dist",
Expand Down
13 changes: 5 additions & 8 deletions packages/plugin-drizzle/src/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Cursor } from "@apibara/protocol";
import { type Cursor, normalizeCursor } from "@apibara/protocol";
import { and, eq, gt, isNull, lt } from "drizzle-orm";
import type {
ExtractTablesWithRelations,
Expand All @@ -15,10 +15,7 @@ const SCHEMA_VERSION_TABLE_NAME = "__indexer_schema_version";
export const checkpoints = pgTable(CHECKPOINTS_TABLE_NAME, {
id: text("id").notNull().primaryKey(),
orderKey: integer("order_key").notNull(),
uniqueKey: text("unique_key")
.$type<`0x${string}` | undefined>()
.notNull()
.default(undefined),
uniqueKey: text("unique_key"),
});

export const filters = pgTable(
Expand Down Expand Up @@ -87,7 +84,7 @@ export async function initializePersistentState<
CREATE TABLE IF NOT EXISTS ${CHECKPOINTS_TABLE_NAME} (
id TEXT PRIMARY KEY,
order_key INTEGER NOT NULL,
unique_key TEXT NOT NULL DEFAULT ''
unique_key TEXT
);
`);

Expand Down Expand Up @@ -212,10 +209,10 @@ export async function getState<
.where(eq(checkpoints.id, indexerId));

const cursor = checkpointRows[0]
? {
? normalizeCursor({
orderKey: BigInt(checkpointRows[0].orderKey),
uniqueKey: checkpointRows[0].uniqueKey,
}
})
: undefined;

const filterRows = await tx
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-drizzle/tests/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ describe("Drizzle test", () => {
{
"id": "indexer_testing_default",
"orderKey": 5000005,
"uniqueKey": "",
"uniqueKey": null,
},
]
`);
Expand Down Expand Up @@ -846,7 +846,7 @@ describe("Drizzle test", () => {
{
"id": "indexer_testing_default",
"orderKey": 108,
"uniqueKey": "",
"uniqueKey": null,
},
]
`);
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe("Drizzle test", () => {
{
"id": "indexer_testing_default",
"orderKey": 107,
"uniqueKey": "",
"uniqueKey": null,
},
]
`);
Expand Down Expand Up @@ -1206,7 +1206,7 @@ describe("Drizzle test", () => {
{
"id": "indexer_testing_default",
"orderKey": 107,
"uniqueKey": "",
"uniqueKey": null,
},
]
`);
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-mongo/src/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Cursor } from "@apibara/protocol";
import { type Cursor, normalizeCursor } from "@apibara/protocol";
import type { ClientSession, Db } from "mongodb";

export type CheckpointSchema = {
id: string;
orderKey: number;
uniqueKey?: `0x${string}`;
uniqueKey: string | null;
};

export type FilterSchema = {
Expand Down Expand Up @@ -98,10 +98,10 @@ export async function getState<TFilter>(props: {
.findOne({ id: indexerId }, { session });

if (checkpointRow) {
cursor = {
cursor = normalizeCursor({
orderKey: BigInt(checkpointRow.orderKey),
uniqueKey: checkpointRow.uniqueKey,
};
});
}

const filterRow = await db
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-mongo/tests/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ describe("MongoDB Test", () => {
{
"cursor": {
"orderKey": 5000005n,
"uniqueKey": null,
},
"filter": undefined,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-sqlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/plugin-sqlite",
"version": "2.0.0-beta.38",
"version": "2.0.0-beta.39",
"type": "module",
"files": [
"dist",
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-sqlite/src/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Cursor } from "@apibara/protocol";
import { type Cursor, normalizeCursor } from "@apibara/protocol";
import type { Database } from "better-sqlite3";

import { assertInTransaction, deserialize, serialize } from "./utils";
Expand Down Expand Up @@ -57,10 +57,10 @@ export function getState<TFilter>(props: {
let filter: TFilter | undefined;

if (storedCursor?.order_key) {
cursor = {
cursor = normalizeCursor({
orderKey: BigInt(storedCursor.order_key),
uniqueKey: storedCursor.unique_key as `0x${string}`,
};
uniqueKey: storedCursor.unique_key ? storedCursor.unique_key : null,
});
}

if (storedFilter) {
Expand Down
24 changes: 24 additions & 0 deletions packages/protocol/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,27 @@ export const cursorFromBytes = Schema.decodeSync(CursorFromBytes);
export function isCursor(value: unknown): value is Cursor {
return Schema.is(Cursor)(value);
}

/** Normalize a cursor.
*
* The challenge is that the `Cursor` validator expects `uniqueKey` to be either a `0x${string}`
* or not present at all. Setting the field to `undefined` will result in a validation error.
*
* @param cursor The cursor to normalize
*/
export function normalizeCursor(cursor: {
orderKey: bigint;
uniqueKey: string | null;
}): Cursor {
if (cursor.uniqueKey !== null && cursor.uniqueKey.length > 0) {
const uniqueKey = cursor.uniqueKey as `0x${string}`;
return {
orderKey: BigInt(cursor.orderKey),
uniqueKey,
};
}

return {
orderKey: BigInt(cursor.orderKey),
};
}