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 examines EIP-6913, which proposes adding the SETCODE (0xfc) instruction to replace contract code while preserving data, allowing a contract to be upgraded without changing its address.
This article translates, summarizes, and expands on the proposal linked below. It reflects the proposal and the historical behavior discussed in the original April 2024 article, rather than asserting that the proposed instruction is available on Ethereum today.
I have also written about a range of other EIPs.
Overview
The proposal introduces SETCODE (0xfc), an instruction that replaces the code of the executing account with code taken from memory.
Motivation
Smart contract upgrades are important because they allow contracts to gain functionality and let developers defer design decisions.
The following sections walk through existing upgrade mechanisms and the benefits of SETCODE.
For background on contract upgrades, see the following article.
https://chaldene.net/upgradable-contracts
Existing Upgrade Mechanisms
Using CALL
The oldest approach upgrades a contract by making a CALL to a separate implementation contract. Its limitation is that every future implementation must be able to modify the internal state.
Using DELEGATECALL
A proxy contract executes the code of an implementation contract on its behalf. This approach has the advantage of working around the account code size limit. Some proxies, however, branch to many different implementation accounts.
Using SELFDESTRUCT and CREATE2
To replace code in place, the contract destroys itself using SELFDESTRUCT and creates new code using CREATE2.
This improves on the earlier approaches by eliminating the need to call an external contract.
Its limitation is that SELFDESTRUCT deletes internal state.
In addition, because SELFDESTRUCT does not delete code until the transaction ends, the contract remains unavailable until CREATE2 completes the upgrade.
Introducing SETCODE
SETCODE offers a further improvement over the mechanisms described above.
It allows contract code to be replaced directly in place.
The instruction is proposed in anticipation of the deprecation of SELFDESTRUCT.
Using SETCODE makes it possible to update code immediately while retaining internal state, allowing safe updates without a loss of availability.
Introducing SETCODE would give developers more flexibility in managing and upgrading contracts.
This is particularly valuable for projects that need to maintain and operate services over long periods.
However, rigorous testing and verification are essential to ensure that the new code is safe and correct.
Specification
The instruction is intended to dynamically upgrade smart contract code, but several important constraints govern its behavior in particular execution environments and circumstances.
Constraints and Exception Handling
Restrictions in Read-Only Execution Scopes
Within a recursive read-only execution scope established by an instruction such as STATICCALL, executing SETCODE results in an exceptional halt.
Read-only scopes cannot perform state-changing operations, including code replacement.
Restrictions Within DELEGATECALL and CREATE
If the code currently executing differs from the executing account's code (for example, through DELEGATECALL or CREATE), executing the SETCODE instruction results in an exceptional halt.
In these contexts, the code being executed is not the account's own deployed code, so modifying that account's code is not permitted.
Execution and Validation
SETCODE consumes two values from the stack: an offset and a length.
Together they specify the memory range containing the new code.
The new code is validated immediately, using the same checks performed on the result of CREATE or CREATE2.
A validation failure may therefore cause an exceptional halt.
The EXTCODESIZE and EXTCODECOPY operations query the updated code.
Message calls such as DELEGATECALL, CALLCODE, CALL, and STATICCALL execute the updated code.
Execution scopes that are already running the replaced code, including the scope that executed SETCODE, continue executing the old code.
Within those scopes, CODESIZE and CODECOPY continue to query the code being executed.
Exceptions and Rollback
Account changes made by SETCODE are rolled back if the current scope or any parent scope executes a revert or halts exceptionally.
This is analogous to the behavior of the SSTORE instruction.
Differences from SELFDESTRUCT
SETCODE differs from SELFDESTRUCT in that it does not clear the account's balance, nonce, or storage.
Gas Consumption
The gas cost of SETCODE is the sum of Gselfdestruct and the number of bytes in the new code multiplied by Gcodedeposit.
This accounts for the computational resources needed to replace the code.
The specification sets strict conditions for performing dynamic code upgrades safely, with error handling and security in mind.
Additional Rationale
Consistency of Behavior
The behavior of CODECOPY, CODESIZE, EXTCODESIZE, and EXTCODECOPY is consistent with their behavior under DELEGATECALL and CREATE because the code being executed may differ from the executing account's code.
In such situations, the instructions need to refer to the appropriate code for the execution context.
Rationale for Gas Consumption
The gas cost of SETCODE is comparable to that of CREATE, but excludes Gcreate, the cost associated with creating a new execution context or a new account.
This is because SETCODE simply replaces the code of an existing account, rather than creating a new context or account.
Other account modification costs are accounted for outside execution gas.
Comparison with SELFDESTRUCT
Unlike SELFDESTRUCT, execution continues normally after SETCODE, allowing validation and the provision of return values.
SELFDESTRUCT terminates execution and immediately deletes the associated account according to this part of the original explanation, whereas SETCODE allows post-update validation and makes it possible to undo the operation using REVERT or a recursive SETCODE if necessary.
Using REVERT allows the SETCODE operation to be undone while saving gas.
Why SETCODE Is Prohibited Within DELEGATECALL
In most DELEGATECALL contexts, prohibiting SETCODE makes it easier to identify mutable code through static analysis.
Account code that does not contain a SETCODE operation can safely be assumed to be immutable.
If code observed in a context that does not revert is immutable, it can continue to be treated as immutable, allowing its immutability to be verified through on-chain static analysis.
This design aims to enable dynamic smart contract upgrades while improving security and efficiency. Developers can maintain code safety while updating contract behavior when necessary.
Compatibility
SELFDESTRUCT Behavior
The SELFDESTRUCT instruction destroys a smart contract and transfers its balance to a specified address.
In the historical behavior discussed by the proposal, the operation does not immediately invalidate the contract: its code and state are not deleted until the end of the transaction.
Other instructions can therefore still execute within the transaction after SELFDESTRUCT has run, but the contract is ultimately deleted when the transaction completes.
Relationship with SETCODE
SETCODE is an instruction for replacing smart contract code in place.
The new code takes effect immediately and replaces the old code.
Since code deletion by SELFDESTRUCT is deferred until the end of the transaction, if SELFDESTRUCT and SETCODE are both used in the same transaction, even code newly installed by SETCODE is deleted along with the entire contract when the transaction ends.
Defined Interaction
A well-defined interaction between SELFDESTRUCT and SETCODE means that how these instructions behave within the same transaction, and the resulting outcome, are predictable.
For example, if SELFDESTRUCT is invoked first and then SETCODE is executed, the new code installed by SETCODE is effective for the remainder of the transaction. At the end of the transaction, however, SELFDESTRUCT deletes the entire account, making the effect temporary.
Although SELFDESTRUCT and SETCODE each have distinct purposes and effects, their use within a single transaction produces predictable and consistent results.
Defining this interaction clearly allows developers to use the instructions more safely and effectively.
Security
Risks of Upgradeability
Persistent Risks of Upgrades
The mere existence of an upgrade mechanism can introduce lasting risks. An improper upgrade may cause a contract to fail permanently.
Limiting Upgradeability
Many contracts should not be upgradeable or subject to change. Where fixed functionality or immutable logic is required, it is preferable to omit upgrade mechanisms entirely.
Restricting Access to Upgrade Operations
Access Restrictions
Access to upgrade operations should be limited to specific users or conditions. Strict access controls prevent unauthorized upgrades.
Performing Upgrades Carefully
Upgrades should not be performed in a hurry or while fatigued. Carelessness and rushed decisions can lead to security incidents.
Thorough Testing
Upgrades should be tested under conditions as close to production as possible. Differences between environments can lead to unexpected outcomes.
Verification by Multiple Engineers
Where possible, multiple engineers should check the upgrade procedure in advance and verify it independently. This helps reduce mistakes and oversights.
The Role of Software and Interfaces
Block Explorers, Wallets, and Other Interfaces
These tools should explicitly identify upgradeable code so users can recognize it.
Client Software Warnings
Client software should display warnings when users approve ERC20 or ERC721 tokens for upgradeable accounts. This helps users recognize the security risks to their tokens.
For more information about ERC20, see the following article.
For more information about ERC721, see the following article.
This section presents concrete strategies and techniques for minimizing the potential risks of upgrade mechanisms and ensuring safety. These guidelines help developers and users operate smart contracts more securely.
Citation
William Morriss (@wjmelements), "EIP-6913: SETCODE instruction [DRAFT]," Ethereum Improvement Proposals, no. 6913, April 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6913.
Conclusion
This article examined EIP-6913, which proposes adding SETCODE (0xfc) to replace contract code while preserving data, allowing contracts to be upgraded without changing their addresses.
I hope this overview helped clarify the proposal.
If you have any questions, feel free to reach out via Twitter direct messages.
I also share information on other platforms, which you can explore below.



