Skip to content

perf(onchain): replace locked Map with per-token instance storage - #352

Open
ifygreg01-best wants to merge 6 commits into
ChainForgee:mainfrom
ifygreg01-best:feat/per-token-locked-storage
Open

perf(onchain): replace locked Map with per-token instance storage#352
ifygreg01-best wants to merge 6 commits into
ChainForgee:mainfrom
ifygreg01-best:feat/per-token-locked-storage

Conversation

@ifygreg01-best

@ifygreg01-best ifygreg01-best commented Jul 16, 2026

Copy link
Copy Markdown

Switch TotalLocked from a single Map<Address, i128> cell to a typed contracttype enum key — TotalLocked::Token(Address) — so each token's locked amount occupies its own instance-storage cell.

Previously, every decrement_locked call (claim, disburse, revoke, cancel_package, refund) deserialised the entire map, mutated one entry, and re-serialised the whole map — an O(N) ratchet that grew with the number of distinct tokens. Each create_package and batch_create_packages also performed the same full read/write inline.

Changes:

  • Add TotalLocked contracttype enum (Token(Address) variant)
  • Add get_locked / set_locked private helpers (single-cell O(1) read/write)
  • Remove KEY_TOTAL_LOCKED constant (no longer needed)
  • Update all 7 sites: create_package, batch_create_packages, decrement_locked, get_total_locked, withdraw_surplus
  • batch_create_packages retains its single read-before/write-after pattern, preserving atomic batch accounting and eliminating the reentrancy window where intermediate locked values were visible
  • Regenerate test snapshots to reflect new storage key layout

All 106 tests pass unchanged (aid_escrow_tests, gas_profiling, integration, batch, boundary, e2e, events, versioning, invariants).

Summary

Testing

# example
npm test

Screenshots

Checklist

Copy link
Copy Markdown
Contributor

👋 Thanks for the contribution! This PR contains code/test changes and CI workflows were never auto-triggered (a GitHub Actions safety requirement for cross-repo forks). To unblock this for merging, a maintainer needs to either:\n\n1. Manually click 'Approve and run workflows' on this PR's Actions tab via the GitHub UI, or\n2. Approve the pending CI on the PR checks page.\n\nOnce the CI checks pass, this can be merged. 🚀

ifygreg01-best and others added 6 commits July 23, 2026 09:06
Switch TotalLocked from a single Map<Address, i128> cell to a typed
contracttype enum key — TotalLocked::Token(Address) — so each token's
locked amount occupies its own instance-storage cell.

Previously, every decrement_locked call (claim, disburse, revoke,
cancel_package, refund) deserialised the entire map, mutated one entry,
and re-serialised the whole map — an O(N) ratchet that grew with the
number of distinct tokens. Each create_package and batch_create_packages
also performed the same full read/write inline.

Changes:
- Add TotalLocked contracttype enum (Token(Address) variant)
- Add get_locked / set_locked private helpers (single-cell O(1) read/write)
- Remove KEY_TOTAL_LOCKED constant (no longer needed)
- Update all 7 sites: create_package, batch_create_packages,
  decrement_locked, get_total_locked, withdraw_surplus
- batch_create_packages retains its single read-before/write-after
  pattern, preserving atomic batch accounting and eliminating the
  reentrancy window where intermediate locked values were visible
- Regenerate test snapshots to reflect new storage key layout

All 106 tests pass unchanged (aid_escrow_tests, gas_profiling,
integration, batch, boundary, e2e, events, versioning, invariants).
* feat: expose model_version in responses for humanitarian, anonymize, and ocr schemas

* feat: unify humanitarian verification parsing and define v2 schema

* fix: update deterministic test assertion to match normalized v2 schema
…ent (ChainForgee#282) (ChainForgee#377)

Co-authored-by: Bug-Hunter-X <Bug-Hunter-X@github.com>
…gee#341)

* feat: implement Sunset and Deprecation headers for legacy AI routes and Redis sliding window rate limiter

* test: add property-based test for AES envelope round-trip

* test: mock prisma client offline and fix rate limiter e2e tests
…talLocked cells

Closes ChainForgee#224.

Problem: KEY_TOTAL_LOCKED stored a single Map<Address, i128> in instance
storage. Every read deserialised the whole map and every write re-serialised
it — O(N-tokens) cost on each claim/disburse/revoke/cancel/refund, and an
optimistic read-modify-write that could be observed by a re-entered call
during batch_create_packages.

Fix: Introduce a #[contracttype] enum TotalLocked { Token(Address) } so each
token gets its own instance-storage cell. Reads and writes now touch exactly
one cell regardless of how many tokens the contract tracks.

Changes:
- Remove KEY_TOTAL_LOCKED Symbol constant
- Add TotalLocked contracttype enum
- Add private helpers get_locked / set_locked
- Rewrite decrement_locked to use the per-token cell
- Update create_package, batch_create_packages, withdraw_surplus, and
  get_total_locked to use the new helpers
- Add prop_*.json pattern to app/onchain/.gitignore (proptest artefacts)
- Refresh test snapshots (storage layout change)

All 130 tests pass (cargo test).
@ifygreg01-best
ifygreg01-best force-pushed the feat/per-token-locked-storage branch from 9892628 to a283f46 Compare July 23, 2026 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace optimistic Map writes on KEY_TOTAL_LOCKED with try_mutate semantics

4 participants