Post
Share your knowledge.
Function Call Order During Invariant Testing with Foundry
I am currently studying the Invariant part of a course and have a question regarding the execution order of functions when using invariant testing. Specifically, I am looking at the statefulFuzz_testInvariantBreakHandler
function which includes the following code snippet:
function statefulFuzz_testInvariantBreakHandler() public {
vm.startPrank(owner);
handlerStatefulFuzzCatches.withdrawToken(mockUSDC);
handlerStatefulFuzzCatches.withdrawToken(yeildERC20);
vm.stopPrank();
assert(mockUSDC.balanceOf(address(handlerStatefulFuzzCatches)) == 0);
assert(yeildERC20.balanceOf(address(handlerStatefulFuzzCatches)) == 0);
assert(mockUSDC.balanceOf(owner) == startingAmount);
assert(yeildERC20.balanceOf(owner) == startingAmount);
}
My question pertains to the order in which handlerStatefulFuzzCatches.withdrawToken(mockUSDC)
and handlerStatefulFuzzCatches.withdrawToken(yeildERC20)
functions are executed in relation to the fuzzing process:
- Are these
withdrawToken
functions called at the beginning of the test, and then the fuzzer attempts to break the invariants by calling other functions? - Or, are these specific
withdrawToken
calls supposed to be executed after the fuzzer has run through other functions?
I am seeking clarification on whether the specified withdrawToken
operations are meant to set up the state for invariant testing. Thank you for your assistance.
- Solidity
- Smart Contract
Answers
1Yes, you are correct. I overlooked the -vvvvv
option in Foundry. The withdrawToken
functions are invoked at the commencement of the test, following which the fuzzer endeavors to disrupt the invariants by invoking other specified functions in the handler. The sequence of events is as follows:
- The
statefulFuzz_testInvariantBreakHandler
function is executed, encompassing all its operations. - A random function from the Handler contract is invoked.
- The state is maintained.
- Repeat step 1 until a disruption occurs.
Essentially, in the initial step, all deposits made by the Handler are withdrawn. This process aims to test the robustness of the system by systematically breaking its functionality.
Do you know the answer?
Please log in and share it.
Cyfrin Updraft is an education platform specializing on teaching the next generation of smart contract developers