test(earn-quest): property tests for incremental user-stat counters - #2170
Merged
RUKAYAT-CODER merged 2 commits intoAug 19, 2026
Merged
Conversation
…arnQuestOne#2153) get_user_stats reads the incrementally-maintained UserCore counters (updated in place by award_xp on the completion path) as an O(1) storage lookup rather than scanning and recomputing over a user's award history. Add a property/fuzz suite (test_incremental_stats) that proves the invariant this relies on: for any sequence of XP awards, the incrementally-folded counters (xp, level, quests_completed) always equal a full recompute from the complete history. The tests are pure (no storage/ledger), so they add no gas to the hot path. Document the O(1) incremental guarantee on get_user_stats.
… no Vec) The earn-quest contract is `#![no_std]`, so `std::vec::Vec` is not in scope. Generate the fuzzed award sequences into a fixed-size `[u64; 64]` buffer and slice it, keeping the property test dependency-free and no_std-compatible.
4 tasks
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.
Closes #2153
Summary
get_user_statsreturns a user's aggregate stats from the incrementally-maintainedUserCorecounters (xp,level,quests_completed) —award_xpupdates them in place on the completion path, soget_user_statsis an O(1) storage read (storage::get_user_stats_or_default) rather than a scan-and-recompute over the user's award history. Read cost and gas therefore do not grow with history.This PR locks in the invariant that optimisation depends on and documents it.
Changes
test_incremental_statssuite — a property/fuzz test that, over thousands of randomised award sequences, asserts the incrementally-folded counters always equal a full recompute from the complete award history (apply_incremental(awards) == full_recompute(awards)), plus boundary checks that the storedlevelmatchescalculate_level(xp)at every threshold. The tests are pure (no storage/ledger/contract calls), so they add no gas to the hot path and can never regress the gas benchmarks.# Performance (#2153)note onreputation::get_user_statsmaking the O(1) incremental guarantee explicit and pointing at the property suite.Acceptance criteria
get_user_stats(no per-call recompute) — already the case; now proven and documented.