Skip to content

feat(onchain): add proptest invariant suite for crowdfund_vault#440

Open
Johnsmichael150 wants to merge 2 commits intoPulsefy:mainfrom
Johnsmichael150:main
Open

feat(onchain): add proptest invariant suite for crowdfund_vault#440
Johnsmichael150 wants to merge 2 commits intoPulsefy:mainfrom
Johnsmichael150:main

Conversation

@Johnsmichael150
Copy link
Copy Markdown

feat(onchain): Add proptest invariant suite for crowdfund_vault

Summary

Introduces a property-based test suite (tests/invariants.rs) for the crowdfund_vault Soroban smart contract using the proptest crate. All changes are in the test layer — no production code was modified.

Motivation

Hand-written unit tests in src/test.rs cover specific examples but cannot exhaustively explore the input space. Property-based testing generates thousands of randomized scenarios to verify that core protocol invariants hold regardless of input, catching edge cases that example-based tests miss.

Changes

New file: apps/onchain/contracts/crowdfund_vault/tests/invariants.rs

Five property tests, each running 1000 cases:

Property Test function Validates
1 — Balance equals sum of contributions prop_balance_equals_sum_of_contributions Req 1.1, 1.5
4 — Contribution tracking round-trip prop_contribution_tracking Req 2.1
7 — Overdraft is rejected prop_overdraft Req 3.1
9 — Single-contributor match approximation prop_single_contributor_match Req 4.2
11 — Paused contract blocks all mutations prop_paused_blocks_mutations Req 5.1, 5.2, 5.3

Modified: apps/onchain/contracts/crowdfund_vault/Cargo.toml

Added proptest = "1" to [dev-dependencies].

Modified: apps/onchain/.gitignore

Excluded proptest-generated snapshot JSON files (**/test_snapshots/prop_*.json, **/test_snapshots/scaffold_*.json) to keep the repo clean.

How to run

# From apps/onchain/
cargo test -p crowdfund_vault --test invariants

Expected output: 6 tests pass (1 scaffold smoke test + 5 property tests).

Test coverage

  • Property 1: Generates 1–5 distinct contributors with amounts 1–1,000,000. Asserts get_balance == Σ amounts and total_deposited == Σ amounts.
  • Property 4: Generates a fresh contributor and deposit amount. Asserts get_contribution == prior + amount.
  • Property 7: Generates deposit D and excess E > 0. Asserts try_withdraw(D + E) returns an error and balance remains D.
  • Property 9: Generates a single contribution C (1–10,000,000). Asserts calculate_match is within 1% of C (fixed-point rounding tolerance for sqrt_scaled).
  • Property 11: Pauses the contract. Asserts try_deposit, try_create_project, and try_withdraw all return ContractPaused and no state changes.

Notes

  • Optional properties (2, 3, 5, 6, 8, 10, 12) were skipped per spec — they can be added incrementally.
  • Each proptest! block constructs a fresh Env inside the closure because Soroban's Env is not Send.
  • Contributor counts are kept to 1–5 per test run to stay within Soroban ledger limits.

Close #429

- Add proptest = "1" to crowdfund_vault dev-dependencies
- Add tests/invariants.rs with 5 property-based tests (1000 cases each):
  - Property 1: balance equals sum of contributions (Req 1.1, 1.5)
  - Property 4: contribution tracking round-trip (Req 2.1)
  - Property 7: overdraft is rejected (Req 3.1)
  - Property 9: single-contributor match approximation (Req 4.2)
  - Property 11: paused contract blocks all mutations (Req 5.1-5.3)
- Update apps/onchain/.gitignore to exclude proptest snapshot JSON files
- Add spec files: .kiro/specs/invariant-hardening/
@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 24, 2026

@Johnsmichael150 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Cedarich
Copy link
Copy Markdown
Contributor

Please fix failing workflow

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.

Invariant Testing & Formal Verification Setup

3 participants