-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEchidnaTestExample.sol
More file actions
240 lines (196 loc) · 8.06 KB
/
EchidnaTestExample.sol
File metadata and controls
240 lines (196 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
/**
* @title Example Echidna Property Tests
* @notice Demonstrates property-based testing with Echidna fuzzer
* @dev Echidna will automatically test all functions with the echidna_ prefix
*
* To run Echidna tests:
* 1. Install Echidna: https://github.com/crytic/echidna
* 2. Run: echidna . --contract EchidnaTestExample --config echidna.yaml
*/
// Custom errors for gas optimization
error ZeroDeposit();
error InsufficientBalance();
error TransferFailed();
/// @notice Simple vault contract for testing
contract Vault {
mapping(address => uint256) public balances;
uint256 public totalDeposits;
event Deposit(address indexed user, uint256 amount);
event Withdraw(address indexed user, uint256 amount);
function deposit() external payable {
if (msg.value == 0) revert ZeroDeposit();
unchecked {
// Safe: totalDeposits tracks contract balance, cannot overflow in practice
balances[msg.sender] += msg.value;
totalDeposits += msg.value;
}
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint256 amount) external {
if (balances[msg.sender] < amount) revert InsufficientBalance();
unchecked {
// Safe: checked above that balance >= amount
balances[msg.sender] -= amount;
totalDeposits -= amount;
}
(bool success, ) = msg.sender.call{value: amount}("");
if (!success) revert TransferFailed();
emit Withdraw(msg.sender, amount);
}
function getBalance(address user) external view returns (uint256) {
return balances[user];
}
}
/// @notice Echidna test contract
/// @dev All properties are prefixed with "echidna_" for automatic discovery
contract EchidnaTestExample {
Vault public vault;
uint256 private initialBalance;
constructor() payable {
vault = new Vault();
initialBalance = address(this).balance;
}
// ========================================================================
// Echidna Property Tests
// ========================================================================
/// @notice Total deposits should never exceed contract balance
/// @dev This is a critical invariant for vault safety
function echidna_total_deposits_lte_balance() public view returns (bool) {
return vault.totalDeposits() <= address(vault).balance;
}
/// @notice Contract balance should equal total deposits
/// @dev Ensures no ether is locked or lost
function echidna_balance_equals_deposits() public view returns (bool) {
return address(vault).balance == vault.totalDeposits();
}
/// @notice User balance should never exceed total deposits
/// @dev Prevents individual user from withdrawing more than total
function echidna_user_balance_lte_total() public view returns (bool) {
return vault.balances(address(this)) <= vault.totalDeposits();
}
/// @notice Total deposits should be non-negative
/// @dev Underflow protection check
function echidna_total_deposits_non_negative() public view returns (bool) {
// uint256 is always >= 0, but this checks for underflow bugs
return vault.totalDeposits() >= 0;
}
/// @notice Vault balance should never decrease below total deposits
/// @dev Ensures solvency - contract must have enough to cover withdrawals
function echidna_vault_is_solvent() public view returns (bool) {
return address(vault).balance >= vault.totalDeposits();
}
// ========================================================================
// Helper Functions for Echidna to Call
// ========================================================================
/// @notice Deposit random amount
function deposit(uint256 amount) public {
// Bound the amount to available balance
if (amount > address(this).balance) {
amount = address(this).balance;
}
if (amount > 0) {
vault.deposit{value: amount}();
}
}
/// @notice Withdraw random amount
function withdraw(uint256 amount) public {
uint256 myBalance = vault.balances(address(this));
if (amount > myBalance) {
amount = myBalance;
}
if (amount > 0) {
vault.withdraw(amount);
}
}
/// @notice Check balance (read-only, helps with coverage)
function checkBalance() public view returns (uint256) {
return vault.getBalance(address(this));
}
// Required to receive ETH
receive() external payable {}
}
/// @notice Advanced Echidna test with state tracking
contract EchidnaAdvancedTest {
Vault public vault;
// Ghost variables for tracking expected state
uint256 private ghost_totalDeposited;
uint256 private ghost_totalWithdrawn;
constructor() payable {
vault = new Vault();
}
// ========================================================================
// Properties Using Ghost Variables
// ========================================================================
/// @notice Total deposits should equal ghost tracking
function echidna_ghost_deposits_match() public view returns (bool) {
return vault.totalDeposits() == (ghost_totalDeposited - ghost_totalWithdrawn);
}
/// @notice Ghost variables should maintain consistency
function echidna_ghost_consistency() public view returns (bool) {
return ghost_totalDeposited >= ghost_totalWithdrawn;
}
/// @notice Contract balance matches expected based on ghost variables
function echidna_balance_matches_ghost() public view returns (bool) {
uint256 expectedBalance = ghost_totalDeposited - ghost_totalWithdrawn;
return address(vault).balance == expectedBalance;
}
// ========================================================================
// Operations with Ghost Variable Updates
// ========================================================================
function depositWithTracking(uint256 amount) public {
if (amount > address(this).balance) {
amount = address(this).balance;
}
if (amount > 0) {
unchecked {
// Safe: tracking actual deposits, overflow extremely unlikely
ghost_totalDeposited += amount;
}
vault.deposit{value: amount}();
}
}
function withdrawWithTracking(uint256 amount) public {
uint256 myBalance = vault.balances(address(this));
if (amount > myBalance) {
amount = myBalance;
}
if (amount > 0) {
unchecked {
// Safe: tracking actual withdrawals, amount <= myBalance
ghost_totalWithdrawn += amount;
}
vault.withdraw(amount);
}
}
receive() external payable {}
}
/// @notice Example with assertion mode testing
contract EchidnaAssertionTest {
Vault public vault;
constructor() payable {
vault = new Vault();
}
/// @notice Test that demonstrates assertion testing
/// @dev In assertion mode, Echidna tries to make this assertion fail
function testDepositWithdrawRoundtrip(uint256 amount) public {
// Bound the amount
if (amount > address(this).balance) {
amount = address(this).balance;
}
if (amount == 0) {
return;
}
uint256 balanceBefore = address(this).balance;
// Deposit
vault.deposit{value: amount}();
assert(vault.balances(address(this)) == amount);
// Withdraw
vault.withdraw(amount);
// Balance should be restored (minus gas costs, but we're not tracking those)
// Note: This might fail due to reentrancy or other issues Echidna can find
assert(address(this).balance >= balanceBefore - amount);
}
receive() external payable {}
}