Skip to content

Commit

Permalink
contracts-bedrock: create tests for OptimismPortalInterop
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfuturistic committed May 15, 2024
1 parent ef40d4e commit 6839e64
Showing 1 changed file with 72 additions and 41 deletions.
113 changes: 72 additions & 41 deletions packages/contracts-bedrock/test/L1/OptimismPortalInterop.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,93 @@
pragma solidity 0.8.15;

// Testing utilities
import { stdError } from "forge-std/Test.sol";
import { VmSafe } from "forge-std/Vm.sol";

import { CommonTest } from "test/setup/CommonTest.sol";
import { NextImpl } from "test/mocks/NextImpl.sol";
import { EIP1967Helper } from "test/mocks/EIP1967Helper.sol";

// Libraries
import { Types } from "src/libraries/Types.sol";
import { Hashing } from "src/libraries/Hashing.sol";
import { Constants } from "src/libraries/Constants.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";

// Target contract dependencies
import { Proxy } from "src/universal/Proxy.sol";
import { ResourceMetering } from "src/L1/ResourceMetering.sol";
import { AddressAliasHelper } from "src/vendor/AddressAliasHelper.sol";
import { L2OutputOracle } from "src/L1/L2OutputOracle.sol";
import { SystemConfig } from "src/L1/SystemConfig.sol";
import { SuperchainConfig } from "src/L1/SuperchainConfig.sol";
import { L1Block } from "src/L2/L1Block.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { OptimismPortal } from "src/L1/OptimismPortal.sol";
import { GasPayingToken } from "src/libraries/GasPayingToken.sol";
import { MockERC20 } from "solmate/test/utils/mocks/MockERC20.sol";
import { AddressAliasHelper } from "src/vendor/AddressAliasHelper.sol";
import "src/libraries/PortalErrors.sol";
import { OptimismPortal } from "src/L1/OptimismPortal.sol";
import { OptimismPortalInterop } from "src/L1/OptimismPortalInterop.sol";
import { L1BlockInterop, ConfigType } from "src/L2/L1BlockInterop.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { Constants } from "src/libraries/Constants.sol";

contract OptimismPortal_Test is CommonTest {
address depositor;
contract OptimismPortalInterop_Test is CommonTest {
OptimismPortalInterop optimismPortalInterop;

/// @notice Marked virtual to be overridden in
/// test/kontrol/deployment/DeploymentSummary.t.sol
function setUp() public virtual override {
super.setUp();
depositor = makeAddr("depositor");
optimismPortalInterop = new OptimismPortalInterop();
}

/// @dev Tests that the config for the gas paying token can be set.
function testFuzz_setConfig_gasPayingToken_succeeds(bytes calldata _value) public {
vm.expectEmit(address(optimismPortalInterop));
emitTransactionDeposited({
_from: Constants.DEPOSITOR_ACCOUNT,
_to: Predeploys.L1_BLOCK_ATTRIBUTES,
_value: 0,
_mint: 0,
_gasLimit: 200_000,
_isCreation: false,
_data: abi.encodeCall(L1BlockInterop.setConfig, (ConfigType.GAS_PAYING_TOKEN, _value))
});

vm.prank(address(optimismPortalInterop.systemConfig()));
optimismPortalInterop.setConfig(ConfigType.GAS_PAYING_TOKEN, _value);
}

/// @dev Tests that setting the gas paying token config as not the system config reverts.
function testFuzz_setConfig_gasPayingToken_notSystemConfig_reverts(bytes calldata _value) public {
vm.expectRevert(Unauthorized.selector);
optimismPortalInterop.setConfig(ConfigType.GAS_PAYING_TOKEN, _value);
}

/// @dev Tests that the config for adding a dependency can be set.
function testFuzz_setConfig_addDependency_succeeds(bytes calldata _value) public {
vm.expectEmit(address(optimismPortalInterop));
emitTransactionDeposited({
_from: Constants.DEPOSITOR_ACCOUNT,
_to: Predeploys.L1_BLOCK_ATTRIBUTES,
_value: 0,
_mint: 0,
_gasLimit: 200_000,
_isCreation: false,
_data: abi.encodeCall(L1BlockInterop.setConfig, (ConfigType.ADD_DEPENDENCY, _value))
});

vm.prank(address(optimismPortalInterop.systemConfig()));
optimismPortalInterop.setConfig(ConfigType.ADD_DEPENDENCY, _value);
}

/// @dev Tests that setting the add dependency config as not the system config reverts.
function testFuzz_setConfig_addDependency_notSystemConfig_reverts(bytes calldata _value) public {
vm.expectRevert(Unauthorized.selector);
optimismPortalInterop.setConfig(ConfigType.ADD_DEPENDENCY, _value);
}

/// @dev Tests that the config for removing a dependency can be set.
function testFuzz_setConfig_removeDependency_succeeds(bytes calldata _value) public {
vm.expectEmit(address(optimismPortalInterop));
emitTransactionDeposited({
_from: Constants.DEPOSITOR_ACCOUNT,
_to: Predeploys.L1_BLOCK_ATTRIBUTES,
_value: 0,
_mint: 0,
_gasLimit: 200_000,
_isCreation: false,
_data: abi.encodeCall(L1BlockInterop.setConfig, (ConfigType.REMOVE_DEPENDENCY, _value))
});

vm.prank(address(optimismPortalInterop.systemConfig()));
optimismPortalInterop.setConfig(ConfigType.REMOVE_DEPENDENCY, _value);
}

function testFuzz_setConfig(ConfigType _type, bytes calldata _value) public {
vm.prank(depositor);

// Emit the special deposit transaction directly that sets the config in the L1Block predeploy contract.
interop.emit TransactionDeposited(
Constants.DEPOSITOR_ACCOUNT,
Predeploys.L1_BLOCK_ATTRIBUTES,
OptimismPortal.DEPOSIT_VERSION,
abi.encodePacked(
uint256(0), // mint
uint256(0), // value
uint64(Constants.SYSTEM_DEPOSIT_GAS_LIMIT), // gasLimit
false, // isCreation,
abi.encodeCall(L1BlockInterop.setConfig, (_type, _value))
)
);
/// @dev Tests that setting the remove dependency config as not the system config reverts.
function testFuzz_setConfig_removeDependency_notSystemConfig_reverts(bytes calldata _value) public {
vm.expectRevert(Unauthorized.selector);
optimismPortalInterop.setConfig(ConfigType.REMOVE_DEPENDENCY, _value);
}
}

0 comments on commit 6839e64

Please sign in to comment.