From 33c2a00ccd676965dbe3788451a2a6d4419f45d9 Mon Sep 17 00:00:00 2001 From: zkJoaquin Date: Thu, 18 Apr 2024 19:15:46 +0800 Subject: [PATCH 1/3] add SyncL2TxHashRelayer --- .../dev-contracts/SyncL2TxHashRelayer.sol | 65 +++++++ hardhat.config.js | 1 + script/deploy_log_name.js | 12 ++ script/deploy_sync_l2_txHash_relayer.js | 167 ++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 contracts/dev-contracts/SyncL2TxHashRelayer.sol create mode 100644 script/deploy_sync_l2_txHash_relayer.js diff --git a/contracts/dev-contracts/SyncL2TxHashRelayer.sol b/contracts/dev-contracts/SyncL2TxHashRelayer.sol new file mode 100644 index 0000000..edfc98d --- /dev/null +++ b/contracts/dev-contracts/SyncL2TxHashRelayer.sol @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 + +pragma solidity ^0.8.0; + +import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; +import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; +import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol"; + +import {IL1Gateway} from "../interfaces/IL1Gateway.sol"; +import {IArbitrator} from "../interfaces/IArbitrator.sol"; +import {IZkLink} from "../interfaces/IZkLink.sol"; + +contract SyncL2TxHashRelayer is OwnableUpgradeable, UUPSUpgradeable, ReentrancyGuardUpgradeable { + /// @dev The address of the primary chain message service + address public immutable PRIMARY_CHAIN_MESSAGE_SERVICE; + + /// @dev The address of the arbitrator contract + IArbitrator public immutable ARBITRATOR; + + /// @dev The gateway for sending message from ethereum to primary chain + IL1Gateway public immutable PRIMARY_CHAIN_GATEWAY; + + constructor(address _primaryChainMessageService, IArbitrator _arbitrator, IL1Gateway _primaryChainGateway) { + _disableInitializers(); + + PRIMARY_CHAIN_MESSAGE_SERVICE = _primaryChainMessageService; + ARBITRATOR = _arbitrator; + PRIMARY_CHAIN_GATEWAY = _primaryChainGateway; + } + + function initialize() external initializer { + __Ownable_init_unchained(); + __UUPSUpgradeable_init_unchained(); + __ReentrancyGuard_init_unchained(); + } + + function _authorizeUpgrade(address newImplementation) internal override onlyOwner { + // can only call by owner + } + + function claimPrimaryChainSyncL2TxHashMessage( + address _sourceChainCanonicalMessageService, + bytes calldata _sourceChainClaimCallData, + address _secondaryChainGateway, + bytes32 _canonicalTxHash, + bytes32 _l2TxHash, + bytes calldata _forwardParams + ) external payable nonReentrant { + require(_sourceChainCanonicalMessageService == PRIMARY_CHAIN_MESSAGE_SERVICE, "INVALID_MESSAGE_SERVICE"); + + // Send l2 tx hash to secondary chain by gateway + bytes[] memory gatewayDataList = new bytes[](1); + bytes memory callData = abi.encodeCall(IZkLink.syncL2TxHash, (_canonicalTxHash, _l2TxHash)); + gatewayDataList[0] = abi.encode(_secondaryChainGateway, 0, callData); + + ARBITRATOR.claimMessage{value: msg.value}( + _sourceChainCanonicalMessageService, + _sourceChainClaimCallData, + PRIMARY_CHAIN_GATEWAY, + 0, + abi.encode(gatewayDataList), + _forwardParams + ); + } +} diff --git a/hardhat.config.js b/hardhat.config.js index d03d352..e8428d6 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -9,6 +9,7 @@ require('./script/deploy_erc20_bridge'); require('./script/deploy_governance'); require('./script/deploy_linea_l2_governance'); require('./script/deploy_zklink_token'); +require('./script/deploy_sync_l2_txHash_relayer'); const BaseConfig = require('./hardhat.base.config'); diff --git a/script/deploy_log_name.js b/script/deploy_log_name.js index cef26ec..62a67fb 100644 --- a/script/deploy_log_name.js +++ b/script/deploy_log_name.js @@ -33,6 +33,13 @@ const DEPLOY_LOG_ARBITRATOR_VERIFIED = 'arbitratorVerified'; const DEPLOY_LOG_ARBITRATOR_TARGET = 'arbitratorTarget'; const DEPLOY_LOG_ARBITRATOR_TARGET_VERIFIED = 'arbitratorTargetVerified'; +// consumed in deploy_sync_l2_txHash_relayer.js +const DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX = 'deploy_syncL2TxHashRelayer'; +const DEPLOY_LOG_SYNCL2TXHASHRELAYER = 'syncL2TxHashRelayer'; +const DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED = 'syncL2TxHashRelayerVerified'; +const DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET = 'syncL2TxHashRelayerTarget'; +const DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET_VERIFIED = 'syncL2TxHashRelayerTargetVerified'; + // consumed in deploy_governance.js const DEPLOY_GOVERNANCE_LOG_PREFIX = 'deploy_governance'; const DEPLOY_LOG_GOVERNANCE = 'governance'; @@ -85,4 +92,9 @@ module.exports = { DEPLOY_LOG_ZKLINK_TOKEN_TARGET, DEPLOY_LOG_ZKLINK_TOKEN_PROXY_VERIFIED, DEPLOY_LOG_ZKLINK_TOKEN_TARGET_VERIFIED, + DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX, + DEPLOY_LOG_SYNCL2TXHASHRELAYER, + DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED, + DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET, + DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET_VERIFIED, }; diff --git a/script/deploy_sync_l2_txHash_relayer.js b/script/deploy_sync_l2_txHash_relayer.js new file mode 100644 index 0000000..e89eb34 --- /dev/null +++ b/script/deploy_sync_l2_txHash_relayer.js @@ -0,0 +1,167 @@ +const fs = require('fs'); +const { getImplementationAddress } = require('@openzeppelin/upgrades-core'); +const { + verifyContractCode, + createOrGetDeployLog, + ChainContractDeployer, + getDeployTx, + readDeployContract, + readDeployLogField, + getLogName, +} = require('./utils'); +const logName = require('./deploy_log_name'); +const { task, types } = require('hardhat/config'); + +function getRelayerContractName() { + return 'SyncL2TxHashRelayer'; +} + +task('deploySyncL2TxHashRelayer', 'Deploy SyncL2TxHashRelayer') + .addParam('messageService', 'The primary chain message service', undefined, types.string, false) + .addParam('l1Gateway', 'The primary chain l1 gateway', undefined, types.string, false) + .addParam( + 'arbitrator', + 'The arbitrator address (default get from arbitrator deploy log)', + undefined, + types.string, + true, + ) + .addParam('skipVerify', 'Skip verify', false, types.boolean, true) + .setAction(async (taskArgs, hardhat) => { + let arbitrator = taskArgs.arbitrator; + if (arbitrator === undefined) { + arbitrator = readDeployLogField(logName.DEPLOY_ARBITRATOR_LOG_PREFIX, logName.DEPLOY_LOG_ARBITRATOR); + } + let l1Gateway = taskArgs.l1Gateway; + let messageService = taskArgs.messageService; + let skipVerify = taskArgs.skipVerify; + console.log('arbitrator', arbitrator); + console.log('primary chain l1 gateway', l1Gateway); + console.log('message service', messageService); + console.log('skip verify contracts?', skipVerify); + + const contractDeployer = new ChainContractDeployer(hardhat); + await contractDeployer.init(); + const deployerWallet = contractDeployer.deployerWallet; + + const { deployLogPath, deployLog } = createOrGetDeployLog(logName.DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX); + deployLog[logName.DEPLOY_LOG_DEPLOYER] = deployerWallet.address; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + + // deploy syncL2TxHashRelayer + let syncL2TxHashRelayerAddr; + if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER in deployLog)) { + console.log('deploy syncL2TxHashRelayer...'); + const contractName = getRelayerContractName(); + const contract = await contractDeployer.deployProxy( + contractName, + [], + [messageService, arbitrator, l1Gateway], + ); + const transaction = await getDeployTx(contract); + syncL2TxHashRelayerAddr = await contract.getAddress(); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER] = syncL2TxHashRelayerAddr; + deployLog[logName.DEPLOY_LOG_DEPLOY_TX_HASH] = transaction.hash; + deployLog[logName.DEPLOY_LOG_DEPLOY_BLOCK_NUMBER] = transaction.blockNumber; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } else { + syncL2TxHashRelayerAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER]; + } + console.log('syncL2TxHashRelayer', syncL2TxHashRelayerAddr); + + let syncL2TxHashRelayerTargetAddr; + if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET in deployLog)) { + console.log('get syncL2TxHashRelayer target...'); + syncL2TxHashRelayerTargetAddr = await getImplementationAddress(hardhat.ethers.provider, syncL2TxHashRelayerAddr); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET] = syncL2TxHashRelayerTargetAddr; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } else { + syncL2TxHashRelayerTargetAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET]; + } + console.log('syncL2TxHashRelayer target', syncL2TxHashRelayerTargetAddr); + + // verify target contract + if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED in deployLog) && !skipVerify) { + await verifyContractCode(hardhat, syncL2TxHashRelayerTargetAddr, [messageService, arbitrator, l1Gateway]); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED] = true; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } + + // verify proxy contract + if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED in deployLog) && !skipVerify) { + await verifyContractCode(hardhat, syncL2TxHashRelayerAddr, []); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED] = true; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } + }); + +task('upgradeSyncL2TxHashRelayer', 'Upgrade syncL2TxHashRelayer') + .addParam('messageService', 'The primary chain message service', undefined, types.string, false) + .addParam('l1Gateway', 'The primary chain l1 gateway', undefined, types.string, false) + .addParam( + 'arbitrator', + 'The arbitrator address (default get from arbitrator deploy log)', + undefined, + types.string, + true, + ) + .addParam('skipVerify', 'Skip verify', false, types.boolean, true) + .setAction(async (taskArgs, hardhat) => { + let arbitrator = taskArgs.arbitrator; + if (arbitrator === undefined) { + arbitrator = readDeployLogField(logName.DEPLOY_ARBITRATOR_LOG_PREFIX, logName.DEPLOY_LOG_ARBITRATOR); + } + let l1Gateway = taskArgs.l1Gateway; + let messageService = taskArgs.messageService; + let skipVerify = taskArgs.skipVerify; + console.log('arbitrator', arbitrator); + console.log('primary chain l1 gateway', l1Gateway); + console.log('message service', messageService); + console.log('skip verify contracts?', skipVerify); + + const l1GatewayLogName = getLogName(logName.DEPLOY_L1_GATEWAY_LOG_PREFIX, targetNetwork); + const l1GatewayAddr = readDeployContract(l1GatewayLogName, logName.DEPLOY_GATEWAY); + if (l1GatewayAddr === undefined) { + console.log(`${targetNetwork} l1 gateway address not exist`); + return; + } + console.log(`The ${targetNetwork} l1 gateway address: ${l1GatewayAddr}`); + + const { deployLogPath, deployLog } = createOrGetDeployLog(logName.DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX); + const contractAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER]; + if (contractAddr === undefined) { + console.log('syncL2TxHashRelayer address not exist'); + return; + } + console.log('syncL2TxHashRelayer', contractAddr); + const oldContractTargetAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET]; + if (oldContractTargetAddr === undefined) { + console.log('syncL2TxHashRelayer target address not exist'); + return; + } + console.log('syncL2TxHashRelayer old target', oldContractTargetAddr); + + const contractDeployer = new ChainContractDeployer(hardhat); + await contractDeployer.init(); + + console.log('upgrade syncL2TxHashRelayer...'); + const contractName = getRelayerContractName(); + const contract = await contractDeployer.upgradeProxy(contractName, contractAddr, [ + messageService, + arbitrator, + l1Gateway, + ]); + const tx = await getDeployTx(contract); + console.log('upgrade tx', tx.hash); + const newContractTargetAddr = await getImplementationAddress(hardhat.ethers.provider, contractAddr); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET] = newContractTargetAddr; + console.log('syncL2TxHashRelayer new target', newContractTargetAddr); + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + + // verify target contract + if (!skipVerify) { + await verifyContractCode(hardhat, newContractTargetAddr, [messageService, arbitrator, l1Gateway]); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET_VERIFIED] = true; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } + }); From 1993b959c1702bdfdf59e31244c351859e5608d9 Mon Sep 17 00:00:00 2001 From: zkJoaquin Date: Thu, 18 Apr 2024 19:34:10 +0800 Subject: [PATCH 2/3] optimise SyncL2TxHashRelayer --- contracts/dev-contracts/DummyArbitrator.sol | 2 + .../dev-contracts/SyncL2TxHashRelayer.sol | 28 +-- contracts/interfaces/IArbitrator.sol | 3 + script/deploy_log_name.js | 4 - script/deploy_sync_l2_txHash_relayer.js | 207 +++++------------- 5 files changed, 63 insertions(+), 181 deletions(-) diff --git a/contracts/dev-contracts/DummyArbitrator.sol b/contracts/dev-contracts/DummyArbitrator.sol index 161943f..e842c93 100644 --- a/contracts/dev-contracts/DummyArbitrator.sol +++ b/contracts/dev-contracts/DummyArbitrator.sol @@ -11,6 +11,8 @@ import {IL1Gateway} from "../interfaces/IL1Gateway.sol"; contract DummyArbitrator is IArbitrator, OwnableUpgradeable, UUPSUpgradeable, ReentrancyGuardUpgradeable { event ReceiveMessage(uint256 value, bytes callData); + IL1Gateway public primaryChainGateway; + function initialize() external initializer { __Ownable_init(); __UUPSUpgradeable_init(); diff --git a/contracts/dev-contracts/SyncL2TxHashRelayer.sol b/contracts/dev-contracts/SyncL2TxHashRelayer.sol index edfc98d..a2e269e 100644 --- a/contracts/dev-contracts/SyncL2TxHashRelayer.sol +++ b/contracts/dev-contracts/SyncL2TxHashRelayer.sol @@ -2,40 +2,22 @@ pragma solidity ^0.8.0; -import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; -import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; -import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol"; +import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import {IL1Gateway} from "../interfaces/IL1Gateway.sol"; import {IArbitrator} from "../interfaces/IArbitrator.sol"; import {IZkLink} from "../interfaces/IZkLink.sol"; -contract SyncL2TxHashRelayer is OwnableUpgradeable, UUPSUpgradeable, ReentrancyGuardUpgradeable { +contract SyncL2TxHashRelayer is ReentrancyGuard { /// @dev The address of the primary chain message service address public immutable PRIMARY_CHAIN_MESSAGE_SERVICE; /// @dev The address of the arbitrator contract IArbitrator public immutable ARBITRATOR; - /// @dev The gateway for sending message from ethereum to primary chain - IL1Gateway public immutable PRIMARY_CHAIN_GATEWAY; - - constructor(address _primaryChainMessageService, IArbitrator _arbitrator, IL1Gateway _primaryChainGateway) { - _disableInitializers(); - + constructor(address _primaryChainMessageService, IArbitrator _arbitrator) { PRIMARY_CHAIN_MESSAGE_SERVICE = _primaryChainMessageService; ARBITRATOR = _arbitrator; - PRIMARY_CHAIN_GATEWAY = _primaryChainGateway; - } - - function initialize() external initializer { - __Ownable_init_unchained(); - __UUPSUpgradeable_init_unchained(); - __ReentrancyGuard_init_unchained(); - } - - function _authorizeUpgrade(address newImplementation) internal override onlyOwner { - // can only call by owner } function claimPrimaryChainSyncL2TxHashMessage( @@ -46,8 +28,6 @@ contract SyncL2TxHashRelayer is OwnableUpgradeable, UUPSUpgradeable, ReentrancyG bytes32 _l2TxHash, bytes calldata _forwardParams ) external payable nonReentrant { - require(_sourceChainCanonicalMessageService == PRIMARY_CHAIN_MESSAGE_SERVICE, "INVALID_MESSAGE_SERVICE"); - // Send l2 tx hash to secondary chain by gateway bytes[] memory gatewayDataList = new bytes[](1); bytes memory callData = abi.encodeCall(IZkLink.syncL2TxHash, (_canonicalTxHash, _l2TxHash)); @@ -56,7 +36,7 @@ contract SyncL2TxHashRelayer is OwnableUpgradeable, UUPSUpgradeable, ReentrancyG ARBITRATOR.claimMessage{value: msg.value}( _sourceChainCanonicalMessageService, _sourceChainClaimCallData, - PRIMARY_CHAIN_GATEWAY, + ARBITRATOR.primaryChainGateway(), 0, abi.encode(gatewayDataList), _forwardParams diff --git a/contracts/interfaces/IArbitrator.sol b/contracts/interfaces/IArbitrator.sol index fbe7634..7e3289b 100644 --- a/contracts/interfaces/IArbitrator.sol +++ b/contracts/interfaces/IArbitrator.sol @@ -7,6 +7,9 @@ interface IArbitrator { /// @notice Return true if relayer is active function isRelayerActive(address _relayer) external view returns (bool); + /// @notice Return the primary chain gateway + function primaryChainGateway() external view returns (IL1Gateway); + /// @notice Enqueue message from EthereumGateway /// @dev Used by EthereumGateway to temporarily store message /// @param _value The msg value diff --git a/script/deploy_log_name.js b/script/deploy_log_name.js index 62a67fb..d1113e6 100644 --- a/script/deploy_log_name.js +++ b/script/deploy_log_name.js @@ -37,8 +37,6 @@ const DEPLOY_LOG_ARBITRATOR_TARGET_VERIFIED = 'arbitratorTargetVerified'; const DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX = 'deploy_syncL2TxHashRelayer'; const DEPLOY_LOG_SYNCL2TXHASHRELAYER = 'syncL2TxHashRelayer'; const DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED = 'syncL2TxHashRelayerVerified'; -const DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET = 'syncL2TxHashRelayerTarget'; -const DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET_VERIFIED = 'syncL2TxHashRelayerTargetVerified'; // consumed in deploy_governance.js const DEPLOY_GOVERNANCE_LOG_PREFIX = 'deploy_governance'; @@ -95,6 +93,4 @@ module.exports = { DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX, DEPLOY_LOG_SYNCL2TXHASHRELAYER, DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED, - DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET, - DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET_VERIFIED, }; diff --git a/script/deploy_sync_l2_txHash_relayer.js b/script/deploy_sync_l2_txHash_relayer.js index e89eb34..4a91771 100644 --- a/script/deploy_sync_l2_txHash_relayer.js +++ b/script/deploy_sync_l2_txHash_relayer.js @@ -1,167 +1,68 @@ const fs = require('fs'); -const { getImplementationAddress } = require('@openzeppelin/upgrades-core'); const { - verifyContractCode, - createOrGetDeployLog, - ChainContractDeployer, - getDeployTx, - readDeployContract, - readDeployLogField, - getLogName, + verifyContractCode, + createOrGetDeployLog, + ChainContractDeployer, + getDeployTx, + readDeployLogField, } = require('./utils'); const logName = require('./deploy_log_name'); const { task, types } = require('hardhat/config'); function getRelayerContractName() { - return 'SyncL2TxHashRelayer'; + return 'SyncL2TxHashRelayer'; } task('deploySyncL2TxHashRelayer', 'Deploy SyncL2TxHashRelayer') - .addParam('messageService', 'The primary chain message service', undefined, types.string, false) - .addParam('l1Gateway', 'The primary chain l1 gateway', undefined, types.string, false) - .addParam( - 'arbitrator', - 'The arbitrator address (default get from arbitrator deploy log)', - undefined, - types.string, - true, - ) - .addParam('skipVerify', 'Skip verify', false, types.boolean, true) - .setAction(async (taskArgs, hardhat) => { - let arbitrator = taskArgs.arbitrator; - if (arbitrator === undefined) { - arbitrator = readDeployLogField(logName.DEPLOY_ARBITRATOR_LOG_PREFIX, logName.DEPLOY_LOG_ARBITRATOR); - } - let l1Gateway = taskArgs.l1Gateway; - let messageService = taskArgs.messageService; - let skipVerify = taskArgs.skipVerify; - console.log('arbitrator', arbitrator); - console.log('primary chain l1 gateway', l1Gateway); - console.log('message service', messageService); - console.log('skip verify contracts?', skipVerify); + .addParam('messageService', 'The primary chain message service', undefined, types.string, false) + .addParam( + 'arbitrator', + 'The arbitrator address (default get from arbitrator deploy log)', + undefined, + types.string, + true, + ) + .addParam('skipVerify', 'Skip verify', false, types.boolean, true) + .setAction(async (taskArgs, hardhat) => { + let arbitrator = taskArgs.arbitrator; + if (arbitrator === undefined) { + arbitrator = readDeployLogField(logName.DEPLOY_ARBITRATOR_LOG_PREFIX, logName.DEPLOY_LOG_ARBITRATOR); + } + let messageService = taskArgs.messageService; + let skipVerify = taskArgs.skipVerify; + console.log('arbitrator', arbitrator); + console.log('message service', messageService); + console.log('skip verify contracts?', skipVerify); - const contractDeployer = new ChainContractDeployer(hardhat); - await contractDeployer.init(); - const deployerWallet = contractDeployer.deployerWallet; + const contractDeployer = new ChainContractDeployer(hardhat); + await contractDeployer.init(); + const deployerWallet = contractDeployer.deployerWallet; - const { deployLogPath, deployLog } = createOrGetDeployLog(logName.DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX); - deployLog[logName.DEPLOY_LOG_DEPLOYER] = deployerWallet.address; - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + const { deployLogPath, deployLog } = createOrGetDeployLog(logName.DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX); + deployLog[logName.DEPLOY_LOG_DEPLOYER] = deployerWallet.address; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - // deploy syncL2TxHashRelayer - let syncL2TxHashRelayerAddr; - if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER in deployLog)) { - console.log('deploy syncL2TxHashRelayer...'); - const contractName = getRelayerContractName(); - const contract = await contractDeployer.deployProxy( - contractName, - [], - [messageService, arbitrator, l1Gateway], - ); - const transaction = await getDeployTx(contract); - syncL2TxHashRelayerAddr = await contract.getAddress(); - deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER] = syncL2TxHashRelayerAddr; - deployLog[logName.DEPLOY_LOG_DEPLOY_TX_HASH] = transaction.hash; - deployLog[logName.DEPLOY_LOG_DEPLOY_BLOCK_NUMBER] = transaction.blockNumber; - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - } else { - syncL2TxHashRelayerAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER]; - } - console.log('syncL2TxHashRelayer', syncL2TxHashRelayerAddr); + // deploy syncL2TxHashRelayer + let syncL2TxHashRelayerAddr; + if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER in deployLog)) { + console.log('deploy syncL2TxHashRelayer...'); + const contractName = getRelayerContractName(); + const contract = await contractDeployer.deployContract(contractName, [messageService, arbitrator]); + const transaction = await getDeployTx(contract); + syncL2TxHashRelayerAddr = await contract.getAddress(); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER] = syncL2TxHashRelayerAddr; + deployLog[logName.DEPLOY_LOG_DEPLOY_TX_HASH] = transaction.hash; + deployLog[logName.DEPLOY_LOG_DEPLOY_BLOCK_NUMBER] = transaction.blockNumber; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } else { + syncL2TxHashRelayerAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER]; + } + console.log('syncL2TxHashRelayer', syncL2TxHashRelayerAddr); - let syncL2TxHashRelayerTargetAddr; - if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET in deployLog)) { - console.log('get syncL2TxHashRelayer target...'); - syncL2TxHashRelayerTargetAddr = await getImplementationAddress(hardhat.ethers.provider, syncL2TxHashRelayerAddr); - deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET] = syncL2TxHashRelayerTargetAddr; - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - } else { - syncL2TxHashRelayerTargetAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET]; - } - console.log('syncL2TxHashRelayer target', syncL2TxHashRelayerTargetAddr); - - // verify target contract - if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED in deployLog) && !skipVerify) { - await verifyContractCode(hardhat, syncL2TxHashRelayerTargetAddr, [messageService, arbitrator, l1Gateway]); - deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED] = true; - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - } - - // verify proxy contract - if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED in deployLog) && !skipVerify) { - await verifyContractCode(hardhat, syncL2TxHashRelayerAddr, []); - deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED] = true; - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - } - }); - -task('upgradeSyncL2TxHashRelayer', 'Upgrade syncL2TxHashRelayer') - .addParam('messageService', 'The primary chain message service', undefined, types.string, false) - .addParam('l1Gateway', 'The primary chain l1 gateway', undefined, types.string, false) - .addParam( - 'arbitrator', - 'The arbitrator address (default get from arbitrator deploy log)', - undefined, - types.string, - true, - ) - .addParam('skipVerify', 'Skip verify', false, types.boolean, true) - .setAction(async (taskArgs, hardhat) => { - let arbitrator = taskArgs.arbitrator; - if (arbitrator === undefined) { - arbitrator = readDeployLogField(logName.DEPLOY_ARBITRATOR_LOG_PREFIX, logName.DEPLOY_LOG_ARBITRATOR); - } - let l1Gateway = taskArgs.l1Gateway; - let messageService = taskArgs.messageService; - let skipVerify = taskArgs.skipVerify; - console.log('arbitrator', arbitrator); - console.log('primary chain l1 gateway', l1Gateway); - console.log('message service', messageService); - console.log('skip verify contracts?', skipVerify); - - const l1GatewayLogName = getLogName(logName.DEPLOY_L1_GATEWAY_LOG_PREFIX, targetNetwork); - const l1GatewayAddr = readDeployContract(l1GatewayLogName, logName.DEPLOY_GATEWAY); - if (l1GatewayAddr === undefined) { - console.log(`${targetNetwork} l1 gateway address not exist`); - return; - } - console.log(`The ${targetNetwork} l1 gateway address: ${l1GatewayAddr}`); - - const { deployLogPath, deployLog } = createOrGetDeployLog(logName.DEPLOY_SYNCL2TXHASHRELAYER_LOG_PREFIX); - const contractAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER]; - if (contractAddr === undefined) { - console.log('syncL2TxHashRelayer address not exist'); - return; - } - console.log('syncL2TxHashRelayer', contractAddr); - const oldContractTargetAddr = deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET]; - if (oldContractTargetAddr === undefined) { - console.log('syncL2TxHashRelayer target address not exist'); - return; - } - console.log('syncL2TxHashRelayer old target', oldContractTargetAddr); - - const contractDeployer = new ChainContractDeployer(hardhat); - await contractDeployer.init(); - - console.log('upgrade syncL2TxHashRelayer...'); - const contractName = getRelayerContractName(); - const contract = await contractDeployer.upgradeProxy(contractName, contractAddr, [ - messageService, - arbitrator, - l1Gateway, - ]); - const tx = await getDeployTx(contract); - console.log('upgrade tx', tx.hash); - const newContractTargetAddr = await getImplementationAddress(hardhat.ethers.provider, contractAddr); - deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET] = newContractTargetAddr; - console.log('syncL2TxHashRelayer new target', newContractTargetAddr); - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - - // verify target contract - if (!skipVerify) { - await verifyContractCode(hardhat, newContractTargetAddr, [messageService, arbitrator, l1Gateway]); - deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_TARGET_VERIFIED] = true; - fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); - } - }); + // verify target contract + if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED in deployLog) && !skipVerify) { + await verifyContractCode(hardhat, syncL2TxHashRelayerAddr, [messageService, arbitrator]); + deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED] = true; + fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); + } + }); From 0b25620c7c9dc28057b1c3c998f987744afe2295 Mon Sep 17 00:00:00 2001 From: zkJoaquin Date: Thu, 18 Apr 2024 19:53:31 +0800 Subject: [PATCH 3/3] del useless dependencies --- .../dev-contracts/SyncL2TxHashRelayer.sol | 23 +++++++------------ script/deploy_sync_l2_txHash_relayer.js | 7 ++---- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/contracts/dev-contracts/SyncL2TxHashRelayer.sol b/contracts/dev-contracts/SyncL2TxHashRelayer.sol index a2e269e..a747adb 100644 --- a/contracts/dev-contracts/SyncL2TxHashRelayer.sol +++ b/contracts/dev-contracts/SyncL2TxHashRelayer.sol @@ -2,36 +2,29 @@ pragma solidity ^0.8.0; -import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol"; - -import {IL1Gateway} from "../interfaces/IL1Gateway.sol"; import {IArbitrator} from "../interfaces/IArbitrator.sol"; import {IZkLink} from "../interfaces/IZkLink.sol"; -contract SyncL2TxHashRelayer is ReentrancyGuard { - /// @dev The address of the primary chain message service - address public immutable PRIMARY_CHAIN_MESSAGE_SERVICE; - +contract SyncL2TxHashRelayer { /// @dev The address of the arbitrator contract IArbitrator public immutable ARBITRATOR; - constructor(address _primaryChainMessageService, IArbitrator _arbitrator) { - PRIMARY_CHAIN_MESSAGE_SERVICE = _primaryChainMessageService; + constructor(IArbitrator _arbitrator) { ARBITRATOR = _arbitrator; } function claimPrimaryChainSyncL2TxHashMessage( address _sourceChainCanonicalMessageService, bytes calldata _sourceChainClaimCallData, - address _secondaryChainGateway, - bytes32 _canonicalTxHash, - bytes32 _l2TxHash, + address _secondaryChainL1Gateway, + bytes32 _secondaryChainL2TxHash, + bytes32 _primaryChainL2TxHash, bytes calldata _forwardParams - ) external payable nonReentrant { + ) external payable { // Send l2 tx hash to secondary chain by gateway bytes[] memory gatewayDataList = new bytes[](1); - bytes memory callData = abi.encodeCall(IZkLink.syncL2TxHash, (_canonicalTxHash, _l2TxHash)); - gatewayDataList[0] = abi.encode(_secondaryChainGateway, 0, callData); + bytes memory callData = abi.encodeCall(IZkLink.syncL2TxHash, (_secondaryChainL2TxHash, _primaryChainL2TxHash)); + gatewayDataList[0] = abi.encode(_secondaryChainL1Gateway, 0, callData); ARBITRATOR.claimMessage{value: msg.value}( _sourceChainCanonicalMessageService, diff --git a/script/deploy_sync_l2_txHash_relayer.js b/script/deploy_sync_l2_txHash_relayer.js index 4a91771..c0fa33b 100644 --- a/script/deploy_sync_l2_txHash_relayer.js +++ b/script/deploy_sync_l2_txHash_relayer.js @@ -14,7 +14,6 @@ function getRelayerContractName() { } task('deploySyncL2TxHashRelayer', 'Deploy SyncL2TxHashRelayer') - .addParam('messageService', 'The primary chain message service', undefined, types.string, false) .addParam( 'arbitrator', 'The arbitrator address (default get from arbitrator deploy log)', @@ -28,10 +27,8 @@ task('deploySyncL2TxHashRelayer', 'Deploy SyncL2TxHashRelayer') if (arbitrator === undefined) { arbitrator = readDeployLogField(logName.DEPLOY_ARBITRATOR_LOG_PREFIX, logName.DEPLOY_LOG_ARBITRATOR); } - let messageService = taskArgs.messageService; let skipVerify = taskArgs.skipVerify; console.log('arbitrator', arbitrator); - console.log('message service', messageService); console.log('skip verify contracts?', skipVerify); const contractDeployer = new ChainContractDeployer(hardhat); @@ -47,7 +44,7 @@ task('deploySyncL2TxHashRelayer', 'Deploy SyncL2TxHashRelayer') if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER in deployLog)) { console.log('deploy syncL2TxHashRelayer...'); const contractName = getRelayerContractName(); - const contract = await contractDeployer.deployContract(contractName, [messageService, arbitrator]); + const contract = await contractDeployer.deployContract(contractName, [arbitrator]); const transaction = await getDeployTx(contract); syncL2TxHashRelayerAddr = await contract.getAddress(); deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER] = syncL2TxHashRelayerAddr; @@ -61,7 +58,7 @@ task('deploySyncL2TxHashRelayer', 'Deploy SyncL2TxHashRelayer') // verify target contract if (!(logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED in deployLog) && !skipVerify) { - await verifyContractCode(hardhat, syncL2TxHashRelayerAddr, [messageService, arbitrator]); + await verifyContractCode(hardhat, syncL2TxHashRelayerAddr, [arbitrator]); deployLog[logName.DEPLOY_LOG_SYNCL2TXHASHRELAYER_VERIFIED] = true; fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2)); }