EIP-7761 EXTCODETYPE instruction
No call decision · EVM & execution · EIP status Stagnant · created 2024-09-01
Lets EOF contracts use the EXTCODETYPE instruction to distinguish whether a counterparty address is an EOA, a legacy contract, or an EOF contract.
Why it is needed
EOF (EIP-7692) removed instructions such as EXTCODESIZE that inspect code internals. However, the safeTransfer functions of ERC-721 and ERC-1155 need to distinguish whether the recipient is an EOA or a contract, and if it is a contract, call back a function like onERC721Received and check its result. EOF has no way to make this distinction, which makes it difficult to implement these two standards as-is. EOF also forbids using a legacy contract as an EXTDELEGATECALL target, so if an EOF proxy contract mistakenly upgrades its implementation to a legacy contract, there is no way to revert it, risking the proxy becoming permanently unusable. EXTCODETYPE is proposed as a minimal instruction to solve both problems.
What changes
Today
- EOF contracts have no code-inspection instructions like EXTCODESIZE, so they cannot tell whether a counterparty is an EOA or a contract
- The safeTransfer safeguard (callback branching) in ERC-721/1155 is difficult to implement as-is in EOF
- If an EOF proxy mistakenly upgrades its implementation to a legacy contract, there is no way to revert it, risking permanent breakage
After EIP-7761
- An EOF contract uses EXTCODETYPE to distinguish whether a target address is an EOA (TYPE_NONE), a legacy contract (TYPE_LEGACY_CONTRACT), or an EOF contract (TYPE_EOF_CONTRACT)
- The safeTransfer callback branching of ERC-721/1155 can be implemented even in an EOF environment
- An EOF proxy can check the target code type before an upgrade to prevent an unsafe upgrade
Who is affected
- Library and standard implementers: ERC-721 and ERC-1155 standards can be implemented in an EOF environment without EXTCODESIZE.
- Proxy and application developers: In the EOF proxy pattern, EXTCODETYPE can be used to verify whether the target is a legacy contract before upgrading the implementation.
- Client and EVM implementation teams: Must implement the new opcode 0xe9, its gas accounting, and the EIP-7702 delegated code handling logic.
In depth
Instruction and constants
EXTCODETYPE is a new EOFv1 instruction with opcode 0xe9, added to the set of valid instructions starting at block FORK_BLKNUM. Before that, EOF code containing 0xe9 is treated as invalid. The return value is one of three constants: TYPE_NONE (0) if there is no code, TYPE_LEGACY_CONTRACT (1) for a legacy contract, and TYPE_EOF_CONTRACT (2) for an EOFv1 contract.
Gas cost
EXTCODETYPE deducts GAS_WARM_ACCESS (100) by default. If target_address is accessed for the first time in this transaction (not in accessed_addresses), an additional amount equal to GAS_COLD_ACCOUNT_ACCESS (2600) minus GAS_WARM_ACCESS is deducted, and the address is registered in accessed_addresses.
Execution procedure
Pops target_address from the stack, and if its upper 12 bytes are not all zero, execution is aborted with an exception. After deducting gas, it loads the code of target_address; if the code is empty, it returns TYPE_NONE. The same applies if the contract is still being created and has no code yet, returning TYPE_NONE to match the behavior of existing instructions like EXTCODESIZE.
Interaction with EIP-7702 delegation
If the loaded code is an EIP-7702 delegation designator (e.g. 0xef0100), it is replaced with the delegated code per the designation and determined accordingly, with additional gas deducted per the delegation designation rules. If gas is insufficient, the entire message frame is aborted, making the state of accessed_addresses moot. After EIP-7702 is activated, there may be an edge case where an EOA sets its code to a contract that does not implement a callback like onERC721Received, causing safeTransfer to revert, but this is considered unlikely to be a problem in practice since real EIP-7702 use cases are expected to have the delegated code implement such callbacks.
Alternatives considered
To solve the ERC-721/1155 callback distinction problem, alternatives considered included adding a status code to EXT*CALL that carries EOA status, adding a 'fail if EOA' flag argument, two return values, a new CALLSTATUS register and instruction, or reintroducing EXTCODESIZE in EOF with the same behavior as legacy. Among these, EXTCODETYPE was chosen as the simplest solution that could be introduced into EOFv1.
Why a new opcode is used
The reason for using the new opcode 0xe9 instead of reusing the existing EXTCODESIZE (0x3b) is the general principle of not reusing opcodes, and the goal of allowing EXTCODETYPE to be independently introduced into the legacy EVM as well, if desired.
Backwards compatibility and current status
Before FORK_BLKNUM, 0xe9 is a value rejected at the EOF validation stage, so no already-deployed EOF contract containing 0xe9 would have its behavior changed. However, the source's Security Considerations section is empty with 'Needs discussion', FORK_BLKNUM is also undetermined ('tbd'), and this EIP's status is Stagnant. No discussion of fork inclusion appears in the call records.