Migrate proxy folder to ethersjs (#4746)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Renan Souza
2023-11-29 21:51:08 +00:00
committed by GitHub
parent c35057978f
commit ae69142379
13 changed files with 785 additions and 856 deletions

View File

@ -1,220 +1,218 @@
const { expectEvent } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { expectRevertCustomError } = require('../../helpers/customError');
const { MAX_UINT64 } = require('../../helpers/constants');
const {
bigint: { MAX_UINT64 },
} = require('../../helpers/constants');
const InitializableMock = artifacts.require('InitializableMock');
const ConstructorInitializableMock = artifacts.require('ConstructorInitializableMock');
const ChildConstructorInitializableMock = artifacts.require('ChildConstructorInitializableMock');
const ReinitializerMock = artifacts.require('ReinitializerMock');
const SampleChild = artifacts.require('SampleChild');
const DisableBad1 = artifacts.require('DisableBad1');
const DisableBad2 = artifacts.require('DisableBad2');
const DisableOk = artifacts.require('DisableOk');
contract('Initializable', function () {
describe('Initializable', function () {
describe('basic testing without inheritance', function () {
beforeEach('deploying', async function () {
this.contract = await InitializableMock.new();
this.mock = await ethers.deployContract('InitializableMock');
});
describe('before initialize', function () {
it('initializer has not run', async function () {
expect(await this.contract.initializerRan()).to.equal(false);
expect(await this.mock.initializerRan()).to.be.false;
});
it('_initializing returns false before initialization', async function () {
expect(await this.contract.isInitializing()).to.equal(false);
expect(await this.mock.isInitializing()).to.be.false;
});
});
describe('after initialize', function () {
beforeEach('initializing', async function () {
await this.contract.initialize();
await this.mock.initialize();
});
it('initializer has run', async function () {
expect(await this.contract.initializerRan()).to.equal(true);
expect(await this.mock.initializerRan()).to.be.true;
});
it('_initializing returns false after initialization', async function () {
expect(await this.contract.isInitializing()).to.equal(false);
expect(await this.mock.isInitializing()).to.be.false;
});
it('initializer does not run again', async function () {
await expectRevertCustomError(this.contract.initialize(), 'InvalidInitialization', []);
await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
});
describe('nested under an initializer', function () {
it('initializer modifier reverts', async function () {
await expectRevertCustomError(this.contract.initializerNested(), 'InvalidInitialization', []);
await expect(this.mock.initializerNested()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
it('onlyInitializing modifier succeeds', async function () {
await this.contract.onlyInitializingNested();
expect(await this.contract.onlyInitializingRan()).to.equal(true);
await this.mock.onlyInitializingNested();
expect(await this.mock.onlyInitializingRan()).to.be.true;
});
});
it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
await expectRevertCustomError(this.contract.initializeOnlyInitializing(), 'NotInitializing', []);
await expect(this.mock.initializeOnlyInitializing()).to.be.revertedWithCustomError(this.mock, 'NotInitializing');
});
});
it('nested initializer can run during construction', async function () {
const contract2 = await ConstructorInitializableMock.new();
expect(await contract2.initializerRan()).to.equal(true);
expect(await contract2.onlyInitializingRan()).to.equal(true);
const mock = await ethers.deployContract('ConstructorInitializableMock');
expect(await mock.initializerRan()).to.be.true;
expect(await mock.onlyInitializingRan()).to.be.true;
});
it('multiple constructor levels can be initializers', async function () {
const contract2 = await ChildConstructorInitializableMock.new();
expect(await contract2.initializerRan()).to.equal(true);
expect(await contract2.childInitializerRan()).to.equal(true);
expect(await contract2.onlyInitializingRan()).to.equal(true);
const mock = await ethers.deployContract('ChildConstructorInitializableMock');
expect(await mock.initializerRan()).to.be.true;
expect(await mock.childInitializerRan()).to.be.true;
expect(await mock.onlyInitializingRan()).to.be.true;
});
describe('reinitialization', function () {
beforeEach('deploying', async function () {
this.contract = await ReinitializerMock.new();
this.mock = await ethers.deployContract('ReinitializerMock');
});
it('can reinitialize', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await this.contract.initialize();
expect(await this.contract.counter()).to.be.bignumber.equal('1');
await this.contract.reinitialize(2);
expect(await this.contract.counter()).to.be.bignumber.equal('2');
await this.contract.reinitialize(3);
expect(await this.contract.counter()).to.be.bignumber.equal('3');
expect(await this.mock.counter()).to.equal(0n);
await this.mock.initialize();
expect(await this.mock.counter()).to.equal(1n);
await this.mock.reinitialize(2);
expect(await this.mock.counter()).to.equal(2n);
await this.mock.reinitialize(3);
expect(await this.mock.counter()).to.equal(3n);
});
it('can jump multiple steps', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await this.contract.initialize();
expect(await this.contract.counter()).to.be.bignumber.equal('1');
await this.contract.reinitialize(128);
expect(await this.contract.counter()).to.be.bignumber.equal('2');
expect(await this.mock.counter()).to.equal(0n);
await this.mock.initialize();
expect(await this.mock.counter()).to.equal(1n);
await this.mock.reinitialize(128);
expect(await this.mock.counter()).to.equal(2n);
});
it('cannot nest reinitializers', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await expectRevertCustomError(this.contract.nestedReinitialize(2, 2), 'InvalidInitialization', []);
await expectRevertCustomError(this.contract.nestedReinitialize(2, 3), 'InvalidInitialization', []);
await expectRevertCustomError(this.contract.nestedReinitialize(3, 2), 'InvalidInitialization', []);
expect(await this.mock.counter()).to.equal(0n);
await expect(this.mock.nestedReinitialize(2, 2)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
await expect(this.mock.nestedReinitialize(2, 3)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
await expect(this.mock.nestedReinitialize(3, 2)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
});
it('can chain reinitializers', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await this.contract.chainReinitialize(2, 3);
expect(await this.contract.counter()).to.be.bignumber.equal('2');
expect(await this.mock.counter()).to.equal(0n);
await this.mock.chainReinitialize(2, 3);
expect(await this.mock.counter()).to.equal(2n);
});
it('_getInitializedVersion returns right version', async function () {
await this.contract.initialize();
expect(await this.contract.getInitializedVersion()).to.be.bignumber.equal('1');
await this.contract.reinitialize(12);
expect(await this.contract.getInitializedVersion()).to.be.bignumber.equal('12');
await this.mock.initialize();
expect(await this.mock.getInitializedVersion()).to.equal(1n);
await this.mock.reinitialize(12);
expect(await this.mock.getInitializedVersion()).to.equal(12n);
});
describe('contract locking', function () {
it('prevents initialization', async function () {
await this.contract.disableInitializers();
await expectRevertCustomError(this.contract.initialize(), 'InvalidInitialization', []);
await this.mock.disableInitializers();
await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
it('prevents re-initialization', async function () {
await this.contract.disableInitializers();
await expectRevertCustomError(this.contract.reinitialize(255), 'InvalidInitialization', []);
await this.mock.disableInitializers();
await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
it('can lock contract after initialization', async function () {
await this.contract.initialize();
await this.contract.disableInitializers();
await expectRevertCustomError(this.contract.reinitialize(255), 'InvalidInitialization', []);
await this.mock.initialize();
await this.mock.disableInitializers();
await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
});
});
describe('events', function () {
it('constructor initialization emits event', async function () {
const contract = await ConstructorInitializableMock.new();
await expectEvent.inTransaction(contract.transactionHash, contract, 'Initialized', { version: '1' });
const mock = await ethers.deployContract('ConstructorInitializableMock');
await expect(mock.deploymentTransaction()).to.emit(mock, 'Initialized').withArgs(1n);
});
it('initialization emits event', async function () {
const contract = await ReinitializerMock.new();
const { receipt } = await contract.initialize();
expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
expectEvent(receipt, 'Initialized', { version: '1' });
const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.initialize()).to.emit(mock, 'Initialized').withArgs(1n);
});
it('reinitialization emits event', async function () {
const contract = await ReinitializerMock.new();
const { receipt } = await contract.reinitialize(128);
expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
expectEvent(receipt, 'Initialized', { version: '128' });
const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.reinitialize(128)).to.emit(mock, 'Initialized').withArgs(128n);
});
it('chained reinitialization emits multiple events', async function () {
const contract = await ReinitializerMock.new();
const mock = await ethers.deployContract('ReinitializerMock');
const { receipt } = await contract.chainReinitialize(2, 3);
expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(2);
expectEvent(receipt, 'Initialized', { version: '2' });
expectEvent(receipt, 'Initialized', { version: '3' });
await expect(mock.chainReinitialize(2, 3))
.to.emit(mock, 'Initialized')
.withArgs(2n)
.to.emit(mock, 'Initialized')
.withArgs(3n);
});
});
describe('complex testing with inheritance', function () {
const mother = '12';
const mother = 12n;
const gramps = '56';
const father = '34';
const child = '78';
const father = 34n;
const child = 78n;
beforeEach('deploying', async function () {
this.contract = await SampleChild.new();
});
beforeEach('initializing', async function () {
await this.contract.initialize(mother, gramps, father, child);
this.mock = await ethers.deployContract('SampleChild');
await this.mock.initialize(mother, gramps, father, child);
});
it('initializes human', async function () {
expect(await this.contract.isHuman()).to.be.equal(true);
expect(await this.mock.isHuman()).to.be.true;
});
it('initializes mother', async function () {
expect(await this.contract.mother()).to.be.bignumber.equal(mother);
expect(await this.mock.mother()).to.equal(mother);
});
it('initializes gramps', async function () {
expect(await this.contract.gramps()).to.be.bignumber.equal(gramps);
expect(await this.mock.gramps()).to.equal(gramps);
});
it('initializes father', async function () {
expect(await this.contract.father()).to.be.bignumber.equal(father);
expect(await this.mock.father()).to.equal(father);
});
it('initializes child', async function () {
expect(await this.contract.child()).to.be.bignumber.equal(child);
expect(await this.mock.child()).to.equal(child);
});
});
describe('disabling initialization', function () {
it('old and new patterns in bad sequence', async function () {
await expectRevertCustomError(DisableBad1.new(), 'InvalidInitialization', []);
await expectRevertCustomError(DisableBad2.new(), 'InvalidInitialization', []);
const DisableBad1 = await ethers.getContractFactory('DisableBad1');
await expect(DisableBad1.deploy()).to.be.revertedWithCustomError(DisableBad1, 'InvalidInitialization');
const DisableBad2 = await ethers.getContractFactory('DisableBad2');
await expect(DisableBad2.deploy()).to.be.revertedWithCustomError(DisableBad2, 'InvalidInitialization');
});
it('old and new patterns in good sequence', async function () {
const ok = await DisableOk.new();
await expectEvent.inConstruction(ok, 'Initialized', { version: '1' });
await expectEvent.inConstruction(ok, 'Initialized', { version: MAX_UINT64 });
const ok = await ethers.deployContract('DisableOk');
await expect(ok.deploymentTransaction())
.to.emit(ok, 'Initialized')
.withArgs(1n)
.to.emit(ok, 'Initialized')
.withArgs(MAX_UINT64);
});
});
});

View File

@ -1,28 +1,36 @@
const { expectEvent } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967');
const { expectRevertCustomError } = require('../../helpers/customError');
const ERC1967Proxy = artifacts.require('ERC1967Proxy');
const UUPSUpgradeableMock = artifacts.require('UUPSUpgradeableMock');
const UUPSUpgradeableUnsafeMock = artifacts.require('UUPSUpgradeableUnsafeMock');
const NonUpgradeableMock = artifacts.require('NonUpgradeableMock');
const UUPSUnsupportedProxiableUUID = artifacts.require('UUPSUnsupportedProxiableUUID');
const Clones = artifacts.require('$Clones');
async function fixture() {
const implInitial = await ethers.deployContract('UUPSUpgradeableMock');
const implUpgradeOk = await ethers.deployContract('UUPSUpgradeableMock');
const implUpgradeUnsafe = await ethers.deployContract('UUPSUpgradeableUnsafeMock');
const implUpgradeNonUUPS = await ethers.deployContract('NonUpgradeableMock');
const implUnsupportedUUID = await ethers.deployContract('UUPSUnsupportedProxiableUUID');
// Used for testing non ERC1967 compliant proxies (clones are proxies that don't use the ERC1967 implementation slot)
const cloneFactory = await ethers.deployContract('$Clones');
contract('UUPSUpgradeable', function () {
before(async function () {
this.implInitial = await UUPSUpgradeableMock.new();
this.implUpgradeOk = await UUPSUpgradeableMock.new();
this.implUpgradeUnsafe = await UUPSUpgradeableUnsafeMock.new();
this.implUpgradeNonUUPS = await NonUpgradeableMock.new();
this.implUnsupportedUUID = await UUPSUnsupportedProxiableUUID.new();
// Used for testing non ERC1967 compliant proxies (clones are proxies that don't use the ERC1967 implementation slot)
this.cloneFactory = await Clones.new();
});
const instance = await ethers
.deployContract('ERC1967Proxy', [implInitial, '0x'])
.then(proxy => implInitial.attach(proxy.target));
return {
implInitial,
implUpgradeOk,
implUpgradeUnsafe,
implUpgradeNonUUPS,
implUnsupportedUUID,
cloneFactory,
instance,
};
}
describe('UUPSUpgradeable', function () {
beforeEach(async function () {
const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
this.instance = await UUPSUpgradeableMock.at(address);
Object.assign(this, await loadFixture(fixture));
});
it('has an interface version', async function () {
@ -30,102 +38,83 @@ contract('UUPSUpgradeable', function () {
});
it('upgrade to upgradeable implementation', async function () {
const { receipt } = await this.instance.upgradeToAndCall(this.implUpgradeOk.address, '0x');
expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
await expect(this.instance.upgradeToAndCall(this.implUpgradeOk, '0x'))
.to.emit(this.instance, 'Upgraded')
.withArgs(this.implUpgradeOk.target);
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeOk.target);
});
it('upgrade to upgradeable implementation with call', async function () {
expect(await this.instance.current()).to.be.bignumber.equal('0');
expect(await this.instance.current()).to.equal(0n);
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 getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
await expect(
this.instance.upgradeToAndCall(this.implUpgradeOk, this.implUpgradeOk.interface.encodeFunctionData('increment')),
)
.to.emit(this.instance, 'Upgraded')
.withArgs(this.implUpgradeOk.target);
expect(await this.instance.current()).to.be.bignumber.equal('1');
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeOk.target);
expect(await this.instance.current()).to.equal(1n);
});
it('calling upgradeTo on the implementation reverts', async function () {
await expectRevertCustomError(
this.implInitial.upgradeToAndCall(this.implUpgradeOk.address, '0x'),
await expect(this.implInitial.upgradeToAndCall(this.implUpgradeOk, '0x')).to.be.revertedWithCustomError(
this.implInitial,
'UUPSUnauthorizedCallContext',
[],
);
});
it('calling upgradeToAndCall on the implementation reverts', async function () {
await expectRevertCustomError(
await expect(
this.implInitial.upgradeToAndCall(
this.implUpgradeOk.address,
this.implUpgradeOk.contract.methods.increment().encodeABI(),
this.implUpgradeOk,
this.implUpgradeOk.interface.encodeFunctionData('increment'),
),
'UUPSUnauthorizedCallContext',
[],
);
});
it('calling upgradeTo from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
const receipt = await this.cloneFactory.$clone(this.implUpgradeOk.address);
const instance = await UUPSUpgradeableMock.at(
receipt.logs.find(({ event }) => event === 'return$clone').args.instance,
);
await expectRevertCustomError(
instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'),
'UUPSUnauthorizedCallContext',
[],
);
).to.be.revertedWithCustomError(this.implUpgradeOk, 'UUPSUnauthorizedCallContext');
});
it('calling upgradeToAndCall from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
const receipt = await this.cloneFactory.$clone(this.implUpgradeOk.address);
const instance = await UUPSUpgradeableMock.at(
receipt.logs.find(({ event }) => event === 'return$clone').args.instance,
);
const instance = await this.cloneFactory.$clone
.staticCall(this.implUpgradeOk)
.then(address => this.implInitial.attach(address));
await this.cloneFactory.$clone(this.implUpgradeOk);
await expectRevertCustomError(
instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'),
await expect(instance.upgradeToAndCall(this.implUpgradeUnsafe, '0x')).to.be.revertedWithCustomError(
instance,
'UUPSUnauthorizedCallContext',
[],
);
});
it('rejects upgrading to an unsupported UUID', async function () {
await expectRevertCustomError(
this.instance.upgradeToAndCall(this.implUnsupportedUUID.address, '0x'),
'UUPSUnsupportedProxiableUUID',
[web3.utils.keccak256('invalid UUID')],
);
await expect(this.instance.upgradeToAndCall(this.implUnsupportedUUID, '0x'))
.to.be.revertedWithCustomError(this.instance, 'UUPSUnsupportedProxiableUUID')
.withArgs(ethers.id('invalid UUID'));
});
it('upgrade to and unsafe upgradeable implementation', async function () {
const { receipt } = await this.instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x');
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeUnsafe.address });
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeUnsafe.address);
await expect(this.instance.upgradeToAndCall(this.implUpgradeUnsafe, '0x'))
.to.emit(this.instance, 'Upgraded')
.withArgs(this.implUpgradeUnsafe.target);
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeUnsafe.target);
});
// delegate to a non existing upgradeTo function causes a low level revert
it('reject upgrade to non uups implementation', async function () {
await expectRevertCustomError(
this.instance.upgradeToAndCall(this.implUpgradeNonUUPS.address, '0x'),
'ERC1967InvalidImplementation',
[this.implUpgradeNonUUPS.address],
);
await expect(this.instance.upgradeToAndCall(this.implUpgradeNonUUPS, '0x'))
.to.be.revertedWithCustomError(this.instance, 'ERC1967InvalidImplementation')
.withArgs(this.implUpgradeNonUUPS.target);
});
it('reject proxy address as implementation', async function () {
const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
const otherInstance = await UUPSUpgradeableMock.at(address);
const otherInstance = await ethers
.deployContract('ERC1967Proxy', [this.implInitial, '0x'])
.then(proxy => this.implInitial.attach(proxy.target));
await expectRevertCustomError(
this.instance.upgradeToAndCall(otherInstance.address, '0x'),
'ERC1967InvalidImplementation',
[otherInstance.address],
);
await expect(this.instance.upgradeToAndCall(otherInstance, '0x'))
.to.be.revertedWithCustomError(this.instance, 'ERC1967InvalidImplementation')
.withArgs(otherInstance.target);
});
});