EIPs Explained

ERC-1822 · Stagnant · ERC · 2019-03-04

ERC-1822: Universal Upgradeable Proxy Standard (UUPS)

The UUPS proxy pattern: a minimal proxy that stores the logic address in a fixed slot and puts the upgrade function in the logic contract itself, with a proxiableUUID check to avoid bricking upgrades.

What problem does it solve?

Contracts are immutable, so upgradeable systems split into a proxy (holds storage and the address users interact with) and a logic contract (holds code), with the proxy forwarding every call via delegatecall. Early proxies kept the logic address in an ordinary storage variable, which could collide with the logic contract's own variables, and they kept the upgrade code in the proxy.

UUPS ("Universal Upgradeable Proxy Standard") proposes:

  • storing the logic address at a pseudo-random slot, keccak256("PROXIABLE"), so it never collides with normal variables, and
  • putting the upgrade logic in the logic contract (via an inherited Proxiable base), keeping the proxy tiny and letting each implementation decide who may upgrade.

How it works

  • Proxy: a constructor that stores the logic address in the fixed slot and delegatecalls an initialisation payload (any "constructor" function of the logic contract), plus a fallback that delegatecalls everything else to the stored address.
  • Proxiable (in the logic contract):
    • proxiableUUID() returns the slot constant, signalling "I am UUPS-compatible".
    • updateCodeAddress(newAddress) (internal) checks that newAddress also returns the same proxiableUUID(), then writes it to the slot.

The check matters: if you upgraded to a contract that lacked the upgrade function, the proxy could never be upgraded again.

The spec also warns about storage layout (only append new variables) and about protecting the logic contract itself, e.g. so nobody can call dangerous functions such as selfdestruct on it directly.

Sample code

The UUPS pattern as most projects use it today, through OpenZeppelin's UUPSUpgradeable (which implements this EIP's proxiableUUID using the ERC-1967 slot):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract CounterV1 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
    uint256 public count;

    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() { _disableInitializers(); } // lock the logic contract itself

    function initialize(address owner) external initializer {
        __Ownable_init(owner);
    }

    function increment() external { count += 1; }

    // Upgrade authorisation lives in the logic contract (the core UUPS idea)
    function _authorizeUpgrade(address) internal override onlyOwner {}
}

The original spec's slot constant, for reference:

// keccak256("PROXIABLE")
bytes32 constant SLOT = 0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7;

Things to know

  • Status is Stagnant, but the pattern is widely used. In practice the logic address lives in the ERC-1967 implementation slot rather than keccak256("PROXIABLE").
  • If a new implementation forgets the upgrade function (or its access control), the proxy is frozen forever or can be taken over. Always test upgrades and use tooling that validates them.
  • Never reorder or remove existing state variables between versions; only append.
  • Initialise the proxy in the same transaction as deployment, and disable initialisers on the logic contract, to avoid front-running and logic-contract takeovers.
  • For multi-facet upgradeable systems, see ERC-2535 (Diamonds).

#proxy #upgradeable #delegatecall #security

Details

Authors
Gabriel Barros <[email protected]>, Patrick Gallagher <[email protected]>
Status
Stagnant
Type
Standards Track / ERC
Created
2019-03-04

Machine-readable: Markdown · JSON