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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityThe 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.
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).
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.
There are two things we want to take away from this equation, even if we don't understand it fully:
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.