Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EIP: Optimize EOA EXTCODEHASH #8261

Merged
merged 15 commits into from
Aug 3, 2024
101 changes: 101 additions & 0 deletions EIPS/eip-7637.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
eip: 7637
title: Optimize EOA EXTCODEHASH
description: Modify the output value of EXTCODEHASH for EOA accounts to `0x`
author: Jame (@ZWJKFLC)
discussions-to: https://ethereum-magicians.org/t/eip-7637-extcodehash-optimize/18946
status: Draft
type: Standards Track
category: Core
created: 2024-02-26
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved
requires: 1052
---



## Abstract

This proposal is an optimization for [EIP-1052](./eip-1052.md),
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved
For addresses with a balance, but without code, the codehash should still be `0x`.

When an address `add.code == 0x` and `add.balance != 0`, `add.codehash==0` is required instead of `add.codehash==keccak256("")`

## Motivation

EIP-1052 was proposed to save gas fees. However, due to some flaws in the set specifications, in actual applications, due to safety concerns, they will not actually be used. In order for EIP-1052 to be truly useful, it should be optimized.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved

If someone uses it based on the proposal of EIP-1052 and does not notice the change when `add.balance != 0`, there may be security issues.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved


## Specification

The behaviour of `EXTCODEHASH` is changed in the following way:

1. When calling `EXTCODEHASH`, the codehash of the address with balance but no code is still `0x`


## Rationale
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved

EIP-1052 In order to include the function of `BALANCE`, let the `EXTCODEHASH` of the address without balance be `0x`, and the `EXTCODEHASH` of the address with balance be `keccak256("")`.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved

The contract address can be calculated in advance. Whether it is `CREATE` or `CREATE2`, it is possible that the contract is not created but has a balance. For security, You can actually only use `keccak256(add.code) == keccak256("")` or `add.code.length ==0` instead of `add.codehash == 0`,, which makes the original intention of EIP-1052 meaningless.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved

For example, uniswap V2 uses stored addresses to determine whether a contract exists. If this `EXTCODEHASH` is optimized, can save a huge amount of gas.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved

If someone uses a `add.codehash==0` to determine whether a contract has been created, due to intuition and the lack of details in many documents, they will not think that the codehash of an address with a balance will change from `0x` to `keccak256("")`. If someone maliciously attacks at this time, it will cause some bad effects.


## Backwards Compatibility

<!-- TODO: -->

Check warning on line 50 in EIPS/eip-7637.md

View workflow job for this annotation

GitHub Actions / EIP Walidator

HTML comments are only allowed while `status` is one of: `Draft`, `Withdrawn`

warning[markdown-html-comments]: HTML comments are only allowed while `status` is one of: `Draft`, `Withdrawn` --> EIPS/eip-7637.md | 50 | <!-- TODO: --> | ::: EIPS/eip-7637.md | 93 | <!-- TODO: --> | = help: see https://ethereum.github.io/eipw/markdown-html-comments/
Needs discussion.
It is unclear whether there is a situation where codehash is actually used to determine whether an address has a balance and the codehash is `keccak256("")`.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved


## Reference Implementation

Code reference for execution-specs

After modification

```python
def extcodehash(evm: Evm) -> None:
address = to_address(pop(evm.stack))
charge_gas(evm, GAS_CODE_HASH)
account = get_account(evm.env.state, address)
codehash = U256.from_be_bytes(keccak256(account.code))
if codehash == U256(hexstr="c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"):
codehash = U256(0)
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved
push(evm.stack, codehash)
evm.pc += 1
```


Source code

```python
def extcodehash(evm: Evm) -> None:
address = to_address(pop(evm.stack))
charge_gas(evm, GAS_CODE_HASH)
account = get_account(evm.env.state, address)
if account == EMPTY_ACCOUNT:
codehash = U256(0)
else:
codehash = U256.from_be_bytes(keccak256(account.code))

push(evm.stack, codehash)
evm.pc += 1
```


## Security Considerations

<!-- TODO: -->
Needs discussion.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved
It is unclear whether there is a situation where codehash is actually used to determine whether an address has a balance and the codehash is `keccak256("")`.
ZWJKFLC marked this conversation as resolved.
Show resolved Hide resolved


## Copyright

Copyright and related rights waived via [CC0](../LICENSE.md).

Loading