ERC-1822 Universal upgradeable proxy standard (Universal Upgradeable Proxy Standard (UUPS))
Stagnant · Other · created 2019-03-04
A standard that stores the logic contract address in a unique storage slot of the proxy contract, allowing the contract's behavior code to be safely replaced while preserving existing data.
Why it is needed
Smart contracts cannot change their code after deployment, so the proxy pattern is used to fix bugs or add features. However, existing proxy implementations risked storage collisions because the proxy and logic contracts used the same storage location for different variables. There was also no standard way to verify that a new logic contract was actually compatible with the proxy, so replacing it with an incompatible logic contract could irrecoverably break the entire proxy. There was also the risk, as seen in the Parity wallet hack, that the logic contract could be called directly from outside and destroyed or tampered with. Constructor argument handling also varied from case to case, making it hard to verify the proxy's bytecode.
What to implement
function() external payable {: Forwards all incoming call data to the stored logic contract via delegatecall and returns the result as is.constructor(bytes memory constructData, address contractLogic) public {: Stores the logic contract address in a unique storage slot and executes the logic contract's constructor via delegatecall using the received initialization data.function updateCodeAddress(address newAddress) internal {: After confirming that the new logic contract's proxiableUUID value matches, replaces the logic address in the storage slot with the new address.function proxiableUUID() public pure returns (bytes32) {: Returns the unique storage slot value this contract uses, allowing compatibility with the contract being upgraded to be verified.
In depth
Unique storage slot and fallback behavior
Instead of declaring a variable, the proxy directly reads and writes the logic contract address with sload/sstore to a unique storage slot, 0xc5f16f0f...622bcf7, the value of keccak256("PROXIABLE"). The fallback function reads the logic address from this slot, copies the entire calldata, and delegatecalls while leaving sub(gas, 10000) gas remaining. If the call fails, it reverts with the returned data; if it succeeds, it returns the returned data as is. Because it uses a fixed hash slot instead of a variable, the storage location never overlaps with any logic contract's variables, giving it 'universal' compatibility.
Arbitrary-argument constructor and multiple constructor selection
The proxy's constructor takes only two values: bytes memory constructData and address contractLogic. After storing contractLogic in the unique slot, it delegatecalls constructData directly to the logic contract to perform the constructor role. This approach lets the logic contract select and call one of multiple constructor functions, such as constructor1 or constructor2. However, if there are multiple constructors, the logic contract side must add a check to prevent re-invocation after initialization. Also, since the calldata passed at creation can be decoded first with the proxy ABI and then with the logic contract ABI, supporting multiple constructors does not interfere with bytecode verification.
Proxiable contract and compatibility check
The logic contract must inherit from Proxiable and implement updateCodeAddress and proxiableUUID. proxiableUUID() is a pure function that returns the unique slot value 0xc5f16f0f...622bcf7 as is. updateCodeAddress(newAddress) only changes the slot value to newAddress via sstore after using require to confirm that Proxiable(newAddress).proxiableUUID() equals this value. It explains that this comparison value could be changed in the future, for example to keccak256("PROXIABLE-ERC1822-v1"), to distinguish other versions of the standard.
Separation of variables and logic
To preserve storage compatibility after an upgrade, the new logic contract must not change the declaration order of existing variables, and new variables must always be added after the existing ones. To make this easier to follow, it is recommended to create a single 'base' contract holding all variables and have subsequent logic contracts inherit from it. This greatly reduces the risk of accidentally changing variable order or overwriting storage values.
Restricting dangerous functions and preventing a Parity wallet style hack
Compatibility checks alone cannot prevent the logic contract itself from being damaged. This is in fact what happened in the Parity wallet hack. To prevent this, it is recommended that native functions like SELFDESTRUCT and functions that can execute external code like CALLCODE and delegatecall() be restricted with onlyOwner, and that ownership of the logic contract be transferred to a null address such as address(1) immediately upon deployment. In the ERC-20 example, the LibraryLock contract has an initialized flag and a delegatedOnly modifier to prevent the logic contract from being tampered with via direct calls that bypass the proxy.
ERC-20 example implementation
The example shows a MyToken contract that inherits from ERC20DataLayout (totalSupply, tokens mapping), Owned, Proxiable, and LibraryLock. constructor1 initializes totalSupply and tokens, locks LibraryLock via initialize(), and then assigns the owner via setOwner. updateCode is protected by two modifiers, onlyOwner and delegatedOnly, so updateCodeAddress can only be executed by the owner and only through a call made via the proxy. transfer is likewise protected by delegatedOnly to block direct calls.
Proposal status
This proposal is Stagnant: no further discussion or revision has taken place since it was written in 2019. Since it was never finalized as a standard, it should undergo a separate review of up to date implementations and security before being adopted as is.
Things to watch out for
- Maintain storage variable order: When changing the logic contract, the declaration order of existing variables must not be changed, and new variables must always be added after the existing ones. Violating this will cause storage values to become mixed up after an upgrade.
- Restrict access to dangerous functions: Functions that can execute external code or destroy the contract, such as SELFDESTRUCT, CALLCODE, and delegatecall(), must be restricted with onlyOwner, and ownership of the logic contract must be transferred to a null address or similar immediately upon deployment. Otherwise, the logic contract itself could be destroyed, as in the Parity wallet hack.
- Required implementation of updateCodeAddress and proxiable: The logic contract must have updateCodeAddress and proxiableUUID. If they are missing, upgrades may be blocked or the proxy may become completely unusable.