fix: replace reverting, quadratic on-chain leaderboard - #13
Open
Xaxxoo wants to merge 1 commit into
Open
Conversation
- Return empty array when registeredPlayers.length == 0 or topN == 0, fixing the arithmetic underflow panic on empty leaderboard - Replace O(n²) bubble sort with O(n * topN) partial selection sort, eliminating quadratic gas growth for view calls - Add deterministic tie-breaking: higher streak first, then earlier registration order - Cap topN to actual player count so oversized requests never revert - Add tests for 0-player, topN=0, single-player, oversized topN, and tie-breaking edge cases Closes DogStark#8 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes #8
getLeaderboard()now returns an empty array whenregisteredPlayers.length == 0ortopN == 0, instead of reverting with an arithmetic panic from the uncheckedtotal - 1expression.totalScore, the one with the higherstreakis ranked first; if streaks are also equal, earlier registration order (lower array index) wins.topNhandled safely:topN > playerCountis clamped toplayerCount, never reverts.Test coverage added
[]topN = 0[]topN > playerCountAll 17 tests pass.
Test plan
npx hardhat test— all tests passgetLeaderboard(10)on empty contract returns[](no revert)getLeaderboard(0)always returns[]topN > playerCountreturns correct subset🤖 Generated with Claude Code