Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article explains smart contract upgrades using the slides from the following study session.
A recording is also available:
For a more detailed explanation, see:
https://chaldene.net/upgradable-contracts
What is a smart contract upgrade?
Aren't smart contracts immutable?

You may have heard that deployed smart contracts cannot be updated. What, then, does upgrading a smart contract mean?

Ordinary deployed contract bytecode cannot simply be edited in place. A typical proxy-based upgrade instead deploys a new implementation contract and changes which implementation the proxy uses.
When are upgrades useful?

Consider a contract that accepts deposits and withdrawals. Suppose it contains a vulnerability that neither its users nor its operators initially recognize.
If the operators discover the vulnerability later, an immutable contract without an appropriate mitigation mechanism may leave deposited funds exposed.

An appropriately designed upgrade mechanism can allow authorized operators to install a corrected implementation. This can mitigate the discovered vulnerability, although it cannot guarantee that funds already lost will be recovered or that the replacement contains no other vulnerabilities.
The CEI pattern

The vulnerable withdrawal example illustrates why the Checks-Effects-Interactions (CEI) pattern matters.
Checks
Validate the conditions for the withdrawal before performing it. For example:
- Does the account have sufficient withdrawable funds?
- Is the caller authorized to withdraw them?
Effects
Update the contract's internal state, such as reducing the caller's recorded balance, before interacting with external contracts.
Interactions
Perform the external interaction, such as transferring funds to the recipient.
What happens if CEI is not followed?
Consider the order Checks → Interactions → Effects.
An external call made before updating the recorded balance can allow the recipient to call back into the withdrawal function. If the contract still exposes the old balance, the nested call may incorrectly pass the withdrawal checks.
This is a reentrancy vulnerability. It is not caused by a successful transaction simply stopping before its remaining instructions execute; an uncaught revert would roll back the relevant state changes.
For a more detailed explanation:
https://chaldene.net/solidity-reentrancy-attack
Advantages and disadvantages of upgradeability
Advantages

Adding functionality
An upgrade can introduce functionality that was not available in the initial implementation.
User confidence
A well-governed upgrade process can reassure users that discovered defects can be addressed.
Fixing vulnerabilities
An authorized upgrade can replace vulnerable application logic.
Long-term usability
As blockchain technologies evolve, applications can incorporate new capabilities without necessarily replacing the address through which users interact with them.
Disadvantages

Security risks
A new implementation or an unsafe upgrade process may introduce vulnerabilities. Upgrades require testing, review, and security assessment.
Gas overhead
Proxy-based execution introduces delegation overhead. Upgraded application logic may also have different execution costs.
Trust assumptions
Upgrade authority can undermine user confidence if operators are able to change application behavior arbitrarily. Governance controls, transparent upgrade procedures, and timelocks can help mitigate this concern.
Compatibility risks
Changing an implementation can break expectations held by users, applications, or other contracts. Maintaining compatible interfaces and storage layouts is important.
Storage slots
Understanding the EVM storage layout is essential for proxy-based upgrades. The following article provides a more detailed explanation:
https://chaldene.net/solidity-private-data#st-toc-h-3
DELEGATECALL
Proxy-based upgrades commonly rely on DELEGATECALL, which executes another contract's code in the caller's storage context. See:
https://chaldene.net/solidity-delegatecall#st-toc-h-1
Proxy contracts

A proxy contract provides an intermediary address through which users interact with an implementation contract.
For an upgradeable proxy, delegated execution allows the implementation's code to operate on the proxy's state.
How upgrades work
The upgrade mechanism

Initially, the proxy delegates calls to the implementation illustrated in blue.
Suppose the application needs additional functionality or a vulnerability fix.

The operators deploy a new implementation, illustrated in yellow. Through the authorized upgrade mechanism, the proxy is configured to use that implementation instead.

The proxy is sometimes described as the storage contract, while the implementation is called the logic contract. Both have distinct Ethereum addresses.
With DELEGATECALL, the implementation's code executes in the proxy's context. Application state is therefore read from and written to the proxy's storage rather than the implementation's own storage.

Why not simply deploy a replacement contract and ask users to interact with its address directly?

Users and integrators must discover the replacement address
Applications would need to announce the new address and update existing integrations. Some users could miss the announcement and continue interacting with the original contract.
Funds sent to an obsolete contract could become inaccessible
Depending on its implementation and available withdrawal mechanisms, an old contract could retain funds mistakenly sent to it.
Migrating application state can be expensive
Moving large amounts of stored application data to a replacement contract can require substantial gas and operational effort.

A proxy can preserve a stable interaction address and avoid copying existing application state merely to change implementations. Some upgrades may nevertheless require explicit initialization or state migration.
Upgrade considerations

Storage collisions

Solidity assigns state variables to storage slots according to its storage-layout rules, including inheritance and packing.
The slide compares existing proxy storage with the storage layout expected by a new implementation.

Changing variable order or incompatible variable types can cause the new implementation to interpret existing storage incorrectly. For example, it might read a slot previously used for a name as though it contained a wallet address.
Developers must preserve compatible storage layouts, including inherited state, and avoid collisions with proxy-management storage. Established patterns such as standardized implementation slots and namespaced storage can help, but do not eliminate the need for validation.
Function selector collisions


The EVM ABI uses a four-byte function selector derived from the first four bytes of the Keccak-256 hash of the canonical function signature, rather than the first four bytes of the function name.
Different signatures can produce the same selector.

A collision between a proxy function and an implementation function can cause a call to reach the wrong function, depending on the proxy's dispatch rules.
Proxy designs must account for this risk when separating administrative operations from application calls.
Constructors and initialization


A constructor runs during contract creation. An implementation constructor initializes the implementation's own storage, not the storage of an existing proxy.
Proxy-based applications therefore typically use an initializer executed in the proxy's context. Initializers must be protected against unauthorized or repeated execution. Implementation constructors can still be useful for tasks such as disabling initialization of the implementation itself.
Other upgrade patterns
Several architectures support contract upgrades.



Common approaches include transparent proxies, UUPS proxies, and beacon-based proxies. Their upgrade authorization and implementation-selection mechanisms differ.
For additional background:
https://chaldene.net/upgradable-contracts
The original study-session images are retained, including their Japanese labels. Their technical descriptions are supplemented by the English text above.
Conclusion
Smart contract upgradeability generally relies on changing the implementation used by a stable proxy rather than editing deployed bytecode directly.
This architecture can simplify maintenance and vulnerability fixes, but introduces important storage-layout, authorization, initialization, and compatibility requirements.
Questions are welcome via Twitter @cardene777.


