Skip to content

Maintain incremental running totals for aggregates #226

Description

@kilodesodiq-arch

Problem Statement. get_aggregates(token) iterates from 0 to KEY_PKG_IDX every call
and reconstructs total_committed, total_claimed, total_expired_cancelled from the
package list. For a deployment with hundreds of packages this is O(N) per read and cannot
be cached.

Why it matters. Dashboard latency and off-chain indexer costs both grow linearly.

Technical Context. lib.rs::get_aggregates reads the pidx index keys, then walks
Package status. A previous fix introduced per-package KEY_PKG_IDX mapping that
preserves enumeration but compounds the iteration cost.

Expected Outcome. Replace per-token i128 totals at status transitions and
get_aggregates returns them in constant time.

Acceptance Criteria.

  • A new KEY_TOTAL_COMMITTED: Map<Address, i128> mirrored after
    KEY_TOTAL_LOCKED semantics.
  • Mutations encapsulated in a helper add_to_status_totals(env, token, status, amount).
  • Bench: get_aggregates constant time regardless of package count.

Implementation Notes. This can be coupled with #16 since both refactor storage access.

Files or modules likely to be affected. src/lib.rs, tests/invariants.rs (new).

Dependencies. #15 (invariant test), #16.

Difficulty. Medium
Estimated effort. M



Backlog item #18 from `docs/maintainer-issue-backlog.md.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions