Skip to content

Commit

Permalink
maint: clean up some usage of vm.assume
Browse files Browse the repository at this point in the history
Cleans up some usage of vm.assume. Will be a series of commits
that do mostly the same thing.
  • Loading branch information
smartcontracts committed Dec 4, 2024
1 parent b002fea commit 8dbadeb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
9 changes: 6 additions & 3 deletions packages/contracts-bedrock/test/L1/ResourceMetering.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,23 @@ contract ResourceMetering_Test is Test {
function testFuzz_meter_largeBlockDiff_succeeds(uint64 _amount, uint256 _blockDiff) external {
// This test fails if the following line is commented out.
// At 12 seconds per block, this number is effectively unreachable.
vm.assume(_blockDiff < 433576281058164217753225238677900874458691);
_blockDiff = uint256(bound(_blockDiff, 0, 433576281058164217753225238677900874458690));

ResourceMetering.ResourceConfig memory rcfg = meter.resourceConfig();
uint64 target = uint64(rcfg.maxResourceLimit) / uint64(rcfg.elasticityMultiplier);
uint64 elasticityMultiplier = uint64(rcfg.elasticityMultiplier);

vm.assume(_amount < target * elasticityMultiplier);
vm.assume(target > 0);
vm.assume(elasticityMultiplier > 0);
_amount = uint64(bound(_amount, 0, target * elasticityMultiplier));

vm.roll(initialBlockNum + _blockDiff);
meter.use(_amount);
}

function testFuzz_meter_useGas_succeeds(uint64 _amount) external {
(, uint64 prevBoughtGas,) = meter.params();
vm.assume(prevBoughtGas + _amount <= meter.resourceConfig().maxResourceLimit);
_amount = uint64(bound(_amount, 0, meter.resourceConfig().maxResourceLimit - prevBoughtGas));

meter.use(_amount);

Expand Down
23 changes: 16 additions & 7 deletions packages/contracts-bedrock/test/L1/SystemConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,21 @@ contract SystemConfig_Init_CustomGasToken is SystemConfig_Init {
// don't use multicall3's address
vm.assume(_token != MULTICALL3_ADDRESS);

vm.assume(bytes(_name).length <= 32);
vm.assume(bytes(_symbol).length <= 32);
// Using vm.assume() would cause too many test rejections.
string memory name = _name;
if (bytes(_name).length > 32) {
name = _name[:32];
}

// Using vm.assume() would cause too many test rejections.
string memory symbol = _symbol;
if (bytes(_symbol).length > 32) {
symbol = _symbol[:32];
}

vm.mockCall(_token, abi.encodeCall(token.decimals, ()), abi.encode(18));
vm.mockCall(_token, abi.encodeCall(token.name, ()), abi.encode(_name));
vm.mockCall(_token, abi.encodeCall(token.symbol, ()), abi.encode(_symbol));
vm.mockCall(_token, abi.encodeCall(token.name, ()), abi.encode(name));
vm.mockCall(_token, abi.encodeCall(token.symbol, ()), abi.encode(symbol));

cleanStorageAndInit(_token);

Expand All @@ -403,8 +412,8 @@ contract SystemConfig_Init_CustomGasToken is SystemConfig_Init {
assertEq(systemConfig.gasPayingTokenSymbol(), "ETH");
} else {
assertEq(addr, _token);
assertEq(systemConfig.gasPayingTokenName(), _name);
assertEq(systemConfig.gasPayingTokenSymbol(), _symbol);
assertEq(systemConfig.gasPayingTokenName(), name);
assertEq(systemConfig.gasPayingTokenSymbol(), symbol);
}
}

Expand Down Expand Up @@ -555,7 +564,7 @@ contract SystemConfig_Setters_TestFail is SystemConfig_Init {

/// @dev Tests that `setEIP1559Params` reverts if the elasticity is zero.
function test_setEIP1559Params_zeroElasticity_reverts(uint32 _denominator) external {
vm.assume(_denominator >= 1);
_denominator = uint32(bound(_denominator, 1, type(uint32).max));
vm.prank(systemConfig.owner());
vm.expectRevert("SystemConfig: elasticity must be >= 1");
systemConfig.setEIP1559Params({ _denominator: _denominator, _elasticity: 0 });
Expand Down
21 changes: 15 additions & 6 deletions packages/contracts-bedrock/test/L1/SystemConfigInterop.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,21 @@ contract SystemConfigInterop_Test is CommonTest {
vm.assume(_token != address(0));
vm.assume(_token != Constants.ETHER);

vm.assume(bytes(_name).length <= 32);
vm.assume(bytes(_symbol).length <= 32);
// Using vm.assume() would cause too many test rejections.
string memory name = _name;
if (bytes(_name).length > 32) {
name = _name[:32];
}

// Using vm.assume() would cause too many test rejections.
string memory symbol = _symbol;
if (bytes(_symbol).length > 32) {
symbol = _symbol[:32];
}

vm.mockCall(_token, abi.encodeCall(ERC20.decimals, ()), abi.encode(18));
vm.mockCall(_token, abi.encodeCall(ERC20.name, ()), abi.encode(_name));
vm.mockCall(_token, abi.encodeCall(ERC20.symbol, ()), abi.encode(_symbol));
vm.mockCall(_token, abi.encodeCall(ERC20.name, ()), abi.encode(name));
vm.mockCall(_token, abi.encodeCall(ERC20.symbol, ()), abi.encode(symbol));

vm.expectCall(
address(optimismPortal),
Expand All @@ -67,8 +76,8 @@ contract SystemConfigInterop_Test is CommonTest {
StaticConfig.encodeSetGasPayingToken({
_token: _token,
_decimals: 18,
_name: GasPayingToken.sanitize(_name),
_symbol: GasPayingToken.sanitize(_symbol)
_name: GasPayingToken.sanitize(name),
_symbol: GasPayingToken.sanitize(symbol)
})
)
)
Expand Down
29 changes: 19 additions & 10 deletions packages/contracts-bedrock/test/L2/L1Block.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,40 @@ contract L1BlockCustomGasToken_Test is L1BlockTest {
function testFuzz_setGasPayingToken_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(_token != Constants.ETHER);
vm.assume(bytes(_name).length <= 32);
vm.assume(bytes(_symbol).length <= 32);

bytes32 name = bytes32(abi.encodePacked(_name));
bytes32 symbol = bytes32(abi.encodePacked(_symbol));
// Using vm.assume() would cause too many test rejections.
string memory name = _name;
if (bytes(_name).length > 32) {
name = _name[:32];
}
bytes32 b32name = bytes32(abi.encodePacked(name));

// Using vm.assume() would cause too many test rejections.
string memory symbol = _symbol;
if (bytes(_symbol).length > 32) {
symbol = _symbol[:32];
}
bytes32 b32symbol = bytes32(abi.encodePacked(symbol));

vm.expectEmit(address(l1Block));
emit GasPayingTokenSet({ token: _token, decimals: _decimals, name: name, symbol: symbol });
emit GasPayingTokenSet({ token: _token, decimals: _decimals, name: b32name, symbol: b32symbol });

vm.prank(depositor);
l1Block.setGasPayingToken({ _token: _token, _decimals: _decimals, _name: name, _symbol: symbol });
l1Block.setGasPayingToken({ _token: _token, _decimals: _decimals, _name: b32name, _symbol: b32symbol });

(address token, uint8 decimals) = l1Block.gasPayingToken();
assertEq(token, _token);
assertEq(decimals, _decimals);

assertEq(_name, l1Block.gasPayingTokenName());
assertEq(_symbol, l1Block.gasPayingTokenSymbol());
assertEq(name, l1Block.gasPayingTokenName());
assertEq(symbol, l1Block.gasPayingTokenSymbol());
assertTrue(l1Block.isCustomGasToken());
}

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts-bedrock/test/libraries/SafeCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ contract SafeCall_Test is Test {

/// @dev Tests that the `send` function with value succeeds.
function testFuzz_sendWithGas_succeeds(address _from, address _to, uint64 _gas, uint256 _value) external {
vm.assume(_gas != 0);
_gas = uint64(bound(_gas, 1, type(uint64).max));
sendTest({ _from: _from, _to: _to, _gas: _gas, _value: _value });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ contract DeputyGuardianModule_setRespectedGameType_Test is DeputyGuardianModule_
contract DeputyGuardianModule_setRespectedGameType_TestFail is DeputyGuardianModule_TestInit {
/// @dev Tests that `setRespectedGameType` when called by a non deputy guardian.
function testFuzz_setRespectedGameType_notDeputyGuardian_reverts(GameType _gameType) external {
vm.assume(GameType.unwrap(optimismPortal2.respectedGameType()) != GameType.unwrap(_gameType));
// Change the game type if it's the same to avoid test rejections.
if (GameType.unwrap(optimismPortal2.respectedGameType()) == GameType.unwrap(_gameType)) {
unchecked {
_gameType = GameType.wrap(GameType.unwrap(_gameType) + 1);
}
}

vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector));
deputyGuardianModule.setRespectedGameType(optimismPortal2, _gameType);
assertNotEq(GameType.unwrap(optimismPortal2.respectedGameType()), GameType.unwrap(_gameType));
Expand Down

0 comments on commit 8dbadeb

Please sign in to comment.