Skip to content

Reduce redundant storage reads in record_usage's validation chain #30

@mikewheeleer

Description

@mikewheeleer

Refactor record_usage to minimise persistent storage reads

Description

record_usage in contracts/escrow/src/lib.rs performs a long sequence of independent env.storage().persistent().get(...) calls — paused flag, max-per-call, min-per-call, require-registration, service-registered, service-disabled, allowlist-enabled, agent-allowed — before it even touches the usage counter. Each persistent read carries ledger cost. This issue tightens the validation chain to read only what is necessary and short-circuit cleanly, lowering the gas cost of the hottest entrypoint.

Requirements and context

  • Repository scope: Agentpay-Org/Agentpay-contracts only.
  • Reorder and gate reads so conditional reads (e.g. ServiceRegistered) only happen when their controlling flag (RequireServiceRegistration) is enabled — the current short-circuit already does some of this; verify and extend it.
  • Avoid re-reading the same key twice within the call; cache locals where a value is used more than once.
  • Behaviour must be identical: same validation order semantics where order is observable (which error fires first), same error codes.
  • Document the read-count before/after in the PR to demonstrate the saving.

Suggested execution

  • Fork the repo and create a branch
  • git checkout -b refactor/contracts-30-record-usage-reads
  • Implement changes
    • Write code in: contracts/escrow/src/lib.rs — tighten the read chain in record_usage.
    • Write comprehensive tests in: contracts/escrow/src/test.rs — existing tests pass; add tests asserting error-precedence order is unchanged (e.g. paused beats zero-requests).
    • Add documentation: note the optimisation rationale in a code comment.
    • Include NatSpec-style doc comments (///) where helpers are introduced.
    • Validate security: no validation skipped, error precedence preserved.
  • Test and commit

Test and commit

  • Run cargo fmt --all -- --check, cargo build, and cargo test.
  • Cover edge cases: every gate still fires under its trigger condition; flags-off path reads the minimum.
  • Include the full cargo test output and a short security notes section in the PR description.

Example commit message

refactor: reduce redundant storage reads in record_usage validation

Guidelines

  • Minimum 95 percent test coverage for impacted modules.
  • Clear, reviewer-focused documentation.
  • Timeframe: 96 hours.

Community & contribution rewards

  • 💬 Join the AgentPay community on Discord for questions, reviews, and faster merges: https://discord.gg/eXvRKkgcv
  • ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions