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

Replace immutable with constant for _PERMIT_TYPEHASH #3196

Merged
merged 6 commits into from Mar 9, 2022
Merged

Replace immutable with constant for _PERMIT_TYPEHASH #3196

merged 6 commits into from Mar 9, 2022

Conversation

pcaversaccio
Copy link
Contributor

@pcaversaccio pcaversaccio commented Feb 17, 2022

This commit is related to the following issue discussion: OpenZeppelin/contracts-wizard#89 (comment)

Since Solidity version 0.6.12 the keccak256 of string literals is treated specially and the hash is evaluated at compile time. Since the OpenZeppelin Wizard also uses constant for OpenZeppelin's AccessControl's roles declarations, it's good practice to make this consistent.

PR Checklist

  • Tests
  • Documentation
  • Changelog entry

This commit is related to the following issue discussion: OpenZeppelin/contracts-wizard#89 (comment)

Since Solidity version `0.6.12` the `keccak256` of string literals is treated specially and the hash is evaluated at compile time. Since the OpenZeppelin Wizard also uses `constant` for OpenZeppelin's AccessControl's roles declarations, it's good practice to make this consistent.
@Amxx
Copy link
Collaborator

Amxx commented Feb 17, 2022

Hello @pcaversaccio

This indeed looks like a significant improvement. It, unfortunately, causes issues with the way our contract are transpiled to the upgradeable version.

The immutable version is currently transpiled to "normal" storage:
https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/master/contracts/token/ERC20/extensions/draft-ERC20PermitUpgradeable.sol#L29

which must be initialized:
https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/master/contracts/token/ERC20/extensions/draft-ERC20PermitUpgradeable.sol#L42

This is not great.

If we apply the changes you propose, we would end up with an upgradeable version that contains one less storage slot. We may be able to fix that by increasing the gap, but the upgrade plugin would consider that an error.

The bottom line is yes, we will fix that, but we need to improve the upgrade plugins in order for that fix to be transparent to users of upgradeable contracts.

@Amxx Amxx requested review from frangio and Amxx February 17, 2022 11:14
@frangio
Copy link
Contributor

frangio commented Feb 17, 2022

@Amxx That should be possible to do with the same changes we are working on for #2901 regarding renaming/retyping the _initialized variable. But yes, in the meantime this is blocked by that.

@pcaversaccio
Copy link
Contributor Author

pcaversaccio commented Feb 19, 2022

@Amxx In light of this discussion, it could be useful to add an additional test to the GitHub actions that check the transpiled contracts for possible warnings/errors for every new PR.

@frangio
Copy link
Contributor

frangio commented Feb 25, 2022

@pcaversaccio Yes, 100%.

@frangio frangio added the on hold Put on hold for some reason that must be specified in a comment. label Feb 25, 2022
@frangio frangio mentioned this pull request Feb 25, 2022
1 task
@frangio
Copy link
Contributor

frangio commented Mar 8, 2022

Blocked on OpenZeppelin/openzeppelin-transpiler#86.

@frangio frangio removed their request for review March 8, 2022 20:43
@frangio frangio removed the on hold Put on hold for some reason that must be specified in a comment. label Mar 8, 2022
@frangio
Copy link
Contributor

frangio commented Mar 8, 2022

This is unblocked now: a new version of the transpiler has been released with a @custom:storage-size tag that will allow us to change this to a constant variable.

@frangio frangio changed the title replace immutable with constant for _PERMIT_TYPEHASH Replace immutable with constant for _PERMIT_TYPEHASH Mar 8, 2022
@Amxx
Copy link
Collaborator

Amxx commented Mar 9, 2022

AFAIK, we have 2 options:

  1. Add @custom:storage-size 51.
    This fill move the immutable (transpilled to private) variable inside the gap, which the upgrade plugins will see as an error.

  2. Do

bytes32 private constant _PERMIT_TYPEHASH = "...."
/// @custom:oz-renamed-from _PERMIT_TYPEHASH
bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;

Will will continue to reserve a slot, but not use it anymore.

@pcaversaccio
Copy link
Contributor Author

@Amxx before we make any further adjustments wouldn't be a good idea to add a further CI transpile test? Concerning your suggestion above I personally would prefer the second option since it implements certain transparency in the code. I would go even further and add a further natspec:

bytes32 private constant _PERMIT_TYPEHASH = "...."
/**
 * @dev In previous versions `_PERMIT_TYPEHASH` was declared as `immutable`. 
 * However, to ensure consistency with the upgradable transpiler, we will continue
 * to reserve a slot.
 * @custom:oz-renamed-from _PERMIT_TYPEHASH
 */
bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;

@Amxx
Copy link
Collaborator

Amxx commented Mar 9, 2022

Having an automated CI test to check storage compatibility is on the roadmap. Since the transpiler just received significant change, we will check everything manually, and use what we learn in the process to design the CI check

@Amxx
Copy link
Collaborator

Amxx commented Mar 9, 2022

bytes32 private constant _PERMIT_TYPEHASH = "...."
/**
 * @dev In previous versions `_PERMIT_TYPEHASH` was declared as `immutable`. 
 * However, to ensure consistency with the upgradable transpiler, we will continue
 * to reserve a slot.
 * @custom:oz-renamed-from _PERMIT_TYPEHASH
 */
bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;

Lets get that merged.

@frangio
Copy link
Contributor

frangio commented Mar 9, 2022

It makes sense to reserve the previously used spot so 👍 to @custom:oz-renamed-from. We still need storage-size 51 though, right? Because the gap wasn't beeing calculated correctly.

pcaversaccio and others added 2 commits March 9, 2022 14:47
Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
@Amxx
Copy link
Collaborator

Amxx commented Mar 9, 2022

That is being addresses in #3252

unless you want to do it here directly

@pcaversaccio
Copy link
Contributor Author

Probably it would make sense to put everything related to draft-ERC20Permit.sol into this PR. You let me know what you prefer and I will update accordingly.

@Amxx
Copy link
Collaborator

Amxx commented Mar 9, 2022

Probably it would make sense to put everything related to draft-ERC20Permit.sol into this PR. You let me know what you prefer and I will update accordingly.

the contract size will be overridden in the patches of the upgradeable repo. No need to worry about it here

Copy link
Collaborator

@Amxx Amxx left a comment

Choose a reason for hiding this comment

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

LGTM. @frangio any final words?

Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

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

Looks good thank you!

@Amxx Amxx merged commit cc1c180 into OpenZeppelin:master Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants