Skip to content

Commit 4ce955b

Browse files
committed
style: apply style guide and fix linter warnings
1 parent eba9343 commit 4ce955b

File tree

2 files changed

+111
-96
lines changed

2 files changed

+111
-96
lines changed

test/invariants/HandlerXanV1.t.sol

Lines changed: 97 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,123 +2,60 @@
22
pragma solidity ^0.8.30;
33

44
import {Test} from "forge-std/Test.sol";
5-
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
6-
import {XanV1} from "src/XanV1.sol";
5+
6+
import {XanV1} from "../../src/XanV1.sol";
77

88
contract XanV1Handler is Test {
99
XanV1 public token;
1010
address public initialHolder;
1111

1212
// ============ GHOST VARIABLES (for invariant tracking) ============
13-
address[] public actors;
14-
mapping(address => bool) internal isActor;
13+
address[] internal _actors;
14+
mapping(address actor => bool isActor) internal _isActor;
1515

1616
// Valid implementation addresses for testing
17-
address[5] public validImpls = [address(0x1111), address(0x2222), address(0x3333), address(0x4444), address(0x5555)];
17+
address[5] internal _validImpls =
18+
[address(0x1111), address(0x2222), address(0x3333), address(0x4444), address(0x5555)];
1819

19-
mapping(address => mapping(address => uint256)) internal previousVotes; // voter => impl => votes
20-
mapping(address => uint256) internal previousLockedBalances;
21-
uint256 internal previousLockedSupply;
20+
mapping(address voter => mapping(address impl => uint256 previousVote)) internal _previousVotes;
21+
mapping(address voter => uint256 previousLockedBalance) internal _previousLockedBalances;
22+
uint256 internal _previousLockedSupply;
2223

2324
constructor(XanV1 _token, address _initialHolder) {
2425
token = _token;
2526
initialHolder = _initialHolder;
2627
_addActor(_initialHolder);
2728
}
2829

29-
// ============ HELPER FUNCTIONS ============
30-
31-
function _addActor(address a) internal {
32-
if (!isActor[a]) {
33-
isActor[a] = true;
34-
actors.push(a);
35-
}
36-
}
37-
38-
function getActors() external view returns (address[] memory) {
39-
return actors;
40-
}
41-
42-
function getValidImpls() external view returns (address[5] memory) {
43-
return validImpls;
44-
}
45-
46-
function getPreviousVotes(address voter, address impl) external view returns (uint256) {
47-
return previousVotes[voter][impl];
48-
}
49-
50-
function getPreviousLockedBalance(address account) external view returns (uint256) {
51-
return previousLockedBalances[account];
52-
}
53-
54-
function getPreviousLockedSupply() external view returns (uint256) {
55-
return previousLockedSupply;
56-
}
57-
58-
function updateStateTracking() public {
59-
// Update previous locked supply
60-
previousLockedSupply = token.lockedSupply();
61-
62-
// Update previous locked balances and votes for all actors
63-
for (uint256 i = 0; i < actors.length; i++) {
64-
address actor = actors[i];
65-
previousLockedBalances[actor] = token.lockedBalanceOf(actor);
66-
// Capture votes across all implementations
67-
for (uint256 j = 0; j < validImpls.length; j++) {
68-
previousVotes[actor][validImpls[j]] = token.getVotes(actor, validImpls[j]);
69-
}
70-
}
71-
}
72-
73-
function updateStateTrackingFor(address account) public {
74-
// Update previous locked supply
75-
previousLockedSupply = token.lockedSupply();
76-
77-
// Update previous locked balance and votes for a specific account
78-
previousLockedBalances[account] = token.lockedBalanceOf(account);
79-
for (uint256 j = 0; j < validImpls.length; j++) {
80-
previousVotes[account][validImpls[j]] = token.getVotes(account, validImpls[j]);
81-
}
82-
}
83-
84-
function updateStateTrackingFor(address account1, address account2) public {
85-
// Update previous locked supply
86-
previousLockedSupply = token.lockedSupply();
87-
88-
// Update state for account1
89-
previousLockedBalances[account1] = token.lockedBalanceOf(account1);
90-
for (uint256 j = 0; j < validImpls.length; j++) {
91-
previousVotes[account1][validImpls[j]] = token.getVotes(account1, validImpls[j]);
92-
}
93-
94-
// Update state for account2
95-
previousLockedBalances[account2] = token.lockedBalanceOf(account2);
96-
for (uint256 j = 0; j < validImpls.length; j++) {
97-
previousVotes[account2][validImpls[j]] = token.getVotes(account2, validImpls[j]);
98-
}
99-
}
100-
10130
// ============ FUZZED BUSINESS LOGIC FUNCTIONS ============
10231

10332
function airdrop(address to, uint256 amount) external {
10433
// bounding to non-zero addresses, to avoid unnecessary reverts
10534
to = address(uint160(bound(uint160(to), 1, type(uint160).max)));
35+
10636
_addActor(to);
37+
10738
uint256 unlocked = token.unlockedBalanceOf(initialHolder);
10839
amount = bound(amount, 0, unlocked);
40+
10941
updateStateTrackingFor(initialHolder, to);
42+
11043
vm.prank(initialHolder);
11144
token.transfer(to, amount);
11245
}
11346

11447
function transfer(address from, address to, uint256 amount) external {
11548
from = address(uint160(bound(uint160(from), 1, type(uint160).max)));
11649
to = address(uint160(bound(uint160(to), 1, type(uint160).max)));
50+
11751
_addActor(from);
11852
_addActor(to);
53+
11954
uint256 unlocked = token.unlockedBalanceOf(from);
12055
amount = bound(amount, 0, unlocked);
56+
12157
updateStateTrackingFor(from, to);
58+
12259
vm.prank(from);
12360
token.transfer(to, amount);
12461
}
@@ -137,8 +74,10 @@ contract XanV1Handler is Test {
13774
function transferAndLock(address from, address to, uint256 amount) external {
13875
from = address(uint160(bound(uint160(from), 1, type(uint160).max)));
13976
to = address(uint160(bound(uint160(to), 1, type(uint160).max)));
77+
14078
_addActor(from);
14179
_addActor(to);
80+
14281
uint256 unlocked = token.unlockedBalanceOf(from);
14382
amount = bound(amount, 0, unlocked);
14483

@@ -154,8 +93,8 @@ contract XanV1Handler is Test {
15493
_addActor(who);
15594

15695
// Restrict to only valid implementation addresses
157-
implIndex = bound(implIndex, 0, validImpls.length - 1);
158-
address impl = validImpls[implIndex];
96+
implIndex = bound(implIndex, 0, _validImpls.length - 1);
97+
address impl = _validImpls[implIndex];
15998

16099
updateStateTrackingFor(who);
161100

@@ -198,8 +137,8 @@ contract XanV1Handler is Test {
198137
// Council-only; prank as council.
199138
address council = token.governanceCouncil();
200139

201-
implIndex = bound(implIndex, 0, validImpls.length - 1);
202-
address impl = validImpls[implIndex];
140+
implIndex = bound(implIndex, 0, _validImpls.length - 1);
141+
address impl = _validImpls[implIndex];
203142

204143
updateStateTracking();
205144

@@ -223,4 +162,78 @@ contract XanV1Handler is Test {
223162
// Anyone can attempt; requires quorum/min-locked reached or will revert.
224163
token.vetoCouncilUpgrade();
225164
}
165+
166+
// ============ GETTER FUNCTIONS ============
167+
168+
function getActors() external view returns (address[] memory arr) {
169+
arr = _actors;
170+
}
171+
172+
function getValidImpls() external view returns (address[5] memory arr) {
173+
arr = _validImpls;
174+
}
175+
176+
function getPreviousVotes(address voter, address impl) external view returns (uint256 previousVotes) {
177+
previousVotes = _previousVotes[voter][impl];
178+
}
179+
180+
function getPreviousLockedBalance(address account) external view returns (uint256 previousLockedBalance) {
181+
previousLockedBalance = _previousLockedBalances[account];
182+
}
183+
184+
function getPreviousLockedSupply() external view returns (uint256 previousLockedSupply) {
185+
previousLockedSupply = _previousLockedSupply;
186+
}
187+
188+
// ============ HELPER FUNCTIONS ============
189+
190+
function updateStateTracking() public {
191+
// Update previous locked supply
192+
_previousLockedSupply = token.lockedSupply();
193+
194+
// Update previous locked balances and votes for all actors
195+
for (uint256 i = 0; i < _actors.length; ++i) {
196+
address actor = _actors[i];
197+
_previousLockedBalances[actor] = token.lockedBalanceOf(actor);
198+
// Capture votes across all implementations
199+
for (uint256 j = 0; j < _validImpls.length; ++j) {
200+
_previousVotes[actor][_validImpls[j]] = token.getVotes(actor, _validImpls[j]);
201+
}
202+
}
203+
}
204+
205+
function updateStateTrackingFor(address account) public {
206+
// Update previous locked supply
207+
_previousLockedSupply = token.lockedSupply();
208+
209+
// Update previous locked balance and votes for a specific account
210+
_previousLockedBalances[account] = token.lockedBalanceOf(account);
211+
for (uint256 j = 0; j < _validImpls.length; ++j) {
212+
_previousVotes[account][_validImpls[j]] = token.getVotes(account, _validImpls[j]);
213+
}
214+
}
215+
216+
function updateStateTrackingFor(address account1, address account2) public {
217+
// Update previous locked supply
218+
_previousLockedSupply = token.lockedSupply();
219+
220+
// Update state for account1
221+
_previousLockedBalances[account1] = token.lockedBalanceOf(account1);
222+
for (uint256 j = 0; j < _validImpls.length; ++j) {
223+
_previousVotes[account1][_validImpls[j]] = token.getVotes(account1, _validImpls[j]);
224+
}
225+
226+
// Update state for account2
227+
_previousLockedBalances[account2] = token.lockedBalanceOf(account2);
228+
for (uint256 j = 0; j < _validImpls.length; ++j) {
229+
_previousVotes[account2][_validImpls[j]] = token.getVotes(account2, _validImpls[j]);
230+
}
231+
}
232+
233+
function _addActor(address a) internal {
234+
if (!_isActor[a]) {
235+
_isActor[a] = true;
236+
_actors.push(a);
237+
}
238+
}
226239
}

test/invariants/InvariantXanV1.t.sol

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.30;
33

4-
import {Test, StdInvariant} from "forge-std/Test.sol";
54
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
6-
import {XanV1} from "src/XanV1.sol";
5+
6+
import {Test, StdInvariant} from "forge-std/Test.sol";
7+
8+
import {XanV1} from "../../src/XanV1.sol";
79
import {XanV1Handler} from "./HandlerXanV1.t.sol";
810

911
contract XanV1Invariants is StdInvariant, Test {
1012
XanV1 public token;
1113
XanV1Handler public handler;
1214

13-
address internal alice = makeAddr("alice");
14-
address internal council = makeAddr("council");
15+
address internal _alice = makeAddr("alice");
16+
address internal _council = makeAddr("council");
1517

1618
function setUp() public {
1719
// Deploy implementation
1820
XanV1 impl = new XanV1();
1921
// Deploy proxy with initializer
20-
bytes memory init = abi.encodeWithSelector(XanV1.initializeV1.selector, alice, council);
22+
bytes memory init = abi.encodeWithSelector(XanV1.initializeV1.selector, _alice, _council);
2123
ERC1967Proxy proxy = new ERC1967Proxy(address(impl), init);
2224
token = XanV1(payable(address(proxy)));
2325

24-
handler = new XanV1Handler(token, alice);
26+
handler = new XanV1Handler(token, _alice);
2527

2628
// Register the handler for invariant fuzzing
2729
targetContract(address(handler));
@@ -36,7 +38,7 @@ contract XanV1Invariants is StdInvariant, Test {
3638
address[] memory actors = handler.getActors();
3739
uint256 sumLocked = 0;
3840

39-
for (uint256 i = 0; i < actors.length; i++) {
41+
for (uint256 i = 0; i < actors.length; ++i) {
4042
uint256 balance = token.balanceOf(actors[i]);
4143
uint256 locked = token.lockedBalanceOf(actors[i]);
4244
uint256 unlocked = token.unlockedBalanceOf(actors[i]);
@@ -55,7 +57,7 @@ contract XanV1Invariants is StdInvariant, Test {
5557
function invariant_lockedBalanceMonotonicity() public view {
5658
address[] memory actors = handler.getActors();
5759

58-
for (uint256 i = 0; i < actors.length; i++) {
60+
for (uint256 i = 0; i < actors.length; ++i) {
5961
address account = actors[i];
6062
uint256 currentLocked = token.lockedBalanceOf(account);
6163
uint256 previousLocked = handler.getPreviousLockedBalance(account);
@@ -70,11 +72,11 @@ contract XanV1Invariants is StdInvariant, Test {
7072
address[] memory actors = handler.getActors();
7173
address[5] memory validImpls = handler.getValidImpls();
7274

73-
for (uint256 i = 0; i < actors.length; i++) {
75+
for (uint256 i = 0; i < actors.length; ++i) {
7476
address voter = actors[i];
7577

7678
// Check all valid implementation addresses to verify monotonicity
77-
for (uint256 j = 0; j < validImpls.length; j++) {
79+
for (uint256 j = 0; j < validImpls.length; ++j) {
7880
address impl = validImpls[j];
7981
uint256 currentVotes = token.getVotes(voter, impl);
8082
uint256 previousVotes = handler.getPreviousVotes(voter, impl);
@@ -89,9 +91,9 @@ contract XanV1Invariants is StdInvariant, Test {
8991
(address vImpl, uint48 vEnd) = token.scheduledVoterBodyUpgrade();
9092
(address cImpl, uint48 cEnd) = token.scheduledCouncilUpgrade();
9193
bool voterScheduled = (vImpl != address(0) && vEnd != 0);
92-
bool councilScheduled = (cImpl != address(0) && cEnd != 0);
94+
bool _councilScheduled = (cImpl != address(0) && cEnd != 0);
9395

94-
assertTrue(!(voterScheduled && councilScheduled), "both voter and council scheduled");
96+
assertTrue(!(voterScheduled && _councilScheduled), "both voter and _council scheduled");
9597
}
9698

9799
//PS-9 (upgrade cancellation token persistence) is implicitly proved by invariant tests above

0 commit comments

Comments
 (0)