From 6b6d4383a59e8f2f3e7574085c509de47dceadce Mon Sep 17 00:00:00 2001 From: Ustas <82833595+ustas-eth@users.noreply.github.com> Date: Sun, 21 Jan 2024 13:48:21 +0000 Subject: [PATCH] [docs] Add warning about `codehash` behavior depending on the account state Co-authored-by: r0qs --- docs/types/value-types.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/types/value-types.rst b/docs/types/value-types.rst index 12fcb0b171d6..ff4b1defd8a9 100644 --- a/docs/types/value-types.rst +++ b/docs/types/value-types.rst @@ -338,6 +338,13 @@ You can query the deployed code for any smart contract. Use ``.code`` to get the ``bytes memory``, which might be empty. Use ``.codehash`` to get the Keccak-256 hash of that code (as a ``bytes32``). Note that ``addr.codehash`` is cheaper than using ``keccak256(addr.code)``. +.. warning:: + The output of ``addr.codehash`` may be ``0`` if the account associated with ``addr`` is empty or non-existent + (i.e., it has no code, zero balance, and zero nonce as defined by `EIP-161 `_). + If the account has no code but a non-zero balance or nonce, then ``addr.codehash`` will output the Keccak-256 hash of empty data + (i.e., ``keccak256("")`` which is equal to ``c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470``), as defined by + `EIP-1052 `_. + .. note:: All contracts can be converted to ``address`` type, so it is possible to query the balance of the current contract using ``address(this).balance``.