TransparentUpgradeableProxy.sol 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (proxy/transparent/TransparentUpgradeableProxy.sol)
  3. pragma solidity ^0.8.19;
  4. import {ERC1967Utils} from "../ERC1967/ERC1967Utils.sol";
  5. import {ERC1967Proxy} from "../ERC1967/ERC1967Proxy.sol";
  6. import {IERC1967} from "../../interfaces/IERC1967.sol";
  7. /**
  8. * @dev Interface for {TransparentUpgradeableProxy}. In order to implement transparency, {TransparentUpgradeableProxy}
  9. * does not implement this interface directly, and some of its functions are implemented by an internal dispatch
  10. * mechanism. The compiler is unaware that these functions are implemented by {TransparentUpgradeableProxy} and will not
  11. * include them in the ABI so this interface must be used to interact with it.
  12. */
  13. interface ITransparentUpgradeableProxy is IERC1967 {
  14. function upgradeTo(address) external;
  15. function upgradeToAndCall(address, bytes memory) external payable;
  16. }
  17. /**
  18. * @dev This contract implements a proxy that is upgradeable by an immutable admin.
  19. *
  20. * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
  21. * clashing], which can potentially be used in an attack, this contract uses the
  22. * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
  23. * things that go hand in hand:
  24. *
  25. * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
  26. * that call matches one of the admin functions exposed by the proxy itself.
  27. * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
  28. * implementation. If the admin tries to call a function on the implementation it will fail with an error indicating the
  29. * proxy admin cannot fallback to the target implementation.
  30. *
  31. * These properties mean that the admin account can only be used for upgrading the proxy, so it's best if it's a dedicated
  32. * account that is not used for anything else. This will avoid headaches due to sudden errors when trying to call a function
  33. * from the proxy implementation.
  34. *
  35. * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
  36. * you should think of the `ProxyAdmin` instance as the real administrative interface of your proxy, which extends from the
  37. * {Ownable} contract to allow for changing the proxy's admin owner.
  38. *
  39. * NOTE: The real interface of this proxy is that defined in `ITransparentUpgradeableProxy`. This contract does not
  40. * inherit from that interface, and instead the admin functions are implicitly implemented using a custom dispatch
  41. * mechanism in `_fallback`. Consequently, the compiler will not produce an ABI for this contract. This is necessary to
  42. * fully implement transparency without decoding reverts caused by selector clashes between the proxy and the
  43. * implementation.
  44. *
  45. * IMPORTANT: This contract avoids unnecessary storage reads by setting the admin only during construction as an immutable variable,
  46. * preventing any changes thereafter. However, the admin slot defined in ERC-1967 can still be overwritten by the implementation
  47. * logic pointed to by this proxy. In such cases, the contract may end up in an undesirable state where the admin slot is different
  48. * from the actual admin.
  49. *
  50. * WARNING: It is not recommended to extend this contract to add additional external functions. If you do so, the compiler
  51. * will not check that there are no selector conflicts, due to the note above. A selector clash between any new function
  52. * and the functions declared in {ITransparentUpgradeableProxy} will be resolved in favor of the new one. This could
  53. * render the admin operations inaccessible, which could prevent upgradeability. Transparency may also be compromised.
  54. */
  55. contract TransparentUpgradeableProxy is ERC1967Proxy {
  56. // An immutable address for the admin avoid unnecessary SLOADs before each call
  57. // at the expense of removing the ability to change the admin once it's set.
  58. // This is acceptable if the admin is always a ProxyAdmin instance or similar contract
  59. // with its own ability to transfer the permissions to another account.
  60. address private immutable _admin;
  61. /**
  62. * @dev The proxy caller is the current admin, and can't fallback to the proxy target.
  63. */
  64. error ProxyDeniedAdminAccess();
  65. /**
  66. * @dev msg.value is not 0.
  67. */
  68. error ProxyNonPayableFunction();
  69. /**
  70. * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
  71. * optionally initialized with `_data` as explained in {ERC1967Proxy-constructor}.
  72. */
  73. constructor(address _logic, address admin_, bytes memory _data) payable ERC1967Proxy(_logic, _data) {
  74. _admin = admin_;
  75. // Set the storage value and emit an event for ERC-1967 compatibility
  76. ERC1967Utils.changeAdmin(admin_);
  77. }
  78. /**
  79. * @dev If caller is the admin process the call internally, otherwise transparently fallback to the proxy behavior
  80. */
  81. function _fallback() internal virtual override {
  82. if (msg.sender == _admin) {
  83. bytes memory ret;
  84. bytes4 selector = msg.sig;
  85. if (selector == ITransparentUpgradeableProxy.upgradeTo.selector) {
  86. ret = _dispatchUpgradeTo();
  87. } else if (selector == ITransparentUpgradeableProxy.upgradeToAndCall.selector) {
  88. ret = _dispatchUpgradeToAndCall();
  89. } else {
  90. revert ProxyDeniedAdminAccess();
  91. }
  92. assembly {
  93. return(add(ret, 0x20), mload(ret))
  94. }
  95. } else {
  96. super._fallback();
  97. }
  98. }
  99. /**
  100. * @dev Upgrade the implementation of the proxy.
  101. */
  102. function _dispatchUpgradeTo() private returns (bytes memory) {
  103. _requireZeroValue();
  104. address newImplementation = abi.decode(msg.data[4:], (address));
  105. ERC1967Utils.upgradeToAndCall(newImplementation, bytes(""), false);
  106. return "";
  107. }
  108. /**
  109. * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
  110. * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
  111. * proxied contract.
  112. */
  113. function _dispatchUpgradeToAndCall() private returns (bytes memory) {
  114. (address newImplementation, bytes memory data) = abi.decode(msg.data[4:], (address, bytes));
  115. ERC1967Utils.upgradeToAndCall(newImplementation, data, true);
  116. return "";
  117. }
  118. /**
  119. * @dev To keep this contract fully transparent, the fallback is payable. This helper is here to enforce
  120. * non-payability of function implemented through dispatchers while still allowing value to pass through.
  121. */
  122. function _requireZeroValue() private {
  123. if (msg.value != 0) {
  124. revert ProxyNonPayableFunction();
  125. }
  126. }
  127. }