Allow the re-initialization of contracts (#3232)
* allow re-initialization of contracts * fix lint * use a private function to avoid code duplication * use oz-retyped-from syntax * add documentation * rephrase * documentation * Update contracts/proxy/utils/Initializable.sol Co-authored-by: Francisco Giordano <frangio.1@gmail.com> * reinitialize test * lint * typos and style * add note about relation between initializer and reinitializer * lint * set _initializing in the modifier * remove unnecessary variable set * rename _preventInitialize -> _disableInitializers * rename preventInitialize -> disableInitializers * test nested reinitializers in reverse order * docs typos and style * edit docs for consistency between initializer and reinitializer Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
const { expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { assert } = require('chai');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const InitializableMock = artifacts.require('InitializableMock');
|
||||
const ConstructorInitializableMock = artifacts.require('ConstructorInitializableMock');
|
||||
const ReinitializerMock = artifacts.require('ReinitializerMock');
|
||||
const SampleChild = artifacts.require('SampleChild');
|
||||
|
||||
contract('Initializable', function (accounts) {
|
||||
@ -13,7 +14,7 @@ contract('Initializable', function (accounts) {
|
||||
|
||||
context('before initialize', function () {
|
||||
it('initializer has not run', async function () {
|
||||
assert.isFalse(await this.contract.initializerRan());
|
||||
expect(await this.contract.initializerRan()).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
@ -23,7 +24,7 @@ contract('Initializable', function (accounts) {
|
||||
});
|
||||
|
||||
it('initializer has run', async function () {
|
||||
assert.isTrue(await this.contract.initializerRan());
|
||||
expect(await this.contract.initializerRan()).to.equal(true);
|
||||
});
|
||||
|
||||
it('initializer does not run again', async function () {
|
||||
@ -38,7 +39,7 @@ contract('Initializable', function (accounts) {
|
||||
|
||||
it('onlyInitializing modifier succeeds', async function () {
|
||||
await this.contract.onlyInitializingNested();
|
||||
assert.isTrue(await this.contract.onlyInitializingRan());
|
||||
expect(await this.contract.onlyInitializingRan()).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
@ -49,15 +50,69 @@ contract('Initializable', function (accounts) {
|
||||
|
||||
it('nested initializer can run during construction', async function () {
|
||||
const contract2 = await ConstructorInitializableMock.new();
|
||||
assert.isTrue(await contract2.initializerRan());
|
||||
assert.isTrue(await contract2.onlyInitializingRan());
|
||||
expect(await contract2.initializerRan()).to.equal(true);
|
||||
expect(await contract2.onlyInitializingRan()).to.equal(true);
|
||||
});
|
||||
|
||||
describe('reinitialization', function () {
|
||||
beforeEach('deploying', async function () {
|
||||
this.contract = await ReinitializerMock.new();
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
it('cannot nest reinitializers', async function () {
|
||||
expect(await this.contract.counter()).to.be.bignumber.equal('0');
|
||||
await expectRevert(this.contract.nestedReinitialize(2, 3), 'Initializable: contract is already initialized');
|
||||
await expectRevert(this.contract.nestedReinitialize(3, 2), 'Initializable: contract is already initialized');
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
describe('contract locking', function () {
|
||||
it('prevents initialization', async function () {
|
||||
await this.contract.disableInitializers();
|
||||
await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
|
||||
});
|
||||
|
||||
it('prevents re-initialization', async function () {
|
||||
await this.contract.disableInitializers();
|
||||
await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
|
||||
});
|
||||
|
||||
it('can lock contract after initialization', async function () {
|
||||
await this.contract.initialize();
|
||||
await this.contract.disableInitializers();
|
||||
await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('complex testing with inheritance', function () {
|
||||
const mother = 12;
|
||||
const mother = '12';
|
||||
const gramps = '56';
|
||||
const father = 34;
|
||||
const child = 78;
|
||||
const father = '34';
|
||||
const child = '78';
|
||||
|
||||
beforeEach('deploying', async function () {
|
||||
this.contract = await SampleChild.new();
|
||||
@ -68,23 +123,23 @@ contract('Initializable', function (accounts) {
|
||||
});
|
||||
|
||||
it('initializes human', async function () {
|
||||
assert.equal(await this.contract.isHuman(), true);
|
||||
expect(await this.contract.isHuman()).to.be.equal(true);
|
||||
});
|
||||
|
||||
it('initializes mother', async function () {
|
||||
assert.equal(await this.contract.mother(), mother);
|
||||
expect(await this.contract.mother()).to.be.bignumber.equal(mother);
|
||||
});
|
||||
|
||||
it('initializes gramps', async function () {
|
||||
assert.equal(await this.contract.gramps(), gramps);
|
||||
expect(await this.contract.gramps()).to.be.bignumber.equal(gramps);
|
||||
});
|
||||
|
||||
it('initializes father', async function () {
|
||||
assert.equal(await this.contract.father(), father);
|
||||
expect(await this.contract.father()).to.be.bignumber.equal(father);
|
||||
});
|
||||
|
||||
it('initializes child', async function () {
|
||||
assert.equal(await this.contract.child(), child);
|
||||
expect(await this.contract.child()).to.be.bignumber.equal(child);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user