|
| 1 | +import { expect } from "chai"; |
| 2 | +import { getAddress, ZeroAddress } from "ethers"; |
| 3 | +import * as hre from "hardhat"; |
| 4 | +import type { Wallet } from "zksync-ethers"; |
| 5 | +import { Contract, Provider } from "zksync-ethers"; |
| 6 | + |
| 7 | +import { LOCAL_RICH_WALLETS, getWallet } from "../../deploy/utils"; |
| 8 | +import { ClaveDeployer } from "../utils/deployer"; |
| 9 | + |
| 10 | +const IMPLEMENTATION_SLOT = |
| 11 | + "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc"; |
| 12 | +const BEACON_SLOT = |
| 13 | + "0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50"; |
| 14 | + |
| 15 | +const storageAddress = (value: string): string => { |
| 16 | + return getAddress(`0x${value.slice(-40)}`); |
| 17 | +}; |
| 18 | + |
| 19 | +describe("BreakableBeaconProxy", () => { |
| 20 | + let deployer: ClaveDeployer; |
| 21 | + let provider: Provider; |
| 22 | + let ownerWallet: Wallet; |
| 23 | + let otherWallet: Wallet; |
| 24 | + let mockAbi: unknown[]; |
| 25 | + |
| 26 | + before(async () => { |
| 27 | + ownerWallet = getWallet(hre, LOCAL_RICH_WALLETS[0].privateKey); |
| 28 | + otherWallet = getWallet(hre, LOCAL_RICH_WALLETS[1].privateKey); |
| 29 | + deployer = new ClaveDeployer(hre, ownerWallet); |
| 30 | + provider = new Provider(hre.network.config.url, undefined, { |
| 31 | + cacheTimeout: -1, |
| 32 | + }); |
| 33 | + |
| 34 | + mockAbi = (await hre.zksyncEthers.loadArtifact("MockBreakableUpgradeable")) |
| 35 | + .abi; |
| 36 | + }); |
| 37 | + |
| 38 | + const deployFixture = async (): Promise<{ |
| 39 | + owner: string; |
| 40 | + implementationV2: Contract; |
| 41 | + implementationV3: Contract; |
| 42 | + beacon: Contract; |
| 43 | + proxy: Contract; |
| 44 | + proxied: Contract; |
| 45 | + }> => { |
| 46 | + const owner = await ownerWallet.getAddress(); |
| 47 | + const implementationV1 = await deployer.deployCustomContract( |
| 48 | + "MockBreakableUpgradeable", |
| 49 | + [1], |
| 50 | + ); |
| 51 | + const implementationV2 = await deployer.deployCustomContract( |
| 52 | + "MockBreakableUpgradeable", |
| 53 | + [2], |
| 54 | + ); |
| 55 | + const implementationV3 = await deployer.deployCustomContract( |
| 56 | + "MockBreakableUpgradeable", |
| 57 | + [3], |
| 58 | + ); |
| 59 | + const beacon = await deployer.deployCustomContract("MockBeacon", [ |
| 60 | + await implementationV1.getAddress(), |
| 61 | + ]); |
| 62 | + const proxy = await deployer.deployCustomContract("BreakableBeaconProxy", [ |
| 63 | + await beacon.getAddress(), |
| 64 | + ]); |
| 65 | + const proxied = new Contract( |
| 66 | + await proxy.getAddress(), |
| 67 | + mockAbi, |
| 68 | + ownerWallet, |
| 69 | + ); |
| 70 | + |
| 71 | + await (await proxied.initialize(owner, 123)).wait(); |
| 72 | + |
| 73 | + return { |
| 74 | + owner, |
| 75 | + implementationV2, |
| 76 | + implementationV3, |
| 77 | + beacon, |
| 78 | + proxy, |
| 79 | + proxied, |
| 80 | + }; |
| 81 | + }; |
| 82 | + |
| 83 | + const expectProxySlots = async ( |
| 84 | + proxy: Contract, |
| 85 | + expectedBeacon: string, |
| 86 | + expectedImplementation: string, |
| 87 | + ): Promise<void> => { |
| 88 | + const proxyAddress = await proxy.getAddress(); |
| 89 | + const [beaconSlot, implementationSlot] = await Promise.all([ |
| 90 | + provider.getStorage(proxyAddress, BEACON_SLOT), |
| 91 | + provider.getStorage(proxyAddress, IMPLEMENTATION_SLOT), |
| 92 | + ]); |
| 93 | + |
| 94 | + expect(storageAddress(beaconSlot)).to.eq(getAddress(expectedBeacon)); |
| 95 | + expect(storageAddress(implementationSlot)).to.eq( |
| 96 | + getAddress(expectedImplementation), |
| 97 | + ); |
| 98 | + }; |
| 99 | + |
| 100 | + it("delegates through the beacon before it is broken", async () => { |
| 101 | + const { owner, implementationV2, beacon, proxy, proxied } = |
| 102 | + await deployFixture(); |
| 103 | + |
| 104 | + expect(await proxied.version()).to.eq(1n); |
| 105 | + expect(await proxied.value()).to.eq(123n); |
| 106 | + expect(await proxied.owner()).to.eq(owner); |
| 107 | + |
| 108 | + await (await beacon.upgradeTo(await implementationV2.getAddress())).wait(); |
| 109 | + |
| 110 | + expect(await proxied.version()).to.eq(2n); |
| 111 | + expect(await proxied.value()).to.eq(123n); |
| 112 | + await expectProxySlots(proxy, await beacon.getAddress(), ZeroAddress); |
| 113 | + }); |
| 114 | + |
| 115 | + it("breaks away from the beacon during a UUPS upgrade", async () => { |
| 116 | + const { |
| 117 | + owner, |
| 118 | + implementationV2, |
| 119 | + implementationV3, |
| 120 | + beacon, |
| 121 | + proxy, |
| 122 | + proxied, |
| 123 | + } = await deployFixture(); |
| 124 | + |
| 125 | + await ( |
| 126 | + await proxied.upgradeToAndCall(await implementationV2.getAddress(), "0x") |
| 127 | + ).wait(); |
| 128 | + |
| 129 | + expect(await proxied.version()).to.eq(2n); |
| 130 | + expect(await proxied.value()).to.eq(123n); |
| 131 | + expect(await proxied.owner()).to.eq(owner); |
| 132 | + await expectProxySlots( |
| 133 | + proxy, |
| 134 | + ZeroAddress, |
| 135 | + await implementationV2.getAddress(), |
| 136 | + ); |
| 137 | + |
| 138 | + await (await beacon.upgradeTo(await implementationV3.getAddress())).wait(); |
| 139 | + |
| 140 | + expect(await proxied.version()).to.eq(2n); |
| 141 | + }); |
| 142 | + |
| 143 | + it("continues to support UUPS upgrades after it is broken", async () => { |
| 144 | + const { implementationV2, implementationV3, proxy, proxied } = |
| 145 | + await deployFixture(); |
| 146 | + |
| 147 | + await ( |
| 148 | + await proxied.upgradeToAndCall(await implementationV2.getAddress(), "0x") |
| 149 | + ).wait(); |
| 150 | + await ( |
| 151 | + await proxied.upgradeToAndCall(await implementationV3.getAddress(), "0x") |
| 152 | + ).wait(); |
| 153 | + |
| 154 | + expect(await proxied.version()).to.eq(3n); |
| 155 | + await expectProxySlots( |
| 156 | + proxy, |
| 157 | + ZeroAddress, |
| 158 | + await implementationV3.getAddress(), |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it("does not break the beacon when authorization fails", async () => { |
| 163 | + const { implementationV2, beacon, proxy, proxied } = await deployFixture(); |
| 164 | + |
| 165 | + await expect( |
| 166 | + proxied |
| 167 | + .connect(otherWallet) |
| 168 | + .upgradeToAndCall(await implementationV2.getAddress(), "0x"), |
| 169 | + ).to.be.revertedWithCustomError(proxied, "NotOwner"); |
| 170 | + |
| 171 | + expect(await proxied.version()).to.eq(1n); |
| 172 | + await expectProxySlots(proxy, await beacon.getAddress(), ZeroAddress); |
| 173 | + }); |
| 174 | +}); |
0 commit comments