Skip to content

Storage example#36

Draft
mohammadfawaz wants to merge 2 commits into
mainfrom
mohammadfawaz/storage_example
Draft

Storage example#36
mohammadfawaz wants to merge 2 commits into
mainfrom
mohammadfawaz/storage_example

Conversation

@mohammadfawaz

@mohammadfawaz mohammadfawaz commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

Adds a storage example demonstrating storage singletons, struct storage, vector storage, and external storage access in Leo 4.

What's in the example

Two programs that together showcase on-chain storage and cross-program reads:

  • treasury.aleo — Declares and manages on-chain state using Leo 4 storage variables:

    • Singletons: storage balance: u64, storage is_frozen: bool, storage deposit_count: u32
    • Struct storage: storage config: FeeConfig — stored and read atomically
    • Vector storage: storage deposit_log: [u64] — dynamically-sized on-chain list with push, pop, get, len, clear
    • Demonstrates unwrap / unwrap_or for reading optional slots, none assignment for clearing, and the Final return type
  • auditor.aleo — Reads treasury storage externally without any getter functions:

    • External singleton reads: treasury.aleo::balance.unwrap_or(0u64)
    • External struct reads: treasury.aleo::config.unwrap_or(treasury.aleo::FeeConfig { ... })
    • External vector reads: treasury.aleo::deposit_log.len(), treasury.aleo::deposit_log.get(0u32)
    • Cross-program struct type references: treasury.aleo::FeeConfig

How to run

run.sh handles the full flow:

  1. Part 1 (local, no devnode): runs treasury operations locally — initialize, deposit, withdraw, freeze, unfreeze, config update
  2. Part 2 (requires leo devnode): deploys both programs, performs treasury operations on-chain, then runs auditor functions that read treasury storage cross-program — including detecting a treasury freeze

@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/storage_example branch from 6a86ed0 to 716997b Compare April 16, 2026 15:38
@mohammadfawaz mohammadfawaz marked this pull request as draft April 16, 2026 15:47
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/storage_example branch from 716997b to 5f014e0 Compare April 16, 2026 17:09
@mohammadfawaz mohammadfawaz self-assigned this May 1, 2026
Treasury improvements
- Added `correct_deposit(index, amount)` and `drop_deposit(index)` so the
  example demonstrates `vec.set` and `vec.swap_remove` (previously listed
  in the comments but not exercised).
- Renamed `pop_last_deposit` -> `undo_last_deposit` and made it consistent
  with the rest of the model: it now refunds `balance` and decrements
  `deposit_count` along with popping the log, so the treasury's three
  storage shapes never drift out of sync.
- Header comment now lists every supported vector op for quick reference.

Unit tests (new)
- `treasury/tests/test_treasury.leo` (16 cases) drives every transition
  and asserts on the resulting on-chain state via
  `treasury.aleo::<var>` reads from inside finalize blocks. Covers
  defaults, initialize, deposit/withdraw, freeze gating, config update,
  every vector op, reset, and `@should_fail` paths for overdraw,
  withdraw-without-balance, freeze-blocks-deposit, freeze-blocks-withdraw,
  and undo-on-empty-log.
- `auditor/tests/test_auditor.leo` (10 cases) exercises cross-program
  reads: snapshot mirrors treasury state, defaults when treasury is
  empty, detects freeze, clears alert after unfreeze, fee-config
  thresholding (default/low/high/fallback), and deposit-log checks.
- Both run with `leo test` against an in-memory ledger; no devnode needed.

README + run.sh
- README now documents the new vector ops, points to the unit tests,
  and adds a "Part 2 - Unit tests" section.
- run.sh demos `correct_deposit` and `drop_deposit` in the local-execution
  walkthrough.
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.

1 participant