Skip to content

Commit

Permalink
fix(contracts): OZ-L1-L02 Initialization Not Disabled for Implementat…
Browse files Browse the repository at this point in the history
…ion Contracts (#639)

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
  • Loading branch information
zimpha and HAOYUatHZ committed Jul 20, 2023
1 parent af6d812 commit 523cc2c
Show file tree
Hide file tree
Showing 44 changed files with 308 additions and 80 deletions.
33 changes: 25 additions & 8 deletions contracts/integration-test/EnforcedTxGateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,36 @@ describe("EnforcedTxGateway.spec", async () => {
let oracle: L2GasPriceOracle;
let queue: L1MessageQueue;

const deployProxy = async (name: string, admin: string): Promise<string> => {
const TransparentUpgradeableProxy = await ethers.getContractFactory("TransparentUpgradeableProxy", deployer);
const Factory = await ethers.getContractFactory(name, deployer);
const impl = await Factory.deploy();
await impl.deployed();
const proxy = await TransparentUpgradeableProxy.deploy(impl.address, admin, "0x");
await proxy.deployed();
return proxy.address;
};

beforeEach(async () => {
[deployer, feeVault, signer] = await ethers.getSigners();

const L1MessageQueue = await ethers.getContractFactory("L1MessageQueue", deployer);
queue = await L1MessageQueue.deploy();
await queue.deployed();
const ProxyAdmin = await ethers.getContractFactory("ProxyAdmin", deployer);
const admin = await ProxyAdmin.deploy();
await admin.deployed();

queue = await ethers.getContractAt("L1MessageQueue", await deployProxy("L1MessageQueue", admin.address), deployer);

const L2GasPriceOracle = await ethers.getContractFactory("L2GasPriceOracle", deployer);
oracle = await L2GasPriceOracle.deploy();
oracle = await ethers.getContractAt(
"L2GasPriceOracle",
await deployProxy("L2GasPriceOracle", admin.address),
deployer
);

const EnforcedTxGateway = await ethers.getContractFactory("EnforcedTxGateway", deployer);
gateway = await EnforcedTxGateway.deploy();
await gateway.deployed();
gateway = await ethers.getContractAt(
"EnforcedTxGateway",
await deployProxy("EnforcedTxGateway", admin.address),
deployer
);

const MockCaller = await ethers.getContractFactory("MockCaller", deployer);
caller = await MockCaller.deploy();
Expand Down
25 changes: 20 additions & 5 deletions contracts/integration-test/L1MessageQueue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,30 @@ describe("L1MessageQueue", async () => {
let oracle: L2GasPriceOracle;
let queue: L1MessageQueue;

const deployProxy = async (name: string, admin: string): Promise<string> => {
const TransparentUpgradeableProxy = await ethers.getContractFactory("TransparentUpgradeableProxy", deployer);
const Factory = await ethers.getContractFactory(name, deployer);
const impl = await Factory.deploy();
await impl.deployed();
const proxy = await TransparentUpgradeableProxy.deploy(impl.address, admin, "0x");
await proxy.deployed();
return proxy.address;
};

beforeEach(async () => {
[deployer, scrollChain, messenger, gateway, signer] = await ethers.getSigners();

const L1MessageQueue = await ethers.getContractFactory("L1MessageQueue", deployer);
queue = await L1MessageQueue.deploy();
await queue.deployed();
const ProxyAdmin = await ethers.getContractFactory("ProxyAdmin", deployer);
const admin = await ProxyAdmin.deploy();
await admin.deployed();

queue = await ethers.getContractAt("L1MessageQueue", await deployProxy("L1MessageQueue", admin.address), deployer);

const L2GasPriceOracle = await ethers.getContractFactory("L2GasPriceOracle", deployer);
oracle = await L2GasPriceOracle.deploy();
oracle = await ethers.getContractAt(
"L2GasPriceOracle",
await deployProxy("L2GasPriceOracle", admin.address),
deployer
);

await oracle.initialize(21000, 0, 8, 16);
await queue.initialize(messenger.address, scrollChain.address, gateway.address, oracle.address, 10000000);
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/L1ScrollMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ contract L1ScrollMessenger is PausableUpgradeable, ScrollMessengerBase, IL1Scrol
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1ScrollMessenger.
/// @param _counterpart The address of L2ScrollMessenger contract in L2.
/// @param _feeVault The address of fee vault, which will be used to collect relayer fee.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/EnforcedTxGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ contract EnforcedTxGateway is OwnableUpgradeable, ReentrancyGuardUpgradeable, Pa
* Constructor *
***************/

constructor() {
_disableInitializers();
}

function initialize(address _queue, address _feeVault) external initializer {
OwnableUpgradeable.__Ownable_init();
ReentrancyGuardUpgradeable.__ReentrancyGuard_init();
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/L1CustomERC20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ contract L1CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L1ERC20G
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1CustomERC20Gateway.
/// @param _counterpart The address of L2CustomERC20Gateway in L2.
/// @param _router The address of L1GatewayRouter.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/L1ERC1155Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ contract L1ERC1155Gateway is
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1ERC1155Gateway.
/// @param _counterpart The address of L2ERC1155Gateway in L2.
/// @param _messenger The address of L1ScrollMessenger.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/L1ERC721Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ contract L1ERC721Gateway is
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1ERC721Gateway.
/// @param _counterpart The address of L2ERC721Gateway in L2.
/// @param _messenger The address of L1ScrollMessenger.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/L1ETHGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ contract L1ETHGateway is Initializable, ScrollGatewayBase, IL1ETHGateway, IMessa
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1ETHGateway.
/// @param _counterpart The address of L2ETHGateway in L2.
/// @param _router The address of L1GatewayRouter.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/L1GatewayRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ contract L1GatewayRouter is OwnableUpgradeable, IL1GatewayRouter {
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1GatewayRouter.
/// @param _ethGateway The address of L1ETHGateway contract.
/// @param _defaultERC20Gateway The address of default ERC20 Gateway contract.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/gateways/L1StandardERC20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ contract L1StandardERC20Gateway is Initializable, ScrollGatewayBase, L1ERC20Gate
* Constructor *
***************/

constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L1StandardERC20Gateway.
/// @param _counterpart The address of L2StandardERC20Gateway in L2.
/// @param _router The address of L1GatewayRouter.
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/L1/gateways/L1WETHGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ contract L1WETHGateway is Initializable, ScrollGatewayBase, L1ERC20Gateway {
***************/

constructor(address _WETH, address _l2WETH) {
_disableInitializers();

WETH = _WETH;
l2WETH = _l2WETH;
}
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/rollup/L1MessageQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ contract L1MessageQueue is OwnableUpgradeable, IL1MessageQueue {
* Constructor *
***************/

constructor() {
_disableInitializers();
}

function initialize(
address _messenger,
address _scrollChain,
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L1/rollup/L2GasPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ contract L2GasPriceOracle is OwnableUpgradeable, IL2GasPriceOracle {
* Constructor *
***************/

constructor() {
_disableInitializers();
}

function initialize(
uint64 _txGas,
uint64 _txGasContractCreation,
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/L1/rollup/ScrollChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ contract ScrollChain is OwnableUpgradeable, IScrollChain {
***************/

constructor(uint64 _chainId) {
_disableInitializers();

layer2ChainId = _chainId;
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/src/L2/L2ScrollMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ contract L2ScrollMessenger is ScrollMessengerBase, PausableUpgradeable, IL2Scrol
address _gasOracle,
address _messageQueue
) {
_disableInitializers();

blockContainer = _blockContainer;
gasOracle = _gasOracle;
messageQueue = _messageQueue;
Expand Down
3 changes: 3 additions & 0 deletions contracts/src/L2/gateways/L2CustomERC20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ contract L2CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20G
/***************
* Constructor *
***************/
constructor() {
_disableInitializers();
}

function initialize(
address _counterpart,
Expand Down
3 changes: 3 additions & 0 deletions contracts/src/L2/gateways/L2ERC1155Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
/***************
* Constructor *
***************/
constructor() {
_disableInitializers();
}

function initialize(address _counterpart, address _messenger) external initializer {
OwnableUpgradeable.__Ownable_init();
Expand Down
3 changes: 3 additions & 0 deletions contracts/src/L2/gateways/L2ERC721Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
/***************
* Constructor *
***************/
constructor() {
_disableInitializers();
}

function initialize(address _counterpart, address _messenger) external initializer {
OwnableUpgradeable.__Ownable_init();
Expand Down
3 changes: 3 additions & 0 deletions contracts/src/L2/gateways/L2ETHGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ contract L2ETHGateway is Initializable, ScrollGatewayBase, IL2ETHGateway {
/***************
* Constructor *
***************/
constructor() {
_disableInitializers();
}

/// @notice Initialize the storage of L2ETHGateway.
/// @param _counterpart The address of L1ETHGateway in L2.
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/L2/gateways/L2GatewayRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ contract L2GatewayRouter is OwnableUpgradeable, IL2GatewayRouter {
* Constructor *
***************/

constructor() {
_disableInitializers();
}

function initialize(address _ethGateway, address _defaultERC20Gateway) external initializer {
OwnableUpgradeable.__Ownable_init();

Expand Down
3 changes: 3 additions & 0 deletions contracts/src/L2/gateways/L2StandardERC20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ contract L2StandardERC20Gateway is Initializable, ScrollGatewayBase, L2ERC20Gate
/***************
* Constructor *
***************/
constructor() {
_disableInitializers();
}

function initialize(
address _counterpart,
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/L2/gateways/L2WETHGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ contract L2WETHGateway is Initializable, ScrollGatewayBase, L2ERC20Gateway {
***************/

constructor(address _WETH, address _l1WETH) {
_disableInitializers();

WETH = _WETH;
l1WETH = _l1WETH;
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/L2/gateways/usdc/L2USDCGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ contract L2USDCGateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20Gateway
***************/

constructor(address _l1USDC, address _l2USDC) {
_disableInitializers();

l1USDC = _l1USDC;
l2USDC = _l2USDC;
}
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/libraries/token/ScrollStandardERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ contract ScrollStandardERC20 is ERC20PermitUpgradeable, IScrollERC20Upgradeable
_;
}

constructor() {
_disableInitializers();
}

function initialize(
string memory _name,
string memory _symbol,
Expand Down
14 changes: 10 additions & 4 deletions contracts/src/test/L1CustomERC20Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity =0.8.16;

import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";

import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

import {IL1ERC20Gateway, L1CustomERC20Gateway} from "../L1/gateways/L1CustomERC20Gateway.sol";
import {L1GatewayRouter} from "../L1/gateways/L1GatewayRouter.sol";
import {IL1ScrollMessenger} from "../L1/IL1ScrollMessenger.sol";
Expand Down Expand Up @@ -50,8 +52,8 @@ contract L1CustomERC20GatewayTest is L1GatewayTestBase {
l2Token = new MockERC20("Mock L2", "ML2", 18);

// Deploy L1 contracts
gateway = new L1CustomERC20Gateway();
router = new L1GatewayRouter();
gateway = _deployGateway();
router = L1GatewayRouter(address(new ERC1967Proxy(address(new L1GatewayRouter()), new bytes(0))));

// Deploy L2 contracts
counterpartGateway = new L2CustomERC20Gateway();
Expand Down Expand Up @@ -128,7 +130,7 @@ contract L1CustomERC20GatewayTest is L1GatewayTestBase {

function testDropMessageMocking() public {
MockScrollMessenger mockMessenger = new MockScrollMessenger();
gateway = new L1CustomERC20Gateway();
gateway = _deployGateway();
gateway.initialize(address(counterpartGateway), address(router), address(mockMessenger));

// only messenger can call, revert
Expand Down Expand Up @@ -216,7 +218,7 @@ contract L1CustomERC20GatewayTest is L1GatewayTestBase {
gateway.finalizeWithdrawERC20(address(l1Token), address(l2Token), sender, recipient, amount, dataToCall);

MockScrollMessenger mockMessenger = new MockScrollMessenger();
gateway = new L1CustomERC20Gateway();
gateway = _deployGateway();
gateway.initialize(address(counterpartGateway), address(router), address(mockMessenger));

// only call by counterpart
Expand Down Expand Up @@ -655,4 +657,8 @@ contract L1CustomERC20GatewayTest is L1GatewayTestBase {
assertBoolEq(true, l1Messenger.isL1MessageSent(keccak256(xDomainCalldata)));
}
}

function _deployGateway() internal returns (L1CustomERC20Gateway) {
return L1CustomERC20Gateway(address(new ERC1967Proxy(address(new L1CustomERC20Gateway()), new bytes(0))));
}
}

0 comments on commit 523cc2c

Please sign in to comment.