Skip to content

Commit acc4ab7

Browse files
authored
docs: add readme for invariant testing examples (#550)
1 parent bb41bc0 commit acc4ab7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#### Usage Examples
44

55
- [Simple examples](simple/test/)
6-
- [Invariant testing](invariants/test/)
6+
- [Invariant testing](invariants/README.md)
77
- [ERC20](tokens/ERC20/test/): verifies OpenZeppelin, Solady, Solmate ERC20 tokens, and CurveTokenV3.
88
- Includes identifying the DEI token bug exploited in the [Deus DAO hack](https://rekt.news/deus-dao-r3kt/).
99
- [ERC721](tokens/ERC721/test/): verifies OpenZeppelin, Solady, and Solmate ERC721 tokens.

examples/invariants/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Halmos Invariant Testing Examples
2+
3+
This directory contains examples demonstrating various approaches to invariant testing with halmos.
4+
5+
## Examples
6+
7+
- **ERC20 Test** ([`ERC20.t.sol`](test/ERC20.t.sol)): Tests the fundamental ERC20 invariant: the sum of all balances equals the total supply. Shows how to write handlers for tracking token holders.
8+
9+
- **ERC721 Test** ([`ERC721.t.sol`](test/ERC721.t.sol)): Simple ERC721 invariant testing without handlers. Demonstrates how straightforward invariant testing can be when state tracking isn't needed.
10+
11+
- **MiniVat Test** ([`MiniVat.t.sol`](test/MiniVat.t.sol)): Halmos version of [Antonio's MiniVat test](https://github.com/aviggiano/property-based-testing-benchmark/blob/main/projects/dai-certora/test/TargetFunctions.sol). Unlike other fuzzers, no bounds are needed, so no handlers are required. Simply deploy the contract and write the invariant.
12+
13+
- **Vat Test** ([`Vat.t.sol`](test/Vat.t.sol)): Tests the full Vat contract using the same invariant as MiniVat. It is more comprehensive and takes longer to run.
14+
15+
- **SimpleState Test** ([`SimpleState.t.sol`](test/SimpleState.t.sol)): Example from the [ItyFuzz paper](https://arxiv.org/pdf/2306.17135) that's hard to detect with simple random testing. Halmos uses exhaustive search and considers only unique state-generating call sequences for efficient space exploration.
16+
17+
- **Reentrancy Test** ([`Reentrancy.t.sol`](test/Reentrancy.t.sol)): Shows how to detect reentrancy exploits using halmos invariant testing. Demonstrates attacker contract implementation using [halmos cheatcodes](https://github.com/a16z/halmos-cheatcodes).
18+
19+
## Running Tests
20+
21+
```bash
22+
cd examples/invariants
23+
24+
# Run all invariant tests
25+
halmos
26+
27+
# Run specific test
28+
halmos --contract ERC20Test
29+
30+
# Run specific invariant
31+
halmos --function invariant_sumOfBalancesEqualsTotalSupply
32+
```

0 commit comments

Comments
 (0)