UUPSLegacyUpgradeable.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./UUPSUpgradeableMockUpgradeable.sol";
  4. import "../../proxy/utils/Initializable.sol";
  5. // This contract implements the pre-4.5 UUPS upgrade function with a rollback test.
  6. // It's used to test that newer UUPS contracts are considered valid upgrades by older UUPS contracts.
  7. contract UUPSUpgradeableLegacyMockUpgradeable is Initializable, UUPSUpgradeableMockUpgradeable {
  8. function __UUPSUpgradeableLegacyMock_init() internal onlyInitializing {
  9. }
  10. function __UUPSUpgradeableLegacyMock_init_unchained() internal onlyInitializing {
  11. }
  12. // Inlined from ERC1967Upgrade
  13. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  14. // ERC1967Upgrade._setImplementation is private so we reproduce it here.
  15. // An extra underscore prevents a name clash error.
  16. function __setImplementation(address newImplementation) private {
  17. require(AddressUpgradeable.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  18. StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  19. }
  20. function _upgradeToAndCallSecureLegacyV1(
  21. address newImplementation,
  22. bytes memory data,
  23. bool forceCall
  24. ) internal {
  25. address oldImplementation = _getImplementation();
  26. // Initial upgrade and setup call
  27. __setImplementation(newImplementation);
  28. if (data.length > 0 || forceCall) {
  29. __functionDelegateCall(newImplementation, data);
  30. }
  31. // Perform rollback test if not already in progress
  32. StorageSlotUpgradeable.BooleanSlot storage rollbackTesting = StorageSlotUpgradeable.getBooleanSlot(_ROLLBACK_SLOT);
  33. if (!rollbackTesting.value) {
  34. // Trigger rollback using upgradeTo from the new implementation
  35. rollbackTesting.value = true;
  36. __functionDelegateCall(
  37. newImplementation,
  38. abi.encodeWithSignature("upgradeTo(address)", oldImplementation)
  39. );
  40. rollbackTesting.value = false;
  41. // Check rollback was effective
  42. require(oldImplementation == _getImplementation(), "ERC1967Upgrade: upgrade breaks further upgrades");
  43. // Finally reset to the new implementation and log the upgrade
  44. _upgradeTo(newImplementation);
  45. }
  46. }
  47. // hooking into the old mechanism
  48. function upgradeTo(address newImplementation) external virtual override {
  49. _upgradeToAndCallSecureLegacyV1(newImplementation, bytes(""), false);
  50. }
  51. function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual override {
  52. _upgradeToAndCallSecureLegacyV1(newImplementation, data, false);
  53. }
  54. // ERC1967Upgrade._functionDelegateCall is private so we reproduce it here.
  55. // An extra underscore prevents a name clash error.
  56. function __functionDelegateCall(address target, bytes memory data) private returns (bytes memory) {
  57. require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");
  58. // solhint-disable-next-line avoid-low-level-calls
  59. (bool success, bytes memory returndata) = target.delegatecall(data);
  60. return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
  61. }
  62. /**
  63. * This empty reserved space is put in place to allow future versions to add new
  64. * variables without shifting down storage in the inheritance chain.
  65. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  66. */
  67. uint256[50] private __gap;
  68. }