Skip to content

Fix reverting leaderboard and replace O(n^2) sorting with bounded top-K - #16

Merged
llinsss merged 2 commits into
DogStark:mainfrom
Xaxxoo:fix/issue-8
Aug 31, 2026
Merged

llinsss merged 2 commits into
DogStark:mainfrom
Xaxxoo:fix/issue-8

Conversation

@Xaxxoo

@Xaxxoo Xaxxoo commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix empty leaderboard revert: Return an empty array when registeredPlayers.length == 0 or topN == 0 to prevent Solidity 0.8 checked arithmetic underflow on total - 1.
  • Replace O(n^2) bubble sort with bounded top-K insertion sort: The new algorithm is O(total * topN), which is effectively O(total) for constant topN values (e.g., 10 or 100). This eliminates the quadratic cost of copying and sorting the entire player set on every call.
  • Define stable tie-breaking: Players with equal totalScore are ordered by registration order (earlier registration first).
  • Add getLeaderboardPage(limit, offset): Pagination support for off-chain UIs that page through the full leaderboard.
  • Comprehensive test coverage: Tests for 0 players, topN=0, single player, tied scores, large-N with small topN, oversized topN clamping, zero-score players, and pagination edge cases.

Closes #8

Test plan

  • Verify getLeaderboard(10) returns empty array with zero registered players (no revert)
  • Verify getLeaderboard(0) returns empty array with registered players
  • Verify single-player leaderboard returns correct entry
  • Verify topN > totalPlayers is clamped correctly
  • Verify tied scores maintain registration-order tie-breaking
  • Verify top-K selection from 10 players requesting top 3
  • Verify pagination via getLeaderboardPage across multiple pages
  • Run npx hardhat test to execute all tests

Generated with Claude Code

Xaxxoo and others added 2 commits August 31, 2026 06:25
- Return empty array when registeredPlayers.length == 0 or topN == 0
  to prevent Solidity 0.8 checked arithmetic underflow
- Replace full bubble sort with bounded insertion sort O(total * topN)
- Define stable tie-breaking: equal scores ordered by registration order
- Add getLeaderboardPage() for pagination support
- Add tests for 0 players, 0 topN, 1 player, tied scores, large-N,
  oversized topN, and pagination edge cases

Closes DogStark#8

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@llinsss
llinsss merged commit fdf446e into DogStark:main Aug 31, 2026
1 check failed
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.

Replace reverting, quadratic on-chain leaderboard implementation

2 participants