Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the re-initialization of contracts #3232

Merged
merged 20 commits into from
Mar 22, 2022
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions contracts/proxy/utils/Initializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import "../../utils/Address.sol";
abstract contract Initializable {
frangio marked this conversation as resolved.
Show resolved Hide resolved
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
bool private _initialized;
uint8 private _initialized;

/**
* @dev Indicates that the contract is in the process of being initialized.
Expand All @@ -47,19 +48,21 @@ abstract contract Initializable {
* @dev Modifier to protect an initializer function from being invoked twice.
*/
modifier initializer() {
// If the contract is initializing we ignore whether _initialized is set in order to support multiple
// inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
// contract may have been reentered.
require(_initializing ? _isConstructor() : !_initialized, "Initializable: contract is already initialized");

bool isTopLevelCall = !_initializing;
bool isTopLevelCall = _setInitializeVestion(1);
_;
if (isTopLevelCall) {
_initializing = true;
_initialized = true;
_initializing = false;
}
}

/**
* @dev Modifier to allow the execution of `onlyInitializing`-protected methods, after the original initialization
* step, through "reinitializer". These "reinitializer" are essential to configure modules that are added through
* upgrades and that require an initialization step.
*/
modifier reinitializer(uint8 version) {
bool isTopLevelCall = _setInitializeVestion(version);
_;

if (isTopLevelCall) {
_initializing = false;
}
Expand All @@ -74,7 +77,18 @@ abstract contract Initializable {
_;
}

function _isConstructor() private view returns (bool) {
return !Address.isContract(address(this));
function _setInitializeVestion(uint8 version) private returns (bool) {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
// If the contract is initializing we ignore whether _initialized is set in order to support multiple
// inheritance patterns, but we only do this in the context of a constructor, and for the lowest level
// of initializers, because in other contexts the contract may have been reentered.
if (_initializing) {
require(version == 1 && !Address.isContract(address(this)), "Initializable: contract is already initialized");
Amxx marked this conversation as resolved.
Show resolved Hide resolved
return false;
} else {
require(_initialized < version, "Initializable: contract is already initialized");
_initializing = true;
_initialized = version;
return true;
}
}
}