What is stored in the Blockchain from a Smart Contract?


I'm an student developing private ethereum Blockchain and I have a question.

When I deploy my Smart Contract on the Blockchain, the full Smart Contract is stored there. Suposing that my smart contract is just a storage of a variable with a setter and a getter, when I call the setter function, what is stored in the blockchain? Only the function setter, or the full smart contract?

I'm working on the voting exemple of ethereum, and i'm worried about the cost in data storage of it.

Thank you and sorry for my bad English


  • o0ragman0oo0ragman0o Member, Moderator Posts: 1,291 mod
    Hi @Drumsjuanma When you deploy a contract, that bytecode goes onto the blockchain as the data of the transaction, which is immutable. Associated with that is the 'state root' which the contract's branch into the state trie, which is a mutable database.
    When you call a contract's function, that transaction and the data associated with it, such as the parameters, is also stored on the blockchain, however any changed that are made by the function to the state variable are store in the state trie.

    State storage is purposefully expensive and there is a gas discount if you delete it. Fundamentally, the state is organised into 256bit slots (as the EVM itself is a 256bit processor). So any variable you create in your contract takes one slot, however there is an optimiser which can pack types of short length together into a single slot. This is most valuable when using structs e.g.

    This struct takes up 3 slots...
    struct Cat
    bytes32 name; // Takes a full 32byte State slot
    uint age; // Takes 256bits (32bytes) Though realy only 8 bits are needed for this datum
    bool gender; // bools take up 8 bits even when packed, but because there's nothing else to pack with wastes a whole 32byte slot
    Using uint8 for age means that age and gender get packed into the one slot and the struct therefore only requires 2.
    struct Cat
    bytes32 name; // Takes a full 32byte State slot
    uint8 age; // Is now packed with gender to only consume 2 slots
    bool gender;
  • DrumsjuanmaDrumsjuanma Member Posts: 6
    Thanks for the answer @o0ragman0o.

    So if my Smart Contract has two variables (X and Y) and I call a setter function to change X, the last position of the state tree would contain only the new value of X, or it will have all the information of all the variables (X and Y in this case)?

    In the first case (if only contains the changed data), how does the state tree works? What is each branch?
  • o0ragman0oo0ragman0o Member, Moderator Posts: 1,291 mod
    Ok, so dragging out my aged printout of Gavin Wood's Yellow Paper updated here, to correct myself, the State trie is also an immutable data structure (a Patricia Merkle Trie), so a `change` to a contracts variable doesn't actually overwrite the older value but creates another branch (like Git). The rehashing of that state yields a new state root which is stored on the blockchain for that account.

    A further correction, the bytecode of a contract is also stored in the state trie, and it is this copy that is executed rather than the bytecode data in the on chain transaction that created it.

    The State trie database itself is a simple key/value mapping but given that it's data is immutable and prone to exponential bloat with age, what's called 'State Trie Pruning' was also implemented in order to keep only the most recent changes.
  • o0ragman0oo0ragman0o Member, Moderator Posts: 1,291 mod
Sign In or Register to comment.