Skip to content

Optimize attestation_engine analytics counters to avoid full-history recomputation #496

Description

@1nonlypiece

📌 Description

attestation_engine maintains incremental analytics counters (TotalAttestations,
TotalViolations, TotalFees, VerifierAttestationCount), but
calculate_compliance_score (line ~1536) and get_health_metrics (line ~1253)
appear to re-aggregate over the full Attestations(commitment_id) history on each
call via the AttestationMetricAggregate reduction. For long histories this is an
unbounded CPU cost per read.

This issue caches aggregates so reads are O(1) rather than O(history).

🎯 Requirements and Context

  • Maintain a per-commitment cached HealthMetrics/aggregate updated incrementally on each attest/record_fees/record_drawdown, instead of recomputing on read.
  • Cached values must exactly match a full recomputation (add an equivalence test).
  • Use checked arithmetic so accumulation cannot overflow.
  • No change to public return types of get_health_metrics/calculate_compliance_score.

🛠️ Suggested Execution

1. Fork the repo and create a branch

git checkout -b perf/attestation-aggregate-cache

2. Implement changes

  • Update the write paths and read paths in contracts/attestation_engine/src/lib.rs to use the cached aggregate, with /// docs.
  • Record before/after costs under benchmarks/.

3. Test and commit

  • Extend contracts/attestation_engine/src/tests.rs and benchmarks.rs; add a cached-vs-recomputed equivalence test.
  • Run: cargo test --target wasm32v1-none --release
  • Edge cases: zero attestations, large history, mixed types, overflow guard.

Example commit message

perf: incremental aggregate cache for attestation analytics reads

✅ Guidelines

  • Minimum 95% test coverage on the cached paths.
  • Document the equivalence guarantee and measured savings.
  • Timeframe: 96 hours.

🏷️ Labels

type-performance · area-contracts · MAYBE REWARDED · GRANTFOX OSS · OFFICIAL CAMPAIGN

💬 Community & Support

  • Join the CommitLabs contributor Discord to coordinate and get unblocked fast: https://discord.gg/WV7tdYkJk
  • Introduce yourself before starting to avoid duplicate work.
  • Maintainers triage actively and review fast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GRANTFOX OSSGrantFox open-source campaignMAYBE REWARDEDEligible for GrantFox rewardOFFICIAL CAMPAIGNOfficial GrantFox campaign issuearea-contractsSmart contract (Soroban/Rust) worktype-performanceBenchmarks / performance

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions