Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save cleanunicorn/d7e4eb65013a4aa1ada956ce8e4b57a8 to your computer and use it in GitHub Desktop.

Select an option

Save cleanunicorn/d7e4eb65013a4aa1ada956ce8e4b57a8 to your computer and use it in GitHub Desktop.

Contract

contract Storage {
    bytes arr;
    
    function storeArr(bytes memory _arr) public {
        arr = _arr;    
    }
}

Storing <= 32 bytes

Calling storeArr(0xb0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0) (32 bytes) we get this in the storage.

Slot 0x00 has:

>>> web3.eth.getStorageAt("0x958b09DB711753a68089A0305d4fac915Bc76369", 0x00)
HexBytes('0x41')

And the actual array data is stored at keccak(0x0000000000000000000000000000000000000000000000000000000000000000):

>>> web3.eth.getStorageAt("0x958b09DB711753a68089A0305d4fac915Bc76369", int(keccak(hexstr=("00"*32)).hex(), 16))
HexBytes('0xb0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0')

Storing > 32 bytes

Calling storeArr(0xb1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1) (33 bytes) we get this in the storage.

We get the length of the stored data:

>>> web3.eth.getStorageAt("0x958b09DB711753a68089A0305d4fac915Bc76369", 0x00)
HexBytes('0x43')

The first word containing 32 bytes:

>>> web3.eth.getStorageAt("0x958b09DB711753a68089A0305d4fac915Bc76369", int(keccak(hexstr=("00"*32)).hex(), 16))
HexBytes('0xb1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1')

The second word containing the remaining 1 byte:

>>> web3.eth.getStorageAt("0x958b09DB711753a68089A0305d4fac915Bc76369", int(keccak(hexstr=("00"*32)).hex(), 16) + 1)
HexBytes('0xb100000000000000000000000000000000000000000000000000000000000000')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment