UUPSLegacyUpgradeable.sol 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. __CountersImpl_init_unchained();
  10. __ERC1967Upgrade_init_unchained();
  11. __UUPSUpgradeable_init_unchained();
  12. __UUPSUpgradeableMock_init_unchained();
  13. __UUPSUpgradeableLegacyMock_init_unchained();
  14. }
  15. function __UUPSUpgradeableLegacyMock_init_unchained() internal onlyInitializing {
  16. }
  17. // Inlined from ERC1967Upgrade
  18. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  19. // ERC1967Upgrade._setImplementation is private so we reproduce it here.
  20. // An extra underscore prevents a name clash error.
  21. function __setImplementation(address newImplementation) private {
  22. require(AddressUpgradeable.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  23. StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  24. }
  25. function _upgradeToAndCallSecureLegacyV1(
  26. address newImplementation,
  27. bytes memory data,
  28. bool forceCall
  29. ) internal {
  30. address oldImplementation = _getImplementation();
  31. // Initial upgrade and setup call
  32. __setImplementation(newImplementation);
  33. if (data.length > 0 || forceCall) {
  34. __functionDelegateCall(newImplementation, data);
  35. }
  36. // Perform rollback test if not already in progress
  37. StorageSlotUpgradeable.BooleanSlot storage rollbackTesting = StorageSlotUpgradeable.getBooleanSlot(_ROLLBACK_SLOT);
  38. if (!rollbackTesting.value) {
  39. // Trigger rollback using upgradeTo from the new implementation
  40. rollbackTesting.value = true;
  41. __functionDelegateCall(
  42. newImplementation,
  43. abi.encodeWithSignature("upgradeTo(address)", oldImplementation)
  44. );
  45. rollbackTesting.value = false;
  46. // Check rollback was effective
  47. require(oldImplementation == _getImplementation(), "ERC1967Upgrade: upgrade breaks further upgrades");
  48. // Finally reset to the new implementation and log the upgrade
  49. _upgradeTo(newImplementation);
  50. }
  51. }
  52. // hooking into the old mechanism
  53. function upgradeTo(address newImplementation) external virtual override {
  54. _upgradeToAndCallSecureLegacyV1(newImplementation, bytes(""), false);
  55. }
  56. function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual override {
  57. _upgradeToAndCallSecureLegacyV1(newImplementation, data, false);
  58. }
  59. // ERC1967Upgrade._functionDelegateCall is private so we reproduce it here.
  60. // An extra underscore prevents a name clash error.
  61. function __functionDelegateCall(address target, bytes memory data) private returns (bytes memory) {
  62. require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");
  63. // solhint-disable-next-line avoid-low-level-calls
  64. (bool success, bytes memory returndata) = target.delegatecall(data);
  65. return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
  66. }
  67. uint256[50] private __gap;
  68. }