Context
After having written the Solidity fuzz test below, it says the vm.assume(
throws too many reverts. The easy solution would be to increase the number of permitted reverts, however, I would like to limit the random parameter values to specific ranges to remove the vm.assume(
statements instead. That would lower the number of thrown reverts.
Code
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25 <0.9.0;
import "forge-std/src/Vm.sol" as vm;
import { PRBTest } from "@prb/test/src/PRBTest.sol";
import { StdCheats } from "forge-std/src/StdCheats.sol";
import { DecentralisedInvestmentManager } from "../../../../src/DecentralisedInvestmentManager.sol";
import { TestHelper } from "../../../TestHelper.sol";
import { InitialiseDim } from "test/InitialiseDim.sol";
import { console2 } from "forge-std/src/console2.sol";
interface IFuzzTriggerReturnAll {
function setUp() external;
function testFuzzAfterRaisePeriodReturnSingleInvestment(
uint256 projectLeadFracNumerator,
uint256 projectLeadFracDenominator,
uint256 firstInvestmentAmount,
uint256 investmentTarget,
uint256 firstCeiling,
uint256 secondCeiling,
uint256 thirdCeiling,
uint32 raisePeriod,
uint32 additionalWaitPeriod,
uint8 firstMultiple,
uint8 secondMultiple,
uint8 thirdMultiple
) external;
}
/**
Tests whether the dim.triggerReturnAll() function ensures the investments are:
- returned if the investment target is not reached, after the raisePeriod has passed.
- not returned if the investment target is reached, after the raisePeriod has passed.
TODO: test whether the investments are:
- not returned if the investment target is not reached, before the raisePeriod has passed.
- not returned if the investment target is reached, before the raisePeriod has passed.
*/
contract FuzzTriggerReturnAll is PRBTest, StdCheats, IFuzzTriggerReturnAll {
address internal _projectLead;
TestHelper private _testHelper;
function setUp() public virtual override {
_testHelper = new TestHelper();
}
function _initialiseRandomDim(
uint256 projectLeadFracNumerator,
uint256 projectLeadFracDenominator,
uint256 investmentTarget,
uint256 firstCeiling,
uint256 secondCeiling,
uint256 thirdCeiling,
uint32 raisePeriod,
uint8 firstMultiple,
uint8 secondMultiple,
uint8 thirdMultiple
) internal virtual returns (DecentralisedInvestmentManager dim) {
// Instantiate the attribute for the contract-under-test.
_projectLead = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
uint256[] memory ceilings = new uint256[](3);
ceilings[0] = firstCeiling;
ceilings[1] = secondCeiling;
ceilings[2] = thirdCeiling;
uint8[] memory multiples = new uint8[](3);
multiples[0] = firstMultiple;
multiples[1] = secondMultiple;
multiples[2] = thirdMultiple;
InitialiseDim initDim = new InitialiseDim({
ceilings: ceilings,
multiples: multiples,
investmentTarget: investmentTarget,
projectLeadFracNumerator: projectLeadFracNumerator,
projectLeadFracDenominator: projectLeadFracDenominator,
projectLead: _projectLead,
raisePeriod: raisePeriod
});
dim = initDim.getDim();
return dim;
}
function _performInvestmentInRandomDim(
DecentralisedInvestmentManager dim,
uint256 someInvestmentAmount,
address payable someInvestorWallet
) internal virtual {
deal(someInvestorWallet, someInvestmentAmount);
// Set the msg.sender address to that of the _firstInvestorWallet for the next call.
vm.prank(address(someInvestorWallet));
// Send investment directly from the investor wallet into the receiveInvestment function.
dim.receiveInvestment{ value: someInvestmentAmount }();
// assertEq(dim.getTierInvestmentLength(), 1, "Error, the _tierInvestments.length was not as expected.");
}
/**
@dev The investor has invested 0.5 eth, and the investment target is 0.6 eth after 12 weeks.
So the investment target is not reached, so all the funds should be returned.
*/
function testFuzzAfterRaisePeriodReturnSingleInvestment(
uint256 projectLeadFracNumerator,
uint256 projectLeadFracDenominator,
uint256 investmentTarget,
uint256 firstInvestmentAmount,
uint256 firstCeiling,
uint256 secondCeiling,
uint256 thirdCeiling,
uint32 additionalWaitPeriod,
uint32 raisePeriod,
uint8 firstMultiple,
uint8 secondMultiple,
uint8 thirdMultiple
) public virtual override {
// Assume requirements on contract initialisation by project lead are valid.
vm.assume(projectLeadFracDenominator > 0);
vm.assume(firstCeiling > 0);
vm.assume(additionalWaitPeriod > 0);
vm.assume(raisePeriod > 0);
vm.assume(investmentTarget > 0);
vm.assume(firstCeiling < secondCeiling);
vm.assume(secondCeiling < thirdCeiling);
vm.assume(investmentTarget <= thirdCeiling);
vm.assume(projectLeadFracNumerator <= projectLeadFracDenominator);
vm.assume(firstMultiple > 1);
vm.assume(secondMultiple > 1);
vm.assume(thirdMultiple > 1);
// Assume an investor tries to invest at least 1 wei.
vm.assume(firstInvestmentAmount > 0);
// Store multiples in an array to assert they do not lead to an overflow when computing the investor return.
uint256[] memory multiples = new uint256[](3);
multiples[0] = firstMultiple;
multiples[1] = secondMultiple;
multiples[2] = thirdMultiple;
vm.assume(!_testHelper.sumOfNrsThrowsOverFlow({ numbers: multiples }));
vm.assume(
!_testHelper.yieldsOverflowMultiply({
a: firstMultiple + secondMultiple + thirdMultiple,
b: firstInvestmentAmount
})
);
// Initialise the contract that is being fuzz tested.
DecentralisedInvestmentManager dim = _initialiseRandomDim({
projectLeadFracNumerator: projectLeadFracNumerator,
projectLeadFracDenominator: projectLeadFracDenominator,
raisePeriod: raisePeriod,
investmentTarget: investmentTarget,
firstCeiling: firstCeiling,
secondCeiling: secondCeiling,
thirdCeiling: thirdCeiling,
firstMultiple: firstMultiple,
secondMultiple: secondMultiple,
thirdMultiple: thirdMultiple
});
// Generate a non-random investor wallet address and make an investment.
address payable firstInvestorWallet = payable(address(uint160(uint256(keccak256(bytes("1"))))));
_performInvestmentInRandomDim({
dim: dim,
someInvestmentAmount: firstInvestmentAmount,
someInvestorWallet: firstInvestorWallet
});
// Perform test logic.
if (firstInvestmentAmount >= investmentTarget) {
vm.prank(_projectLead);
// solhint-disable-next-line not-rely-on-time
vm.warp(block.timestamp + raisePeriod + additionalWaitPeriod);
vm.expectRevert(bytes("Investment target reached!"));
dim.triggerReturnAll();
} else {
vm.prank(_projectLead);
// solhint-disable-next-line not-rely-on-time
vm.warp(block.timestamp + raisePeriod + additionalWaitPeriod);
dim.triggerReturnAll();
assertEq(address(dim).balance, 0 ether, "The dim did not contain 0 ether after returning all investments.");
}
}
}
Output
When I run that test above with:
clear && forge test -vvvvvv --match-test testFuzzAfterRaisePeriodReturnSingleInvestment --via-ir
It gives output:
Ran 1 test for test/functional/DecentralisedInvestmentManager/triggerReturnAll/FuzzTriggerReturnAll.t.sol:FuzzTriggerReturnAll
[FAIL. Reason: The `vm.assume` cheatcode rejected too many inputs (65536 allowed)] testFuzzAfterRaisePeriodReturnSingleInvestment(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint32,uint32,uint8,uint8,uint8) (runs: 0, μ: 0, ~: 0)
Traces:
[334958] FuzzTriggerReturnAll::setUp()
├─ [280325] → new TestHelper@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
│ └─ ← [Return] 1400 bytes of code
└─ ← [Return]
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 1.02s (1.02s CPU time)
Ran 1 test suite in 1.57s (1.02s CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/functional/DecentralisedInvestmentManager/triggerReturnAll/FuzzTriggerReturnAll.t.sol:FuzzTriggerReturnAll
[FAIL. Reason: The `vm.assume` cheatcode rejected too many inputs (65536 allowed)] testFuzzAfterRaisePeriodReturnSingleInvestment(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint32,uint32,uint8,uint8,uint8) (runs: 0, μ: 0, ~: 0)
Encountered a total of 1 failing tests, 0 tests succeeded
Documentation
This documentation describes one can use the fixture to set the random parameters to a hardcoded random range, like:
uint32[] public fixtureAmount = [1, 5, 555];
However, I do not want to hardcode the random values, as I may not be as random as I expect, and I expect the randomness from Forge to be perhaps a bit better tailored to edge cases than pure randomness.
So instead I would like to set a fixture like:
uint256 fixtureFirstCeiling > 0
However, that is invalid Solidity syntax.
Note
I initially had a nested for loop of 15 steps deep, however, that is not desirable from a code-readability perspective.
Question
How can I constrain the range of random parameters (on one side of the range) in a Forge fuzz test?