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

Improve Initializable readability using intermediate variables #4576

Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/lazy-rice-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`Initializable`: Use intermediate variables to improve readability.
12 changes: 11 additions & 1 deletion contracts/proxy/utils/Initializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

// 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();
}
$._initialized = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$._initialized = 1;
$._initializing = isTopLevelCall;

May be cheaper here, removing redundant SLOAD operation in case of !isTopLevelCall

Copy link
Collaborator Author

@Amxx Amxx Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand your proposal. Before doing _;, we want to make sure the initializing is true, this would possibly invert it to false.

Also I'm not sure where the duplicated sload is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'm not sure where the duplicated sload is.

Since $ is a struct with two fields packed in one storage slot, compiler need to load this slot if you change only one field. If you write all the fields of the structure at once, the compiler does not need to read this slot before SSTORE

Copy link
Contributor

@0xVolosnikov 0xVolosnikov Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it could (in theory) save around 100 gas here

$._initialized = 1;
if (isTopLevelCall) {
$._initializing = true;
}

If do it like

$._initialized = 1;
$._initializing = isTopLevelCall;

Copy link
Contributor

@0xVolosnikov 0xVolosnikov Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would possibly invert it to false

Upd: yes, in this case my proposal should be rather:

  $._initialized = 1;
  $._initializing = true;

And here:

$._initialized = 1;
$._initializing = false;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing it that way requires proving that the _; part doesn't change the value of $._initialized.

I think it is true, because $._initializing = true prevents that value from changing in the rest of the contract, but it's more difficult to see.

I think I prefer to keep the current code at this stage (audit fixes) and come back to this for a future release. @vladyan18 can you open an issue?

Expand Down