Skip to content

Commit

Permalink
contracts-bedrock: update L1BlockInterop
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfuturistic committed May 23, 2024
1 parent 4b1c1a8 commit 6860c6f
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 338 deletions.
13 changes: 10 additions & 3 deletions packages/contracts-bedrock/scripts/L2Genesis.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,16 @@ contract L2Genesis is Deployer {

/// @notice This predeploy is following the safety invariant #1.
function setL1Block() public {
_setImplementationCode(Predeploys.L1_BLOCK_ATTRIBUTES);
// Note: L1 block attributes are set to 0.
// Before the first user-tx the state is overwritten with actual L1 attributes.
if (cfg.useInterop()) {
string memory cname = "L1BlockInterop";
address impl = Predeploys.predeployToCodeNamespace(Predeploys.L1_BLOCK_ATTRIBUTES);
console.log("Setting %s implementation at: %s", cname, impl);
vm.etch(impl, vm.getDeployedCode(string.concat(cname, ".sol:", cname)));
} else {
_setImplementationCode(Predeploys.L1_BLOCK_ATTRIBUTES);
// Note: L1 block attributes are set to 0.
// Before the first user-tx the state is overwritten with actual L1 attributes.
}
}

/// @notice This predeploy is following the safety invariant #1.
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts-bedrock/semver-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
},
"src/L2/L1Block.sol": {
"initCodeHash": "0x00961e82f3ed7f7755115c897304063e283bff0bed1200d50e0abe5c59424069",
"sourceCodeHash": "0x9c19260697d5ed5f85318098dead3f5882d77f1ee87233da1b47d1e87c88bce8"
"sourceCodeHash": "0x4ce2aa66bf3ed7881b64bb88b899e8fa26e350ddd21c6561988a8f14cf332647"
},
"src/L2/L1BlockInterop.sol": {
"initCodeHash": "0x2929683ae3e4f76fca87462f0217b6e24c418bc964328f8d095f0e90343d2cae",
"sourceCodeHash": "0x78ca58451d973e9896eeb9d19699943b7a4e96c36da2724f47d5aafdf243abcd"
"initCodeHash": "0x058e46e34981a28b7b820772061bbade0676765875806cde2fd77750b6b29058",
"sourceCodeHash": "0x56caef2333dcbf461bc8cd442cad00c57a1946443200a6fb9e22b52f91f77127"
},
"src/L2/L1FeeVault.sol": {
"initCodeHash": "0x2744d34573be83206d1b75d049d18a7bb37f9058e68c0803e5008c46b0dc2474",
Expand Down
90 changes: 64 additions & 26 deletions packages/contracts-bedrock/snapshots/abi/L1BlockInterop.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "dependencySet",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "dependencySetSize",
Expand Down Expand Up @@ -250,6 +231,24 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "enum ConfigType",
"name": "_type",
"type": "uint8"
},
{
"internalType": "bytes",
"name": "_value",
"type": "bytes"
}
],
"name": "setConfig",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -333,13 +332,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "setL1BlockValuesInterop",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "timestamp",
Expand All @@ -366,6 +358,32 @@
"stateMutability": "pure",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "chainId",
"type": "uint256"
}
],
"name": "DependencyAdded",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "chainId",
"type": "uint256"
}
],
"name": "DependencyRemoved",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -397,6 +415,26 @@
"name": "GasPayingTokenSet",
"type": "event"
},
{
"inputs": [],
"name": "AlreadyDependency",
"type": "error"
},
{
"inputs": [],
"name": "CantRemovedDependency",
"type": "error"
},
{
"inputs": [],
"name": "DependencySetSizeTooLarge",
"type": "error"
},
{
"inputs": [],
"name": "NotDependency",
"type": "error"
},
{
"inputs": [],
"name": "NotDepositor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
"type": "uint256"
},
{
"bytes": "32",
"bytes": "64",
"label": "dependencySet",
"offset": 0,
"slot": "8",
"type": "uint256[]"
"type": "struct EnumerableSet.UintSet"
}
]
8 changes: 3 additions & 5 deletions packages/contracts-bedrock/src/L2/L1Block.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.15;
import { ISemver } from "src/universal/ISemver.sol";
import { Constants } from "src/libraries/Constants.sol";
import { GasPayingToken, IGasToken } from "src/libraries/GasPayingToken.sol";
import "src/libraries/L1BlockErrors.sol";

/// @custom:proxied
/// @custom:predeploy 0x4200000000000000000000000000000000000015
Expand All @@ -13,9 +14,6 @@ import { GasPayingToken, IGasToken } from "src/libraries/GasPayingToken.sol";
/// set by the "depositor" account, a special system address. Depositor account transactions
/// are created by the protocol whenever we move to a new epoch.
contract L1Block is ISemver, IGasToken {
/// @notice Error returns when a non-depositor account tries to set L1 block values.
error NotDepositor();

/// @notice Event emitted when the gas paying token is set.
event GasPayingTokenSet(address indexed token, uint8 indexed decimals, bytes32 name, bytes32 symbol);

Expand Down Expand Up @@ -59,9 +57,9 @@ contract L1Block is ISemver, IGasToken {
/// @notice The latest L1 blob base fee.
uint256 public blobBaseFee;

/// @custom:semver 1.4.0
/// @custom:semver 1.4.1-beta.1
function version() public pure virtual returns (string memory) {
return "1.4.0";
return "1.4.1-beta.1";
}

/// @notice Returns the gas paying token, its decimals, name and symbol.
Expand Down
159 changes: 77 additions & 82 deletions packages/contracts-bedrock/src/L2/L1BlockInterop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,103 @@
pragma solidity 0.8.15;

import { L1Block } from "src/L2/L1Block.sol";
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { GasPayingToken } from "src/libraries/GasPayingToken.sol";
import { StaticConfig } from "src/libraries/StaticConfig.sol";
import "src/libraries/L1BlockErrors.sol";

/// @notice Thrown when a non-depositor account attempts to set L1 block values.
error NotDepositor();

/// @notice Thrown when dependencySetSize does not match the length of the dependency set.
error DependencySetSizeMismatch();
/// @notice Enum representing different types of configurations that can be set on L1Block.
/// @custom:value GAS_PAYING_TOKEN Represents the config type for the gas paying token.
/// @custom:value ADD_DEPENDENCY Represents the config type for adding a chain to the interchain dependency set.
/// @custom:value REMOVE_DEPENDENCY Represents the config type for removing a chain from the interchain dependency set.
enum ConfigType {
GAS_PAYING_TOKEN,
ADD_DEPENDENCY,
REMOVE_DEPENDENCY
}

/// @custom:proxied
/// @custom:predeploy 0x4200000000000000000000000000000000000015
/// @title L1BlockInterop
/// @notice Interop extenstions of L1Block.
contract L1BlockInterop is L1Block {
/// @notice The chain IDs of the interop dependency set.
uint256[] public dependencySet;
using EnumerableSet for EnumerableSet.UintSet;

/// @notice Event emitted when a new dependency is added to the interop dependency set.
event DependencyAdded(uint256 indexed chainId);

/// @notice Event emitted when a dependency is removed from the interop dependency set.
event DependencyRemoved(uint256 indexed chainId);

/// @custom:semver 1.3.0+interop
/// @notice The interop dependency set, containing the chain IDs in it.
EnumerableSet.UintSet dependencySet;

/// @custom:semver +interop
function version() public pure override returns (string memory) {
return string.concat(super.version(), "+interop");
}

/// @notice Updates the L1 block values for an Interop upgraded chain.
/// Params are packed and passed in as raw msg.data instead of ABI to reduce calldata size.
/// Params are expected to be in the following order:
/// 1. _baseFeeScalar L1 base fee scalar
/// 2. _blobBaseFeeScalar L1 blob base fee scalar
/// 3. _sequenceNumber Number of L2 blocks since epoch start.
/// 4. _timestamp L1 timestamp.
/// 5. _number L1 blocknumber.
/// 6. _basefee L1 base fee.
/// 7. _blobBaseFee L1 blob base fee.
/// 8. _hash L1 blockhash.
/// 9. _batcherHash Versioned hash to authenticate batcher by.
/// 10. _dependencySetSize Size of the interop dependency set.
/// 11. _dependencySet Array of chain IDs for the interop dependency set.
function setL1BlockValuesInterop() external {
address depositor = DEPOSITOR_ACCOUNT();
assembly {
// Revert if the caller is not the depositor account.
if xor(caller(), depositor) {
mstore(0x00, 0x3cc50b45) // 0x3cc50b45 is the 4-byte selector of "NotDepositor()"
revert(0x1C, 0x04) // returns the stored 4-byte selector from above
}
// sequencenum (uint64), blobBaseFeeScalar (uint32), baseFeeScalar (uint32)
sstore(sequenceNumber.slot, shr(128, calldataload(4)))
// number (uint64) and timestamp (uint64)
sstore(number.slot, shr(128, calldataload(20)))
sstore(basefee.slot, calldataload(36)) // uint256
sstore(blobBaseFee.slot, calldataload(68)) // uint256
sstore(hash.slot, calldataload(100)) // bytes32
sstore(batcherHash.slot, calldataload(132)) // bytes32

// Load dependencySetSize from calldata (at offset 164 after calldata for setL1BlockValuesEcotone ends)
let dependencySetSize_ := shr(248, calldataload(164))

// Revert if dependencySetSize_ doesn't match the length of dependencySet in calldata
if xor(add(165, mul(dependencySetSize_, 0x20)), calldatasize()) {
mstore(0x00, 0x44165b6a) // 0x44165b6a is the 4-byte selector of "DependencySetSizeMismatch()"
revert(0x1C, 0x04) // returns the stored 4-byte selector from above
}

// Use memory to hash and get the start index of dependencySet
mstore(0x00, dependencySet.slot)
let dependencySetStartIndex := keccak256(0x00, 0x20)

// Iterate over calldata dependencySet and write to store dependencySet
for { let i := 0 } lt(i, dependencySetSize_) { i := add(i, 1) } {
// Load value from calldata and write to storage (dependencySet) at index
let val := calldataload(add(165, mul(i, 0x20)))
sstore(add(dependencySetStartIndex, i), val)
}

// Update length of dependencySet array
sstore(dependencySet.slot, dependencySetSize_)
}
}

/// @notice Returns true if a chain ID is in the interop dependency set and false otherwise.
/// Every chain ID is in the interop dependency set of itself.
/// The chain's chain ID is always considered to be in the dependency set.
/// @param _chainId The chain ID to check.
/// @return True if the chain ID to check is in the interop dependency set. False otherwise.
function isInDependencySet(uint256 _chainId) public view returns (bool) {
// Every chain ID is in the interop dependency set of itself.
if (_chainId == block.chainid) {
return true;
}

uint256 length = dependencySet.length;
for (uint256 i = 0; i < length;) {
if (dependencySet[i] == _chainId) {
return true;
}
unchecked {
i++;
}
}

return false;
return _chainId == block.chainid || dependencySet.contains(_chainId);
}

/// @notice Returns the size of the interop dependency set.
/// @return The size of the interop dependency set.
function dependencySetSize() external view returns (uint8) {
return uint8(dependencySet.length);
return uint8(dependencySet.length());
}

/// @notice Sets static configuration options for the L2 system. Can only be called by the special
/// depositor account.
/// @param _type The type of configuration to set.
/// @param _value The encoded value with which to set the configuration.
function setConfig(ConfigType _type, bytes calldata _value) external {
if (msg.sender != DEPOSITOR_ACCOUNT()) revert NotDepositor();

if (_type == ConfigType.GAS_PAYING_TOKEN) {
_setGasPayingToken(_value);
} else if (_type == ConfigType.ADD_DEPENDENCY) {
_addDependency(_value);
} else if (_type == ConfigType.REMOVE_DEPENDENCY) {
_removeDependency(_value);
}
}

/// @notice Internal method to set the gas paying token.
/// @param _value The encoded value with which to set the gas paying token.
function _setGasPayingToken(bytes calldata _value) internal {
(address token, uint8 decimals, bytes32 name, bytes32 symbol) = StaticConfig.decodeSetGasPayingToken(_value);

GasPayingToken.set({ _token: token, _decimals: decimals, _name: name, _symbol: symbol });

emit GasPayingTokenSet({ token: token, decimals: decimals, name: name, symbol: symbol });
}

/// @notice Internal method to add a dependency to the interop dependency set.
/// @param _value The encoded value with which to add the dependency.
function _addDependency(bytes calldata _value) internal {
uint256 chainId = StaticConfig.decodeAddDependency(_value);

if (dependencySet.length() == type(uint8).max) revert DependencySetSizeTooLarge();

if (chainId == block.chainid || !dependencySet.add(chainId)) revert AlreadyDependency();

emit DependencyAdded(chainId);
}

/// @notice Internal method to remove a dependency from the interop dependency set.
/// @param _value The encoded value with which to remove the dependency.
function _removeDependency(bytes calldata _value) internal {
uint256 chainId = StaticConfig.decodeRemoveDependency(_value);

if (chainId == block.chainid) revert CantRemovedDependency();

if (!dependencySet.remove(chainId)) revert NotDependency();

emit DependencyRemoved(chainId);
}
}
Loading

0 comments on commit 6860c6f

Please sign in to comment.