Solidity – How State Variables Are Stored and Updated in the EVM

Architectureblocksevmsoliditystate-variable

The Smart contract and the state variables are stored in the blockchain, let us say during deployment of a smart contract the state variable and smart contracts are stored in the block 1, and in future the state variables are changed by a some transaction, will the variable in the block 1 gets changed ? (If so it will disapprove immutability of the blockchain), or will it create a new block with new state variables and code, then it will be a kind of versioning of smart contracts but it is obvious it is not true.

  1. How does the variable is stored in the blockchain?

  2. What happens when a state variable is changed ?, is changes made to the block where the smart contract is first deployed or new block is created with a smart contract with new value ? or new block is created which just has the transaction history(ie. Value of A changed to 10), if then can I get an Transaction History?

  3. What information is present in the transaction, when an request to change the variable state is place ( only the variable change or the whole smart contract with new values)

Best Answer

The Ethereum blockchain is a state machine. When you define a state variable in a submitted transaction that data is stored and synchronised across the nodes of the network. For example “x is ‘hello’”

When you submit a transaction making a change to that variable you essentially store a message that “x is now ‘goodbye’.” This is synchronised across the nodes.

When you query what that variable is currently your client essentially reruns every transaction and state change to discern what the value is. It says “x was ‘hello’” then it was changed to ‘goodbye’ in block 5. Therefore the value is currently ‘goodbye’.

As such to get the current value you need a fully synced node which contains all the changes to the variable.

Related Topic