Implement UUPS proxy (ERC1822) (#2542)

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
Hadrien Croubois
2021-04-16 16:41:47 +02:00
committed by GitHub
parent 538b6d21b1
commit 1c676ac0ec
16 changed files with 595 additions and 134 deletions

View File

@ -0,0 +1,72 @@
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const ERC1967Proxy = artifacts.require('ERC1967Proxy');
const UUPSUpgradeableMock = artifacts.require('UUPSUpgradeableMock');
const UUPSUpgradeableUnsafeMock = artifacts.require('UUPSUpgradeableUnsafeMock');
const UUPSUpgradeableBrokenMock = artifacts.require('UUPSUpgradeableBrokenMock');
const CountersImpl = artifacts.require('CountersImpl');
contract('UUPSUpgradeable', function (accounts) {
before(async function () {
this.implInitial = await UUPSUpgradeableMock.new();
this.implUpgradeOk = await UUPSUpgradeableMock.new();
this.implUpgradeUnsafe = await UUPSUpgradeableUnsafeMock.new();
this.implUpgradeBroken = await UUPSUpgradeableBrokenMock.new();
this.implUpgradeNonUUPS = await CountersImpl.new();
});
beforeEach(async function () {
const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
this.instance = await UUPSUpgradeableMock.at(address);
});
it('upgrade to upgradeable implementation', async function () {
const { receipt } = await this.instance.upgradeTo(this.implUpgradeOk.address);
expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
});
it('upgrade to upgradeable implementation with call', async function () {
expect(await this.instance.current()).to.be.bignumber.equal('0');
const { receipt } = await this.instance.upgradeToAndCall(
this.implUpgradeOk.address,
this.implUpgradeOk.contract.methods.increment().encodeABI(),
);
expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
expect(await this.instance.current()).to.be.bignumber.equal('1');
});
it('upgrade to and unsafe upgradeable implementation', async function () {
const { receipt } = await this.instance.upgradeTo(this.implUpgradeUnsafe.address);
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeUnsafe.address });
});
it('reject upgrade to broken upgradeable implementation', async function () {
await expectRevert(
this.instance.upgradeTo(this.implUpgradeBroken.address),
'ERC1967Upgrade: upgrade breaks further upgrades',
);
});
// delegate to a non existing upgradeTo function causes a low level revert
it('reject upgrade to non uups implementation', async function () {
await expectRevert(
this.instance.upgradeTo(this.implUpgradeNonUUPS.address),
'Address: low-level delegate call failed',
);
});
it('reject proxy address as implementation', async function () {
const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
const otherInstance = await UUPSUpgradeableMock.at(address);
// infinite loop reverts when a nested call is out-of-gas
await expectRevert(
this.instance.upgradeTo(otherInstance.address),
'Address: low-level delegate call failed',
);
});
});

View File

@ -25,7 +25,7 @@ contract('BeaconProxy', function (accounts) {
it('non-contract beacon', async function () {
await expectRevert(
BeaconProxy.new(anotherAccount, '0x'),
'BeaconProxy: beacon is not a contract',
'ERC1967: new beacon is not a contract',
);
});
@ -40,7 +40,7 @@ contract('BeaconProxy', function (accounts) {
const beacon = await BadBeaconNotContract.new();
await expectRevert(
BeaconProxy.new(beacon.address, '0x'),
'BeaconProxy: beacon implementation is not a contract',
'ERC1967: beacon implementation is not a contract',
);
});
});

View File

@ -80,7 +80,7 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
it('reverts', async function () {
await expectRevert(
this.proxy.upgradeTo(ZERO_ADDRESS, { from }),
'ERC1967Proxy: new implementation is not a contract',
'ERC1967: new implementation is not a contract',
);
});
});
@ -304,7 +304,7 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
it('reverts', async function () {
await expectRevert(
this.proxy.changeAdmin(ZERO_ADDRESS, { from: proxyAdminAddress }),
'TransparentUpgradeableProxy: new admin is the zero address',
'ERC1967: new admin is the zero address',
);
});
});

View File

@ -0,0 +1,110 @@
const { constants, BN } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const StorageSlotMock = artifacts.require('StorageSlotMock');
const slot = web3.utils.keccak256('some.storage.slot');
const otherSlot = web3.utils.keccak256('some.other.storage.slot');
contract('StorageSlot', function (accounts) {
beforeEach(async function () {
this.store = await StorageSlotMock.new();
});
describe('boolean storage slot', function () {
beforeEach(async function () {
this.value = true;
});
it('set', async function () {
await this.store.setBoolean(slot, this.value);
});
describe('get', function () {
beforeEach(async function () {
await this.store.setBoolean(slot, this.value);
});
it('from right slot', async function () {
expect(await this.store.getBoolean(slot)).to.be.equal(this.value);
});
it('from other slot', async function () {
expect(await this.store.getBoolean(otherSlot)).to.be.equal(false);
});
});
});
describe('address storage slot', function () {
beforeEach(async function () {
this.value = accounts[1];
});
it('set', async function () {
await this.store.setAddress(slot, this.value);
});
describe('get', function () {
beforeEach(async function () {
await this.store.setAddress(slot, this.value);
});
it('from right slot', async function () {
expect(await this.store.getAddress(slot)).to.be.equal(this.value);
});
it('from other slot', async function () {
expect(await this.store.getAddress(otherSlot)).to.be.equal(constants.ZERO_ADDRESS);
});
});
});
describe('bytes32 storage slot', function () {
beforeEach(async function () {
this.value = web3.utils.keccak256('some byte32 value');
});
it('set', async function () {
await this.store.setBytes32(slot, this.value);
});
describe('get', function () {
beforeEach(async function () {
await this.store.setBytes32(slot, this.value);
});
it('from right slot', async function () {
expect(await this.store.getBytes32(slot)).to.be.equal(this.value);
});
it('from other slot', async function () {
expect(await this.store.getBytes32(otherSlot)).to.be.equal(constants.ZERO_BYTES32);
});
});
});
describe('uint256 storage slot', function () {
beforeEach(async function () {
this.value = new BN(1742);
});
it('set', async function () {
await this.store.setUint256(slot, this.value);
});
describe('get', function () {
beforeEach(async function () {
await this.store.setUint256(slot, this.value);
});
it('from right slot', async function () {
expect(await this.store.getUint256(slot)).to.be.bignumber.equal(this.value);
});
it('from other slot', async function () {
expect(await this.store.getUint256(otherSlot)).to.be.bignumber.equal('0');
});
});
});
});