-
Notifications
You must be signed in to change notification settings - Fork 68
test: add fork tests for upgradable contracts #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+17,840
−115
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6df57b
test: implement fork tests for upgradable contracts
s2imonovic 42ddb51
fix: lint
s2imonovic 9666280
fix: remappings
s2imonovic 9048ce0
fix: fmt
s2imonovic 23285df
Merge remote-tracking branch 'origin/develop' into test/fork-tests-up…
s2imonovic 9b10fcf
fix: generate
s2imonovic 23e46ed
fix: generate
s2imonovic 5ad6c1c
refactor: remove upgrade scripts
s2imonovic 700115c
feat: check admin address after the upgrade
s2imonovic 0c33979
fix: generate
s2imonovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.26; | ||
|
|
||
| import "../../dependencies/forge-std-1.9.2/src/Test.sol"; | ||
| import "../../dependencies/forge-std-1.9.2/src/Vm.sol"; | ||
|
|
||
| abstract contract BaseForkTest is Test { | ||
| // RPCs of all supported chains. | ||
| string public ETHEREUM_RPC_URL = vm.envString("ETHEREUM_RPC_URL"); | ||
| string public BSC_RPC_URL = vm.envString("BSC_RPC_URL"); | ||
| string public POLYGON_RPC_URL = vm.envString("POLYGON_RPC_URL"); | ||
| string public BASE_RPC_URL = vm.envString("BASE_RPC_URL"); | ||
| string public ARBITRUM_RPC_URL = vm.envString("ARBITRUM_RPC_URL"); | ||
| string public AVALANCHE_RPC_URL = vm.envString("AVALANCHE_RPC_URL"); | ||
| string public ZETACHAIN_RPC_URL = vm.envString("ZETACHAIN_RPC_URL"); | ||
|
|
||
| // The fork identifiers. | ||
| uint256 ethereumForkId; | ||
| uint256 bscForkId; | ||
| uint256 polygonForkId; | ||
| uint256 baseForkId; | ||
| uint256 arbitrumForkId; | ||
| uint256 avalancheForkId; | ||
| uint256 zetachainForkId; | ||
|
|
||
| // Mainnet admins. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test only work for mainnet? We should precise it somewhere. Otherwise can't we make it dependent on the network used? |
||
| address constant ETHEREUM_ADMIN = 0xaeB6dDB7708467814D557e340283248be8E43124; | ||
| address constant BSC_ADMIN = 0xaf28a257D292e7f0E531073f70a175b57E0261a8; | ||
| address constant POLYGON_ADMIN = 0x7828F92E7d79E141189f24C98aceF71Bc07bad3f; | ||
| address constant BASE_ADMIN = 0xF43CF8b3F3D22d4cC33f964c59076eaB2F8A108E; | ||
| address constant ARBITRUM_ADMIN = 0xdE3fb63723f0EEed8967ff9124e1c3bA89871b03; | ||
| address constant AVALANCHE_ADMIN = 0xdE3fb63723f0EEed8967ff9124e1c3bA89871b03; | ||
| address constant ZETACHAIN_ADMIN = 0x01d8207AfF7a7d029114Ee35afc72d0e133B7a0A; | ||
|
|
||
| struct ChainConfig { | ||
| uint256 forkId; | ||
| address contractAddress; | ||
| address admin; | ||
| string rpcUrl; | ||
| string name; | ||
| } | ||
|
|
||
| ChainConfig[] public chains; | ||
|
|
||
| function setUp() public virtual { | ||
| ethereumForkId = vm.createFork(ETHEREUM_RPC_URL); | ||
| bscForkId = vm.createFork(BSC_RPC_URL); | ||
| polygonForkId = vm.createFork(POLYGON_RPC_URL); | ||
| baseForkId = vm.createFork(BASE_RPC_URL); | ||
| arbitrumForkId = vm.createFork(ARBITRUM_RPC_URL); | ||
| avalancheForkId = vm.createFork(AVALANCHE_RPC_URL); | ||
| zetachainForkId = vm.createFork(ZETACHAIN_RPC_URL); | ||
|
|
||
| _setupChains(); | ||
| } | ||
|
|
||
| function _setupChains() internal virtual; | ||
|
|
||
| function testForkIdDiffer() public view { | ||
| assert(ethereumForkId != bscForkId); | ||
| assert(bscForkId != polygonForkId); | ||
| assert(polygonForkId != baseForkId); | ||
| assert(baseForkId != arbitrumForkId); | ||
| assert(arbitrumForkId != avalancheForkId); | ||
| assert(avalancheForkId != zetachainForkId); | ||
| } | ||
|
|
||
| function testCanSwitchForks() public { | ||
| for (uint256 i = 0; i < chains.length; i++) { | ||
| vm.selectFork(chains[i].forkId); | ||
| assertEq(vm.activeFork(), chains[i].forkId); | ||
| } | ||
| } | ||
|
|
||
| function testUpgradeGatewayOnAllChains() public { | ||
| for (uint256 i = 0; i < chains.length; i++) { | ||
| console.log("\n=== Testing upgrade on", chains[i].name, "==="); | ||
| _testUpgradeContract(chains[i]); | ||
| } | ||
| } | ||
|
|
||
| function _testUpgradeContract(ChainConfig memory config) internal virtual; | ||
|
|
||
| function _getImplementation(address proxy) internal view returns (address) { | ||
| bytes32 slot = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; | ||
| return address(uint160(uint256(vm.load(proxy, slot)))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.26; | ||
|
|
||
| import { ERC20Custody } from "../../contracts/evm/ERC20Custody.sol"; | ||
| import "./BaseForkTest.sol"; | ||
|
|
||
| contract ERC20CustodyForkTest is BaseForkTest { | ||
| // ERC20Custody proxy addresses. | ||
| address constant ETHEREUM_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5; | ||
| address constant BSC_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5; | ||
| address constant POLYGON_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5; | ||
| address constant BASE_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5; | ||
| address constant ARBITRUM_ERC20CUSTODY_PROXY = 0xECe33274237E6422f2668eD7dEE5901b16336aA0; | ||
| address constant AVALANCHE_ERC20CUSTODY_PROXY = 0xECe33274237E6422f2668eD7dEE5901b16336aA0; | ||
|
|
||
| function _setupChains() internal override { | ||
| chains.push( | ||
| ChainConfig(ethereumForkId, ETHEREUM_ERC20CUSTODY_PROXY, ETHEREUM_ADMIN, ETHEREUM_RPC_URL, "Ethereum") | ||
| ); | ||
| chains.push(ChainConfig(bscForkId, BSC_ERC20CUSTODY_PROXY, BSC_ADMIN, BSC_RPC_URL, "BSC")); | ||
| chains.push(ChainConfig(polygonForkId, POLYGON_ERC20CUSTODY_PROXY, POLYGON_ADMIN, POLYGON_RPC_URL, "Polygon")); | ||
| chains.push(ChainConfig(baseForkId, BASE_ERC20CUSTODY_PROXY, BASE_ADMIN, BASE_RPC_URL, "Base")); | ||
| chains.push( | ||
| ChainConfig(arbitrumForkId, ARBITRUM_ERC20CUSTODY_PROXY, ARBITRUM_ADMIN, ARBITRUM_RPC_URL, "Arbitrum") | ||
| ); | ||
| chains.push( | ||
| ChainConfig(avalancheForkId, AVALANCHE_ERC20CUSTODY_PROXY, AVALANCHE_ADMIN, AVALANCHE_RPC_URL, "Avalanche") | ||
| ); | ||
| } | ||
s2imonovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function _testUpgradeContract(ChainConfig memory config) internal override { | ||
| vm.selectFork(config.forkId); | ||
|
|
||
| // Get the current proxy contract. | ||
| ERC20Custody custody = ERC20Custody(config.contractAddress); | ||
|
|
||
s2imonovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Storage state before the upgrade. | ||
| address gatewayBefore = address(custody.gateway()); | ||
| address tssAddressBefore = custody.tssAddress(); | ||
| bool supportsLegacyBefore = custody.supportsLegacy(); | ||
|
|
||
| // Deploy new implementation. | ||
| vm.startPrank(config.admin); | ||
| ERC20Custody localNewImplementation = new ERC20Custody(); | ||
|
|
||
| // Upgrade the proxy. | ||
| custody.upgradeToAndCall(address(localNewImplementation), ""); | ||
|
|
||
| // Verify upgrade. | ||
| address onChainNewImplementation = _getImplementation(address(custody)); | ||
| assertEq(onChainNewImplementation, address(localNewImplementation), "Upgrade failed."); | ||
| console.log("Successfully upgraded to:", onChainNewImplementation); | ||
|
|
||
| // Verify state preservation. | ||
| assertEq(address(custody.gateway()), gatewayBefore, "Gateway address changed."); | ||
| assertEq(custody.tssAddress(), tssAddressBefore, "TSS address changed."); | ||
| assertEq(custody.supportsLegacy(), supportsLegacyBefore, "Supports legacy changed."); | ||
| console.log("State preserved after upgrade"); | ||
s2imonovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| vm.stopPrank(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.26; | ||
|
|
||
| import { GatewayEVM } from "../../contracts/evm/GatewayEVM.sol"; | ||
| import "./BaseForkTest.sol"; | ||
|
|
||
| contract GatewayEVMForkTest is BaseForkTest { | ||
| // GatewayEVM proxy addresses. | ||
| address constant ETHEREUM_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed; | ||
| address constant BSC_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed; | ||
| address constant POLYGON_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed; | ||
| address constant BASE_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed; | ||
| address constant ARBITRUM_GATEWAY_PROXY = 0x1C53e188Bc2E471f9D4A4762CFf843d32C2C8549; | ||
| address constant AVALANCHE_GATEWAY_PROXY = 0x1C53e188Bc2E471f9D4A4762CFf843d32C2C8549; | ||
|
|
||
| function _setupChains() internal override { | ||
| chains.push(ChainConfig(ethereumForkId, ETHEREUM_GATEWAY_PROXY, ETHEREUM_ADMIN, ETHEREUM_RPC_URL, "Ethereum")); | ||
| chains.push(ChainConfig(bscForkId, BSC_GATEWAY_PROXY, BSC_ADMIN, BSC_RPC_URL, "BSC")); | ||
| chains.push(ChainConfig(polygonForkId, POLYGON_GATEWAY_PROXY, POLYGON_ADMIN, POLYGON_RPC_URL, "Polygon")); | ||
| chains.push(ChainConfig(baseForkId, BASE_GATEWAY_PROXY, BASE_ADMIN, BASE_RPC_URL, "Base")); | ||
| chains.push(ChainConfig(arbitrumForkId, ARBITRUM_GATEWAY_PROXY, ARBITRUM_ADMIN, ARBITRUM_RPC_URL, "Arbitrum")); | ||
| chains.push( | ||
| ChainConfig(avalancheForkId, AVALANCHE_GATEWAY_PROXY, AVALANCHE_ADMIN, AVALANCHE_RPC_URL, "Avalanche") | ||
| ); | ||
| } | ||
|
|
||
| function _testUpgradeContract(ChainConfig memory config) internal override { | ||
| vm.selectFork(config.forkId); | ||
|
|
||
| // Get the current proxy contract. | ||
| GatewayEVM gateway = GatewayEVM(config.contractAddress); | ||
|
|
||
| // Storage state before the upgrade. | ||
| address custodyBefore = gateway.custody(); | ||
| address tssAddressBefore = gateway.tssAddress(); | ||
| address zetaConnectorBefore = gateway.zetaConnector(); | ||
| address zetaTokenBefore = gateway.zetaToken(); | ||
|
|
||
| // Deploy new implementation. | ||
| vm.startPrank(config.admin); | ||
| GatewayEVM localNewImplementation = new GatewayEVM(); | ||
|
|
||
| // Upgrade the proxy. | ||
| gateway.upgradeToAndCall(address(localNewImplementation), ""); | ||
|
|
||
| // Verify upgrade. | ||
| address onChainNewImplementation = _getImplementation(address(gateway)); | ||
| assertEq(onChainNewImplementation, address(localNewImplementation), "Upgrade failed."); | ||
| console.log("Successfully upgraded to:", onChainNewImplementation); | ||
|
|
||
| // Verify state preservation. | ||
| assertEq(gateway.custody(), custodyBefore, "Custody address changed."); | ||
| assertEq(gateway.tssAddress(), tssAddressBefore, "TSS address changed."); | ||
| assertEq(gateway.zetaConnector(), zetaConnectorBefore, "Connector address changed."); | ||
| assertEq(gateway.zetaToken(), zetaTokenBefore, "Zeta token address changed."); | ||
| console.log("State preserved after upgrade"); | ||
|
|
||
| vm.stopPrank(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.26; | ||
|
|
||
| import { GatewayZEVM } from "../../contracts/zevm/GatewayZEVM.sol"; | ||
| import "./BaseForkTest.sol"; | ||
|
|
||
| contract GatewayZEVMForkTest is BaseForkTest { | ||
| // GatewayZEVM proxy address. | ||
| address constant ZETACHAIN_GATEWAY_PROXY = 0xfEDD7A6e3Ef1cC470fbfbF955a22D793dDC0F44E; | ||
|
|
||
| function _setupChains() internal override { | ||
| chains.push( | ||
| ChainConfig(zetachainForkId, ZETACHAIN_GATEWAY_PROXY, ZETACHAIN_ADMIN, ZETACHAIN_RPC_URL, "ZetaChain") | ||
| ); | ||
| } | ||
|
|
||
| function _testUpgradeContract(ChainConfig memory config) internal override { | ||
| vm.selectFork(config.forkId); | ||
|
|
||
| // Get the current proxy contract. | ||
| GatewayZEVM gateway = GatewayZEVM(payable(config.contractAddress)); | ||
|
|
||
| // Storage state before the upgrade. | ||
| address zetaTokenBefore = gateway.zetaToken(); | ||
|
|
||
| // Deploy new implementation. | ||
| vm.startPrank(config.admin); | ||
| GatewayZEVM localNewImplementation = new GatewayZEVM(); | ||
|
|
||
| // Upgrade the proxy. | ||
| gateway.upgradeToAndCall(address(localNewImplementation), ""); | ||
|
|
||
| // Verify upgrade. | ||
| address onChainNewImplementation = _getImplementation(address(gateway)); | ||
| assertEq(onChainNewImplementation, address(localNewImplementation), "Upgrade failed."); | ||
| console.log("Successfully upgraded to:", onChainNewImplementation); | ||
|
|
||
| // Verify state preservation. | ||
| assertEq(gateway.zetaToken(), zetaTokenBefore, "Zeta token address changed."); | ||
| console.log("State preserved after upgrade"); | ||
|
|
||
| vm.stopPrank(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.