Skip to content

Commit d16ec78

Browse files
committed
switch to namespaces
1 parent 0a670d0 commit d16ec78

File tree

118 files changed

+1017
-1271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+1017
-1271
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/access/AccessControlUpgradeable.sol

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,23 @@ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable,
5858
bytes32 adminRole;
5959
}
6060

61-
mapping(bytes32 role => RoleData) private _roles;
6261

6362
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
6463

64+
/// @custom:storage-location erc7201:openzeppelin.storage.AccessControl
65+
struct AccessControlStorage {
66+
mapping(bytes32 role => RoleData) _roles;
67+
}
68+
69+
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.AccessControl")) - 1))
70+
bytes32 private constant AccessControlStorageLocation = 0x02dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b6268ef;
71+
72+
function _getAccessControlStorage() private pure returns (AccessControlStorage storage $) {
73+
assembly {
74+
$.slot := AccessControlStorageLocation
75+
}
76+
}
77+
6578
/**
6679
* @dev Modifier that checks that an account has a specific role. Reverts
6780
* with an {AccessControlUnauthorizedAccount} error including the required role.
@@ -82,7 +95,8 @@ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable,
8295
* @dev Returns `true` if `account` has been granted `role`.
8396
*/
8497
function hasRole(bytes32 role, address account) public view virtual returns (bool) {
85-
return _roles[role].hasRole[account];
98+
AccessControlStorage storage $ = _getAccessControlStorage();
99+
return $._roles[role].hasRole[account];
86100
}
87101

88102
/**
@@ -110,7 +124,8 @@ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable,
110124
* To change a role's admin, use {_setRoleAdmin}.
111125
*/
112126
function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
113-
return _roles[role].adminRole;
127+
AccessControlStorage storage $ = _getAccessControlStorage();
128+
return $._roles[role].adminRole;
114129
}
115130

116131
/**
@@ -174,8 +189,9 @@ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable,
174189
* Emits a {RoleAdminChanged} event.
175190
*/
176191
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
192+
AccessControlStorage storage $ = _getAccessControlStorage();
177193
bytes32 previousAdminRole = getRoleAdmin(role);
178-
_roles[role].adminRole = adminRole;
194+
$._roles[role].adminRole = adminRole;
179195
emit RoleAdminChanged(role, previousAdminRole, adminRole);
180196
}
181197

@@ -187,8 +203,9 @@ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable,
187203
* May emit a {RoleGranted} event.
188204
*/
189205
function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
206+
AccessControlStorage storage $ = _getAccessControlStorage();
190207
if (!hasRole(role, account)) {
191-
_roles[role].hasRole[account] = true;
208+
$._roles[role].hasRole[account] = true;
192209
emit RoleGranted(role, account, _msgSender());
193210
return true;
194211
} else {
@@ -204,19 +221,13 @@ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable,
204221
* May emit a {RoleRevoked} event.
205222
*/
206223
function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
224+
AccessControlStorage storage $ = _getAccessControlStorage();
207225
if (hasRole(role, account)) {
208-
_roles[role].hasRole[account] = false;
226+
$._roles[role].hasRole[account] = false;
209227
emit RoleRevoked(role, account, _msgSender());
210228
return true;
211229
} else {
212230
return false;
213231
}
214232
}
215-
216-
/**
217-
* @dev This empty reserved space is put in place to allow future versions to add new
218-
* variables without shifting down storage in the inheritance chain.
219-
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
220-
*/
221-
uint256[49] private __gap;
222233
}

contracts/access/Ownable2StepUpgradeable.sol

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,37 @@ abstract contract Ownable2StepUpgradeable is Initializable, OwnableUpgradeable {
2323

2424
function __Ownable2Step_init_unchained() internal onlyInitializing {
2525
}
26-
address private _pendingOwner;
26+
/// @custom:storage-location erc7201:openzeppelin.storage.Ownable2Step
27+
struct Ownable2StepStorage {
28+
address _pendingOwner;
29+
}
30+
31+
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Ownable2Step")) - 1))
32+
bytes32 private constant Ownable2StepStorageLocation = 0x237e158222e3e6968b72b9db0d8043aacf074ad9f650f0d1606b4d82ee432c4d;
33+
34+
function _getOwnable2StepStorage() private pure returns (Ownable2StepStorage storage $) {
35+
assembly {
36+
$.slot := Ownable2StepStorageLocation
37+
}
38+
}
2739

2840
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
2941

3042
/**
3143
* @dev Returns the address of the pending owner.
3244
*/
3345
function pendingOwner() public view virtual returns (address) {
34-
return _pendingOwner;
46+
Ownable2StepStorage storage $ = _getOwnable2StepStorage();
47+
return $._pendingOwner;
3548
}
3649

3750
/**
3851
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
3952
* Can only be called by the current owner.
4053
*/
4154
function transferOwnership(address newOwner) public virtual override onlyOwner {
42-
_pendingOwner = newOwner;
55+
Ownable2StepStorage storage $ = _getOwnable2StepStorage();
56+
$._pendingOwner = newOwner;
4357
emit OwnershipTransferStarted(owner(), newOwner);
4458
}
4559

@@ -48,7 +62,8 @@ abstract contract Ownable2StepUpgradeable is Initializable, OwnableUpgradeable {
4862
* Internal function without access restriction.
4963
*/
5064
function _transferOwnership(address newOwner) internal virtual override {
51-
delete _pendingOwner;
65+
Ownable2StepStorage storage $ = _getOwnable2StepStorage();
66+
delete $._pendingOwner;
5267
super._transferOwnership(newOwner);
5368
}
5469

@@ -62,11 +77,4 @@ abstract contract Ownable2StepUpgradeable is Initializable, OwnableUpgradeable {
6277
}
6378
_transferOwnership(sender);
6479
}
65-
66-
/**
67-
* @dev This empty reserved space is put in place to allow future versions to add new
68-
* variables without shifting down storage in the inheritance chain.
69-
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
70-
*/
71-
uint256[49] private __gap;
7280
}

contracts/access/OwnableUpgradeable.sol

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,19 @@ import "../proxy/utils/Initializable.sol";
1919
* the owner.
2020
*/
2121
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
22-
address private _owner;
22+
/// @custom:storage-location erc7201:openzeppelin.storage.Ownable
23+
struct OwnableStorage {
24+
address _owner;
25+
}
26+
27+
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Ownable")) - 1))
28+
bytes32 private constant OwnableStorageLocation = 0x9016d09d72d40fdae2fd8ceac6b6234c7706214fd39c1cd1e609a0528c19938f;
29+
30+
function _getOwnableStorage() private pure returns (OwnableStorage storage $) {
31+
assembly {
32+
$.slot := OwnableStorageLocation
33+
}
34+
}
2335

2436
/**
2537
* @dev The caller account is not authorized to perform an operation.
@@ -56,7 +68,8 @@ abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
5668
* @dev Returns the address of the current owner.
5769
*/
5870
function owner() public view virtual returns (address) {
59-
return _owner;
71+
OwnableStorage storage $ = _getOwnableStorage();
72+
return $._owner;
6073
}
6174

6275
/**
@@ -95,15 +108,9 @@ abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
95108
* Internal function without access restriction.
96109
*/
97110
function _transferOwnership(address newOwner) internal virtual {
98-
address oldOwner = _owner;
99-
_owner = newOwner;
111+
OwnableStorage storage $ = _getOwnableStorage();
112+
address oldOwner = $._owner;
113+
$._owner = newOwner;
100114
emit OwnershipTransferred(oldOwner, newOwner);
101115
}
102-
103-
/**
104-
* @dev This empty reserved space is put in place to allow future versions to add new
105-
* variables without shifting down storage in the inheritance chain.
106-
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
107-
*/
108-
uint256[49] private __gap;
109116
}

0 commit comments

Comments
 (0)