ERC1967Upgrade.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.0-rc.0 (proxy/ERC1967/ERC1967Upgrade.sol)
  3. pragma solidity ^0.8.2;
  4. import "../beacon/IBeacon.sol";
  5. import "../../utils/Address.sol";
  6. import "../../utils/StorageSlot.sol";
  7. /**
  8. * @dev This abstract contract provides getters and event emitting update functions for
  9. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
  10. *
  11. * _Available since v4.1._
  12. *
  13. * @custom:oz-upgrades-unsafe-allow delegatecall
  14. */
  15. abstract contract ERC1967Upgrade {
  16. // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
  17. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  18. /**
  19. * @dev Storage slot with the address of the current implementation.
  20. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  21. * validated in the constructor.
  22. */
  23. bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  24. /**
  25. * @dev Emitted when the implementation is upgraded.
  26. */
  27. event Upgraded(address indexed implementation);
  28. /**
  29. * @dev Returns the current implementation address.
  30. */
  31. function _getImplementation() internal view returns (address) {
  32. return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  33. }
  34. /**
  35. * @dev Stores a new address in the EIP1967 implementation slot.
  36. */
  37. function _setImplementation(address newImplementation) private {
  38. require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  39. StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  40. }
  41. /**
  42. * @dev Perform implementation upgrade
  43. *
  44. * Emits an {Upgraded} event.
  45. */
  46. function _upgradeTo(address newImplementation) internal {
  47. _setImplementation(newImplementation);
  48. emit Upgraded(newImplementation);
  49. }
  50. /**
  51. * @dev Perform implementation upgrade with additional setup call.
  52. *
  53. * Emits an {Upgraded} event.
  54. */
  55. function _upgradeToAndCall(
  56. address newImplementation,
  57. bytes memory data,
  58. bool forceCall
  59. ) internal {
  60. _upgradeTo(newImplementation);
  61. if (data.length > 0 || forceCall) {
  62. Address.functionDelegateCall(newImplementation, data);
  63. }
  64. }
  65. /**
  66. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  67. *
  68. * Emits an {Upgraded} event.
  69. */
  70. function _upgradeToAndCallSecure(
  71. address newImplementation,
  72. bytes memory data,
  73. bool forceCall
  74. ) internal {
  75. address oldImplementation = _getImplementation();
  76. // Initial upgrade and setup call
  77. _setImplementation(newImplementation);
  78. if (data.length > 0 || forceCall) {
  79. Address.functionDelegateCall(newImplementation, data);
  80. }
  81. // Perform rollback test if not already in progress
  82. StorageSlot.BooleanSlot storage rollbackTesting = StorageSlot.getBooleanSlot(_ROLLBACK_SLOT);
  83. if (!rollbackTesting.value) {
  84. // Trigger rollback using upgradeTo from the new implementation
  85. rollbackTesting.value = true;
  86. Address.functionDelegateCall(
  87. newImplementation,
  88. abi.encodeWithSignature("upgradeTo(address)", oldImplementation)
  89. );
  90. rollbackTesting.value = false;
  91. // Check rollback was effective
  92. require(oldImplementation == _getImplementation(), "ERC1967Upgrade: upgrade breaks further upgrades");
  93. // Finally reset to the new implementation and log the upgrade
  94. _upgradeTo(newImplementation);
  95. }
  96. }
  97. /**
  98. * @dev Storage slot with the admin of the contract.
  99. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  100. * validated in the constructor.
  101. */
  102. bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  103. /**
  104. * @dev Emitted when the admin account has changed.
  105. */
  106. event AdminChanged(address previousAdmin, address newAdmin);
  107. /**
  108. * @dev Returns the current admin.
  109. */
  110. function _getAdmin() internal view returns (address) {
  111. return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
  112. }
  113. /**
  114. * @dev Stores a new address in the EIP1967 admin slot.
  115. */
  116. function _setAdmin(address newAdmin) private {
  117. require(newAdmin != address(0), "ERC1967: new admin is the zero address");
  118. StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
  119. }
  120. /**
  121. * @dev Changes the admin of the proxy.
  122. *
  123. * Emits an {AdminChanged} event.
  124. */
  125. function _changeAdmin(address newAdmin) internal {
  126. emit AdminChanged(_getAdmin(), newAdmin);
  127. _setAdmin(newAdmin);
  128. }
  129. /**
  130. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  131. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  132. */
  133. bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  134. /**
  135. * @dev Emitted when the beacon is upgraded.
  136. */
  137. event BeaconUpgraded(address indexed beacon);
  138. /**
  139. * @dev Returns the current beacon.
  140. */
  141. function _getBeacon() internal view returns (address) {
  142. return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
  143. }
  144. /**
  145. * @dev Stores a new beacon in the EIP1967 beacon slot.
  146. */
  147. function _setBeacon(address newBeacon) private {
  148. require(Address.isContract(newBeacon), "ERC1967: new beacon is not a contract");
  149. require(
  150. Address.isContract(IBeacon(newBeacon).implementation()),
  151. "ERC1967: beacon implementation is not a contract"
  152. );
  153. StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
  154. }
  155. /**
  156. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  157. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  158. *
  159. * Emits a {BeaconUpgraded} event.
  160. */
  161. function _upgradeBeaconToAndCall(
  162. address newBeacon,
  163. bytes memory data,
  164. bool forceCall
  165. ) internal {
  166. _setBeacon(newBeacon);
  167. emit BeaconUpgraded(newBeacon);
  168. if (data.length > 0 || forceCall) {
  169. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  170. }
  171. }
  172. }