From b7de50c2f14082ee654ccbbdb27c50ff28fd54b0 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 5 Sep 2023 15:47:05 +0200 Subject: [PATCH 1/4] Improve Initializable readability using intermediate variables --- .changeset/lazy-rice-joke.md | 5 +++++ contracts/proxy/utils/Initializable.sol | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/lazy-rice-joke.md diff --git a/.changeset/lazy-rice-joke.md b/.changeset/lazy-rice-joke.md new file mode 100644 index 00000000000..e1d34d455a0 --- /dev/null +++ b/.changeset/lazy-rice-joke.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': minor +--- + +`Initializable`: introduce intermediate variable to improve readability diff --git a/contracts/proxy/utils/Initializable.sol b/contracts/proxy/utils/Initializable.sol index cc6d9c9624c..11f79d03acb 100644 --- a/contracts/proxy/utils/Initializable.sol +++ b/contracts/proxy/utils/Initializable.sol @@ -104,9 +104,19 @@ abstract contract Initializable { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); + // Cache values to avoid duplicated sloads bool isTopLevelCall = !$._initializing; uint64 initialized = $._initialized; - if (!(isTopLevelCall && initialized < 1) && !(address(this).code.length == 0 && initialized == 1)) { + + // Are allowed: + // - "initialSetup" calls: the contract is not in the initializing state and no previous version was + // initialized + // - "construction" calls: the contract is already initialized at step 1 (no reininitialization) and the + // current contract is not yet constructed. + bool initialSetup = initialized == 0 && isTopLevelCall; + bool construction = initialized == 1 && address(this).code.length == 0; + + if (!firstCall && !construction) { revert AlreadyInitialized(); } $._initialized = 1; From 9249a0bd9e8bd60ae68680e9c410a9a3eb80d28e Mon Sep 17 00:00:00 2001 From: Francisco Date: Tue, 5 Sep 2023 11:51:28 -0300 Subject: [PATCH 2/4] Update lazy-rice-joke.md --- .changeset/lazy-rice-joke.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/lazy-rice-joke.md b/.changeset/lazy-rice-joke.md index e1d34d455a0..6e1243002ee 100644 --- a/.changeset/lazy-rice-joke.md +++ b/.changeset/lazy-rice-joke.md @@ -2,4 +2,4 @@ 'openzeppelin-solidity': minor --- -`Initializable`: introduce intermediate variable to improve readability +`Initializable`: Use intermediate variables to improve readability. From 329dc22bafc36c701ada65d00555ae4772e69d94 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 5 Sep 2023 16:54:31 +0200 Subject: [PATCH 3/4] Update contracts/proxy/utils/Initializable.sol Co-authored-by: Francisco --- contracts/proxy/utils/Initializable.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/proxy/utils/Initializable.sol b/contracts/proxy/utils/Initializable.sol index 11f79d03acb..c8f11fe3672 100644 --- a/contracts/proxy/utils/Initializable.sol +++ b/contracts/proxy/utils/Initializable.sol @@ -108,11 +108,11 @@ abstract contract Initializable { bool isTopLevelCall = !$._initializing; uint64 initialized = $._initialized; - // Are allowed: - // - "initialSetup" calls: the contract is not in the initializing state and no previous version was - // initialized - // - "construction" calls: the contract is already initialized at step 1 (no reininitialization) and the - // current contract is not yet constructed. + // 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; From 175b1e9e09b19f90d96b472a0e0b4ebea9a47762 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 5 Sep 2023 17:27:26 +0200 Subject: [PATCH 4/4] Update contracts/proxy/utils/Initializable.sol Co-authored-by: Francisco --- contracts/proxy/utils/Initializable.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/proxy/utils/Initializable.sol b/contracts/proxy/utils/Initializable.sol index c8f11fe3672..7545bcca64a 100644 --- a/contracts/proxy/utils/Initializable.sol +++ b/contracts/proxy/utils/Initializable.sol @@ -116,7 +116,7 @@ abstract contract Initializable { bool initialSetup = initialized == 0 && isTopLevelCall; bool construction = initialized == 1 && address(this).code.length == 0; - if (!firstCall && !construction) { + if (!initialSetup && !construction) { revert AlreadyInitialized(); } $._initialized = 1;