Skip to content

get_dashboard_metrics reads every campaign in core/lib.rs` — quadratic in storage cost #24

Description

@P3az3

Problem Statement

crates/contracts/core/src/lib.rs::get_dashboard_metrics walks id from 1..=total_campaigns reading each one:

let mut active_campaigns: u64 = 0;
let mut id: u64 = 1;
while id <= total_campaigns {
    if let Some(c) = env.storage().persistent().get::<_, Campaign>(&campaign_key(id))
    { if c.active { active_campaigns += 1; } }
    id += 1;
}

This is O(N) reads. For platforms with 10,000 campaigns, this is 10,000 reads per view call. Soroban resource limits will exceed under non-trivial calls.

Why it matters

This is in the legacy core contract, but the same logic may propagate to campaign over time. Currently, campaign is single-campaign-per-instance, so the problem is structural in core.

Expected Outcome

Either:

(a) Maintain a separate ActiveCampaignCount storage key, incremented/decremented on status transitions, returning active_campaigns in O(1). Recommended.

(b) Add an explicit pagination cap (e.g., max 256 campaigns per view) so the view call stays within budget.

Acceptance Criteria

  • New storage key ActiveCampaignCount (in core contracts).
  • create_campaign increments it (campaigns start active).
  • withdraw / approve may not change the active flag in core; revisit.
  • get_dashboard_metrics reads the counter in O(1).
  • New test asserts increment/decrement behavior.

Implementation Notes

  • If core is being deprecated, this fix may be unnecessary — Issue 17.

Affected Files / Modules

  • crates/contracts/core/src/lib.rs

Dependencies — Issue 17 (deprecating core simplifies this).

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26performancePerformance / cost / budget optimisation.priority/p2Should-do in current quarter.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions