Jelajahi Sumber

bridge: move evm state setup into a separate contract

Change-Id: Ibc790ba971be5144c0af65870d424c9c62b52039
valentin 4 tahun lalu
induk
melakukan
45d22ce84f

+ 0 - 21
ethereum/contracts/bridge/BridgeImplementation.sol

@@ -10,27 +10,6 @@ import "./Bridge.sol";
 
 
 contract BridgeImplementation is Bridge {
-
-    function initialize(
-        uint16 chainId,
-        address wormhole,
-        uint16 governanceChainId,
-        bytes32 governanceContract,
-        address tokenImplementation,
-        address WETH
-    ) initializer public {
-        setChainId(chainId);
-
-        setWormhole(wormhole);
-
-        setGovernanceChainId(governanceChainId);
-        setGovernanceContract(governanceContract);
-
-        setTokenImplementation(tokenImplementation);
-
-        setWETH(WETH);
-    }
-
     // Beacon getter for the token contracts
     function implementation() public view returns (address) {
         return tokenImplementation();

+ 34 - 0
ethereum/contracts/bridge/BridgeSetup.sol

@@ -0,0 +1,34 @@
+// contracts/BridgeSetup.sol
+// SPDX-License-Identifier: Apache 2
+
+pragma solidity ^0.8.0;
+pragma experimental ABIEncoderV2;
+
+import "./BridgeGovernance.sol";
+
+import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Upgrade.sol";
+
+contract BridgeSetup is BridgeSetters, ERC1967Upgrade {
+    function setup(
+        address implementation,
+        uint16 chainId,
+        address wormhole,
+        uint16 governanceChainId,
+        bytes32 governanceContract,
+        address tokenImplementation,
+        address WETH
+    ) public {
+        setChainId(chainId);
+
+        setWormhole(wormhole);
+
+        setGovernanceChainId(governanceChainId);
+        setGovernanceContract(governanceContract);
+
+        setTokenImplementation(tokenImplementation);
+
+        setWETH(WETH);
+
+        _upgradeTo(implementation);
+    }
+}

+ 8 - 3
ethereum/migrations/3_deploy_bridge.js

@@ -2,6 +2,7 @@ require('dotenv').config({ path: "../.env" });
 
 const TokenBridge = artifacts.require("TokenBridge");
 const BridgeImplementation = artifacts.require("BridgeImplementation");
+const BridgeSetup = artifacts.require("BridgeSetup");
 const TokenImplementation = artifacts.require("TokenImplementation");
 const Wormhole = artifacts.require("Wormhole");
 
@@ -14,12 +15,16 @@ module.exports = async function (deployer) {
     // deploy token implementation
     await deployer.deploy(TokenImplementation);
 
+    // deploy setup
+    await deployer.deploy(BridgeSetup);
+
     // deploy implementation
     await deployer.deploy(BridgeImplementation);
 
     // encode initialisation data
-    const impl = new web3.eth.Contract(BridgeImplementation.abi, BridgeImplementation.address);
-    const initData = impl.methods.initialize(
+    const setup = new web3.eth.Contract(BridgeSetup.abi, BridgeSetup.address);
+    const initData = setup.methods.setup(
+        BridgeImplementation.address,
         chainId,
         (await Wormhole.deployed()).address,
         governanceChainId,
@@ -29,5 +34,5 @@ module.exports = async function (deployer) {
     ).encodeABI();
 
     // deploy proxy
-    await deployer.deploy(TokenBridge, BridgeImplementation.address, initData);
+    await deployer.deploy(TokenBridge, BridgeSetup.address, initData);
 };

+ 0 - 21
ethereum/test/bridge.js

@@ -53,27 +53,6 @@ contract("Bridge", function () {
         assert.equal(governanceContract, testGovernanceContract);
     })
 
-    it("initialize should be non-reentrant", async function(){
-        const initialized = new web3.eth.Contract(BridgeImplementationFullABI, TokenBridge.address);
-
-        try{
-            await initialized.methods.initialize(
-                1,
-                Wormhole.address,
-                1,
-                testGovernanceContract,
-                TokenImplementation.address,
-                WETH
-            ).estimateGas();
-        }  catch (error) {
-            assert.equal(error.message, "Returned error: VM Exception while processing transaction: revert already initialized")
-            return
-        }
-
-        assert.fail("did not fail")
-    })
-
-
     it("should register a foreign bridge implementation correctly", async function() {
         const initialized = new web3.eth.Contract(BridgeImplementationFullABI, TokenBridge.address);
         const accounts = await web3.eth.getAccounts();