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

Using extcodehash instead of extcodesize for less gas #1802

Merged
merged 5 commits into from
Jul 8, 2019

Conversation

PhABC
Copy link
Contributor

@PhABC PhABC commented Jun 18, 2019

extcodehash uses less gas then extcodesize. You can tell which address is a contract by the hash (see EIP-1052).

Fixes #

`extcodehash` uses less gas then `extcodesize`. You can tell which address is a contract by the hash (see EIP-1052).
@nventuro nventuro added the contracts Smart contract code. label Jun 19, 2019
@nventuro nventuro requested a review from frangio June 19, 2019 00:15
@nventuro
Copy link
Contributor

Thank you @PhABC! extcodehash uses 400 gas, while extcodesize uses 700. I did a quick test in Remix and this version does indeed seem to be cheaper, while preserving semantics.

We should add a comment explaining what 0x0 and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 mean: they are the codehashes for a new account, and an account with no code respectively. I assume 'new account' means an account that has not been targeted by any transaction, but I'm not entirely sure of this.

Also, could you add a changelog entry for this improvement? Thanks!

@PhABC
Copy link
Contributor Author

PhABC commented Jun 19, 2019

@nventuro Added comments! Not sure where to add a changelog entry however.

@frangio
Copy link
Contributor

frangio commented Jun 22, 2019

Oh wow, it's super interesting that it returns two different values depending if the account has been used or not! I wonder if this distinction can be used for something?

@PhABC The changelog entry should go in CHANGELOG.md, under a subsection "Improvements" for the last version.

@stale
Copy link

stale bot commented Jul 7, 2019

Hi all!
This Pull Request has not had any recent activity, is it still relevant? If so, what is blocking it? Is there anything we can do to help move it forward?
Thanks!

@stale stale bot added the stale label Jul 7, 2019
@stale stale bot removed the stale label Jul 8, 2019
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the changelog entry. Thank you @PhABC!

@frangio frangio merged commit 0282c36 into OpenZeppelin:master Jul 8, 2019
@juli
Copy link

juli commented Jul 13, 2020

This isContract code using EXTCODEHASH is not cheaper, consider changing it back to EXTCODESIZE if there isn't any other known benefit.
The gas cost savings (if any, not counting keccak256 constant deployment cost) were short-lived, EXTCODEHASH gas cost was changed to 700 by EIP-1884.

References:
EIP-1884
go-ethereum commit
Solidity compiler issue
Solidity CALL compiler code

@Amxx
Copy link
Collaborator

Amxx commented Nov 16, 2021

Note: EIP-2929 affect pricing of these opcodes.

Gas cost for both operations is the same:

  • 100 if the queried entry is in touched_storage_slots (warm access)
  • 2100 if the queried entry is in not touched_storage_slots (cold access)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts Smart contract code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants