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": "plugin-drizzle: fix state persistence when switching back to backfill",
"packageName": "@apibara/indexer",
"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 state persistence when switching back to backfill",
"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": "plugin-mongo: explicitly set uniqueKey to null if undefined",
"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": "plugin-drizzle: less strict cursor check",
"packageName": "@apibara/plugin-sqlite",
"email": "francesco@ceccon.me",
"dependentChangeType": "patch"
}
33 changes: 28 additions & 5 deletions packages/indexer/src/internal/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type MockMessagesOptions = {
finalizeToIndex: number;
finalizeTriggerIndex: number;
};
uniqueKey?: boolean;
baseBlockNumber?: bigint;
};

export function generateMockMessages(
Expand All @@ -29,33 +31,50 @@ export function generateMockMessages(
const finalizeAt = options?.finalize;
const messages: MockStreamResponse[] = [];

const baseBlockNumber = options?.baseBlockNumber ?? BigInt(5_000_000);

for (let i = 0; i < count; i++) {
const currentBlockNumber = baseBlockNumber + BigInt(i);
const uniqueKey = uniqueKeyFromOrderKey(currentBlockNumber);
if (invalidateAt && i === invalidateAt.invalidateTriggerIndex) {
const invalidateToBlock =
baseBlockNumber + BigInt(invalidateAt.invalidateFromIndex);
messages.push({
_tag: "invalidate",
invalidate: {
cursor: {
orderKey: BigInt(5_000_000 + invalidateAt.invalidateFromIndex),
orderKey: invalidateToBlock,
uniqueKey: options?.uniqueKey
? uniqueKeyFromOrderKey(invalidateToBlock)
: undefined,
},
},
} as Invalidate);
} else if (finalizeAt && i === finalizeAt.finalizeTriggerIndex) {
const fianlizedToBlock =
baseBlockNumber + BigInt(finalizeAt.finalizeToIndex);
messages.push({
_tag: "finalize",
finalize: {
cursor: {
orderKey: BigInt(5_000_000 + finalizeAt.finalizeToIndex),
orderKey: fianlizedToBlock,
uniqueKey: options?.uniqueKey
? uniqueKeyFromOrderKey(fianlizedToBlock)
: undefined,
},
},
} as Finalize);
} else {
messages.push({
_tag: "data",
data: {
cursor: { orderKey: BigInt(5_000_000 + i - 1) },
cursor: { orderKey: currentBlockNumber - 1n },
finality: "accepted",
data: [{ data: `${5_000_000 + i}` }],
endCursor: { orderKey: BigInt(5_000_000 + i) },
data: [{ data: `${baseBlockNumber + BigInt(i)}` }],
endCursor: {
orderKey: currentBlockNumber,
uniqueKey: options?.uniqueKey ? uniqueKey : undefined,
},
production: "backfill",
},
});
Expand All @@ -65,6 +84,10 @@ export function generateMockMessages(
return messages;
}

function uniqueKeyFromOrderKey(orderKey: bigint): `0x${string}` {
return `0xff00${orderKey.toString()}`;
}

type MockIndexerParams = {
internalContext?: InternalContext;
override?: Partial<IndexerConfig<MockFilter, MockBlock>>;
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-drizzle/src/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ export async function persistState<
target: checkpoints.id,
set: {
orderKey: Number(endCursor.orderKey),
uniqueKey: endCursor.uniqueKey,
// Explicitly set the unique key to `null` to indicate that it has been deleted
// Otherwise drizzle will not update its value.
uniqueKey: endCursor.uniqueKey ? endCursor.uniqueKey : null,
},
});

Expand Down
Loading