You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[HIGH] get_top_players uses O(n²) selection sort with full Vec rebuilds — gas bomb that reverts on real leaderboards #16
[HIGH] get_top_players uses O(n²) selection sort with full Vec rebuilds — gas bomb that reverts on real leaderboards
Summary
get_top_players (leaderboard/src/lib.rs, lines ~361–432) sorts the top list with a selection sort where each swap rebuilds the entire Vec:
for i in0..n {letmut max_idx = i;for j in(i + 1)..n { ...}if max_idx != i {// rebuild the whole Vec to swap two elementsletmut rebuilt:Vec<PlayerEntry> = Vec::new(&env);for k in0..n { ...}
all = rebuilt;}}
With MAX_TOP_PLAYERS = 50, the worst case is ~1225 comparisons and up to ~1225 full Vec rebuilds of 50 elements each — tens of thousands of allocations in Soroban's linear memory. This exceeds the gas/CPU budget on a full leaderboard, so get_top_playersalways reverts for real users. The comment even acknowledges it's O(n²) but the "optimization" (fewer rebuilds when already sorted) does not help the common case where points are interleaved.
Why it's unsolvable by a localized patch
The sort is on-read — every call re-sorts from scratch; there is no cached sorted order.
Soroban Vec is immutable — "swapping" requires a full rebuild, so any comparison-sort is O(n²) allocations.
Reducing MAX_TOP_PLAYERS changes the product and still leaves the O(n²) cost.
A correct fix requires maintaining a sorted index at write time (in upsert_top) or a heap/ordered structure, so reads are O(k) — a storage-model redesign.
The root problem is sorting on read with an immutable data structure. A correct fix needs a write-time-ordered index, which is a fundamental change to the leaderboard storage.
Activity
grantfox-oss commentedon Aug 16, 2026
🦊 GrantFox — @pixels26 has been assigned to this issue as part of the Third Campaign campaign!
Next steps:
Closes #16)Good luck! Track your progress on GrantFox.
Aycode01 commentedon Aug 29, 2026
Hi can I work on this
grantfox-oss commentedon Aug 29, 2026
🦊 GrantFox — @Aycode01 has been assigned to this issue as part of the Third Campaign campaign!
Next steps:
Closes #16)Good luck! Track your progress on GrantFox.
grantfox-oss commentedon Aug 31, 2026
🎉 This issue has been marked as completed on GrantFox!
@Aycode01's PR #200 was approved and merged by @Muyideen-js.
🏆 @Aycode01: You earned 40 FoxPoints for this contribution! Your current tier: Builder (1,268 total points). Track your full progress on GrantFox.
👏 Great work, @Aycode01! Keep contributing to SPulse-Org.