Skip to content

Commit

Permalink
rename ERC1967Upgrade to ERC1967Utils and apply some recommandation f…
Browse files Browse the repository at this point in the history
…rom code review
  • Loading branch information
Amxx committed Jun 15, 2023
1 parent 59b854d commit 7ed9e54
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
14 changes: 7 additions & 7 deletions contracts/mocks/proxy/UUPSLegacy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import "./UUPSUpgradeableMock.sol";
// This contract implements the pre-4.5 UUPS upgrade function with a rollback test.
// It's used to test that newer UUPS contracts are considered valid upgrades by older UUPS contracts.
contract UUPSUpgradeableLegacyMock is UUPSUpgradeableMock {
// Inlined from ERC1967Upgrade
// Inlined from ERC1967Utils
bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;

// ERC1967Upgrade._setImplementation is private so we reproduce it here.
// ERC1967Utils._setImplementation is private so we reproduce it here.
// An extra underscore prevents a name clash error.
function __setImplementation(address newImplementation) private {
require(newImplementation.code.length > 0, "ERC1967: new implementation is not a contract");
StorageSlot.getAddressSlot(ERC1967Upgrade.IMPLEMENTATION_SLOT).value = newImplementation;
StorageSlot.getAddressSlot(ERC1967Utils.IMPLEMENTATION_SLOT).value = newImplementation;
}

function _upgradeToAndCallSecureLegacyV1(address newImplementation, bytes memory data, bool forceCall) internal {
address oldImplementation = ERC1967Upgrade.getImplementation();
address oldImplementation = ERC1967Utils.getImplementation();

// Initial upgrade and setup call
__setImplementation(newImplementation);
Expand All @@ -35,11 +35,11 @@ contract UUPSUpgradeableLegacyMock is UUPSUpgradeableMock {
rollbackTesting.value = false;
// Check rollback was effective
require(
oldImplementation == ERC1967Upgrade.getImplementation(),
"ERC1967Upgrade: upgrade breaks further upgrades"
oldImplementation == ERC1967Utils.getImplementation(),
"ERC1967Utils: upgrade breaks further upgrades"
);
// Finally reset to the new implementation and log the upgrade
ERC1967Upgrade.upgradeTo(newImplementation);
ERC1967Utils.upgradeTo(newImplementation);
}
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/mocks/proxy/UUPSUpgradeableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ contract UUPSUpgradeableMock is NonUpgradeableMock, UUPSUpgradeable {

contract UUPSUpgradeableUnsafeMock is UUPSUpgradeableMock {
function upgradeTo(address newImplementation) public override {
ERC1967Upgrade.upgradeToAndCall(newImplementation, bytes(""), false);
ERC1967Utils.upgradeToAndCall(newImplementation, bytes(""), false);
}

function upgradeToAndCall(address newImplementation, bytes memory data) public payable override {
ERC1967Upgrade.upgradeToAndCall(newImplementation, data, false);
ERC1967Utils.upgradeToAndCall(newImplementation, data, false);
}
}
6 changes: 3 additions & 3 deletions contracts/proxy/ERC1967/ERC1967Proxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pragma solidity ^0.8.19;

import "../Proxy.sol";
import "./ERC1967Upgrade.sol";
import "./ERC1967Utils.sol";

/**
* @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an
Expand All @@ -20,7 +20,7 @@ contract ERC1967Proxy is Proxy {
* function call, and allows initializing the storage of the proxy like a Solidity constructor.
*/
constructor(address _logic, bytes memory _data) payable {
ERC1967Upgrade.upgradeToAndCall(_logic, _data, false);
ERC1967Utils.upgradeToAndCall(_logic, _data, false);
}

/**
Expand All @@ -31,6 +31,6 @@ contract ERC1967Proxy is Proxy {
* `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
*/
function _implementation() internal view virtual override returns (address impl) {
return ERC1967Upgrade.getImplementation();
return ERC1967Utils.getImplementation();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Upgrade.sol)
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Utils.sol)

pragma solidity ^0.8.20;

Expand All @@ -15,8 +15,9 @@ import "../../utils/StorageSlot.sol";
*
* _Available since v4.1._
*/
library ERC1967Upgrade {
// =================== BEGIN --- COPIED FROM IERC1967.sol --- REMOVAL REQUIRES SOLIDITY 0.8.21 ====================
library ERC1967Utils {
// We re-declare ERC-1967 events here because they can't be used directly from IERC1967.
// This will be fixed in Solidity 0.8.21. At that point we should remove these events.
/**
* @dev Emitted when the implementation is upgraded.
*/
Expand All @@ -31,7 +32,6 @@ library ERC1967Upgrade {
* @dev Emitted when the beacon is changed.
*/
event BeaconUpgraded(address indexed beacon);
// ==================== END --- COPIED FROM IERC1967.sol --- REMOVAL REQUIRES SOLIDITY 0.8.21 =====================

// This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
Expand Down Expand Up @@ -84,17 +84,17 @@ library ERC1967Upgrade {
/**
* @dev Perform implementation upgrade
*
* Emits an {Upgraded} event.
* Emits an {IERC1967-Upgraded} event.
*/
function upgradeTo(address newImplementation) internal {
_setImplementation(newImplementation);
emit /*IERC1967.*/ Upgraded(newImplementation);
emit Upgraded(newImplementation);
}

/**
* @dev Perform implementation upgrade with additional setup call.
*
* Emits an {Upgraded} event.
* Emits an {IERC1967-Upgraded} event.
*/
function upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
upgradeTo(newImplementation);
Expand All @@ -106,7 +106,7 @@ library ERC1967Upgrade {
/**
* @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
*
* Emits an {Upgraded} event.
* Emits an {IERC1967-Upgraded} event.
*/
function upgradeToAndCallUUPS(address newImplementation, bytes memory data, bool forceCall) internal {
// Upgrades from old implementations will perform a rollback test. This test requires the new
Expand Down Expand Up @@ -159,16 +159,16 @@ library ERC1967Upgrade {
/**
* @dev Changes the admin of the proxy.
*
* Emits an {AdminChanged} event.
* Emits an {IERC1967-AdminChanged} event.
*/
function changeAdmin(address newAdmin) internal {
emit /*IERC1967.*/ AdminChanged(getAdmin(), newAdmin);
emit AdminChanged(getAdmin(), 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.
* This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
*/
// solhint-disable-next-line private-vars-leading-underscore
bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
Expand Down Expand Up @@ -200,11 +200,11 @@ library ERC1967Upgrade {
* @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
* not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
*
* Emits a {BeaconUpgraded} event.
* Emits an {IERC1967-BeaconUpgraded} event.
*/
function upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
_setBeacon(newBeacon);
emit /*IERC1967.*/ BeaconUpgraded(newBeacon);
emit BeaconUpgraded(newBeacon);
if (data.length > 0 || forceCall) {
Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/proxy/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Most of the proxies below are built on an abstract base contract.
In order to avoid clashes with the storage variables of the implementation contract behind a proxy, we use https://eips.ethereum.org/EIPS/eip-1967[EIP1967] storage slots.

- {ERC1967Upgrade}: Internal functions to get and set the storage slots defined in EIP1967.
- {ERC1967Utils}: Internal functions to get and set the storage slots defined in EIP1967.
- {ERC1967Proxy}: A proxy using EIP1967 storage slots. Not upgradeable by default.
There are two alternative ways to add upgradeability to an ERC1967 proxy. Their differences are explained below in <<transparent-vs-uups>>.
Expand Down Expand Up @@ -60,7 +60,7 @@ The current implementation of this security mechanism uses https://eips.ethereum

{{ERC1967Proxy}}

{{ERC1967Upgrade}}
{{ERC1967Utils}}

== Transparent Proxy

Expand Down
6 changes: 3 additions & 3 deletions contracts/proxy/beacon/BeaconProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.19;

import "./IBeacon.sol";
import "../Proxy.sol";
import "../ERC1967/ERC1967Upgrade.sol";
import "../ERC1967/ERC1967Utils.sol";

/**
* @dev This contract implements a proxy that gets the implementation address for each call from an {UpgradeableBeacon}.
Expand All @@ -28,13 +28,13 @@ contract BeaconProxy is Proxy {
* - `beacon` must be a contract with the interface {IBeacon}.
*/
constructor(address beacon, bytes memory data) payable {
ERC1967Upgrade.upgradeBeaconToAndCall(beacon, data, false);
ERC1967Utils.upgradeBeaconToAndCall(beacon, data, false);
}

/**
* @dev Returns the current implementation address of the associated beacon.
*/
function _implementation() internal view virtual override returns (address) {
return IBeacon(ERC1967Upgrade.getBeacon()).implementation();
return IBeacon(ERC1967Utils.getBeacon()).implementation();
}
}
10 changes: 5 additions & 5 deletions contracts/proxy/transparent/TransparentUpgradeableProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ contract TransparentUpgradeableProxy is ERC1967Proxy {
* optionally initialized with `_data` as explained in {ERC1967Proxy-constructor}.
*/
constructor(address _logic, address admin_, bytes memory _data) payable ERC1967Proxy(_logic, _data) {
ERC1967Upgrade.changeAdmin(admin_);
ERC1967Utils.changeAdmin(admin_);
}

/**
* @dev If caller is the admin process the call internally, otherwise transparently fallback to the proxy behavior
*/
function _fallback() internal virtual override {
if (msg.sender == ERC1967Upgrade.getAdmin()) {
if (msg.sender == ERC1967Utils.getAdmin()) {
bytes memory ret;
bytes4 selector = msg.sig;
if (selector == ITransparentUpgradeableProxy.upgradeTo.selector) {
Expand Down Expand Up @@ -103,7 +103,7 @@ contract TransparentUpgradeableProxy is ERC1967Proxy {
_requireZeroValue();

address newAdmin = abi.decode(msg.data[4:], (address));
ERC1967Upgrade.changeAdmin(newAdmin);
ERC1967Utils.changeAdmin(newAdmin);

return "";
}
Expand All @@ -115,7 +115,7 @@ contract TransparentUpgradeableProxy is ERC1967Proxy {
_requireZeroValue();

address newImplementation = abi.decode(msg.data[4:], (address));
ERC1967Upgrade.upgradeToAndCall(newImplementation, bytes(""), false);
ERC1967Utils.upgradeToAndCall(newImplementation, bytes(""), false);

return "";
}
Expand All @@ -127,7 +127,7 @@ contract TransparentUpgradeableProxy is ERC1967Proxy {
*/
function _dispatchUpgradeToAndCall() private returns (bytes memory) {
(address newImplementation, bytes memory data) = abi.decode(msg.data[4:], (address, bytes));
ERC1967Upgrade.upgradeToAndCall(newImplementation, data, true);
ERC1967Utils.upgradeToAndCall(newImplementation, data, true);

return "";
}
Expand Down
10 changes: 5 additions & 5 deletions contracts/proxy/utils/UUPSUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pragma solidity ^0.8.19;

import "../../interfaces/draft-IERC1822.sol";
import "../ERC1967/ERC1967Upgrade.sol";
import "../ERC1967/ERC1967Utils.sol";

/**
* @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
Expand Down Expand Up @@ -39,7 +39,7 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable {
// Must be called through delegatecall
revert UUPSUnauthorizedCallContext();
}
if (ERC1967Upgrade.getImplementation() != __self) {
if (ERC1967Utils.getImplementation() != __self) {
// Must be called through an active proxy
revert UUPSUnauthorizedCallContext();
}
Expand Down Expand Up @@ -67,7 +67,7 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable {
* function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
*/
function proxiableUUID() external view virtual notDelegated returns (bytes32) {
return ERC1967Upgrade.IMPLEMENTATION_SLOT;
return ERC1967Utils.IMPLEMENTATION_SLOT;
}

/**
Expand All @@ -81,7 +81,7 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable {
*/
function upgradeTo(address newImplementation) public virtual onlyProxy {
_authorizeUpgrade(newImplementation);
ERC1967Upgrade.upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
ERC1967Utils.upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
}

/**
Expand All @@ -96,7 +96,7 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable {
*/
function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
_authorizeUpgrade(newImplementation);
ERC1967Upgrade.upgradeToAndCallUUPS(newImplementation, data, true);
ERC1967Utils.upgradeToAndCallUUPS(newImplementation, data, true);
}

/**
Expand Down

0 comments on commit 7ed9e54

Please sign in to comment.