Add documentation for proxies (#2344)

This commit is contained in:
Francisco Giordano
2020-09-02 20:19:54 -03:00
committed by GitHub
parent 885b76f66f
commit 6bc2ae3731
7 changed files with 179 additions and 125 deletions

View File

@ -6,19 +6,20 @@ import "./Proxy.sol";
import "../utils/Address.sol";
/**
* @title UpgradeableProxy
* @dev This contract implements a proxy that allows to change the
* implementation address to which it will delegate.
* Such a change is called an implementation upgrade.
* @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an
* implementation address that can be changed. This address is stored in storage in the location specified by
* https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the
* implementation behind the proxy.
*
* Upgradeability is only provided internally through {_upgradeTo}. For an externally upgradeable proxy see
* {TransparentUpgradeableProxy}.
*/
contract UpgradeableProxy is Proxy {
/**
* @dev Contract constructor.
* @param _logic Address of the initial implementation.
* @param _data Data to send as msg.data to the implementation to initialize the proxied contract.
* It should include the signature and the parameters of the function to be called, as described in
* https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
* This parameter is optional, if no data is given the initialization call to proxied contract will be skipped.
* @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`.
*
* If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded
* function call, and allows initializating the storage of the proxy like a Solidity constructor.
*/
constructor(address _logic, bytes memory _data) public payable {
assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1));
@ -28,11 +29,10 @@ contract UpgradeableProxy is Proxy {
(bool success,) = _logic.delegatecall(_data);
require(success);
}
}
}
/**
* @dev Emitted when the implementation is upgraded.
* @param implementation Address of the new implementation.
*/
event Upgraded(address indexed implementation);
@ -44,8 +44,7 @@ contract UpgradeableProxy is Proxy {
bytes32 private constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
/**
* @dev Returns the current implementation.
* @return impl Address of the current implementation
* @dev Returns the current implementation address.
*/
function _implementation() internal override view returns (address impl) {
bytes32 slot = _IMPLEMENTATION_SLOT;
@ -57,7 +56,8 @@ contract UpgradeableProxy is Proxy {
/**
* @dev Upgrades the proxy to a new implementation.
* @param newImplementation Address of the new implementation.
*
* Emits an {Upgraded} event.
*/
function _upgradeTo(address newImplementation) internal {
_setImplementation(newImplementation);
@ -65,8 +65,7 @@ contract UpgradeableProxy is Proxy {
}
/**
* @dev Sets the implementation address of the proxy.
* @param newImplementation Address of the new implementation.
* @dev Stores a new address in the EIP1967 implementation slot.
*/
function _setImplementation(address newImplementation) internal {
require(Address.isContract(newImplementation), "UpgradeableProxy: new implementation is not a contract");