feat(onchain): maintain incremental running totals for aggregates (#226)#416
Open
Moonwalker-rgb wants to merge 4 commits into
Open
Conversation
…ainForgee#226) Replace O(N) iteration in get_aggregates with constant-time reads from per-token running totals maintained at every status transition. Changes: add KEY_TOTAL_COMMITTED and KEY_TOTAL_EXPIRED_CANCELLED maps, add_to_status_totals helper, hook all status transitions, rewrite get_aggregates to O(1). Also fix: disburse now correctly tracks claimed totals.
Multi-line argument formatting for all add_to_status_totals invocations. Collapse Map::new chain into single line. Remove duplicate doc comment on get_aggregates.
Revert add_to_status_totals calls to single-line format. Map init in helper now two lines as preferred by rustfmt.
Multi-line formatting only for add_to_status_totals calls where -package.amount pushes line length past the threshold (disburse, revoke, refund, cancel_package). Short calls stay single-line.
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.
Problem
Resolves #226 — Maintain incremental running totals for aggregates
get_aggregates(token)currently iterates from 0 toKEY_PKG_IDXevery call, reconstructingtotal_committed,total_claimed, andtotal_expired_cancelledby walking the full package list. For deployments with hundreds of packages, this is O(N) per read, driving dashboard latency and indexer costs linearly upward. The existing per-packageKEY_PKG_IDXmapping preserves enumeration but compounds the iteration cost.What was implemented
New storage keys — constant-time per-token totals
Added two new instance-storage maps mirroring the existing
KEY_TOTAL_LOCKEDsemantics:KEY_TOTAL_COMMITTED("cmt") —Map<Address, i128>tracking committed (Created) amounts per tokenKEY_TOTAL_EXPIRED_CANCELLED("expcan") —Map<Address, i128>tracking expired/cancelled/refunded amounts per tokenadd_to_status_totalshelperA private, zero-clamped helper function that updates the correct storage map based on
PackageStatus:Created→KEY_TOTAL_COMMITTEDClaimed→KEY_TOTAL_CLAIMED(already existed)Expired | Cancelled | Refunded→KEY_TOTAL_EXPIRED_CANCELLEDPositive amounts increment the total; negative amounts decrement with a zero floor — consistent with the existing
decrement_lockedpattern. No-ops onamount == 0.Status transition hooks — every transition tracked
create_package/batch_create_packagescommitted += amountclaim/claim_with_proof(viafinalize_claim)committed -= amount,claimed += amountdisbursecommitted -= amount,claimed += amountrevokecommitted -= amount,expired_cancelled += amountcancel_packagecommitted -= amount,expired_cancelled += amountrefundcommitted -= amount,expired_cancelled += amountrefundThe
refundfunction uses awas_committedflag captured before the intermediatepackage.status = Expiredmutation to correctly distinguish the Created path from the Expired/Cancelled re-entry paths.get_aggregatesrewritten to O(1)Instead of iterating
0..KEY_PKG_IDXand summing package amounts,get_aggregatesnow reads the three storage maps in constant time. The return type (Aggregates) and its semantics are unchanged — all existing tests pass without modification.Bug fix included
disbursepreviously never updatedKEY_TOTAL_CLAIMED. Disbursed packages now correctly contribute tototal_claimedin bothget_aggregatesandget_total_claimed.How to manually verify
cargo test --package aid_escrow --test gas_profiling) confirmsget_aggregatesis now constant-time regardless of package count.prop_event_replay_conservationinvariant fuzz test (100 random sequences) continues to hold.Follow-up work recommended
batch_create_packagesto accumulate the total committed amount and calladd_to_status_totalsonce after the loop instead of N times inside it, reducing storage writes for large batches.get_aggregateswith 10, 100, and 500 packages to quantify the O(1) improvement.get_total_committed(token)andget_total_expired_cancelled(token)to expose the new storage keys to off-chain indexers.