* 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>
91 lines
2.0 KiB
Solidity
91 lines
2.0 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
import "../proxy/utils/Initializable.sol";
|
|
|
|
/**
|
|
* @title InitializableMock
|
|
* @dev This contract is a mock to test initializable functionality
|
|
*/
|
|
contract InitializableMock is Initializable {
|
|
bool public initializerRan;
|
|
bool public onlyInitializingRan;
|
|
uint256 public x;
|
|
|
|
function initialize() public initializer {
|
|
initializerRan = true;
|
|
}
|
|
|
|
function initializeOnlyInitializing() public onlyInitializing {
|
|
onlyInitializingRan = true;
|
|
}
|
|
|
|
function initializerNested() public initializer {
|
|
initialize();
|
|
}
|
|
|
|
function onlyInitializingNested() public initializer {
|
|
initializeOnlyInitializing();
|
|
}
|
|
|
|
function initializeWithX(uint256 _x) public payable initializer {
|
|
x = _x;
|
|
}
|
|
|
|
function nonInitializable(uint256 _x) public payable {
|
|
x = _x;
|
|
}
|
|
|
|
function fail() public pure {
|
|
require(false, "InitializableMock forced failure");
|
|
}
|
|
}
|
|
|
|
contract ConstructorInitializableMock is Initializable {
|
|
bool public initializerRan;
|
|
bool public onlyInitializingRan;
|
|
|
|
constructor() initializer {
|
|
initialize();
|
|
initializeOnlyInitializing();
|
|
}
|
|
|
|
function initialize() public initializer {
|
|
initializerRan = true;
|
|
}
|
|
|
|
function initializeOnlyInitializing() public onlyInitializing {
|
|
onlyInitializingRan = true;
|
|
}
|
|
}
|
|
|
|
contract ReinitializerMock is Initializable {
|
|
uint256 public counter;
|
|
|
|
function initialize() public initializer {
|
|
doStuff();
|
|
}
|
|
|
|
function reinitialize(uint8 i) public reinitializer(i) {
|
|
doStuff();
|
|
}
|
|
|
|
function nestedReinitialize(uint8 i, uint8 j) public reinitializer(i) {
|
|
reinitialize(j);
|
|
}
|
|
|
|
function chainReinitialize(uint8 i, uint8 j) public {
|
|
reinitialize(i);
|
|
reinitialize(j);
|
|
}
|
|
|
|
function disableInitializers() public {
|
|
_disableInitializers();
|
|
}
|
|
|
|
function doStuff() public onlyInitializing {
|
|
counter++;
|
|
}
|
|
}
|