feat: use extcodesize for isContract to reduce gas (#2311)
* feat: use extcodesize for isContract to reduce gas * feat: add changelog entry
This commit is contained in:
committed by
GitHub
parent
98e862e162
commit
c801c8d2bb
@ -1,5 +1,10 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 3.1.1 (Unreleased)
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
* `Address.isContract`: switched from `extcodehash` to `extcodesize` for less gas usage. ([#2311](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2311))
|
||||||
|
|
||||||
## 3.1.0 (2020-06-23)
|
## 3.1.0 (2020-06-23)
|
||||||
|
|
||||||
### New features
|
### New features
|
||||||
|
|||||||
@ -24,14 +24,14 @@ library Address {
|
|||||||
* ====
|
* ====
|
||||||
*/
|
*/
|
||||||
function isContract(address account) internal view returns (bool) {
|
function isContract(address account) internal view returns (bool) {
|
||||||
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
|
// This method relies in extcodesize, which returns 0 for contracts in
|
||||||
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
|
// construction, since the code is only stored at the end of the
|
||||||
// for accounts without code, i.e. `keccak256('')`
|
// constructor execution.
|
||||||
bytes32 codehash;
|
|
||||||
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
|
uint256 size;
|
||||||
// solhint-disable-next-line no-inline-assembly
|
// solhint-disable-next-line no-inline-assembly
|
||||||
assembly { codehash := extcodehash(account) }
|
assembly { size := extcodesize(account) }
|
||||||
return (codehash != accountHash && codehash != 0x0);
|
return size > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user