Skip to content

Commit 0282c36

Browse files
PhABCfrangio
authored andcommitted
Using extcodehash instead of extcodesize for less gas (#1802)
* Using extcodehash instead of extcodesize for less gas `extcodehash` uses less gas then `extcodesize`. You can tell which address is a contract by the hash (see EIP-1052). * Fix * Add explainer * Update Address.sol * add changelog entry
1 parent c9f328e commit 0282c36

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
### New features:
66
* `Address.toPayable`: added a helper to convert between address types without having to resort to low-level casting. ([#1773](https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1773))
77

8+
### Improvements:
9+
* `Address.isContract`: switched from `extcodesize` to `extcodehash` for less gas usage. ([#1802](https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1802))
10+
11+
### Bugfixes
12+
813
## 2.3.0 (2019-05-27)
914

1015
### New features:

contracts/utils/Address.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ library Address {
1818
// This method relies in extcodesize, which returns 0 for contracts in
1919
// construction, since the code is only stored at the end of the
2020
// constructor execution.
21-
22-
uint256 size;
21+
22+
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
23+
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
24+
// for accounts without code, i.e. `keccak256('')`
25+
bytes32 codehash;
26+
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
2327
// solhint-disable-next-line no-inline-assembly
24-
assembly { size := extcodesize(account) }
25-
return size > 0;
28+
assembly { codehash := extcodehash(account) }
29+
return (codehash != 0x0 && codehash != accountHash);
2630
}
2731

2832
/**

0 commit comments

Comments
 (0)