Skip to content
Verified contractPartialMIT

OptimismPortal2

Address0x157fe938848ba6f19f0998dfd6aa23d3701baa39
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.15

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.15+commit.e14f2714
Language
solidity
Optimization
Yes, 5,000 runs
EVM version
london
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,352,298
Creation transaction
0x18150faffe…b1afde90a2

50 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/L1/OptimismPortal2.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

// Contracts
import { ProxyAdminOwnedBase } from "src/universal/ProxyAdminOwnedBase.sol";
import { Initializable } from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import { ResourceMetering } from "src/L1/ResourceMetering.sol";
import { ReinitializableBase } from "src/universal/ReinitializableBase.sol";

// Libraries
import { EOA } from "src/libraries/EOA.sol";
import { SafeCall } from "src/libraries/SafeCall.sol";
import { Constants } from "src/libraries/Constants.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { Types } from "src/libraries/Types.sol";
import { Hashing } from "src/libraries/Hashing.sol";
import { SecureMerkleTrie } from "src/libraries/trie/SecureMerkleTrie.sol";
import { AddressAliasHelper } from "src/vendor/AddressAliasHelper.sol";
import { Claim, GameStatus, GameType, GameTypes } from "src/dispute/lib/Types.sol";
import { Features } from "src/libraries/Features.sol";
import { PortalAllowlist } from "src/libraries/PortalAllowlist.sol";

// Interfaces
import { ISemver } from "interfaces/universal/ISemver.sol";
import { ISystemConfig } from "interfaces/L1/ISystemConfig.sol";
import { IResourceMetering } from "interfaces/L1/IResourceMetering.sol";
import { IDisputeGameFactory } from "interfaces/dispute/IDisputeGameFactory.sol";
import { IDisputeGame } from "interfaces/dispute/IDisputeGame.sol";
import { IAnchorStateRegistry } from "interfaces/dispute/IAnchorStateRegistry.sol";
import { IETHLockbox } from "interfaces/L1/IETHLockbox.sol";
import { IOptimismPortal2 } from "interfaces/L1/IOptimismPortal2.sol";
import { ISuperchainConfig } from "interfaces/L1/ISuperchainConfig.sol";

/// @custom:proxied true
/// @title OptimismPortal2
/// @notice The OptimismPortal is a low-level contract responsible for passing messages between L1
///         and L2. Messages sent directly to the OptimismPortal have no form of replayability.
///         Users are encouraged to use the L1CrossDomainMessenger for a higher-level interface.
contract OptimismPortal2 is Initializable, ResourceMetering, ReinitializableBase, ProxyAdminOwnedBase, ISemver {
    /// @notice Represents a proven withdrawal.
    /// @custom:field disputeGameProxy Game that the withdrawal was proven against.
    /// @custom:field timestamp        Timestamp at which the withdrawal was proven.
    struct ProvenWithdrawal {
        IDisputeGame disputeGameProxy;
        uint64 timestamp;
    }

    /// @notice The delay between when a withdrawal is proven and when it may be finalized.
    uint256 internal immutable PROOF_MATURITY_DELAY_SECONDS;

    /// @notice Version of the deposit event.
    uint256 internal constant DEPOSIT_VERSION = 0;

    /// @notice The L2 gas limit set when eth is deposited using the receive() function.
    uint64 internal constant RECEIVE_DEFAULT_GAS_LIMIT = 100_000;

    /// @notice Address of the L2 account which initiated a withdrawal in this transaction.
    ///         If the value of this variable is the default L2 sender address, then we are NOT
    ///         inside of a call to finalizeWithdrawalTransaction.
    address public l2Sender;

    /// @notice A list of withdrawal hashes which have been successfully finalized.
    mapping(bytes32 => bool) public finalizedWithdrawals;

    /// @custom:legacy
    /// @custom:spacer provenWithdrawals
    /// @notice Spacer taking up the legacy `provenWithdrawals` mapping slot.
    bytes32 private spacer_52_0_32;

    /// @custom:legacy
    /// @custom:spacer paused
    /// @notice Spacer for backwards compatibility.
    bool private spacer_53_0_1;

    /// @custom:legacy
    /// @custom:spacer superchainConfig
    /// @notice Spacer for backwards compatibility.
    address private spacer_53_1_20;

    /// @custom:legacy
    /// @custom:spacer l2Oracle
    /// @notice Spacer taking up the legacy `l2Oracle` address slot.
    address private spacer_54_0_20;

    /// @notice Address of the SystemConfig contract.
    /// @custom:network-specific
    ISystemConfig public systemConfig;

    /// @custom:network-specific
    /// @custom:legacy
    /// @custom:spacer disputeGameFactory
    /// @notice Spacer taking up the legacy `disputeGameFactory` address slot.
    address private spacer_56_0_20;

    /// @notice A mapping of withdrawal hashes to proof submitters to ProvenWithdrawal data.
    mapping(bytes32 => mapping(address => ProvenWithdrawal)) public provenWithdrawals;

    /// @custom:legacy
    /// @custom:spacer disputeGameBlacklist
    bytes32 private spacer_58_0_32;

    /// @custom:legacy
    /// @custom:spacer respectedGameType
    GameType private spacer_59_0_4;

    /// @custom:legacy
    /// @custom:spacer respectedGameTypeUpdatedAt
    uint64 private spacer_59_4_8;

    /// @notice Mapping of withdrawal hashes to addresses that have submitted a proof for the
    ///         withdrawal. Original OptimismPortal contract only allowed one proof to be submitted
    ///         for any given withdrawal hash. Fault Proofs version of this contract must allow
    ///         multiple proofs for the same withdrawal hash to prevent a malicious user from
    ///         blocking other withdrawals by proving them against invalid proposals. Submitters
    ///         are tracked in an array to simplify the off-chain process of determining which
    ///         proof submission should be used when finalizing a withdrawal.
    mapping(bytes32 => address[]) public proofSubmitters;

    /// @custom:legacy
    /// @custom:spacer _balance
    uint256 private spacer_61_0_32;

    /// @notice Address of the AnchorStateRegistry contract.
    IAnchorStateRegistry public anchorStateRegistry;

    /// @notice Address of the ETHLockbox contract. Set by `initialize` when given a nonzero
    ///         lockbox and by `migrateToSharedDisputeGame`. Must be nonzero while the ETH_LOCKBOX
    ///         feature is enabled.
    IETHLockbox public ethLockbox;

    /// @custom:legacy
    /// @custom:spacer superRootsActive
    bool private spacer_63_20_1;

    /// @notice Emitted when the Portal is migrated.
    /// @param oldLockbox The lockbox before the migration
    /// @param newLockbox The shared lockbox
    /// @param oldAnchorStateRegistry The anchorStateRegistry used before the migration
    /// @param newAnchorStateRegistry The anchorStateRegistry used after the migration
    event PortalMigrated(
        IETHLockbox oldLockbox,
        IETHLockbox newLockbox,
        IAnchorStateRegistry oldAnchorStateRegistry,
        IAnchorStateRegistry newAnchorStateRegistry
    );

    /// @notice Migrates the total ETH balance to the ETHLockbox.
    event ETHMigrated(address indexed lockbox, uint256 balance);

    /// @notice Emitted when a transaction is deposited from L1 to L2. The parameters of this event
    ///         are read by the rollup node and used to derive deposit transactions on L2.
    /// @param from       Address that triggered the deposit transaction.
    /// @param to         Address that the deposit transaction is directed to.
    /// @param version    Version of this deposit transaction event.
    /// @param opaqueData ABI encoded deposit data to be parsed off-chain.
    event TransactionDeposited(address indexed from, address indexed to, uint256 indexed version, bytes opaqueData);

    /// @notice Emitted when a withdrawal transaction is proven.
    /// @param withdrawalHash Hash of the withdrawal transaction.
    /// @param from           Address that triggered the withdrawal transaction.
    /// @param to             Address that the withdrawal transaction is directed to.
    event WithdrawalProven(bytes32 indexed withdrawalHash, address indexed from, address indexed to);

    /// @notice Emitted when a withdrawal transaction is proven. Exists as a separate event to
    ///         allow for backwards compatibility for tooling that observes the WithdrawalProven
    ///         event.
    /// @param withdrawalHash Hash of the withdrawal transaction.
    /// @param proofSubmitter Address of the proof submitter.
    event WithdrawalProvenExtension1(bytes32 indexed withdrawalHash, address indexed proofSubmitter);

    /// @notice Emitted when a withdrawal transaction is finalized.
    /// @param withdrawalHash Hash of the withdrawal transaction.
    /// @param success        Whether the withdrawal transaction was successful.
    event WithdrawalFinalized(bytes32 indexed withdrawalHash, bool success);

    /// @notice Emitted when a proof for a withdrawal transaction is deleted.
    /// @param withdrawalHash Hash of the withdrawal transaction.
    /// @param proofSubmitter Address of the proof submitter.
    event WithdrawalProofDeleted(bytes32 indexed withdrawalHash, address indexed proofSubmitter);

    /// @notice Thrown when a withdrawal has already been finalized.
    error OptimismPortal_AlreadyFinalized();

    /// @notice Thrown when the target of a withdrawal is unsafe.
    error OptimismPortal_BadTarget();

    /// @notice Thrown when the calldata for a deposit is too large.
    error OptimismPortal_CalldataTooLarge();

    /// @notice Thrown when the portal is paused.
    error OptimismPortal_CallPaused();

    /// @notice Thrown when a CGT withdrawal is not allowed.
    error OptimismPortal_NotAllowedOnCGTMode();

    /// @notice Thrown when a gas estimation transaction is being executed.
    error OptimismPortal_GasEstimation();

    /// @notice Thrown when the gas limit for a deposit is too low.
    error OptimismPortal_GasLimitTooLow();

    /// @notice Thrown when the target of a withdrawal is not a proper dispute game.
    error OptimismPortal_ImproperDisputeGame();

    /// @notice Thrown when a withdrawal has not been proven against a valid dispute game.
    error OptimismPortal_InvalidDisputeGame();

    /// @notice Thrown when Interop is set without the lockbox feature flag
    error OptimismPortal_InvalidInteropState();

    /// @notice Thrown when a withdrawal has not been proven against a valid merkle proof.
    error OptimismPortal_InvalidMerkleProof();

    /// @notice Thrown when a withdrawal has not been proven against a valid output root proof.
    error OptimismPortal_InvalidOutputRootProof();

    /// @notice Thrown when a withdrawal's timestamp is not greater than the dispute game's creation timestamp.
    error OptimismPortal_InvalidProofTimestamp();

    /// @notice Thrown when the root claim of a dispute game is invalid.
    error OptimismPortal_InvalidRootClaim();

    /// @notice Thrown when a withdrawal from the L2CrossDomainMessenger fails in the
    ///         L1CrossDomainMessenger.
    error OptimismPortal_L1CDMCallFailed();

    /// @notice Thrown when migrating to the registry that was previously
    /// set on the OptimismPortal prior to the migration
    error OptimismPortal_MigratingToSameRegistry();

    /// @notice Thrown when a withdrawal is being finalized by a reentrant call.
    error OptimismPortal_NoReentrancy();

    /// @notice Thrown when calling a function that is only available when INTEROP
    /// is enabled
    error OptimismPortal_NotUsingInterop();

    /// @notice Thrown when calling a function that requires an active ETHLockbox.
    error OptimismPortal_NotUsingLockbox();

    /// @notice Thrown when a withdrawal has not been proven for long enough.
    error OptimismPortal_ProofNotOldEnough();

    /// @notice Thrown when a withdrawal has not been proven.
    error OptimismPortal_Unproven();

    /// @notice Thrown when ETHLockbox is set/unset incorrectly depending on the feature flag.
    error OptimismPortal_InvalidLockboxState();

    /// @notice Thrown when migrateToSharedDisputeGame is called with a zero address.
    error OptimismPortal_ZeroAddress();

    /// @notice Thrown when the new lockbox has not authorized this portal.
    error OptimismPortal_LockboxNotAuthorizedForPortal();

    /// @notice Thrown when a dispute game has not been permanently invalidated.
    error OptimismPortal_DisputeGameNotInvalidated();

    /// @notice Thrown when a deposit is made by an address not in the depositor allowlist.
    error OptimismPortal_DepositorNotAllowed();

    /// @notice Thrown when a withdrawal is proven or finalized by an address not in the allowlist.
    error OptimismPortal_WithdrawerNotAllowed();

    /// @notice Semantic version.
    /// @custom:semver 6.1.0+allowlist
    function version() public pure virtual returns (string memory) {
        return "6.1.0+allowlist";
    }

    /// @param _proofMaturityDelaySeconds The proof maturity delay in seconds.
    constructor(uint256 _proofMaturityDelaySeconds) ReinitializableBase(3) {
        PROOF_MATURITY_DELAY_SECONDS = _proofMaturityDelaySeconds;
        _disableInitializers();
    }

    /// @notice Initializer.
    /// @param _systemConfig Address of the SystemConfig.
    /// @param _anchorStateRegistry Address of the AnchorStateRegistry.
    function initialize(
        ISystemConfig _systemConfig,
        IAnchorStateRegistry _anchorStateRegistry,
        IETHLockbox _ethLockbox
    )
        external
        reinitializer(initVersion())
    {
        // Initialization transactions must come from the ProxyAdmin or its owner.
        _assertOnlyProxyAdminOrProxyAdminOwner();

        // Now perform initialization logic.
        systemConfig = _systemConfig;
        anchorStateRegistry = _anchorStateRegistry;
        if (address(_ethLockbox) != address(0)) {
            ethLockbox = _ethLockbox;
        }

        _assertValidInteropState();
        // Assert that the lockbox state is valid.
        _assertValidLockboxState();

        // Set the l2Sender slot, only if it is currently empty. This signals the first
        // initialization of the contract.
        if (l2Sender == address(0)) {
            l2Sender = Constants.DEFAULT_L2_SENDER;
        }

        // Initialize the ResourceMetering contract.
        __ResourceMetering_init();
    }

    /// @notice Getter for the current paused status.
    function paused() public view returns (bool) {
        return ethLockbox.paused();
    }

    /// @notice Getter for the proof maturity delay.
    function proofMaturityDelaySeconds() public view returns (uint256) {
        return PROOF_MATURITY_DELAY_SECONDS;
    }

    /// @notice Getter for the address of the DisputeGameFactory contract.
    function disputeGameFactory() public view returns (IDisputeGameFactory) {
        return anchorStateRegistry.disputeGameFactory();
    }

    /// @notice Returns the SuperchainConfig contract.
    /// @return ISuperchainConfig The SuperchainConfig contract.
    function superchainConfig() external view returns (ISuperchainConfig) {
        return ethLockbox.superchainConfig();
    }

    /// @custom:legacy
    /// @notice Getter function for the address of the guardian.
    function guardian() external view returns (address) {
        return ethLockbox.guardian();
    }

    /// @custom:legacy
    /// @notice Getter for the dispute game finality delay.
    function disputeGameFinalityDelaySeconds() external view returns (uint256) {
        return anchorStateRegistry.disputeGameFinalityDelaySeconds();
    }

    /// @custom:legacy
    /// @notice Getter for the respected game type.
    function respectedGameType() external view returns (GameType) {
        return anchorStateRegistry.respectedGameType();
    }

    /// @custom:legacy
    /// @notice Getter for the retirement timestamp. Note that this value NO LONGER reflects the
    ///         timestamp at which the respected game type was updated. Game retirement and
    ///         respected game type value have been decoupled, this function now only returns the
    ///         retirement timestamp.
    function respectedGameTypeUpdatedAt() external view returns (uint64) {
        return anchorStateRegistry.retirementTimestamp();
    }

    /// @custom:legacy
    /// @notice Getter for the dispute game blacklist.
    /// @param _disputeGame The dispute game to check.
    /// @return Whether the dispute game is blacklisted.
    function disputeGameBlacklist(IDisputeGame _disputeGame) public view returns (bool) {
        return anchorStateRegistry.disputeGameBlacklist(_disputeGame);
    }

    /// @notice Computes the minimum gas limit for a deposit.
    ///         The minimum gas limit linearly increases based on the size of the calldata.
    ///         This is to prevent users from creating L2 resource usage without paying for it.
    ///         This function can be used when interacting with the portal to ensure forwards
    ///         compatibility.
    /// @param _byteCount Number of bytes in the calldata.
    /// @return The minimum gas limit for a deposit.
    function minimumGasLimit(uint64 _byteCount) public pure returns (uint64) {
        return _byteCount * 40 + 21000;
    }

    /// @notice Accepts value so that users can send ETH directly to this contract and have the
    ///         funds be deposited to their address on L2. This is intended as a convenience
    ///         function for EOAs. Contracts should call the depositTransaction() function directly
    ///         otherwise any deposited funds will be lost due to address aliasing.
    receive() external payable {
        depositTransaction(msg.sender, msg.value, RECEIVE_DEFAULT_GAS_LIMIT, false, bytes(""));
    }

    /// @notice Accepts ETH value without triggering a deposit to L2.
    function donateETH() external payable {
        // Intentionally empty.
    }

    /// @notice Proves a withdrawal transaction using an Output Root proof.
    /// @param _tx               Withdrawal transaction to finalize.
    /// @param _disputeGameIndex Index of the dispute game to prove the withdrawal against.
    /// @param _outputRootProof  Inclusion proof of the L2ToL1MessagePasser storage root.
    /// @param _withdrawalProof  Inclusion proof of the withdrawal within the L2ToL1MessagePasser.
    function proveWithdrawalTransaction(
        Types.WithdrawalTransaction memory _tx,
        uint256 _disputeGameIndex,
        Types.OutputRootProof calldata _outputRootProof,
        bytes[] calldata _withdrawalProof
    )
        external
    {
        // Cannot prove withdrawal transactions while the system is paused.
        _assertNotPaused();

        // Only allowlisted addresses can prove withdrawals.
        if (!PortalAllowlist.isAllowed(msg.sender)) {
            revert OptimismPortal_WithdrawerNotAllowed();
        }

        // Make sure that the target address is safe.
        if (_isUnsafeTarget(_tx.target)) {
            revert OptimismPortal_BadTarget();
        }

        // Cannot prove withdrawal with value when custom gas token mode is enabled.
        if (_isUsingCustomGasToken()) {
            if (_tx.value > 0) revert OptimismPortal_NotAllowedOnCGTMode();
        }

        // Fetch the dispute game proxy from the `DisputeGameFactory` contract.
        (,, IDisputeGame disputeGameProxy) = disputeGameFactory().gameAtIndex(_disputeGameIndex);

        // Game must be a Proper Game.
        if (!anchorStateRegistry.isGameProper(disputeGameProxy)) {
            revert OptimismPortal_ImproperDisputeGame();
        }

        // Game must have been respected game type when created.
        if (!anchorStateRegistry.isGameRespected(disputeGameProxy)) {
            revert OptimismPortal_InvalidDisputeGame();
        }

        // Game must not have resolved in favor of the Challenger (invalid root claim).
        if (disputeGameProxy.status() == GameStatus.CHALLENGER_WINS) {
            revert OptimismPortal_InvalidDisputeGame();
        }

        // As a sanity check, we make sure that the current timestamp is not less than or equal to
        // the dispute game's creation timestamp. Not strictly necessary but extra layer of
        // safety against weird bugs. Note that this blocks withdrawals from being proven in the
        // same block that a dispute game is created.
        if (block.timestamp <= disputeGameProxy.createdAt().raw()) {
            revert OptimismPortal_InvalidProofTimestamp();
        }

        // Extract the output root claim. Super game types use rootClaimByChainId to extract
        // the per-chain output root from the super root. Legacy game types use rootClaim directly.
        // TODO(#19816): Post interop clean up the legacy rootClaim() usage in OptimismPortal2.
        Claim outputRootClaim;
        if (GameTypes.isSuperGame(disputeGameProxy.gameType())) {
            outputRootClaim = disputeGameProxy.rootClaimByChainId(systemConfig.l2ChainId());
        } else {
            outputRootClaim = disputeGameProxy.rootClaim();
        }

        // Verify that the output root can be generated with the elements in the proof.
        if (outputRootClaim.raw() != Hashing.hashOutputRootProof(_outputRootProof)) {
            revert OptimismPortal_InvalidOutputRootProof();
        }

        // Load the ProvenWithdrawal into memory, using the withdrawal hash as a unique identifier.
        bytes32 withdrawalHash = Hashing.hashWithdrawal(_tx);

        // Compute the storage slot of the withdrawal hash in the L2ToL1MessagePasser contract.
        // Refer to the Solidity documentation for more information on how storage layouts are
        // computed for mappings.
        bytes32 storageKey = keccak256(
            abi.encode(
                withdrawalHash,
                uint256(0) // The withdrawals mapping is at the first slot in the layout.
            )
        );

        // Verify that the hash of this withdrawal was stored in the L2toL1MessagePasser contract
        // on L2. If this is true, under the assumption that the SecureMerkleTrie does not have
        // bugs, then we know that this withdrawal was actually triggered on L2 and can therefore
        // be relayed on L1.
        if (
            SecureMerkleTrie.verifyInclusionProof({
                    _key: abi.encode(storageKey),
                    _value: hex"01",
                    _proof: _withdrawalProof,
                    _root: _outputRootProof.messagePasserStorageRoot
                }) == false
        ) {
            revert OptimismPortal_InvalidMerkleProof();
        }

        // Designate the withdrawalHash as proven by storing the disputeGameProxy and timestamp in
        // the provenWithdrawals mapping. A given user may re-prove a withdrawalHash multiple
        // times, but each proof will reset the proof timer.
        provenWithdrawals[withdrawalHash][msg.sender] =
            ProvenWithdrawal({ disputeGameProxy: disputeGameProxy, timestamp: uint64(block.timestamp) });

        // Add the proof submitter to the list of proof submitters for this withdrawal hash.
        proofSubmitters[withdrawalHash].push(msg.sender);

        // Emit a WithdrawalProven events.
        emit WithdrawalProven(withdrawalHash, _tx.sender, _tx.target);
        emit WithdrawalProvenExtension1(withdrawalHash, msg.sender);
    }

    /// @notice Finalizes a withdrawal transaction.
    /// @param _tx Withdrawal transaction to finalize.
    function finalizeWithdrawalTransaction(Types.WithdrawalTransaction memory _tx) external {
        finalizeWithdrawalTransactionExternalProof(_tx, msg.sender);
    }

    /// @notice Migrates the total ETH balance of this contract to the ETHLockbox. Custom gas
    ///         token chains keep custody in the portal and cannot migrate ETH.
    function migrateLiquidity() public {
        if (!_isUsingLockbox()) revert OptimismPortal_NotUsingLockbox();
        // Liquidity migration can only be triggered by the ProxyAdmin owner.
        _assertOnlyProxyAdminOwner();

        if (_isUsingCustomGasToken()) revert OptimismPortal_NotAllowedOnCGTMode();

        // Migrate the liquidity.
        uint256 ethBalance = address(this).balance;
        ethLockbox.lockETH{ value: ethBalance }();
        emit ETHMigrated(address(ethLockbox), ethBalance);
    }

    /// @notice Allows the owner of the ProxyAdmin to migrate the OptimismPortal to use a new
    ///         lockbox, point at a new AnchorStateRegistry, and start to use the Super Roots proof
    ///         method. Primarily used for OptimismPortal instances to join the interop set, but
    ///         can also be used to swap the proof method from Output Roots to Super Roots if the
    ///         provided lockbox is the same as the current one.
    /// @dev    It is possible to change lockboxes without migrating liquidity. This can cause one
    ///         of the OptimismPortal instances connected to the new lockbox to not be able to
    ///         unlock sufficient ETH to finalize withdrawals which would trigger reverts. To avoid
    ///         this issue, guarantee that this function is called atomically alongside the
    ///         ETHLockbox.migrateLiquidity() function within the same transaction.
    /// @param _newLockbox The address of the new ETHLockbox contract.
    /// @param _newAnchorStateRegistry The address of the new AnchorStateRegistry contract.

    function migrateToSharedDisputeGame(
        IETHLockbox _newLockbox,
        IAnchorStateRegistry _newAnchorStateRegistry
    )
        external
    {
        if (!_isUsingInterop()) revert OptimismPortal_NotUsingInterop();
        // Migration can only be triggered when the system is not paused because the migration can
        // potentially unpause the system as a result of the modified ETHLockbox address.
        _assertNotPaused();

        // Migration can only be triggered by the ProxyAdmin owner.
        _assertOnlyProxyAdminOwner();

        // Chains can use this method to swap the proof method from Output Roots to Super Roots
        // without joining the interop set. In this case, the old and new lockboxes will be the
        // same. However, whether or not a chain is joining the interop set, all chains will need a
        // new AnchorStateRegistry when migrating to Super Roots. We therefore check that the new
        // AnchorStateRegistry is different than the old one to prevent this function from being
        // accidentally misused.
        if (anchorStateRegistry == _newAnchorStateRegistry) {
            revert OptimismPortal_MigratingToSameRegistry();
        }

        // Defense-in-depth: reject obvious operator footguns. The ProxyAdmin owner is fully
        // trusted (can upgrade the portal), but these checks make it harder to brick the portal by
        // installing zero / non-contract / unauthorized / mis-wired addresses.
        if (address(_newLockbox) == address(0) || address(_newAnchorStateRegistry) == address(0)) {
            revert OptimismPortal_ZeroAddress();
        }
        if (!_newLockbox.authorizedPortals(IOptimismPortal2(payable(address(this))))) {
            revert OptimismPortal_LockboxNotAuthorizedForPortal();
        }

        // Update the ETHLockbox.
        IETHLockbox oldLockbox = ethLockbox;
        ethLockbox = _newLockbox;

        // Update the AnchorStateRegistry.
        IAnchorStateRegistry oldAnchorStateRegistry = anchorStateRegistry;
        anchorStateRegistry = _newAnchorStateRegistry;

        // Emit a PortalMigrated event.
        emit PortalMigrated(oldLockbox, _newLockbox, oldAnchorStateRegistry, _newAnchorStateRegistry);
    }

    /// @notice Finalizes a withdrawal transaction, using an external proof submitter.
    /// @param _tx Withdrawal transaction to finalize.
    /// @param _proofSubmitter Address of the proof submitter.
    function finalizeWithdrawalTransactionExternalProof(
        Types.WithdrawalTransaction memory _tx,
        address _proofSubmitter
    )
        public
    {
        // Cannot finalize withdrawal transactions while the system is paused.
        _assertNotPaused();

        // Only allowlisted addresses can finalize withdrawals.
        if (!PortalAllowlist.isAllowed(msg.sender)) {
            revert OptimismPortal_WithdrawerNotAllowed();
        }

        // Cannot finalize withdrawal with value when custom gas token mode is enabled.
        if (_isUsingCustomGasToken()) {
            if (_tx.value > 0) revert OptimismPortal_NotAllowedOnCGTMode();
        }

        // Make sure that the l2Sender has not yet been set. The l2Sender is set to a value other
        // than the default value when a withdrawal transaction is being finalized. This check is
        // a defacto reentrancy guard.
        if (l2Sender != Constants.DEFAULT_L2_SENDER) {
            revert OptimismPortal_NoReentrancy();
        }

        // Make sure that the target address is safe.
        if (_isUnsafeTarget(_tx.target)) {
            revert OptimismPortal_BadTarget();
        }

        // Grab the withdrawal.
        bytes32 withdrawalHash = Hashing.hashWithdrawal(_tx);

        // Check that the withdrawal can be finalized.
        checkWithdrawal(withdrawalHash, _proofSubmitter);

        // Mark the withdrawal as finalized so it can't be replayed.
        finalizedWithdrawals[withdrawalHash] = true;

        // If using ETHLockbox, unlock the ETH from the ETHLockbox.
        if (_isUsingLockbox()) {
            if (_tx.value > 0) ethLockbox.unlockETH(_tx.value);
        }

        // Set the l2Sender so contracts know who triggered this withdrawal on L2.
        l2Sender = _tx.sender;

        // Trigger the call to the target contract. We use a custom low level method
        // SafeCall.callWithMinGas to ensure two key properties
        //   1. Target contracts cannot force this call to run out of gas by returning a very large
        //      amount of data (and this is OK because we don't care about the returndata here).
        //   2. The amount of gas provided to the execution context of the target is at least the
        //      gas limit specified by the user. If there is not enough gas in the current context
        //      to accomplish this, `callWithMinGas` will revert.
        bool success = SafeCall.callWithMinGas(_tx.target, _tx.gasLimit, _tx.value, _tx.data);

        // Reset the l2Sender back to the default value.
        l2Sender = Constants.DEFAULT_L2_SENDER;

        // Reverting here is useful for determining the exact gas cost to successfully execute the
        // sub call to the target contract if the minimum gas limit specified by the user would not
        // be sufficient to execute the sub call.
        if (!success && tx.origin == Constants.ESTIMATION_ADDRESS) {
            revert OptimismPortal_GasEstimation();
        }

        // A failed call means the L1CrossDomainMessenger reverted without recording the message,
        // e.g. because it ran out of gas. Revert so the withdrawal stays unfinalized and can be
        // finalized again with more gas, instead of being finalized with no way to replay it.
        //
        // Not every messenger failure is a gas problem. The messenger reverts whatever the gas
        // when the message targets an unsafe address (the L1CrossDomainMessenger or this
        // contract) or is a legacy message that was already relayed. Such withdrawals can be
        // proven but never finalized, where before they were finalized as failed. Their funds
        // stay locked in the same place either way.
        //
        // Checking the sender is enough because the L2CrossDomainMessenger only initiates
        // withdrawals to `otherMessenger`, and blocks `L2ToL1MessagePasser` as a relay target.
        if (!success && _tx.sender == Predeploys.L2_CROSS_DOMAIN_MESSENGER) {
            revert OptimismPortal_L1CDMCallFailed();
        }

        // All other withdrawals are immediately finalized. Replayability can
        // be achieved through contracts built on top of this contract
        emit WithdrawalFinalized(withdrawalHash, success);

        // If using ETHLockbox, send ETH back to the Lockbox in the case of a failed transaction or
        // it'll get stuck here and would need to be moved back via admin action.
        if (_isUsingLockbox()) {
            if (!success && _tx.value > 0) {
                ethLockbox.lockETH{ value: _tx.value }();
            }
        }
    }

    /// @notice Checks that a withdrawal has been proven and is ready to be finalized.
    /// @param _withdrawalHash Hash of the withdrawal.
    /// @param _proofSubmitter Address of the proof submitter.
    function checkWithdrawal(bytes32 _withdrawalHash, address _proofSubmitter) public view {
        // Grab the withdrawal and dispute game proxy.
        ProvenWithdrawal memory provenWithdrawal = provenWithdrawals[_withdrawalHash][_proofSubmitter];
        IDisputeGame disputeGameProxy = provenWithdrawal.disputeGameProxy;

        // Check that this withdrawal has not already been finalized, this is replay protection.
        if (finalizedWithdrawals[_withdrawalHash]) {
            revert OptimismPortal_AlreadyFinalized();
        }

        // A withdrawal can only be finalized if it has been proven. We know that a withdrawal has
        // been proven at least once when its timestamp is non-zero. Unproven withdrawals will have
        // a timestamp of zero.
        if (provenWithdrawal.timestamp == 0) {
            revert OptimismPortal_Unproven();
        }

        // As a sanity check, we make sure that the proven withdrawal's timestamp is greater than
        // starting timestamp inside the Dispute Game. Not strictly necessary but extra layer of
        // safety against weird bugs in the proving step. Note that this blocks withdrawals that
        // are proven in the same block that a dispute game is created.
        if (provenWithdrawal.timestamp <= disputeGameProxy.createdAt().raw()) {
            revert OptimismPortal_InvalidProofTimestamp();
        }

        // A proven withdrawal must wait at least `PROOF_MATURITY_DELAY_SECONDS` before finalizing.
        if (block.timestamp - provenWithdrawal.timestamp <= PROOF_MATURITY_DELAY_SECONDS) {
            revert OptimismPortal_ProofNotOldEnough();
        }

        // Check that the root claim is valid.
        if (!anchorStateRegistry.isGameClaimValid(disputeGameProxy)) {
            revert OptimismPortal_InvalidRootClaim();
        }
    }

    /// @notice Deletes a withdrawal proof whose dispute game can never lead to finalization.
    ///         Permissionless because both conditions checked here are permanent.
    /// @param _withdrawalHash Hash of the withdrawal.
    /// @param _proofSubmitter Address of the proof submitter.
    function deleteProvenWithdrawal(bytes32 _withdrawalHash, address _proofSubmitter) external {
        ProvenWithdrawal memory provenWithdrawal = provenWithdrawals[_withdrawalHash][_proofSubmitter];
        if (provenWithdrawal.timestamp == 0) {
            revert OptimismPortal_Unproven();
        }

        IDisputeGame disputeGameProxy = provenWithdrawal.disputeGameProxy;
        if (
            disputeGameProxy.status() != GameStatus.CHALLENGER_WINS
                && !anchorStateRegistry.isGameBlacklisted(disputeGameProxy)
        ) {
            revert OptimismPortal_DisputeGameNotInvalidated();
        }

        delete provenWithdrawals[_withdrawalHash][_proofSubmitter];

        emit WithdrawalProofDeleted(_withdrawalHash, _proofSubmitter);
    }

    /// @notice Accepts deposits of ETH and data, and emits a TransactionDeposited event for use in
    ///         deriving deposit transactions. Note that if a deposit is made by a contract, its
    ///         address will be aliased when retrieved using `tx.origin` or `msg.sender`. Consider
    ///         using the CrossDomainMessenger contracts for a simpler developer experience.
    /// @dev    The `msg.value` is locked on the ETHLockbox and minted as ETH when the deposit
    ///         arrives on L2, while `_value` specifies how much ETH to send to the target.
    /// @param _to         Target address on L2.
    /// @param _value      ETH value to send to the recipient.
    /// @param _gasLimit   Amount of L2 gas to purchase by burning gas on L1.
    /// @param _isCreation Whether or not the transaction is a contract creation.
    /// @param _data       Data to trigger the recipient with.
    function depositTransaction(
        address _to,
        uint256 _value,
        uint64 _gasLimit,
        bool _isCreation,
        bytes memory _data
    )
        public
        payable
        metered(_gasLimit)
    {
        // This check will prevent deposits from being made by addresses that are not allowed to make deposits.
        // It will also prevent calls from the L1CrossDomainMessenger, and prevents the L1StandardBridge
        // from depositing ETH and minting tokens on L2.
        // The proxy admin owner is always allowed so that L2 contract upgrades can be deposited.
        if (!PortalAllowlist.isAllowed(msg.sender) && msg.sender != proxyAdminOwner()) {
            revert OptimismPortal_DepositorNotAllowed();
        }

        if (_isUsingCustomGasToken()) {
            if (msg.value > 0) revert OptimismPortal_NotAllowedOnCGTMode();
        }

        // If using ETHLockbox, lock the ETH in the ETHLockbox.
        if (_isUsingLockbox()) {
            if (msg.value > 0) ethLockbox.lockETH{ value: msg.value }();
        }

        // Just to be safe, make sure that people specify address(0) as the target when doing
        // contract creations.
        if (_isCreation && _to != address(0)) {
            revert OptimismPortal_BadTarget();
        }

        // Prevent depositing transactions that have too small of a gas limit. Users should pay
        // more for more resource usage.
        if (_gasLimit < minimumGasLimit(uint64(_data.length))) {
            revert OptimismPortal_GasLimitTooLow();
        }

        // Prevent the creation of deposit transactions that have too much calldata. This gives an
        // upper limit on the size of unsafe blocks over the p2p network. 120kb is chosen to ensure
        // that the transaction can fit into the p2p network policy of 128kb even though deposit
        // transactions are not gossipped over the p2p network.
        if (_data.length > 120_000) {
            revert OptimismPortal_CalldataTooLarge();
        }

        // Transform the from-address to its alias if the caller is a contract.
        address from = msg.sender;
        if (!EOA.isSenderEOA()) {
            from = AddressAliasHelper.applyL1ToL2Alias(msg.sender);
        }

        // Compute the opaque data that will be emitted as part of the TransactionDeposited event.
        // We use opaque data so that we can update the TransactionDeposited event in the future
        // without breaking the current interface.
        bytes memory opaqueData = abi.encodePacked(msg.value, _value, _gasLimit, _isCreation, _data);

        // Emit a TransactionDeposited event so that the rollup node can derive a deposit
        // transaction for this deposit.
        emit TransactionDeposited(from, _to, DEPOSIT_VERSION, opaqueData);
    }

    /// @notice External getter for the number of proof submitters for a withdrawal hash.
    /// @param _withdrawalHash Hash of the withdrawal.
    /// @return The number of proof submitters for the withdrawal hash.
    function numProofSubmitters(bytes32 _withdrawalHash) external view returns (uint256) {
        return proofSubmitters[_withdrawalHash].length;
    }

    /// @notice Checks if the ETHLockbox feature is enabled.
    /// @return bool True if the ETHLockbox feature is enabled.
    function _isUsingLockbox() internal view returns (bool) {
        return systemConfig.isFeatureEnabled(Features.ETH_LOCKBOX) && address(ethLockbox) != address(0);
    }

    /// @notice Checks if the Interop feature is enabled.
    /// @return bool True if the Interop feature is enabled.
    function _isUsingInterop() internal view returns (bool) {
        return systemConfig.isFeatureEnabled(Features.INTEROP) && systemConfig.isFeatureEnabled(Features.ETH_LOCKBOX);
    }

    /// @notice Checks if the Custom Gas Token feature is enabled.
    /// @return bool True if the Custom Gas Token feature is enabled.
    function _isUsingCustomGasToken() internal view returns (bool) {
        // NOTE: Chains are not supposed to enable Custom Gas Token (CGT) mode after initial deployment.
        //       Enabling CGT post-deployment is strongly discouraged and may lead to unexpected behavior.
        return systemConfig.isFeatureEnabled(Features.CUSTOM_GAS_TOKEN);
    }

    /// @notice Asserts that the contract is not paused.
    function _assertNotPaused() internal view {
        if (paused()) {
            revert OptimismPortal_CallPaused();
        }
    }

    /// @notice Asserts the ETHLockbox feature flag must be set if INTEROP is set
    function _assertValidInteropState() internal view {
        if (systemConfig.isFeatureEnabled(Features.INTEROP) && !systemConfig.isFeatureEnabled(Features.ETH_LOCKBOX)) {
            revert OptimismPortal_InvalidInteropState();
        }
    }

    /// @notice Asserts that the ETHLockbox is configured.
    function _assertValidLockboxState() internal view {
        if (!systemConfig.isFeatureEnabled(Features.ETH_LOCKBOX) || address(ethLockbox) == address(0)) {
            revert OptimismPortal_InvalidLockboxState();
        }
    }

    /// @notice Checks if a target address is unsafe.
    function _isUnsafeTarget(address _target) internal view virtual returns (bool) {
        // Prevent users from targeting an unsafe target address on a withdrawal transaction.
        return _target == address(this) || _target == address(ethLockbox);
    }

    /// @notice Getter for the resource config. Used internally by the ResourceMetering contract.
    ///         The SystemConfig is the source of truth for the resource config.
    /// @return config_ ResourceMetering ResourceConfig
    function _resourceConfig() internal view override returns (ResourceMetering.ResourceConfig memory config_) {
        IResourceMetering.ResourceConfig memory config = systemConfig.resourceConfig();
        assembly ("memory-safe") {
            config_ := config
        }
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 5000,
    "enabled": true
  },
  "evmVersion": "london",
  "remappings": [
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-v5/=lib/openzeppelin-contracts-v5/contracts/",
    "@rari-capital/solmate/=lib/solmate/",
    "@lib-keccak/=lib/lib-keccak/contracts/lib/",
    "@solady/=lib/solady/src/",
    "forge-std/=lib/forge-std/src/",
    "ds-test/=lib/forge-std/lib/ds-test/src/",
    "safe-contracts/=lib/safe-contracts/contracts/",
    "kontrol-cheatcodes/=lib/kontrol-cheatcodes/src/",
    "interfaces/=interfaces/",
    "lib/lib-keccak/:solady/=lib/lib-keccak/lib/solady/",
    "lib/lib-keccak/:solady/=lib/lib-keccak/lib/solady/",
    "@solady-test/=lib/lib-keccak/lib/solady/test/",
    "erc4626-tests/=lib/openzeppelin-contracts-v5/lib/erc4626-tests/",
    "lib-keccak/=lib/lib-keccak/contracts/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts-v5/=lib/openzeppelin-contracts-v5/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "solady/=lib/solady/",
    "solmate/=lib/solmate/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.