Skip to content

Replace optimistic Map writes on KEY_TOTAL_LOCKED with try_mutate semantics #224

Description

@kilodesodiq-arch

Problem Statement. decrement_locked reads, mutates in-memory, then writes the whole
Map<Address, i128>. Because Map is a Soroban instance-storage value, every read
deserializes and every write serializes — an O(N) instance-storage ratchet every time a
token is decremented.

Why it matters. Beyond CPU cost, the bigger risk is that during
batch_create_packages, intermediate locked-map reads are technically the contract's
"current" state, but a reentered call would see them and could overcommit.

Technical Context. lib.rs::decrement_locked is invoked after every claim,
disburse, revoke, cancel_package, and refund. Each call serializes a fresh map.

Expected Outcome. Switch from Map to a per-token instance-storage Symbol → i128
(TotalLocked: token) and read/write one cell per call.

Acceptance Criteria.

  • All four call sites (claim, disburse, revoke, cancel_package, refund) use the new
    accessor.
  • Existing aid_escrow_tests.rs continues to pass unchanged.
  • Gas profiling of gas_profiling.rs shows measurable improvement for batch ops
    (positive or neutral).

Implementation Notes. Use env.storage().instance().set(&TotalLocked::Token(token), &new) and a typed accessor via contracttype enum.

Files or modules likely to be affected. app/onchain/contracts/aid_escrow/src/lib.rs,
tests/aid_escrow_tests.rs, tests/gas_profiling.rs.

Dependencies. #18 (incremental aggregates).

Difficulty. Hard
Estimated effort. L



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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions