Skip to content

fix: shares keyset-pagination cursor can silently skip shares at page boundary - #15

Merged
colneech-dev merged 1 commit into
mainfrom
fix/share-paging-keyset-cursor
Nov 24, 2026
Merged

colneech-dev merged 1 commit into
mainfrom
fix/share-paging-keyset-cursor

Conversation

@colneech-dev

Copy link
Copy Markdown
Owner

Summary

  • PPLNS/PPLNSBF/PROP payment schemes page through shares via ReadSharesBeforeAsync, resuming each page from the previous page's last row's created timestamp with a plain created < before filter.
  • Shares are batch-inserted and created timestamp resolution isn't fine enough to guarantee uniqueness under load. If a run of shares sharing the exact same created timestamp straddles a page boundary, the ones that didn't fit in the earlier page are permanently dropped by the next page's < filter — silently under-crediting affected miners' reward share, and undercounting the equivalent discarded-share accounting/cleanup paths.
  • Fix: added shares.id (BIGSERIAL) as a strictly-increasing tiebreaker. ReadSharesBeforeAsync now orders by (created DESC, id DESC) and, for resumed pages, filters by created < @before OR (created = @before AND id < @beforeId) — a stable keyset cursor that can't lose rows at a shared timestamp. The first page of each run (inclusive=true) is unaffected since there's no prior cursor.
  • Added a supporting index (poolid, created DESC, id DESC), a migration (add_share_id_column.sql), and registered it with SchemaDriftTests.Every_fork_added_column_has_a_migration_script.

Test plan

  • dotnet build — 0 errors (WSL, Release)
  • SchemaDriftTests run to confirm the new migration is recognized
  • Apply add_share_id_column.sql against a production database before deploying this build (payout schemes will throw if shares.id doesn't exist)

🤖 Generated with Claude Code

… boundary

PPLNSPaymentScheme, PPLNSBFPaymentScheme and PROPPaymentScheme all page through
the shares table via ReadSharesBeforeAsync, resuming each page from the
previous page's last row's `created` timestamp with a plain `created < before`
filter. Shares are inserted in batches, and `created` timestamp resolution is
not fine enough to guarantee every share has a distinct value under load. If
the page boundary falls in the middle of a run of shares sharing the exact
same `created` timestamp, the shares from that run that didn't make it into
the earlier page are permanently excluded from both pages: the next page's
`<` filter drops everything at that timestamp, not just the rows already
seen. This silently under-credits affected miners' PPLNS/PPLNSBF/PROP reward
share, and the equivalent discarded-share accounting/cleanup paths undercount
too.

Fix: add a `shares.id` BIGSERIAL column (migration
add_share_id_column.sql) and use it as a strictly-increasing tiebreaker
alongside `created` for both ordering and the page cursor. ReadSharesBeforeAsync
now orders by (created DESC, id DESC) and, once a prior page has been read,
filters by `created < @before OR (created = @before AND id < @beforeId)`,
giving a stable keyset cursor that can't lose rows at a shared timestamp. The
first page per run (inclusive=true) is unaffected, since it has no prior
cursor to resume from.

Added a supporting index (poolid, created DESC, id DESC) and registered the
migration with SchemaDriftTests.Every_fork_added_column_has_a_migration_script.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@colneech-dev
colneech-dev merged commit c522c37 into main Nov 24, 2026
1 check passed
@colneech-dev
colneech-dev deleted the fix/share-paging-keyset-cursor branch November 24, 2026 21:27
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.

1 participant