7

Thinking about smart contract storage, how is the data arranged?

I know that every variable within a contract has an unique ID, therefore, it's possible to create a node with a structure like <varID, value> and organize something like:

                                    DATA_ROOT
                                        | 
                    /----------------------------------------\
                   /                                          \
              H(H1 + H2)                                 H(H3 + H4)
                   |                                          |
          /-----------------\                        /-----------------\
         /                   \                      /                   \
H(<varID1, value1>)  H(<varID2, value2>)  H(<varID3, value3>)  H(<varID4, value4>)

Is the above correct?

This leads to this one more question:

Is this tree/trie full structure in the state tree under the node that identifies the contract account that owns this data?

                                 ACCOUNT 0X9127...
                                        |    
                       /----------------|---------------\                  
                      /                 |                \
4817221  -------- BALANCE           DATA_ROOT           CODE ---------- 0xFA21E
                                        | 
                    /----------------------------------------\
                   /                                          \
              H(H1 + H2)                                 H(H3 + H4)
                   |                                          |
          /-----------------\                        /-----------------\
         /                   \                      /                   \
H(<varID1, value1>)  H(<varID2, value2>)  H(<varID3, value3>)  H(<varID4, value4>)

Edit:

Summarizing my question: Is contract storage a sub-tree of the following trie?

State tree

10

Ethereum uses a Merkle-Patricia trie to store its entire state. The same trie is used to store everything, both general state (i.e. what accounts exist, balances, nonces), as well as contract internal state.

For example you can retrieve the current state root of the head block of the chain. Using that as the root of the merkle patricia trie backed by the state database you can retrieve the account object associated with an address. That object will contain various data (e.g. code, balance), but also the hash of the contracts internal state.

You can create a second merkle patricia trie based on this internal state root backed by the same database, and you've accessed the data storage of the contract. A contract's internal storage is basically a key-value store, so you can access any data mapped to a particular key hash, but I've no idea how solidity maps the various storage data fields to unique hashes (my bet is it hashes the names too, among others).

  • thank's for your answer. I know, in the end, the contract internal state it's all about bytecode and data. I'm trying to understand how this low level script is arranged. – Henrique Barcelos Apr 19 '16 at 13:36

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.