Skip to content

Commit

Permalink
contract change following idea of ethereum-optimism/specs#122
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiangxu committed Aug 23, 2024
1 parent ce49d0c commit c01c362
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/contracts-bedrock/lib/clones-with-immutable-args
Submodule clones-with-immutable-args added at 105efe
1 change: 1 addition & 0 deletions packages/contracts-bedrock/lib/openzeppelin-contracts-v5
Submodule openzeppelin-contracts-v5 added at dbb610
26 changes: 4 additions & 22 deletions packages/contracts-bedrock/src/L1/SystemConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Storage } from "src/libraries/Storage.sol";
import { Constants } from "src/libraries/Constants.sol";
import { OptimismPortal } from "src/L1/OptimismPortal.sol";
import { GasPayingToken, IGasToken } from "src/libraries/GasPayingToken.sol";
import { BatchInbox } from "src/libraries/BatchInbox.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/// @title SystemConfig
Expand All @@ -25,8 +26,7 @@ contract SystemConfig is OwnableUpgradeable, ISemver, IGasToken {
BATCHER,
GAS_CONFIG,
GAS_LIMIT,
UNSAFE_BLOCK_SIGNER,
BATCH_INBOX
UNSAFE_BLOCK_SIGNER
}

/// @notice Struct representing the addresses of L1 system contracts. These should be the
Expand Down Expand Up @@ -72,9 +72,6 @@ contract SystemConfig is OwnableUpgradeable, ISemver, IGasToken {
bytes32 public constant OPTIMISM_MINTABLE_ERC20_FACTORY_SLOT =
bytes32(uint256(keccak256("systemconfig.optimismmintableerc20factory")) - 1);

/// @notice Storage slot that the batch inbox address is stored at.
bytes32 public constant BATCH_INBOX_SLOT = bytes32(uint256(keccak256("systemconfig.batchinbox")) - 1);

/// @notice Storage slot for block at which the op-node can start searching for logs from.
bytes32 public constant START_BLOCK_SLOT = bytes32(uint256(keccak256("systemconfig.startBlock")) - 1);

Expand Down Expand Up @@ -200,7 +197,7 @@ contract SystemConfig is OwnableUpgradeable, ISemver, IGasToken {
_setGasLimit(_gasLimit);

Storage.setAddress(UNSAFE_BLOCK_SIGNER_SLOT, _unsafeBlockSigner);
_setBatchInbox(_batchInbox);
BatchInbox.set(_batchInbox);
Storage.setAddress(L1_CROSS_DOMAIN_MESSENGER_SLOT, _addresses.l1CrossDomainMessenger);
Storage.setAddress(L1_ERC_721_BRIDGE_SLOT, _addresses.l1ERC721Bridge);
Storage.setAddress(L1_STANDARD_BRIDGE_SLOT, _addresses.l1StandardBridge);
Expand Down Expand Up @@ -273,7 +270,7 @@ contract SystemConfig is OwnableUpgradeable, ISemver, IGasToken {

/// @notice Getter for the BatchInbox address.
function batchInbox() external view returns (address addr_) {
addr_ = Storage.getAddress(BATCH_INBOX_SLOT);
addr_ = BatchInbox.get();
}

/// @notice Getter for the StartBlock number.
Expand Down Expand Up @@ -357,21 +354,6 @@ contract SystemConfig is OwnableUpgradeable, ISemver, IGasToken {
emit ConfigUpdate(VERSION, UpdateType.BATCHER, data);
}

/// @notice Updates the batch inbox address. Can only be called by the owner.
/// @param _batchInbox New batch inbox address.
function setBatchInbox(address _batchInbox) external onlyOwner {
_setBatchInbox(_batchInbox);
}

/// @notice Updates the batch inbox address.
/// @param _batchInbox New batch inbox address.
function _setBatchInbox(address _batchInbox) internal {
Storage.setAddress(BATCH_INBOX_SLOT, _batchInbox);

bytes memory data = abi.encode(_batchInbox);
emit ConfigUpdate(VERSION, UpdateType.BATCH_INBOX, data);
}

/// @notice Updates gas config. Can only be called by the owner.
/// Deprecated in favor of setGasConfigEcotone since the Ecotone upgrade.
/// @param _overhead New overhead value.
Expand Down
15 changes: 15 additions & 0 deletions packages/contracts-bedrock/src/L1/SystemConfigInterop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.15;
import { Constants } from "src/libraries/Constants.sol";
import { OptimismPortalInterop as OptimismPortal } from "src/L1/OptimismPortalInterop.sol";
import { GasPayingToken } from "src/libraries/GasPayingToken.sol";
import { BatchInbox } from "src/libraries/BatchInbox.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { SystemConfig } from "src/L1/SystemConfig.sol";
import { ConfigType } from "src/L2/L1BlockInterop.sol";
Expand Down Expand Up @@ -63,4 +64,18 @@ contract SystemConfigInterop is SystemConfig {
ConfigType.REMOVE_DEPENDENCY, StaticConfig.encodeRemoveDependency(_chainId)
);
}

/// @notice Updates the batch inbox address. Can only be called by the owner.
/// @param _batchInbox New batch inbox address.
function setBatchInbox(address _batchInbox) external onlyOwner {
if (_batchInbox != BatchInbox.get()) {
BatchInbox.set(_batchInbox);
OptimismPortal(payable(optimismPortal())).setConfig(
ConfigType.SET_BATCH_INBOX,
StaticConfig.encodeSetBatchInbox({
_batchInbox: _batchInbox,
})
);
}
}
}
4 changes: 0 additions & 4 deletions packages/contracts-bedrock/src/L2/L1Block.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ contract L1Block is ISemver, IGasToken {
/// @notice The versioned hash to authenticate the batcher by.
bytes32 public batcherHash;

/// @notice The canonical batch inbox.
bytes32 public batchInbox;

/// @notice The overhead value applied to the L1 portion of the transaction fee.
/// @custom:legacy
uint256 public l1FeeOverhead;
Expand Down Expand Up @@ -152,7 +149,6 @@ contract L1Block is ISemver, IGasToken {
sstore(blobBaseFee.slot, calldataload(68)) // uint256
sstore(hash.slot, calldataload(100)) // bytes32
sstore(batcherHash.slot, calldataload(132)) // bytes32
sstore(batchInbox.slot, calldataload(164)) // bytes32
}
}

Expand Down
14 changes: 13 additions & 1 deletion packages/contracts-bedrock/src/L2/L1BlockInterop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableS
import { GasPayingToken } from "src/libraries/GasPayingToken.sol";
import { StaticConfig } from "src/libraries/StaticConfig.sol";
import "src/libraries/L1BlockErrors.sol";
import { BatchInbox } from "src/libraries/BatchInbox.sol";

/// @notice Enum representing different types of configurations that can be set on L1BlockInterop.
/// @custom:value SET_GAS_PAYING_TOKEN Represents the config type for setting the gas paying token.
/// @custom:value ADD_DEPENDENCY Represents the config type for adding a chain to the interop dependency set.
/// @custom:value REMOVE_DEPENDENCY Represents the config type for removing a chain from the interop dependency set.
/// @custom:value SET_BATCH_INBOX Represents the config type for setting the batch inbox.
enum ConfigType {
SET_GAS_PAYING_TOKEN,
ADD_DEPENDENCY,
REMOVE_DEPENDENCY
REMOVE_DEPENDENCY,
SET_BATCH_INBOX
}

/// @custom:proxied
Expand Down Expand Up @@ -65,6 +68,8 @@ contract L1BlockInterop is L1Block {
_addDependency(_value);
} else if (_type == ConfigType.REMOVE_DEPENDENCY) {
_removeDependency(_value);
} else if (_type == ConfigType.SET_BATCH_INBOX) {
_setBatchInbox(_value);
}
}

Expand Down Expand Up @@ -101,4 +106,11 @@ contract L1BlockInterop is L1Block {

emit DependencyRemoved(chainId);
}

/// @notice Internal method to set the batch inbox address.
/// @param _value The encoded value with which to set the batch inbox address.
function _setBatchInbox(bytes calldata _value) internal {
uint256 batchInbox = StaticConfig.decodeSetBatchInbox(_value);

This comment has been minimized.

Copy link
@tynes

tynes Aug 23, 2024

This can just use sstore directly

BatchInbox.set(batchInbox);
}
}
24 changes: 24 additions & 0 deletions packages/contracts-bedrock/src/libraries/BatchInbox.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { Storage } from "src/libraries/Storage.sol";

/// @title BatchInbox
/// @notice Handles reading and writing the batch inbox address to storage.
/// To be used in any place where batch inbox address is read or
/// written to state. If multiple contracts use this library, the
/// values in storage should be kept in sync between them.
library BatchInbox {
/// @notice Storage slot that the batch inbox address is stored at.
bytes32 public constant BATCH_INBOX_SLOT = bytes32(uint256(keccak256("opstack.batchinbox")) - 1);

/// @notice Writes the batch inbox address to the magic storage slot.
function set(address _batchInbox) internal {
Storage.setAddress(BATCH_INBOX_SLOT, _batchInbox);
}

/// @notice Reads the batch inbox address from the magic storage slot.
function get() internal view returns (address) {
return Storage.getAddress(BATCH_INBOX_SLOT);
}
}
14 changes: 14 additions & 0 deletions packages/contracts-bedrock/src/libraries/StaticConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ library StaticConfig {
function decodeRemoveDependency(bytes memory _data) internal pure returns (uint256) {
return abi.decode(_data, (uint256));
}

/// @notice Encodes the static configuration data for setting a batch inbox.
/// @param _batchInbox Address of the batch inbox.
/// @return Encoded static configuration data.

This comment has been minimized.

Copy link
@tynes

tynes Aug 23, 2024

We don't need this library since the batch inbox is fixed size and we aren't packing anything in the most significant bits

function encodeSetBatchInbox(address _batchInbox) internal pure returns (bytes memory) {
return abi.encode(_batchInbox);
}

/// @notice Decodes the static configuration data for setting a batch inbox.
/// @param _data Encoded static configuration data.
/// @return Decoded Address of the batch inbox.
function decodeSetBatchInbox(bytes memory _data) internal pure returns (address) {
return abi.decode(_data, (address));
}
}

0 comments on commit c01c362

Please sign in to comment.