Skip to content

[Task]: Add property/invariant tests for the vault balance accounting #71

Description

@grantfox-oss

The current suite tests individual functions with fixed inputs. It does not assert that the core accounting invariants hold across arbitrary sequences of operations — and those invariants are exactly what keeps user funds safe. A regression in any of them is a fund-loss bug that fixed-input tests can easily miss.

The invariants that must hold after every operation, for every user/asset:

  1. 0 <= locked <= balance
  2. balance >= 0
  3. On finalize, locked decreases by exactly the task's plan_cost and balance decreases by exactly spent, where 0 <= spent <= plan_cost.
  4. total_spent is monotonically non-decreasing.
  5. sum(get_balance over users) for an asset never exceeds the vault's real token_balance for that asset (only assertable once issue feat: Add update_orchestrator function to allow users to rotate their… #34's reconciliation view lands — treat as optional/stretch here).

Goal

A new test module that drives multi-step operation sequences against a single vault and asserts invariants 1–4 after each step.

Requirements & constraints

  • Deterministic and reproducible. Use a seeded generator (a small hand-rolled LCG over a fixed op vocabulary is enough and keeps the crate #![no_std]/dependency-free). If you prefer proptest, gate it strictly behind #[cfg(test)] dev-dependencies and keep CI green.
  • Coverage of the op vocabulary: deposit, withdraw, create_task, release_payment, complete_task, cancel_task, force_complete_stale_task (advance ledger time via env.ledger() to trigger staleness).
  • Sequences should include multiple users and at least two assets so cross-account/cross-asset isolation is exercised.
  • On any invariant violation, the failure message must print the seed and the step index so it's reproducible.

Acceptance criteria

  • New seeded, deterministic test module drives randomized multi-step sequences
  • Invariants 1–4 asserted after every operation
  • At least one sequence exercises partial releases followed by completion and checks the refund math (invariant 3)
  • Generator covers the full op vocabulary above, with ≥2 users and ≥2 assets
  • Failures report the seed + step index
  • No production (lib.rs) changes are required
  • cargo test and cargo clippy --all-targets -- -D warnings pass

Pointers

  • Add to contracts/agent-vault/src/tests.rs; reuse the existing setup_test() harness and token-minting helpers.

Notes for contributors

If you'd like to work on this, comment below so we can assign it to you.
Questions welcome — see CONTRIBUTING.md for setup
and workflow.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions