Skip to content

Commit

Permalink
refactor: proxy mocks simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybuidl committed Oct 16, 2023
1 parent c98a69d commit 2a0bea5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,58 +62,3 @@ contract UUPSUnsupportedProxiableUUID is UUPSUpgradeableMock {
return "UUPSUnsupportedProxiableUUID 1.0.0";
}
}

contract UUPSUpgradableInitializableInheritanceV1 is UUPSProxiable, Initializable {
address public governor;
uint256 public counter;
uint256[50] __gap;

constructor() {
_disableInitializers();
}

function initialize(address _governor) external virtual reinitializer(1) {
governor = _governor;
counter = 1;
}

function _authorizeUpgrade(address) internal view override {
require(governor == msg.sender, "No privilege to upgrade");
}

function increment() external {
++counter;
}

function version() external pure virtual returns (string memory) {
return "V1";
}
}

contract UUPSUpgradableInitializableInheritanceV2 is UUPSUpgradableInitializableInheritanceV1 {
string public newVariable;
uint256[50] __gap2;

constructor() {
_disableInitializers();
}

function initializeV2(string memory _newVariable) external reinitializer(2) {
newVariable = _newVariable;
this.increment();
}

function version() external pure virtual override returns (string memory) {
return "V2";
}
}

contract UUPSUpgradableInitializableInheritanceV3Bad is UUPSUpgradableInitializableInheritanceV2 {
constructor() {
_disableInitializers();
}

function initializeV3() external reinitializer(1) {
// Wrong reinitializer version.
}
}
62 changes: 62 additions & 0 deletions contracts/src/proxy/mock/by-inheritance/UpgradedByInheritance.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//SPDX-License-Identifier: MIT
// Adapted from <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/mocks/proxy/UUPSUpgradeableMock.sol>

pragma solidity 0.8.18;

import "../../UUPSProxiable.sol";
import "../../Initializable.sol";

contract UpgradedByInheritanceV1 is UUPSProxiable, Initializable {
address public governor;
uint256 public counter;
uint256[50] __gap;

constructor() {
_disableInitializers();
}

function initialize(address _governor) external virtual reinitializer(1) {
governor = _governor;
counter = 1;
}

function _authorizeUpgrade(address) internal view override {
require(governor == msg.sender, "No privilege to upgrade");
}

function increment() external {
++counter;
}

function version() external pure virtual returns (string memory) {
return "V1";
}
}

contract UpgradedByInheritanceV2 is UpgradedByInheritanceV1 {
string public newVariable;
uint256[50] __gap2;

constructor() {
_disableInitializers();
}

function initializeV2(string memory _newVariable) external reinitializer(2) {
newVariable = _newVariable;
this.increment();
}

function version() external pure virtual override returns (string memory) {
return "V2";
}
}

contract UpgradedByInheritanceV3Bad is UpgradedByInheritanceV2 {
constructor() {
_disableInitializers();
}

function initializeV3() external reinitializer(1) {
// Wrong reinitializer version.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

pragma solidity 0.8.18;

import "../UUPSProxiable.sol";
import "../Initializable.sol";
import "../../UUPSProxiable.sol";
import "../../Initializable.sol";

contract UUPSUpgradableInitializable is UUPSProxiable, Initializable {
contract UpgradedByRewrite is UUPSProxiable, Initializable {
//------------------------
// V1 State
//------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

pragma solidity 0.8.18;

import "../UUPSProxiable.sol";
import "../Initializable.sol";
import "../../UUPSProxiable.sol";
import "../../Initializable.sol";

contract UUPSUpgradableInitializable is UUPSProxiable, Initializable {
contract UpgradedByRewrite is UUPSProxiable, Initializable {
//------------------------
// V1 State
//------------------------
Expand Down
45 changes: 18 additions & 27 deletions contracts/test/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { log } from "console";
import { ethers, deployments } from "hardhat";
import { DeployResult } from "hardhat-deploy/types";
import { deployUpgradable } from "../../deploy/utils/deployUpgradable";
import {
UUPSUpgradableInitializableInheritanceV1,
UUPSUpgradableInitializableInheritanceV2,
} from "../../typechain-types";
import { UpgradedByInheritanceV1, UpgradedByInheritanceV2 } from "../../typechain-types";
import { UpgradedByRewrite as UpgradedByRewriteV1 } from "../../typechain-types/src/proxy/mock/by-rewrite";
import { UpgradedByRewrite as UpgradedByRewriteV2 } from "../../typechain-types/src/proxy/mock/by-rewrite/UpgradedByRewriteV2.sol";

let deployer;
let user1;
Expand Down Expand Up @@ -119,8 +118,8 @@ describe("Upgradability", async () => {
before("Setup Contracts", async () => {
[deployer] = await ethers.getSigners();

proxyDeployment = await deployUpgradable(deployments, "UUPSUpgradableInitializable", {
contract: "src/proxy/mock/UUPSUpgradableInitializable.sol:UUPSUpgradableInitializable",
proxyDeployment = await deployUpgradable(deployments, "UpgradedByRewrite", {
contract: "src/proxy/mock/by-rewrite/UpgradedByRewrite.sol:UpgradedByRewrite",
from: deployer.address,
args: [deployer.address],
log: true,
Expand All @@ -131,11 +130,9 @@ describe("Upgradability", async () => {
});

it("Initializes v1", async () => {
proxy = (await ethers.getContract("UUPSUpgradableInitializable")) as UUPSUpgradableInitializableInheritanceV1;
proxy = (await ethers.getContract("UpgradedByRewrite")) as UpgradedByRewriteV1;

implementation = (await ethers.getContract(
"UUPSUpgradableInitializable_Implementation"
)) as UUPSUpgradableInitializableInheritanceV1;
implementation = (await ethers.getContract("UpgradedByRewrite_Implementation")) as UpgradedByRewriteV1;

expect(await proxy.governor()).to.equal(deployer.address);

Expand All @@ -150,16 +147,16 @@ describe("Upgradability", async () => {
});

it("Upgrades to v2 and initializes", async () => {
proxyDeployment = await deployUpgradable(deployments, "UUPSUpgradableInitializable", {
contract: "src/proxy/mock/UUPSUpgradableInitializableV2.sol:UUPSUpgradableInitializable",
proxyDeployment = await deployUpgradable(deployments, "UpgradedByRewrite", {
contract: "src/proxy/mock/by-rewrite/UpgradedByRewriteV2.sol:UpgradedByRewrite",
from: deployer.address,
args: ["Future of France"],
log: true,
});
if (!proxyDeployment.implementation) {
throw new Error("No implementation address");
}
proxy = (await ethers.getContract("UUPSUpgradableInitializable")) as UUPSUpgradableInitializableInheritanceV2;
proxy = (await ethers.getContract("UpgradedByRewrite")) as UpgradedByRewriteV2;
expect(await proxy.governor()).to.equal(deployer.address);

expect(await proxy.counter()).to.equal(3);
Expand All @@ -176,7 +173,7 @@ describe("Upgradability", async () => {
before("Setup Contracts", async () => {
[deployer] = await ethers.getSigners();

proxyDeployment = await deployUpgradable(deployments, "UUPSUpgradableInitializableInheritanceV1", {
proxyDeployment = await deployUpgradable(deployments, "UpgradedByInheritanceV1", {
from: deployer.address,
args: [deployer.address],
log: true,
Expand All @@ -187,13 +184,9 @@ describe("Upgradability", async () => {
});

it("Initializes v1", async () => {
proxy = (await ethers.getContract(
"UUPSUpgradableInitializableInheritanceV1"
)) as UUPSUpgradableInitializableInheritanceV1;
proxy = (await ethers.getContract("UpgradedByInheritanceV1")) as UpgradedByInheritanceV1;

implementation = (await ethers.getContract(
"UUPSUpgradableInitializableInheritanceV1_Implementation"
)) as UUPSUpgradableInitializableInheritanceV1;
implementation = (await ethers.getContract("UpgradedByInheritanceV1_Implementation")) as UpgradedByInheritanceV1;

expect(await proxy.governor()).to.equal(deployer.address);

Expand All @@ -208,17 +201,15 @@ describe("Upgradability", async () => {
});

it("Upgrades to v2 and initializes", async () => {
proxyDeployment = await deployUpgradable(deployments, "UUPSUpgradableInitializableInheritanceV1", {
newImplementation: "UUPSUpgradableInitializableInheritanceV2",
proxyDeployment = await deployUpgradable(deployments, "UpgradedByInheritanceV1", {
newImplementation: "UpgradedByInheritanceV2",
initializer: "initializeV2",
from: deployer.address,
args: ["Future of France"],
log: true,
});

proxy = (await ethers.getContract(
"UUPSUpgradableInitializableInheritanceV1"
)) as UUPSUpgradableInitializableInheritanceV2;
proxy = (await ethers.getContract("UpgradedByInheritanceV1")) as UpgradedByInheritanceV2;

expect(await proxy.governor()).to.equal(deployer.address);

Expand All @@ -233,8 +224,8 @@ describe("Upgradability", async () => {

it("Cannot upgrade to v3 which has an invalid initializer", async () => {
await expect(
deployUpgradable(deployments, "UUPSUpgradableInitializableInheritanceV1", {
newImplementation: "UUPSUpgradableInitializableInheritanceV3Bad",
deployUpgradable(deployments, "UpgradedByInheritanceV1", {
newImplementation: "UpgradedByInheritanceV3Bad",
initializer: "initializeV3",
from: deployer.address,
args: [],
Expand Down

0 comments on commit 2a0bea5

Please sign in to comment.