Skip to content

Commit

Permalink
use immutable value to save gas
Browse files Browse the repository at this point in the history
  • Loading branch information
zkbenny committed Feb 5, 2024
1 parent 791a41f commit 243891d
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 107 deletions.
14 changes: 4 additions & 10 deletions contracts/gateway/L1BaseGateway.sol
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.0;

import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {IArbitrator} from "../interfaces/IArbitrator.sol";
import {IL1Gateway} from "../interfaces/IL1Gateway.sol";

abstract contract L1BaseGateway is IL1Gateway, UUPSUpgradeable {
abstract contract L1BaseGateway is IL1Gateway {
/// @notice The arbitrator to confirm synchronization
IArbitrator public arbitrator;
IArbitrator public immutable arbitrator;

Check warning on line 9 in contracts/gateway/L1BaseGateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
uint256[50] private __gap;

/// @dev Modifier to make sure the caller is the known arbitrator.
modifier onlyArbitrator() {
require(msg.sender == address(arbitrator), "Not arbitrator");
_;
}

function __L1BaseGateway_init(IArbitrator _arbitrator) internal onlyInitializing {
__UUPSUpgradeable_init();
__L1BaseGateway_init_unchained(_arbitrator);
}

function __L1BaseGateway_init_unchained(IArbitrator _arbitrator) internal onlyInitializing {
constructor(IArbitrator _arbitrator) {
arbitrator = _arbitrator;
}
}
14 changes: 4 additions & 10 deletions contracts/gateway/L2BaseGateway.sol
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.0;

import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {IL2Gateway} from "../interfaces/IL2Gateway.sol";

abstract contract L2BaseGateway is IL2Gateway, UUPSUpgradeable {
abstract contract L2BaseGateway is IL2Gateway {
/// @notice The zkLink contract
address public zkLink;
address public immutable zkLink;

Check warning on line 8 in contracts/gateway/L2BaseGateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
uint256[50] private __gap;

/// @dev Ensure withdraw come from zkLink
modifier onlyZkLink() {
require(msg.sender == zkLink, "Not zkLink contract");
_;
}

function __L2BaseGateway_init(address _zkLink) internal onlyInitializing {
__UUPSUpgradeable_init();
__L2BaseGateway_init_unchained(_zkLink);
}

function __L2BaseGateway_init_unchained(address _zkLink) internal onlyInitializing {
constructor(address _zkLink) {
zkLink = _zkLink;
}
}
11 changes: 6 additions & 5 deletions contracts/gateway/arbitrum/ArbitrumL1Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {IArbitrumGateway} from "../../interfaces/arbitrum/IArbitrumGateway.sol";

contract ArbitrumL1Gateway is IArbitrumGateway, L1BaseGateway, BaseGateway {
/// @notice Arbitrum inbox on local chain
Inbox public inbox;
Inbox public immutable inbox;

Check warning on line 13 in contracts/gateway/arbitrum/ArbitrumL1Gateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @dev Modifier to make sure the original sender is gateway on remote chain.
modifier onlyRemoteGateway() {
Expand All @@ -22,13 +22,14 @@ contract ArbitrumL1Gateway is IArbitrumGateway, L1BaseGateway, BaseGateway {
_;
}

function initialize(IArbitrator _arbitrator, Inbox _inbox) external initializer {
__L1BaseGateway_init(_arbitrator);
__BaseGateway_init();

constructor(IArbitrator _arbitrator, Inbox _inbox) L1BaseGateway(_arbitrator) {
inbox = _inbox;
}

function initialize() external initializer {
__BaseGateway_init();
}

function sendMessage(
uint256 _value,
bytes memory _callData,
Expand Down
5 changes: 3 additions & 2 deletions contracts/gateway/arbitrum/ArbitrumL2Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ contract ArbitrumL2Gateway is IArbitrumGateway, L2BaseGateway, BaseGateway {
_;
}

function initialize(address _zkLink) external initializer {
__L2BaseGateway_init(_zkLink);
constructor(address _zkLink) L2BaseGateway(_zkLink) {}

function initialize() external initializer {
__BaseGateway_init();
}

Expand Down
15 changes: 11 additions & 4 deletions contracts/gateway/ethereum/EthereumGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
pragma solidity ^0.8.0;

import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import {IArbitrator} from "../../interfaces/IArbitrator.sol";
import {L2BaseGateway} from "../L2BaseGateway.sol";
import {L1BaseGateway} from "../L1BaseGateway.sol";

contract EthereumGateway is L1BaseGateway, L2BaseGateway, OwnableUpgradeable, ReentrancyGuardUpgradeable {
function initialize(IArbitrator _arbitrator, address _zkLink) external initializer {
__L1BaseGateway_init(_arbitrator);
__L2BaseGateway_init(_zkLink);
contract EthereumGateway is
L1BaseGateway,
L2BaseGateway,
OwnableUpgradeable,
UUPSUpgradeable,
ReentrancyGuardUpgradeable
{
constructor(IArbitrator _arbitrator, address _zkLink) L1BaseGateway(_arbitrator) L2BaseGateway(_zkLink) {}

function initialize() external initializer {
__Ownable_init();
__UUPSUpgradeable_init();
__ReentrancyGuard_init();
}

Expand Down
13 changes: 6 additions & 7 deletions contracts/gateway/linea/LineaGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {ILineaGateway} from "../../interfaces/linea/ILineaGateway.sol";

abstract contract LineaGateway is BaseGateway, ILineaGateway {
/// @notice Linea message service on local chain
IMessageService public messageService;
IMessageService public immutable messageService;

Check warning on line 10 in contracts/gateway/linea/LineaGateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
uint256[50] private __gap;

/// @dev Modifier to make sure the caller is the known message service.
modifier onlyMessageService() {
Expand All @@ -28,13 +28,12 @@ abstract contract LineaGateway is BaseGateway, ILineaGateway {
_;
}

function __LineaGateway_init(IMessageService _messageService) internal onlyInitializing {
__BaseGateway_init();
__LineaGateway_init_unchained(_messageService);
constructor(IMessageService _messageService) {
messageService = _messageService;
}

function __LineaGateway_init_unchained(IMessageService _messageService) internal onlyInitializing {
messageService = _messageService;
function __LineaGateway_init() internal onlyInitializing {
__BaseGateway_init();
}

function claimMessage(uint256 _value, bytes calldata _callData, uint256 _nonce) external nonReentrant {
Expand Down
10 changes: 7 additions & 3 deletions contracts/gateway/linea/LineaL1Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import {LineaGateway} from "./LineaGateway.sol";
import {L1BaseGateway} from "../L1BaseGateway.sol";

contract LineaL1Gateway is L1BaseGateway, LineaGateway {
function initialize(IArbitrator _arbitrator, IMessageService _messageService) external initializer {
__L1BaseGateway_init(_arbitrator);
__LineaGateway_init(_messageService);
constructor(
IArbitrator _arbitrator,
IMessageService _messageService
) L1BaseGateway(_arbitrator) LineaGateway(_messageService) {}

function initialize() external initializer {
__LineaGateway_init();
}

function sendMessage(uint256 _value, bytes memory _callData, bytes memory) external payable onlyArbitrator {
Expand Down
10 changes: 7 additions & 3 deletions contracts/gateway/linea/LineaL2Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {LineaGateway} from "./LineaGateway.sol";
import {L2BaseGateway} from "../L2BaseGateway.sol";

contract LineaL2Gateway is L2BaseGateway, LineaGateway {
function initialize(address _zkLink, IMessageService _messageService) external initializer {
__L2BaseGateway_init(_zkLink);
__LineaGateway_init(_messageService);
constructor(
address _zkLink,
IMessageService _messageService
) L2BaseGateway(_zkLink) LineaGateway(_messageService) {}

function initialize() external initializer {
__LineaGateway_init();
}

function sendMessage(uint256 value, bytes memory callData) external payable override onlyZkLink {
Expand Down
13 changes: 6 additions & 7 deletions contracts/gateway/scroll/ScrollGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@ import {BaseGateway} from "../BaseGateway.sol";

abstract contract ScrollGateway is BaseGateway, IScrollGateway {
/// @notice Linea message service on local chain
IScrollMessenger public messageService;
IScrollMessenger public immutable messageService;

Check warning on line 10 in contracts/gateway/scroll/ScrollGateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
uint256[50] private __gap;

/// @dev Modifier to make sure the caller is the known message service.
modifier onlyMessageService() {
require(msg.sender == address(messageService), "Not message service");
_;
}

function __ScrollGateway_init(IScrollMessenger _messageService) internal onlyInitializing {
__BaseGateway_init();
__ScrollGateway_init_unchained(_messageService);
constructor(IScrollMessenger _messageService) {
messageService = _messageService;
}

function __ScrollGateway_init_unchained(IScrollMessenger _messageService) internal onlyInitializing {
messageService = _messageService;
function __ScrollGateway_init() internal onlyInitializing {
__BaseGateway_init();
}
}
10 changes: 7 additions & 3 deletions contracts/gateway/scroll/ScrollL1Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import {L1BaseGateway} from "../L1BaseGateway.sol";
import {ScrollGateway} from "./ScrollGateway.sol";

contract ScrollL1Gateway is ScrollGateway, L1BaseGateway {
function initialize(IArbitrator _arbitrator, IScrollMessenger _messageService) external initializer {
__L1BaseGateway_init(_arbitrator);
__ScrollGateway_init(_messageService);
constructor(
IArbitrator _arbitrator,
IScrollMessenger _messageService
) L1BaseGateway(_arbitrator) ScrollGateway(_messageService) {}

function initialize() external initializer {
__ScrollGateway_init();
}

function sendMessage(
Expand Down
10 changes: 7 additions & 3 deletions contracts/gateway/scroll/ScrollL2Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {L2BaseGateway} from "../L2BaseGateway.sol";
import {ScrollGateway} from "./ScrollGateway.sol";

contract ScrollL2Gateway is L2BaseGateway, ScrollGateway {
function initialize(address _zkLink, IScrollMessenger _messageService) external initializer {
__L2BaseGateway_init(_zkLink);
__ScrollGateway_init(_messageService);
constructor(
address _zkLink,
IScrollMessenger _messageService
) L2BaseGateway(_zkLink) ScrollGateway(_messageService) {}

function initialize() external initializer {
__ScrollGateway_init();
}

function sendMessage(uint256 _value, bytes memory _callData) external payable override onlyZkLink {
Expand Down
11 changes: 6 additions & 5 deletions contracts/gateway/zkpolygon/ZkPolygonL1Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {BaseGateway} from "../BaseGateway.sol";

contract ZkPolygonL1Gateway is IZkPolygonGateway, L1BaseGateway, BaseGateway {
/// @notice ZkPolygon message service on local chain
IZkPolygon public messageService;
IZkPolygon public immutable messageService;

Check warning on line 12 in contracts/gateway/zkpolygon/ZkPolygonL1Gateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

uint32 public constant ETH_NETWORK_ID = 1;
// Default to true
Expand All @@ -24,13 +24,14 @@ contract ZkPolygonL1Gateway is IZkPolygonGateway, L1BaseGateway, BaseGateway {
/// @dev Used to indicate that zkSync L2 -> L1 message was already processed
mapping(uint256 => mapping(uint256 => bool)) public isMessageFinalized;

function initialize(IArbitrator _arbitrator, IZkPolygon _messageService) external initializer {
__L1BaseGateway_init(_arbitrator);
__BaseGateway_init();

constructor(IArbitrator _arbitrator, IZkPolygon _messageService) L1BaseGateway(_arbitrator) {
messageService = _messageService;
}

function initialize() external initializer {
__BaseGateway_init();
}

function sendMessage(uint256 _value, bytes memory _callData, bytes memory) external payable onlyArbitrator {
bytes memory executeData = abi.encodeCall(IZkPolygonGateway.claimMessageCallback, (_value, _callData));
messageService.bridgeMessage{value: msg.value}(
Expand Down
10 changes: 6 additions & 4 deletions contracts/gateway/zkpolygon/ZkPolygonL2Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {BaseGateway} from "../BaseGateway.sol";

contract ZkPolygonL2Gateway is IZkPolygonGateway, L2BaseGateway, BaseGateway {
/// @notice ZkPolygon message service on local chain
IZkPolygon public messageService;
IZkPolygon public immutable messageService;

Check warning on line 11 in contracts/gateway/zkpolygon/ZkPolygonL2Gateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

uint32 public constant ETH_NETWORK_ID = 0;
// Default to true
Expand All @@ -19,13 +19,15 @@ contract ZkPolygonL2Gateway is IZkPolygonGateway, L2BaseGateway, BaseGateway {
require(msg.sender == address(messageService), "Not remote gateway");
_;
}
function initialize(address _zkLink, IZkPolygon _messageService) external initializer {
__L2BaseGateway_init(_zkLink);
__BaseGateway_init();

constructor(address _zkLink, IZkPolygon _messageService) L2BaseGateway(_zkLink) {
messageService = _messageService;
}

function initialize() external initializer {
__BaseGateway_init();
}

function sendMessage(uint256 _value, bytes memory _callData) external payable onlyZkLink {
bytes memory executeData = abi.encodeCall(IZkPolygonGateway.claimMessageCallback, (_value, _callData));
messageService.bridgeMessage{value: msg.value}(
Expand Down
11 changes: 6 additions & 5 deletions contracts/gateway/zksync/ZkSyncL1Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import {L2Message} from "../../zksync/l1-contracts/zksync/Storage.sol";

contract ZkSyncL1Gateway is IZkSyncL1Gateway, L1BaseGateway, BaseGateway {
/// @notice ZkSync message service on local chain
IMailbox public messageService;
IMailbox public immutable messageService;

Check warning on line 14 in contracts/gateway/zksync/ZkSyncL1Gateway.sol

View workflow job for this annotation

GitHub Actions / Test solidity contracts

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @dev A mapping L2 batch number => message number => flag
/// @dev Used to indicate that zkSync L2 -> L1 message was already processed
mapping(uint256 => mapping(uint256 => bool)) public isMessageFinalized;

constructor(IArbitrator _arbitrator, IMailbox _messageService) L1BaseGateway(_arbitrator) {
messageService = _messageService;
}

/// @dev Receive eth from zkSync canonical bridge
receive() external payable {}

function initialize(IArbitrator _arbitrator, IMailbox _messageService) external initializer {
__L1BaseGateway_init(_arbitrator);
function initialize() external initializer {
__BaseGateway_init();

messageService = _messageService;
}

function sendMessage(
Expand Down
5 changes: 3 additions & 2 deletions contracts/gateway/zksync/ZkSyncL2Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ contract ZkSyncL2Gateway is IZkSyncL2Gateway, L2BaseGateway, BaseGateway {
_;
}

function initialize(address _zkLink) external initializer {
__L2BaseGateway_init(_zkLink);
constructor(address _zkLink) L2BaseGateway(_zkLink) {}

function initialize() external initializer {
__BaseGateway_init();
}

Expand Down
Loading

0 comments on commit 243891d

Please sign in to comment.