2

Are the keys and values all from the code? will the nonce or balance be the key?

why there is a default value of storageroot since there are always somethings real?

1 Answer 1

1

Summary

The initial storage hash of your contract depends on what your initialization code (typically constructor and instance variables in Solidity) is loading into contract storage. Usually these are all your instance variables, but your contract storage could be empty if you don't have anything to store.

The nonce and balance are will not be your keys. They are stored separately in the contract state.

The values in the contract storage tree will typically come from your code, but the keys are dependent on how you write your EVM bytecode. Solidity uses a storage slots system that counts up with each variable declaration and uses that index as the key.

There is a default value even if your storage is empty because it will be the root hash of the empty trie.


Some Details

According to equation 90 in the most current version (Berlin) of the Ethereum Yellow Paper, the storage root (third element in the tuple below) is the root hash of the empty trie. The nonce and balance are stored separately as the first and second elements of the tuple. Note the empty code hash as the last element in the tuple — the contract has not yet been initialized (with code or anything else).

equation 90 in Ethereum Yellow Paper: Berlin Version

This is only the first step of creating a new contract. When you create a contract, you pass "initialization code" with that CREATE/CREATE2 instruction. The initialization code (denoted i in the paper) is regular EVM bytecode that runs normally right after the contract is created in the empty state. We see this explicitly in equation 94.

equation 94 in Ethereum Yellow Paper: Berlin Version

There are two things we want to take away from this equation, even if we don't understand it fully:

  1. we are executing normal EVM instructions that can change EVM state (including contract storage), and
  2. we get back object code (denoted o) that will be the code for the contract going forward after initialization completes.

We see point number 2 formalized in equation 106.

This means that your initialization code could do whatever it wants to do, including put things in contract storage using SSTORE or basically anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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