14

I would like to explore the storage data of a contract. I have geth fully synched and I interact with it over RPC. There is the function "eth.getStorageAt(addr, index, block)" provided, but you need to pass an index to it. Unfortunately this index doesn't seem to be an incrementing integer as I thought. I tried accessing the storage of the DAO as an example, and you can view the storage of it here: https://live.ether.camp/account/0xbb9bc244d798123fde783fcc1c72d3bb8c189413 (be sure to turn "Smart mode" off). As you can see, the first few indices of the storage are the numbers 0-22 with some missing in between, but then it changes to hashes. Now, if you're running a node, you need to be able to iterate over the full storage, and thus you need to be able to calculate these indices. How would I do that?

tl;dr: I want to iterate over the whole storage of a contract, how would I do that?

11

You need to know the key, and then storage indices are based on sha3 (Keccak-256) of the key. There are further details depending on mapping and dynamically-sized array types and see:

https://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage

Statically-sized variables (everything except mapping and dynamically-sized array types) are laid out contiguously in storage starting from position 0.

...

Due to their unpredictable size, mapping and dynamically-sized array types use a sha3 computation to find the starting position of the value or the array data. These starting positions are always full stack slots.

Same Solidity docs provide further details and the following example:

contract C {
  struct s { uint a; uint b; }
  uint x;
  mapping(uint => mapping(uint => s)) data;
}

The position of data[4][9].b is at sha3(uint256(9) . sha3(uint256(4) . uint256(1))) + 1


Contract data should generally be accessed via the functions it provides; poking at internals is never going to be as easy. There's also no convention that storage indices must be computed the same way that Solidity does.

  • The documentation linked to above really needs some working javascript/web3 examples. It leaves out key implementation details that can only be guessed, which i've tried without success. Would be great if someone could provide working examples for this question. Especially for dynamic arrays and mappings. – slothbag Jun 27 '16 at 23:16
  • 1. There is an example in linked doc 2. Contract data should generally be accessed via the functions it provides; poking at internals is never going to be as easy 3. There is Edit on Github on the upper right for anyone to add further examples. 4. There's no actual convention that indices must be computed the same way that Solidity does – eth Jun 27 '16 at 23:55
  • Thanks, if someone provides a working example i'll gladly submit a PR to the doco on Github. – slothbag Jun 28 '16 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.