fix(backend): stabilize getStreamEvents cursor pagination for tied timestamps#977
Open
BernardOnuh wants to merge 1 commit into
Open
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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