Skip to content

Commit

Permalink
Add fuzz test for rollupInitialized
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed Sep 27, 2024
1 parent 9f17bcb commit 7f4161f
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions test/foundry/ERC20RollupEventInbox.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import {ERC20PresetMinterPauser} from
"@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";

contract ERC20RollupEventInboxTest is AbsRollupEventInboxTest {
// 7 gwei basefee
uint256 public constant L2_BASEFEE = 7_000_000_000;

// 80 gwei L1 basefee
uint256 public constant L1_BASEFEE = 80_000_000_000;

function setUp() public {
rollupEventInbox =
IRollupEventInbox(TestUtil.deployProxy(address(new ERC20RollupEventInbox())));
Expand Down Expand Up @@ -107,18 +113,76 @@ contract ERC20RollupEventInboxTest is AbsRollupEventInboxTest {
rollupEventInbox.rollupInitialized(chainId, chainConfig);
}

function testFuzz_rollupInitialized(uint256 exchangeRate) public {
_setSequencerInbox(true);

uint256 chainId = 500;
string memory chainConfig = "chainConfig2";

// check if call will overflow due to too high exchange rate
bool expectedToOverflow = false;
{
unchecked {
uint256 mul = (L1_BASEFEE + L2_BASEFEE) * exchangeRate;
if (exchangeRate != 0 && ((mul / exchangeRate) != (L1_BASEFEE + L2_BASEFEE))) {
expectedToOverflow = true;
}
}
}

if (expectedToOverflow) {
vm.mockCall(
address(
ISequencerInbox(rollupEventInbox.bridge().sequencerInbox()).feeTokenPricer()
),
abi.encodeWithSelector(IFeeTokenPricer.getExchangeRate.selector),
abi.encode(exchangeRate)
);
vm.mockCall(
address(0x6c),
abi.encodeWithSignature("getL1BaseFeeEstimate()"),
abi.encode(L1_BASEFEE)
);
vm.fee(L2_BASEFEE);
vm.expectRevert(stdError.arithmeticError);
} else {
uint8 expectedInitMsgVersion = 1;
uint256 expectedCurrentDataCost = _calculateExpectedCurrentDataCost(exchangeRate, true);
bytes memory expectedInitMsg = abi.encodePacked(
chainId, expectedInitMsgVersion, expectedCurrentDataCost, chainConfig
);

// expect event
vm.expectEmit(true, true, true, true);
emit MessageDelivered(
0,
bytes32(0),
address(rollupEventInbox),
INITIALIZATION_MSG_TYPE,
address(0),
keccak256(expectedInitMsg),
uint256(0),
uint64(block.timestamp)
);

vm.expectEmit(true, true, true, true);
emit InboxMessageDelivered(0, expectedInitMsg);
}

vm.prank(rollup);
rollupEventInbox.rollupInitialized(chainId, chainConfig);
}

function _calculateExpectedCurrentDataCost(uint256 exchangeRate, bool isArbHosted)
internal
returns (uint256)
{
// 7 gwei basefee
uint256 l2Fee = 7_000_000_000;
uint256 l2Fee = L2_BASEFEE;
vm.fee(l2Fee);

uint256 l1Fee = 0;
if (isArbHosted) {
// 80 gwei L1 basefee
l1Fee = 80_000_000_000;
l1Fee = L1_BASEFEE;
vm.mockCall(
address(0x6c), abi.encodeWithSignature("getL1BaseFeeEstimate()"), abi.encode(l1Fee)
);
Expand Down

0 comments on commit 7f4161f

Please sign in to comment.