Skip to content

Commit 6bc2ae3

Browse files
authored
Add documentation for proxies (#2344)
1 parent 885b76f commit 6bc2ae3

File tree

7 files changed

+179
-125
lines changed

7 files changed

+179
-125
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ charset = utf-8
88
end_of_line = lf
99
indent_style = space
1010
insert_final_newline = true
11-
trim_trailing_whitespace = true
11+
trim_trailing_whitespace = false
1212
max_line_length = 120
1313

1414
[*.sol]
1515
indent_size = 4
1616

1717
[*.js]
1818
indent_size = 2
19+
20+
[*.adoc]
21+
max_line_length = 0

contracts/proxy/Initializable.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ pragma solidity >=0.4.24 <0.7.0;
44

55

66
/**
7-
* @title Initializable
8-
*
9-
* @dev Helper contract to support initializer functions. To use it, replace
10-
* the constructor with a function that has the `initializer` modifier.
11-
* WARNING: Unlike constructors, initializer functions must be manually
12-
* invoked. This applies both to deploying an Initializable contract, as well
13-
* as extending an Initializable contract via inheritance.
14-
* WARNING: When used with inheritance, manual care must be taken to not invoke
15-
* a parent initializer twice, or ensure that all initializers are idempotent,
16-
* because this is not dealt with automatically as with constructors.
7+
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
8+
* behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
9+
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
10+
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
11+
*
12+
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
13+
* possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
14+
*
15+
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
16+
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
1717
*/
1818
contract Initializable {
1919

@@ -28,7 +28,7 @@ contract Initializable {
2828
bool private _initializing;
2929

3030
/**
31-
* @dev Modifier to use in the initializer function of a contract.
31+
* @dev Modifier to protect an initializer function from being invoked twice.
3232
*/
3333
modifier initializer() {
3434
require(_initializing || _isConstructor() || !_initialized, "Initializable: contract is already initialized");

contracts/proxy/Proxy.sol

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,20 @@
33
pragma solidity ^0.6.0;
44

55
/**
6-
* @title Proxy
7-
* @dev Implements delegation of calls to other contracts, with proper
8-
* forwarding of return values and bubbling of failures.
9-
* It defines a fallback function that delegates all calls to the address
10-
* returned by the abstract _implementation() internal function.
6+
* @dev This abstract contract provides a fallback function that delegates all calls to another contract using the EVM
7+
* instruction `delegatecall`. We refer to the second contract as the _implementation_ behind the proxy, and it has to
8+
* be specified by overriding the virtual {_implementation} function.
9+
*
10+
* Additionally, delegation to the implementation can be triggered manually through the {_fallback} function, or to a
11+
* different contract through the {_delegate} function.
12+
*
13+
* The success and return data of the delegated call will be returned back to the caller of the proxy.
1114
*/
1215
abstract contract Proxy {
1316
/**
14-
* @dev Fallback function.
15-
* Implemented entirely in `_fallback`.
16-
*/
17-
fallback () payable external {
18-
_fallback();
19-
}
20-
21-
/**
22-
* @dev Receive function.
23-
* Implemented entirely in `_fallback`.
24-
*/
25-
receive () payable external {
26-
_fallback();
27-
}
28-
29-
/**
30-
* @return The Address of the implementation.
31-
*/
32-
function _implementation() internal virtual view returns (address);
33-
34-
/**
35-
* @dev Delegates execution to an implementation contract.
36-
* This is a low level function that doesn't return to its internal call site.
37-
* It will return to the external caller whatever the implementation returns.
38-
* @param implementation Address to delegate.
17+
* @dev Delegates the current call to `implementation`.
18+
*
19+
* This function does not return to its internall call site, it will return directly to the external caller.
3920
*/
4021
function _delegate(address implementation) internal {
4122
// solhint-disable-next-line no-inline-assembly
@@ -60,19 +41,43 @@ abstract contract Proxy {
6041
}
6142

6243
/**
63-
* @dev Function that is run as the first thing in the fallback function.
64-
* Can be redefined in derived contracts to add functionality.
65-
* Redefinitions must call super._willFallback().
44+
* @dev This is a virtual function that should be overriden so it returns the address to which the fallback function
45+
* and {_fallback} should delegate.
6646
*/
67-
function _willFallback() internal virtual {
68-
}
47+
function _implementation() internal virtual view returns (address);
6948

7049
/**
71-
* @dev fallback implementation.
72-
* Extracted to enable manual triggering.
50+
* @dev Delegates the current call to the address returned by `_implementation()`.
51+
*
52+
* This function does not return to its internall call site, it will return directly to the external caller.
7353
*/
7454
function _fallback() internal {
7555
_willFallback();
7656
_delegate(_implementation());
7757
}
58+
59+
/**
60+
* @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if no other
61+
* function in the contract matches the call data.
62+
*/
63+
fallback () payable external {
64+
_fallback();
65+
}
66+
67+
/**
68+
* @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if call data
69+
* is empty.
70+
*/
71+
receive () payable external {
72+
_fallback();
73+
}
74+
75+
/**
76+
* @dev Hook that is called before falling back to the implementation. Can happen as part of a manual `_fallback`
77+
* call, or as part of the Solidity `fallback` or `receive` functions.
78+
*
79+
* If overriden should call `super._willFallback()`.
80+
*/
81+
function _willFallback() internal virtual {
82+
}
7883
}

contracts/proxy/ProxyAdmin.sol

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import "../access/Ownable.sol";
66
import "./TransparentUpgradeableProxy.sol";
77

88
/**
9-
* @title ProxyAdmin
10-
* @dev This contract is the admin of a proxy, and is in charge
11-
* of upgrading it as well as transferring it to another admin.
9+
* @dev This is an auxiliary contract meant to be assigned as the admin of a {TransparentUpgradeableProxy}. For an
10+
* explanation of why you would want to use this see the documentation for {TransparentUpgradeableProxy}.
1211
*/
1312
contract ProxyAdmin is Ownable {
1413

1514
/**
16-
* @dev Returns the current implementation of a proxy.
17-
* This is needed because only the proxy admin can query it.
18-
* @return The address of the current implementation of the proxy.
15+
* @dev Returns the current implementation of `proxy`.
16+
*
17+
* Requirements:
18+
*
19+
* - This contract must be the admin of `proxy`.
1920
*/
2021
function getProxyImplementation(TransparentUpgradeableProxy proxy) public view returns (address) {
2122
// We need to manually run the static call since the getter cannot be flagged as view
@@ -26,8 +27,11 @@ contract ProxyAdmin is Ownable {
2627
}
2728

2829
/**
29-
* @dev Returns the admin of a proxy. Only the admin can query it.
30-
* @return The address of the current admin of the proxy.
30+
* @dev Returns the current admin of `proxy`.
31+
*
32+
* Requirements:
33+
*
34+
* - This contract must be the admin of `proxy`.
3135
*/
3236
function getProxyAdmin(TransparentUpgradeableProxy proxy) public view returns (address) {
3337
// We need to manually run the static call since the getter cannot be flagged as view
@@ -38,31 +42,34 @@ contract ProxyAdmin is Ownable {
3842
}
3943

4044
/**
41-
* @dev Changes the admin of a proxy.
42-
* @param proxy Proxy to change admin.
43-
* @param newAdmin Address to transfer proxy administration to.
45+
* @dev Changes the admin of `proxy` to `newAdmin`.
46+
*
47+
* Requirements:
48+
*
49+
* - This contract must be the current admin of `proxy`.
4450
*/
4551
function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public onlyOwner {
4652
proxy.changeAdmin(newAdmin);
4753
}
4854

4955
/**
50-
* @dev Upgrades a proxy to the newest implementation of a contract.
51-
* @param proxy Proxy to be upgraded.
52-
* @param implementation the address of the Implementation.
56+
* @dev Upgrades `proxy` to `implementation`. See {TransparentUpgradeableProxy-upgradeTo}.
57+
*
58+
* Requirements:
59+
*
60+
* - This contract must be the admin of `proxy`.
5361
*/
5462
function upgrade(TransparentUpgradeableProxy proxy, address implementation) public onlyOwner {
5563
proxy.upgradeTo(implementation);
5664
}
5765

5866
/**
59-
* @dev Upgrades a proxy to the newest implementation of a contract and forwards a function call to it.
60-
* This is useful to initialize the proxied contract.
61-
* @param proxy Proxy to be upgraded.
62-
* @param implementation Address of the Implementation.
63-
* @param data Data to send as msg.data in the low level call.
64-
* It should include the signature and the parameters of the function to be called, as described in
65-
* https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
67+
* @dev Upgrades `proxy` to `implementation` and calls a function on the new implementation. See
68+
* {TransparentUpgradeableProxy-upgradeToAndCall}.
69+
*
70+
* Requirements:
71+
*
72+
* - This contract must be the admin of `proxy`.
6673
*/
6774
function upgradeAndCall(TransparentUpgradeableProxy proxy, address implementation, bytes memory data) public payable onlyOwner {
6875
proxy.upgradeToAndCall{value: msg.value}(implementation, data);

contracts/proxy/README.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
= Proxies
2+
3+
[.readme-notice]
4+
NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/proxy
5+
6+
This is a low-level set of contracts implementing the proxy pattern for upgradeability. For an in-depth overview of this pattern check out the xref:upgrades-plugins::proxies.adoc[Proxy Upgrade Pattern] page.
7+
8+
The abstract {Proxy} contract implements the core delegation functionality. If the concrete proxies that we provide below are not suitable, we encourage building on top of this base contract since it contains an assembly block that may be hard to get right.
9+
10+
Upgradeability is implemented in the {UpgradeableProxy} contract, although it provides only an internal upgrade interface. For an upgrade interface exposed externally to an admin, we provide {TransparentUpgradeableProxy}. Both of these contracts use the storage slots specified in https://eips.ethereum.org/EIPS/eip-1967[EIP1967] to avoid clashes with the storage of the implementation contract behind the proxy.
11+
12+
CAUTION: Using upgradeable proxies correctly and securely is a difficult task that requires deep knowledge of the proxy pattern, Solidity, and the EVM. Unless you want a lot of low level control, we recommend using the xref:upgrades-plugins::index.adoc[OpenZeppelin Upgrades Plugins] for Truffle and Buidler.
13+
14+
== Core
15+
16+
{{Proxy}}
17+
18+
{{UpgradeableProxy}}
19+
20+
{{TransparentUpgradeableProxy}}
21+
22+
== Utilities
23+
24+
{{Initializable}}
25+
26+
{{ProxyAdmin}}

0 commit comments

Comments
 (0)