Skip to content

Commit

Permalink
Merge pull request #6 from matter-labs/sb-use-l2-chain-admin
Browse files Browse the repository at this point in the history
Use separate l2 chain admin
  • Loading branch information
StanislavBreadless authored Nov 21, 2024
2 parents 5599678 + e864827 commit 660ac5e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
83 changes: 71 additions & 12 deletions l1-contracts/deploy-scripts/GatewayPreparation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import {ETH_TOKEN_ADDRESS} from "contracts/common/Config.sol";
import {DataEncoding} from "contracts/common/libraries/DataEncoding.sol";
import {IAdmin} from "contracts/state-transition/chain-interfaces/IAdmin.sol";
import {FinalizeL1DepositParams} from "contracts/bridge/interfaces/IL1Nullifier.sol";
import {AccessControlRestriction} from "contracts/governance/AccessControlRestriction.sol";
import {L2ContractsBytecodesLib} from "./L2ContractsBytecodesLib.sol";
import {ChainAdmin} from "contracts/governance/ChainAdmin.sol";
import {Call} from "contracts/governance/Common.sol";

import {IChainTypeManager} from "contracts/state-transition/IChainTypeManager.sol";

Expand Down Expand Up @@ -68,6 +72,7 @@ contract GatewayPreparation is Script {

struct Output {
bytes32 governanceL2TxHash;
address l2ChainAdminAddress;
address gatewayTransactionFiltererImplementation;
address gatewayTransactionFiltererProxy;
}
Expand Down Expand Up @@ -121,14 +126,27 @@ contract GatewayPreparation is Script {
output.gatewayTransactionFiltererImplementation
);
vm.serializeAddress("root", "gateway_transaction_filterer_proxy", output.gatewayTransactionFiltererProxy);
vm.serializeAddress("root", "l2_chain_admin_address", output.l2ChainAdminAddress);
string memory toml = vm.serializeBytes32("root", "governance_l2_tx_hash", output.governanceL2TxHash);
string memory path = string.concat(vm.projectRoot(), "/script-out/output-gateway-preparation-l1.toml");
vm.writeToml(toml, path);
}

function saveOutput(address l2ChainAdminAddress) internal {
Output memory output = Output({
governanceL2TxHash: bytes32(0),
l2ChainAdminAddress: l2ChainAdminAddress,
gatewayTransactionFiltererImplementation: address(0),
gatewayTransactionFiltererProxy: address(0)
});

saveOutput(output);
}

function saveOutput(bytes32 governanceL2TxHash) internal {
Output memory output = Output({
governanceL2TxHash: governanceL2TxHash,
l2ChainAdminAddress: address(0),
gatewayTransactionFiltererImplementation: address(0),
gatewayTransactionFiltererProxy: address(0)
});
Expand All @@ -142,6 +160,7 @@ contract GatewayPreparation is Script {
) internal {
Output memory output = Output({
governanceL2TxHash: bytes32(0),
l2ChainAdminAddress: address(0),
gatewayTransactionFiltererImplementation: gatewayTransactionFiltererImplementation,
gatewayTransactionFiltererProxy: gatewayTransactionFiltererProxy
});
Expand Down Expand Up @@ -250,8 +269,31 @@ contract GatewayPreparation is Script {
saveOutput(l2TxHash);
}

function deployL2ChainAdmin() public {
initializeConfig();

// FIXME: it is deployed without any restrictions.
address l2ChainAdminAddress = Utils.deployThroughL1({
bytecode: L2ContractsBytecodesLib.readChainAdminBytecode(),
constructorargs: abi.encode(new address[](0)),
create2salt: bytes32(0),
l2GasLimit: Utils.MAX_PRIORITY_TX_GAS,
factoryDeps: new bytes[](0),
chainId: config.gatewayChainId,
bridgehubAddress: config.bridgehub,
l1SharedBridgeProxy: config.sharedBridgeProxy
});

saveOutput(l2ChainAdminAddress);
}

/// @dev Calling this function requires private key to the admin of the chain
function migrateChainToGateway(address chainAdmin, address accessControlRestriction, uint256 chainId) public {
function migrateChainToGateway(
address chainAdmin,
address l2ChainAdmin,
address accessControlRestriction,
uint256 chainId
) public {
initializeConfig();

IBridgehub bridgehubContract = IBridgehub(config.bridgehub);
Expand Down Expand Up @@ -281,9 +323,6 @@ contract GatewayPreparation is Script {

console.log("Chain Admin address:", chainAdmin);

// TODO(EVM-746): Use L2-based chain admin contract
address l2ChainAdmin = AddressAliasHelper.applyL1ToL2Alias(chainAdmin);

bytes32 chainAssetId = IBridgehub(config.bridgehub).ctmAssetIdFromChainId(chainId);

uint256 currentSettlementLayer = IBridgehub(config.bridgehub).settlementLayer(chainId);
Expand Down Expand Up @@ -324,6 +363,7 @@ contract GatewayPreparation is Script {
function startMigrateChainFromGateway(
address chainAdmin,
address accessControlRestriction,
address l2ChainAdmin,
uint256 chainId
) public {
initializeConfig();
Expand All @@ -346,15 +386,26 @@ contract GatewayPreparation is Script {

bytes32 ctmAssetId = bridgehub.ctmAssetIdFromChainId(chainId);
L2AssetRouter l2AssetRouter = L2AssetRouter(L2_ASSET_ROUTER_ADDR);
bytes memory l2Calldata = abi.encodeCall(IL2AssetRouter.withdraw, (ctmAssetId, bridgehubBurnData));

bytes memory l2Calldata;

{
bytes memory data = abi.encodeCall(IL2AssetRouter.withdraw, (ctmAssetId, bridgehubBurnData));

Call[] memory calls = new Call[](1);
calls[0] = Call({target: L2_ASSET_ROUTER_ADDR, value: 0, data: data});

l2Calldata = abi.encodeCall(ChainAdmin.multicall, (calls, true));
}
// FIXME: this should migrate to use L2 transactions directly
bytes32 l2TxHash = Utils.runAdminL1L2DirectTransaction(
_getL1GasPrice(),
chainAdmin,
accessControlRestriction,
l2Calldata,
Utils.MAX_PRIORITY_TX_GAS,
new bytes[](0),
L2_ASSET_ROUTER_ADDR,
l2ChainAdmin,
config.gatewayChainId,
config.bridgehub,
config.sharedBridgeProxy
Expand Down Expand Up @@ -398,7 +449,8 @@ contract GatewayPreparation is Script {
uint256 chainId,
address l1DAValidator,
address l2DAValidator,
address chainDiamondProxyOnGateway
address chainDiamondProxyOnGateway,
address chainAdminOnGateway
) public {
initializeConfig();

Expand All @@ -408,10 +460,10 @@ contract GatewayPreparation is Script {
_getL1GasPrice(),
chainAdmin,
accessControlRestriction,
data,
_callL2AdminCalldata(data, chainDiamondProxyOnGateway),
Utils.MAX_PRIORITY_TX_GAS,
new bytes[](0),
chainDiamondProxyOnGateway,
chainAdminOnGateway,
config.gatewayChainId,
config.bridgehub,
config.sharedBridgeProxy
Expand All @@ -425,7 +477,8 @@ contract GatewayPreparation is Script {
address accessControlRestriction,
uint256 chainId,
address validatorAddress,
address gatewayValidatorTimelock
address gatewayValidatorTimelock,
address chainAdminOnGateway
) public {
initializeConfig();

Expand All @@ -435,10 +488,10 @@ contract GatewayPreparation is Script {
_getL1GasPrice(),
chainAdmin,
accessControlRestriction,
data,
_callL2AdminCalldata(data, gatewayValidatorTimelock),
Utils.MAX_PRIORITY_TX_GAS,
new bytes[](0),
gatewayValidatorTimelock,
chainAdminOnGateway,
config.gatewayChainId,
config.bridgehub,
config.sharedBridgeProxy
Expand All @@ -447,6 +500,12 @@ contract GatewayPreparation is Script {
saveOutput(l2TxHash);
}

function _callL2AdminCalldata(bytes memory _data, address _target) private returns (bytes memory adminCalldata) {
Call[] memory calls = new Call[](1);
calls[0] = Call({target: _target, value: 0, data: _data});
adminCalldata = abi.encodeCall(ChainAdmin.multicall, (calls, true));
}

/// TODO(EVM-748): make that function support non-ETH based chains
function supplyGatewayWallet(address addr, uint256 amount) public {
initializeConfig();
Expand Down
6 changes: 6 additions & 0 deletions l1-contracts/deploy-scripts/L2ContractsBytecodesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,10 @@ library L2ContractsBytecodesLib {
function readTimestampAsserterBytecode() internal view returns (bytes memory) {
return Utils.readZKFoundryBytecodeL2("TimestampAsserter.sol", "TimestampAsserter");
}

/// @notice Reads the bytecode of the ChainAdmin contract.
/// @return The bytecode of the ChainAdmin contract.
function readChainAdminBytecode() internal view returns (bytes memory) {
return Utils.readZKFoundryBytecodeL1("ChainAdmin.sol", "ChainAdmin");
}
}
5 changes: 5 additions & 0 deletions l1-contracts/test/foundry/l1/integration/L1GatewayTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ contract L1GatewayTests is L1ContractDeployer, ZKChainDeployer, TokenDeployer, L
_setUpGatewayWithFilterer();
gatewayScript.migrateChainToGateway(
migratingChain.getAdmin(),
address(1),
_extractAccessControlRestriction(migratingChain.getAdmin()),
migratingChainId
);
Expand All @@ -138,6 +139,7 @@ contract L1GatewayTests is L1ContractDeployer, ZKChainDeployer, TokenDeployer, L
_setUpGatewayWithFilterer();
gatewayScript.migrateChainToGateway(
migratingChain.getAdmin(),
address(1),
_extractAccessControlRestriction(migratingChain.getAdmin()),
migratingChainId
);
Expand All @@ -149,6 +151,7 @@ contract L1GatewayTests is L1ContractDeployer, ZKChainDeployer, TokenDeployer, L
_setUpGatewayWithFilterer();
gatewayScript.migrateChainToGateway(
migratingChain.getAdmin(),
address(1),
_extractAccessControlRestriction(migratingChain.getAdmin()),
migratingChainId
);
Expand All @@ -170,6 +173,7 @@ contract L1GatewayTests is L1ContractDeployer, ZKChainDeployer, TokenDeployer, L
_setUpGatewayWithFilterer();
gatewayScript.migrateChainToGateway(
migratingChain.getAdmin(),
address(1),
_extractAccessControlRestriction(migratingChain.getAdmin()),
migratingChainId
);
Expand Down Expand Up @@ -249,6 +253,7 @@ contract L1GatewayTests is L1ContractDeployer, ZKChainDeployer, TokenDeployer, L
_setUpGatewayWithFilterer();
gatewayScript.migrateChainToGateway(
migratingChain.getAdmin(),
address(1),
_extractAccessControlRestriction(migratingChain.getAdmin()),
migratingChainId
);
Expand Down

0 comments on commit 660ac5e

Please sign in to comment.