I'm having trouble figuring out how variables are stored on the block chain. How can a smart contract be immutable and have global variables that can change?

Just say I a simple contract like below...

contract SimpleStorage {
  uint storedData;

  function set(uint x) {
    storedData = x;

  function get() constant returns (uint) {
    return storedData;

I understand this code will be broken down into byte code and stored on the blockchain. What I don't understand is where the storedData variable will be saved when I set a new value on it. Wouldn't the bytecode on that block need to be updated once I update the value?

4 Answers 4


As I understand it, the "world state" is not actually stored on the chain, only a hash of it. It can be computed by running through the entire transaction history of the blockchain, or more likely just fetched from another node kind enough to share it.

It can be trivially verified as correct though, because the hash of the current world state stored in each new block.

So, the contract bytecode and the storedData variable are both just stored somewhere in the world state.

The reason contract code is immutable, but the variable is not, is that there are opcodes in the EVM that allow changing of the contract's "storage", but none that modify the code. So, there is a valid transaction you can make that will cause every node in the network to update the variable to the new value in their copy of the world state. But there's no valid transaction you can make that will cause other nodes to update the contract code in their world state.

There are more details about this stuff in the formal specification. See section 4 for more information. Specifically, check out the storageRoot and codeHash properties of account state in section 4.1, and the stateRoot property of blocks, in section 4.4.


What's immutable is the contract itself, so to speak. Once deployed, the contract can not be changed, but that doesn't mean its state variables can't change if the contract was designed with the corresponding functions.

If the contract I develop and deploy has a state variable "balance" but no setter function, it won't allow this variable to be modified, not even by me. And being the contract immutable, no one, not even me eill ever be able to modify the contract to add a setter function for "balance" to be changed.


As explained previously by pabloruiz55 and Aaron Sikes. Each contract has a private storage, meaning only the contract has access to write to them. This storage is protected by Merkle Patricia Trees.

The idea behind the algorithm is that the minimum modification of flipping a single bit of storage will generate a different storageRoot and everybody will notice immediately.

Each contract storageRoot is stored in something knows as the "World State", this also includes addresses' balances, contract's code, etc. This "World State" is protected again with a Merkle Patricia Tree. The result after applying all transactions in a block is recorded in the block as stateRoot.

If a third party wants to modify the storage of a contract it has to convince to the majority of the nodes in the Ethereum network that the new state is valid.


A contract has "storage" which is kept separate from its bytecode. Solidity automatically translates variables like that into gets and sets from your contract's storage. See the solidity docs section on it.

You can also read about the world state in section 4.1 of the formal specification

  • 1
    Where does that contract storage live? That memory needs to take up space somewhere. Oct 29, 2017 at 20:29
  • I believe they are both just stored somewhere in the world state. There are opcodes available that allow contract code to modify storage values, but no opcodes that allow it to modify its code. You may also want to read section 4.1 of the formal spec at yellowpaper.io Oct 30, 2017 at 18:11
  • @user2132770 I added a more detailed answer to the question above Nov 1, 2017 at 3:29

Your Answer

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

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