Improve Initializable readability using intermediate variables (#4576)
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
5
.changeset/lazy-rice-joke.md
Normal file
5
.changeset/lazy-rice-joke.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'openzeppelin-solidity': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
`Initializable`: Use intermediate variables to improve readability.
|
||||||
@ -104,9 +104,19 @@ abstract contract Initializable {
|
|||||||
// solhint-disable-next-line var-name-mixedcase
|
// solhint-disable-next-line var-name-mixedcase
|
||||||
InitializableStorage storage $ = _getInitializableStorage();
|
InitializableStorage storage $ = _getInitializableStorage();
|
||||||
|
|
||||||
|
// Cache values to avoid duplicated sloads
|
||||||
bool isTopLevelCall = !$._initializing;
|
bool isTopLevelCall = !$._initializing;
|
||||||
uint64 initialized = $._initialized;
|
uint64 initialized = $._initialized;
|
||||||
if (!(isTopLevelCall && initialized < 1) && !(address(this).code.length == 0 && initialized == 1)) {
|
|
||||||
|
// Allowed calls:
|
||||||
|
// - initialSetup: the contract is not in the initializing state and no previous version was
|
||||||
|
// initialized
|
||||||
|
// - construction: the contract is initialized at version 1 (no reininitialization) and the
|
||||||
|
// current contract is just being deployed
|
||||||
|
bool initialSetup = initialized == 0 && isTopLevelCall;
|
||||||
|
bool construction = initialized == 1 && address(this).code.length == 0;
|
||||||
|
|
||||||
|
if (!initialSetup && !construction) {
|
||||||
revert AlreadyInitialized();
|
revert AlreadyInitialized();
|
||||||
}
|
}
|
||||||
$._initialized = 1;
|
$._initialized = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user