Commit f0461a14 authored by Michael Amadi's avatar Michael Amadi Committed by GitHub

ctb: remove unnecessary vm.assumes (#13264)

* test utils

* test utils

* rm unncesessary vm.assume

* rm unncesessary vm.assume

* rm unncesessary vm.assume

* fixes...

* rm testutils
parent f21f95e2
......@@ -36,7 +36,6 @@ contract DataAvailabilityChallengeTest is CommonTest {
// EntryPoint will revert if using amount > type(uint112).max.
vm.assume(sender != Preinstalls.EntryPoint_v060);
vm.assume(sender != address(dataAvailabilityChallenge));
vm.assume(sender.balance == 0);
vm.deal(sender, amount);
vm.prank(sender);
......@@ -59,7 +58,6 @@ contract DataAvailabilityChallengeTest is CommonTest {
vm.assume(sender != Preinstalls.EntryPoint_v060);
vm.assume(sender != address(dataAvailabilityChallenge));
vm.assume(sender != deploy.mustGetAddress("DataAvailabilityChallenge"));
vm.assume(sender.balance == 0);
vm.deal(sender, amount);
vm.prank(sender);
......@@ -86,10 +84,11 @@ contract DataAvailabilityChallengeTest is CommonTest {
vm.assume(challenger != address(0));
// Assume the block number is not close to the max uint256 value
vm.assume(
challengedBlockNumber
< type(uint256).max - dataAvailabilityChallenge.challengeWindow()
- dataAvailabilityChallenge.resolveWindow()
challengedBlockNumber = bound(
challengedBlockNumber,
0,
type(uint256).max - dataAvailabilityChallenge.challengeWindow() - dataAvailabilityChallenge.resolveWindow()
- 1
);
uint256 requiredBond = dataAvailabilityChallenge.bondSize();
......@@ -139,10 +138,11 @@ contract DataAvailabilityChallengeTest is CommonTest {
vm.assume(challenger != address(0));
// Assume the block number is not close to the max uint256 value
vm.assume(
challengedBlockNumber
< type(uint256).max - dataAvailabilityChallenge.challengeWindow()
- dataAvailabilityChallenge.resolveWindow()
challengedBlockNumber = bound(
challengedBlockNumber,
0,
type(uint256).max - dataAvailabilityChallenge.challengeWindow() - dataAvailabilityChallenge.resolveWindow()
- 1
);
uint256 requiredBond = dataAvailabilityChallenge.bondSize();
......@@ -265,10 +265,11 @@ contract DataAvailabilityChallengeTest is CommonTest {
dataAvailabilityChallenge.setResolverRefundPercentage(resolverRefundPercentage);
// Assume the block number is not close to the max uint256 value
vm.assume(
challengedBlockNumber
< type(uint256).max - dataAvailabilityChallenge.challengeWindow()
- dataAvailabilityChallenge.resolveWindow()
challengedBlockNumber = bound(
challengedBlockNumber,
0,
type(uint256).max - dataAvailabilityChallenge.challengeWindow() - dataAvailabilityChallenge.resolveWindow()
- 1
);
bytes memory challengedCommitment = computeCommitmentKeccak256(preImage);
......@@ -356,10 +357,11 @@ contract DataAvailabilityChallengeTest is CommonTest {
dataAvailabilityChallenge.setResolverRefundPercentage(resolverRefundPercentage);
// Assume the block number is not close to the max uint256 value
vm.assume(
challengedBlockNumber
< type(uint256).max - dataAvailabilityChallenge.challengeWindow()
- dataAvailabilityChallenge.resolveWindow()
challengedBlockNumber = bound(
challengedBlockNumber,
0,
type(uint256).max - dataAvailabilityChallenge.challengeWindow() - dataAvailabilityChallenge.resolveWindow()
- 1
);
bytes memory challengedCommitment = computeCommitmentKeccak256(wrongPreImage);
......@@ -458,10 +460,11 @@ contract DataAvailabilityChallengeTest is CommonTest {
function test_unlockBond_succeeds(bytes memory preImage, uint256 challengedBlockNumber) public {
// Assume the block number is not close to the max uint256 value
vm.assume(
challengedBlockNumber
< type(uint256).max - dataAvailabilityChallenge.challengeWindow()
- dataAvailabilityChallenge.resolveWindow()
challengedBlockNumber = bound(
challengedBlockNumber,
0,
type(uint256).max - dataAvailabilityChallenge.challengeWindow() - dataAvailabilityChallenge.resolveWindow()
- 1
);
bytes memory challengedCommitment = computeCommitmentKeccak256(preImage);
......
......@@ -162,14 +162,16 @@ contract OptimismPortal_Test is CommonTest {
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
internal
{
if (_isCreation) {
_to = address(0);
}
vm.assume(_data.length <= 120_000);
if (_data.length > 120_000) {
_data = _data[0:120_000];
}
IResourceMetering.ResourceConfig memory rcfg = systemConfig.resourceConfig();
_gasLimit =
uint64(bound(_gasLimit, optimismPortal.minimumGasLimit(uint64(_data.length)), rcfg.maxResourceLimit));
......@@ -207,7 +209,7 @@ contract OptimismPortal_Test is CommonTest {
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -232,7 +234,7 @@ contract OptimismPortal_Test is CommonTest {
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -476,13 +478,17 @@ contract OptimismPortal_Test is CommonTest {
/// `depositTransaction` function. This is a simple differential test.
function test_setGasPayingToken_correctEvent_succeeds(
address _token,
string memory _name,
string memory _symbol
string calldata _name,
string calldata _symbol
)
external
{
vm.assume(bytes(_name).length <= 32);
vm.assume(bytes(_symbol).length <= 32);
if (bytes(_name).length > 32) {
_name = _name[0:32];
}
if (bytes(_symbol).length > 32) {
_symbol = _symbol[0:32];
}
bytes32 name = GasPayingToken.sanitize(_name);
bytes32 symbol = GasPayingToken.sanitize(_symbol);
......@@ -1436,14 +1442,16 @@ contract OptimismPortalWithMockERC20_Test is OptimismPortal_FinalizeWithdrawal_T
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
internal
{
if (_isCreation) {
_to = address(0);
}
vm.assume(_data.length <= 120_000);
if (_data.length > 120_000) {
_data = _data[0:120_000];
}
IResourceMetering.ResourceConfig memory rcfg = systemConfig.resourceConfig();
_gasLimit =
uint64(bound(_gasLimit, optimismPortal.minimumGasLimit(uint64(_data.length)), rcfg.maxResourceLimit));
......@@ -1482,7 +1490,7 @@ contract OptimismPortalWithMockERC20_Test is OptimismPortal_FinalizeWithdrawal_T
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -1507,7 +1515,7 @@ contract OptimismPortalWithMockERC20_Test is OptimismPortal_FinalizeWithdrawal_T
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -1663,14 +1671,16 @@ contract OptimismPortalWithMockERC20_Test is OptimismPortal_FinalizeWithdrawal_T
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
internal
{
if (_isCreation) {
_to = address(0);
}
vm.assume(_data.length <= 120_000);
if (_data.length > 120_000) {
_data = _data[0:120_000];
}
IResourceMetering.ResourceConfig memory rcfg = systemConfig.resourceConfig();
_gasLimit =
uint64(bound(_gasLimit, optimismPortal.minimumGasLimit(uint64(_data.length)), rcfg.maxResourceLimit));
......@@ -1704,7 +1714,7 @@ contract OptimismPortalWithMockERC20_Test is OptimismPortal_FinalizeWithdrawal_T
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -1727,7 +1737,7 @@ contract OptimismPortalWithMockERC20_Test is OptimismPortal_FinalizeWithdrawal_T
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......
......@@ -329,13 +329,17 @@ contract OptimismPortal2_Test is CommonTest {
/// `depositTransaction` function. This is a simple differential test.
function test_setGasPayingToken_correctEvent_succeeds(
address _token,
string memory _name,
string memory _symbol
string calldata _name,
string calldata _symbol
)
external
{
vm.assume(bytes(_name).length <= 32);
vm.assume(bytes(_symbol).length <= 32);
if (bytes(_name).length > 32) {
_name = _name[0:32];
}
if (bytes(_symbol).length > 32) {
_symbol = _symbol[0:32];
}
bytes32 name = GasPayingToken.sanitize(_name);
bytes32 symbol = GasPayingToken.sanitize(_symbol);
......@@ -1711,14 +1715,16 @@ contract OptimismPortal2WithMockERC20_Test is OptimismPortal2_FinalizeWithdrawal
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
internal
{
if (_isCreation) {
_to = address(0);
}
vm.assume(_data.length <= 120_000);
if (_data.length > 120_000) {
_data = _data[0:120_000];
}
IResourceMetering.ResourceConfig memory rcfg = systemConfig.resourceConfig();
_gasLimit =
uint64(bound(_gasLimit, optimismPortal2.minimumGasLimit(uint64(_data.length)), rcfg.maxResourceLimit));
......@@ -1757,7 +1763,7 @@ contract OptimismPortal2WithMockERC20_Test is OptimismPortal2_FinalizeWithdrawal
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -1782,7 +1788,7 @@ contract OptimismPortal2WithMockERC20_Test is OptimismPortal2_FinalizeWithdrawal
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -1947,14 +1953,17 @@ contract OptimismPortal2WithMockERC20_Test is OptimismPortal2_FinalizeWithdrawal
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
internal
{
if (_isCreation) {
_to = address(0);
}
vm.assume(_data.length <= 120_000);
if (_data.length > 120_000) {
_data = _data[0:120_000];
}
IResourceMetering.ResourceConfig memory rcfg = systemConfig.resourceConfig();
_gasLimit =
uint64(bound(_gasLimit, optimismPortal2.minimumGasLimit(uint64(_data.length)), rcfg.maxResourceLimit));
......@@ -1988,7 +1997,7 @@ contract OptimismPortal2WithMockERC20_Test is OptimismPortal2_FinalizeWithdrawal
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......@@ -2011,7 +2020,7 @@ contract OptimismPortal2WithMockERC20_Test is OptimismPortal2_FinalizeWithdrawal
uint256 _value,
uint64 _gasLimit,
bool _isCreation,
bytes memory _data
bytes calldata _data
)
external
{
......
......@@ -639,8 +639,8 @@ contract SystemConfig_Setters_Test is SystemConfig_Init {
/// @dev Tests that `setEIP1559Params` updates the EIP1559 parameters successfully.
function testFuzz_setEIP1559Params_succeeds(uint32 _denominator, uint32 _elasticity) external {
vm.assume(_denominator > 1);
vm.assume(_elasticity > 1);
_denominator = uint32(bound(_denominator, 2, type(uint32).max));
_elasticity = uint32(bound(_elasticity, 2, type(uint32).max));
vm.expectEmit(address(systemConfig));
emit ConfigUpdate(
......
......@@ -298,7 +298,7 @@ contract CrossL2InboxTest is Test {
/// @dev Tests that the `executeMessage` function reverts when called with an identifier with an invalid timestamp.
function testFuzz_executeMessage_invalidTimestamp_reverts(
Identifier calldata _id,
Identifier memory _id,
address _target,
bytes calldata _message,
uint256 _value
......@@ -307,7 +307,7 @@ contract CrossL2InboxTest is Test {
setInteropStart
{
// Ensure that the id's timestamp is invalid (greater than the current block timestamp)
vm.assume(_id.timestamp > block.timestamp);
_id.timestamp = bound(_id.timestamp, block.timestamp + 1, type(uint256).max);
// Ensure is not a deposit transaction
vm.mockCall({
......@@ -488,7 +488,7 @@ contract CrossL2InboxTest is Test {
/// @dev Tests that the `validateMessage` function reverts when called with an identifier with a timestamp later
/// than current block.timestamp.
function testFuzz_validateMessage_invalidTimestamp_reverts(
Identifier calldata _id,
Identifier memory _id,
bytes32 _messageHash
)
external
......@@ -502,7 +502,7 @@ contract CrossL2InboxTest is Test {
});
// Ensure that the id's timestamp is invalid (greater than the current block timestamp)
vm.assume(_id.timestamp > block.timestamp);
_id.timestamp = bound(_id.timestamp, block.timestamp + 1, type(uint256).max);
// Expect a revert with the InvalidTimestamp selector
vm.expectRevert(InvalidTimestamp.selector);
......
......@@ -64,8 +64,6 @@ contract L1BlockInteropTest is CommonTest {
/// @dev Tests that the dependency set size is correct when adding an arbitrary number of chain IDs.
function testFuzz_dependencySetSize_succeeds(uint8 _dependencySetSize) public prankDepositor {
vm.assume(_dependencySetSize <= type(uint8).max);
uint256 uniqueCount = 0;
for (uint256 i = 0; i < _dependencySetSize; i++) {
......
......@@ -144,7 +144,7 @@ contract L2ToL2CrossDomainMessengerTest is Test {
vm.assume(_target != Predeploys.CROSS_L2_INBOX && _target != Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER);
// Ensure that _value is greater than 0
vm.assume(_value > 0);
_value = bound(_value, 1, type(uint256).max);
// Add sufficient value to the contract to send the message with
vm.deal(address(this), _value);
......
......@@ -1130,7 +1130,7 @@ contract MIPS2_Test is CommonTest {
)
public
{
vm.assume(_wakeup != sys.FUTEX_EMPTY_ADDR);
_wakeup = uint32(_bound(_wakeup, 0, sys.FUTEX_EMPTY_ADDR - 1));
threading.createThread();
threading.createThread();
......@@ -1178,8 +1178,9 @@ contract MIPS2_Test is CommonTest {
)
public
{
vm.assume(_wakeup != sys.FUTEX_EMPTY_ADDR);
vm.assume(_wakeup != _futexAddr);
// -2 incase _wakeup == _futexAddr and _wakeup needs to be incremented
_wakeup = uint32(_bound(_wakeup, 0, sys.FUTEX_EMPTY_ADDR - 2));
if (_wakeup == _futexAddr) _wakeup++;
threading.createThread();
threading.createThread();
......
......@@ -127,10 +127,19 @@ contract Bytes_slice_TestFail is Test {
/// @notice Tests that, when given an input bytes array of length `n`, the `slice` function will
/// always revert if `_start + _length > n`.
function testFuzz_slice_outOfBounds_reverts(bytes memory _input, uint256 _start, uint256 _length) public {
// We want a valid start index and a length that will not overflow.
vm.assume(_start < _input.length && _length < type(uint256).max - 31);
// We want a valid start index that will not overflow.
if (_input.length == 0) {
_start = 0;
} else {
_start = bound(_start, 0, _input.length - 1);
}
// And a length that will not overflow.
// But, we want an invalid slice length.
vm.assume(_start + _length > _input.length);
if (_start > 31) {
_length = bound(_length, (_input.length - _start) + 1, type(uint256).max - _start);
} else {
_length = bound(_length, (_input.length - _start) + 1, type(uint256).max - 31);
}
vm.expectRevert("slice_outOfBounds");
Bytes.slice(_input, _start, _length);
......@@ -140,7 +149,7 @@ contract Bytes_slice_TestFail is Test {
/// the `slice` function reverts.
function testFuzz_slice_lengthOverflows_reverts(bytes memory _input, uint256 _start, uint256 _length) public {
// Ensure that the `_length` will overflow if a number >= 31 is added to it.
vm.assume(_length > type(uint256).max - 31);
_length = uint256(bound(_length, type(uint256).max - 30, type(uint256).max));
vm.expectRevert("slice_overflow");
Bytes.slice(_input, _start, _length);
......
......@@ -57,15 +57,19 @@ contract GasPayingToken_Roundtrip_Test is Test {
function testFuzz_setGetWithSanitize_succeeds(
address _token,
uint8 _decimals,
string memory _name,
string memory _symbol
string calldata _name,
string calldata _symbol
)
external
{
vm.assume(_token != address(0));
vm.assume(bytes(_name).length <= 32);
vm.assume(bytes(_symbol).length <= 32);
vm.assume(_token != Constants.ETHER);
if (bytes(_name).length > 32) {
_name = string(bytes(_name)[0:32]);
}
if (bytes(_symbol).length > 32) {
_symbol = string(bytes(_symbol)[0:32]);
}
GasPayingToken.set(_token, _decimals, GasPayingToken.sanitize(_name), GasPayingToken.sanitize(_symbol));
......
......@@ -12,7 +12,7 @@ import { SafeCall } from "src/libraries/SafeCall.sol";
contract SafeCall_Test is Test {
/// @notice Helper function to deduplicate code. Makes all assumptions required for these tests.
function assumeNot(address _addr) internal {
vm.assume(_addr.balance == 0);
vm.deal(_addr, 0);
vm.assume(_addr != address(this));
assumeAddressIsNot(_addr, StdCheatsSafe.AddressType.ForgeAddress, StdCheatsSafe.AddressType.Precompile);
}
......
......@@ -26,7 +26,7 @@ contract TransientContextTest is Test {
/// @notice Tests that `increment()` increments the call depth.
/// @param _startingCallDepth Starting call depth.
function testFuzz_increment_succeeds(uint256 _startingCallDepth) public {
vm.assume(_startingCallDepth < type(uint256).max);
_startingCallDepth = bound(_startingCallDepth, 0, type(uint256).max - 1);
assembly ("memory-safe") {
tstore(sload(callDepthSlot.slot), _startingCallDepth)
}
......@@ -39,7 +39,7 @@ contract TransientContextTest is Test {
/// @notice Tests that `decrement()` decrements the call depth.
/// @param _startingCallDepth Starting call depth.
function testFuzz_decrement_succeeds(uint256 _startingCallDepth) public {
vm.assume(_startingCallDepth > 0);
_startingCallDepth = bound(_startingCallDepth, 1, type(uint256).max);
assembly ("memory-safe") {
tstore(sload(callDepthSlot.slot), _startingCallDepth)
}
......@@ -144,7 +144,7 @@ contract TransientReentrancyAwareTest is TransientContextTest, TransientReentran
/// @param _slot Slot to test.
/// @param _value Value to test.
function testFuzz_reentrantAware_succeeds(uint256 _callDepth, bytes32 _slot, uint256 _value) public {
vm.assume(_callDepth < type(uint256).max);
_callDepth = bound(_callDepth, 0, type(uint256).max - 1);
assembly ("memory-safe") {
tstore(sload(callDepthSlot.slot), _callDepth)
}
......@@ -172,7 +172,7 @@ contract TransientReentrancyAwareTest is TransientContextTest, TransientReentran
)
public
{
vm.assume(_callDepth < type(uint256).max - 1);
_callDepth = bound(_callDepth, 0, type(uint256).max - 2);
assembly ("memory-safe") {
tstore(sload(callDepthSlot.slot), _callDepth)
}
......
......@@ -23,10 +23,13 @@ contract CheckBalanceLowTest is Test {
/// @notice Fuzz the `check` function and assert that it always returns true
/// when the target's balance is smaller than the threshold.
function testFuzz_check_succeeds(address _target, uint256 _threshold) external view {
function testFuzz_check_succeeds(address _target, uint256 _threshold) external {
CheckBalanceLow.Params memory p = CheckBalanceLow.Params({ target: _target, threshold: _threshold });
vm.assume(_target.balance < _threshold);
if (_target.balance >= p.threshold) {
if (_threshold == 0) p.threshold = 1;
vm.deal(_target, p.threshold - 1);
}
assertEq(c.check(abi.encode(p)), true);
}
......@@ -37,7 +40,7 @@ contract CheckBalanceLowTest is Test {
CheckBalanceLow.Params memory p = CheckBalanceLow.Params({ target: _target, threshold: _threshold });
// prevent overflows
vm.assume(_threshold != type(uint256).max);
_threshold = bound(_threshold, 0, type(uint256).max - 1);
vm.deal(_target, _threshold + 1);
assertEq(c.check(abi.encode(p)), false);
......
......@@ -30,8 +30,11 @@ contract CrossDomainMessenger_BaseGas_Test is CommonTest {
/// or equal to the minimum gas limit value on the OptimismPortal.
/// This guarantees that the messengers will always pass sufficient
/// gas to the OptimismPortal.
function testFuzz_baseGas_portalMinGasLimit_succeeds(bytes memory _data, uint32 _minGasLimit) external view {
vm.assume(_data.length <= type(uint64).max);
function testFuzz_baseGas_portalMinGasLimit_succeeds(bytes calldata _data, uint32 _minGasLimit) external view {
if (_data.length > type(uint64).max) {
_data = _data[0:type(uint64).max];
}
uint64 baseGas = l1CrossDomainMessenger.baseGas(_data, _minGasLimit);
uint64 minGasLimit = optimismPortal.minimumGasLimit(uint64(_data.length));
assertTrue(baseGas >= minGasLimit);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment