UUPSUpgradeable.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0-rc.1) (proxy/utils/UUPSUpgradeable.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC1822Proxiable} from "../../interfaces/draft-IERC1822.sol";
  5. import {ERC1967Utils} from "../ERC1967/ERC1967Utils.sol";
  6. /**
  7. * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
  8. * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
  9. *
  10. * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
  11. * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
  12. * `UUPSUpgradeable` with a custom implementation of upgrades.
  13. *
  14. * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
  15. *
  16. * @custom:stateless
  17. */
  18. abstract contract UUPSUpgradeable is IERC1822Proxiable {
  19. /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
  20. address private immutable __self = address(this);
  21. /**
  22. * @dev The version of the upgrade interface of the contract. If this getter is missing, both `upgradeTo(address)`
  23. * and `upgradeToAndCall(address,bytes)` are present, and `upgradeTo` must be used if no function should be called,
  24. * while `upgradeToAndCall` will invoke the `receive` function if the second argument is the empty byte string.
  25. * If the getter returns `"5.0.0"`, only `upgradeToAndCall(address,bytes)` is present, and the second argument must
  26. * be the empty byte string if no function should be called, making it impossible to invoke the `receive` function
  27. * during an upgrade.
  28. */
  29. string public constant UPGRADE_INTERFACE_VERSION = "5.0.0";
  30. /**
  31. * @dev The call is from an unauthorized context.
  32. */
  33. error UUPSUnauthorizedCallContext();
  34. /**
  35. * @dev The storage `slot` is unsupported as a UUID.
  36. */
  37. error UUPSUnsupportedProxiableUUID(bytes32 slot);
  38. /**
  39. * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
  40. * a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
  41. * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
  42. * function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
  43. * fail.
  44. */
  45. modifier onlyProxy() {
  46. _checkProxy();
  47. _;
  48. }
  49. /**
  50. * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
  51. * callable on the implementing contract but not through proxies.
  52. */
  53. modifier notDelegated() {
  54. _checkNotDelegated();
  55. _;
  56. }
  57. /**
  58. * @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
  59. * implementation. It is used to validate the implementation's compatibility when performing an upgrade.
  60. *
  61. * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
  62. * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
  63. * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
  64. */
  65. function proxiableUUID() external view virtual notDelegated returns (bytes32) {
  66. return ERC1967Utils.IMPLEMENTATION_SLOT;
  67. }
  68. /**
  69. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  70. * encoded in `data`.
  71. *
  72. * Calls {_authorizeUpgrade}.
  73. *
  74. * Emits an {Upgraded} event.
  75. *
  76. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  77. */
  78. function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
  79. _authorizeUpgrade(newImplementation);
  80. _upgradeToAndCallUUPS(newImplementation, data);
  81. }
  82. /**
  83. * @dev Reverts if the execution is not performed via delegatecall or the execution
  84. * context is not of a proxy with an ERC1967-compliant implementation pointing to self.
  85. * See {_onlyProxy}.
  86. */
  87. function _checkProxy() internal view virtual {
  88. if (
  89. address(this) == __self || // Must be called through delegatecall
  90. ERC1967Utils.getImplementation() != __self // Must be called through an active proxy
  91. ) {
  92. revert UUPSUnauthorizedCallContext();
  93. }
  94. }
  95. /**
  96. * @dev Reverts if the execution is performed via delegatecall.
  97. * See {notDelegated}.
  98. */
  99. function _checkNotDelegated() internal view virtual {
  100. if (address(this) != __self) {
  101. // Must not be called through delegatecall
  102. revert UUPSUnauthorizedCallContext();
  103. }
  104. }
  105. /**
  106. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  107. * {upgradeToAndCall}.
  108. *
  109. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  110. *
  111. * ```solidity
  112. * function _authorizeUpgrade(address) internal onlyOwner {}
  113. * ```
  114. */
  115. function _authorizeUpgrade(address newImplementation) internal virtual;
  116. /**
  117. * @dev Performs an implementation upgrade with a security check for UUPS proxies, and additional setup call.
  118. *
  119. * As a security check, {proxiableUUID} is invoked in the new implementation, and the return value
  120. * is expected to be the implementation slot in ERC1967.
  121. *
  122. * Emits an {IERC1967-Upgraded} event.
  123. */
  124. function _upgradeToAndCallUUPS(address newImplementation, bytes memory data) private {
  125. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  126. if (slot != ERC1967Utils.IMPLEMENTATION_SLOT) {
  127. revert UUPSUnsupportedProxiableUUID(slot);
  128. }
  129. ERC1967Utils.upgradeToAndCall(newImplementation, data);
  130. } catch {
  131. // The implementation is not UUPS
  132. revert ERC1967Utils.ERC1967InvalidImplementation(newImplementation);
  133. }
  134. }
  135. }