Lesson 3 of 4 Secure Smart Contract Development

Upgradeable Contracts and Proxies

Upgradeability lets a team fix bugs after deployment, at the cost of complexity and trust. This lesson explains how proxies work, compares transparent, UUPS and beacon proxies, and covers initialisers, storage layout and the security risks of upgrades.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how a proxy forwards calls to an implementation.
  • Compare transparent, UUPS and beacon proxies.
  • Explain why upgradeable contracts use initialisers.
  • Identify storage layout rules for upgrades.
  • Describe the trust trade-off of upgradeability.

How a proxy works

Deployed code cannot change, but a contract can forward calls elsewhere. In the proxy pattern, users interact with a proxy contract that holds all state and ether. Its fallback function forwards every call with delegatecall to an implementation (logic) contract, whose code then runs against the proxy's storage. Upgrading means changing the implementation address stored in the proxy; the proxy's address, state and balance stay the same.

OpenZeppelin proxies store the implementation address in a standard, pseudo-random storage slot defined by ERC-1967, so it cannot collide with the implementation's own variables.

The following diagram shows a proxy before and after an upgrade.

How an upgradeable proxy works. Users always call the proxy, which holds all state and ether. Its fallback forwards every call with delegatecall to the implementation contract, whose code runs against the proxy's storage. Upgrading changes the implementation address, stored in the ERC-1967 slot, so the proxy's address, state and balance stay the same. Storage layouts of old and new implementations must stay compatible.
Users keep one address while the logic changes.
True or false

Inside a proxy

Exam skills 3.2.3

Check how a proxy and its implementation share the work.

Log in to try this activity Interactive activities are free with an account.

Proxy types

The following comparison sets out the three common proxy designs.

Transparent proxy
Upgrade logic lives inThe proxy (managed by a ProxyAdmin)
Gas per callSlightly higher
Main riskAdmin and user function clashes (handled by the pattern)
Typical useGeneral purpose
UUPS proxy
Upgrade logic lives inThe implementation (ERC-1822)
Gas per callLower
Main riskDeploying an implementation without upgrade logic bricks upgrades
Typical useMost new upgradeable contracts
Beacon proxy
Upgrade logic lives inA beacon contract that proxies query
Gas per callSlightly higher
Main riskOne beacon change upgrades every proxy
Typical useMany identical instances, such as user vaults
Sort into groups

Which proxy type?

Exam skills 3.2.3

Match each description to the proxy design it belongs to.

Log in to try this activity Interactive activities are free with an account.

Initialisers and storage layout

A constructor runs when the implementation is deployed, writing to the implementation's own storage, not the proxy's. Upgradeable contracts therefore replace constructors with an initialize function, protected by OpenZeppelin's initializer modifier so it can run only once, and called through the proxy at deployment. The implementation's constructor calls _disableInitializers() so nobody can initialise the implementation directly.

Because the implementation code reads the proxy's storage slot by slot, every new version must keep the existing layout. The following list gives the rules.

  • Never remove, reorder or change the type of existing state variables.
  • Add new variables only after the existing ones (or in a reserved gap or namespaced storage).
  • Do not use immutable for values that must survive upgrades unless intended, and do not set state in constructors.
  • Use the OpenZeppelin upgrades plugins, which check layout compatibility before an upgrade.
Scenario questions

Safe upgrades

Exam skills 3.2.3

Apply the storage layout rules when writing a new implementation version.

Log in to try this activity Interactive activities are free with an account.

The trade-off

Upgradeability is a trust decision

Upgradeability replaces "the code cannot change" with "the code can change whenever the upgrade key holder decides". Users must trust that key holder. Production systems protect upgrades with a multisig and a timelock, announce upgrades in advance, and some deliberately deploy immutable contracts or renounce upgrade rights once the system is mature.

Worked example

A corrupted balance

Version 1 of an upgradeable token declares uint256 totalSupply; mapping(address => uint256) balances;. Version 2 adds address treasury; as the first state variable. After the upgrade, treasury returns a strange address and balances look wrong. Why?

Inserting treasury first shifted every variable by one slot: version 2 reads slot 0, which still holds version 1's totalSupply, as the treasury address, and looks for balances in the wrong place. New variables must be appended after the existing ones. The upgrades plugin's layout check would have rejected this upgrade.

Worked example

The uninitialised implementation

A team deploys a UUPS implementation and initialises only the proxy. A security researcher warns that the implementation contract itself is uninitialised. Why does it matter?

Anyone could call initialize on the implementation, become its owner and, in older UUPS designs, call its upgrade function to make it delegatecall code that destroys or corrupts it, affecting every proxy that uses it. Calling _disableInitializers() in the implementation's constructor prevents this.

Check yourself

Why can an upgradeable implementation not use its constructor to set the owner?

Show answer

The constructor runs when the implementation is deployed and writes to the implementation's own storage, not the proxy's. An initialize function called through the proxy sets the proxy's state.

Exam Tips

Remember for the exam

  • A proxy holds state and delegatecalls an implementation that can be replaced.
  • Transparent: upgrade logic in the proxy; UUPS: in the implementation; beacon: shared by many proxies.
  • Use initialize with the initializer modifier, not constructors; disable initialisers on implementations.
  • Never reorder or remove state variables; append new ones.
  • Upgrade keys need multisig and timelock protection.