vm.assume(collateralAmount != 0)
Not Working As Expected
#1699
-
I am currently in the invariant testing portion of the course "Advanced Foundry", doing the "Stablecoin" project. There while creating handlers for The code that works (this was used in the video): function redeemCollateral(uint256 _collateralSeed, uint256 _amountCollateral) public {
ERC20Mock collateral = _getCollateralFromSeed(_collateralSeed);
uint256 maxCollateralToRedeem = engine.getCollateralBalanceOfUser(msg.sender, address(collateral));
_amountCollateral = bound(_amountCollateral, 0, maxCollateralToRedeem);
if (_amountCollateral == 0) {
return;
}
vm.startPrank(msg.sender);
engine.redeemCollateral(address(collateral), _amountCollateral);
vm.stopPrank();
} The code that's giving errors: function redeemCollateral(uint256 _collateralSeed, uint256 _amountCollateral) public {
ERC20Mock collateral = _getCollateralFromSeed(_collateralSeed);
uint256 maxCollateralToRedeem = engine.getCollateralBalanceOfUser(msg.sender, address(collateral));
_amountCollateral = bound(_amountCollateral, 0, maxCollateralToRedeem);
// if (_amountCollateral == 0) {
// return;
// }
vm.assume(_amountCollateral != 0);
vm.startPrank(msg.sender);
engine.redeemCollateral(address(collateral), _amountCollateral);
vm.stopPrank();
} Error with full detail: Ran 1 test for test/fuzz/InvariantsTest.t.sol:InvariantsTest
[FAIL. Reason: FOUNDRY::ASSUME]
[Sequence]
sender=0x4E08Dc99A3Fb19E89b543F51dBbE176AfC6c52F5 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=failed() args=[]
sender=0x0000000000000000000000000000000000000376 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=depositCollateral(uint256,uint256) args=[0, 278921122950954583493403671619077362421400 [2.789e41]]
sender=0x0000000000000000000000000000000031e92b82 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=redeemCollateral(uint256,uint256) args=[3935, 1868]
invariant_protocolMustHaveMoreValueThanTotalCollateralSupply() (runs: 1, calls: 3, reverts: 1)
Logs:
Bound Result 49012822293230851315290756765
Bound Result 0
_amountCollateral: 0
Traces:
[12272059] InvariantsTest::setUp()
├─ [4989807] → new DeployDSC@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
│ └─ ← 24809 bytes of code
├─ [5807306] DeployDSC::run()
│ ├─ [3777689] → new HelperConfig@0x104fBc016F4bb334D775a19E8A6510109AC63E00
│ │ ├─ [0] VM::startBroadcast()
│ │ │ └─ ← ()
│ │ ├─ [372455] → new MockV3Aggregator@0x90193C961A926261B756D1E5bb255e67ff9498A1
│ │ │ └─ ← 1083 bytes of code
│ │ ├─ [658896] → new ERC20Mock@0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496
│ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11])
│ │ │ └─ ← 2829 bytes of code
│ │ ├─ [372455] → new MockV3Aggregator@0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3
│ │ │ └─ ← 1083 bytes of code
│ │ ├─ [658896] → new ERC20Mock@0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76
│ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11])
│ │ │ └─ ← 2829 bytes of code
│ │ ├─ [0] VM::stopBroadcast()
│ │ │ └─ ← ()
│ │ └─ ← 7121 bytes of code
│ ├─ [912] HelperConfig::activeNetworkConfig() [staticcall]
│ │ └─ ← MockV3Aggregator: [0x90193C961A926261B756D1E5bb255e67ff9498A1], MockV3Aggregator: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76]
│ ├─ [0] VM::startBroadcast(<pk>)
│ │ └─ ← ()
│ ├─ [750023] → new DecentralizedStableCoin@0x5FbDB2315678afecb367f032d93F642f64180aa3
│ │ ├─ emit OwnershipTransferred(previousOwner: 0x0000000000000000000000000000000000000000, newOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)
│ │ └─ ← 3402 bytes of code
│ ├─ [1038652] → new DSCEngine@0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
│ │ └─ ← 4510 bytes of code
│ ├─ [2446] DecentralizedStableCoin::transferOwnership(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512])
│ │ ├─ emit OwnershipTransferred(previousOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, newOwner: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512])
│ │ └─ ← ()
│ ├─ [0] VM::stopBroadcast()
│ │ └─ ← ()
│ └─ ← DecentralizedStableCoin: [0x5FbDB2315678afecb367f032d93F642f64180aa3], DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], HelperConfig: [0x104fBc016F4bb334D775a19E8A6510109AC63E00]
├─ [912] HelperConfig::activeNetworkConfig() [staticcall]
│ └─ ← MockV3Aggregator: [0x90193C961A926261B756D1E5bb255e67ff9498A1], MockV3Aggregator: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76]
├─ [1202245] → new Handler@0x2e234DAe75C793f67A35089C9d99245E1C58470b
│ ├─ [1313] DSCEngine::getCollateralTokens() [staticcall]
│ │ └─ ← [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496, 0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76]
│ └─ ← 5327 bytes of code
└─ ← ()
[6261] Handler::failed()
├─ [0] VM::load(VM: [0x7109709ECfa91a80626fF3989D68f67F5b1DD12D], 0x6661696c65640000000000000000000000000000000000000000000000000000)
│ └─ ← 0x0000000000000000000000000000000000000000000000000000000000000000
└─ ← false
[106258] Handler::depositCollateral(0, 278921122950954583493403671619077362421400 [2.789e41])
├─ [0] console::log("Bound Result", 49012822293230851315290756765 [4.901e28]) [staticcall]
│ └─ ← ()
├─ [0] VM::startPrank(0x0000000000000000000000000000000000000376)
│ └─ ← ()
├─ [29528] ERC20Mock::mint(0x0000000000000000000000000000000000000376, 49012822293230851315290756765 [4.901e28])
│ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x0000000000000000000000000000000000000376, value: 49012822293230851315290756765 [4.901e28])
│ └─ ← ()
├─ [24651] ERC20Mock::approve(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 49012822293230851315290756765 [4.901e28])
│ ├─ emit Approval(owner: 0x0000000000000000000000000000000000000376, spender: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], value: 49012822293230851315290756765 [4.901e28])
│ └─ ← true
├─ [60327] DSCEngine::depositCollateral(ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], 49012822293230851315290756765 [4.901e28])
│ ├─ emit CollateralDeposited(user: 0x0000000000000000000000000000000000000376, token: ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], amount: 49012822293230851315290756765 [4.901e28])
│ ├─ [27689] ERC20Mock::transferFrom(0x0000000000000000000000000000000000000376, DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 49012822293230851315290756765 [4.901e28])
│ │ ├─ emit Approval(owner: 0x0000000000000000000000000000000000000376, spender: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], value: 0)
│ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000376, to: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], value: 49012822293230851315290756765 [4.901e28])
│ │ └─ ← true
│ └─ ← ()
├─ [0] VM::stopPrank()
│ └─ ← ()
└─ ← ()
[17805] Handler::redeemCollateral(3935, 1868)
├─ [2738] DSCEngine::getCollateralBalanceOfUser(0x0000000000000000000000000000000031e92b82, ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76]) [staticcall]
│ └─ ← 0
├─ [0] console::log("Bound Result", 0) [staticcall]
│ └─ ← ()
├─ [0] console::log("_amountCollateral: ", 0) [staticcall]
│ └─ ← ()
├─ [0] VM::assume(false) [staticcall]
│ └─ ← FOUNDRY::ASSUME
└─ ← FOUNDRY::ASSUME
Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 18.60ms
Ran 1 test suite in 18.60ms: 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/fuzz/InvariantsTest.t.sol:InvariantsTest
[FAIL. Reason: FOUNDRY::ASSUME]
[Sequence]
sender=0x4E08Dc99A3Fb19E89b543F51dBbE176AfC6c52F5 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=failed() args=[]
sender=0x0000000000000000000000000000000000000376 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=depositCollateral(uint256,uint256) args=[0, 278921122950954583493403671619077362421400 [2.789e41]]
sender=0x0000000000000000000000000000000031e92b82 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=redeemCollateral(uint256,uint256) args=[3935, 1868]
invariant_protocolMustHaveMoreValueThanTotalCollateralSupply() (runs: 1, calls: 3, reverts: 1)
Encountered a total of 1 failing tests, 0 tests succeeded |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hello @mahadihassanriyadh, I must confess that I didn't implement the |
Beta Was this translation helpful? Give feedback.
-
So
Does that make sense? |
Beta Was this translation helpful? Give feedback.
So
vm.assume(_amountCollateral != 0);
is sort of "dumb" with how it works with fuzz runs. In the video, that was actually a bit of a mistake of mine.vm.assume();
will blindly toss the run if it sees a condition that is not satisfied, and essentially reverts. Since we have setfail_on_revert
totrue
, this means that anytimevm.assume()
hits a condition that evaluates tofalse
the run is considered failed.Does that make sense?