Skip to content

Commit b6111fa

Browse files
authored
Use namespaced storage for upgradeable contracts (#4534)
1 parent 095c8e1 commit b6111fa

File tree

8 files changed

+75
-21
lines changed

8 files changed

+75
-21
lines changed

.changeset/wet-bears-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': major
3+
---
4+
5+
Upgradeable contracts now use namespaced storage (EIP-7201).

.github/actions/setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ runs:
55
steps:
66
- uses: actions/setup-node@v3
77
with:
8-
node-version: 14.x
8+
node-version: 16.x
99
- uses: actions/cache@v3
1010
id: cache
1111
with:

contracts/governance/Governor.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
299299
bytes[] memory calldatas,
300300
string memory description,
301301
address proposer
302-
) internal virtual returns (uint256) {
303-
uint256 proposalId = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
302+
) internal virtual returns (uint256 proposalId) {
303+
proposalId = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
304304

305305
if (targets.length != values.length || targets.length != calldatas.length || targets.length == 0) {
306306
revert GovernorInvalidProposalLength(targets.length, calldatas.length, values.length);
@@ -329,7 +329,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
329329
description
330330
);
331331

332-
return proposalId;
332+
// Using a named return variable to avoid stack too deep errors
333333
}
334334

335335
/**

contracts/governance/extensions/GovernorVotes.sol

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ import {SafeCast} from "../../utils/math/SafeCast.sol";
1212
* @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token, or since v4.5 an {ERC721Votes} token.
1313
*/
1414
abstract contract GovernorVotes is Governor {
15-
IERC5805 public immutable token;
15+
IERC5805 private immutable _token;
1616

1717
constructor(IVotes tokenAddress) {
18-
token = IERC5805(address(tokenAddress));
18+
_token = IERC5805(address(tokenAddress));
19+
}
20+
21+
/**
22+
* @dev The token that voting power is sourced from.
23+
*/
24+
function token() public view virtual returns (IERC5805) {
25+
return _token;
1926
}
2027

2128
/**
2229
* @dev Clock (as specified in EIP-6372) is set to match the token's clock. Fallback to block numbers if the token
2330
* does not implement EIP-6372.
2431
*/
2532
function clock() public view virtual override returns (uint48) {
26-
try token.clock() returns (uint48 timepoint) {
33+
try token().clock() returns (uint48 timepoint) {
2734
return timepoint;
2835
} catch {
2936
return SafeCast.toUint48(block.number);
@@ -35,7 +42,7 @@ abstract contract GovernorVotes is Governor {
3542
*/
3643
// solhint-disable-next-line func-name-mixedcase
3744
function CLOCK_MODE() public view virtual override returns (string memory) {
38-
try token.CLOCK_MODE() returns (string memory clockmode) {
45+
try token().CLOCK_MODE() returns (string memory clockmode) {
3946
return clockmode;
4047
} catch {
4148
return "mode=blocknumber&from=default";
@@ -50,6 +57,6 @@ abstract contract GovernorVotes is Governor {
5057
uint256 timepoint,
5158
bytes memory /*params*/
5259
) internal view virtual override returns (uint256) {
53-
return token.getPastVotes(account, timepoint);
60+
return token().getPastVotes(account, timepoint);
5461
}
5562
}

contracts/governance/extensions/GovernorVotesQuorumFraction.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ abstract contract GovernorVotesQuorumFraction is GovernorVotes {
7171
* @dev Returns the quorum for a timepoint, in terms of number of votes: `supply * numerator / denominator`.
7272
*/
7373
function quorum(uint256 timepoint) public view virtual override returns (uint256) {
74-
return (token.getPastTotalSupply(timepoint) * quorumNumerator(timepoint)) / quorumDenominator();
74+
return (token().getPastTotalSupply(timepoint) * quorumNumerator(timepoint)) / quorumDenominator();
7575
}
7676

7777
/**

package-lock.json

Lines changed: 47 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"semver": "^7.3.5",
8686
"solhint": "^3.3.6",
8787
"solhint-plugin-openzeppelin": "file:scripts/solhint-custom",
88-
"solidity-ast": "^0.4.25",
88+
"solidity-ast": "^0.4.50",
8989
"solidity-coverage": "^0.8.0",
9090
"solidity-docgen": "^0.6.0-beta.29",
9191
"undici": "^5.22.1",

scripts/upgradeable/transpile.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fi
2222
# -i: use included Initializable
2323
# -x: exclude proxy-related contracts with a few exceptions
2424
# -p: emit public initializer
25+
# -n: use namespaces
26+
# -N: exclude from namespaces transformation
2527
npx @openzeppelin/upgrade-safe-transpiler@latest -D \
2628
-b "$build_info" \
2729
-i contracts/proxy/utils/Initializable.sol \
@@ -32,7 +34,9 @@ npx @openzeppelin/upgrade-safe-transpiler@latest -D \
3234
-x '!contracts/proxy/ERC1967/ERC1967Utils.sol' \
3335
-x '!contracts/proxy/utils/UUPSUpgradeable.sol' \
3436
-x '!contracts/proxy/beacon/IBeacon.sol' \
35-
-p 'contracts/**/presets/**/*'
37+
-p 'contracts/**/presets/**/*' \
38+
-n \
39+
-N 'contracts/mocks/**/*'
3640

3741
# delete compilation artifacts of vanilla code
3842
npm run clean

0 commit comments

Comments
 (0)