In the security course test directory, what happens if you dosn't specify targetContract(address(handler)); #377
-
|
In this contract: // SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import {Test} from "forge-std/Test.sol";
import {StdInvariant} from "forge-std/StdInvariant.sol";
import {HandlerStatefulFuzzCatches} from "../../../src/invariant-break/HandlerStatefulFuzzCatches.sol";
import {YieldERC20} from "../../mocks/YieldERC20.sol";
import {MockUSDC} from "../../mocks/MockUSDC.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Handler} from "./Handler.t.sol";
contract InvariantBreakHardTest is StdInvariant, Test {
HandlerStatefulFuzzCatches handlerStatefulFuzzCatches;
YieldERC20 yeildERC20;
MockUSDC mockUSDC;
IERC20[] public supportedTokens;
uint256 public startingAmount;
address owner = makeAddr("owner");
Handler handler;
function setUp() public {
vm.startPrank(owner);
// Give our owner 1M tokens each
yeildERC20 = new YieldERC20();
startingAmount = yeildERC20.INITIAL_SUPPLY();
mockUSDC = new MockUSDC();
mockUSDC.mint(owner, startingAmount);
supportedTokens.push(mockUSDC);
supportedTokens.push(yeildERC20);
handlerStatefulFuzzCatches = new HandlerStatefulFuzzCatches(supportedTokens);
vm.stopPrank();
handler = new Handler(handlerStatefulFuzzCatches, yeildERC20, mockUSDC);
bytes4[] memory selectors = new bytes4[](3);
selectors[0] = handler.depositYieldERC20.selector;
selectors[1] = handler.withdrawYieldERC20.selector;
selectors[2] = handler.withdrawMockUSDC.selector;
targetSelector(FuzzSelector({addr: address(handler), selectors: selectors}));
targetContract(address(handler));
}
// THIS however, catches our bug!!!
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);
}
}
what happens if you remove targetContract(address(handler)); |
Beta Was this translation helpful? Give feedback.
Answered by
Rahul251952
Aug 29, 2025
Replies: 2 comments 1 reply
-
|
I haven't tried that before, Why not try it and see. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
If we don't target the handler, the invariant will test the entire main file. Doing so will make it fuzz only the handler. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
BigAlQ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If we don't target the handler, the invariant will test the entire main file. Doing so will make it fuzz only the handler.