Commit 6552adaf by Hadrien Croubois Committed by Francisco Giordano

Merge ERC1967Storage into ERC1967Upgrade (#2644)

(cherry picked from commit 5756e319)
parent a664fb1e
...@@ -27,6 +27,6 @@ contract ERC1967Proxy is Proxy, ERC1967Upgrade { ...@@ -27,6 +27,6 @@ contract ERC1967Proxy is Proxy, ERC1967Upgrade {
* @dev Returns the current implementation address. * @dev Returns the current implementation address.
*/ */
function _implementation() internal view virtual override returns (address impl) { function _implementation() internal view virtual override returns (address impl) {
return ERC1967Storage._getImplementation(); return ERC1967Upgrade._getImplementation();
} }
} }
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../beacon/IBeacon.sol";
import "../../utils/Address.sol";
import "../../utils/StorageSlot.sol";
/**
* @dev This abstract contract provides setters and getters for the different
* https://eips.ethereum.org/EIPS/eip-1967[EIP1967] storage slots.
*
* _Available since v4.1._
*/
abstract contract ERC1967Storage {
/**
* @dev Storage slot with the address of the current implementation.
* This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
* validated in the constructor.
*/
bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
/**
* @dev Returns the current implementation address.
*/
function _getImplementation() internal view returns (address) {
return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
}
/**
* @dev Stores a new address in the EIP1967 implementation slot.
*/
function _setImplementation(address newImplementation) internal {
require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
}
/**
* @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
* This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
*/
bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
/**
* @dev Returns the current beacon.
*/
function _getBeacon() internal view returns (address) {
return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
}
/**
* @dev Stores a new beacon in the EIP1967 beacon slot.
*/
function _setBeacon(address newBeacon) internal {
require(
Address.isContract(newBeacon),
"ERC1967: new beacon is not a contract"
);
require(
Address.isContract(IBeacon(newBeacon).implementation()),
"ERC1967: beacon implementation is not a contract"
);
StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
}
/**
* @dev Storage slot with the admin of the contract.
* This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
* validated in the constructor.
*/
bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
/**
* @dev Returns the current admin.
*/
function _getAdmin() internal view returns (address) {
return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
}
/**
* @dev Stores a new address in the EIP1967 admin slot.
*/
function _setAdmin(address newAdmin) internal {
require(newAdmin != address(0), "ERC1967: new admin is the zero address");
StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
}
}
...@@ -2,34 +2,48 @@ ...@@ -2,34 +2,48 @@
pragma solidity ^0.8.2; pragma solidity ^0.8.2;
import "./ERC1967Storage.sol"; import "../beacon/IBeacon.sol";
import "../../utils/Address.sol";
import "../../utils/StorageSlot.sol";
/** /**
* @dev This abstract contract provides event emitting update functions for * @dev This abstract contract provides getters and event emitting update functions for
* https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
* *
* _Available since v4.1._ * _Available since v4.1._
* *
* @custom:oz-upgrades-unsafe-allow delegatecall * @custom:oz-upgrades-unsafe-allow delegatecall
*/ */
abstract contract ERC1967Upgrade is ERC1967Storage { abstract contract ERC1967Upgrade {
// This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1 // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143; bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
/** /**
* @dev Storage slot with the address of the current implementation.
* This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
* validated in the constructor.
*/
bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
/**
* @dev Emitted when the implementation is upgraded. * @dev Emitted when the implementation is upgraded.
*/ */
event Upgraded(address indexed implementation); event Upgraded(address indexed implementation);
/** /**
* @dev Emitted when the beacon is upgraded. * @dev Returns the current implementation address.
*/ */
event BeaconUpgraded(address indexed beacon); function _getImplementation() internal view returns (address) {
return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
}
/** /**
* @dev Emitted when the admin account has changed. * @dev Stores a new address in the EIP1967 implementation slot.
*/ */
event AdminChanged(address previousAdmin, address newAdmin); function _setImplementation(address newImplementation) private {
require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
}
/** /**
* @dev Perform implementation upgrade * @dev Perform implementation upgrade
...@@ -104,6 +118,33 @@ abstract contract ERC1967Upgrade is ERC1967Storage { ...@@ -104,6 +118,33 @@ abstract contract ERC1967Upgrade is ERC1967Storage {
} }
/** /**
* @dev Storage slot with the admin of the contract.
* This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
* validated in the constructor.
*/
bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
/**
* @dev Emitted when the admin account has changed.
*/
event AdminChanged(address previousAdmin, address newAdmin);
/**
* @dev Returns the current admin.
*/
function _getAdmin() internal view returns (address) {
return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
}
/**
* @dev Stores a new address in the EIP1967 admin slot.
*/
function _setAdmin(address newAdmin) private {
require(newAdmin != address(0), "ERC1967: new admin is the zero address");
StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
}
/**
* @dev Changes the admin of the proxy. * @dev Changes the admin of the proxy.
* *
* Emits an {AdminChanged} event. * Emits an {AdminChanged} event.
...@@ -112,4 +153,37 @@ abstract contract ERC1967Upgrade is ERC1967Storage { ...@@ -112,4 +153,37 @@ abstract contract ERC1967Upgrade is ERC1967Storage {
emit AdminChanged(_getAdmin(), newAdmin); emit AdminChanged(_getAdmin(), newAdmin);
_setAdmin(newAdmin); _setAdmin(newAdmin);
} }
/**
* @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
* This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
*/
bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
/**
* @dev Emitted when the beacon is upgraded.
*/
event BeaconUpgraded(address indexed beacon);
/**
* @dev Returns the current beacon.
*/
function _getBeacon() internal view returns (address) {
return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
}
/**
* @dev Stores a new beacon in the EIP1967 beacon slot.
*/
function _setBeacon(address newBeacon) private {
require(
Address.isContract(newBeacon),
"ERC1967: new beacon is not a contract"
);
require(
Address.isContract(IBeacon(newBeacon).implementation()),
"ERC1967: beacon implementation is not a contract"
);
StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
}
} }
...@@ -40,8 +40,6 @@ When doing an upgrade, the second parameter of the {upgradeToAndCall} function a ...@@ -40,8 +40,6 @@ When doing an upgrade, the second parameter of the {upgradeToAndCall} function a
{{ERC1967Proxy}} {{ERC1967Proxy}}
{{ERC1967Storage}}
{{ERC1967Upgrade}} {{ERC1967Upgrade}}
== UUPS == UUPS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment