Skip to content

fix(backend): stabilize getStreamEvents cursor pagination for tied timestamps#977

Open
BernardOnuh wants to merge 1 commit into
LabsCrypt:mainfrom
BernardOnuh:fix/803-stream-events-cursor-pagination
Open

fix(backend): stabilize getStreamEvents cursor pagination for tied timestamps#977
BernardOnuh wants to merge 1 commit into
LabsCrypt:mainfrom
BernardOnuh:fix/803-stream-events-cursor-pagination

Conversation

@BernardOnuh

@BernardOnuh BernardOnuh commented Jul 1, 2026

Copy link
Copy Markdown

Description

Closes #803

Problem

getStreamEvents in backend/src/controllers/stream.controller.ts used orderBy: { timestamp: order } combined with Prisma cursor pagination (cursor: { id: cursor }, skip: 1). timestamp is not unique — events in the same block/ledger can share a timestamp — so sorting by it alone gives Postgres no guaranteed row order for tied rows across separate queries. As a result, paginating with cursor could skip or duplicate events whose timestamps tie with the cursor row.

Fix

Added id (unique) as a secondary sort key:

diff

  • orderBy: { timestamp: order },
  • orderBy: [{ timestamp: order }, { id: order }],

This makes the sort fully deterministic — no two rows can ever tie on (timestamp, id) — so cursor pagination is now stable regardless of how many events share a timestamp.

Testing

Added describe('getStreamEvents', ...) in backend/tests/stream.controller.test.ts:

Asserts orderBy sent to Prisma includes id as a tiebreaker.

A pagination regression test against a mocked dataset with tied timestamps, where the mock randomizes tie order on every call (mirroring how Postgres offers no ordering guarantee for ties without a unique tiebreaker — a plain in-process stable sort would not have caught this bug). Walks multiple cursor pages and asserts no event is skipped or duplicated.

Verified the fix's necessity by running the pre-fix logic against the tied-timestamp scenario 200 times standalone: it failed (skipped/duplicated rows) in ~91% of runs. The post-fix logic (with id tiebreaker) had 0 failures across 200 runs.

npm test run locally in backend/ — please confirm green before merge (not runnable in the sandbox used to draft this fix due to Prisma engine download being network-restricted there).

Acceptance criteria (from #803)

Add a unique tiebreaker to orderBy so cursor pagination is stable

Add a test with multiple events sharing one timestamp asserting no row is skipped or duplicated

Out of scope

No changes to the events DB schema, per the issue.

Files changed

backend/src/controllers/stream.controller.ts

backend/tests/stream.controller.test.ts

…mestamps

Add id as a unique tiebreaker to orderBy so cursor pagination no
longer skips or duplicates events that share a timestamp.

Fixes LabsCrypt#803
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] getStreamEvents uses Prisma cursor on id while ordering by non-unique timestamp - pages skip/duplicate tied-timestamp events

1 participant