From a0505a876cd7494d37a8850e93a01b79c75eabb3 Mon Sep 17 00:00:00 2001 From: zkbenny Date: Tue, 5 Dec 2023 19:12:45 +0800 Subject: [PATCH] add txNonce to bridge message --- contracts/gateway/LineaL1Gateway.sol | 16 +++++++++------- contracts/gateway/LineaL2Gateway.sol | 8 ++++---- contracts/interfaces/ILineaL1Gateway.sol | 2 +- contracts/interfaces/ILineaL2Gateway.sol | 9 +++++---- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/contracts/gateway/LineaL1Gateway.sol b/contracts/gateway/LineaL1Gateway.sol index 41ad73f..b5d8f53 100644 --- a/contracts/gateway/LineaL1Gateway.sol +++ b/contracts/gateway/LineaL1Gateway.sol @@ -16,19 +16,20 @@ contract LineaL1Gateway is ZkLinkAcceptor, LineaGateway, ILineaL1Gateway { uint64 public fee; /// @notice Used to prevent off-chain monitoring events from being lost - uint192 public txNonce; + uint32 public txNonce; function depositETH(bytes32 _zkLinkAddress, uint8 _subAccountId) external payable override nonReentrant whenNotPaused { // ensure amount bridged is not zero require(msg.value > fee, "Value too low"); uint256 amount = msg.value - fee; - bytes memory callData = abi.encodeCall(ILineaL2Gateway.claimETHCallback, (_zkLinkAddress, _subAccountId, amount)); + uint32 _txNonce = txNonce; + bytes memory callData = abi.encodeCall(ILineaL2Gateway.claimETHCallback, (_txNonce, _zkLinkAddress, _subAccountId, amount)); // transfer no fee to Linea messageService.sendMessage{value: amount}(remoteGateway, 0, callData); - emit Deposit(txNonce, ETH_ADDRESS, amount, _zkLinkAddress, _subAccountId, false); - txNonce++; + emit Deposit(_txNonce, ETH_ADDRESS, amount, _zkLinkAddress, _subAccountId, false); + txNonce = _txNonce + 1; } function depositERC20(address _token, uint256 _amount, bytes32 _zkLinkAddress, uint8 _subAccountId, bool _mapping) external payable override nonReentrant whenNotPaused { @@ -47,11 +48,12 @@ contract LineaL1Gateway is ZkLinkAcceptor, LineaGateway, ILineaL1Gateway { (bool isUSDC, address nativeToken) = bridgeERC20ToRemoteGateway(_token, _amount, 0); // send depositERC20 command to LineaL2Gateway(the second message send to Linea) - bytes memory executeData = abi.encodeCall(ILineaL2Gateway.claimERC20Callback, (isUSDC, nativeToken, _amount, _zkLinkAddress, _subAccountId, _mapping)); + uint32 _txNonce = txNonce; + bytes memory executeData = abi.encodeCall(ILineaL2Gateway.claimERC20Callback, (_txNonce, isUSDC, nativeToken, _amount, _zkLinkAddress, _subAccountId, _mapping)); messageService.sendMessage(remoteGateway, 0, executeData); - emit Deposit(txNonce, _token, _amount, _zkLinkAddress, _subAccountId, _mapping); - txNonce++; + emit Deposit(_txNonce, _token, _amount, _zkLinkAddress, _subAccountId, _mapping); + txNonce = _txNonce + 1; } function claimETHCallback(address _owner, uint128 _amount, uint32 _accountIdOfNonce, uint8 _subAccountIdOfNonce, uint32 _nonce, uint16 _fastWithdrawFeeRate) external payable override onlyMessageService onlyRemoteGateway { diff --git a/contracts/gateway/LineaL2Gateway.sol b/contracts/gateway/LineaL2Gateway.sol index dfcd397..c77bb01 100644 --- a/contracts/gateway/LineaL2Gateway.sol +++ b/contracts/gateway/LineaL2Gateway.sol @@ -21,14 +21,14 @@ contract LineaL2Gateway is LineaGateway, ILineaL2Gateway { _; } - function claimETHCallback(bytes32 _zkLinkAddress, uint8 _subAccountId, uint256 _amount) external payable onlyMessageService onlyRemoteGateway { + function claimETHCallback(uint32 _txNonce, bytes32 _zkLinkAddress, uint8 _subAccountId, uint256 _amount) external payable onlyMessageService onlyRemoteGateway { require(msg.value == _amount, "Claim eth value not match"); zkLink.depositETH{value: _amount}(_zkLinkAddress, _subAccountId); - emit ClaimedDepositETH(_zkLinkAddress, _subAccountId, _amount); + emit ClaimedDeposit(_txNonce); } - function claimERC20Callback(bool _isUSDC, address _nativeToken, uint256 _amount, bytes32 _zkLinkAddress, uint8 _subAccountId, bool _mapping) external override onlyMessageService onlyRemoteGateway { + function claimERC20Callback(uint32 _txNonce, bool _isUSDC, address _nativeToken, uint256 _amount, bytes32 _zkLinkAddress, uint8 _subAccountId, bool _mapping) external override onlyMessageService onlyRemoteGateway { // find target token on Linea address targetToken = getTargetToken(_isUSDC, _nativeToken); // approve token to zkLink @@ -36,7 +36,7 @@ contract LineaL2Gateway is LineaGateway, ILineaL2Gateway { // deposit erc20 to zkLink uint104 amount = uint104(_amount); zkLink.depositERC20(IERC20(targetToken), amount, _zkLinkAddress, _subAccountId, _mapping); - emit ClaimedDepositERC20(targetToken, amount, _zkLinkAddress, _subAccountId, _mapping); + emit ClaimedDeposit(_txNonce); } function withdrawETH(address _owner, uint128 _amount, uint32 _accountIdOfNonce, uint8 _subAccountIdOfNonce, uint32 _nonce, uint16 _fastWithdrawFeeRate) external payable override onlyZkLink whenNotPaused { diff --git a/contracts/interfaces/ILineaL1Gateway.sol b/contracts/interfaces/ILineaL1Gateway.sol index 1bd3bce..1847c9b 100644 --- a/contracts/interfaces/ILineaL1Gateway.sol +++ b/contracts/interfaces/ILineaL1Gateway.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.0; import {ILineaGateway} from "./ILineaGateway.sol"; interface ILineaL1Gateway is ILineaGateway { - event Deposit(uint192 indexed txNonce, address token, uint256 amount, bytes32 zklinkAddress, uint8 subAccountId, bool _mapping); + event Deposit(uint32 indexed txNonce, address token, uint256 amount, bytes32 zklinkAddress, uint8 subAccountId, bool _mapping); event ClaimedWithdrawETH(address _receiver, uint256 _amount); event ClaimedWithdrawERC20(address _receiver, address _token, uint256 _amount); event SetFee(uint64 fee); diff --git a/contracts/interfaces/ILineaL2Gateway.sol b/contracts/interfaces/ILineaL2Gateway.sol index 50525a0..3e45923 100644 --- a/contracts/interfaces/ILineaL2Gateway.sol +++ b/contracts/interfaces/ILineaL2Gateway.sol @@ -5,22 +5,23 @@ import {IL2Gateway} from "./IL2Gateway.sol"; import {ILineaGateway} from "./ILineaGateway.sol"; interface ILineaL2Gateway is ILineaGateway, IL2Gateway { - event ClaimedDepositETH(bytes32 _zkLinkAddress, uint8 _subAccountId, uint256 _amount); - event ClaimedDepositERC20(address _token, uint104 _amount, bytes32 _zkLinkAddress, uint8 _subAccountId, bool _mapping); + event ClaimedDeposit(uint32 indexed _txNonce); event SetZkLink(address _zkLink); /// @notice Claim ETH callback from message service + /// @param _txNonce The deposit sequence of L1 gateway /// @param _zkLinkAddress The zkLink address deposited to /// @param _subAccountId The sub account id /// @param _amount The eth amount to deposit - function claimETHCallback(bytes32 _zkLinkAddress, uint8 _subAccountId, uint256 _amount) external payable; + function claimETHCallback(uint32 _txNonce, bytes32 _zkLinkAddress, uint8 _subAccountId, uint256 _amount) external payable; /// @notice Claim ERC20 callback from message service + /// @param _txNonce The deposit sequence of L1 gateway /// @param _isUSDC True when the native token is usdc /// @param _nativeToken The native token /// @param _amount The amount to deposit /// @param _zkLinkAddress The zkLink address deposited to /// @param _subAccountId The sub account id /// @param _mapping If receive a mapping token on zkLink - function claimERC20Callback(bool _isUSDC, address _nativeToken, uint256 _amount, bytes32 _zkLinkAddress, uint8 _subAccountId, bool _mapping) external; + function claimERC20Callback(uint32 _txNonce, bool _isUSDC, address _nativeToken, uint256 _amount, bytes32 _zkLinkAddress, uint8 _subAccountId, bool _mapping) external; }