We are trying to modify a solidity smart contract code for ourselves but couldn't figure out what this block does.
function isContract(address account) internal view returns (bool) { // According to EIP-1052, 0x0 is the value returned for not-yet created accounts // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned // for accounts without code, i.e. keccak256('') bytes32 codehash; bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; // solhint-disable-next-line no-inline-assembly assembly { codehash := extcodehash(account) } return (codehash != accountHash && codehash != 0x0); } Can you please help us out?Here is the full source code of the contract
11 Answer
The extcodehash opcode (introduced in EIP-1052) returns a hash of the address bytecode (instead of the actual bytecode).
Hash of 0x0 is stored in the accountHash value. So if the extcodehash returns the 0xc5d2... value, it means it's an EOA (stands for "externally owned address", simply - an address without a contract).
Also, extcodehash of a never used address address is 0x0. The definition of "never used" is in the EIP-161 linked from the 1052.
So the isContract() function returns true, if both conditions are met:
The address holds a non-zero bytecode, i.e. it's a smart contract (
codehash != accountHash).AND
The address has been used by a state-changing operation. (
codehash != 0x0). Which is a redundant condition, because contract deployment is always a state-changing operation.