-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
invariant shrink #6683: check if test failed instead revert #7257
Merged
+192
−4
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
testdata/fuzz/invariant/common/InvariantShrinkWithAssert.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "ds-test/test.sol"; | ||
import "../../../cheats/Vm.sol"; | ||
|
||
struct FuzzSelector { | ||
address addr; | ||
bytes4[] selectors; | ||
} | ||
|
||
contract Counter { | ||
uint256 public number; | ||
|
||
function setNumber(uint256 newNumber) public { | ||
number = newNumber; | ||
} | ||
|
||
function increment() public { | ||
number++; | ||
} | ||
|
||
function decrement() public { | ||
number--; | ||
} | ||
|
||
function double() public { | ||
number *= 2; | ||
} | ||
|
||
function half() public { | ||
number /= 2; | ||
} | ||
|
||
function triple() public { | ||
number *= 3; | ||
} | ||
|
||
function third() public { | ||
number /= 3; | ||
} | ||
|
||
function quadruple() public { | ||
number *= 4; | ||
} | ||
|
||
function quarter() public { | ||
number /= 4; | ||
} | ||
} | ||
|
||
contract Handler is DSTest { | ||
Counter public counter; | ||
|
||
constructor(Counter _counter) { | ||
counter = _counter; | ||
counter.setNumber(0); | ||
} | ||
|
||
function increment() public { | ||
counter.increment(); | ||
} | ||
|
||
function setNumber(uint256 x) public { | ||
counter.setNumber(x); | ||
} | ||
} | ||
|
||
contract InvariantShrinkWithAssert is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
Counter public counter; | ||
Handler handler; | ||
|
||
function setUp() public { | ||
counter = new Counter(); | ||
handler = new Handler(counter); | ||
} | ||
|
||
function targetSelectors() public returns (FuzzSelector[] memory) { | ||
FuzzSelector[] memory targets = new FuzzSelector[](1); | ||
bytes4[] memory selectors = new bytes4[](2); | ||
selectors[0] = handler.increment.selector; | ||
selectors[1] = handler.setNumber.selector; | ||
targets[0] = FuzzSelector(address(handler), selectors); | ||
return targets; | ||
} | ||
|
||
function invariant_with_assert() public { | ||
assertTrue(counter.number() != 3); | ||
} | ||
} | ||
|
||
contract InvariantShrinkWithRequire is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
Counter public counter; | ||
Handler handler; | ||
|
||
function setUp() public { | ||
counter = new Counter(); | ||
handler = new Handler(counter); | ||
} | ||
|
||
function targetSelectors() public returns (FuzzSelector[] memory) { | ||
FuzzSelector[] memory targets = new FuzzSelector[](1); | ||
bytes4[] memory selectors = new bytes4[](2); | ||
selectors[0] = handler.increment.selector; | ||
selectors[1] = handler.setNumber.selector; | ||
targets[0] = FuzzSelector(address(handler), selectors); | ||
return targets; | ||
} | ||
|
||
function invariant_with_require() public { | ||
require(counter.number() != 3, "wrong counter"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need the clone here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the next param is
&call_result
which borrows the value so if not cloning fails withThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, the
pub fn is_raw_call_success(
fn is not very good, I see why we need the state as a separate argbut this means for this call, we can use
mut call_result.state_changeset.take().unwrap()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, this makes sense, I made following change in 9d2b073 + made test more robust as they can change in less steps