feat(reputation): accumulate per-address reputation deltas in memory … - #2168
Open
Chigybillionz wants to merge 1 commit into
Open
feat(reputation): accumulate per-address reputation deltas in memory …#2168Chigybillionz wants to merge 1 commit into
Chigybillionz wants to merge 1 commit into
Conversation
…for batched writes
Contributor
|
Well done on the job done so far! |
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.
Close #2154
Summary of the Issue
contracts/earn-quest/src/reputation.rsupdated reputation per submission with an individual storage read and write for each entry. During batch approvals or batch XP awards, this pattern executed redundant storage operations per submission, burning significant gas and CPU instructions on Soroban.Root Cause
reputation.rslacked an in-memory batch accumulation utility to group reputation updates by target address prior to storage persistence. As a result, processingget_user_stats_or_default) andset_user_stats) instead of consolidating updates into a single atomic read-modify-write cycle.Solution Implemented
Implemented
award_xp_batchincontracts/earn-quest/src/reputation.rs(and exposed it viaEarnQuestContractinlib.rs). The function aggregates per-address experience points (XP) and completed quest counts in memory using asoroban_sdk::Map<Address, (u64, u32)>during batch processing. Once accumulated, a single storage read + write is executed for each unique affected user address.Key Changes Made
contracts/earn-quest/src/reputation.rs:pub fn award_xp_batch(env: &Env, grants: &Vec<(Address, u64)>) -> Result<(), Error>using in-memory delta accumulation.contracts/earn-quest/src/lib.rs:award_xp_batchentrypoint in#[contractimpl]withbump_instance_ttl.contracts/earn-quest/tests/test_reputation.rs:test_award_xp_batch_equivalence_single_user_multiple_grants,test_award_xp_batch_equivalence_multiple_users, andtest_award_xp_batch_empty_grant_list) confirming final user state (XP, level, quests completed) is mathematically identical between batched and sequential writes.contracts/earn-quest/tests/gas_benchmarks.rs:benchmark_award_xp_batch_impacttest demonstrating a 78.66% CPU instruction cost reduction (1,190,537instructions saved for 5 awards).Any Trade-offs or Considerations
xp_awardedevents are emitted once per unique user address per batch with the aggregate XP delta awarded, rather than emitting individual events for each sub-entry. This further minimizes event logging gas without losing accounting fidelity.award_xpcalls remain fully preserved for non-batched flows.Testing Steps (How to Verify the Fix)
cargo test --test test_reputationRun Gas Impact Benchmarks:
bash
cargo test --test gas_benchmarks -- --nocapture
Run Full Test Suite:
bash
cargo test
Please kindly review this task. If there are any corrections, improvements, adjustments, or merge conflicts that you notice regarding my implementation, I'd really appreciate your feedback. I'd also love to hear your overall review of my work on this branch.Thank you!