Skip to content

Commit

Permalink
Merge 30cb13f into f95beef
Browse files Browse the repository at this point in the history
  • Loading branch information
kelemeno authored Sep 10, 2024
2 parents f95beef + 30cb13f commit 7dc5166
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 48 deletions.
150 changes: 110 additions & 40 deletions l1-contracts/contracts/bridgehub/Bridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {BridgehubL2TransactionRequest, L2Message, L2Log, TxStatus} from "../comm
import {AddressAliasHelper} from "../vendor/AddressAliasHelper.sol";
import {IMessageRoot} from "./IMessageRoot.sol";
import {ICTMDeploymentTracker} from "./ICTMDeploymentTracker.sol";
import {AssetHandlerNotRegistered, ZKChainLimitReached, Unauthorized, CTMAlreadyRegistered, CTMNotRegistered, ZeroChainId, ChainIdTooBig, SharedBridgeNotSet, BridgeHubAlreadyRegistered, AddressTooLow, MsgValueMismatch, WrongMagicValue, ZeroAddress} from "../common/L1ContractErrors.sol";
import {MigrationPaused, AssetIdAlreadyRegistered, ChainAlreadyLive, ChainNotLegacy, CTMNotRegistered, ChainIdNotRegistered, AssetHandlerNotRegistered, ZKChainLimitReached, CTMAlreadyRegistered, CTMNotRegistered, ZeroChainId, ChainIdTooBig, BridgeHubAlreadyRegistered, AddressTooLow, MsgValueMismatch, ZeroAddress, Unauthorized, SharedBridgeNotSet, WrongMagicValue, ChainIdAlreadyExists, ChainIdMismatch, ChainIdCantBeCurrentChain, EmptyAssetId, AssetIdNotSupported, IncorrectBridgeHubAddress} from "../common/L1ContractErrors.sol";

/// @author Matter Labs
/// @custom:security-contact [email protected]
Expand Down Expand Up @@ -108,28 +108,38 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
}

modifier onlyChainCTM(uint256 _chainId) {
require(msg.sender == chainTypeManager[_chainId], "BH: not chain CTM");
if (msg.sender != chainTypeManager[_chainId]) {
revert Unauthorized(msg.sender);
}
_;
}

modifier onlyL1() {
require(L1_CHAIN_ID == block.chainid, "BH: not L1");
if (L1_CHAIN_ID != block.chainid) {
revert Unauthorized(msg.sender);
}
_;
}

modifier onlySettlementLayerRelayedSender() {
/// There is no sender for the wrapping, we use a virtual address.
require(msg.sender == SETTLEMENT_LAYER_RELAY_SENDER, "BH: not relayed senser");
if (msg.sender != SETTLEMENT_LAYER_RELAY_SENDER) {
revert Unauthorized(msg.sender);
}
_;
}

modifier onlyAssetRouter() {
require(msg.sender == assetRouter, "BH: not asset router");
if (msg.sender != assetRouter) {
revert Unauthorized(msg.sender);
}
_;
}

modifier whenMigrationsNotPaused() {
require(!migrationPaused, "BH: migrations paused");
if (migrationPaused) {
revert MigrationPaused();
}
_;
}

Expand Down Expand Up @@ -218,11 +228,16 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
/// @param _chainId The chainId of the legacy chain we are migrating.
function setLegacyChainAddress(uint256 _chainId) external {
address ctm = chainTypeManager[_chainId];
require(ctm != address(0), "BH: chain not legacy");
require(!zkChainMap.contains(_chainId), "BH: chain already migrated");
/// Note we have to do this before CTM is upgraded.
if (ctm == address(0)) {
revert ChainNotLegacy();
}
if (zkChainMap.contains(_chainId)) {
revert ChainAlreadyLive();
}
address chainAddress = IChainTypeManager(ctm).getZKChainLegacy(_chainId);
require(chainAddress != address(0), "BH: chain not legacy 2");
if (chainAddress == address(0)) {
revert ChainNotLegacy();
}
_registerNewZKChain(_chainId, chainAddress);
}

Expand Down Expand Up @@ -260,7 +275,9 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
/// @notice asset id can represent any token contract with the appropriate interface/functionality
/// @param _baseTokenAssetId asset id of base token to be registered
function addTokenAssetId(bytes32 _baseTokenAssetId) external onlyOwner {
require(!assetIdIsRegistered[_baseTokenAssetId], "BH: asset id already registered");
if (assetIdIsRegistered[_baseTokenAssetId]) {
revert AssetIdAlreadyRegistered();
}
assetIdIsRegistered[_baseTokenAssetId] = true;

emit BaseTokenAssetIdRegistered(_baseTokenAssetId);
Expand Down Expand Up @@ -294,8 +311,12 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus

address sender = L1_CHAIN_ID == block.chainid ? msg.sender : AddressAliasHelper.undoL1ToL2Alias(msg.sender);
// This method can be accessed by l1CtmDeployer only
require(sender == address(l1CtmDeployer), "BH: not ctm deployer");
require(chainTypeManagerIsRegistered[_assetAddress], "CTM not registered");
if (sender != address(l1CtmDeployer)) {
revert Unauthorized(sender);
}
if (!chainTypeManagerIsRegistered[_assetAddress]) {
revert CTMNotRegistered();
}

bytes32 assetInfo = keccak256(abi.encode(L1_CHAIN_ID, sender, _additionalData));
ctmAssetIdToAddress[assetInfo] = _assetAddress;
Expand Down Expand Up @@ -325,32 +346,7 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
bytes calldata _initData,
bytes[] calldata _factoryDeps
) external onlyOwnerOrAdmin nonReentrant whenNotPaused onlyL1 returns (uint256) {
if (_chainId == 0) {
revert ZeroChainId();
}
if (_chainId > type(uint48).max) {
revert ChainIdTooBig();
}
require(_chainId != block.chainid, "BH: chain id must not match current chainid");
if (_chainTypeManager == address(0)) {
revert ZeroAddress();
}
if (_baseTokenAssetId == bytes32(0)) {
revert ZeroAddress();
}

if (!chainTypeManagerIsRegistered[_chainTypeManager]) {
revert CTMNotRegistered();
}

require(assetIdIsRegistered[_baseTokenAssetId], "BH: asset id not registered");

if (assetRouter == address(0)) {
revert SharedBridgeNotSet();
}
if (chainTypeManager[_chainId] != address(0)) {
revert BridgeHubAlreadyRegistered();
}
_validateChainParams({_chainId: _chainId, _assetId: _baseTokenAssetId, _chainTypeManager: _chainTypeManager});

chainTypeManager[_chainId] = _chainTypeManager;

Expand Down Expand Up @@ -423,7 +419,9 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus

function ctmAssetIdFromChainId(uint256 _chainId) public view override returns (bytes32) {
address ctmAddress = chainTypeManager[_chainId];
require(ctmAddress != address(0), "chain id not registered");
if (ctmAddress == address(0)) {
revert ChainIdNotRegistered(_chainId);
}
return ctmAssetId(chainTypeManager[_chainId]);
}

Expand Down Expand Up @@ -786,6 +784,78 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
});
}

/// @dev Registers an already deployed chain with the bridgehub
/// @param _chainId The chain Id of the chain
/// @param _zkChain Address of the zkChain
function registerAlreadyDeployedZKChain(uint256 _chainId, address _zkChain) external onlyOwner onlyL1 {
if (_zkChain == address(0)) {
revert ZeroAddress();
}
if (zkChainMap.contains(_chainId)) {
revert ChainIdAlreadyExists();
}
if (IZKChain(_zkChain).getChainId() != _chainId) {
revert ChainIdMismatch();
}

address ctm = IZKChain(_zkChain).getChainTypeManager();
address chainAdmin = IZKChain(_zkChain).getAdmin();
bytes32 chainBaseTokenAssetId = IZKChain(_zkChain).getBaseTokenAssetId();
address bridgeHub = IZKChain(_zkChain).getBridgehub();

if (bridgeHub != address(this)) {
revert IncorrectBridgeHubAddress(bridgeHub);
}

_validateChainParams({_chainId: _chainId, _assetId: chainBaseTokenAssetId, _chainTypeManager: ctm});

chainTypeManager[_chainId] = ctm;

baseTokenAssetId[_chainId] = chainBaseTokenAssetId;
settlementLayer[_chainId] = block.chainid;

_registerNewZKChain(_chainId, _zkChain);
messageRoot.addNewChain(_chainId);

emit NewChain(_chainId, ctm, chainAdmin);
}

function _validateChainParams(uint256 _chainId, bytes32 _assetId, address _chainTypeManager) internal view {
if (_chainId == 0) {
revert ZeroChainId();
}

if (_chainId > type(uint48).max) {
revert ChainIdTooBig();
}

if (_chainId == block.chainid) {
revert ChainIdCantBeCurrentChain();
}

if (_chainTypeManager == address(0)) {
revert ZeroAddress();
}
if (_assetId == bytes32(0)) {
revert EmptyAssetId();
}

if (!chainTypeManagerIsRegistered[_chainTypeManager]) {
revert CTMNotRegistered();
}

if (!assetIdIsRegistered[_assetId]) {
revert AssetIdNotSupported(_assetId);
}

if (assetRouter == address(0)) {
revert SharedBridgeNotSet();
}
if (chainTypeManager[_chainId] != address(0)) {
revert BridgeHubAlreadyRegistered();
}
}

/*//////////////////////////////////////////////////////////////
PAUSE
//////////////////////////////////////////////////////////////*/
Expand Down
4 changes: 4 additions & 0 deletions l1-contracts/contracts/bridgehub/IBridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ interface IBridgehub is IAssetHandler, IL1AssetHandler {

function migrationPaused() external view returns (bool);

function admin() external view returns (address);

/// Mailbox forwarder

function proveL2MessageInclusion(
Expand Down Expand Up @@ -224,4 +226,6 @@ interface IBridgehub is IAssetHandler, IL1AssetHandler {
function L1_CHAIN_ID() external view returns (uint256);

function setLegacyBaseTokenAssetId(uint256 _chainId) external;

function registerAlreadyDeployedZKChain(uint256 _chainId, address _hyperchain) external;
}
22 changes: 20 additions & 2 deletions l1-contracts/contracts/common/L1ContractErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ error AmountMustBeGreaterThanZero();
error AssetHandlerDoesNotExist(bytes32 assetId);
//
error AssetIdMismatch(bytes32 expected, bytes32 supplied);
//
error AssetIdAlreadyRegistered();
// 0x0bfcef28
error AlreadyWhitelisted(address);
// 0x04a0b7e9
error AssetIdNotSupported(bytes32 assetId);
// 0x6afd6c20
error BadReturnData();
// 0x6ef9a972
Expand All @@ -65,6 +69,16 @@ error CanOnlyProcessOneBatch();
error CantExecuteUnprovenBatches();
// 0xe18cb383
error CantRevertExecutedBatch();
// 0x24591d89
error ChainIdAlreadyExists();
// 0x717a1656
error ChainIdCantBeCurrentChain();
// 0xa179f8c9
error ChainIdMismatch();
//
error ChainIdNotRegistered(uint256 chainId);
//
error ChainNotLegacy();
// 0x78d2ed02
error ChainAlreadyLive();
// 0x8f620a06
Expand All @@ -91,6 +105,8 @@ error DiamondFreezeIncorrectState();
error DiamondNotFrozen();
//
error EmptyAddress();
// 0x2d4d012f
error EmptyAssetId();
// 0xfc7ab1d3
error EmptyBlobVersionHash(uint256 index);
//
Expand Down Expand Up @@ -124,6 +140,8 @@ error HashMismatch(bytes32 expected, bytes32 actual);
error ZKChainLimitReached();
//
error InsufficientAllowance(uint256 providedAllowance, uint256 requiredAmount);
// 0xdd381a4c
error IncorrectBridgeHubAddress(address bridgehub);
// 0x826fb11e
error InsufficientChainBalance();
// 0x356680b7
Expand Down Expand Up @@ -194,6 +212,8 @@ error MerkleIndexOutOfBounds();
error MerklePathEmpty();
// 0x1c500385
error MerklePathOutOfBounds();
//
error MigrationPaused();
// 0xfa44b527
error MissingSystemLogs(uint256 expected, uint256 actual);
// 0x4a094431
Expand Down Expand Up @@ -385,8 +405,6 @@ error IncorrectBatchBounds(
uint256 processFromProvided,
uint256 processToProvided
);
// 0x04a0b7e9
error AssetIdNotSupported(bytes32 assetId);
// 0x64107968
error AssetHandlerNotRegistered(bytes32 assetId);

Expand Down
5 changes: 2 additions & 3 deletions l1-contracts/deploy-scripts/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ pragma solidity 0.8.24;

import {Vm} from "forge-std/Vm.sol";

import {Bridgehub} from "contracts/bridgehub/Bridgehub.sol";
import {L2TransactionRequestDirect} from "contracts/bridgehub/IBridgehub.sol";
import {L2TransactionRequestDirect, IBridgehub} from "contracts/bridgehub/IBridgehub.sol";
import {IGovernance} from "contracts/governance/IGovernance.sol";
import {IERC20} from "@openzeppelin/contracts-v4/token/ERC20/IERC20.sol";
import {Ownable} from "@openzeppelin/contracts-v4/access/Ownable.sol";
Expand Down Expand Up @@ -232,7 +231,7 @@ library Utils {
address bridgehubAddress,
address l1SharedBridgeProxy
) internal {
Bridgehub bridgehub = Bridgehub(bridgehubAddress);
IBridgehub bridgehub = IBridgehub(bridgehubAddress);
uint256 gasPrice = bytesToUint256(vm.rpc("eth_gasPrice", "[]"));

uint256 requiredValueToDeploy = bridgehub.l2TransactionBaseCost(
Expand Down
Loading

0 comments on commit 7dc5166

Please sign in to comment.