Skip to content

Commit

Permalink
fix: feedback from gas
Browse files Browse the repository at this point in the history
  • Loading branch information
0xteddybear committed Oct 18, 2024
1 parent f92068b commit 0568531
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 43 deletions.
4 changes: 2 additions & 2 deletions medusa.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"html",
"lcov"
],
"targetContracts": ["InvariantGreeter"],
"targetContracts": ["FuzzTest"],
"predeployedContracts": {},
"targetContractsBalances": [],
"constructorArgs": {},
Expand Down Expand Up @@ -75,7 +75,7 @@
"compilation": {
"platform": "crytic-compile",
"platformConfig": {
"target": "test/invariants/fuzz/Greeter.t.sol",
"target": "test/invariants/fuzz/FuzzTest.t.sol",
"solcVersion": "",
"exportDirectory": "",
"args": []
Expand Down
2 changes: 1 addition & 1 deletion test/invariants/PROPERTIES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
| id | Properties | Type |
| Id | Properties | Type |
| --- | --------------------------------------------------- | ------------ |
| 1 | Greeting should never be empty | Valid state |
| 2 | Only the owner can set the greeting | State transition |
8 changes: 8 additions & 0 deletions test/invariants/fuzz/FuzzTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {GreeterGuidedHandlers} from './handlers/guided/Greeter.t.sol';
import {GreeterUnguidedHandlers} from './handlers/unguided/Greeter.t.sol';
import {GreeterProperties} from './properties/Greeter.t.sol';

contract FuzzTest is GreeterGuidedHandlers, GreeterUnguidedHandlers, GreeterProperties {}
40 changes: 0 additions & 40 deletions test/invariants/fuzz/Greeter.t.sol

This file was deleted.

15 changes: 15 additions & 0 deletions test/invariants/fuzz/handlers/guided/Greeter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {GreeterSetup} from '../../setup/Greeter.t.sol';

contract GreeterGuidedHandlers is GreeterSetup {
function handler_setGreeting(string memory _newGreeting) external {
// no need to prank since this contract deployed the greeter and is therefore its owner
try _targetContract.setGreeting(_newGreeting) {
assert(keccak256(bytes(_targetContract.greeting())) == keccak256(bytes(_newGreeting)));
} catch {
assert(keccak256(bytes(_newGreeting)) == keccak256(''));
}
}
}
18 changes: 18 additions & 0 deletions test/invariants/fuzz/handlers/unguided/Greeter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {GreeterSetup} from '../../setup/Greeter.t.sol';

contract GreeterUnguidedHandlers is GreeterSetup {
/// @custom:property-id 2
/// @custom:property Only the owner can set the greeting
function handler_setGreeting(address _caller, string memory _newGreeting) external {
vm.prank(_caller);
try _targetContract.setGreeting(_newGreeting) {
assert(keccak256(bytes(_targetContract.greeting())) == keccak256(bytes(_newGreeting)));
assert(_caller == _targetContract.OWNER());
} catch {
assert(_caller != _targetContract.OWNER() || keccak256(bytes(_newGreeting)) == keccak256(''));
}
}
}
12 changes: 12 additions & 0 deletions test/invariants/fuzz/properties/Greeter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {GreeterSetup} from '../setup/Greeter.t.sol';

contract GreeterProperties is GreeterSetup {
/// @custom:property-id 1
/// @custom:property Greeting should never be empty
function property_greetingIsNeverEmpty() external view {
assert(keccak256(bytes(_targetContract.greeting())) != keccak256(''));
}
}
13 changes: 13 additions & 0 deletions test/invariants/fuzz/setup/Greeter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {Greeter, IERC20} from 'contracts/Greeter.sol';
import {CommonBase} from 'forge-std/Base.sol';

contract GreeterSetup is CommonBase {
Greeter internal _targetContract;

constructor() {
_targetContract = new Greeter('a', IERC20(address(1)));
}
}

0 comments on commit 0568531

Please sign in to comment.