ERC1967Upgrade.sol 6.5 KB

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